From 9d102a62ec910d44a43b2788738e55c774f8a533 Mon Sep 17 00:00:00 2001 From: sourceful-karlson <66862653+sourceful-karlson@users.noreply.github.com> Date: Thu, 21 Sep 2023 14:53:41 +0100 Subject: [PATCH] fix: tests for kustomize patches support (#21) * add tables * ci: add applicationset argocd * ci: add applicationset argocd * ci: add applicationset argocd * ci: add applicationset argocd --- util/kustomize/kustomize_test.go | 53 +++++++++++++++++++ .../deployment.yaml | 21 ++++++++ .../kustomization.yaml | 2 + 3 files changed, 76 insertions(+) create mode 100644 util/kustomize/testdata/kustomization_yaml_patches/deployment.yaml create mode 100644 util/kustomize/testdata/kustomization_yaml_patches/kustomization.yaml diff --git a/util/kustomize/kustomize_test.go b/util/kustomize/kustomize_test.go index b959a1f9a4680..573cb87fb602c 100644 --- a/util/kustomize/kustomize_test.go +++ b/util/kustomize/kustomize_test.go @@ -22,6 +22,7 @@ const kustomization2a = "kustomization_yml" const kustomization2b = "Kustomization" const kustomization3 = "force_common" const kustomization4 = "custom_version" +const kustomization5 = "kustomization_yaml_patches" func testDataDir(tb testing.TB, testData string) (string, error) { res := tb.TempDir() @@ -350,3 +351,55 @@ func TestKustomizeCustomVersion(t *testing.T) { assert.Nil(t, err) assert.Equal(t, "ARGOCD_APP_NAME=argo-cd-tests\n", string(content)) } + +func TestKustomizeBuildPatches(t *testing.T) { + appPath, err := testDataDir(t, kustomization5) + assert.Nil(t, err) + kustomize := NewKustomizeApp(appPath, git.NopCreds{}, "", "") + + kustomizeSource := v1alpha1.ApplicationSourceKustomize{ + Patches: []v1alpha1.KustomizePatch{ + { + Patch: `[ { "op": "replace", "path": "/spec/template/spec/containers/0/ports/0/containerPort", "value": 443 }, { "op": "replace", "path": "/spec/template/spec/containers/0/name", "value": "test" }]`, + Target: &v1alpha1.KustomizeSelector{ + KustomizeResId: v1alpha1.KustomizeResId{ + KustomizeGvk: v1alpha1.KustomizeGvk{ + Kind: "Deployment", + }, + Name: "nginx-deployment", + }, + }, + }, + }, + } + objs, _, err := kustomize.Build(&kustomizeSource, nil, nil) + assert.Nil(t, err) + obj := objs[0] + containers, found, err := unstructured.NestedSlice(obj.Object, "spec", "template", "spec", "containers") + assert.Nil(t, err) + assert.Equal(t, found, true) + + ports, found, err := unstructured.NestedSlice( + containers[0].(map[string]interface{}), + "ports", + ) + assert.Equal(t, found, true) + assert.Nil(t, err) + + port, found, err := unstructured.NestedInt64( + ports[0].(map[string]interface{}), + "containerPort", + ) + + assert.Equal(t, found, true) + assert.Nil(t, err) + assert.Equal(t, port, int64(443)) + + name, found, err := unstructured.NestedString( + containers[0].(map[string]interface{}), + "name", + ) + assert.Equal(t, found, true) + assert.Nil(t, err) + assert.Equal(t, name, "test") +} diff --git a/util/kustomize/testdata/kustomization_yaml_patches/deployment.yaml b/util/kustomize/testdata/kustomization_yaml_patches/deployment.yaml new file mode 100644 index 0000000000000..545961bb6094d --- /dev/null +++ b/util/kustomize/testdata/kustomization_yaml_patches/deployment.yaml @@ -0,0 +1,21 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-deployment + labels: + app: nginx +spec: + replicas: 3 + selector: + matchLabels: + app: nginx + template: + metadata: + labels: + app: nginx + spec: + containers: + - name: nginx + image: nginx:1.15.4 + ports: + - containerPort: 80 \ No newline at end of file diff --git a/util/kustomize/testdata/kustomization_yaml_patches/kustomization.yaml b/util/kustomize/testdata/kustomization_yaml_patches/kustomization.yaml new file mode 100644 index 0000000000000..5c89a0172d3eb --- /dev/null +++ b/util/kustomize/testdata/kustomization_yaml_patches/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - ./deployment.yaml