Ensure diff doesn't persist patches

This commit is contained in:
Julian V. Modesto
2020-04-02 17:14:17 -04:00
parent facda9abfd
commit 9c0320f1bf
3 changed files with 22 additions and 16 deletions

View File

@@ -34,21 +34,34 @@ run_kubectl_diff_tests() {
kubectl apply -f hack/testdata/pod.yaml
kube::test::get_object_assert 'pod' "{{range.items}}{{ if eq ${id_field:?} \\\"test-pod\\\" }}found{{end}}{{end}}:" 'found:'
initialResourceVersion=$(kubectl get "${kube_flags[@]:?}" -f hack/testdata/pod.yaml -o go-template='{{ .metadata.resourceVersion }}')
# Make sure that diffing the resource right after returns nothing (0 exit code).
kubectl diff -f hack/testdata/pod.yaml
# Ensure diff only dry-runs and doesn't persist change
resourceVersion=$(kubectl get "${kube_flags[@]:?}" -f hack/testdata/pod.yaml -o go-template='{{ .metadata.resourceVersion }}')
kube::test::if_has_string "${resourceVersion}" "${initialResourceVersion}"
# Make sure that:
# 1. the exit code for diff is 1 because it found a difference
# 2. the difference contains the changed image
output_message=$(kubectl diff -f hack/testdata/pod-changed.yaml || test $? -eq 1)
kube::test::if_has_string "${output_message}" 'k8s.gcr.io/pause:3.0'
# Ensure diff only dry-runs and doesn't persist change
resourceVersion=$(kubectl get "${kube_flags[@]:?}" -f hack/testdata/pod.yaml -o go-template='{{ .metadata.resourceVersion }}')
kube::test::if_has_string "${resourceVersion}" "${initialResourceVersion}"
# Test found diff with server-side apply
kubectl apply -f hack/testdata/pod.yaml
output_message=$(kubectl diff -f hack/testdata/pod-changed.yaml --server-side --force-conflicts || test $? -eq 1)
kube::test::if_has_string "${output_message}" 'k8s.gcr.io/pause:3.0'
# Ensure diff --server-side only dry-runs and doesn't persist change
resourceVersion=$(kubectl get "${kube_flags[@]:?}" -f hack/testdata/pod.yaml -o go-template='{{ .metadata.resourceVersion }}')
kube::test::if_has_string "${resourceVersion}" "${initialResourceVersion}"
# Test that we have a return code bigger than 1 if there is an error when diffing
kubectl diff -f hack/testdata/invalid-pod.yaml || test $? -gt 1