From 1010916c1adc3b74f3ffaf5a2a5fae7d0bc2ef77 Mon Sep 17 00:00:00 2001 From: Shiyang Wang Date: Fri, 11 Aug 2017 15:46:45 +0800 Subject: [PATCH] add cmd-test for sort-by command --- hack/lib/test.sh | 11 ++++++++ hack/make-rules/test-cmd-util.sh | 32 ++++++++++++++++++++++ hack/testdata/sorted-pods/sorted-pod1.yaml | 10 +++++++ hack/testdata/sorted-pods/sorted-pod2.yaml | 10 +++++++ hack/testdata/sorted-pods/sorted-pod3.yaml | 10 +++++++ 5 files changed, 73 insertions(+) create mode 100644 hack/testdata/sorted-pods/sorted-pod1.yaml create mode 100644 hack/testdata/sorted-pods/sorted-pod2.yaml create mode 100644 hack/testdata/sorted-pods/sorted-pod3.yaml diff --git a/hack/lib/test.sh b/hack/lib/test.sh index c669fc79ff2..b9e28912fa0 100644 --- a/hack/lib/test.sh +++ b/hack/lib/test.sh @@ -250,6 +250,17 @@ kube::test::describe_resource_events_assert() { fi } +# Compare sort-by resource name output with expected order specify in the last parameter +kube::test::if_sort_by_has_correct_order() { + local array=($(echo "$1" |awk '{if(NR!=1) print $1}')) + local var + for i in "${array[@]}"; do + var+="$i:" + done + + kube::test::if_has_string "$var" "${@:$#}" +} + kube::test::if_has_string() { local message=$1 local match=$2 diff --git a/hack/make-rules/test-cmd-util.sh b/hack/make-rules/test-cmd-util.sh index 35fb3622d11..a490624e16a 100644 --- a/hack/make-rules/test-cmd-util.sh +++ b/hack/make-rules/test-cmd-util.sh @@ -3940,6 +3940,38 @@ run_kubectl_sort_by_tests() { # Post-condition: valid-pod doesn't exist kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" '' + ### sort-by should works by sorting by name + # Create three PODs + # Pre-condition: no POD exists + kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" '' + # Command + kubectl create "${kube_flags[@]}" -f hack/testdata/sorted-pods/sorted-pod1.yaml + # Post-condition: sorted-pod1 is created + kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'sorted-pod1:' + # Command + kubectl create "${kube_flags[@]}" -f hack/testdata/sorted-pods/sorted-pod2.yaml + # Post-condition: sorted-pod1 is created + kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'sorted-pod1:sorted-pod2:' + # Command + kubectl create "${kube_flags[@]}" -f hack/testdata/sorted-pods/sorted-pod3.yaml + # Post-condition: sorted-pod1 is created + kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'sorted-pod1:sorted-pod2:sorted-pod3:' + + # Check output of sort-by '{metadata.name}' + output_message=$(kubectl get pods --sort-by="{metadata.name}") + kube::test::if_sort_by_has_correct_order "${output_message}" "sorted-pod1:sorted-pod2:sorted-pod3:" + + # Check output of sort-by '{metadata.labels.name}' + output_message=$(kubectl get pods --sort-by="{metadata.labels.name}") + kube::test::if_sort_by_has_correct_order "${output_message}" "sorted-pod3:sorted-pod2:sorted-pod1:" + + ### Clean up + # Pre-condition: valid-pod exists + kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'sorted-pod1:sorted-pod2:sorted-pod3:' + # Command + kubectl delete "${kube_flags[@]}" pod --grace-period=0 --force --all + # Post-condition: valid-pod doesn't exist + kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" '' set +o nounset set +o errexit diff --git a/hack/testdata/sorted-pods/sorted-pod1.yaml b/hack/testdata/sorted-pods/sorted-pod1.yaml new file mode 100644 index 00000000000..fba02b9017c --- /dev/null +++ b/hack/testdata/sorted-pods/sorted-pod1.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Pod +metadata: + name: sorted-pod1 + labels: + name: sorted-pod3-label +spec: + containers: + - name: kubernetes-pause + image: gcr.io/google-containers/pause:2.0 diff --git a/hack/testdata/sorted-pods/sorted-pod2.yaml b/hack/testdata/sorted-pods/sorted-pod2.yaml new file mode 100644 index 00000000000..f0ab7e906ea --- /dev/null +++ b/hack/testdata/sorted-pods/sorted-pod2.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Pod +metadata: + name: sorted-pod2 + labels: + name: sorted-pod2-label +spec: + containers: + - name: kubernetes-pause + image: gcr.io/google-containers/pause:2.0 diff --git a/hack/testdata/sorted-pods/sorted-pod3.yaml b/hack/testdata/sorted-pods/sorted-pod3.yaml new file mode 100644 index 00000000000..5d166786c77 --- /dev/null +++ b/hack/testdata/sorted-pods/sorted-pod3.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Pod +metadata: + name: sorted-pod3 + labels: + name: sorted-pod1-label +spec: + containers: + - name: kubernetes-pause + image: gcr.io/google-containers/pause:2.0