diff.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env bash
  2. # Copyright 2018 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. set -o errexit
  16. set -o nounset
  17. set -o pipefail
  18. # Runs tests for kubectl diff
  19. run_kubectl_diff_tests() {
  20. set -o nounset
  21. set -o errexit
  22. create_and_use_new_namespace
  23. kube::log::status "Testing kubectl diff"
  24. # Test that it works when the live object doesn't exist
  25. output_message=$(! kubectl diff -f hack/testdata/pod.yaml)
  26. kube::test::if_has_string "${output_message}" 'test-pod'
  27. kubectl apply -f hack/testdata/pod.yaml
  28. # Make sure that diffing the resource right after returns nothing (0 exit code).
  29. kubectl diff -f hack/testdata/pod.yaml
  30. # Make sure that:
  31. # 1. the exit code for diff is 1 because it found a difference
  32. # 2. the difference contains the changed image
  33. output_message=$(kubectl diff -f hack/testdata/pod-changed.yaml || test $? -eq 1)
  34. kube::test::if_has_string "${output_message}" 'k8s.gcr.io/pause:3.0'
  35. # Test that we have a return code bigger than 1 if there is an error when diffing
  36. kubectl diff -f hack/testdata/invalid-pod.yaml || test $? -gt 1
  37. kubectl delete -f hack/testdata/pod.yaml
  38. set +o nounset
  39. set +o errexit
  40. }
  41. run_kubectl_diff_same_names() {
  42. set -o nounset
  43. set -o errexit
  44. create_and_use_new_namespace
  45. kube::log::status "Test kubectl diff with multiple resources with the same name"
  46. output_message=$(KUBECTL_EXTERNAL_DIFF="find" kubectl diff -Rf hack/testdata/diff/)
  47. kube::test::if_has_string "${output_message}" 'v1\.Pod\..*\.test'
  48. kube::test::if_has_string "${output_message}" 'apps\.v1\.Deployment\..*\.test'
  49. kube::test::if_has_string "${output_message}" 'v1\.ConfigMap\..*\.test'
  50. kube::test::if_has_string "${output_message}" 'v1\.Secret\..*\.test'
  51. set +o nounset
  52. set +o errexit
  53. }