Skip to content

Commit

Permalink
Add tests for additional files. Delete unused methods
Browse files Browse the repository at this point in the history
Signed-off-by: vtiwari5 <[email protected]>
  • Loading branch information
vtiwari5 committed Jan 7, 2025
1 parent c826838 commit 33c039e
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 89 deletions.
13 changes: 0 additions & 13 deletions admiral/pkg/apis/admiral/filters/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,3 @@ func Logger(inner http.Handler, name string) http.Handler {
)
})
}

func Auth(inner http.Handler, name string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

log.Printf(
"Auth Logger for endpoint %s", name,
)
//TODO implement authnz

inner.ServeHTTP(w, r)

})
}
86 changes: 86 additions & 0 deletions admiral/pkg/controller/secret/secretcontroller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package secret
import (
"context"
"fmt"
"k8s.io/client-go/kubernetes"
"reflect"
"sync"
"testing"
Expand Down Expand Up @@ -547,3 +548,88 @@ func TestRemoveClusterFromShard(t *testing.T) {
})
}
}

func TestNewClustersStore(t *testing.T) {
t.Parallel()
store := newClustersStore()
assert.NotNil(t, store)
}

// Initializes a new Controller with default secret resolver when admiralProfile is "default"
func TestNewControllerWithDefaultProfile(t *testing.T) {
kubeclientset := &kubernetes.Clientset{}
namespace := "test-namespace"
cs := &ClusterStore{}
addCallback := func(config *rest.Config, dataKey string, resyncPeriod util.ResyncIntervals) error { return nil }
updateCallback := func(config *rest.Config, dataKey string, resyncPeriod util.ResyncIntervals) error { return nil }
removeCallback := func(dataKey string) error { return nil }
admiralProfile := common.AdmiralProfileDefault
secretResolverConfig := ""

controller := NewController(kubeclientset, namespace, cs, addCallback, updateCallback, removeCallback, admiralProfile, secretResolverConfig)

if controller == nil {
t.Fatalf("Expected controller to be initialized, got nil")
}

if controller.secretResolver == nil {
t.Fatalf("Expected secret resolver to be initialized, got nil")
}
}

// Handles unrecognized admiralProfile by logging an error and returning nil
func TestNewControllerWithUnrecognizedProfile(t *testing.T) {
kubeclientset := &kubernetes.Clientset{}
namespace := "test-namespace"
cs := &ClusterStore{}
addCallback := func(config *rest.Config, dataKey string, resyncPeriod util.ResyncIntervals) error { return nil }
updateCallback := func(config *rest.Config, dataKey string, resyncPeriod util.ResyncIntervals) error { return nil }
removeCallback := func(dataKey string) error { return nil }
admiralProfile := "unknown-profile"
secretResolverConfig := ""

controller := NewController(kubeclientset, namespace, cs, addCallback, updateCallback, removeCallback, admiralProfile, secretResolverConfig)

if controller != nil {
t.Fatalf("Expected controller to be nil for unrecognized profile, got non-nil")
}
}

// Successfully initializes a new Controller with valid parameters
func TestStartSecretControllerInitialization(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

mockK8s := &kubernetes.Clientset{}
addCallback := func(config *rest.Config, dataKey string, resyncPeriod util.ResyncIntervals) error { return nil }
updateCallback := func(config *rest.Config, dataKey string, resyncPeriod util.ResyncIntervals) error { return nil }
removeCallback := func(dataKey string) error { return nil }
namespace := "default"
admiralProfile := common.AdmiralProfileDefault
secretResolverConfig := ""

controller, err := StartSecretController(ctx, mockK8s, addCallback, updateCallback, removeCallback, namespace, admiralProfile, secretResolverConfig)

if err != nil {
t.Fatalf("Expected no error, got %v", err)
}

if controller == nil {
t.Fatalf("Expected controller to be initialized, got nil")
}
}

// Returns error when kubeConfig is empty
func TestCreateRemoteClusterWithEmptyKubeConfig(t *testing.T) {
// Arrange
controller := &Controller{}

// Act
remoteCluster, restConfig, err := controller.createRemoteCluster([]byte(""), "secretName", "clusterID", "namespace")

// Assert
assert.Nil(t, remoteCluster)
assert.Nil(t, restConfig)
assert.Error(t, err)
assert.Equal(t, "kubeconfig is empty", err.Error())
}
49 changes: 49 additions & 0 deletions admiral/pkg/controller/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package util

import (
"bytes"
"github.com/sirupsen/logrus/hooks/test"
"reflect"
"strings"
"testing"
"time"

log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -149,3 +152,49 @@ func TestLogElapsedTime(t *testing.T) {
assert.Contains(t, buf.String(), "op=test_op identity=test_identity env=test_env cluster=test_clusterId txTime=")
log.SetOutput(oldOut)
}

// Logs the elapsed time in milliseconds after execution
func TestLogElapsedTimeController(t *testing.T) {
logger := log.NewEntry(log.New())
logMessage := "Test operation"

logFunc := LogElapsedTimeController(logger, logMessage)

// Simulate some operation
time.Sleep(100 * time.Millisecond)

// Capture the log output
hook := test.NewLocal(logger.Logger)
logFunc()

if len(hook.Entries) != 1 {
t.Fatalf("Expected one log entry, got %d", len(hook.Entries))
}

entry := hook.LastEntry()
if !strings.Contains(entry.Message, logMessage) {
t.Errorf("Expected log message to contain %q, got %q", logMessage, entry.Message)
}

if !strings.Contains(entry.Message, "txTime=") {
t.Errorf("Expected log message to contain elapsed time, got %q", entry.Message)
}
}

// Function returns a closure that logs elapsed time
func TestLogElapsedTimeForTaskClosure(t *testing.T) {
logger := log.NewEntry(log.New())
op := "operation"
name := "taskName"
namespace := "default"
cluster := "cluster1"
message := "test message"

logFunc := LogElapsedTimeForTask(logger, op, name, namespace, cluster, message)

if logFunc == nil {
t.Error("Expected a non-nil closure function")
}

logFunc()
}
15 changes: 0 additions & 15 deletions admiral/pkg/registry/rolloutConfig_parser.go

This file was deleted.

61 changes: 0 additions & 61 deletions admiral/pkg/test/eventually.go

This file was deleted.

0 comments on commit 33c039e

Please sign in to comment.