Commit 22f10278 authored by Thong Kuah's avatar Thong Kuah
Browse files

Merge branch 'use-helm-3-for-chart-test' into 'beta'

chore: Use Helm 3 for chart test

See merge request gitlab-org/cluster-integration/auto-deploy-image!115
parents 9ef39b42 fa1acf95
Loading
Loading
Loading
Loading
+12 −17
Original line number Diff line number Diff line
variables:
  SAST_DISABLE_DIND: "true"
  SCAN_KUBERNETES_MANIFESTS: "true"
  SAST_DEFAULT_ANALYZERS: "kubesec,secrets"

include:
  - template: SAST.gitlab-ci.yml

stages:
  - build
  - test
  - release

.chart-job:
  image: "registry.gitlab.com/gitlab-org/gitlab-build-images:alpine-helm"
  image: "$BUILD_IMAGE_NAME"
  before_script:
    - cd assets/auto-deploy-app

chart:compile_manifests:
  extends: .chart-job
  stage: build
  script:
    - mkdir manifests
    - helm init --client-only
    - helm dependency build .
    - helm template -f values.yaml --output-dir manifests .
  artifacts:
    paths:
      - manifests

chart:lint:
  extends: .chart-job
  stage: test
@@ -35,14 +21,23 @@ chart:lint:
    - helm lint .

kubesec-sast:
  needs: ["chart:compile_manifests"]
  variables:
    KUBESEC_HELM_CHARTS_PATH: 'assets/auto-deploy-app'
    KUBESEC_HELM_OPTIONS: '-f assets/auto-deploy-app/values.yaml'
  before_script:
    - cd assets/auto-deploy-app
    - helm repo add stable https://kubernetes-charts.storage.googleapis.com/
    - helm dependency build .
  # we are using merge request pipelines, so we need to override the rules
  rules:
    - if: $SCAN_KUBERNETES_MANIFESTS == 'true'

chart:test:
  extends: .chart-job
  stage: test
  script:
    - apk add --no-cache build-base go
    - helm init --client-only
    - helm repo add stable https://kubernetes-charts.storage.googleapis.com/
    - helm dependency build .
    - cd test && GO111MODULE=auto go test .

+2 −2
Original line number Diff line number Diff line
@@ -46,8 +46,8 @@ on the tests, you need to have [Helm 2](https://v2.helm.sh/docs/) and
To run the tests, run the following commands from the root of your copy of `auto-deploy-app`:

```shell
helm init --client-only               # required only once
helm dependency build .               # required only once
helm repo add stable https://kubernetes-charts.storage.googleapis.com/ # required only once
helm dependency build .               # required any time the dependencies change
cd test
GO111MODULE=auto go test .            # required for every change to the tests or the template
```
+3 −3
Original line number Diff line number Diff line
@@ -3,8 +3,8 @@ module gitlab.com/gitlab-org/charts/auto-deploy-app/test
go 1.13

require (
	github.com/gruntwork-io/terratest v0.23.0
	github.com/gruntwork-io/terratest v0.28.15
	github.com/stretchr/testify v1.4.0
	k8s.io/api v0.0.0-20181110191121-a33c8200050f
	k8s.io/apimachinery v0.0.0-20190704094520-6f131bee5e2c
	k8s.io/api v0.18.3
	k8s.io/apimachinery v0.18.3
)
+389 −25

File changed.

Preview size limit exceeded, changes collapsed.

+118 −78
Original line number Diff line number Diff line
package main

import (
	"regexp"
	"strings"
	"testing"

@@ -26,6 +27,8 @@ func TestDeploymentTemplate(t *testing.T) {
		Release  string
		Values   map[string]string

		ExpectedErrorRegexp *regexp.Regexp

		ExpectedName         string
		ExpectedRelease      string
		ExpectedStrategyType extensions.DeploymentStrategyType
@@ -40,13 +43,12 @@ func TestDeploymentTemplate(t *testing.T) {
			ExpectedName:         "productionOverridden",
			ExpectedRelease:      "production",
			ExpectedStrategyType: extensions.DeploymentStrategyType(""),
		},
		{
		}, {
			// See https://github.com/helm/helm/issues/6006
			CaseName: "long release name",
			Release:  strings.Repeat("r", 80),
			ExpectedName:         strings.Repeat("r", 63),
			ExpectedRelease:      strings.Repeat("r", 80),
			ExpectedStrategyType: extensions.DeploymentStrategyType(""),

			ExpectedErrorRegexp: regexp.MustCompile("Error: release name .* exceeds max length of 53"),
		},
		{
			CaseName: "strategyType",
@@ -92,7 +94,16 @@ func TestDeploymentTemplate(t *testing.T) {
				KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName),
			}

			output := helm.RenderTemplate(t, options, helmChartPath, tc.Release, []string{"templates/deployment.yaml"})
			output, err := helm.RenderTemplateE(t, options, helmChartPath, tc.Release, []string{"templates/deployment.yaml"})

			if tc.ExpectedErrorRegexp != nil {
				require.Regexp(t, tc.ExpectedErrorRegexp, err.Error())
				return
			}
			if err != nil {
				t.Error(err)
				return
			}

			var deployment extensions.Deployment
			helm.UnmarshalK8SYaml(t, output, &deployment)
@@ -107,7 +118,7 @@ func TestDeploymentTemplate(t *testing.T) {
			require.Equal(t, map[string]string{
				"app":      tc.ExpectedName,
				"chart":    chartName,
				"heritage": "Tiller",
				"heritage": "Helm",
				"release":  tc.ExpectedRelease,
				"tier":     "web",
				"track":    "stable",
@@ -236,7 +247,7 @@ func TestDeploymentTemplate(t *testing.T) {
			require.Equal(t, map[string]string{
				"app":      tc.ExpectedName,
				"chart":    chartName,
				"heritage": "Tiller",
				"heritage": "Helm",
				"release":  tc.ExpectedRelease,
				"tier":     "web",
				"track":    "stable",
@@ -265,6 +276,8 @@ func TestWorkerDeploymentTemplate(t *testing.T) {
		Release  string
		Values   map[string]string

		ExpectedErrorRegexp *regexp.Regexp

		ExpectedName        string
		ExpectedRelease     string
		ExpectedDeployments []workerDeploymentTestCase
@@ -293,23 +306,12 @@ func TestWorkerDeploymentTemplate(t *testing.T) {
					ExpectedStrategyType: extensions.DeploymentStrategyType(""),
				},
			},
		},
		{
		}, {
			// See https://github.com/helm/helm/issues/6006
			CaseName: "long release name",
			Release:  strings.Repeat("r", 80),
			Values: map[string]string{
				"workers.worker1.command[0]": "echo",
				"workers.worker1.command[1]": "worker1",
			},
			ExpectedName:    strings.Repeat("r", 63),
			ExpectedRelease: strings.Repeat("r", 80),
			ExpectedDeployments: []workerDeploymentTestCase{
				{
					ExpectedName:         strings.Repeat("r", 63) + "-worker1",
					ExpectedCmd:          []string{"echo", "worker1"},
					ExpectedStrategyType: extensions.DeploymentStrategyType(""),
				},
			},

			ExpectedErrorRegexp: regexp.MustCompile("Error: release name .* exceeds max length of 53"),
		},
		{
			CaseName: "strategyType",
@@ -384,7 +386,16 @@ func TestWorkerDeploymentTemplate(t *testing.T) {
				KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName),
			}

			output := helm.RenderTemplate(t, options, helmChartPath, tc.Release, []string{"templates/worker-deployment.yaml"})
			output, err := helm.RenderTemplateE(t, options, helmChartPath, tc.Release, []string{"templates/worker-deployment.yaml"})

			if tc.ExpectedErrorRegexp != nil {
				require.Regexp(t, tc.ExpectedErrorRegexp, err.Error())
				return
			}
			if err != nil {
				t.Error(err)
				return
			}

			var deployments deploymentList
			helm.UnmarshalK8SYaml(t, output, &deployments)
@@ -402,7 +413,7 @@ func TestWorkerDeploymentTemplate(t *testing.T) {
				}, deployment.Annotations)
				require.Equal(t, map[string]string{
					"chart":    chartName,
					"heritage": "Tiller",
					"heritage": "Helm",
					"release":  tc.ExpectedRelease,
					"tier":     "worker",
					"track":    "stable",
@@ -509,7 +520,7 @@ func TestWorkerDeploymentTemplate(t *testing.T) {
				}, deployment.Annotations)
				require.Equal(t, map[string]string{
					"chart":    chartName,
					"heritage": "Tiller",
					"heritage": "Helm",
					"release":  tc.ExpectedRelease,
					"tier":     "worker",
					"track":    "stable",
@@ -542,7 +553,7 @@ func TestNetworkPolicyDeployment(t *testing.T) {
		"app":      releaseName,
		"chart":    chartName,
		"release":  releaseName,
		"heritage": "Tiller",
		"heritage": "Helm",
	}

	tcs := []struct {
@@ -550,6 +561,8 @@ func TestNetworkPolicyDeployment(t *testing.T) {
		valueFiles []string
		values     map[string]string

		expectedErrorRegexp *regexp.Regexp

		meta        metav1.ObjectMeta
		podSelector metav1.LabelSelector
		policyTypes []netV1.PolicyType
@@ -557,7 +570,8 @@ func TestNetworkPolicyDeployment(t *testing.T) {
		egress      []netV1.NetworkPolicyEgressRule
	}{
		{
			name: "defaults",
			name:                "disabled by default",
			expectedErrorRegexp: regexp.MustCompile("Error: could not find template templates/network-policy.yaml in chart"),
		},
		{
			name:        "with default policy",
@@ -622,7 +636,16 @@ func TestNetworkPolicyDeployment(t *testing.T) {
				ValuesFiles: tc.valueFiles,
				SetValues:   tc.values,
			}
			output := helm.RenderTemplate(t, opts, helmChartPath, releaseName, templates)
			output, err := helm.RenderTemplateE(t, opts, helmChartPath, releaseName, templates)

			if tc.expectedErrorRegexp != nil {
				require.Regexp(t, tc.expectedErrorRegexp, err.Error())
				return
			}
			if err != nil {
				t.Error(err)
				return
			}

			policy := new(netV1.NetworkPolicy)
			helm.UnmarshalK8SYaml(t, output, policy)
@@ -686,7 +709,7 @@ SecRule REQUEST_HEADERS:Content-Type \"text/plain\" \"log,deny,id:\'20010\',stat
				ValuesFiles: tc.valueFiles,
				SetValues:   tc.values,
			}
			output := helm.RenderTemplate(t, opts, helmChartPath, "", templates)
			output := helm.RenderTemplate(t, opts, helmChartPath, "ModSecurity-test-release", templates)

			ingress := new(extensions.Ingress)
			helm.UnmarshalK8SYaml(t, output, ingress)
@@ -703,51 +726,52 @@ func TestIngressTemplate_Disable(t *testing.T) {
		name   string
		values map[string]string

		expectedrelease string
		expectedName        string
		expectedErrorRegexp *regexp.Regexp
	}{
		{
			name:         "defaults",
			expectedrelease: releaseName + "-auto-deploy",
			expectedName: releaseName + "-auto-deploy",
		},
		{
			name:         "with ingress.enabled key undefined, but service is enabled",
			values:       map[string]string{"ingress.enabled": "null", "service.enabled": "true"},
			expectedrelease: releaseName + "-auto-deploy",
			expectedName: releaseName + "-auto-deploy",
		},
		{
			name:                "with service enabled and track non-stable",
			values:              map[string]string{"service.enabled": "true", "application.track": "non-stable"},
			expectedrelease: "",
			expectedErrorRegexp: regexp.MustCompile("Error: could not find template templates/ingress.yaml in chart"),
		},
		{
			name:                "with service disabled and track stable",
			values:              map[string]string{"service.enabled": "false", "application.track": "stable"},
			expectedrelease: "",
			expectedErrorRegexp: regexp.MustCompile("Error: could not find template templates/ingress.yaml in chart"),
		},
		{
			name:                "with service disabled and track non-stable",
			values:              map[string]string{"service.enabled": "false", "application.track": "non-stable"},
			expectedrelease: "",
			expectedErrorRegexp: regexp.MustCompile("Error: could not find template templates/ingress.yaml in chart"),
		},
		{
			name:                "with ingress disabled",
			values:              map[string]string{"ingress.enabled": "false"},
			expectedrelease: "",
			expectedErrorRegexp: regexp.MustCompile("Error: could not find template templates/ingress.yaml in chart"),
		},
		{
			name:                "with ingress enabled and track non-stable",
			values:              map[string]string{"ingress.enabled": "true", "application.track": "non-stable"},
			expectedrelease: "",
			expectedErrorRegexp: regexp.MustCompile("Error: could not find template templates/ingress.yaml in chart"),
		},
		{
			name:                "with ingress enabled and service disabled",
			values:              map[string]string{"ingress.enabled": "true", "service.enabled": "false"},
			expectedrelease: "",
			expectedErrorRegexp: regexp.MustCompile("Error: could not find template templates/ingress.yaml in chart"),
		},
		{
			name:                "with ingress disabled and service enabled and track stable",
			values:              map[string]string{"ingress.enabled": "false", "service.enabled": "true", "application.track": "stable"},
			expectedrelease: "",
			expectedErrorRegexp: regexp.MustCompile("Error: could not find template templates/ingress.yaml in chart"),
		},
	}

@@ -756,12 +780,20 @@ func TestIngressTemplate_Disable(t *testing.T) {
			opts := &helm.Options{
				SetValues: tc.values,
			}
			output := helm.RenderTemplate(t, opts, helmChartPath, releaseName, templates)
			output, err := helm.RenderTemplateE(t, opts, helmChartPath, releaseName, templates)

			ingress := new(extensions.Ingress)
			if tc.expectedErrorRegexp != nil {
				require.Regexp(t, tc.expectedErrorRegexp, err.Error())
				return
			}
			if err != nil {
				t.Error(err)
				return
			}

			ingress := new(extensions.Ingress)
			helm.UnmarshalK8SYaml(t, output, ingress)
			require.Equal(t, tc.expectedrelease, ingress.ObjectMeta.Name)
			require.Equal(t, tc.expectedName, ingress.ObjectMeta.Name)
		})
	}
}
@@ -773,26 +805,27 @@ func TestServiceTemplate_Disable(t *testing.T) {
		name   string
		values map[string]string

		expectedrelease string
		expectedName        string
		expectedErrorRegexp *regexp.Regexp
	}{
		{
			name:         "defaults",
			expectedrelease: releaseName + "-auto-deploy",
			expectedName: releaseName + "-auto-deploy",
		},
		{
			name:                "with service enabled and track non-stable",
			values:              map[string]string{"service.enabled": "true", "application.track": "non-stable"},
			expectedrelease: "",
			expectedErrorRegexp: regexp.MustCompile("Error: could not find template templates/service.yaml in chart"),
		},
		{
			name:                "with service disabled and track stable",
			values:              map[string]string{"service.enabled": "false", "application.track": "stable"},
			expectedrelease: "",
			expectedErrorRegexp: regexp.MustCompile("Error: could not find template templates/service.yaml in chart"),
		},
		{
			name:                "with service disabled and track non-stable",
			values:              map[string]string{"service.enabled": "false", "application.track": "non-stable"},
			expectedrelease: "",
			expectedErrorRegexp: regexp.MustCompile("Error: could not find template templates/service.yaml in chart"),
		},
	}

@@ -801,13 +834,20 @@ func TestServiceTemplate_Disable(t *testing.T) {
			opts := &helm.Options{
				SetValues: tc.values,
			}
			output := helm.RenderTemplate(t, opts, helmChartPath, releaseName, templates)
			output, err := helm.RenderTemplateE(t, opts, helmChartPath, releaseName, templates)

			service := new(coreV1.Service)
			if tc.expectedErrorRegexp != nil {
				require.Regexp(t, tc.expectedErrorRegexp, err.Error())
				return
			}
			if err != nil {
				t.Error(err)
				return
			}

			service := new(coreV1.Service)
			helm.UnmarshalK8SYaml(t, output, service)

			require.Equal(t, tc.expectedrelease, service.ObjectMeta.Name)
			require.Equal(t, tc.expectedName, service.ObjectMeta.Name)
		})
	}
}
+1 −1

File changed.

Contains only whitespace changes.

Loading