From c9c8ea20e51d7660081d857b992b8e410475e4f6 Mon Sep 17 00:00:00 2001 From: Abhijit Mukherjee Date: Mon, 20 May 2024 21:48:17 +0530 Subject: [PATCH] Add Helm Test to cover additional attributes introduced in values.yaml used during kanister operator installation (#2881) * Add support for --dry-run in Helm Client Signed-off-by: Rajat Gupta * refactor app installs to set dryRun as false Signed-off-by: Rajat Gupta * Add support for send output of helm install with dryRun enabled Signed-off-by: Rajat Gupta * Add Helper Functions for Helm Installation Output Signed-off-by: Rajat Gupta * Address review comments Signed-off-by: Abhijit Mukherjee * Address lint error Signed-off-by: Abhijit Mukherjee * Initial commit Signed-off-by: Abhijit Mukherjee * Fixed test cases failures Signed-off-by: Abhijit Mukherjee * Address review comments Signed-off-by: Abhijit Mukherjee * Address review comments Signed-off-by: Abhijit Mukherjee * Added [nit] Signed-off-by: Abhijit Mukherjee * Address comments Signed-off-by: Abhijit Mukherjee * Address review comments Signed-off-by: Abhijit Mukherjee * Removed un-used func Signed-off-by: Abhijit Mukherjee * Fix lint error and helm test error Signed-off-by: Abhijit Mukherjee * Addressed review comments. Refactor func to include filter Signed-off-by: Abhijit Mukherjee * Address refactoring review comments Signed-off-by: Abhijit Mukherjee * Address review comments Signed-off-by: Abhijit Mukherjee * Updated doc comments and removed unnecessary comments Signed-off-by: Abhijit Mukherjee * Minor refactor Signed-off-by: Abhijit Mukherjee * Address review comments Signed-off-by: Abhijit Mukherjee * Updated test comparison Signed-off-by: Abhijit Mukherjee * Shortened func comments Signed-off-by: Abhijit Mukherjee * Addressed review comments Signed-off-by: Abhijit Mukherjee * Update helm helper func name and usages Signed-off-by: Abhijit Mukherjee * Addressed review comment Signed-off-by: Abhijit Mukherjee --------- Signed-off-by: Rajat Gupta Signed-off-by: Abhijit Mukherjee Co-authored-by: Rajat Gupta <37516416+r4rajat@users.noreply.github.com> Co-authored-by: Rajat Gupta --- pkg/helm/helm_helpers.go | 4 ++ pkg/testing/helm/helm_test.go | 93 +++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/pkg/helm/helm_helpers.go b/pkg/helm/helm_helpers.go index f1ca11c641..d0dc397d25 100644 --- a/pkg/helm/helm_helpers.go +++ b/pkg/helm/helm_helpers.go @@ -32,6 +32,10 @@ type k8sObj struct { type K8sObjectType string +const ( + K8sObjectTypeDeployment K8sObjectType = "deployment" +) + type RenderedResource struct { name string // renderedManifest holds the dry run raw yaml of the resource. diff --git a/pkg/testing/helm/helm_test.go b/pkg/testing/helm/helm_test.go index 1ad86a1507..aa96208291 100644 --- a/pkg/testing/helm/helm_test.go +++ b/pkg/testing/helm/helm_test.go @@ -20,6 +20,8 @@ import ( "testing" . "gopkg.in/check.v1" + appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" @@ -106,6 +108,97 @@ func (h *HelmTestSuite) TestResourcesFromManifestAfterDryRunHelmInstall(c *C) { c.Assert(len(resources) > 0, Equals, true) } +// TestSelectedDeploymentAttrFromKanisterHelmDryRunInstall test case does a dry run install of the `kanister` helm chart and validates +// use cases for `nodeSelector` and `toleration` attributes in the helmValues.yaml. This function is specific to `deployment` resource. +func (h *HelmTestSuite) TestSelectedDeploymentAttrFromKanisterHelmDryRunInstall(c *C) { + nodeSelector := map[string]string{ + "selector-key": "selector-value", + } + toleration := []corev1.Toleration{ + { + Key: "taint-key", + Operator: corev1.TolerationOpEqual, + Value: "taint-value", + Effect: corev1.TaintEffectNoSchedule, + }, + } + + var testCases = []struct { + testName string + helmValues map[string]string + expectedNodeSelector map[string]string + expectedTolerations []corev1.Toleration + }{ + { + testName: "Both nodeSelector and tolerations are present", + helmValues: map[string]string{ + "bpValidatingWebhook.enabled": "false", + "nodeSelector.selector-key": "selector-value", + "tolerations[0].key": "taint-key", + "tolerations[0].operator": "Equal", + "tolerations[0].value": "taint-value", + "tolerations[0].effect": "NoSchedule", + }, + expectedNodeSelector: nodeSelector, + expectedTolerations: toleration, + }, + { + testName: "Only nodeSelector is present", + helmValues: map[string]string{ + "bpValidatingWebhook.enabled": "false", + "nodeSelector.selector-key": "selector-value", + }, + expectedNodeSelector: nodeSelector, + expectedTolerations: nil, + }, + { + testName: "Only tolerations is present", + helmValues: map[string]string{ + "bpValidatingWebhook.enabled": "false", + "tolerations[0].key": "taint-key", + "tolerations[0].operator": "Equal", + "tolerations[0].value": "taint-value", + "tolerations[0].effect": "NoSchedule", + }, + expectedNodeSelector: nil, + expectedTolerations: toleration, + }, + { + testName: "Both nodeSelector and tolerations are not present", + helmValues: map[string]string{ + "bpValidatingWebhook.enabled": "false", + }, + expectedNodeSelector: nil, + expectedTolerations: nil, + }, + } + for _, tc := range testCases { + c.Logf("Test name:%s ", tc.testName) + defer func() { + h.helmApp.dryRun = false + }() + // Installing kanister release from local kanister-operator - Dry run" + testApp, err := NewHelmApp(tc.helmValues, kanisterName, "../../../helm/kanister-operator", kanisterName, "", true) + c.Assert(err, IsNil) + + out, err := testApp.Install() + c.Assert(err, IsNil) + resources := helm.ResourcesFromRenderedManifest(out, func(kind helm.K8sObjectType) bool { + return kind == helm.K8sObjectTypeDeployment + }) + c.Assert(len(resources) > 0, Equals, true) + // Take the deployment resources + deployments, err := helm.K8sObjectsFromRenderedResources[*appsv1.Deployment](resources) + c.Assert(err, IsNil) + // Use only the required deployment + var obj = deployments[h.deploymentName] + c.Assert(obj, NotNil) + + c.Assert(obj.Spec.Template.Spec.NodeSelector, DeepEquals, tc.expectedNodeSelector) + c.Assert(obj.Spec.Template.Spec.Tolerations, DeepEquals, tc.expectedTolerations) + } +} + func (h *HelmTestSuite) TearDownSuite(c *C) { c.Log("Uninstalling chart") err := h.helmApp.Uninstall()