From e48bd4e1e92d972e8d14aca33cbfda36d8987cc5 Mon Sep 17 00:00:00 2001 From: karlson Date: Thu, 21 Sep 2023 10:50:33 +0100 Subject: [PATCH 1/5] add tables --- util/kustomize/kustomize_test.go | 30 +++++++++++++++++++ .../testdata/patches/deployment.yaml | 23 ++++++++++++++ .../testdata/patches/kustomization.yaml | 3 ++ 3 files changed, 56 insertions(+) create mode 100644 util/kustomize/testdata/patches/deployment.yaml create mode 100644 util/kustomize/testdata/patches/kustomization.yaml diff --git a/util/kustomize/kustomize_test.go b/util/kustomize/kustomize_test.go index b959a1f9a4680..93ff53de217a7 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 = "patches" func testDataDir(tb testing.TB, testData string) (string, error) { res := tb.TempDir() @@ -350,3 +351,32 @@ func TestKustomizeCustomVersion(t *testing.T) { assert.Nil(t, err) assert.Equal(t, "ARGOCD_APP_NAME=argo-cd-tests\n", string(content)) } + +func TestKustomizePatches(t *testing.T) { + ask := v1alpha1.ApplicationSourceKustomize{ + Patches: []v1alpha1.KustomizePatch{ + { + Patch: `|- + - op: replace + path: /spec/template/spec/containers/0/ports/0/containerPort + value: 443`, + Target: &v1alpha1.KustomizeSelector{ + KustomizeResId: v1alpha1.KustomizeResId{ + KustomizeGvk: v1alpha1.KustomizeGvk{ + Kind: "Deployment", + }, + Name: "nginx-deployment", + }, + }, + }, + }, + } + + appPath, err := testDataDir(t, kustomization5) + assert.Nil(t, err) + kustomize := NewKustomizeApp(appPath, git.NopCreds{}, "", "") + objs, images, err := kustomize.Build(&ask, nil, nil) + + fmt.Println(objs) + fmt.Println(images) +} diff --git a/util/kustomize/testdata/patches/deployment.yaml b/util/kustomize/testdata/patches/deployment.yaml new file mode 100644 index 0000000000000..051ce6b0fd034 --- /dev/null +++ b/util/kustomize/testdata/patches/deployment.yaml @@ -0,0 +1,23 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-deployment + annotations: + baz: quux + 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 diff --git a/util/kustomize/testdata/patches/kustomization.yaml b/util/kustomize/testdata/patches/kustomization.yaml new file mode 100644 index 0000000000000..88b7852d27e3c --- /dev/null +++ b/util/kustomize/testdata/patches/kustomization.yaml @@ -0,0 +1,3 @@ +resources: + - ./deployment.yaml + From c00b5d5b3423e81c23b626cb9f4c13d47f3d6fc2 Mon Sep 17 00:00:00 2001 From: karlson Date: Thu, 21 Sep 2023 13:45:00 +0100 Subject: [PATCH 2/5] ci: add applicationset argocd --- util/kustomize/kustomize_test.go | 34 +++++++++++-------- .../deployment.yaml | 4 +-- .../kustomization.yaml | 1 - 3 files changed, 21 insertions(+), 18 deletions(-) rename util/kustomize/testdata/{patches => kustomization_yaml_patches}/deployment.yaml (84%) rename util/kustomize/testdata/{patches => kustomization_yaml_patches}/kustomization.yaml (97%) diff --git a/util/kustomize/kustomize_test.go b/util/kustomize/kustomize_test.go index 93ff53de217a7..0afda405228fa 100644 --- a/util/kustomize/kustomize_test.go +++ b/util/kustomize/kustomize_test.go @@ -22,7 +22,7 @@ const kustomization2a = "kustomization_yml" const kustomization2b = "Kustomization" const kustomization3 = "force_common" const kustomization4 = "custom_version" -const kustomization5 = "patches" +const kustomization5 = "kustomization_yaml_patches" func testDataDir(tb testing.TB, testData string) (string, error) { res := tb.TempDir() @@ -352,14 +352,23 @@ func TestKustomizeCustomVersion(t *testing.T) { assert.Equal(t, "ARGOCD_APP_NAME=argo-cd-tests\n", string(content)) } -func TestKustomizePatches(t *testing.T) { - ask := v1alpha1.ApplicationSourceKustomize{ +func TestKustomizeBuildPatches(t *testing.T) { + appPath, err := testDataDir(t, kustomization5) + assert.Nil(t, err) + kustomize := NewKustomizeApp(appPath, git.NopCreds{}, "", "") + env := &v1alpha1.Env{ + &v1alpha1.EnvEntry{Name: "ARGOCD_APP_NAME", Value: "argo-cd-tests"}, + } + kustomizeSource := v1alpha1.ApplicationSourceKustomize{ + Replicas: []v1alpha1.KustomizeReplica{ + { + Name: "nginx-deployment", + Count: intstr.FromInt(2), + }, + }, Patches: []v1alpha1.KustomizePatch{ { - Patch: `|- - - op: replace - path: /spec/template/spec/containers/0/ports/0/containerPort - value: 443`, + Patch: `[ { "op": "replace", "path": "/spec/template/spec/containers/0/ports/0/containerPort", "value": 443 } ]`, Target: &v1alpha1.KustomizeSelector{ KustomizeResId: v1alpha1.KustomizeResId{ KustomizeGvk: v1alpha1.KustomizeGvk{ @@ -371,12 +380,9 @@ func TestKustomizePatches(t *testing.T) { }, }, } + objs, images, err := kustomize.Build(&kustomizeSource, nil, env) - appPath, err := testDataDir(t, kustomization5) - assert.Nil(t, err) - kustomize := NewKustomizeApp(appPath, git.NopCreds{}, "", "") - objs, images, err := kustomize.Build(&ask, nil, nil) - - fmt.Println(objs) - fmt.Println(images) + fmt.Println(err) + fmt.Println(objs[0]) + fmt.Println(images[0]) } diff --git a/util/kustomize/testdata/patches/deployment.yaml b/util/kustomize/testdata/kustomization_yaml_patches/deployment.yaml similarity index 84% rename from util/kustomize/testdata/patches/deployment.yaml rename to util/kustomize/testdata/kustomization_yaml_patches/deployment.yaml index 051ce6b0fd034..545961bb6094d 100644 --- a/util/kustomize/testdata/patches/deployment.yaml +++ b/util/kustomize/testdata/kustomization_yaml_patches/deployment.yaml @@ -2,8 +2,6 @@ apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment - annotations: - baz: quux labels: app: nginx spec: @@ -20,4 +18,4 @@ spec: - name: nginx image: nginx:1.15.4 ports: - - containerPort: 80 + - containerPort: 80 \ No newline at end of file diff --git a/util/kustomize/testdata/patches/kustomization.yaml b/util/kustomize/testdata/kustomization_yaml_patches/kustomization.yaml similarity index 97% rename from util/kustomize/testdata/patches/kustomization.yaml rename to util/kustomize/testdata/kustomization_yaml_patches/kustomization.yaml index 88b7852d27e3c..5c89a0172d3eb 100644 --- a/util/kustomize/testdata/patches/kustomization.yaml +++ b/util/kustomize/testdata/kustomization_yaml_patches/kustomization.yaml @@ -1,3 +1,2 @@ resources: - ./deployment.yaml - From 1a8832cb4e7b0717250c823d9cdc9bb71c2f2f80 Mon Sep 17 00:00:00 2001 From: karlson Date: Thu, 21 Sep 2023 14:24:18 +0100 Subject: [PATCH 3/5] ci: add applicationset argocd --- util/kustomize/kustomize_test.go | 38 +++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/util/kustomize/kustomize_test.go b/util/kustomize/kustomize_test.go index 0afda405228fa..5e58016eed367 100644 --- a/util/kustomize/kustomize_test.go +++ b/util/kustomize/kustomize_test.go @@ -356,16 +356,8 @@ func TestKustomizeBuildPatches(t *testing.T) { appPath, err := testDataDir(t, kustomization5) assert.Nil(t, err) kustomize := NewKustomizeApp(appPath, git.NopCreds{}, "", "") - env := &v1alpha1.Env{ - &v1alpha1.EnvEntry{Name: "ARGOCD_APP_NAME", Value: "argo-cd-tests"}, - } + kustomizeSource := v1alpha1.ApplicationSourceKustomize{ - Replicas: []v1alpha1.KustomizeReplica{ - { - Name: "nginx-deployment", - Count: intstr.FromInt(2), - }, - }, Patches: []v1alpha1.KustomizePatch{ { Patch: `[ { "op": "replace", "path": "/spec/template/spec/containers/0/ports/0/containerPort", "value": 443 } ]`, @@ -380,9 +372,29 @@ func TestKustomizeBuildPatches(t *testing.T) { }, }, } - objs, images, err := kustomize.Build(&kustomizeSource, nil, env) + 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, int(443)) + + fmt.Println(port) - fmt.Println(err) - fmt.Println(objs[0]) - fmt.Println(images[0]) } From 2edc878481f67cecb4f3a6187fa515250edc9691 Mon Sep 17 00:00:00 2001 From: karlson Date: Thu, 21 Sep 2023 14:32:43 +0100 Subject: [PATCH 4/5] ci: add applicationset argocd --- util/kustomize/kustomize_test.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/util/kustomize/kustomize_test.go b/util/kustomize/kustomize_test.go index 5e58016eed367..e6ac0cfcc1546 100644 --- a/util/kustomize/kustomize_test.go +++ b/util/kustomize/kustomize_test.go @@ -393,8 +393,5 @@ func TestKustomizeBuildPatches(t *testing.T) { assert.Equal(t, found, true) assert.Nil(t, err) - assert.Equal(t, port, int(443)) - - fmt.Println(port) - + assert.Equal(t, port, int64(443)) } From 362cf3be0dc4acf5514f47dbc47f29756d5a6b54 Mon Sep 17 00:00:00 2001 From: karlson Date: Thu, 21 Sep 2023 14:40:54 +0100 Subject: [PATCH 5/5] ci: add applicationset argocd --- util/kustomize/kustomize_test.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/util/kustomize/kustomize_test.go b/util/kustomize/kustomize_test.go index e6ac0cfcc1546..573cb87fb602c 100644 --- a/util/kustomize/kustomize_test.go +++ b/util/kustomize/kustomize_test.go @@ -360,7 +360,7 @@ func TestKustomizeBuildPatches(t *testing.T) { kustomizeSource := v1alpha1.ApplicationSourceKustomize{ Patches: []v1alpha1.KustomizePatch{ { - Patch: `[ { "op": "replace", "path": "/spec/template/spec/containers/0/ports/0/containerPort", "value": 443 } ]`, + 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{ @@ -394,4 +394,12 @@ func TestKustomizeBuildPatches(t *testing.T) { 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") }