From 79c2e7eb1c37820fd86c3234af72d3f7358c2755 Mon Sep 17 00:00:00 2001 From: Rob Holland Date: Tue, 19 Nov 2024 14:40:00 +0000 Subject: [PATCH 1/6] Add some unit tests. These are more succinct and much faster than the terratest tests. --- charts/temporal/.helmignore | 1 + charts/temporal/tests/deployment_test.go | 36 +++++++++++ .../tests/server_deployment_test.yaml | 62 +++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 charts/temporal/tests/server_deployment_test.yaml diff --git a/charts/temporal/.helmignore b/charts/temporal/.helmignore index 50af0317..f85b0ee2 100644 --- a/charts/temporal/.helmignore +++ b/charts/temporal/.helmignore @@ -20,3 +20,4 @@ .idea/ *.tmproj .vscode/ +tests/ diff --git a/charts/temporal/tests/deployment_test.go b/charts/temporal/tests/deployment_test.go index 6364f1d5..9e6bb618 100644 --- a/charts/temporal/tests/deployment_test.go +++ b/charts/temporal/tests/deployment_test.go @@ -172,3 +172,39 @@ func TestTemplateServerDeploymentLabels(t *testing.T) { require.Equal(t, "zero", deployment.ObjectMeta.Labels["zero"]) require.Equal(t, "zero", deployment.Spec.Template.ObjectMeta.Labels["zero"]) } + +func TestTemplateServerDeploymentReplicaCount(t *testing.T) { + // t.Parallel() + + helmChartPath, err := filepath.Abs("../") + releaseName := "temporal" + require.NoError(t, err) + + namespaceName := "temporal-" + strings.ToLower(random.UniqueId()) + + options := &helm.Options{ + SetValues: map[string]string{ + "server.replicaCount": "2", + "server.frontend.replicaCount": "3", + }, + KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName), + BuildDependencies: true, + } + + output := helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/server-deployment.yaml"}) + var nonFrontendDeployment appsv1.Deployment + var frontendDeployment appsv1.Deployment + + for _, output := range strings.Split(output, "---") { + if strings.Contains(output, "frontend") { + helm.UnmarshalK8SYaml(t, output, &frontendDeployment) + } else { + helm.UnmarshalK8SYaml(t, output, &nonFrontendDeployment) + } + } + + require.NotNil(t, frontendDeployment.Spec.Replicas) + require.Equal(t, int32(3), *frontendDeployment.Spec.Replicas) + require.NotNil(t, nonFrontendDeployment.Spec.Replicas) + require.Equal(t, int32(2), *nonFrontendDeployment.Spec.Replicas) +} diff --git a/charts/temporal/tests/server_deployment_test.yaml b/charts/temporal/tests/server_deployment_test.yaml new file mode 100644 index 00000000..866a6428 --- /dev/null +++ b/charts/temporal/tests/server_deployment_test.yaml @@ -0,0 +1,62 @@ +suite: test server deployment +templates: + - server-deployment.yaml + - server-configmap.yaml +set: + server: + replicaCount: 2 + resources: + requests: + cpu: 100m + memory: 128Mi + limits: + cpu: 200m + memory: 256Mi + frontend: + replicaCount: 3 + resources: + requests: + cpu: 200m + memory: 256Mi + limits: + cpu: 400m + memory: 512Mi +tests: + - it: defaults to service wide replica count + template: templates/server-deployment.yaml + documentSelector: + path: '$[?(@.metadata.name != "RELEASE-NAME-temporal-frontend")].kind' + value: Deployment + matchMany: true + asserts: + - equal: + path: spec.replicas + value: 2 + - it: favours service specific replica count + template: templates/server-deployment.yaml + documentSelector: + path: metadata.name + value: RELEASE-NAME-temporal-frontend + asserts: + - equal: + path: spec.replicas + value: 3 + - it: defaults to service wide resources + template: templates/server-deployment.yaml + documentSelector: + path: '$[?(@.metadata.name != "RELEASE-NAME-temporal-frontend")].kind' + value: Deployment + matchMany: true + asserts: + - equal: + path: spec.template.spec.containers[0].resources.requests.cpu + value: 100m + - it: favours service specific resources + template: templates/server-deployment.yaml + documentSelector: + path: metadata.name + value: RELEASE-NAME-temporal-frontend + asserts: + - equal: + path: spec.template.spec.containers[0].resources.requests.cpu + value: 200m From b97878fcf184ce4945dd5d341e35c1c44463d892 Mon Sep 17 00:00:00 2001 From: Rob Holland Date: Tue, 19 Nov 2024 14:41:39 +0000 Subject: [PATCH 2/6] Remove duplicated test in favour of unit tests. --- charts/temporal/tests/deployment_test.go | 36 ------------------------ 1 file changed, 36 deletions(-) diff --git a/charts/temporal/tests/deployment_test.go b/charts/temporal/tests/deployment_test.go index 9e6bb618..6364f1d5 100644 --- a/charts/temporal/tests/deployment_test.go +++ b/charts/temporal/tests/deployment_test.go @@ -172,39 +172,3 @@ func TestTemplateServerDeploymentLabels(t *testing.T) { require.Equal(t, "zero", deployment.ObjectMeta.Labels["zero"]) require.Equal(t, "zero", deployment.Spec.Template.ObjectMeta.Labels["zero"]) } - -func TestTemplateServerDeploymentReplicaCount(t *testing.T) { - // t.Parallel() - - helmChartPath, err := filepath.Abs("../") - releaseName := "temporal" - require.NoError(t, err) - - namespaceName := "temporal-" + strings.ToLower(random.UniqueId()) - - options := &helm.Options{ - SetValues: map[string]string{ - "server.replicaCount": "2", - "server.frontend.replicaCount": "3", - }, - KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName), - BuildDependencies: true, - } - - output := helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/server-deployment.yaml"}) - var nonFrontendDeployment appsv1.Deployment - var frontendDeployment appsv1.Deployment - - for _, output := range strings.Split(output, "---") { - if strings.Contains(output, "frontend") { - helm.UnmarshalK8SYaml(t, output, &frontendDeployment) - } else { - helm.UnmarshalK8SYaml(t, output, &nonFrontendDeployment) - } - } - - require.NotNil(t, frontendDeployment.Spec.Replicas) - require.Equal(t, int32(3), *frontendDeployment.Spec.Replicas) - require.NotNil(t, nonFrontendDeployment.Spec.Replicas) - require.Equal(t, int32(2), *nonFrontendDeployment.Spec.Replicas) -} From 34127729cb19d85de9f9a0d4b8e6b6b46293c909 Mon Sep 17 00:00:00 2001 From: Rob Holland Date: Tue, 19 Nov 2024 14:46:20 +0000 Subject: [PATCH 3/6] Simplify test setup a little. --- charts/temporal/tests/server_deployment_test.yaml | 8 -------- 1 file changed, 8 deletions(-) diff --git a/charts/temporal/tests/server_deployment_test.yaml b/charts/temporal/tests/server_deployment_test.yaml index 866a6428..3c4d5557 100644 --- a/charts/temporal/tests/server_deployment_test.yaml +++ b/charts/temporal/tests/server_deployment_test.yaml @@ -8,19 +8,11 @@ set: resources: requests: cpu: 100m - memory: 128Mi - limits: - cpu: 200m - memory: 256Mi frontend: replicaCount: 3 resources: requests: cpu: 200m - memory: 256Mi - limits: - cpu: 400m - memory: 512Mi tests: - it: defaults to service wide replica count template: templates/server-deployment.yaml From 2469aee6410b52790f661dbb21cf4d5db43e980c Mon Sep 17 00:00:00 2001 From: Rob Holland Date: Tue, 19 Nov 2024 14:49:27 +0000 Subject: [PATCH 4/6] Add helm unittest to the CI. --- .github/workflows/ci.yml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3bd71b70..3c17f4c1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -49,4 +49,10 @@ jobs: - name: Run template tests working-directory: charts/temporal/tests - run: go test \ No newline at end of file + run: go test + + - name: Install helm-unittest + run: helm plugin install https://github.com/quintush/helm-unittest + + - name: Run helm-unittest + run: helm unittest charts/temporal From 8117e127ac7d8c3036c2fb4d698e51563c9c86bc Mon Sep 17 00:00:00 2001 From: Rob Holland Date: Thu, 21 Nov 2024 12:15:27 +0000 Subject: [PATCH 5/6] See if working directory makes a difference. --- .github/workflows/ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3c17f4c1..4e0dbb18 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -55,4 +55,5 @@ jobs: run: helm plugin install https://github.com/quintush/helm-unittest - name: Run helm-unittest - run: helm unittest charts/temporal + working-directory: charts/temporal + run: helm unittest . From ca278e586946dafe6087e0d38ddc8cda2ab1191d Mon Sep 17 00:00:00 2001 From: Rob Holland Date: Thu, 21 Nov 2024 12:21:02 +0000 Subject: [PATCH 6/6] Somehow was using the wrong unittest repo... --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4e0dbb18..501598a2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -52,7 +52,7 @@ jobs: run: go test - name: Install helm-unittest - run: helm plugin install https://github.com/quintush/helm-unittest + run: helm plugin install https://github.com/helm-unittest/helm-unittest.git - name: Run helm-unittest working-directory: charts/temporal