From 59f92058b03e9313cf212befcdce081932f34ce7 Mon Sep 17 00:00:00 2001 From: Vivek Reddy Date: Wed, 4 Dec 2024 06:51:36 +0000 Subject: [PATCH] fixed unit test cases Signed-off-by: Vivek Reddy --- .../workflows/build-test-push-workflow.yml | 22 ++-- pkg/splunk/common/types.go | 1 + pkg/splunk/enterprise/clustermanager_test.go | 32 +++-- pkg/splunk/enterprise/clustermaster_test.go | 15 ++- pkg/splunk/enterprise/finalizers_test.go | 115 +++++++++++++++--- pkg/splunk/enterprise/indexercluster_test.go | 4 +- pkg/splunk/enterprise/licensemanager_test.go | 12 +- pkg/splunk/enterprise/licensemaster_test.go | 15 ++- .../enterprise/monitoringconsole_test.go | 11 +- .../enterprise/searchheadcluster_test.go | 7 +- pkg/splunk/enterprise/standalone_test.go | 9 +- pkg/splunk/enterprise/types.go | 18 +-- pkg/splunk/enterprise/util.go | 10 +- pkg/splunk/enterprise/util_test.go | 10 +- pkg/splunk/test/controller.go | 73 ++++++++++- pkg/splunk/test/util.go | 15 +++ 16 files changed, 294 insertions(+), 75 deletions(-) diff --git a/.github/workflows/build-test-push-workflow.yml b/.github/workflows/build-test-push-workflow.yml index 25e0c7aa3..00974fd98 100644 --- a/.github/workflows/build-test-push-workflow.yml +++ b/.github/workflows/build-test-push-workflow.yml @@ -36,17 +36,17 @@ jobs: run: | make setup/ginkgo go mod tidy - #- name: Run Unit Tests - # run: | - # echo "commenting for now" - # # make test - #- name: Run Code Coverage - # run: goveralls -coverprofile=coverage.out -service=circle-ci -repotoken ${{ secrets.COVERALLS_TOKEN }} - #- name: Upload Coverage artifacts - # uses: actions/upload-artifact@v4.4.0 - # with: - # name: coverage.out - # path: coverage.out + - name: Run Unit Tests + run: | + echo "commenting for now" + # make test + - name: Run Code Coverage + run: goveralls -coverprofile=coverage.out -service=circle-ci -repotoken ${{ secrets.COVERALLS_TOKEN }} + - name: Upload Coverage artifacts + uses: actions/upload-artifact@v4.4.0 + with: + name: coverage.out + path: coverage.out build-operator-image: runs-on: ubuntu-latest needs: unit-tests diff --git a/pkg/splunk/common/types.go b/pkg/splunk/common/types.go index 944a13b89..f771623de 100644 --- a/pkg/splunk/common/types.go +++ b/pkg/splunk/common/types.go @@ -32,6 +32,7 @@ type MetaObject interface { schema.ObjectKind runtime.Object GetObjectMeta() metav1.Object + GetObjectKind() schema.ObjectKind } // The ControllerClient interfaces implements methods of the Kubernetes controller-runtime client diff --git a/pkg/splunk/enterprise/clustermanager_test.go b/pkg/splunk/enterprise/clustermanager_test.go index 14579151b..3cb480353 100644 --- a/pkg/splunk/enterprise/clustermanager_test.go +++ b/pkg/splunk/enterprise/clustermanager_test.go @@ -19,15 +19,15 @@ import ( "context" "errors" "fmt" + enterpriseApi "github.com/splunk/splunk-operator/api/v4" "io" + "k8s.io/apimachinery/pkg/runtime/schema" "os" "path/filepath" "runtime/debug" "strings" "testing" "time" - "k8s.io/apimachinery/pkg/runtime/schema" - enterpriseApi "github.com/splunk/splunk-operator/api/v4" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" @@ -74,6 +74,7 @@ func TestApplyClusterManager(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-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"}, @@ -98,12 +99,13 @@ func TestApplyClusterManager(t *testing.T) { } listmockCall := []spltest.MockFuncCall{ {ListOpts: listOpts}} - 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]}} + createCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Create": {funcCalls[0], funcCalls[3], funcCalls[4], funcCalls[6], funcCalls[9], funcCalls[5]}, "List": {listmockCall[0]}, "Update": {funcCalls[0]}} + updateCalls := map[string][]spltest.MockFuncCall{"Get": updateFuncCalls, "Update": {funcCalls[5]}, "List": {listmockCall[0]}} current := enterpriseApi.ClusterManager{ TypeMeta: metav1.TypeMeta{ - Kind: "ClusterManager", + Kind: "ClusterManager", + APIVersion: "enterprise.splunk.com/v4", }, ObjectMeta: metav1.ObjectMeta{ Name: "stack1", @@ -115,13 +117,16 @@ func TestApplyClusterManager(t *testing.T) { }, }, } - var gvk schema.GroupVersionKind - gvk.Kind = "SearchHead" - gvk.Group = "enterprise.splunk.com" - gvk.Version = "v4" + // Define GroupVersionKind + gvk := schema.GroupVersionKind{ + Group: "enterprise.splunk.com", + Version: "v4", + Kind: "ClusterManager", + } current.SetGroupVersionKind(gvk) revised := current.DeepCopy() revised.Spec.Image = "splunk/test" + revised.SetGroupVersionKind(gvk) reconcile := func(c *spltest.MockClient, cr interface{}) error { _, err := ApplyClusterManager(ctx, c, cr.(*enterpriseApi.ClusterManager)) return err @@ -499,6 +504,7 @@ func TestApplyClusterManagerWithSmartstore(t *testing.T) { {MetaName: "*v1.ConfigMap-test-splunk-stack1-clustermanager-smartstore"}, {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"}, @@ -521,6 +527,7 @@ func TestApplyClusterManagerWithSmartstore(t *testing.T) { {MetaName: "*v1.ConfigMap-test-splunk-stack1-clustermanager-smartstore"}, {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"}, @@ -550,8 +557,8 @@ func TestApplyClusterManagerWithSmartstore(t *testing.T) { {ListOpts: listOpts}, {ListOpts: listOpts1}, } - createCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Create": {funcCalls[7], funcCalls[10], funcCalls[12]}, "List": {listmockCall[0], listmockCall[0], listmockCall[1]}, "Update": {funcCalls[0], funcCalls[3], funcCalls[13]}} - updateCalls := map[string][]spltest.MockFuncCall{"Get": updateFuncCalls, "Update": {funcCalls[8]}, "List": {listmockCall[0]}} + createCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Create": {funcCalls[7], funcCalls[8], funcCalls[11], funcCalls[13]}, "List": {listmockCall[0], listmockCall[0], listmockCall[1]}, "Update": {funcCalls[0], funcCalls[3], funcCalls[14]}} + updateCalls := map[string][]spltest.MockFuncCall{"Get": updateFuncCalls, "Update": {funcCalls[9]}, "List": {listmockCall[0]}} current := enterpriseApi.ClusterManager{ TypeMeta: metav1.TypeMeta{ @@ -943,8 +950,9 @@ func TestApplyClusterManagerDeletion(t *testing.T) { // Create S3 secret s3Secret := spltest.GetMockS3SecretKeys("s3-secret") - c.AddObject(&s3Secret) + configmap := spltest.GetMockPerCRConfigMap("splunk-cluster-manager-stack1-configmap") + c.AddObject(&configmap) // Create namespace scoped secret _, err := splutil.ApplyNamespaceScopedSecretObject(ctx, c, "test") diff --git a/pkg/splunk/enterprise/clustermaster_test.go b/pkg/splunk/enterprise/clustermaster_test.go index 70ee16357..3b8575fc1 100644 --- a/pkg/splunk/enterprise/clustermaster_test.go +++ b/pkg/splunk/enterprise/clustermaster_test.go @@ -56,6 +56,7 @@ func TestApplyClusterMaster(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-master-stack1-configmap"}, {MetaName: "*v1.Service-test-splunk-stack1-indexer-service"}, {MetaName: "*v1." + splcommon.TestStack1ClusterManagerService}, {MetaName: "*v1.StatefulSet-test-splunk-stack1-cluster-master"}, @@ -73,6 +74,7 @@ func TestApplyClusterMaster(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-cluster-master-stack1-configmap"}, {MetaName: "*v1.Service-test-splunk-stack1-indexer-service"}, {MetaName: "*v1." + splcommon.TestStack1ClusterManagerService}, {MetaName: "*v1.StatefulSet-test-splunk-stack1-cluster-master"}, @@ -98,8 +100,8 @@ func TestApplyClusterMaster(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[5]}, "List": {listmockCall[0]}, "Update": {funcCalls[0]}} - updateCalls := map[string][]spltest.MockFuncCall{"Get": updateFuncCalls, "Update": {funcCalls[5]}, "List": {listmockCall[0]}} + createCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Create": {funcCalls[0], funcCalls[3], funcCalls[4], funcCalls[5], funcCalls[8], funcCalls[10], funcCalls[6]}, "List": {listmockCall[0]}, "Update": {funcCalls[0]}} + updateCalls := map[string][]spltest.MockFuncCall{"Get": updateFuncCalls, "Update": {funcCalls[6]}, "List": {listmockCall[0]}} current := enterpriseApiV3.ClusterMaster{ TypeMeta: metav1.TypeMeta{ @@ -203,6 +205,7 @@ func TestApplyClusterMasterWithSmartstore(t *testing.T) { {MetaName: "*v1." + splcommon.TestStack1ClusterManagerConfigMapSmartStore}, {MetaName: "*v1.Secret-test-splunk-test-secret"}, {MetaName: "*v1.Secret-test-splunk-test-secret"}, + {MetaName: "*v1.ConfigMap-test-splunk-cluster-master-stack1-configmap"}, {MetaName: "*v1.Service-test-splunk-stack1-indexer-service"}, {MetaName: "*v1.Service-test-splunk-stack1-cluster-master-service"}, {MetaName: "*v1.StatefulSet-test-splunk-stack1-cluster-master"}, @@ -226,6 +229,7 @@ func TestApplyClusterMasterWithSmartstore(t *testing.T) { {MetaName: "*v1." + splcommon.TestStack1ClusterManagerConfigMapSmartStore}, {MetaName: "*v1.Secret-test-splunk-test-secret"}, {MetaName: "*v1.Secret-test-splunk-test-secret"}, + {MetaName: "*v1.ConfigMap-test-splunk-cluster-master-stack1-configmap"}, {MetaName: "*v1.Service-test-splunk-stack1-indexer-service"}, {MetaName: "*v1.Service-test-splunk-stack1-cluster-master-service"}, {MetaName: "*v1.StatefulSet-test-splunk-stack1-cluster-master"}, @@ -251,8 +255,8 @@ func TestApplyClusterMasterWithSmartstore(t *testing.T) { } listmockCall := []spltest.MockFuncCall{ {ListOpts: listOpts}} - createCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Create": {funcCalls[7], funcCalls[8], funcCalls[11], funcCalls[13]}, "List": {listmockCall[0], listmockCall[0]}, "Update": {funcCalls[0], funcCalls[3], funcCalls[14]}} - updateCalls := map[string][]spltest.MockFuncCall{"Get": updateFuncCalls, "Update": {funcCalls[9]}, "List": {listmockCall[0]}} + createCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Create": {funcCalls[7], funcCalls[8], funcCalls[9], funcCalls[12], funcCalls[14]}, "List": {listmockCall[0], listmockCall[0]}, "Update": {funcCalls[0], funcCalls[3], funcCalls[15]}} + updateCalls := map[string][]spltest.MockFuncCall{"Get": updateFuncCalls, "Update": {funcCalls[10]}, "List": {listmockCall[0]}} current := enterpriseApiV3.ClusterMaster{ TypeMeta: metav1.TypeMeta{ @@ -636,8 +640,9 @@ func TestApplyCLusterMasterDeletion(t *testing.T) { // Create S3 secret s3Secret := spltest.GetMockS3SecretKeys("s3-secret") - c.AddObject(&s3Secret) + configmap := spltest.GetMockPerCRConfigMap("splunk-cluster-master-stack1-configmap") + c.AddObject(&configmap) // Create namespace scoped secret _, err := splutil.ApplyNamespaceScopedSecretObject(ctx, c, "test") diff --git a/pkg/splunk/enterprise/finalizers_test.go b/pkg/splunk/enterprise/finalizers_test.go index e171532ac..cd9ce0085 100644 --- a/pkg/splunk/enterprise/finalizers_test.go +++ b/pkg/splunk/enterprise/finalizers_test.go @@ -139,48 +139,105 @@ 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"}, + mockCalls["Get"] = []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.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"}, - }...) + } + mockCalls["Create"] = []spltest.MockFuncCall{ + {MetaName: "*v1.Secret-test-splunk-test-secret"}, + {MetaName: "*v1.ConfigMap-test-splunk-standalone-stack1-configmap"}, + } case "LicenseMaster": - mockCalls["Get"] = append(mockCalls["Get"], []spltest.MockFuncCall{ + mockCalls["Get"] = []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.ConfigMap-test-splunk-license-master-stack1-configmap"}, + {MetaName: "*v1.Secret-test-splunk-test-secret"}, {MetaName: "*v1.StatefulSet-test-splunk-stack1-license-master"}, {MetaName: "*v3.LicenseMaster-test-stack1"}, {MetaName: "*v3.LicenseMaster-test-stack1"}, - }...) + } + mockCalls["Create"] = []spltest.MockFuncCall{ + {MetaName: "*v1.Secret-test-splunk-test-secret"}, + {MetaName: "*v1.ConfigMap-test-splunk-license-master-stack1-configmap"}, + } case "LicenseManager": - mockCalls["Get"] = append(mockCalls["Get"], []spltest.MockFuncCall{ + mockCalls["Get"] = []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.ConfigMap-test-splunk-license-manager-stack1-configmap"}, + {MetaName: "*v1.Secret-test-splunk-test-secret"}, {MetaName: "*v1.StatefulSet-test-splunk-stack1-license-manager"}, {MetaName: "*v4.LicenseManager-test-stack1"}, {MetaName: "*v4.LicenseManager-test-stack1"}, - }...) + } + mockCalls["Create"] = []spltest.MockFuncCall{ + {MetaName: "*v1.Secret-test-splunk-test-secret"}, + {MetaName: "*v1.ConfigMap-test-splunk-license-manager-stack1-configmap"}, + } case "SearchHeadCluster": - mockCalls["Get"] = append(mockCalls["Get"], []spltest.MockFuncCall{ + mockCalls["Get"] = []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.ConfigMap-test-splunk-search-head-stack1-configmap"}, + {MetaName: "*v1.Secret-test-splunk-test-secret"}, {MetaName: "*v1.StatefulSet-test-splunk-stack1-search-head"}, {MetaName: "*v4.SearchHeadCluster-test-stack1"}, {MetaName: "*v4.SearchHeadCluster-test-stack1"}, - }...) + } + mockCalls["Create"] = []spltest.MockFuncCall{ + {MetaName: "*v1.Secret-test-splunk-test-secret"}, + {MetaName: "*v1.ConfigMap-test-splunk-search-head-stack1-configmap"}, + } case "ClusterMaster": - mockCalls["Get"] = append(mockCalls["Get"], []spltest.MockFuncCall{ + mockCalls["Get"] = []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.ConfigMap-test-splunk-cluster-master-stack1-configmap"}, + {MetaName: "*v1.Secret-test-splunk-test-secret"}, {MetaName: "*v1.StatefulSet-test-splunk-stack1-cluster-master"}, {MetaName: "*v3.ClusterMaster-test-stack1"}, {MetaName: "*v3.ClusterMaster-test-stack1"}, - }...) + } + mockCalls["Create"] = []spltest.MockFuncCall{ + {MetaName: "*v1.Secret-test-splunk-test-secret"}, + {MetaName: "*v1.ConfigMap-test-splunk-cluster-master-stack1-configmap"}, + } + case "IndexerCluster": + mockCalls["Create"] = []spltest.MockFuncCall{ + {MetaName: "*v1.Secret-test-splunk-test-secret"}, + {MetaName: "*v1.ConfigMap-test-splunk-indexer-stack1-configmap"}, + } case "ClusterManager": - mockCalls["Get"] = append(mockCalls["Get"], []spltest.MockFuncCall{ + mockCalls["Get"] = []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.ConfigMap-test-splunk-cluster-manager-stack1-configmap"}, + {MetaName: "*v1.Secret-test-splunk-test-secret"}, {MetaName: "*v1.StatefulSet-test-splunk-stack1-cluster-manager"}, {MetaName: "*v4.ClusterManager-test-stack1"}, {MetaName: "*v4.ClusterManager-test-stack1"}, - }...) + } + mockCalls["Create"] = []spltest.MockFuncCall{ + {MetaName: "*v1.Secret-test-splunk-test-secret"}, + {MetaName: "*v1.ConfigMap-test-splunk-cluster-manager-stack1-configmap"}, + } listOptsTest := []client.ListOption{ client.InNamespace(cr.GetNamespace()), @@ -194,8 +251,18 @@ func splunkDeletionTester(t *testing.T, cr splcommon.MetaObject, delete func(spl }...) mockCalls["List"][0], mockCalls["List"][len(mockCalls["List"])-1] = mockCalls["List"][len(mockCalls["List"])-1], mockCalls["List"][0] case "MonitoringConsole": - mockCalls["Get"] = append(mockCalls["Get"], spltest.MockFuncCall{MetaName: "*v4.MonitoringConsole-test-stack1"}) - mockCalls["Get"] = append(mockCalls["Get"], spltest.MockFuncCall{MetaName: "*v4.MonitoringConsole-test-stack1"}) + mockCalls["Get"] = []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.ConfigMap-test-splunk-monitoring-console-stack1-configmap"}, + {MetaName: "*v4.MonitoringConsole-test-stack1"}, + {MetaName: "*v4.MonitoringConsole-test-stack1"}, + } + mockCalls["Create"] = []spltest.MockFuncCall{ + {MetaName: "*v1.Secret-test-splunk-test-secret"}, + {MetaName: "*v1.ConfigMap-test-splunk-monitoring-console-stack1-configmap"}, + } } } else { mockCalls["Update"] = []spltest.MockFuncCall{ @@ -222,6 +289,24 @@ func splunkDeletionTester(t *testing.T, cr splcommon.MetaObject, delete func(spl {MetaName: "*v4.IndexerCluster-test-stack1"}, {MetaName: "*v4.IndexerCluster-test-stack1"}, } + switch cr.GetObjectKind().GroupVersionKind().Kind { + case "IndexerCluster": + mockCalls["Create"] = []spltest.MockFuncCall{ + {MetaName: "*v1.Secret-test-splunk-test-secret"}, + {MetaName: "*v1.ConfigMap-test-splunk-indexer-stack1-configmap"}, + } + mockCalls["Get"] = []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.ConfigMap-test-splunk-indexer-stack1-configmap"}, + {MetaName: "*v4.ClusterManager-test-manager1"}, + {MetaName: "*v1.Secret-test-splunk-test-secret"}, + {MetaName: "*v1.StatefulSet-test-splunk-stack1-indexer"}, + {MetaName: "*v4.IndexerCluster-test-stack1"}, + {MetaName: "*v4.IndexerCluster-test-stack1"}, + } + } } } diff --git a/pkg/splunk/enterprise/indexercluster_test.go b/pkg/splunk/enterprise/indexercluster_test.go index 028fd33e6..80d1269b3 100644 --- a/pkg/splunk/enterprise/indexercluster_test.go +++ b/pkg/splunk/enterprise/indexercluster_test.go @@ -152,6 +152,7 @@ func TestApplyIndexerCluster(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-indexer-stack1-configmap"}, {MetaName: "*v4.ClusterManager-test-manager1"}, {MetaName: "*v1.Service-test-splunk-stack1-indexer-headless"}, {MetaName: "*v1.Service-test-splunk-stack1-indexer-service"}, @@ -167,6 +168,7 @@ func TestApplyIndexerCluster(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-indexer-stack1-configmap"}, {MetaName: "*v4.ClusterManager-test-manager1"}, {MetaName: "*v1.Service-test-splunk-stack1-indexer-headless"}, {MetaName: "*v1.Service-test-splunk-stack1-indexer-service"}, @@ -194,7 +196,7 @@ func TestApplyIndexerCluster(t *testing.T) { {ListOpts: listOpts}, {ListOpts: listOpts1}, } - createCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Create": {funcCalls[0], funcCalls[4], funcCalls[5], funcCalls[8], funcCalls[10]}, "Update": {funcCalls[0]}, "List": {listmockCall[0], listmockCall[1]}} + createCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Create": {funcCalls[0], funcCalls[3], funcCalls[5], funcCalls[6], funcCalls[9], funcCalls[11]}, "Update": {funcCalls[0]}, "List": {listmockCall[0], listmockCall[1]}} updateCalls := map[string][]spltest.MockFuncCall{"Get": updateFuncCalls, "List": {listmockCall[0], listmockCall[1]}} current := enterpriseApi.IndexerCluster{ diff --git a/pkg/splunk/enterprise/licensemanager_test.go b/pkg/splunk/enterprise/licensemanager_test.go index 71b32113c..60896b511 100644 --- a/pkg/splunk/enterprise/licensemanager_test.go +++ b/pkg/splunk/enterprise/licensemanager_test.go @@ -49,6 +49,7 @@ func TestApplyLicenseManager(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-license-manager-stack1-configmap"}, {MetaName: "*v1.Service-test-splunk-stack1-license-manager-service"}, {MetaName: "*v1.StatefulSet-test-splunk-stack1-license-manager"}, {MetaName: "*v1.ConfigMap-test-splunk-test-probe-configmap"}, @@ -72,9 +73,9 @@ func TestApplyLicenseManager(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[10]}, "Update": {funcCalls[0]}, "List": {listmockCall[0]}} - updateFuncCalls := []spltest.MockFuncCall{funcCalls[0], funcCalls[1], funcCalls[3], funcCalls[4], funcCalls[5], funcCalls[7], funcCalls[8], funcCalls[9], funcCalls[10], funcCalls[9], funcCalls[11], funcCalls[12]} - updateCalls := map[string][]spltest.MockFuncCall{"Get": updateFuncCalls, "Update": {funcCalls[4]}, "List": {listmockCall[0]}} + createCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Create": {funcCalls[0], funcCalls[3], funcCalls[4], funcCalls[6], funcCalls[9], funcCalls[10]}, "Update": {funcCalls[0]}, "List": {listmockCall[0]}} + updateFuncCalls := []spltest.MockFuncCall{funcCalls[0], funcCalls[1], funcCalls[3], funcCalls[4], funcCalls[5], funcCalls[6], funcCalls[8], funcCalls[9], funcCalls[10], funcCalls[11], funcCalls[10], funcCalls[12], funcCalls[12]} + updateCalls := map[string][]spltest.MockFuncCall{"Get": updateFuncCalls, "Update": {funcCalls[5]}, "List": {listmockCall[0]}} current := enterpriseApi.LicenseManager{ TypeMeta: metav1.TypeMeta{ Kind: "LicenseManager", @@ -239,8 +240,9 @@ func TestAppFrameworkApplyLicenseManagerShouldNotFail(t *testing.T) { // Create S3 secret s3Secret := spltest.GetMockS3SecretKeys("s3-secret") - client.AddObject(&s3Secret) + configmap := spltest.GetMockPerCRConfigMap("splunk-license-manager-stack1-configmap") + client.AddObject(&configmap) // to pass the validation stage, add the directory to download apps err = os.MkdirAll(splcommon.AppDownloadVolume, 0755) @@ -648,6 +650,8 @@ func TestApplyLicenseManagerDeletion(t *testing.T) { s3Secret := spltest.GetMockS3SecretKeys("s3-secret") c.AddObject(&s3Secret) + configmap := spltest.GetMockPerCRConfigMap("splunk-license-manager-stack1-configmap") + c.AddObject(&configmap) // Create namespace scoped secret _, err := splutil.ApplyNamespaceScopedSecretObject(ctx, c, "test") diff --git a/pkg/splunk/enterprise/licensemaster_test.go b/pkg/splunk/enterprise/licensemaster_test.go index 9e74afd5b..6ee855766 100644 --- a/pkg/splunk/enterprise/licensemaster_test.go +++ b/pkg/splunk/enterprise/licensemaster_test.go @@ -65,6 +65,7 @@ func TestApplyLicenseMaster(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-license-master-stack1-configmap"}, {MetaName: "*v1." + splcommon.TestStack1LicenseManagerServiceTestService}, {MetaName: "*v1." + splcommon.TestStack1LicenseManagerStatefulSet}, {MetaName: "*v1.ConfigMap-test-splunk-test-probe-configmap"}, @@ -88,9 +89,9 @@ func TestApplyLicenseMaster(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[10]}, "Update": {funcCalls[0]}, "List": {listmockCall[0]}} - updateFuncCalls := []spltest.MockFuncCall{funcCalls[0], funcCalls[1], funcCalls[3], funcCalls[4], funcCalls[5], funcCalls[7], funcCalls[8], funcCalls[9], funcCalls[10], funcCalls[9], funcCalls[11], funcCalls[12]} - updateCalls := map[string][]spltest.MockFuncCall{"Get": updateFuncCalls, "Update": {funcCalls[4]}, "List": {listmockCall[0]}} + createCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Create": {funcCalls[0], funcCalls[3], funcCalls[4], funcCalls[7], funcCalls[9], funcCalls[11]}, "Update": {funcCalls[0]}, "List": {listmockCall[0]}} + updateFuncCalls := []spltest.MockFuncCall{funcCalls[0], funcCalls[1], funcCalls[3], funcCalls[4], funcCalls[5], funcCalls[6], funcCalls[8], funcCalls[9], funcCalls[10], funcCalls[11], funcCalls[10], funcCalls[12], funcCalls[13]} + updateCalls := map[string][]spltest.MockFuncCall{"Get": updateFuncCalls, "Update": {funcCalls[10]}, "List": {listmockCall[0]}} current := enterpriseApiV3.LicenseMaster{ TypeMeta: metav1.TypeMeta{ Kind: "LicenseMaster", @@ -252,6 +253,8 @@ func TestAppFrameworkApplyLicenseMasterShouldNotFail(t *testing.T) { s3Secret := spltest.GetMockS3SecretKeys("s3-secret") client.AddObject(&s3Secret) + configmap := spltest.GetMockPerCRConfigMap("splunk-license-master-stack1-configmap") + client.AddObject(&configmap) // to pass the validation stage, add the directory to download apps err = os.MkdirAll(splcommon.AppDownloadVolume, 0755) @@ -332,6 +335,8 @@ func TestLicensemasterGetAppsListForAWSS3ClientShouldNotFail(t *testing.T) { s3Secret := spltest.GetMockS3SecretKeys("s3-secret") client.AddObject(&s3Secret) + configmap := spltest.GetMockPerCRConfigMap("splunk-license-master-stack1-configmap") + client.AddObject(&configmap) // Create namespace scoped secret _, err := splutil.ApplyNamespaceScopedSecretObject(ctx, client, "test") @@ -557,6 +562,8 @@ func TestLicenseMasterGetAppsListForAWSS3ClientShouldFail(t *testing.T) { } client.AddObject(&s3Secret) + configmap := spltest.GetMockPerCRConfigMap("splunk-license-master-stack1-configmap") + client.AddObject(&configmap) _, err = remoteDataClientMgr.GetAppsList(ctx) if err == nil { @@ -659,6 +666,8 @@ func TestApplyLicenseMasterDeletion(t *testing.T) { s3Secret := spltest.GetMockS3SecretKeys("s3-secret") c.AddObject(&s3Secret) + configmap := spltest.GetMockPerCRConfigMap("splunk-license-master-stack1-configmap") + c.AddObject(&configmap) // Create namespace scoped secret _, err := splutil.ApplyNamespaceScopedSecretObject(ctx, c, "test") diff --git a/pkg/splunk/enterprise/monitoringconsole_test.go b/pkg/splunk/enterprise/monitoringconsole_test.go index 7e24cb5e1..6756be1bb 100644 --- a/pkg/splunk/enterprise/monitoringconsole_test.go +++ b/pkg/splunk/enterprise/monitoringconsole_test.go @@ -61,6 +61,7 @@ func TestApplyMonitoringConsole(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-monitoring-console-stack1-configmap"}, {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"}, @@ -79,6 +80,7 @@ func TestApplyMonitoringConsole(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-monitoring-console-stack1-configmap"}, {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"}, @@ -112,8 +114,8 @@ func TestApplyMonitoringConsole(t *testing.T) { {ListOpts: listOpts2}, } - 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], listmockCall[1], listmockCall[1], listmockCall[1], listmockCall[1], listmockCall[1]}} - updateCalls := map[string][]spltest.MockFuncCall{"Get": updateFuncCalls, "Update": {updateFuncCalls[4]}, "List": {listmockCall[0], listmockCall[1], listmockCall[1], listmockCall[1], listmockCall[1], listmockCall[1]}} + createCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Create": {funcCalls[0], funcCalls[3], funcCalls[4], funcCalls[5], funcCalls[8], funcCalls[10], funcCalls[11], funcCalls[6]}, "Update": {funcCalls[0], funcCalls[11]}, "List": {listmockCall[0], listmockCall[1], listmockCall[1], listmockCall[1], listmockCall[1], listmockCall[1]}} + updateCalls := map[string][]spltest.MockFuncCall{"Get": updateFuncCalls, "Update": {updateFuncCalls[5]}, "List": {listmockCall[0], listmockCall[1], listmockCall[1], listmockCall[1], listmockCall[1], listmockCall[1]}} current := enterpriseApi.MonitoringConsole{ TypeMeta: metav1.TypeMeta{ @@ -463,8 +465,9 @@ func TestAppFrameworkApplyMonitoringConsoleShouldNotFail(t *testing.T) { // Create S3 secret s3Secret := spltest.GetMockS3SecretKeys("s3-secret") - client.AddObject(&s3Secret) + configmap := spltest.GetMockPerCRConfigMap("splunk-monitoring-console-monitoringConsole-configmap") + client.AddObject(&configmap) // to pass the validation stage, add the directory to download apps _ = os.MkdirAll(splcommon.AppDownloadVolume, 0755) @@ -1048,6 +1051,8 @@ func TestApplyMonitoringConsoleDeletion(t *testing.T) { s3Secret := spltest.GetMockS3SecretKeys("s3-secret") c.AddObject(&s3Secret) + configmap := spltest.GetMockPerCRConfigMap("splunk-monitoring-console-stack1-configmap") + c.AddObject(&configmap) // Create namespace scoped secret _, err := splutil.ApplyNamespaceScopedSecretObject(ctx, c, "test") diff --git a/pkg/splunk/enterprise/searchheadcluster_test.go b/pkg/splunk/enterprise/searchheadcluster_test.go index 6bc37c2c8..f2eebe279 100644 --- a/pkg/splunk/enterprise/searchheadcluster_test.go +++ b/pkg/splunk/enterprise/searchheadcluster_test.go @@ -69,6 +69,8 @@ func TestApplySearchHeadCluster(t *testing.T) { {MetaName: "*v1.Secret-test-splunk-test-secret"}, {MetaName: "*v1.Secret-test-splunk-test-secret"}, + {MetaName: "*v1.ConfigMap-test-splunk-search-head-stack1-configmap"}, + {MetaName: "*v1.Service-test-splunk-stack1-search-head-headless"}, {MetaName: "*v1.Service-test-splunk-stack1-search-head-service"}, @@ -99,6 +101,7 @@ func TestApplySearchHeadCluster(t *testing.T) { createFuncCalls := []spltest.MockFuncCall{ {MetaName: "*v1.Secret-test-splunk-test-secret"}, {MetaName: "*v1.Secret-test-splunk-test-secret"}, + {MetaName: "*v1.ConfigMap-test-splunk-search-head-stack1-configmap"}, {MetaName: "*v1.Service-test-splunk-stack1-search-head-headless"}, {MetaName: "*v1.Service-test-splunk-stack1-search-head-service"}, @@ -135,8 +138,8 @@ func TestApplySearchHeadCluster(t *testing.T) { listmockCall := []spltest.MockFuncCall{ {ListOpts: listOpts}} - createCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Create": {funcCalls[0], funcCalls[3], funcCalls[4], funcCalls[5], funcCalls[8], funcCalls[10], funcCalls[12], funcCalls[16], funcCalls[17]}, "Update": {funcCalls[0]}, "List": {listmockCall[0], listmockCall[0]}} - updateCalls := map[string][]spltest.MockFuncCall{"Get": createFuncCalls, "Update": {createFuncCalls[5], createFuncCalls[9]}, "List": {listmockCall[0], listmockCall[0]}} + createCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Create": {funcCalls[0], funcCalls[3], funcCalls[4], funcCalls[5], funcCalls[6], funcCalls[9], funcCalls[11], funcCalls[13], funcCalls[17], funcCalls[18]}, "Update": {funcCalls[0]}, "List": {listmockCall[0], listmockCall[0]}} + updateCalls := map[string][]spltest.MockFuncCall{"Get": createFuncCalls, "Update": {createFuncCalls[6], createFuncCalls[10]}, "List": {listmockCall[0], listmockCall[0]}} statefulSet := enterpriseApi.SearchHeadCluster{ TypeMeta: metav1.TypeMeta{ Kind: "SearchHeadCluster", diff --git a/pkg/splunk/enterprise/standalone_test.go b/pkg/splunk/enterprise/standalone_test.go index cf120ca98..f3dde8741 100644 --- a/pkg/splunk/enterprise/standalone_test.go +++ b/pkg/splunk/enterprise/standalone_test.go @@ -257,7 +257,7 @@ 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[8], funcCalls[10], funcCalls[12], funcCalls[15]}, "Update": {funcCalls[0]}, "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{ @@ -506,6 +506,8 @@ func TestAppFrameworkApplyStandaloneShouldNotFail(t *testing.T) { s3Secret := spltest.GetMockS3SecretKeys("s3-secret") client.AddObject(&s3Secret) + configmap := spltest.GetMockPerCRConfigMap("splunk-standalone-standalone-configmap") + client.AddObject(&configmap) // to pass the validation stage, add the directory to download apps err = os.MkdirAll(splcommon.AppDownloadVolume, 0755) @@ -575,6 +577,9 @@ func TestAppFrameworkApplyStandaloneScalingUpShouldNotFail(t *testing.T) { client.AddObject(&s3Secret) + configmap := spltest.GetMockPerCRConfigMap("splunk-standalone-standalone-configmap") + client.AddObject(&configmap) + // to pass the validation stage, add the directory to download apps err = os.MkdirAll(splcommon.AppDownloadVolume, 0755) defer os.RemoveAll(splcommon.AppDownloadVolume) @@ -981,6 +986,8 @@ func TestApplyStandaloneDeletion(t *testing.T) { s3Secret := spltest.GetMockS3SecretKeys("s3-secret") c.AddObject(&s3Secret) + configmap := spltest.GetMockPerCRConfigMap("splunk-standalone-stack1-configmap") + c.AddObject(&configmap) // Create namespace scoped secret _, err := splutil.ApplyNamespaceScopedSecretObject(ctx, c, "test") diff --git a/pkg/splunk/enterprise/types.go b/pkg/splunk/enterprise/types.go index 2db67dc3f..7b34c5eeb 100644 --- a/pkg/splunk/enterprise/types.go +++ b/pkg/splunk/enterprise/types.go @@ -276,24 +276,24 @@ func (instanceType InstanceType) ToKind() string { func KindToInstanceString(kind string) string { switch kind { - case "Standalone": - return SplunkStandalone.ToString() - case "IndexerCluster": - return SplunkClusterMaster.ToString() - case "ClustetrManager": + case "ClusterManager": return SplunkClusterManager.ToString() - case "LicenseMaster": - return SplunkIndexer.ToString() case "ClusterMaster": - return SplunkClusterMaster.ToString() + return SplunkClusterMaster.ToString() + case "IndexerCluster": + return SplunkIndexer.ToString() case "LicenseManager": return SplunkLicenseManager.ToString() + case "LicenseMaster": + return SplunkLicenseMaster.ToString() case "MonitoringConsole": return SplunkMonitoringConsole.ToString() case "SearchHeadCluster": return SplunkSearchHead.ToString() case "SearchHead": return SplunkSearchHead.ToString() + case "Standalone": + return SplunkStandalone.ToString() } return "" -} \ No newline at end of file +} diff --git a/pkg/splunk/enterprise/util.go b/pkg/splunk/enterprise/util.go index 3d568f0e7..5ada2a06c 100644 --- a/pkg/splunk/enterprise/util.go +++ b/pkg/splunk/enterprise/util.go @@ -230,7 +230,7 @@ func ApplySplunkConfig(ctx context.Context, client splcommon.ControllerClient, c return nil, err } } - err = ReconcileCRSpecificConfigMap(ctx, client, cr) + err = ReconcileCRSpecificConfigMap(ctx, client, cr, instanceType) if err != nil { return nil, err } @@ -239,11 +239,15 @@ func ApplySplunkConfig(ctx context.Context, client splcommon.ControllerClient, c } // ReconcileCRSpecificConfigMap reconciles CR Specific config map exists and contains the ManualUpdate field set to "off" -func ReconcileCRSpecificConfigMap(ctx context.Context, client splcommon.ControllerClient, cr splcommon.MetaObject) error { +func ReconcileCRSpecificConfigMap(ctx context.Context, client splcommon.ControllerClient, cr splcommon.MetaObject, instanceType InstanceType) error { reqLogger := log.FromContext(ctx) scopedLog := reqLogger.WithName("ReconcileCRSpecificConfigMap").WithValues("name", cr.GetName(), "namespace", cr.GetNamespace()) + a := cr.GetObjectKind() + b := a.GroupVersionKind() + c := b.Kind + d := KindToInstanceString(c) - configMapName := fmt.Sprintf(perCrConfigMapNameStr, KindToInstanceString(cr.GroupVersionKind().Kind), cr.GetName()) + configMapName := fmt.Sprintf(perCrConfigMapNameStr, d, cr.GetName()) namespacedName := types.NamespacedName{Namespace: cr.GetNamespace(), Name: configMapName} configMap, err := splctrl.GetConfigMap(ctx, client, namespacedName) diff --git a/pkg/splunk/enterprise/util_test.go b/pkg/splunk/enterprise/util_test.go index adff5986c..6d5032fac 100644 --- a/pkg/splunk/enterprise/util_test.go +++ b/pkg/splunk/enterprise/util_test.go @@ -30,10 +30,10 @@ import ( appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" "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" @@ -226,7 +226,7 @@ func TestApplySplunkConfig(t *testing.T) { {MetaName: "*v1.ConfigMap-test-splunk-search-head-stack1-configmap"}, } 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] }} + updateCalls := map[string][]spltest.MockFuncCall{"Get": {funcCalls[0], funcCalls[1], funcCalls[3], funcCalls[5]}} searchHeadCR := enterpriseApi.SearchHeadCluster{ TypeMeta: metav1.TypeMeta{ Kind: "SearcHead", @@ -239,7 +239,7 @@ func TestApplySplunkConfig(t *testing.T) { var gvk schema.GroupVersionKind gvk.Kind = "SearchHead" gvk.Group = "enterprise.splunk.com" - gvk.Version = "v4" + gvk.Version = "v4" searchHeadCR.SetGroupVersionKind(gvk) searchHeadCR.Spec.Defaults = "defaults-yaml" searchHeadRevised := searchHeadCR.DeepCopy() @@ -275,7 +275,7 @@ func TestApplySplunkConfig(t *testing.T) { } funcCalls = []spltest.MockFuncCall{ {MetaName: "*v1.Secret-test-splunk-test-secret"}, - {MetaName: "*v1.ConfigMap-test-splunk-cluster-master-stack1-configmap"}, + {MetaName: "*v1.ConfigMap-test-splunk-indexer-stack1-configmap"}, } 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]}} @@ -1354,7 +1354,7 @@ func TestUpdateManualAppUpdateConfigMapLocked(t *testing.T) { } crKindMap := make(map[string]string) - crKindMap["manualUpdate"] = "off" + 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 diff --git a/pkg/splunk/test/controller.go b/pkg/splunk/test/controller.go index cd4412812..301f004a6 100644 --- a/pkg/splunk/test/controller.go +++ b/pkg/splunk/test/controller.go @@ -179,6 +179,19 @@ func copyMockObject(dst, src *client.Object) { //srcP := *src // default if no types match //*dst = srcP.DeepCopyObject() + // Set GVK if missing + srcObj := *src + gvk := srcObj.GetObjectKind().GroupVersionKind() + if gvk.Empty() { + gvk = schema.GroupVersionKind{ + Group: "enterprise.splunk.com", // Replace with your default group + Version: "v4", + Kind: reflect.TypeOf(srcObj).Elem().Name(), + } + srcObj.GetObjectKind().SetGroupVersionKind(gvk) + } + + *dst = srcObj.DeepCopyObject().(client.Object) } // copyMockObjectList uses the global MockObjectCopiers to perform the typed copy of a client.Object from src to dst @@ -296,7 +309,8 @@ type MockClient struct { // RESTMapper wrapper for REST Client // FIXME func (c MockClient) RESTMapper() meta.RESTMapper { - ne := &meta.DefaultRESTMapper{} + ne := meta.NewDefaultRESTMapper([]schema.GroupVersion{ + {Group: "enterprise.splunk.com", Version: "v1"}}) return ne } @@ -304,6 +318,7 @@ func (c MockClient) RESTMapper() meta.RESTMapper { // FIXME func (c MockClient) Scheme() *runtime.Scheme { sc := &runtime.Scheme{} + setupScheme(sc) return sc } @@ -493,6 +508,14 @@ func (c *MockClient) CheckCalls(t *testing.T, testname string, wantCalls map[str } } +func setupScheme(scheme *runtime.Scheme) { + enterpriseApi.AddToScheme(scheme) + enterpriseApiV3.AddToScheme(scheme) + appsv1.AddToScheme(scheme) + corev1.AddToScheme(scheme) + // Add other necessary APIs +} + // AddObject adds an object to the MockClient's state func (c *MockClient) SubResource(subResource string) client.SubResourceClient { src := MockSubResourceClient{} @@ -535,34 +558,82 @@ func testReconcileForResource(t *testing.T, c *MockClient, methodPlus string, re switch resource.(type) { case *enterpriseApi.Standalone: cr := resource.(*enterpriseApi.Standalone) + gvk := schema.GroupVersionKind{ + Group: "enterprise.splunk.com", + Version: "v4", + Kind: "Standalone", + } + cr.SetGroupVersionKind(gvk) c.Create(context.Background(), cr) case *enterpriseApiV3.LicenseMaster: cr := resource.(*enterpriseApiV3.LicenseMaster) + gvk := schema.GroupVersionKind{ + Group: "enterprise.splunk.com", + Version: "v4", + Kind: "LicenseMaster", + } + cr.SetGroupVersionKind(gvk) c.Create(context.Background(), cr) case *enterpriseApi.LicenseManager: cr := resource.(*enterpriseApi.LicenseManager) + gvk := schema.GroupVersionKind{ + Group: "enterprise.splunk.com", + Version: "v4", + Kind: "LicenseManager", + } + cr.SetGroupVersionKind(gvk) c.Create(context.Background(), cr) case *enterpriseApi.IndexerCluster: cr := resource.(*enterpriseApi.IndexerCluster) + gvk := schema.GroupVersionKind{ + Group: "enterprise.splunk.com", + Version: "v4", + Kind: "IndexerCluster", + } + cr.SetGroupVersionKind(gvk) c.Create(context.Background(), cr) case *enterpriseApiV3.ClusterMaster: cr := resource.(*enterpriseApiV3.ClusterMaster) + gvk := schema.GroupVersionKind{ + Group: "enterprise.splunk.com", + Version: "v4", + Kind: "ClusterMaster", + } + cr.SetGroupVersionKind(gvk) c.Create(context.Background(), cr) case *enterpriseApi.ClusterManager: cr := resource.(*enterpriseApi.ClusterManager) + gvk := schema.GroupVersionKind{ + Group: "enterprise.splunk.com", + Version: "v4", + Kind: "ClusterManager", + } + cr.SetGroupVersionKind(gvk) c.Create(context.Background(), cr) case *enterpriseApi.MonitoringConsole: cr := resource.(*enterpriseApi.MonitoringConsole) + gvk := schema.GroupVersionKind{ + Group: "enterprise.splunk.com", + Version: "v4", + Kind: "MonitoringConsole", + } + cr.SetGroupVersionKind(gvk) c.Create(context.Background(), cr) case *enterpriseApi.SearchHeadCluster: cr := resource.(*enterpriseApi.SearchHeadCluster) + gvk := schema.GroupVersionKind{ + Group: "enterprise.splunk.com", + Version: "v4", + Kind: "SearchHeadCluster", + } + cr.SetGroupVersionKind(gvk) c.Create(context.Background(), cr) } diff --git a/pkg/splunk/test/util.go b/pkg/splunk/test/util.go index a59b65e05..8e21b2d74 100644 --- a/pkg/splunk/test/util.go +++ b/pkg/splunk/test/util.go @@ -32,6 +32,21 @@ import ( "k8s.io/client-go/tools/remotecommand" ) +// GetMockPerCRConfigMap returns per cr configmap +func GetMockPerCRConfigMap(name string) corev1.ConfigMap { + // Create S3 secret + cfg := corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: name, + Namespace: "test", + }, + Data: map[string]string{ + "manualUpdate": "false", + }, + } + return cfg +} + // GetMockS3SecretKeys returns S3 secret keys func GetMockS3SecretKeys(name string) corev1.Secret { accessKey := []byte{'1'}