Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all: enable and fix issues from shellcheck #1438

Merged
merged 11 commits into from
Jun 2, 2020
Merged
2 changes: 2 additions & 0 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ jobs:
# based on command results. So, create a file if a build fails and check
# if the file exists to set the right exit code.
run: find . -name go.mod -execdir sh -c 'go build ./... || touch build_failed.txt' \; ; test ! -f build_failed.txt
- name: shellcheck
run: find . -name "*.sh" -exec shellcheck {} \;
4 changes: 2 additions & 2 deletions getting-started/devflowapp/deployment/sed_wrapper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
# Substitue values for environment variables in file $1

echo "Substituting values for password and project ($PROJECT)"
sed -e "s/_DB_PASSWORD/$_DB_PASSWORD/g" -i $1
sed -e "s/PROJECT_ID/$PROJECT/g" -i $1
sed -e "s/_DB_PASSWORD/$_DB_PASSWORD/g" -i "$1"
sed -e "s/PROJECT_ID/$PROJECT/g" -i "$1"

2 changes: 1 addition & 1 deletion getting-started/gce/startup-script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ curl -s "https://storage.googleapis.com/signals-agents/logging/google-fluentd-in
service google-fluentd restart &

APP_LOCATION=$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/app-location" -H "Metadata-Flavor: Google")
gsutil cp $APP_LOCATION app.tar.gz
gsutil cp "$APP_LOCATION" app.tar.gz
tar -xzf app.tar.gz

# Start the service included in app.tar.gz.
Expand Down
7 changes: 4 additions & 3 deletions getting-started/gopher-run/cmd/training.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#! /bin/bash
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -35,19 +36,19 @@ while true; do
DATE="$(date '+%Y%m%d_%H%M%S')"
# Include date so JOB_ID is unique
JOB_ID="${MODEL_NAME}_${DATE}"
gcloud beta ai-platform jobs submit training $JOB_ID \
gcloud beta ai-platform jobs submit training "$JOB_ID" \
--master-image-uri=$IMAGE_URI --scale-tier=BASIC --job-dir=$JOB_DIR \
-- \
--preprocess --training_data_path=$TRAINING_DATA_PATH --objective=multi:softmax --num_class=4

VERSION_NAME="V_${DATE}"
gcloud ai-platform versions create $VERSION_NAME \
gcloud ai-platform versions create "$VERSION_NAME" \
--model ${MODEL_NAME} \
--origin "${JOB_DIR}model/" \
--runtime-version=1.11 \
--framework ${FRAMEWORK} \
--python-version=2.7
gcloud ai-platform versions set-default ${VERSION_NAME} --model=${MODEL_NAME}
gcloud ai-platform versions set-default "${VERSION_NAME}" --model="${MODEL_NAME}"

# Repeate every 30 minutes
sleep 1800
Expand Down
6 changes: 3 additions & 3 deletions memorystore/redis/gce_deployment/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fi

if [ -z "$ZONE" ]; then
ZONE=$(gcloud config get-value compute/zone -q)
echo $ZONE
echo "$ZONE"
fi


Expand All @@ -36,8 +36,8 @@ gcloud compute instances create my-instance \
--machine-type=g1-small \
--scopes cloud-platform \
--metadata-from-file startup-script=startup-script.sh \
--metadata gcs-bucket=$GCS_BUCKET_NAME,redis-host=$REDISHOST,redis-port=$REDISPORT \
--zone $ZONE \
--metadata gcs-bucket="$GCS_BUCKET_NAME",redis-host="$REDISHOST",redis-port="$REDISPORT" \
--zone "$ZONE" \
--tags http-server

gcloud compute firewall-rules create allow-http-server-8080 \
Expand Down
1 change: 1 addition & 0 deletions memorystore/redis/gce_deployment/startup-script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set -ex

# Talk to the metadata server to get the project id and location of application binary.
PROJECTID=$(curl -s "http://metadata.google.internal/computeMetadata/v1/project/project-id" -H "Metadata-Flavor: Google")
export PROJECTID
GCS_BUCKET_NAME=$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/gcs-bucket" -H "Metadata-Flavor: Google")
REDISHOST=$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/redis-host" -H "Metadata-Flavor: Google")
REDISPORT=$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/redis-port" -H "Metadata-Flavor: Google")
Expand Down
4 changes: 2 additions & 2 deletions testing/docker/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ GO111MODULE=off go get github.com/GoogleCloudPlatform/golang-samples/testing/gim
github.com/jstemmer/go-junit-report \
golang.org/x/tools/cmd/goimports

(
# Get the SDK tar and untar it.
cd /tmp

Expand All @@ -40,5 +41,4 @@ rm $TARFILE

./google-cloud-sdk/bin/gcloud -q components update
./google-cloud-sdk/bin/gcloud -q components install app-engine-go

cd -
)
46 changes: 26 additions & 20 deletions testing/kokoro/system_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ TIMEOUT=60m

# Also see trampoline.sh - system_tests.sh is only run for PRs when there are
# significant changes.
SIGNIFICANT_CHANGES=$(git --no-pager diff --name-only master..HEAD | egrep -v '(\.md$|^\.github)' || true)
SIGNIFICANT_CHANGES=$(git --no-pager diff --name-only master..HEAD | grep -Ev '(\.md$|^\.github)' || true)
# CHANGED_DIRS is the list of significant top-level directories that changed,
# but weren't deleted by the current PR.
# CHANGED_DIRS will be empty when run on master.
CHANGED_DIRS=$(echo $SIGNIFICANT_CHANGES | tr ' ' '\n' | grep "/" | cut -d/ -f1 | sort -u | tr '\n' ' ' | xargs ls -d 2>/dev/null || true)
CHANGED_DIRS=$(echo "$SIGNIFICANT_CHANGES" | tr ' ' '\n' | grep "/" | cut -d/ -f1 | sort -u | tr '\n' ' ' | xargs ls -d 2>/dev/null || true)

# List all modules in changed directories.
# If running on master will collect all modules in the repo, including the root module.
GO_CHANGED_MODULES=$(find ${CHANGED_DIRS:-.} -name go.mod)
# shellcheck disable=SC2086
GO_CHANGED_MODULES="$(find ${CHANGED_DIRS:-.} -name go.mod)"
# If we didn't find any modules, use the root module.
GO_CHANGED_MODULES=${GO_CHANGED_MODULES:-./go.mod}
# Exclude the root module, if present, from the list of sub-modules.
Expand All @@ -57,31 +58,33 @@ GO_CHANGED_SUBMODULES=${GO_CHANGED_MODULES#./go.mod}
# Does not include static analysis checks.
RUN_ALL_TESTS="0"
# If this is a nightly test (not a PR), run all tests.
if [ -z ${KOKORO_GITHUB_PULL_REQUEST_NUMBER:-} ]; then
if [ -z "${KOKORO_GITHUB_PULL_REQUEST_NUMBER:-}" ]; then
RUN_ALL_TESTS="1"
# If the change touches a repo-spanning file or directory of significance, run all tests.
elif echo $SIGNIFICANT_CHANGES | tr ' ' '\n' | grep "^go.mod$" || [[ $CHANGED_DIRS =~ "testing" || $CHANGED_DIRS =~ "internal" ]]; then
elif echo "$SIGNIFICANT_CHANGES" | tr ' ' '\n' | grep "^go.mod$" || [[ $CHANGED_DIRS =~ "testing" || $CHANGED_DIRS =~ "internal" ]]; then
RUN_ALL_TESTS="1"
fi

## Static Analysis
# Do the easy stuff before running tests or reserving a project. Fail fast!
set +x

if [ $GOLANG_SAMPLES_GO_VET ]; then
if [ "$GOLANG_SAMPLES_GO_VET" ]; then
echo "Running 'goimports compliance check'"
set -x
diff -u <(echo -n) <(goimports -d .)
set +x
for i in $GO_CHANGED_MODULES; do
mod="$(dirname $i)"
pushd $mod > /dev/null;
mod=$(dirname "$i")
pushd "$mod" > /dev/null;
# Fail if a dependency was added without the necessary go.mod/go.sum change
# being part of the commit.
echo "Running 'go.mod/go.sum sync check' in '$mod'..."
set -x
go mod tidy;
# shellcheck disable=SC2162
git diff go.mod | tee /dev/stderr | (! read)
# shellcheck disable=SC2162
[ -f go.sum ] && git diff go.sum | tee /dev/stderr | (! read)
set +x
popd > /dev/null;
Expand All @@ -98,8 +101,8 @@ if [ $GOLANG_SAMPLES_GO_VET ]; then
# Recursive submodules are not supported.
set +x
for i in $GO_CHANGED_SUBMODULES; do
mod="$(dirname $i)"
pushd $mod > /dev/null;
mod=$(dirname "$i")
pushd "$mod" > /dev/null;
echo "Running 'go vet' in '$mod'..."
set -x
go vet ./...
Expand Down Expand Up @@ -137,7 +140,8 @@ go install ./testing/sampletests
# This is changed to a project-specific credential after a project is leased.
export GOOGLE_APPLICATION_CREDENTIALS=$KOKORO_KEYSTORE_DIR/71386_kokoro-golang-samples-tests
gimmeproj version;
export GOLANG_SAMPLES_PROJECT_ID=$(gimmeproj -project golang-samples-tests lease $TIMEOUT);
GOLANG_SAMPLES_PROJECT_ID=$(gimmeproj -project golang-samples-tests lease $TIMEOUT);
export GOLANG_SAMPLES_PROJECT_ID
if [ -z "$GOLANG_SAMPLES_PROJECT_ID" ]; then
echo "Lease failed."
exit 1
Expand All @@ -146,6 +150,7 @@ echo "Running tests in project $GOLANG_SAMPLES_PROJECT_ID";

# Always return the project and clean the cache so Kokoro doesn't try to copy
# it when exiting.
# shellcheck disable=SC2064
trap "go clean -modcache; gimmeproj -project golang-samples-tests done $GOLANG_SAMPLES_PROJECT_ID" EXIT

set +x
Expand Down Expand Up @@ -183,19 +188,20 @@ runTests() {
set +x
echo "Running 'go test' in '$(pwd)'..."
set -x
2>&1 go test -timeout $TIMEOUT -v ${1:-./...} | tee sponge_log.log
cat sponge_log.log | /go/bin/go-junit-report -set-exit-code > raw_log.xml
exit_code=$(($exit_code + $?))
2>&1 go test -timeout $TIMEOUT -v "${1:-./...}" | tee sponge_log.log
/go/bin/go-junit-report -set-exit-code < sponge_log.log > raw_log.xml
exit_code=$((exit_code + $?))
# Add region tags tested to test case properties.
cat raw_log.xml | sampletests > sponge_log.xml
sampletests < raw_log.xml > sponge_log.xml
rm raw_log.xml # No need to keep this around.
set +x
}

if [[ $RUN_ALL_TESTS = "1" ]]; then
echo "Running all tests"
# shellcheck disable=SC2044
for i in $(find . -name go.mod); do
pushd "$(dirname $i)" > /dev/null;
pushd "$(dirname "$i")" > /dev/null;
runTests
popd > /dev/null;
done
Expand All @@ -206,7 +212,7 @@ else
runTests . # Always run root tests.
echo "Running tests in modified directories: $CHANGED_DIRS"
for d in $CHANGED_DIRS; do
mods="$(find $d -name go.mod)"
mods=$(find "$d" -name go.mod)
# If there are no modules, just run the tests directly.
if [[ -z "$mods" ]]; then
pushd "$d" > /dev/null;
Expand All @@ -215,7 +221,7 @@ else
# Otherwise, run the tests in all Go directories. This way, we don't have to
# check to see if there are tests that aren't in a sub-module.
else
goDirectories="$(find $d -name "*.go" -printf "%h\n" | sort -u)"
goDirectories="$(find "$d" -name "*.go" -printf "%h\n" | sort -u)"
if [[ -n "$goDirectories" ]]; then
for gd in $goDirectories; do
pushd "$gd" > /dev/null;
Expand All @@ -230,8 +236,8 @@ fi
# If we're running system tests, send the test log to the Build Cop Bot.
# See https://github.com/googleapis/repo-automation-bots/tree/master/packages/buildcop.
if [[ $KOKORO_BUILD_ARTIFACTS_SUBDIR = *"system-tests"* ]]; then
chmod +x $KOKORO_GFILE_DIR/linux_amd64/buildcop
$KOKORO_GFILE_DIR/linux_amd64/buildcop
chmod +x "$KOKORO_GFILE_DIR"/linux_amd64/buildcop
"$KOKORO_GFILE_DIR"/linux_amd64/buildcop
fi

exit $exit_code
10 changes: 5 additions & 5 deletions testing/kokoro/trampoline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ set -x

date

cd github/golang-samples
cd github/golang-samples || exit 1

SIGNIFICANT_CHANGES="$(git --no-pager diff --name-only master..HEAD | egrep -v '(\.md$|^\.github)' || true)"
SIGNIFICANT_CHANGES="$(git --no-pager diff --name-only master..HEAD | grep -Ev '(\.md$|^\.github)' || true)"

# If this is a PR with only insignificant changes, don't run any tests.
if [[ -n ${KOKORO_GITHUB_PULL_REQUEST_NUMBER:-} ]] && [[ -z "$SIGNIFICANT_CHANGES" ]]; then
echo "No big changes. Not running any tests."
exit 0
fi

cd -
cd - || exit 1

function cleanup() {
chmod +x ${KOKORO_GFILE_DIR}/trampoline_cleanup.sh
${KOKORO_GFILE_DIR}/trampoline_cleanup.sh
chmod +x "${KOKORO_GFILE_DIR}"/trampoline_cleanup.sh
"${KOKORO_GFILE_DIR}"/trampoline_cleanup.sh
echo "cleanup";
}
trap cleanup EXIT
Expand Down