From 6f87d39511c90533734eecc7e3c8e8ca169cce6d Mon Sep 17 00:00:00 2001 From: Vivek Reddy Date: Mon, 22 Jul 2024 11:57:10 -0700 Subject: [PATCH] failing mc unit test Signed-off-by: Vivek Reddy --- .../enterprise/monitoringconsole_test.go | 37 +++++++++-- pkg/splunk/enterprise/upgrade.go | 6 +- pkg/splunk/enterprise/upgrade_test.go | 64 ++++++++++--------- 3 files changed, 69 insertions(+), 38 deletions(-) diff --git a/pkg/splunk/enterprise/monitoringconsole_test.go b/pkg/splunk/enterprise/monitoringconsole_test.go index 07459c2a7..a40fd0233 100644 --- a/pkg/splunk/enterprise/monitoringconsole_test.go +++ b/pkg/splunk/enterprise/monitoringconsole_test.go @@ -58,6 +58,26 @@ func init() { func TestApplyMonitoringConsole(t *testing.T) { funcCalls := []spltest.MockFuncCall{ + {MetaName: "*v1.Secret-test-splunk-test-secret"}, + {MetaName: "*v1.Secret-test-splunk-test-secret"}, + {MetaName: "*v1.Secret-test-splunk-test-secret"}, + {MetaName: "*v1.Service-test-splunk-stack1-monitoring-console-headless"}, + {MetaName: "*v1.Service-test-splunk-stack1-monitoring-console-service"}, + {MetaName: "*v1.StatefulSet-test-splunk-stack1-monitoring-console"}, + {MetaName: "*v1.ConfigMap-test-splunk-test-probe-configmap"}, + {MetaName: "*v1.ConfigMap-test-splunk-test-probe-configmap"}, + {MetaName: "*v1.Secret-test-splunk-test-secret"}, + {MetaName: "*v1.Secret-test-splunk-stack1-monitoring-console-secret-v1"}, + {MetaName: "*v1.ConfigMap-test-splunk-stack1-monitoring-console"}, + {MetaName: "*v1.ConfigMap-test-splunk-stack1-monitoring-console"}, + {MetaName: "*v1.ConfigMap-test-splunk-stack1-monitoring-console"}, + //{MetaName: "*v1.StatefulSet-test-splunk-stack1-monitoring-console"}, + //{MetaName: "*v1.StatefulSet-test-splunk-stack1-monitoring-console"}, + {MetaName: "*v4.MonitoringConsole-test-stack1"}, + {MetaName: "*v4.MonitoringConsole-test-stack1"}, + } + + createFuncCalls := []spltest.MockFuncCall{ {MetaName: "*v1.Secret-test-splunk-test-secret"}, {MetaName: "*v1.Secret-test-splunk-test-secret"}, {MetaName: "*v1.Secret-test-splunk-test-secret"}, @@ -91,9 +111,9 @@ func TestApplyMonitoringConsole(t *testing.T) { {MetaName: "*v1.ConfigMap-test-splunk-stack1-monitoring-console"}, {MetaName: "*v1.ConfigMap-test-splunk-stack1-monitoring-console"}, {MetaName: "*v1.ConfigMap-test-splunk-stack1-monitoring-console"}, - {MetaName: "*v1.StatefulSet-test-splunk-stack1-monitoring-console"}, - {MetaName: "*v1.StatefulSet-test-splunk-stack1-monitoring-console"}, - {MetaName: "*v1.StatefulSet-test-splunk-stack1-monitoring-console"}, + //{MetaName: "*v1.StatefulSet-test-splunk-stack1-monitoring-console"}, + //{MetaName: "*v1.StatefulSet-test-splunk-stack1-monitoring-console"}, + //{MetaName: "*v1.StatefulSet-test-splunk-stack1-monitoring-console"}, {MetaName: "*v4.MonitoringConsole-test-stack1"}, {MetaName: "*v4.MonitoringConsole-test-stack1"}, @@ -103,15 +123,20 @@ func TestApplyMonitoringConsole(t *testing.T) { "app.kubernetes.io/component": "versionedSecrets", "app.kubernetes.io/managed-by": "splunk-operator", } + listOpts := []client.ListOption{ client.InNamespace("test"), client.MatchingLabels(labels), } + listmockCall := []spltest.MockFuncCall{ - {ListOpts: listOpts}} + {ListOpts: listOpts}, + {MetaName: "test"}, + {MetaName: "test"}, + } - createCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Create": {funcCalls[0], funcCalls[3], funcCalls[4], funcCalls[7], funcCalls[9], funcCalls[10], funcCalls[5]}, "Update": {funcCalls[0], funcCalls[10]}, "List": {listmockCall[0]}} - updateCalls := map[string][]spltest.MockFuncCall{"Get": updateFuncCalls, "Update": {updateFuncCalls[4]}, "List": {listmockCall[0]}} + createCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Create": {createFuncCalls[0], createFuncCalls[3], createFuncCalls[4], createFuncCalls[7], createFuncCalls[9], createFuncCalls[10], {MetaName: "*v1.Event-test-"}}, "Update": {funcCalls[0], funcCalls[10]}, "List": listmockCall} + updateCalls := map[string][]spltest.MockFuncCall{"Get": updateFuncCalls, "Update": {}, "List": {listmockCall[0]}} current := enterpriseApi.MonitoringConsole{ TypeMeta: metav1.TypeMeta{ diff --git a/pkg/splunk/enterprise/upgrade.go b/pkg/splunk/enterprise/upgrade.go index 6173571b7..c8bed8079 100644 --- a/pkg/splunk/enterprise/upgrade.go +++ b/pkg/splunk/enterprise/upgrade.go @@ -278,7 +278,7 @@ MonitoringConsole: for _, item := range clusterManagerList.Items { if item.Spec.MonitoringConsoleRef.Name == cr.GetName() { - if item.Status.Phase != enterpriseApi.PhaseReady { + if item.Status.Phase != enterpriseApi.PhaseReady { message := fmt.Sprintf("cluster manager %s is not ready ", item.Name) return false, fmt.Errorf(message) } @@ -300,7 +300,7 @@ MonitoringConsole: } for _, item := range serachHeadClusterList.Items { if item.Spec.MonitoringConsoleRef.Name == cr.GetName() { - if item.Status.Phase != enterpriseApi.PhaseReady { + if item.Status.Phase != enterpriseApi.PhaseReady { message := fmt.Sprintf("search head cluster %s is not ready ", item.Name) return false, fmt.Errorf(message) } @@ -323,7 +323,7 @@ MonitoringConsole: for _, item := range standaloneList.Items { if item.Spec.MonitoringConsoleRef.Name == cr.GetName() { if item.Status.Phase != enterpriseApi.PhaseReady || spec.Image != item.Spec.Image { - message := fmt.Sprintf("standalone %s is not ready" , item.Name) + message := fmt.Sprintf("standalone %s is not ready", item.Name) return false, fmt.Errorf(message) } } diff --git a/pkg/splunk/enterprise/upgrade_test.go b/pkg/splunk/enterprise/upgrade_test.go index 17869e22e..37b736acd 100644 --- a/pkg/splunk/enterprise/upgrade_test.go +++ b/pkg/splunk/enterprise/upgrade_test.go @@ -20,7 +20,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client/fake" ) -func xTestUpgradePathValidation(t *testing.T) { +func TestUpgradePathValidation(t *testing.T) { builder := fake.NewClientBuilder() client := builder.Build() @@ -292,33 +292,10 @@ func xTestUpgradePathValidation(t *testing.T) { return nil } - // indexer is not ready as search head is not yet updated + // indexer is not ready as search head is not yet updated _, err = ApplyIndexerClusterManager(ctx, client, &idx) - if err != nil && !k8serrors.IsNotFound(err) { - t.Errorf("ApplyIndexerClusterManager should not have returned error; err=%v", err) - } - - // mointoring console statefulset is created here - _, err = ApplyMonitoringConsole(ctx, client, &mc) - if err != nil && !k8serrors.IsNotFound(err) { - t.Errorf("applyMonitoringConsole should not have returned error; err=%v", err) - } - // create pods for cluster manager - createPods(t, ctx, client, "monitoring-console", fmt.Sprintf("splunk-%s-monitoring-console-0", lm.Name), lm.Namespace, lm.Spec.Image) - updateStatefulSetsInTest(t, ctx, client, 1, fmt.Sprintf("splunk-%s-monitoring-console", lm.Name), lm.Namespace) - // mointoring console statefulset is created here - _, err = ApplyMonitoringConsole(ctx, client, &mc) - if err != nil && !k8serrors.IsNotFound(err) { - t.Errorf("applyMonitoringConsole should not have returned error; err=%v", err) - } - - err = client.Get(ctx, namespacedName, &mc) - if err != nil { - t.Errorf("get should not have returned error; err=%v", err) - } - - if mc.Status.Phase != enterpriseApi.PhaseReady { - t.Errorf("mc is not in ready state") + if err == nil { + t.Errorf("ApplyIndexerClusterManager should not have succeeded as search head is not ready") } // Monitoring console is ready now, now this should crete statefulset but statefulset is not in ready phase @@ -431,6 +408,29 @@ func xTestUpgradePathValidation(t *testing.T) { return extraEnv, err } + // mointoring console statefulset is created here + _, err = ApplyMonitoringConsole(ctx, client, &mc) + if err != nil && !k8serrors.IsNotFound(err) { + t.Errorf("applyMonitoringConsole should not have returned error; err=%v", err) + } + // create pods for cluster manager + createPods(t, ctx, client, "monitoring-console", fmt.Sprintf("splunk-%s-monitoring-console-0", lm.Name), lm.Namespace, lm.Spec.Image) + updateStatefulSetsInTest(t, ctx, client, 1, fmt.Sprintf("splunk-%s-monitoring-console", lm.Name), lm.Namespace) + // mointoring console statefulset is created here + _, err = ApplyMonitoringConsole(ctx, client, &mc) + if err != nil && !k8serrors.IsNotFound(err) { + t.Errorf("applyMonitoringConsole should not have returned error; err=%v", err) + } + + err = client.Get(ctx, namespacedName, &mc) + if err != nil { + t.Errorf("get should not have returned error; err=%v", err) + } + + if mc.Status.Phase != enterpriseApi.PhaseReady { + t.Errorf("mc is not in ready state") + } + // ------- Step2 starts here ----- // Update // standalone @@ -522,9 +522,10 @@ func xTestUpgradePathValidation(t *testing.T) { if err != nil { t.Errorf("applyClusterManager after update should not have returned error; err=%v", err) } - _, err = ApplyMonitoringConsole(ctx, client, &mc) + + _, err = ApplyClusterManager(ctx, client, &cm) if err != nil { - t.Errorf("applyMonitoringConsole after update should not have returned error; err=%v", err) + t.Errorf("applyClusterManager after update should not have returned error; err=%v", err) } shc.Status.TelAppInstalled = true @@ -609,6 +610,11 @@ func xTestUpgradePathValidation(t *testing.T) { t.Errorf("ApplyIndexerClusterManager after update should not have returned error; err=%v", err) } + _, err = ApplyMonitoringConsole(ctx, client, &mc) + if err != nil { + t.Errorf("applyMonitoringConsole after update should not have returned error; err=%v", err) + } + } func createPods(t *testing.T, ctx context.Context, client common.ControllerClient, crtype, name, namespace, image string) {