Skip to content

Commit

Permalink
unit test cases fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
vivekr-splunk committed Nov 21, 2024
1 parent fb52259 commit e53a19c
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 41 deletions.
10 changes: 8 additions & 2 deletions pkg/splunk/enterprise/clustermanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"strings"
"testing"
"time"

"k8s.io/apimachinery/pkg/runtime/schema"
enterpriseApi "github.com/splunk/splunk-operator/api/v4"

appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -57,6 +57,7 @@ func TestApplyClusterManager(t *testing.T) {
{MetaName: "*v1.Secret-test-splunk-test-secret"},
{MetaName: "*v1.Secret-test-splunk-test-secret"},
{MetaName: "*v1.Secret-test-splunk-test-secret"},
{MetaName: "*v1.ConfigMap-test-splunk-cluster-manager-stack1-configmap"},
{MetaName: "*v1.Service-test-splunk-stack1-cluster-manager-service"},
{MetaName: "*v1.StatefulSet-test-splunk-stack1-cluster-manager"},
{MetaName: "*v1.ConfigMap-test-splunk-test-probe-configmap"},
Expand Down Expand Up @@ -97,7 +98,7 @@ func TestApplyClusterManager(t *testing.T) {
}
listmockCall := []spltest.MockFuncCall{
{ListOpts: listOpts}}
createCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Create": {funcCalls[0], funcCalls[3], funcCalls[6], funcCalls[8], funcCalls[4]}, "List": {listmockCall[0]}, "Update": {funcCalls[0]}}
createCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Create": {funcCalls[0], funcCalls[3], funcCalls[3], funcCalls[6], funcCalls[8], funcCalls[4]}, "List": {listmockCall[0]}, "Update": {funcCalls[0]}}
updateCalls := map[string][]spltest.MockFuncCall{"Get": updateFuncCalls, "Update": {funcCalls[4]}, "List": {listmockCall[0]}}

current := enterpriseApi.ClusterManager{
Expand All @@ -114,6 +115,11 @@ func TestApplyClusterManager(t *testing.T) {
},
},
}
var gvk schema.GroupVersionKind
gvk.Kind = "SearchHead"
gvk.Group = "enterprise.splunk.com"
gvk.Version = "v4"
current.SetGroupVersionKind(gvk)
revised := current.DeepCopy()
revised.Spec.Image = "splunk/test"
reconcile := func(c *spltest.MockClient, cr interface{}) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/splunk/enterprise/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ func getManualUpdateStatus(ctx context.Context, client splcommon.ControllerClien
scopedLog.Error(err, "Unable to get namespace specific configMap", "name", configMapName)
}

namespacedName = types.NamespacedName{Namespace: cr.GetNamespace(), Name: fmt.Sprintf("splunk-config-%s", cr.GetName())}
namespacedName = types.NamespacedName{Namespace: cr.GetNamespace(), Name: fmt.Sprintf(perCrConfigMapNameStr, KindToInstanceString(cr.GroupVersionKind().Kind), cr.GetName())}
CrconfigMap, err := splctrl.GetConfigMap(ctx, client, namespacedName)
if err == nil {
data := CrconfigMap.Data["manualUpdate"]
Expand Down
2 changes: 2 additions & 0 deletions pkg/splunk/enterprise/finalizers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ func splunkDeletionTester(t *testing.T, cr splcommon.MetaObject, delete func(spl
switch cr.GetObjectKind().GroupVersionKind().Kind {
case "Standalone":
mockCalls["Get"] = append(mockCalls["Get"], []spltest.MockFuncCall{
//{MetaName: "*v1.ConfigMap-test-splunk-standalone-stack1-configmap"},
{MetaName: "*v1.Secret-test-splunk-test-secret"},
{MetaName: "*v1.StatefulSet-test-splunk-stack1-standalone"},
{MetaName: "*v4.Standalone-test-stack1"},
{MetaName: "*v4.Standalone-test-stack1"},
Expand Down
3 changes: 3 additions & 0 deletions pkg/splunk/enterprise/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ const (
// identifier
probeConfigMapTemplateStr = "splunk-%s-probe-configmap"

// per cr configmap name
perCrConfigMapNameStr = "splunk-%s-%s-configmap"

// livenessScriptName
livenessScriptName = "livenessProbe.sh"

Expand Down
2 changes: 0 additions & 2 deletions pkg/splunk/enterprise/searchheadcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ func ApplySearchHeadCluster(ctx context.Context, client splcommon.ControllerClie
cr.Status.AdminPasswordChangedSecrets = make(map[string]bool)
}



// check if deletion has been requested
if cr.ObjectMeta.DeletionTimestamp != nil {
if cr.Spec.MonitoringConsoleRef.Name != "" {
Expand Down
12 changes: 8 additions & 4 deletions pkg/splunk/enterprise/standalone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func TestApplyStandalone(t *testing.T) {
{MetaName: "*v1.Secret-test-splunk-test-secret"},
{MetaName: "*v1.Secret-test-splunk-test-secret"},
{MetaName: "*v1.Secret-test-splunk-test-secret"},
{MetaName: "*v1.ConfigMap-test-splunk-standalone-stack1-configmap"},
{MetaName: "*v1.Service-test-splunk-stack1-standalone-headless"},
{MetaName: "*v1.Service-test-splunk-stack1-standalone-service"},
{MetaName: "*v1.StatefulSet-test-splunk-stack1-standalone"},
Expand All @@ -80,6 +81,7 @@ func TestApplyStandalone(t *testing.T) {
updatefuncCalls := []spltest.MockFuncCall{
{MetaName: "*v1.Secret-test-splunk-test-secret"},
{MetaName: "*v1.Secret-test-splunk-test-secret"},
{MetaName: "*v1.ConfigMap-test-splunk-standalone-stack1-configmap"},
{MetaName: "*v1.Service-test-splunk-stack1-standalone-headless"},
{MetaName: "*v1.Service-test-splunk-stack1-standalone-service"},
{MetaName: "*v1.StatefulSet-test-splunk-stack1-standalone"},
Expand Down Expand Up @@ -110,8 +112,8 @@ func TestApplyStandalone(t *testing.T) {
listmockCall := []spltest.MockFuncCall{
{ListOpts: listOpts}}

createCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Create": {funcCalls[0], funcCalls[3], funcCalls[4], funcCalls[7], funcCalls[9], funcCalls[12]}, "Update": {funcCalls[0]}, "List": {listmockCall[0]}}
updateCalls := map[string][]spltest.MockFuncCall{"Get": updateFuncCalls, "Update": {funcCalls[12]}, "List": {listmockCall[0]}}
createCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Create": {funcCalls[0], funcCalls[3], funcCalls[4], funcCalls[5], funcCalls[8], funcCalls[10], funcCalls[13]}, "Update": {funcCalls[0]}, "List": {listmockCall[0]}}
updateCalls := map[string][]spltest.MockFuncCall{"Get": updateFuncCalls, "Update": {funcCalls[13]}, "List": {listmockCall[0]}}
current := enterpriseApi.Standalone{
TypeMeta: metav1.TypeMeta{
Kind: "Standalone",
Expand Down Expand Up @@ -204,6 +206,7 @@ func TestApplyStandaloneWithSmartstore(t *testing.T) {
{MetaName: "*v1.ConfigMap-test-splunk-stack1-standalone-smartstore"},
{MetaName: "*v1.Secret-test-splunk-test-secret"},
{MetaName: "*v1.Secret-test-splunk-test-secret"},
{MetaName: "*v1.ConfigMap-test-splunk-standalone-stack1-configmap"},
{MetaName: "*v1.Service-test-splunk-stack1-standalone-headless"},
{MetaName: "*v1.Service-test-splunk-stack1-standalone-service"},
{MetaName: "*v1.StatefulSet-test-splunk-stack1-standalone"},
Expand All @@ -227,6 +230,7 @@ func TestApplyStandaloneWithSmartstore(t *testing.T) {
{MetaName: "*v1.ConfigMap-test-splunk-stack1-standalone-smartstore"},
{MetaName: "*v1.Secret-test-splunk-test-secret"},
{MetaName: "*v1.Secret-test-splunk-test-secret"},
{MetaName: "*v1.ConfigMap-test-splunk-standalone-stack1-configmap"},
{MetaName: "*v1.Service-test-splunk-stack1-standalone-headless"},
{MetaName: "*v1.Service-test-splunk-stack1-standalone-service"},
{MetaName: "*v1.StatefulSet-test-splunk-stack1-standalone"},
Expand All @@ -253,8 +257,8 @@ func TestApplyStandaloneWithSmartstore(t *testing.T) {
listmockCall := []spltest.MockFuncCall{
{ListOpts: listOpts}}

createCalls := map[string][]spltest.MockFuncCall{"Get": createFuncCalls, "Create": {funcCalls[2], funcCalls[6], funcCalls[7], funcCalls[9], funcCalls[11], funcCalls[14]}, "Update": {funcCalls[0]}, "List": {listmockCall[0]}}
updateCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Update": {funcCalls[8]}, "List": {listmockCall[0]}}
createCalls := map[string][]spltest.MockFuncCall{"Get": createFuncCalls, "Create": {funcCalls[2],funcCalls[6], funcCalls[7], funcCalls[8], funcCalls[10], funcCalls[12], funcCalls[15]}, "Update": {funcCalls[0]}, "List": {listmockCall[0]}}
updateCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Update": {funcCalls[9]}, "List": {listmockCall[0]}}

current := enterpriseApi.Standalone{
TypeMeta: metav1.TypeMeta{
Expand Down
24 changes: 24 additions & 0 deletions pkg/splunk/enterprise/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,27 @@ func (instanceType InstanceType) ToKind() string {
}
return kind
}

func KindToInstanceString(kind string) string {
switch kind {
case "Standalone":
return SplunkStandalone.ToString()
case "IndexerCluster":
return SplunkClusterMaster.ToString()
case "ClustetrManager":
return SplunkClusterManager.ToString()
case "LicenseMaster":
return SplunkIndexer.ToString()
case "ClusterMaster":
return SplunkClusterMaster.ToString()
case "LicenseManager":
return SplunkLicenseManager.ToString()
case "MonitoringConsole":
return SplunkMonitoringConsole.ToString()
case "SearchHeadCluster":
return SplunkSearchHead.ToString()
case "SearchHead":
return SplunkSearchHead.ToString()
}
return ""
}
6 changes: 3 additions & 3 deletions pkg/splunk/enterprise/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func ReconcileCRSpecificConfigMap(ctx context.Context, client splcommon.Controll
reqLogger := log.FromContext(ctx)
scopedLog := reqLogger.WithName("ReconcileCRSpecificConfigMap").WithValues("name", cr.GetName(), "namespace", cr.GetNamespace())

configMapName := fmt.Sprintf("splunk-config-%s", cr.GetName())
configMapName := fmt.Sprintf(perCrConfigMapNameStr, KindToInstanceString(cr.GroupVersionKind().Kind), cr.GetName())
namespacedName := types.NamespacedName{Namespace: cr.GetNamespace(), Name: configMapName}

configMap, err := splctrl.GetConfigMap(ctx, client, namespacedName)
Expand Down Expand Up @@ -1472,7 +1472,7 @@ func shouldCheckAppRepoStatus(ctx context.Context, client splcommon.ControllerCl
}
return true
} else {
configMapName := fmt.Sprintf("splunk-config-%s", cr.GetName())
configMapName := fmt.Sprintf(perCrConfigMapNameStr, KindToInstanceString(cr.GroupVersionKind().Kind), cr.GetName())
if getManualUpdateStatus(ctx, client, cr, configMapName) == "on" {
*turnOffManualChecking = true
return true
Expand Down Expand Up @@ -1562,7 +1562,7 @@ func updateManualAppUpdateConfigMapLocked(ctx context.Context, client splcommon.
}

// now check namespace specific configmap if it contains manualUpdate settings
crScopedConfigMapName := fmt.Sprintf("splunk-config-%s", cr.GetName())
crScopedConfigMapName := fmt.Sprintf(perCrConfigMapNameStr, KindToInstanceString(cr.GroupVersionKind().Kind), cr.GetName())
crNamespacedName := types.NamespacedName{Namespace: cr.GetNamespace(), Name: crScopedConfigMapName}
configMap, err := splctrl.GetConfigMap(ctx, client, crNamespacedName)
if err != nil {
Expand Down
40 changes: 27 additions & 13 deletions pkg/splunk/enterprise/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
Expand Down Expand Up @@ -222,9 +223,10 @@ func TestApplySplunkConfig(t *testing.T) {
{MetaName: "*v1.Secret-test-splunk-test-secret"},
{MetaName: "*v1.ConfigMap-test-splunk-stack1-search-head-defaults"},
{MetaName: "*v1.ConfigMap-test-splunk-stack1-search-head-defaults"},
{MetaName: "*v1.ConfigMap-test-splunk-search-head-stack1-configmap"},
}
createCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Create": {funcCalls[0], funcCalls[3]}, "Update": {funcCalls[0]}}
updateCalls := map[string][]spltest.MockFuncCall{"Get": {funcCalls[0], funcCalls[1], funcCalls[3]}}
createCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Create": {funcCalls[0], funcCalls[3], funcCalls[5]}, "Update": {funcCalls[0]}}
updateCalls := map[string][]spltest.MockFuncCall{"Get": {funcCalls[0], funcCalls[1], funcCalls[3],funcCalls[5] }}
searchHeadCR := enterpriseApi.SearchHeadCluster{
TypeMeta: metav1.TypeMeta{
Kind: "SearcHead",
Expand All @@ -234,6 +236,11 @@ func TestApplySplunkConfig(t *testing.T) {
Namespace: "test",
},
}
var gvk schema.GroupVersionKind
gvk.Kind = "SearchHead"
gvk.Group = "enterprise.splunk.com"
gvk.Version = "v4"
searchHeadCR.SetGroupVersionKind(gvk)
searchHeadCR.Spec.Defaults = "defaults-yaml"
searchHeadRevised := searchHeadCR.DeepCopy()
searchHeadRevised.Spec.Image = "splunk/test"
Expand Down Expand Up @@ -268,9 +275,10 @@ func TestApplySplunkConfig(t *testing.T) {
}
funcCalls = []spltest.MockFuncCall{
{MetaName: "*v1.Secret-test-splunk-test-secret"},
{MetaName: "*v1.ConfigMap-test-splunk-cluster-master-stack1-configmap"},
}
createCalls = map[string][]spltest.MockFuncCall{"Get": {funcCalls[0], funcCalls[0], funcCalls[0]}, "Create": funcCalls, "Update": {funcCalls[0]}}
updateCalls = map[string][]spltest.MockFuncCall{"Get": {funcCalls[0], funcCalls[0]}}
createCalls = map[string][]spltest.MockFuncCall{"Get": {funcCalls[0], funcCalls[0], funcCalls[0], funcCalls[1]}, "Create": funcCalls, "Update": {funcCalls[0]}}
updateCalls = map[string][]spltest.MockFuncCall{"Get": {funcCalls[0], funcCalls[0], funcCalls[1]}}

spltest.ReconcileTesterWithoutRedundantCheck(t, "TestApplySplunkConfig", &indexerCR, indexerRevised, createCalls, updateCalls, reconcile, false)

Expand Down Expand Up @@ -695,7 +703,7 @@ func TestInitAndCheckAppInfoStatusShouldNotFail(t *testing.T) {

crConfigMap1 := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("splunk-config-%s", cr.Name),
Name: fmt.Sprintf(perCrConfigMapNameStr, KindToInstanceString(cr.Kind), cr.Name),
Namespace: cr.GetNamespace(),
},
Data: map[string]string{
Expand All @@ -714,7 +722,7 @@ func TestInitAndCheckAppInfoStatusShouldNotFail(t *testing.T) {

crConfigMap2 := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("splunk-config-%s", revised.Name),
Name: fmt.Sprintf(perCrConfigMapNameStr, KindToInstanceString(revised.Kind), revised.Name),
Namespace: cr.GetNamespace(),
},
Data: map[string]string{
Expand Down Expand Up @@ -1339,19 +1347,25 @@ func TestUpdateManualAppUpdateConfigMapLocked(t *testing.T) {
kind := cr.GetObjectKind().GroupVersionKind().Kind
var turnOffManualChecking bool

crKindMap := make(map[string]string)
configMapData := `status: on
refCount: 1`
crKindMap[cr.GetObjectKind().GroupVersionKind().Kind] = configMapData

configMap := splctrl.PrepareConfigMap(GetSplunkManualAppUpdateConfigMapName(cr.GetNamespace()), cr.GetNamespace(), crKindMap)

// Test1: with no confiMap added, we should return error
err := updateManualAppUpdateConfigMapLocked(ctx, c, &cr, appStatusContext, kind, turnOffManualChecking)
if err == nil {
t.Errorf("updateManualAppUpdateConfigMapLocked should have returned error since there is no configMap yet.")
}

crKindMap := make(map[string]string)
crKindMap["manualUpdate"] = "off"
crConfigMap := splctrl.PrepareConfigMap(fmt.Sprintf(perCrConfigMapNameStr, KindToInstanceString(cr.GroupVersionKind().Kind), cr.GetName()), cr.GetNamespace(), crKindMap)

// now add the confiMap to the client
c.AddObject(crConfigMap)

crKindMap = make(map[string]string)
configMapData := `status: on
refCount: 1`
crKindMap[cr.GetObjectKind().GroupVersionKind().Kind] = configMapData
configMap := splctrl.PrepareConfigMap(GetSplunkManualAppUpdateConfigMapName(cr.GetNamespace()), cr.GetNamespace(), crKindMap)

// now add the confiMap to the client
c.AddObject(configMap)

Expand Down
Loading

0 comments on commit e53a19c

Please sign in to comment.