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()