| 
									
										
										
										
											2019-05-16 02:36:56 +08:00
										 |  |  | #!/usr/bin/env bash
 | 
					
						
							|  |  |  | # Copyright 2019 The Kubernetes Authors. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  | # you may not use this file except in compliance with the License. | 
					
						
							|  |  |  | # You may obtain a copy of the License at | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #     http://www.apache.org/licenses/LICENSE-2.0 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  | # distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  | # See the License for the specific language governing permissions and | 
					
						
							|  |  |  | # limitations under the License. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-21 18:10:46 +08:00
										 |  |  | # This script checks whether e2e test code which contains `Expect()` but not use | 
					
						
							|  |  |  | # the e2e framework exists or not. | 
					
						
							|  |  |  | # Usage: `hack/verify-test-code.sh`. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-16 02:36:56 +08:00
										 |  |  | set -o errexit | 
					
						
							|  |  |  | set -o nounset | 
					
						
							|  |  |  | set -o pipefail | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. | 
					
						
							| 
									
										
										
										
											2019-06-20 04:34:52 +08:00
										 |  |  | source "${KUBE_ROOT}/hack/lib/init.sh" | 
					
						
							| 
									
										
										
										
											2019-05-16 02:36:56 +08:00
										 |  |  | cd "${KUBE_ROOT}" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-20 04:34:52 +08:00
										 |  |  | all_e2e_files=() | 
					
						
							| 
									
										
										
										
											2019-05-30 00:39:38 +08:00
										 |  |  | # NOTE: This checks e2e test code without the e2e framework which contains Expect().To(HaveOccurred()) | 
					
						
							| 
									
										
										
										
											2019-06-20 04:34:52 +08:00
										 |  |  | kube::util::read-array all_e2e_files < <(find test/e2e{,_node,_kubeadm} -name '*.go' | grep -v 'test/e2e/framework/') | 
					
						
							| 
									
										
										
										
											2019-05-16 02:36:56 +08:00
										 |  |  | errors_expect_no_error=() | 
					
						
							|  |  |  | for file in "${all_e2e_files[@]}" | 
					
						
							|  |  |  | do | 
					
						
							| 
									
										
										
										
											2019-06-06 02:38:00 +08:00
										 |  |  |     if grep -E "Expect\(.*\)\.(NotTo|ToNot)\(.*HaveOccurred\(\)" "${file}" > /dev/null | 
					
						
							| 
									
										
										
										
											2019-05-16 02:36:56 +08:00
										 |  |  |     then | 
					
						
							|  |  |  |         errors_expect_no_error+=( "${file}" ) | 
					
						
							|  |  |  |     fi | 
					
						
							| 
									
										
										
										
											2020-09-04 02:37:01 +08:00
										 |  |  |     if grep -E "Expect\(err\)\.To\(gomega\.BeNil\(\)\)" "${file}" > /dev/null | 
					
						
							|  |  |  |     then | 
					
						
							|  |  |  |         errors_expect_no_error+=( "${file}" ) | 
					
						
							|  |  |  |     fi | 
					
						
							| 
									
										
										
										
											2019-05-16 02:36:56 +08:00
										 |  |  | done | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-16 02:03:12 +08:00
										 |  |  | all_e2e_framework_files=() | 
					
						
							|  |  |  | kube::util::read-array all_e2e_framework_files < <(find test/e2e/framework/ -name '*.go' | grep -v "_test.go") | 
					
						
							|  |  |  | errors_framework_contains_tests=() | 
					
						
							|  |  |  | for file in "${all_e2e_framework_files[@]}" | 
					
						
							|  |  |  | do | 
					
						
							|  |  |  |     if grep -E "(ConformanceIt\(.*, func\(\) {|ginkgo.It\(.*, func\(\) {)" "${file}" > /dev/null | 
					
						
							|  |  |  |     then | 
					
						
							|  |  |  |         errors_framework_contains_tests+=( "${file}" ) | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  | done | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-16 02:36:56 +08:00
										 |  |  | if [ ${#errors_expect_no_error[@]} -ne 0 ]; then | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     echo "Errors:" | 
					
						
							|  |  |  |     for err in "${errors_expect_no_error[@]}"; do | 
					
						
							|  |  |  |       echo "$err" | 
					
						
							|  |  |  |     done | 
					
						
							|  |  |  |     echo | 
					
						
							|  |  |  |     echo 'The above files need to use framework.ExpectNoError(err) instead of ' | 
					
						
							|  |  |  |     echo 'Expect(err).NotTo(HaveOccurred()) or gomega.Expect(err).NotTo(gomega.HaveOccurred())' | 
					
						
							|  |  |  |     echo | 
					
						
							|  |  |  |   } >&2 | 
					
						
							|  |  |  |   exit 1 | 
					
						
							|  |  |  | fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-16 02:03:12 +08:00
										 |  |  | if [ ${#errors_framework_contains_tests[@]} -ne 0 ]; then | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     echo "Errors:" | 
					
						
							|  |  |  |     for err in "${errors_framework_contains_tests[@]}"; do | 
					
						
							|  |  |  |       echo "$err" | 
					
						
							|  |  |  |     done | 
					
						
							|  |  |  |     echo | 
					
						
							|  |  |  |     echo 'The above e2e framework files should not contain any e2e tests which are implemented ' | 
					
						
							|  |  |  |     echo 'with framework.ConformanceIt() or ginkgo.It()' | 
					
						
							|  |  |  |     echo | 
					
						
							|  |  |  |   } >&2 | 
					
						
							|  |  |  |   exit 1 | 
					
						
							|  |  |  | fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-16 02:36:56 +08:00
										 |  |  | echo 'Congratulations!  All e2e test source files are valid.' |