Skip to content

Commit

Permalink
fix: tests for kustomize patches support (#21)
Browse files Browse the repository at this point in the history
* add tables

* ci: add applicationset argocd

* ci: add applicationset argocd

* ci: add applicationset argocd

* ci: add applicationset argocd
  • Loading branch information
sourceful-karlson authored Sep 21, 2023
1 parent e04d389 commit 9d102a6
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
53 changes: 53 additions & 0 deletions util/kustomize/kustomize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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")
}
21 changes: 21 additions & 0 deletions util/kustomize/testdata/kustomization_yaml_patches/deployment.yaml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
resources:
- ./deployment.yaml

0 comments on commit 9d102a6

Please sign in to comment.