...

Text file src/cloud.google.com/go/storage/emulator_test.sh

Documentation: cloud.google.com/go/storage

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

View as plain text