Commit 50fdadac authored by Aaron Walker's avatar Aaron Walker
Browse files

test: lifecycle tests for deployment and workers

parent 9b52b170
Loading
Loading
Loading
Loading
+49 −0
Original line number Diff line number Diff line
@@ -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
+1 −0
Original line number Diff line number Diff line
@@ -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
+121 −0
Original line number Diff line number Diff line
@@ -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