From c0bd4ab7d0820e8f678781442675db993415c022 Mon Sep 17 00:00:00 2001 From: Dylan Arbour Date: Sun, 4 Apr 2021 17:05:07 -0400 Subject: [PATCH] Test values overrides Adds a test that loads the helmChart from the updated resource and verifies that `testOverride` (the value overrode in the test fixtures) changes from `false` to `true`. Signed-off-by: Dylan Arbour --- controllers/helmchart_controller_test.go | 98 +++++++++++++------ .../testdata/charts/helmchart/override.yaml | 4 + .../testdata/charts/helmchart/values.yaml | 4 + .../charts/helmchartwithdeps/override.yaml | 70 +++++++++++++ .../charts/helmchartwithdeps/values.yaml | 4 + 5 files changed, 150 insertions(+), 30 deletions(-) create mode 100644 controllers/testdata/charts/helmchartwithdeps/override.yaml diff --git a/controllers/helmchart_controller_test.go b/controllers/helmchart_controller_test.go index 0c39e482..ddda8d49 100644 --- a/controllers/helmchart_controller_test.go +++ b/controllers/helmchart_controller_test.go @@ -133,6 +133,10 @@ var _ = Describe("HelmChartReconciler", func() { _ = k8sClient.Get(context.Background(), key, got) return got.Status.Artifact != nil && storage.ArtifactExist(*got.Status.Artifact) }, timeout, interval).Should(BeTrue()) + helmChart, err := loader.Load(storage.LocalPath(*got.Status.Artifact)) + Expect(err).NotTo(HaveOccurred()) + Expect(helmChart.Values["testDefault"]).To(BeTrue()) + Expect(helmChart.Values["testOverride"]).To(BeFalse()) By("Packaging a new chart version and regenerating the index") Expect(helmServer.PackageChartWithVersion(path.Join("testdata/charts/helmchart"), "0.2.0")).Should(Succeed()) @@ -585,13 +589,55 @@ var _ = Describe("HelmChartReconciler", func() { Expect(err).NotTo(HaveOccurred()) By("Expecting new artifact revision and GC") + now := &sourcev1.HelmChart{} Eventually(func() bool { - now := &sourcev1.HelmChart{} _ = k8sClient.Get(context.Background(), key, now) // Test revision change and garbage collection return now.Status.Artifact.Revision != got.Status.Artifact.Revision && !storage.ArtifactExist(*got.Status.Artifact) }, timeout, interval).Should(BeTrue()) + helmChart, err := loader.Load(storage.LocalPath(*now.Status.Artifact)) + Expect(err).NotTo(HaveOccurred()) + Expect(helmChart.Values["testDefault"]).To(BeTrue()) + Expect(helmChart.Values["testOverride"]).To(BeFalse()) + + When("Setting valid valuesFile attribute", func() { + updated := &sourcev1.HelmChart{} + Expect(k8sClient.Get(context.Background(), key, updated)).To(Succeed()) + updated.Spec.ValuesFile = "./testdata/charts/helmchart/override.yaml" + Expect(k8sClient.Update(context.Background(), updated)).To(Succeed()) + got := &sourcev1.HelmChart{} + Eventually(func() bool { + _ = k8sClient.Get(context.Background(), key, got) + return got.Status.Artifact.Checksum != updated.Status.Artifact.Checksum && + storage.ArtifactExist(*got.Status.Artifact) + }, timeout, interval).Should(BeTrue()) + f, err := os.Stat(storage.LocalPath(*got.Status.Artifact)) + Expect(err).NotTo(HaveOccurred()) + Expect(f.Size()).To(BeNumerically(">", 0)) + helmChart, err := loader.Load(storage.LocalPath(*got.Status.Artifact)) + Expect(err).NotTo(HaveOccurred()) + Expect(helmChart.Values["testOverride"]).To(BeTrue()) + }) + + When("Setting invalid valuesFile attribute", func() { + updated := &sourcev1.HelmChart{} + Expect(k8sClient.Get(context.Background(), key, updated)).To(Succeed()) + updated.Spec.ValuesFile = "./testdata/charts/helmchart/invalid.yaml" + Expect(k8sClient.Update(context.Background(), updated)).To(Succeed()) + got := &sourcev1.HelmChart{} + Eventually(func() bool { + _ = k8sClient.Get(context.Background(), key, got) + return got.Status.ObservedGeneration > updated.Status.ObservedGeneration && + storage.ArtifactExist(*got.Status.Artifact) + }, timeout, interval).Should(BeTrue()) + f, err := os.Stat(storage.LocalPath(*got.Status.Artifact)) + Expect(err).NotTo(HaveOccurred()) + Expect(f.Size()).To(BeNumerically(">", 0)) + helmChart, err := loader.Load(storage.LocalPath(*got.Status.Artifact)) + Expect(err).NotTo(HaveOccurred()) + Expect(helmChart.Values["testOverride"]).To(BeTrue()) + }) }) It("Creates artifacts with .tgz file", func() { @@ -693,31 +739,6 @@ var _ = Describe("HelmChartReconciler", func() { return got.Status.Artifact != nil && storage.ArtifactExist(*got.Status.Artifact) }, timeout, interval).Should(BeTrue()) - - When("Setting valid valuesFile attribute", func() { - updated := &sourcev1.HelmChart{} - Expect(k8sClient.Get(context.Background(), key, updated)).To(Succeed()) - chart.Spec.ValuesFile = "./charts/helmchart/override.yaml" - Expect(k8sClient.Update(context.Background(), updated)).To(Succeed()) - got := &sourcev1.HelmChart{} - Eventually(func() bool { - _ = k8sClient.Get(context.Background(), key, got) - return got.Status.Artifact != nil && - storage.ArtifactExist(*got.Status.Artifact) - }, timeout, interval).Should(BeTrue()) - }) - - When("Setting invalid valuesFile attribute", func() { - updated := &sourcev1.HelmChart{} - Expect(k8sClient.Get(context.Background(), key, updated)).To(Succeed()) - chart.Spec.ValuesFile = "invalid.yaml" - Expect(k8sClient.Update(context.Background(), updated)).To(Succeed()) - got := &sourcev1.HelmChart{} - Eventually(func() bool { - _ = k8sClient.Get(context.Background(), key, got) - return got.Status.Artifact != nil && got.Status.Artifact.Revision == updated.Status.Artifact.Revision - }, timeout, interval).Should(BeTrue()) - }) }) }) @@ -961,30 +982,47 @@ var _ = Describe("HelmChartReconciler", func() { return got.Status.Artifact != nil && storage.ArtifactExist(*got.Status.Artifact) }, timeout, interval).Should(BeTrue()) + helmChart, err := loader.Load(storage.LocalPath(*got.Status.Artifact)) + Expect(err).NotTo(HaveOccurred()) + Expect(helmChart.Values["testDefault"]).To(BeTrue()) + Expect(helmChart.Values["testOverride"]).To(BeFalse()) When("Setting valid valuesFile attribute", func() { updated := &sourcev1.HelmChart{} Expect(k8sClient.Get(context.Background(), key, updated)).To(Succeed()) - chart.Spec.ValuesFile = "override.yaml" + updated.Spec.ValuesFile = "./testdata/charts/helmchartwithdeps/override.yaml" Expect(k8sClient.Update(context.Background(), updated)).To(Succeed()) got := &sourcev1.HelmChart{} Eventually(func() bool { _ = k8sClient.Get(context.Background(), key, got) - return got.Status.Artifact != nil && + return got.Status.Artifact.Checksum != updated.Status.Artifact.Checksum && storage.ArtifactExist(*got.Status.Artifact) }, timeout, interval).Should(BeTrue()) + f, err := os.Stat(storage.LocalPath(*got.Status.Artifact)) + Expect(err).NotTo(HaveOccurred()) + Expect(f.Size()).To(BeNumerically(">", 0)) + helmChart, err := loader.Load(storage.LocalPath(*got.Status.Artifact)) + Expect(err).NotTo(HaveOccurred()) + Expect(helmChart.Values["testOverride"]).To(BeTrue()) }) When("Setting invalid valuesFile attribute", func() { updated := &sourcev1.HelmChart{} Expect(k8sClient.Get(context.Background(), key, updated)).To(Succeed()) - chart.Spec.ValuesFile = "./charts/helmchart/override.yaml" + updated.Spec.ValuesFile = "./testdata/charts/helmchartwithdeps/invalid.yaml" Expect(k8sClient.Update(context.Background(), updated)).To(Succeed()) got := &sourcev1.HelmChart{} Eventually(func() bool { _ = k8sClient.Get(context.Background(), key, got) - return got.Status.Artifact != nil && got.Status.Artifact.Revision == updated.Status.Artifact.Revision + return got.Status.ObservedGeneration > updated.Status.ObservedGeneration && + storage.ArtifactExist(*got.Status.Artifact) }, timeout, interval).Should(BeTrue()) + f, err := os.Stat(storage.LocalPath(*got.Status.Artifact)) + Expect(err).NotTo(HaveOccurred()) + Expect(f.Size()).To(BeNumerically(">", 0)) + helmChart, err := loader.Load(storage.LocalPath(*got.Status.Artifact)) + Expect(err).NotTo(HaveOccurred()) + Expect(helmChart.Values["testOverride"]).To(BeTrue()) }) }) }) diff --git a/controllers/testdata/charts/helmchart/override.yaml b/controllers/testdata/charts/helmchart/override.yaml index e08cec5b..d01d3acf 100644 --- a/controllers/testdata/charts/helmchart/override.yaml +++ b/controllers/testdata/charts/helmchart/override.yaml @@ -64,3 +64,7 @@ nodeSelector: {} tolerations: [] affinity: {} + +# Values for tests +# testDefault: true +testOverride: true diff --git a/controllers/testdata/charts/helmchart/values.yaml b/controllers/testdata/charts/helmchart/values.yaml index 40e7aa0b..5f7ae58b 100644 --- a/controllers/testdata/charts/helmchart/values.yaml +++ b/controllers/testdata/charts/helmchart/values.yaml @@ -64,3 +64,7 @@ nodeSelector: {} tolerations: [] affinity: {} + +# Values for tests +testDefault: true +testOverride: false diff --git a/controllers/testdata/charts/helmchartwithdeps/override.yaml b/controllers/testdata/charts/helmchartwithdeps/override.yaml new file mode 100644 index 00000000..d01d3acf --- /dev/null +++ b/controllers/testdata/charts/helmchartwithdeps/override.yaml @@ -0,0 +1,70 @@ +# Override values for helmchart. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 3 + +image: + repository: nginx + pullPolicy: IfNotPresent + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + +serviceAccount: + # Specifies whether a service account should be created + create: true + # The name of the service account to use. + # If not set and create is true, a name is generated using the fullname template + name: + +podSecurityContext: {} + # fsGroup: 2000 + +securityContext: {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true + # runAsUser: 1000 + +service: + type: ClusterIP + port: 80 + +ingress: + enabled: false + annotations: {} + # kubernetes.io/ingress.class: nginx + # kubernetes.io/tls-acme: "true" + hosts: + - host: chart-example.local + paths: [] + tls: [] + # - secretName: chart-example-tls + # hosts: + # - chart-example.local + +resources: {} + # We usually recommend not to specify default resources and to leave this as a conscious + # choice for the user. This also increases chances charts run on environments with little + # resources, such as Minikube. If you do want to specify resources, uncomment the following + # lines, adjust them as necessary, and remove the curly braces after 'resources:'. + # limits: + # cpu: 100m + # memory: 128Mi + # requests: + # cpu: 100m + # memory: 128Mi + +nodeSelector: {} + +tolerations: [] + +affinity: {} + +# Values for tests +# testDefault: true +testOverride: true diff --git a/controllers/testdata/charts/helmchartwithdeps/values.yaml b/controllers/testdata/charts/helmchartwithdeps/values.yaml index 8213f28c..cfee8151 100644 --- a/controllers/testdata/charts/helmchartwithdeps/values.yaml +++ b/controllers/testdata/charts/helmchartwithdeps/values.yaml @@ -64,3 +64,7 @@ nodeSelector: {} tolerations: [] affinity: {} + +# Values for tests +testDefault: true +testOverride: false