Skip to content

Commit

Permalink
chore: run bash shell in strict mode
Browse files Browse the repository at this point in the history
According to Github Actions doc [1], linux platform will use `bash -e
{0}` mode to run script. But we also need `pipefail` and `nounset` in
CI. This patch is aimed to add strict mode setting before any run.

And remove the `| tee` and egrep commands since we don't need this
workaround to show error.

REF:

1: https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsshell

ISSUE: etcd-io#15514

Signed-off-by: Wei Fu <fuweid89@gmail.com>
  • Loading branch information
fuweid committed Mar 21, 2023
1 parent 6d01ab6 commit 3a4eba4
Show file tree
Hide file tree
Showing 63 changed files with 192 additions and 78 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jobs:
- env:
TARGET: ${{ matrix.target }}
run: |
set -euo pipefail
echo "${TARGET}"
case "${TARGET}" in
linux-amd64)
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/contrib.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ jobs:
- uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 # v3.5.0
with:
go-version: "1.19.7"
- run: make -C contrib/mixin tools test
- run: |
set -euo pipefail
make -C contrib/mixin tools test
2 changes: 2 additions & 0 deletions .github/workflows/coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ jobs:
- env:
TARGET: ${{ matrix.target }}
run: |
set -euo pipefail
mkdir "${TARGET}"
case "${TARGET}" in
linux-amd64-coverage)
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/e2e-arm64.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ jobs:
- env:
TARGET: ${{ matrix.target }}
run: |
set -euo pipefail
echo "${TARGET}"
case "${TARGET}" in
linux-arm64-e2e)
PASSES='build release e2e' CPU='4' EXPECT_DEBUG='true' COVER='false' RACE='true' ./scripts/test.sh 2>&1 | tee test.log
! grep -E "(--- FAIL:|FAIL:|DATA RACE|panic: test timed out|appears to have leaked)" -B50 -A10 test.log
PASSES='build release e2e' CPU='4' EXPECT_DEBUG='true' COVER='false' RACE='true' ./scripts/test.sh
;;
*)
echo "Failed to find target"
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ jobs:
- env:
TARGET: ${{ matrix.target }}
run: |
set -euo pipefail
echo "${TARGET}"
case "${TARGET}" in
linux-amd64-e2e)
PASSES='build release e2e' CPU='4' EXPECT_DEBUG='true' COVER='false' RACE='true' ./scripts/test.sh 2>&1 | tee test.log
! grep -E "(--- FAIL:|FAIL:|DATA RACE|panic: test timed out|appears to have leaked)" -B50 -A10 test.log
PASSES='build release e2e' CPU='4' EXPECT_DEBUG='true' COVER='false' RACE='true' ./scripts/test.sh
;;
linux-386-e2e)
GOARCH=386 PASSES='build e2e' CPU='4' EXPECT_DEBUG='true' COVER='false' RACE='true' ./scripts/test.sh 2>&1 | tee test.log
! grep -E "(--- FAIL:|FAIL:|DATA RACE|panic: test timed out|appears to have leaked)" -B50 -A10 test.log
GOARCH=386 PASSES='build e2e' CPU='4' EXPECT_DEBUG='true' COVER='false' RACE='true' ./scripts/test.sh
;;
*)
echo "Failed to find target"
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/fuzzing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ jobs:
- uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 # v3.5.0
with:
go-version: "1.19.7"
- run: GOARCH=amd64 CPU=4 make fuzz
- run: |
set -euo pipefail
GOARCH=amd64 CPU=4 make fuzz
- uses: actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce # v3.1.2
if: failure()
with:
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/govuln.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ jobs:
with:
go-version: "1.19.7"
- run: date
- run: go install golang.org/x/vuln/cmd/govulncheck@latest && govulncheck ./...
- run: |
set -euo pipefail
go install golang.org/x/vuln/cmd/govulncheck@latest && govulncheck ./...
8 changes: 4 additions & 4 deletions .github/workflows/grpcproxy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ jobs:
- env:
TARGET: ${{ matrix.target }}
run: |
set -euo pipefail
echo "${TARGET}"
case "${TARGET}" in
linux-amd64-grpcproxy-integration)
PASSES='build grpcproxy_integration' CPU='4' COVER='false' RACE='true' ./scripts/test.sh 2>&1 | tee test.log
! grep -E "(--- FAIL:|FAIL:|DATA RACE|panic: test timed out|appears to have leaked)" -B50 -A10 test.log
PASSES='build grpcproxy_integration' CPU='4' COVER='false' RACE='true' ./scripts/test.sh
;;
linux-amd64-grpcproxy-e2e)
PASSES='build grpcproxy_e2e' CPU='4' COVER='false' RACE='true' ./scripts/test.sh 2>&1 | tee test.log
! grep -E "(--- FAIL:|FAIL:|DATA RACE|panic: test timed out|appears to have leaked)" -B50 -A10 test.log
PASSES='build grpcproxy_e2e' CPU='4' COVER='false' RACE='true' ./scripts/test.sh
;;
*)
echo "Failed to find target"
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ jobs:
with:
go-version: "1.19.7"
- run: |
set -euo pipefail
git config --global user.email "github-action@etcd.io"
git config --global user.name "Github Action"
gpg --batch --gen-key <<EOF
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/robustness-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ jobs:
env:
GITHUB_REF: ${{ inputs.ref }}
run: |
set -euo pipefail
case "${GITHUB_REF}" in
release-3.5)
make build-failpoints-release-3.5
Expand All @@ -45,6 +47,8 @@ jobs:
esac
- name: test-robustness
run: |
set -euo pipefail
# Use --failfast to avoid overriding report generated by failed test
EXPECT_DEBUG=true GO_TEST_FLAGS='-v --count ${{ inputs.count }} --timeout ${{ inputs.testTimeout }} --failfast --run TestRobustness' RESULTS_DIR=/tmp/results make test-robustness
- uses: actions/upload-artifact@v2
Expand Down
10 changes: 8 additions & 2 deletions .github/workflows/static-analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,11 @@ jobs:
with:
version: '3.14.0'
repo-token: ${{ secrets.GITHUB_TOKEN }}
- run: make verify
- run: make fix
- run: |
set -euo pipefail
make verify
- run: |
set -euo pipefail
make fix
2 changes: 2 additions & 0 deletions .github/workflows/tests-arm64.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ jobs:
- env:
TARGET: ${{ matrix.target }}
run: |
set -euo pipefail
mkdir "${TARGET}"
export JUNIT_REPORT_DIR=$(realpath ${TARGET})
case "${TARGET}" in
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ jobs:
- env:
TARGET: ${{ matrix.target }}
run: |
set -euo pipefail
mkdir "${TARGET}"
export JUNIT_REPORT_DIR=$(realpath ${TARGET})
case "${TARGET}" in
Expand Down
4 changes: 3 additions & 1 deletion hack/benchmark/bench.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/bin/bash -e
#!/usr/bin/env bash

set -euo pipefail

leader=http://localhost:2379
# assume three servers
Expand Down
4 changes: 3 additions & 1 deletion pkg/proxy/fixtures/gencerts.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/bin/bash
#!/usr/bin/env bash

set -euo pipefail

if ! [[ "$0" =~ "./gencerts.sh" ]]; then
echo "must be run from 'fixtures'"
Expand Down
2 changes: 1 addition & 1 deletion scripts/build-binary.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

set -e
set -euo pipefail

source ./scripts/test_lib.sh

Expand Down
2 changes: 1 addition & 1 deletion scripts/build-docker.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

set -e
set -euo pipefail

if [ "$#" -ne 1 ]; then
echo "Usage: $0 VERSION" >&2
Expand Down
2 changes: 1 addition & 1 deletion scripts/build-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Build all release binaries and images to directory ./release.
# Run from repository root.
#
set -e
set -euo pipefail

source ./scripts/test_lib.sh

Expand Down
2 changes: 2 additions & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# This scripts build the etcd binaries
# To build the tools, run `build_tools.sh`

set -euo pipefail

source ./scripts/test_lib.sh
source ./scripts/build_lib.sh

Expand Down
13 changes: 10 additions & 3 deletions scripts/build_lib.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
#!/usr/bin/env bash

set -euo pipefail

source ./scripts/test_lib.sh

GIT_SHA=$(git rev-parse --short HEAD || echo "GitNotFound")
VERSION_SYMBOL="${ROOT_MODULE}/api/v3/version.GitSHA"

# use go env if noset
GOOS=${GOOS:-$(go env GOOS)}
GOARCH=${GOARCH:-$(go env GOARCH)}
GO_BUILD_FLAGS=${GO_BUILD_FLAGS:-}

# Set GO_LDFLAGS="-s" for building without symbols for debugging.
# shellcheck disable=SC2206
GO_LDFLAGS=(${GO_LDFLAGS} "-X=${VERSION_SYMBOL}=${GIT_SHA}")
GO_LDFLAGS=(${GO_LDFLAGS:-} "-X=${VERSION_SYMBOL}=${GIT_SHA}")
GO_BUILD_ENV=("CGO_ENABLED=0" "GO_BUILD_FLAGS=${GO_BUILD_FLAGS}" "GOOS=${GOOS}" "GOARCH=${GOARCH}")

etcd_build() {
out="bin"
if [[ -n "${BINDIR}" ]]; then out="${BINDIR}"; fi
if [[ -n "${BINDIR:-}" ]]; then out="${BINDIR}"; fi

run rm -f "${out}/etcd"
(
Expand Down Expand Up @@ -62,7 +69,7 @@ etcd_build() {

tools_build() {
out="bin"
if [[ -n "${BINDIR}" ]]; then out="${BINDIR}"; fi
if [[ -n "${BINDIR:-}" ]]; then out="${BINDIR}"; fi
tools_path="tools/benchmark
tools/etcd-dump-db
tools/etcd-dump-logs
Expand Down
2 changes: 2 additions & 0 deletions scripts/build_tools.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env bash

set -euo pipefail

source ./scripts/test_lib.sh
source ./scripts/build_lib.sh

Expand Down
2 changes: 1 addition & 1 deletion scripts/codecov_upload.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Script used to collect and upload test coverage (mostly by travis).
# Usage ./test_coverage_upload.sh [log_file]

set -o pipefail
set -euo pipefail

LOG_FILE=${1:-test-coverage.log}

Expand Down
2 changes: 1 addition & 1 deletion scripts/fix.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

set -e
set -euo pipefail

# Top level problems with modules can lead to test_lib being not functional
go mod tidy
Expand Down
4 changes: 3 additions & 1 deletion scripts/fuzzing.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env bash
set -e

set -euo pipefail

source ./scripts/test_lib.sh

GO_CMD="go"
Expand Down
3 changes: 2 additions & 1 deletion scripts/genproto.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
# Generate all etcd protobuf bindings.
# Run from repository root directory named etcd.
#
set -e
set -euo pipefail

shopt -s globstar

if ! [[ "$0" =~ scripts/genproto.sh ]]; then
Expand Down
2 changes: 1 addition & 1 deletion scripts/install-marker.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

set -e
set -euo pipefail

ARCH=$1

Expand Down
7 changes: 3 additions & 4 deletions scripts/measure-test-flakiness.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#!/bin/bash
#!/usr/bin/env bash

set -e
set -o pipefail
set -euo pipefail

if [[ -z ${GITHUB_TOKEN} ]]
if [[ -z ${GITHUB_TOKEN:-} ]]
then
echo "Please set the \$GITHUB_TOKEN environment variable for the script to work"
exit 1
Expand Down
2 changes: 1 addition & 1 deletion scripts/release_mod.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# % DRY_RUN=false REMOTE_REPO="origin" ./scripts/release_mod.sh push_mod_tags

set -e
set -euo pipefail

source ./scripts/test_lib.sh

Expand Down
15 changes: 9 additions & 6 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ set -e
# Consider command as failed when any component of the pipe fails:
# https://stackoverflow.com/questions/1221833/pipe-output-and-capture-exit-status-in-bash
set -o pipefail
set -o nounset

# The test script is not supposed to make any changes to the files
# e.g. add/update missing dependencies. Such divergences should be
Expand All @@ -46,6 +47,8 @@ export ETCD_VERIFY=all
source ./scripts/test_lib.sh
source ./scripts/build_lib.sh

OUTPUT_FILE=${OUTPUT_FILE:-""}

if [ -n "${OUTPUT_FILE}" ]; then
log_callout "Dumping output to: ${OUTPUT_FILE}"
exec > >(tee -a "${OUTPUT_FILE}") 2>&1
Expand All @@ -55,12 +58,12 @@ PASSES=${PASSES:-"gofmt bom dep build unit"}
PKG=${PKG:-}
SHELLCHECK_VERSION=${SHELLCHECK_VERSION:-"v0.8.0"}

if [ -z "$GOARCH" ]; then
if [ -z "${GOARCH:-}" ]; then
GOARCH=$(go env GOARCH);
fi

# determine whether target supports race detection
if [ -z "${RACE}" ] ; then
if [ -z "${RACE:-}" ] ; then
if [ "$GOARCH" == "amd64" ]; then
RACE="--race"
else
Expand All @@ -72,14 +75,14 @@ fi

# This options make sense for cases where SUT (System Under Test) is compiled by test.
COMMON_TEST_FLAGS=("${RACE}")
if [[ -n "${CPU}" ]]; then
if [[ -n "${CPU:-}" ]]; then
COMMON_TEST_FLAGS+=("--cpu=${CPU}")
fi

log_callout "Running with ${COMMON_TEST_FLAGS[*]}"

RUN_ARG=()
if [ -n "${TESTCASE}" ]; then
if [ -n "${TESTCASE:-}" ]; then
RUN_ARG=("-run=${TESTCASE}")
fi

Expand Down Expand Up @@ -256,7 +259,7 @@ function merge_cov {

function cov_pass {
# shellcheck disable=SC2153
if [ -z "$COVERDIR" ]; then
if [ -z "${COVERDIR:-}" ]; then
log_error "COVERDIR undeclared"
return 255
fi
Expand Down Expand Up @@ -566,7 +569,7 @@ function release_pass {
rm -f ./bin/etcd-last-release
# to grab latest patch release; bump this up for every minor release
UPGRADE_VER=$(git tag -l --sort=-version:refname "v3.5.*" | head -1 | cut -d- -f1)
if [ -n "$MANUAL_VER" ]; then
if [ -n "${MANUAL_VER:-}" ]; then
# in case, we need to test against different version
UPGRADE_VER=$MANUAL_VER
fi
Expand Down
Loading

0 comments on commit 3a4eba4

Please sign in to comment.