test-e2e-node.sh 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #!/usr/bin/env bash
  2. # Copyright 2016 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. KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/../..
  16. source "${KUBE_ROOT}/hack/lib/init.sh"
  17. focus=${FOCUS:-""}
  18. skip=${SKIP-"\[Flaky\]|\[Slow\]|\[Serial\]"}
  19. # The number of tests that can run in parallel depends on what tests
  20. # are running and on the size of the node. Too many, and tests will
  21. # fail due to resource contention. 8 is a reasonable default for a
  22. # n1-standard-1 node.
  23. # Currently, parallelism only affects when REMOTE=true. For local test,
  24. # ginkgo default parallelism (cores - 1) is used.
  25. parallelism=${PARALLELISM:-8}
  26. artifacts="${ARTIFACTS:-"/tmp/_artifacts/$(date +%y%m%dT%H%M%S)"}"
  27. remote=${REMOTE:-"false"}
  28. runtime=${RUNTIME:-"docker"}
  29. container_runtime_endpoint=${CONTAINER_RUNTIME_ENDPOINT:-""}
  30. image_service_endpoint=${IMAGE_SERVICE_ENDPOINT:-""}
  31. run_until_failure=${RUN_UNTIL_FAILURE:-"false"}
  32. test_args=${TEST_ARGS:-""}
  33. system_spec_name=${SYSTEM_SPEC_NAME:-}
  34. extra_envs=${EXTRA_ENVS:-}
  35. # Parse the flags to pass to ginkgo
  36. ginkgoflags=""
  37. if [[ ${parallelism} -gt 1 ]]; then
  38. ginkgoflags="${ginkgoflags} -nodes=${parallelism} "
  39. fi
  40. if [[ ${focus} != "" ]]; then
  41. ginkgoflags="${ginkgoflags} -focus=\"${focus}\" "
  42. fi
  43. if [[ ${skip} != "" ]]; then
  44. ginkgoflags="${ginkgoflags} -skip=\"${skip}\" "
  45. fi
  46. if [[ ${run_until_failure} != "" ]]; then
  47. ginkgoflags="${ginkgoflags} -untilItFails=${run_until_failure} "
  48. fi
  49. # Setup the directory to copy test artifacts (logs, junit.xml, etc) from remote host to local host
  50. if [ ! -d "${artifacts}" ]; then
  51. echo "Creating artifacts directory at ${artifacts}"
  52. mkdir -p "${artifacts}"
  53. fi
  54. echo "Test artifacts will be written to ${artifacts}"
  55. if [[ ${runtime} == "remote" ]] ; then
  56. if [[ -n ${container_runtime_endpoint} ]] ; then
  57. test_args="--container-runtime-endpoint=${container_runtime_endpoint} ${test_args}"
  58. fi
  59. if [[ -n ${image_service_endpoint} ]] ; then
  60. test_args="--image-service-endpoint=${image_service_endpoint} ${test_args}"
  61. fi
  62. fi
  63. if [ "${remote}" = true ] ; then
  64. # The following options are only valid in remote run.
  65. images=${IMAGES:-""}
  66. hosts=${HOSTS:-""}
  67. image_project=${IMAGE_PROJECT:-"kubernetes-node-e2e-images"}
  68. metadata=${INSTANCE_METADATA:-""}
  69. list_images=${LIST_IMAGES:-false}
  70. if [[ ${list_images} == "true" ]]; then
  71. gcloud compute images list --project="${image_project}" | grep "e2e-node"
  72. exit 0
  73. fi
  74. gubernator=${GUBERNATOR:-"false"}
  75. image_config_file=${IMAGE_CONFIG_FILE:-""}
  76. if [[ ${hosts} == "" && ${images} == "" && ${image_config_file} == "" ]]; then
  77. image_project="${IMAGE_PROJECT:-"cos-cloud"}"
  78. gci_image=$(gcloud compute images list --project "${image_project}" \
  79. --no-standard-images --filter="name ~ 'cos-beta.*'" --format="table[no-heading](name)")
  80. images=${gci_image}
  81. metadata="user-data<${KUBE_ROOT}/test/e2e_node/jenkins/gci-init.yaml,gci-update-strategy=update_disabled"
  82. fi
  83. instance_prefix=${INSTANCE_PREFIX:-"test"}
  84. cleanup=${CLEANUP:-"true"}
  85. delete_instances=${DELETE_INSTANCES:-"false"}
  86. test_suite=${TEST_SUITE:-"default"}
  87. # Get the compute zone
  88. zone=${ZONE:-"$(gcloud info --format='value(config.properties.compute.zone)')"}
  89. if [[ ${zone} == "" ]]; then
  90. echo "Could not find gcloud compute/zone when running: \`gcloud info --format='value(config.properties.compute.zone)'\`"
  91. exit 1
  92. fi
  93. # Get the compute project
  94. project=$(gcloud info --format='value(config.project)')
  95. if [[ ${project} == "" ]]; then
  96. echo "Could not find gcloud project when running: \`gcloud info --format='value(config.project)'\`"
  97. exit 1
  98. fi
  99. # Check if any of the images specified already have running instances. If so reuse those instances
  100. # by moving the IMAGE to a HOST
  101. if [[ ${images} != "" ]]; then
  102. IFS=',' read -ra IM <<< "${images}"
  103. images=""
  104. for i in "${IM[@]}"; do
  105. if gcloud compute instances list "${instance_prefix}-${i}" | grep "${i}"; then
  106. if [[ "${hosts}" != "" ]]; then
  107. hosts="${hosts},"
  108. fi
  109. echo "Reusing host ${instance_prefix}-${i}"
  110. hosts="${hosts}${instance_prefix}-${i}"
  111. else
  112. if [[ "${images}" != "" ]]; then
  113. images="${images},"
  114. fi
  115. images="${images}${i}"
  116. fi
  117. done
  118. fi
  119. # Output the configuration we will try to run
  120. echo "Running tests remotely using"
  121. echo "Project: ${project}"
  122. echo "Image Project: ${image_project}"
  123. echo "Compute/Zone: ${zone}"
  124. echo "Images: ${images}"
  125. echo "Hosts: ${hosts}"
  126. echo "Ginkgo Flags: ${ginkgoflags}"
  127. echo "Instance Metadata: ${metadata}"
  128. echo "Image Config File: ${image_config_file}"
  129. # Invoke the runner
  130. go run test/e2e_node/runner/remote/run_remote.go --logtostderr --vmodule=*=4 --ssh-env="gce" \
  131. --zone="${zone}" --project="${project}" --gubernator="${gubernator}" \
  132. --hosts="${hosts}" --images="${images}" --cleanup="${cleanup}" \
  133. --results-dir="${artifacts}" --ginkgo-flags="${ginkgoflags}" \
  134. --image-project="${image_project}" --instance-name-prefix="${instance_prefix}" \
  135. --delete-instances="${delete_instances}" --test_args="${test_args}" --instance-metadata="${metadata}" \
  136. --image-config-file="${image_config_file}" --system-spec-name="${system_spec_name}" \
  137. --extra-envs="${extra_envs}" --test-suite="${test_suite}" \
  138. 2>&1 | tee -i "${artifacts}/build-log.txt"
  139. exit $?
  140. else
  141. # Refresh sudo credentials if needed
  142. if ping -c 1 -q metadata.google.internal &> /dev/null; then
  143. echo 'Running on CGE, not asking for sudo credentials'
  144. elif sudo --non-interactive "$(which bash)" -c true 2> /dev/null; then
  145. # if we can run bash without a password, it's a pretty safe bet that either
  146. # we can run any command without a password, or that sudo credentials
  147. # are already cached - and they've just been re-cached
  148. echo 'No need to refresh sudo credentials'
  149. else
  150. echo 'Updating sudo credentials'
  151. sudo --validate || exit 1
  152. fi
  153. # Do not use any network plugin by default. User could override the flags with
  154. # test_args.
  155. test_args='--kubelet-flags="--network-plugin= --cni-bin-dir=" '${test_args}
  156. # Runtime flags
  157. test_args='--kubelet-flags="--container-runtime='${runtime}'" '${test_args}
  158. # Test using the host the script was run on
  159. # Provided for backwards compatibility
  160. go run test/e2e_node/runner/local/run_local.go \
  161. --system-spec-name="${system_spec_name}" --extra-envs="${extra_envs}" \
  162. --ginkgo-flags="${ginkgoflags}" --test-flags="--container-runtime=${runtime} \
  163. --alsologtostderr --v 4 --report-dir=${artifacts} --node-name $(hostname) \
  164. ${test_args}" --build-dependencies=true 2>&1 | tee -i "${artifacts}/build-log.txt"
  165. exit $?
  166. fi