diff --git a/assets/auto-deploy-app/test/templates/deployment_test.go b/assets/auto-deploy-app/test/templates/deployment_test.go index 215cb53f848a3de2b002c9a6c04297d2c1405ea2..46846791c2ef81e3ee2319e5f97f8a32ac5156cf 100644 --- a/assets/auto-deploy-app/test/templates/deployment_test.go +++ b/assets/auto-deploy-app/test/templates/deployment_test.go @@ -223,6 +223,55 @@ func TestDeploymentTemplate(t *testing.T) { }) } + // deployment lifecycle + for _, tc := range []struct { + CaseName string + Release string + Values map[string]string + + ExpectedLifecycle *coreV1.Lifecycle + }{ + { + CaseName: "lifecycle", + Release: "production", + Values: map[string]string{ + "lifecycle.preStop.exec.command[0]": "/bin/sh", + "lifecycle.preStop.exec.command[1]": "-c", + "lifecycle.preStop.exec.command[2]": "sleep 10", + }, + ExpectedLifecycle: &coreV1.Lifecycle{ + PreStop: &coreV1.Handler{ + Exec: &coreV1.ExecAction{ + Command: []string{"/bin/sh", "-c", "sleep 10"}, + }, + }, + }, + }, + } { + 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.ExpectedLifecycle, deployment.Spec.Template.Spec.Containers[0].Lifecycle) + }) + } + // deployment livenessProbe, and readinessProbe tests for _, tc := range []struct { CaseName string diff --git a/assets/auto-deploy-app/test/templates/test_helpers.go b/assets/auto-deploy-app/test/templates/test_helpers.go index 2408c3b6ffa15a47c448fc5e66bda94f2a17e898..8bc12de73554ed51ab49b981d41daf95633e2c7a 100644 --- a/assets/auto-deploy-app/test/templates/test_helpers.go +++ b/assets/auto-deploy-app/test/templates/test_helpers.go @@ -44,6 +44,7 @@ type workerDeploymentTestCase struct { ExpectedCmd []string ExpectedStrategyType appsV1.DeploymentStrategyType ExpectedSelector *metav1.LabelSelector + ExpectedLifecycle *coreV1.Lifecycle ExpectedLivenessProbe *coreV1.Probe ExpectedReadinessProbe *coreV1.Probe ExpectedNodeSelector map[string]string diff --git a/assets/auto-deploy-app/test/templates/workerdeployment_test.go b/assets/auto-deploy-app/test/templates/workerdeployment_test.go index c6422c366999ef80a3ac70f1f7f0220b41a31022..fd7b49811d208ac3145441dc84e03ff59152ffd1 100644 --- a/assets/auto-deploy-app/test/templates/workerdeployment_test.go +++ b/assets/auto-deploy-app/test/templates/workerdeployment_test.go @@ -319,6 +319,127 @@ func TestWorkerDeploymentTemplate(t *testing.T) { }) } + // worker lifecycle + for _, tc := range []struct { + CaseName string + Values map[string]string + Release string + + ExpectedDeployments []workerDeploymentTestCase + }{ + { + CaseName: "lifecycle", + Release: "production", + Values: map[string]string{ + "workers.worker1.command[0]": "echo", + "workers.worker1.command[1]": "worker1", + "workers.worker1.lifecycle.preStop.exec.command[0]": "/bin/sh", + "workers.worker1.lifecycle.preStop.exec.command[1]": "-c", + "workers.worker1.lifecycle.preStop.exec.command[2]": "sleep 10", + "workers.worker2.command[0]": "echo", + "workers.worker2.command[1]": "worker2", + "workers.worker2.lifecycle.preStop.exec.command[0]": "/bin/sh", + "workers.worker2.lifecycle.preStop.exec.command[1]": "-c", + "workers.worker2.lifecycle.preStop.exec.command[2]": "sleep 15", + }, + ExpectedDeployments: []workerDeploymentTestCase{ + { + ExpectedName: "production-worker1", + ExpectedCmd: []string{"echo", "worker1"}, + ExpectedLifecycle: &coreV1.Lifecycle{ + PreStop: &coreV1.Handler{ + Exec: &coreV1.ExecAction{ + Command: []string{"/bin/sh", "-c", "sleep 10"}, + }, + }, + }, + }, + { + ExpectedName: "production-worker2", + ExpectedCmd: []string{"echo", "worker2"}, + ExpectedLifecycle: &coreV1.Lifecycle{ + PreStop: &coreV1.Handler{ + Exec: &coreV1.ExecAction{ + Command: []string{"/bin/sh", "-c", "sleep 15"}, + }, + }, + }, + }, + }, + }, + { + CaseName: "preStopCommand", + Release: "production", + Values: map[string]string{ + "workers.worker1.command[0]": "echo", + "workers.worker1.command[1]": "worker1", + "workers.worker1.preStopCommand[0]": "/bin/sh", + "workers.worker1.preStopCommand[1]": "-c", + "workers.worker1.preStopCommand[2]": "sleep 10", + "workers.worker2.command[0]": "echo", + "workers.worker2.command[1]": "worker2", + "workers.worker2.preStopCommand[0]": "/bin/sh", + "workers.worker2.preStopCommand[1]": "-c", + "workers.worker2.preStopCommand[2]": "sleep 15", + }, + ExpectedDeployments: []workerDeploymentTestCase{ + { + ExpectedName: "production-worker1", + ExpectedCmd: []string{"echo", "worker1"}, + ExpectedLifecycle: &coreV1.Lifecycle{ + PreStop: &coreV1.Handler{ + Exec: &coreV1.ExecAction{ + Command: []string{"/bin/sh", "-c", "sleep 10"}, + }, + }, + }, + }, + { + ExpectedName: "production-worker2", + ExpectedCmd: []string{"echo", "worker2"}, + ExpectedLifecycle: &coreV1.Lifecycle{ + PreStop: &coreV1.Handler{ + Exec: &coreV1.ExecAction{ + Command: []string{"/bin/sh", "-c", "sleep 15"}, + }, + }, + }, + }, + }, + }, + } { + 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/worker-deployment.yaml"}) + + var deployments deploymentAppsV1List + helm.UnmarshalK8SYaml(t, output, &deployments) + + require.Len(t, deployments.Items, len(tc.ExpectedDeployments)) + + for i, expectedDeployment := range tc.ExpectedDeployments { + deployment := deployments.Items[i] + require.Equal(t, expectedDeployment.ExpectedName, deployment.Name) + require.Len(t, deployment.Spec.Template.Spec.Containers, 1) + require.Equal(t, expectedDeployment.ExpectedCmd, deployment.Spec.Template.Spec.Containers[0].Command) + require.Equal(t, expectedDeployment.ExpectedLifecycle, deployment.Spec.Template.Spec.Containers[0].Lifecycle) + } + }) + } + // worker livenessProbe, and readinessProbe tests for _, tc := range []struct { CaseName string