test.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #!/bin/bash
  2. # Copyright 2015 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. # A set of helpers for tests
  16. readonly reset=$(tput sgr0)
  17. readonly bold=$(tput bold)
  18. readonly black=$(tput setaf 0)
  19. readonly red=$(tput setaf 1)
  20. readonly green=$(tput setaf 2)
  21. kube::test::clear_all() {
  22. kubectl delete "${kube_flags[@]}" rc,pods --all --grace-period=0
  23. }
  24. # Force exact match of a returned result for a object query. Wrap this with || to support multiple
  25. # valid return types.
  26. kube::test::get_object_assert() {
  27. local object=$1
  28. local request=$2
  29. local expected=$3
  30. local args=${4:-}
  31. res=$(eval kubectl ${args} get "${kube_flags[@]}" $object -o go-template=\"$request\")
  32. if [[ "$res" =~ ^$expected$ ]]; then
  33. echo -n ${green}
  34. echo "Successful get $object $request: $res"
  35. echo -n ${reset}
  36. return 0
  37. else
  38. echo ${bold}${red}
  39. echo "FAIL!"
  40. echo "Get $object $request"
  41. echo " Expected: $expected"
  42. echo " Got: $res"
  43. echo ${reset}${red}
  44. caller
  45. echo ${reset}
  46. return 1
  47. fi
  48. }
  49. kube::test::get_object_jsonpath_assert() {
  50. local object=$1
  51. local request=$2
  52. local expected=$3
  53. res=$(eval kubectl get "${kube_flags[@]}" $object -o jsonpath=\"$request\")
  54. if [[ "$res" =~ ^$expected$ ]]; then
  55. echo -n ${green}
  56. echo "Successful get $object $request: $res"
  57. echo -n ${reset}
  58. return 0
  59. else
  60. echo ${bold}${red}
  61. echo "FAIL!"
  62. echo "Get $object $request"
  63. echo " Expected: $expected"
  64. echo " Got: $res"
  65. echo ${reset}${red}
  66. caller
  67. echo ${reset}
  68. return 1
  69. fi
  70. }
  71. kube::test::describe_object_assert() {
  72. local resource=$1
  73. local object=$2
  74. local matches=${@:3}
  75. result=$(eval kubectl describe "${kube_flags[@]}" $resource $object)
  76. for match in ${matches}; do
  77. if [[ ! $(echo "$result" | grep ${match}) ]]; then
  78. echo ${bold}${red}
  79. echo "FAIL!"
  80. echo "Describe $resource $object"
  81. echo " Expected Match: $match"
  82. echo " Not found in:"
  83. echo "$result"
  84. echo ${reset}${red}
  85. caller
  86. echo ${reset}
  87. return 1
  88. fi
  89. done
  90. echo -n ${green}
  91. echo "Successful describe $resource $object:"
  92. echo "$result"
  93. echo -n ${reset}
  94. return 0
  95. }
  96. kube::test::describe_object_events_assert() {
  97. local resource=$1
  98. local object=$2
  99. local showevents=${3:-"true"}
  100. if [[ -z "${3:-}" ]]; then
  101. result=$(eval kubectl describe "${kube_flags[@]}" $resource $object)
  102. else
  103. result=$(eval kubectl describe "${kube_flags[@]}" "--show-events=$showevents" $resource $object)
  104. fi
  105. if [[ -n $(echo "$result" | grep "No events.\|Events:") ]]; then
  106. local has_events="true"
  107. else
  108. local has_events="false"
  109. fi
  110. if [[ $showevents == $has_events ]]; then
  111. echo -n ${green}
  112. echo "Successful describe"
  113. echo "$result"
  114. echo ${reset}
  115. return 0
  116. else
  117. echo ${bold}${red}
  118. echo "FAIL"
  119. if [[ $showevents == "false" ]]; then
  120. echo " Events information should not be described in:"
  121. else
  122. echo " Events information not found in:"
  123. fi
  124. echo $result
  125. echo ${reset}${red}
  126. caller
  127. echo ${reset}
  128. return 1
  129. fi
  130. }
  131. kube::test::describe_resource_assert() {
  132. local resource=$1
  133. local matches=${@:2}
  134. result=$(eval kubectl describe "${kube_flags[@]}" $resource)
  135. for match in ${matches}; do
  136. if [[ ! $(echo "$result" | grep ${match}) ]]; then
  137. echo ${bold}${red}
  138. echo "FAIL!"
  139. echo "Describe $resource"
  140. echo " Expected Match: $match"
  141. echo " Not found in:"
  142. echo "$result"
  143. echo ${reset}${red}
  144. caller
  145. echo ${reset}
  146. return 1
  147. fi
  148. done
  149. echo -n ${green}
  150. echo "Successful describe $resource:"
  151. echo "$result"
  152. echo -n ${reset}
  153. return 0
  154. }
  155. kube::test::describe_resource_events_assert() {
  156. local resource=$1
  157. local showevents=${2:-"true"}
  158. result=$(eval kubectl describe "${kube_flags[@]}" "--show-events=$showevents" $resource)
  159. if [[ $(echo "$result" | grep "No events.\|Events:") ]]; then
  160. local has_events="true"
  161. else
  162. local has_events="false"
  163. fi
  164. if [[ $showevents == $has_events ]]; then
  165. echo -n ${green}
  166. echo "Successful describe"
  167. echo "$result"
  168. echo -n ${reset}
  169. return 0
  170. else
  171. echo ${bold}${red}
  172. echo "FAIL"
  173. if [[ $showevents == "false" ]]; then
  174. echo " Events information should not be described in:"
  175. else
  176. echo " Events information not found in:"
  177. fi
  178. echo $result
  179. caller
  180. echo ${reset}
  181. return 1
  182. fi
  183. }
  184. kube::test::if_has_string() {
  185. local message=$1
  186. local match=$2
  187. if [[ $(echo "$message" | grep "$match") ]]; then
  188. echo "Successful"
  189. echo "message:$message"
  190. echo "has:$match"
  191. return 0
  192. else
  193. echo "FAIL!"
  194. echo "message:$message"
  195. echo "has not:$match"
  196. caller
  197. return 1
  198. fi
  199. }