From d5e5f1b54273a901c190be32b28c14d82d4d1a3f Mon Sep 17 00:00:00 2001 From: Prasad Chandrasekaran Date: Sat, 18 Mar 2023 12:08:49 +0530 Subject: [PATCH] scripts: Add testing of etcd in local image in release workflow. Signed-off-by: Prasad Chandrasekaran --- .github/workflows/release.yaml | 6 ++- scripts/test_images.sh | 87 ++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100755 scripts/test_images.sh diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 766401338..7ed16b0df 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -9,7 +9,8 @@ jobs: - uses: actions/setup-go@4d34df0c2316fe8122ab82dc22947d607c0c91f9 # v4.0.0 with: go-version: "1.19.7" - - run: | + - name: release + run: | set -euo pipefail git config --global user.email "github-action@etcd.io" @@ -25,3 +26,6 @@ jobs: Expire-Date: 0 EOF DRY_RUN=true ./scripts/release.sh --no-upload --no-docker-push --in-place 3.6.99 + - name: test-image + run: | + VERSION=3.6.99 ./scripts/test_images.sh diff --git a/scripts/test_images.sh b/scripts/test_images.sh new file mode 100755 index 000000000..7980ef732 --- /dev/null +++ b/scripts/test_images.sh @@ -0,0 +1,87 @@ +#!/usr/bin/env bash + +# http://redsymbol.net/articles/unofficial-bash-strict-mode/ +set -euo pipefail +IFS=$'\n\t' + +source ./scripts/test_lib.sh +source ./scripts/build_lib.sh + +function startContainer { + # run docker in the background + docker run -d --rm --name "${RUN_NAME}" "${IMAGE}" + + # wait for etcd daemon to bootstrap + sleep 5 +} + +# stop container +trap 'docker stop "${RUN_NAME}"' EXIT + +function runVersionCheck { + binary="/usr/local/bin/""$1" + versionString="$2" + Out=$(docker exec "${RUN_NAME}" "${binary}" "${versionString}") + foundVersion=$(echo "$Out" | head -1 | rev | cut -d" " -f 1 | rev ) + if [[ "${foundVersion}" != "${VERSION}" ]]; then + echo "error: Invalid Version. Got $foundVersion, expected $VERSION. Error: $Out" + exit 1 + fi +} + +# Can't proceed without docker +if ! command -v docker >/dev/null; then + log_error "cannot find docker" + exit 1 +fi + +# You can't run darwin binaries in linux containers +if [[ $(go env GOOS) == "darwin" ]]; then + echo "Please use linux machine for release builds." + exit 1 +fi + +# Pick defaults based on release workflow +ARCH=$(go env GOARCH) +REPOSITARY=${REPOSITARY:-"gcr.io/etcd-development/etcd"} +if [ -n "$VERSION" ]; then + # Expected Format: v3.6.99-amd64 + TAG=v"${VERSION}"-"${ARCH}" +else + echo "Terminating test, VERSION not supplied" + exit 1 +fi +IMAGE=${IMAGE:-"${REPOSITARY}:${TAG}"} + +# ETCD related values +RUN_NAME="test_etcd" +KEY="foo" +VALUE="bar" + +if [[ "$(docker images -q "${IMAGE}" 2> /dev/null)" == "" ]]; then + echo "${IMAGE} not present locally" + exit 1 +fi + +startContainer + +# Version check +runVersionCheck "etcd" "--version" +runVersionCheck "etcdctl" "version" +runVersionCheck "etcdutl" "version" + +# Put/Get check +PUT=$(docker exec "${RUN_NAME}" /usr/local/bin/etcdctl put "${KEY}" "${VALUE}") +if [ "${PUT}" != "OK" ]; then + echo "Problem with Putting in etcd" + exit 1 +fi + +GET=$(docker exec "${RUN_NAME}" /usr/local/bin/etcdctl get "$KEY" --print-value-only) +if [ "${GET}" != "${VALUE}" ]; then + echo "Problem with getting foo bar in etcd. Got ${GET}" + exit 1 +fi + +echo "Succesfully tested etcd local image ${TAG}" +