From 42814978698b5497d02f905a6631a272bd235678 Mon Sep 17 00:00:00 2001 From: Janet Kuo Date: Fri, 23 Oct 2015 15:33:52 -0700 Subject: [PATCH] Test that kubectl apply update annotation only if apply is already called --- hack/test-cmd.sh | 39 ++++++++++++++++++++++++++++++++++++ hack/testdata/pod-apply.yaml | 10 +++++++++ hack/testdata/pod.yaml | 10 +++++++++ 3 files changed, 59 insertions(+) create mode 100644 hack/testdata/pod-apply.yaml create mode 100644 hack/testdata/pod.yaml diff --git a/hack/test-cmd.sh b/hack/test-cmd.sh index 4646b69f915..60bc9445420 100755 --- a/hack/test-cmd.sh +++ b/hack/test-cmd.sh @@ -526,6 +526,45 @@ runTests() { # Post-condition: no PODs are running kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" '' + ## kubectl apply should update configuration annotations only if apply is already called + ## 1. kubectl create doesn't set the annotation + # Pre-Condition: no POD is running + kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" '' + # Command: create a pod "test-pod" + kubectl create -f hack/testdata/pod.yaml "${kube_flags[@]}" + # Post-Condition: pod "test-pod" is running + kube::test::get_object_assert 'pods test-pod' "{{${labels_field}.name}}" 'test-pod-label' + # Post-Condition: pod "test-pod" doesn't have configuration annotation + ! [[ "$(kubectl get pods test-pod -o yaml "${kube_flags[@]}" | grep kubectl.kubernetes.io/last-applied-configuration)" ]] + ## 2. kubectl replace doesn't set the annotation + kubectl get pods test-pod -o yaml "${kube_flags[@]}" | sed 's/test-pod-label/test-pod-replaced/g' > "${KUBE_TEMP}"/test-pod-replace.yaml + # Command: replace the pod "test-pod" + kubectl replace -f "${KUBE_TEMP}"/test-pod-replace.yaml "${kube_flags[@]}" + # Post-Condition: pod "test-pod" is replaced + kube::test::get_object_assert 'pods test-pod' "{{${labels_field}.name}}" 'test-pod-replaced' + # Post-Condition: pod "test-pod" doesn't have configuration annotation + ! [[ "$(kubectl get pods test-pod -o yaml "${kube_flags[@]}" | grep kubectl.kubernetes.io/last-applied-configuration)" ]] + ## 3. kubectl apply does set the annotation + # Command: apply the pod "test-pod" + kubectl apply -f hack/testdata/pod-apply.yaml "${kube_flags[@]}" + # Post-Condition: pod "test-pod" is applied + kube::test::get_object_assert 'pods test-pod' "{{${labels_field}.name}}" 'test-pod-applied' + # Post-Condition: pod "test-pod" has configuration annotation + [[ "$(kubectl get pods test-pod -o yaml "${kube_flags[@]}" | grep kubectl.kubernetes.io/last-applied-configuration)" ]] + kubectl get pods test-pod -o yaml "${kube_flags[@]}" | grep kubectl.kubernetes.io/last-applied-configuration > "${KUBE_TEMP}"/annotation-configuration + ## 4. kubectl replace updates an existing annotation + kubectl get pods test-pod -o yaml "${kube_flags[@]}" | sed 's/test-pod-applied/test-pod-replaced/g' > "${KUBE_TEMP}"/test-pod-replace.yaml + # Command: replace the pod "test-pod" + kubectl replace -f "${KUBE_TEMP}"/test-pod-replace.yaml "${kube_flags[@]}" + # Post-Condition: pod "test-pod" is replaced + kube::test::get_object_assert 'pods test-pod' "{{${labels_field}.name}}" 'test-pod-replaced' + # Post-Condition: pod "test-pod" has configuration annotation, and it's updated (different from the annotation when it's applied) + [[ "$(kubectl get pods test-pod -o yaml "${kube_flags[@]}" | grep kubectl.kubernetes.io/last-applied-configuration)" ]] + kubectl get pods test-pod -o yaml "${kube_flags[@]}" | grep kubectl.kubernetes.io/last-applied-configuration > "${KUBE_TEMP}"/annotation-configuration-replaced + ! [[ $(diff -q "${KUBE_TEMP}"/annotation-configuration "${KUBE_TEMP}"/annotation-configuration-replaced > /dev/null) ]] + # Clean up + rm "${KUBE_TEMP}"/test-pod-replace.yaml "${KUBE_TEMP}"/annotation-configuration "${KUBE_TEMP}"/annotation-configuration-replaced + ############## # Namespaces # ############## diff --git a/hack/testdata/pod-apply.yaml b/hack/testdata/pod-apply.yaml new file mode 100644 index 00000000000..3b8df43f260 --- /dev/null +++ b/hack/testdata/pod-apply.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Pod +metadata: + name: test-pod + labels: + name: test-pod-applied +spec: + containers: + - name: kubernetes-pause + image: kubernetes/pause \ No newline at end of file diff --git a/hack/testdata/pod.yaml b/hack/testdata/pod.yaml new file mode 100644 index 00000000000..99499fd870b --- /dev/null +++ b/hack/testdata/pod.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Pod +metadata: + name: test-pod + labels: + name: test-pod-label +spec: + containers: + - name: kubernetes-pause + image: kubernetes/pause \ No newline at end of file