From ffe08a6b4b107d2d685be39bd083e684c63114dd Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Fri, 28 Jun 2024 12:03:20 -0700 Subject: [PATCH] Add lint for shellcheck, fix issues --- Makefile | 11 ++++++++++- _test_tools/git_askpass.sh | 6 +++--- _test_tools/ncsvr/ncsvr.sh | 15 ++++++++------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index da8f35d45..ed03f7259 100644 --- a/Makefile +++ b/Makefile @@ -283,4 +283,13 @@ lint-staticcheck: lint-golangci-lint: go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.59.0 run -lint: lint-staticcheck lint-golangci-lint +lint-shellcheck: + docker run \ + --rm \ + -v `pwd`:`pwd` \ + -w `pwd` \ + docker.io/koalaman/shellcheck-alpine:v0.9.0 \ + shellcheck \ + $$(git ls-files ':!:vendor' '*.sh') + +lint: lint-staticcheck lint-golangci-lint lint-shellcheck diff --git a/_test_tools/git_askpass.sh b/_test_tools/git_askpass.sh index cde34eeb6..977d2537c 100755 --- a/_test_tools/git_askpass.sh +++ b/_test_tools/git_askpass.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # # Copyright 2019 The Kubernetes Authors. # @@ -20,7 +20,7 @@ set -o errexit set -o nounset # Ask pass some ops, fail if it mismatched the magic password. -if [ "$1" = "clone" -o "$1" = "ls-remote" -o "$1" = "fetch" ]; then +if [[ "$1" == "clone" || "$1" == "ls-remote" || "$1" = "fetch" ]]; then # `git credential fill` requires the repo url match to consume the credentials stored by git-sync. # Askpass git only support repo started with "file://" which is used in test_e2e.sh. REPO=$(echo "$@" | grep -o "file://[^ ]*") @@ -28,7 +28,7 @@ if [ "$1" = "clone" -o "$1" = "ls-remote" -o "$1" = "fetch" ]; then USERNAME=$(echo "${OUTPUT}" | grep "^username=.*") PASSWD=$(echo "${OUTPUT}" | grep "^password=.*") # Test case must match the magic username and password below. - if [ "${USERNAME}" != "username=my-username" -o "${PASSWD}" != "password=my-password" ]; then + if [[ "${USERNAME}" != "username=my-username" || "${PASSWD}" != "password=my-password" ]]; then echo "invalid test username/password pair: ${USERNAME}:${PASSWD}" exit 1 fi diff --git a/_test_tools/ncsvr/ncsvr.sh b/_test_tools/ncsvr/ncsvr.sh index 3b5b4d73d..1a0c25b4c 100755 --- a/_test_tools/ncsvr/ncsvr.sh +++ b/_test_tools/ncsvr/ncsvr.sh @@ -14,17 +14,18 @@ # See the License for the specific language governing permissions and # limitations under the License. - -if [ -z "$1" -o -z "$2" ]; then +if [ -z "$1" ] || [ -z "$2" ]; then echo "usage: $0 " exit 1 fi -F="/tmp/fifo.$RANDOM" - +# This construction allows the passed-in command ($2) to optionally read from +# the client before responding (e.g. an HTTP request). +CMD_TO_NC=$(mktemp -u) +NC_TO_CMD=$(mktemp -u) +mkfifo "$CMD_TO_NC" "$NC_TO_CMD" while true; do - rm -f "$F" - mkfifo "$F" - cat "$F" | sh -c "$2" 2>&1 | nc -l -p "$1" -N -w1 > "$F" + sh -c "$2" > "$CMD_TO_NC" 2>&1 < "$NC_TO_CMD" & + nc -l -p "$1" -N -w1 < "$CMD_TO_NC" > "$NC_TO_CMD" date >> /var/log/hits done