Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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