emulator_test.sh 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/bin/bash
  2. # Copyright 2021 Google LLC
  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. # Fail on any error
  16. set -eo pipefail
  17. # Display commands being run
  18. set -x
  19. # Only run on Go 1.17+
  20. min_minor_ver=17
  21. v=`go version | { read _ _ v _; echo ${v#go}; }`
  22. comps=(${v//./ })
  23. minor_ver=${comps[1]}
  24. if [ "$minor_ver" -lt "$min_minor_ver" ]; then
  25. echo minor version $minor_ver, skipping
  26. exit 0
  27. fi
  28. export STORAGE_EMULATOR_HOST="http://localhost:9000"
  29. export STORAGE_EMULATOR_HOST_GRPC="localhost:8888"
  30. DEFAULT_IMAGE_NAME='gcr.io/cloud-devrel-public-resources/storage-testbench'
  31. DEFAULT_IMAGE_TAG='latest'
  32. DOCKER_IMAGE=${DEFAULT_IMAGE_NAME}:${DEFAULT_IMAGE_TAG}
  33. CONTAINER_NAME=storage_testbench
  34. # Note: --net=host makes the container bind directly to the Docker host’s network,
  35. # with no network isolation. If we were to use port-mapping instead, reset connection errors
  36. # would be captured differently and cause unexpected test behaviour.
  37. # The host networking driver works only on Linux hosts.
  38. # See more about using host networking: https://docs.docker.com/network/host/
  39. DOCKER_NETWORK="--net=host"
  40. # Note: We do not expect the RetryConformanceTest suite to pass on darwin due to
  41. # differences in the network errors emitted by the system.
  42. if [ `go env GOOS` == 'darwin' ]; then
  43. DOCKER_NETWORK="-p 9000:9000 -p 8888:8888"
  44. fi
  45. # Get the docker image for the testbench
  46. docker pull $DOCKER_IMAGE
  47. # Start the testbench
  48. docker run --name $CONTAINER_NAME --rm -d $DOCKER_NETWORK $DOCKER_IMAGE
  49. echo "Running the Cloud Storage testbench: $STORAGE_EMULATOR_HOST"
  50. sleep 1
  51. # Stop the testbench & cleanup environment variables
  52. function cleanup() {
  53. echo "Cleanup testbench"
  54. docker stop $CONTAINER_NAME
  55. unset STORAGE_EMULATOR_HOST;
  56. unset STORAGE_EMULATOR_HOST_GRPC;
  57. }
  58. trap cleanup EXIT
  59. # Check that the server is running - retry several times to allow for start-up time
  60. response=$(curl -w "%{http_code}\n" $STORAGE_EMULATOR_HOST --retry-connrefused --retry 5 -o /dev/null)
  61. if [[ $response != 200 ]]
  62. then
  63. echo "Testbench server did not start correctly"
  64. exit 1
  65. fi
  66. # Start the gRPC server on port 8888.
  67. echo "Starting the gRPC server on port 8888"
  68. response=$(curl -w "%{http_code}\n" --retry 5 --retry-max-time 40 -o /dev/null "$STORAGE_EMULATOR_HOST/start_grpc?port=8888")
  69. if [[ $response != 200 ]]
  70. then
  71. echo "Testbench gRPC server did not start correctly"
  72. exit 1
  73. fi
  74. # Run tests
  75. go test -v -timeout 10m ./ -run="^Test(RetryConformance|.*Emulated)$" -short 2>&1 | tee -a sponge_log.log