Commit 5f0fc70d authored by Martin Schurz's avatar Martin Schurz
Browse files

fix: add tests for ImagePullSecret

parent 7de38409
Loading
Loading
Loading
Loading
+108 −0
Original line number Diff line number Diff line
@@ -881,3 +881,111 @@ func TestCronJobTemplateWithContainerSecurityContext(t *testing.T) {
		})
	}
}

func TestCronjobImagePullSecrets(t *testing.T) {
	for _, tc := range []struct {
		CaseName                   string
		Values                     map[string]string
		Release 				   string
		ExpectedImagePullSecrets   []coreV1.LocalObjectReference
	}{
		{
			CaseName: "default secret",
			Release:  "production",
			Values: map[string]string{
				"cronjobs.job1.command[0]": "echo",
				"cronjobs.job1.args[0]":    "hello",
				"cronjobs.job2.command[0]": "echo",
				"cronjobs.job2.args[0]":    "hello",
			},

			ExpectedImagePullSecrets: []coreV1.LocalObjectReference{
				{
					Name: "gitlab-registry",
				},
			},
		},
		{
			CaseName: "present secret",
			Release:  "production",
			Values: map[string]string{
				"cronjobs.job1.command[0]": "echo",
				"cronjobs.job1.args[0]":    "hello",
				"cronjobs.job2.command[0]": "echo",
				"cronjobs.job2.args[0]":    "hello",
				"image.secrets[0].name":    "expected-secret",
			},

			ExpectedImagePullSecrets: []coreV1.LocalObjectReference{
				{
					Name: "expected-secret",
				},
			},
		},
		{
			CaseName: "multiple secrets",
			Release:  "production",
			Values: map[string]string{
				"cronjobs.job1.command[0]": "echo",
				"cronjobs.job1.args[0]":    "hello",
				"cronjobs.job2.command[0]": "echo",
				"cronjobs.job2.args[0]":    "hello",
				"image.secrets[0].name":    "expected-secret",
				"image.secrets[1].name":    "additional-secret",
			},

			ExpectedImagePullSecrets: []coreV1.LocalObjectReference{
				{
					Name: "expected-secret",
				},
				{
					Name: "additional-secret",
				},
			},
		},
		{
			CaseName: "missing secret",
			Release:  "production",
			Values: map[string]string{
				"cronjobs.job1.command[0]": "echo",
				"cronjobs.job1.args[0]":    "hello",
				"cronjobs.job2.command[0]": "echo",
				"cronjobs.job2.args[0]":    "hello",
				"image.secrets":            "null",
			},

			ExpectedImagePullSecrets: nil,
		},
	} {
		t.Run(tc.CaseName, func(t *testing.T) {
			namespaceName := "minimal-ruby-app-" + strings.ToLower(random.UniqueId())

			values := map[string]string{
				"gitlab.app": "auto-devops-examples/minimal-ruby-app",
				"gitlab.env": "prod",
			}

			mergeStringMap(values, tc.Values)

			options := &helm.Options{
				ValuesFiles:    []string{},
				SetValues:      values,
				KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName),
			}

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

			if err != nil {
				t.Error(err)
				return
			}

			var cronjobs batchV1beta1.CronJobList
			helm.UnmarshalK8SYaml(t, output, &cronjobs)

			for _, cronjob := range cronjobs.Items {
				require.Equal(t, tc.ExpectedImagePullSecrets, cronjob.Spec.JobTemplate.Spec.Template.Spec.ImagePullSecrets)
			}
		})
	}
}
 No newline at end of file
+94 −0
Original line number Diff line number Diff line
@@ -76,3 +76,97 @@ func TestInitializeDatabaseUrlEnvironmentVariable(t *testing.T) {
		})
	}
}

func TestInitializeDatabaseImagePullSecrets(t *testing.T) {
	releaseName := "initialize-application-database-image-pull-secrets"

	tcs := []struct {
		CaseName                 string
		Values                   map[string]string
		ExpectedImagePullSecrets []coreV1.LocalObjectReference
		Template                 string
	}{
		{
			CaseName: "default-secret",
			Values: map[string]string{
				"application.initializeCommand": "echo initialize",
			},
			ExpectedImagePullSecrets: []coreV1.LocalObjectReference{
				{
					Name: "gitlab-registry",
				},
			},
			Template: "templates/db-initialize-job.yaml",
		},
		{
			CaseName: "present-secret",
			Values: map[string]string{
				"application.initializeCommand": "echo initialize",
				"image.secrets[0].name": "expected-secret",
			},
			ExpectedImagePullSecrets: []coreV1.LocalObjectReference{
				{
					Name: "expected-secret",
				},
			},
			Template: "templates/db-initialize-job.yaml",
		},
		{
			CaseName: "multiple-secrets",
			Values: map[string]string{
				"application.initializeCommand": "echo initialize",
				"image.secrets[0].name": "expected-secret",
				"image.secrets[1].name": "additional-secret",
			},
			ExpectedImagePullSecrets: []coreV1.LocalObjectReference{
				{
					Name: "expected-secret",
				},
				{
					Name: "additional-secret",
				},
			},
			Template: "templates/db-initialize-job.yaml",
		},
		{
			CaseName: "missing-secret",
			Values: map[string]string{
				"application.initializeCommand": "echo initialize",
				"image.secrets": "null",
			},
			ExpectedImagePullSecrets: nil,
			Template: "templates/db-initialize-job.yaml",
		},
	}

	for _, tc := range tcs {
		t.Run(tc.CaseName, func(t *testing.T) {

			namespaceName := "minimal-ruby-app-" + strings.ToLower(random.UniqueId())

			values := map[string]string{
				"gitlab.app": "auto-devops-examples/minimal-ruby-app",
				"gitlab.env": "prod",
			}

			mergeStringMap(values, tc.Values)

			options := &helm.Options{
				SetValues:      values,
				KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName),
			}

			output, err := helm.RenderTemplateE(t, options, helmChartPath, releaseName, []string{tc.Template})

			if err != nil {
				t.Error(err)
				return
			}

			deployment := new(appsV1.Deployment)
			helm.UnmarshalK8SYaml(t, output, &deployment)

			require.Equal(t, tc.ExpectedImagePullSecrets, deployment.Spec.Template.Spec.ImagePullSecrets)
		})
	}
}
+94 −0
Original line number Diff line number Diff line
@@ -76,3 +76,97 @@ func TestMigrateDatabaseUrlEnvironmentVariable(t *testing.T) {
		})
	}
}

func TestMigrateDatabaseImagePullSecrets(t *testing.T) {
	releaseName := "migrate-application-database-image-pull-secrets"

	tcs := []struct {
		CaseName                 string
		Values                   map[string]string
		ExpectedImagePullSecrets []coreV1.LocalObjectReference
		Template                 string
	}{
		{
			CaseName: "default-secret",
			Values: map[string]string{
				"application.migrateCommand": "echo migrate",
			},
			ExpectedImagePullSecrets: []coreV1.LocalObjectReference{
				{
					Name: "gitlab-registry",
				},
			},
			Template: "templates/db-migrate-hook.yaml",
		},
		{
			CaseName: "present-secret",
			Values: map[string]string{
				"application.migrateCommand": "echo migrate",
				"image.secrets[0].name": "expected-secret",
			},
			ExpectedImagePullSecrets: []coreV1.LocalObjectReference{
				{
					Name: "expected-secret",
				},
			},
			Template: "templates/db-migrate-hook.yaml",
		},
		{
			CaseName: "multiple-secrets",
			Values: map[string]string{
				"application.migrateCommand": "echo migrate",
				"image.secrets[0].name": "expected-secret",
				"image.secrets[1].name": "additional-secret",
			},
			ExpectedImagePullSecrets: []coreV1.LocalObjectReference{
				{
					Name: "expected-secret",
				},
				{
					Name: "additional-secret",
				},
			},
			Template: "templates/db-migrate-hook.yaml",
		},
		{
			CaseName: "missing-secret",
			Values: map[string]string{
				"application.migrateCommand": "echo migrate",
				"image.secrets": "null",
			},
			ExpectedImagePullSecrets: nil,
			Template: "templates/db-migrate-hook.yaml",
		},
	}

	for _, tc := range tcs {
		t.Run(tc.CaseName, func(t *testing.T) {

			namespaceName := "minimal-ruby-app-" + strings.ToLower(random.UniqueId())

			values := map[string]string{
				"gitlab.app": "auto-devops-examples/minimal-ruby-app",
				"gitlab.env": "prod",
			}

			mergeStringMap(values, tc.Values)

			options := &helm.Options{
				SetValues:      values,
				KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName),
			}

			output, err := helm.RenderTemplateE(t, options, helmChartPath, releaseName, []string{tc.Template})

			if err != nil {
				t.Error(err)
				return
			}

			deployment := new(appsV1.Deployment)
			helm.UnmarshalK8SYaml(t, output, &deployment)

			require.Equal(t, tc.ExpectedImagePullSecrets, deployment.Spec.Template.Spec.ImagePullSecrets)
		})
	}
}
+78 −0
Original line number Diff line number Diff line
@@ -264,6 +264,84 @@ func TestDeploymentTemplate(t *testing.T) {
		})
	}

	// ImagePullSecrets
	for _, tc := range []struct {
		CaseName                   string
		Release                    string
		Values                     map[string]string
		ExpectedImagePullSecrets   []coreV1.LocalObjectReference
	}{
		{
			CaseName: "default secret",
			Release:  "production",
			Values: map[string]string{},
			ExpectedImagePullSecrets: []coreV1.LocalObjectReference{
				{
					Name: "gitlab-registry",
				},
			},
		},
		{
			CaseName: "present secret",
			Release:  "production",
			Values: map[string]string{
				"image.secrets[0].name": "expected-secret",
			},
			ExpectedImagePullSecrets: []coreV1.LocalObjectReference{
				{
					Name: "expected-secret",
				},
			},
		},
		{
			CaseName: "multiple secrets",
			Release:  "production",
			Values: map[string]string{
				"image.secrets[0].name": "expected-secret",
				"image.secrets[1].name": "additional-secret",
			},
			ExpectedImagePullSecrets: []coreV1.LocalObjectReference{
				{
					Name: "expected-secret",
				},
				{
					Name: "additional-secret",
				},
			},
		},
		{
			CaseName: "missing secret",
			Release:  "production",
			Values: map[string]string{
				"image.secrets": "null",
			},
			ExpectedImagePullSecrets: nil,
		},
	} {
		t.Run(tc.CaseName, func(t *testing.T) {
			namespaceName := "minimal-ruby-app-" + strings.ToLower(random.UniqueId())

			values := map[string]string{
				"gitlab.app": "auto-devops-examples/minimal-ruby-app",
				"gitlab.env": "prod",
			}

			mergeStringMap(values, tc.Values)

			options := &helm.Options{
				SetValues:      values,
				KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName),
			}

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

			var deployment appsV1.Deployment
			helm.UnmarshalK8SYaml(t, output, &deployment)

			require.Equal(t, tc.ExpectedImagePullSecrets, deployment.Spec.Template.Spec.ImagePullSecrets)
		})
	}

	// serviceAccountName
	for _, tc := range []struct {
		CaseName                   string
+25 −0
Original line number Diff line number Diff line
@@ -348,6 +348,18 @@ func TestWorkerDeploymentTemplate(t *testing.T) {

		ExpectedImagePullSecrets []coreV1.LocalObjectReference
	}{
		{
			CaseName: "global image secrets default",
			Release:  "production",
			Values: map[string]string{
				"workers.worker1.command[0]": "echo",
			},
			ExpectedImagePullSecrets: []coreV1.LocalObjectReference{
				{
					Name: "gitlab-registry",
				},
			},
		},
		{
			CaseName: "worker image secrets are defined",
			Release:  "production",
@@ -360,6 +372,19 @@ func TestWorkerDeploymentTemplate(t *testing.T) {
				},
			},
		},
		{
			CaseName: "global image secrets are defined",
			Release:  "production",
			Values: map[string]string{
				"image.secrets[0].name": "expected-secret",
				"workers.worker1.command[0]": "echo",
			},
			ExpectedImagePullSecrets: []coreV1.LocalObjectReference{
				{
					Name: "expected-secret",
				},
			},
		},
	} {
		t.Run(tc.CaseName, func(t *testing.T) {
			namespaceName := "minimal-ruby-app-" + strings.ToLower(random.UniqueId())