Skip to content

Commit e53a19c

Browse files
committed
unit test cases fixed
1 parent fb52259 commit e53a19c

File tree

10 files changed

+92
-41
lines changed

10 files changed

+92
-41
lines changed

pkg/splunk/enterprise/clustermanager_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"strings"
2727
"testing"
2828
"time"
29-
29+
"k8s.io/apimachinery/pkg/runtime/schema"
3030
enterpriseApi "github.com/splunk/splunk-operator/api/v4"
3131

3232
appsv1 "k8s.io/api/apps/v1"
@@ -57,6 +57,7 @@ func TestApplyClusterManager(t *testing.T) {
5757
{MetaName: "*v1.Secret-test-splunk-test-secret"},
5858
{MetaName: "*v1.Secret-test-splunk-test-secret"},
5959
{MetaName: "*v1.Secret-test-splunk-test-secret"},
60+
{MetaName: "*v1.ConfigMap-test-splunk-cluster-manager-stack1-configmap"},
6061
{MetaName: "*v1.Service-test-splunk-stack1-cluster-manager-service"},
6162
{MetaName: "*v1.StatefulSet-test-splunk-stack1-cluster-manager"},
6263
{MetaName: "*v1.ConfigMap-test-splunk-test-probe-configmap"},
@@ -97,7 +98,7 @@ func TestApplyClusterManager(t *testing.T) {
9798
}
9899
listmockCall := []spltest.MockFuncCall{
99100
{ListOpts: listOpts}}
100-
createCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Create": {funcCalls[0], funcCalls[3], funcCalls[6], funcCalls[8], funcCalls[4]}, "List": {listmockCall[0]}, "Update": {funcCalls[0]}}
101+
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]}}
101102
updateCalls := map[string][]spltest.MockFuncCall{"Get": updateFuncCalls, "Update": {funcCalls[4]}, "List": {listmockCall[0]}}
102103

103104
current := enterpriseApi.ClusterManager{
@@ -114,6 +115,11 @@ func TestApplyClusterManager(t *testing.T) {
114115
},
115116
},
116117
}
118+
var gvk schema.GroupVersionKind
119+
gvk.Kind = "SearchHead"
120+
gvk.Group = "enterprise.splunk.com"
121+
gvk.Version = "v4"
122+
current.SetGroupVersionKind(gvk)
117123
revised := current.DeepCopy()
118124
revised.Spec.Image = "splunk/test"
119125
reconcile := func(c *spltest.MockClient, cr interface{}) error {

pkg/splunk/enterprise/configuration.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1268,7 +1268,7 @@ func getManualUpdateStatus(ctx context.Context, client splcommon.ControllerClien
12681268
scopedLog.Error(err, "Unable to get namespace specific configMap", "name", configMapName)
12691269
}
12701270

1271-
namespacedName = types.NamespacedName{Namespace: cr.GetNamespace(), Name: fmt.Sprintf("splunk-config-%s", cr.GetName())}
1271+
namespacedName = types.NamespacedName{Namespace: cr.GetNamespace(), Name: fmt.Sprintf(perCrConfigMapNameStr, KindToInstanceString(cr.GroupVersionKind().Kind), cr.GetName())}
12721272
CrconfigMap, err := splctrl.GetConfigMap(ctx, client, namespacedName)
12731273
if err == nil {
12741274
data := CrconfigMap.Data["manualUpdate"]

pkg/splunk/enterprise/finalizers_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ func splunkDeletionTester(t *testing.T, cr splcommon.MetaObject, delete func(spl
140140
switch cr.GetObjectKind().GroupVersionKind().Kind {
141141
case "Standalone":
142142
mockCalls["Get"] = append(mockCalls["Get"], []spltest.MockFuncCall{
143+
//{MetaName: "*v1.ConfigMap-test-splunk-standalone-stack1-configmap"},
144+
{MetaName: "*v1.Secret-test-splunk-test-secret"},
143145
{MetaName: "*v1.StatefulSet-test-splunk-stack1-standalone"},
144146
{MetaName: "*v4.Standalone-test-stack1"},
145147
{MetaName: "*v4.Standalone-test-stack1"},

pkg/splunk/enterprise/names.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ const (
4747
// identifier
4848
probeConfigMapTemplateStr = "splunk-%s-probe-configmap"
4949

50+
// per cr configmap name
51+
perCrConfigMapNameStr = "splunk-%s-%s-configmap"
52+
5053
// livenessScriptName
5154
livenessScriptName = "livenessProbe.sh"
5255

pkg/splunk/enterprise/searchheadcluster.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@ func ApplySearchHeadCluster(ctx context.Context, client splcommon.ControllerClie
109109
cr.Status.AdminPasswordChangedSecrets = make(map[string]bool)
110110
}
111111

112-
113-
114112
// check if deletion has been requested
115113
if cr.ObjectMeta.DeletionTimestamp != nil {
116114
if cr.Spec.MonitoringConsoleRef.Name != "" {

pkg/splunk/enterprise/standalone_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func TestApplyStandalone(t *testing.T) {
6363
{MetaName: "*v1.Secret-test-splunk-test-secret"},
6464
{MetaName: "*v1.Secret-test-splunk-test-secret"},
6565
{MetaName: "*v1.Secret-test-splunk-test-secret"},
66+
{MetaName: "*v1.ConfigMap-test-splunk-standalone-stack1-configmap"},
6667
{MetaName: "*v1.Service-test-splunk-stack1-standalone-headless"},
6768
{MetaName: "*v1.Service-test-splunk-stack1-standalone-service"},
6869
{MetaName: "*v1.StatefulSet-test-splunk-stack1-standalone"},
@@ -80,6 +81,7 @@ func TestApplyStandalone(t *testing.T) {
8081
updatefuncCalls := []spltest.MockFuncCall{
8182
{MetaName: "*v1.Secret-test-splunk-test-secret"},
8283
{MetaName: "*v1.Secret-test-splunk-test-secret"},
84+
{MetaName: "*v1.ConfigMap-test-splunk-standalone-stack1-configmap"},
8385
{MetaName: "*v1.Service-test-splunk-stack1-standalone-headless"},
8486
{MetaName: "*v1.Service-test-splunk-stack1-standalone-service"},
8587
{MetaName: "*v1.StatefulSet-test-splunk-stack1-standalone"},
@@ -110,8 +112,8 @@ func TestApplyStandalone(t *testing.T) {
110112
listmockCall := []spltest.MockFuncCall{
111113
{ListOpts: listOpts}}
112114

113-
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]}}
114-
updateCalls := map[string][]spltest.MockFuncCall{"Get": updateFuncCalls, "Update": {funcCalls[12]}, "List": {listmockCall[0]}}
115+
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]}}
116+
updateCalls := map[string][]spltest.MockFuncCall{"Get": updateFuncCalls, "Update": {funcCalls[13]}, "List": {listmockCall[0]}}
115117
current := enterpriseApi.Standalone{
116118
TypeMeta: metav1.TypeMeta{
117119
Kind: "Standalone",
@@ -204,6 +206,7 @@ func TestApplyStandaloneWithSmartstore(t *testing.T) {
204206
{MetaName: "*v1.ConfigMap-test-splunk-stack1-standalone-smartstore"},
205207
{MetaName: "*v1.Secret-test-splunk-test-secret"},
206208
{MetaName: "*v1.Secret-test-splunk-test-secret"},
209+
{MetaName: "*v1.ConfigMap-test-splunk-standalone-stack1-configmap"},
207210
{MetaName: "*v1.Service-test-splunk-stack1-standalone-headless"},
208211
{MetaName: "*v1.Service-test-splunk-stack1-standalone-service"},
209212
{MetaName: "*v1.StatefulSet-test-splunk-stack1-standalone"},
@@ -227,6 +230,7 @@ func TestApplyStandaloneWithSmartstore(t *testing.T) {
227230
{MetaName: "*v1.ConfigMap-test-splunk-stack1-standalone-smartstore"},
228231
{MetaName: "*v1.Secret-test-splunk-test-secret"},
229232
{MetaName: "*v1.Secret-test-splunk-test-secret"},
233+
{MetaName: "*v1.ConfigMap-test-splunk-standalone-stack1-configmap"},
230234
{MetaName: "*v1.Service-test-splunk-stack1-standalone-headless"},
231235
{MetaName: "*v1.Service-test-splunk-stack1-standalone-service"},
232236
{MetaName: "*v1.StatefulSet-test-splunk-stack1-standalone"},
@@ -253,8 +257,8 @@ func TestApplyStandaloneWithSmartstore(t *testing.T) {
253257
listmockCall := []spltest.MockFuncCall{
254258
{ListOpts: listOpts}}
255259

256-
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]}}
257-
updateCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Update": {funcCalls[8]}, "List": {listmockCall[0]}}
260+
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]}}
261+
updateCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Update": {funcCalls[9]}, "List": {listmockCall[0]}}
258262

259263
current := enterpriseApi.Standalone{
260264
TypeMeta: metav1.TypeMeta{

pkg/splunk/enterprise/types.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,27 @@ func (instanceType InstanceType) ToKind() string {
273273
}
274274
return kind
275275
}
276+
277+
func KindToInstanceString(kind string) string {
278+
switch kind {
279+
case "Standalone":
280+
return SplunkStandalone.ToString()
281+
case "IndexerCluster":
282+
return SplunkClusterMaster.ToString()
283+
case "ClustetrManager":
284+
return SplunkClusterManager.ToString()
285+
case "LicenseMaster":
286+
return SplunkIndexer.ToString()
287+
case "ClusterMaster":
288+
return SplunkClusterMaster.ToString()
289+
case "LicenseManager":
290+
return SplunkLicenseManager.ToString()
291+
case "MonitoringConsole":
292+
return SplunkMonitoringConsole.ToString()
293+
case "SearchHeadCluster":
294+
return SplunkSearchHead.ToString()
295+
case "SearchHead":
296+
return SplunkSearchHead.ToString()
297+
}
298+
return ""
299+
}

pkg/splunk/enterprise/util.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func ReconcileCRSpecificConfigMap(ctx context.Context, client splcommon.Controll
243243
reqLogger := log.FromContext(ctx)
244244
scopedLog := reqLogger.WithName("ReconcileCRSpecificConfigMap").WithValues("name", cr.GetName(), "namespace", cr.GetNamespace())
245245

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

249249
configMap, err := splctrl.GetConfigMap(ctx, client, namespacedName)
@@ -1472,7 +1472,7 @@ func shouldCheckAppRepoStatus(ctx context.Context, client splcommon.ControllerCl
14721472
}
14731473
return true
14741474
} else {
1475-
configMapName := fmt.Sprintf("splunk-config-%s", cr.GetName())
1475+
configMapName := fmt.Sprintf(perCrConfigMapNameStr, KindToInstanceString(cr.GroupVersionKind().Kind), cr.GetName())
14761476
if getManualUpdateStatus(ctx, client, cr, configMapName) == "on" {
14771477
*turnOffManualChecking = true
14781478
return true
@@ -1562,7 +1562,7 @@ func updateManualAppUpdateConfigMapLocked(ctx context.Context, client splcommon.
15621562
}
15631563

15641564
// now check namespace specific configmap if it contains manualUpdate settings
1565-
crScopedConfigMapName := fmt.Sprintf("splunk-config-%s", cr.GetName())
1565+
crScopedConfigMapName := fmt.Sprintf(perCrConfigMapNameStr, KindToInstanceString(cr.GroupVersionKind().Kind), cr.GetName())
15661566
crNamespacedName := types.NamespacedName{Namespace: cr.GetNamespace(), Name: crScopedConfigMapName}
15671567
configMap, err := splctrl.GetConfigMap(ctx, client, crNamespacedName)
15681568
if err != nil {

pkg/splunk/enterprise/util_test.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"k8s.io/apimachinery/pkg/types"
3434
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3535
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
36+
"k8s.io/apimachinery/pkg/runtime/schema"
3637
"sigs.k8s.io/controller-runtime/pkg/client/fake"
3738
"sigs.k8s.io/controller-runtime/pkg/log"
3839
"sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -222,9 +223,10 @@ func TestApplySplunkConfig(t *testing.T) {
222223
{MetaName: "*v1.Secret-test-splunk-test-secret"},
223224
{MetaName: "*v1.ConfigMap-test-splunk-stack1-search-head-defaults"},
224225
{MetaName: "*v1.ConfigMap-test-splunk-stack1-search-head-defaults"},
226+
{MetaName: "*v1.ConfigMap-test-splunk-search-head-stack1-configmap"},
225227
}
226-
createCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Create": {funcCalls[0], funcCalls[3]}, "Update": {funcCalls[0]}}
227-
updateCalls := map[string][]spltest.MockFuncCall{"Get": {funcCalls[0], funcCalls[1], funcCalls[3]}}
228+
createCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Create": {funcCalls[0], funcCalls[3], funcCalls[5]}, "Update": {funcCalls[0]}}
229+
updateCalls := map[string][]spltest.MockFuncCall{"Get": {funcCalls[0], funcCalls[1], funcCalls[3],funcCalls[5] }}
228230
searchHeadCR := enterpriseApi.SearchHeadCluster{
229231
TypeMeta: metav1.TypeMeta{
230232
Kind: "SearcHead",
@@ -234,6 +236,11 @@ func TestApplySplunkConfig(t *testing.T) {
234236
Namespace: "test",
235237
},
236238
}
239+
var gvk schema.GroupVersionKind
240+
gvk.Kind = "SearchHead"
241+
gvk.Group = "enterprise.splunk.com"
242+
gvk.Version = "v4"
243+
searchHeadCR.SetGroupVersionKind(gvk)
237244
searchHeadCR.Spec.Defaults = "defaults-yaml"
238245
searchHeadRevised := searchHeadCR.DeepCopy()
239246
searchHeadRevised.Spec.Image = "splunk/test"
@@ -268,9 +275,10 @@ func TestApplySplunkConfig(t *testing.T) {
268275
}
269276
funcCalls = []spltest.MockFuncCall{
270277
{MetaName: "*v1.Secret-test-splunk-test-secret"},
278+
{MetaName: "*v1.ConfigMap-test-splunk-cluster-master-stack1-configmap"},
271279
}
272-
createCalls = map[string][]spltest.MockFuncCall{"Get": {funcCalls[0], funcCalls[0], funcCalls[0]}, "Create": funcCalls, "Update": {funcCalls[0]}}
273-
updateCalls = map[string][]spltest.MockFuncCall{"Get": {funcCalls[0], funcCalls[0]}}
280+
createCalls = map[string][]spltest.MockFuncCall{"Get": {funcCalls[0], funcCalls[0], funcCalls[0], funcCalls[1]}, "Create": funcCalls, "Update": {funcCalls[0]}}
281+
updateCalls = map[string][]spltest.MockFuncCall{"Get": {funcCalls[0], funcCalls[0], funcCalls[1]}}
274282

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

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

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

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

1342-
crKindMap := make(map[string]string)
1343-
configMapData := `status: on
1344-
refCount: 1`
1345-
crKindMap[cr.GetObjectKind().GroupVersionKind().Kind] = configMapData
1346-
1347-
configMap := splctrl.PrepareConfigMap(GetSplunkManualAppUpdateConfigMapName(cr.GetNamespace()), cr.GetNamespace(), crKindMap)
1348-
13491350
// Test1: with no confiMap added, we should return error
13501351
err := updateManualAppUpdateConfigMapLocked(ctx, c, &cr, appStatusContext, kind, turnOffManualChecking)
13511352
if err == nil {
13521353
t.Errorf("updateManualAppUpdateConfigMapLocked should have returned error since there is no configMap yet.")
13531354
}
13541355

1356+
crKindMap := make(map[string]string)
1357+
crKindMap["manualUpdate"] = "off"
1358+
crConfigMap := splctrl.PrepareConfigMap(fmt.Sprintf(perCrConfigMapNameStr, KindToInstanceString(cr.GroupVersionKind().Kind), cr.GetName()), cr.GetNamespace(), crKindMap)
1359+
1360+
// now add the confiMap to the client
1361+
c.AddObject(crConfigMap)
1362+
1363+
crKindMap = make(map[string]string)
1364+
configMapData := `status: on
1365+
refCount: 1`
1366+
crKindMap[cr.GetObjectKind().GroupVersionKind().Kind] = configMapData
1367+
configMap := splctrl.PrepareConfigMap(GetSplunkManualAppUpdateConfigMapName(cr.GetNamespace()), cr.GetNamespace(), crKindMap)
1368+
13551369
// now add the confiMap to the client
13561370
c.AddObject(configMap)
13571371

0 commit comments

Comments
 (0)