From 889148dd707bf092baf7024c7b89ea56abd60497 Mon Sep 17 00:00:00 2001 From: Tom Wieczorek Date: Thu, 7 Sep 2023 12:29:35 +0200 Subject: [PATCH] POSIX compatibility for test scripts (#548) * Use portable shebang line * Replace some easily avoidable bashisms * Fix some small ShellCheck warnings * Allow local keyword, as all the interesting target shells support it (bash, ash, dash) Signed-off-by: Tom Wieczorek Co-authored-by: Kimmo Lehto --- smoke-test/smoke-backup-restore.sh | 14 +++---- smoke-test/smoke-basic.sh | 5 +-- smoke-test/smoke-dynamic.sh | 6 +-- smoke-test/smoke-files.sh | 61 +++++++++++++++--------------- smoke-test/smoke-init.sh | 2 +- smoke-test/smoke-reset.sh | 2 +- smoke-test/smoke-upgrade.sh | 6 +-- smoke-test/smoke.common.sh | 20 +++++----- 8 files changed, 56 insertions(+), 60 deletions(-) diff --git a/smoke-test/smoke-backup-restore.sh b/smoke-test/smoke-backup-restore.sh index 9753fbb9..e6c73e44 100755 --- a/smoke-test/smoke-backup-restore.sh +++ b/smoke-test/smoke-backup-restore.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env sh K0SCTL_CONFIG=${K0SCTL_CONFIG:-"k0sctl.yaml"} @@ -8,7 +8,7 @@ set -e trap runCleanup EXIT # custom exit trap to cleanup the backup archives -function runCleanup() { +runCleanup() { cleanup rm k0s_backup*.tar.gz } @@ -19,8 +19,8 @@ createCluster ../k0sctl apply --config "${K0SCTL_CONFIG}" --debug # Collect some facts so we can validate restore actually did full restore -system_ns_uid=$(footloose ssh root@manager0 -- k0s kubectl --kubeconfig "/var/lib/k0s/pki/admin.conf" get -n kube-system namespace kube-system -o template={{.metadata.uid}}) -node_uid=$(footloose ssh root@manager0 -- k0s kubectl --kubeconfig "/var/lib/k0s/pki/admin.conf" get node worker0 -o template={{.metadata.uid}}) +system_ns_uid=$(footloose ssh root@manager0 -- k0s kubectl --kubeconfig "/var/lib/k0s/pki/admin.conf" get -n kube-system namespace kube-system -o template='{{.metadata.uid}}') +node_uid=$(footloose ssh root@manager0 -- k0s kubectl --kubeconfig "/var/lib/k0s/pki/admin.conf" get node worker0 -o template='{{.metadata.uid}}') ../k0sctl backup --config "${K0SCTL_CONFIG}" --debug @@ -28,15 +28,15 @@ node_uid=$(footloose ssh root@manager0 -- k0s kubectl --kubeconfig "/var/lib/k0s footloose ssh root@manager0 -- k0s stop footloose ssh root@manager0 -- k0s reset -../k0sctl apply --config "${K0SCTL_CONFIG}" --debug --restore-from $(ls k0s_backup*.tar.gz) +../k0sctl apply --config "${K0SCTL_CONFIG}" --debug --restore-from "$(ls k0s_backup*.tar.gz)" # Verify kube object UIDs match so we know we did full restore of the API objects -new_system_ns_uid=$(footloose ssh root@manager0 -- k0s kubectl --kubeconfig "/var/lib/k0s/pki/admin.conf" get -n kube-system namespace kube-system -o template={{.metadata.uid}}) +new_system_ns_uid=$(footloose ssh root@manager0 -- k0s kubectl --kubeconfig "/var/lib/k0s/pki/admin.conf" get -n kube-system namespace kube-system -o template='{{.metadata.uid}}') if [ "$system_ns_uid" != "$new_system_ns_uid" ]; then echo "kube-system UIDs do not match after restore!!!" exit 1 fi -new_node_uid=$(footloose ssh root@manager0 -- k0s kubectl --kubeconfig "/var/lib/k0s/pki/admin.conf" get node worker0 -o template={{.metadata.uid}}) +new_node_uid=$(footloose ssh root@manager0 -- k0s kubectl --kubeconfig "/var/lib/k0s/pki/admin.conf" get node worker0 -o template='{{.metadata.uid}}') if [ "$node_uid" != "$new_node_uid" ]; then echo "worker0 UIDs do not match after restore!!!" exit 1 diff --git a/smoke-test/smoke-basic.sh b/smoke-test/smoke-basic.sh index 49bb2c92..f1404fd7 100755 --- a/smoke-test/smoke-basic.sh +++ b/smoke-test/smoke-basic.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env sh K0SCTL_CONFIG=${K0SCTL_CONFIG:-"k0sctl.yaml"} @@ -34,9 +34,8 @@ echo "* Using k0sctl kubecofig locally" ../k0sctl kubeconfig --config k0sctl.yaml > kubeconfig echo "* Output:" -cat kubeconfig | grep -v -- "-data" +grep -v -- -data kubeconfig echo "* Running kubectl" ./kubectl --kubeconfig kubeconfig get nodes echo "* Done" - diff --git a/smoke-test/smoke-dynamic.sh b/smoke-test/smoke-dynamic.sh index 85fd9892..03e06bd6 100755 --- a/smoke-test/smoke-dynamic.sh +++ b/smoke-test/smoke-dynamic.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env sh K0SCTL_CONFIG=${K0SCTL_CONFIG:-"k0sctl-dynamic.yaml"} @@ -20,10 +20,10 @@ counter=0 echo "* Verifying dynamic config reconciliation was a success" until ../k0sctl config status -o json --config "${K0SCTL_CONFIG}" | grep -q "SuccessfulReconcile" do - [[ counter -eq $max_retry ]] && echo "Failed!" && exit 1 + [ $counter -eq $max_retry ] && echo "Failed!" && exit 1 echo "* Waiting for a couple of seconds to retry" sleep 5 - ((counter++)) + counter=$((counter+1)) done echo "* OK" diff --git a/smoke-test/smoke-files.sh b/smoke-test/smoke-files.sh index eed620e6..74aa72a7 100755 --- a/smoke-test/smoke-files.sh +++ b/smoke-test/smoke-files.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env sh K0SCTL_TEMPLATE=${K0SCTL_TEMPLATE:-"k0sctl.yaml.tpl"} @@ -15,8 +15,7 @@ createCluster remoteCommand() { local userhost="$1" shift - local cmd="$@" - footloose ssh "${userhost}" -- ${cmd} + footloose ssh "${userhost}" -- "$@" } remoteFileExist() { @@ -54,67 +53,67 @@ echo "* Starting apply" echo "* Verifying uploads" remoteCommand root@manager0 "apt-get update > /dev/null && apt-get install tree > /dev/null && tree -fp" -echo -n " - Single file using destination file path and user:group .. " +printf %s " - Single file using destination file path and user:group .. " remoteFileExist root@manager0 /root/singlefile/renamed.txt -echo -n "[exist]" +printf %s "[exist]" remoteCommand root@manager0 stat -c '%U:%G' /root/singlefile/renamed.txt | grep -q test:test -echo -n "[stat]" +printf %s "[stat]" echo "OK" -echo -n " - Single file using destination dir .. " +printf %s " - Single file using destination dir .. " remoteFileExist root@manager0 /root/destdir/toplevel.txt echo "OK" -echo -n " - PermMode 644 .. " +printf %s " - PermMode 644 .. " remoteFileExist root@manager0 /root/chmod/script.sh -echo -n "[exist]" +printf %s "[exist]" remoteCommand root@manager0 stat -c '%a' /root/chmod/script.sh | grep -q 644 -echo -n "[stat] " +printf %s "[stat] " echo "OK" -echo -n " - PermMode transfer .." +printf %s " - PermMode transfer .." remoteFileExist root@manager0 /root/chmod_exec/script.sh -echo -n "[exist] " +printf %s "[exist] " remoteCommand root@manager0 stat -c '%a' /root/chmod_exec/script.sh | grep -q 744 -echo -n "[stat] " +printf %s "[stat] " remoteCommand root@manager0 /root/chmod_exec/script.sh | grep -q hello -echo -n "[run] " +printf %s "[run] " echo "OK" -echo -n " - Directory using destination dir .. " +printf %s " - Directory using destination dir .. " remoteFileExist root@manager0 /root/dir/toplevel.txt -echo -n "[1] " +printf %s "[1] " remoteFileExist root@manager0 /root/dir/nested/nested.txt -echo -n "[2] " +printf %s "[2] " remoteFileExist root@manager0 /root/dir/nested/exclude-on-glob -echo -n "[3] " +printf %s "[3] " echo "OK" -echo -n " - Glob using destination dir .. " +printf %s " - Glob using destination dir .. " remoteFileExist root@manager0 /root/glob/toplevel.txt -echo -n "[1] " +printf %s "[1] " remoteFileExist root@manager0 /root/glob/nested/nested.txt -echo -n "[2] " -! remoteFileExist root@manager0 /root/glob/nested/exclude-on-glob -echo -n "[3] " +printf %s "[2] " +if remoteFileExist root@manager0 /root/glob/nested/exclude-on-glob; then exit 1; fi +printf %s "[3] " remoteCommand root@manager0 stat -c '%a' /root/glob | grep -q 700 -echo -n "[stat1]" +printf %s "[stat1]" remoteCommand root@manager0 stat -c '%a' /root/glob/nested | grep -q 700 -echo -n "[stat2]" +printf %s "[stat2]" echo "OK" -echo -n " - URL using destination file .. " +printf %s " - URL using destination file .. " remoteFileExist root@manager0 /root/url/releases.json -echo -n "[exist] " +printf %s "[exist] " remoteFileContent root@manager0 /root/url/releases.json | grep -q html_url -echo -n "[content] " +printf %s "[content] " echo "OK" -echo -n " - URL using destination dir .. " +printf %s " - URL using destination dir .. " remoteFileExist root@manager0 /root/url_destdir/releases -echo -n "[exist] " +printf %s "[exist] " remoteFileContent root@manager0 /root/url_destdir/releases | grep -q html_url -echo -n "[content] " +printf %s "[content] " echo "OK" echo "* Done" diff --git a/smoke-test/smoke-init.sh b/smoke-test/smoke-init.sh index 528d84a5..6f35e73e 100755 --- a/smoke-test/smoke-init.sh +++ b/smoke-test/smoke-init.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env sh set -e diff --git a/smoke-test/smoke-reset.sh b/smoke-test/smoke-reset.sh index eda92834..fa13f6e6 100755 --- a/smoke-test/smoke-reset.sh +++ b/smoke-test/smoke-reset.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env sh K0SCTL_CONFIG=${K0SCTL_CONFIG:-"k0sctl.yaml"} diff --git a/smoke-test/smoke-upgrade.sh b/smoke-test/smoke-upgrade.sh index 2eeb3cc7..600c1678 100755 --- a/smoke-test/smoke-upgrade.sh +++ b/smoke-test/smoke-upgrade.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env sh K0SCTL_CONFIG=${K0SCTL_CONFIG:-"k0sctl.yaml"} @@ -16,8 +16,6 @@ K0S_VERSION="${K0S_FROM}" echo "Installing ${K0S_VERSION}" ../k0sctl apply --config "${K0SCTL_CONFIG}" --debug -# Create config with blank version (to use latest) and apply as upgrade -K0S_VERSION="$(curl -s https://docs.k0sproject.io/stable.txt)" - +# Create config with latest version and apply as upgrade echo "Upgrading to ${K0S_VERSION}" ../k0sctl apply --config "${K0SCTL_CONFIG}" --debug diff --git a/smoke-test/smoke.common.sh b/smoke-test/smoke.common.sh index 7e1d133a..89df375c 100644 --- a/smoke-test/smoke.common.sh +++ b/smoke-test/smoke.common.sh @@ -1,42 +1,42 @@ FOOTLOOSE_TEMPLATE=${FOOTLOOSE_TEMPLATE:-"footloose.yaml.tpl"} -export LINUX_IMAGE=${LINUX_IMAGE:-"quay.io/footloose/ubuntu18.04"} -export PRESERVE_CLUSTER=${PRESERVE_CLUSTER:-""} +export LINUX_IMAGE="${LINUX_IMAGE:-"quay.io/footloose/ubuntu18.04"}" +export PRESERVE_CLUSTER="${PRESERVE_CLUSTER:-""}" export DISABLE_TELEMETRY=true export K0S_VERSION -function createCluster() { +createCluster() { envsubst < "${FOOTLOOSE_TEMPLATE}" > footloose.yaml footloose create if [ "${LINUX_IMAGE}" = "quay.io/footloose/debian10" ]; then for host in $(footloose status -o json|grep hostname|cut -d"\"" -f4); do - footloose ssh root@${host} -- rm -f /etc/machine-id /var/lib/dbus/machine-id - footloose ssh root@${host} -- systemd-machine-id-setup + footloose ssh root@"${host}" -- rm -f /etc/machine-id /var/lib/dbus/machine-id + footloose ssh root@"${host}" -- systemd-machine-id-setup done fi } -function deleteCluster() { +deleteCluster() { # cleanup any existing cluster envsubst < "${FOOTLOOSE_TEMPLATE}" > footloose.yaml footloose delete && docker volume prune -f } -function cleanup() { - echo -e "Cleaning up..." +cleanup() { + echo "Cleaning up..." if [ -z "${PRESERVE_CLUSTER}" ]; then deleteCluster fi } -function downloadKubectl() { +downloadKubectl() { OS=$(uname | tr '[:upper:]' '[:lower:]') ARCH="amd64" case $(uname -m) in arm,arm64) ARCH="arm64" ;; esac - [ -f kubectl ] || (curl -L https://storage.googleapis.com/kubernetes-release/release/v1.21.3/bin/${OS}/${ARCH}/kubectl > ./kubectl && chmod +x ./kubectl) + [ -f kubectl ] || (curl -L https://storage.googleapis.com/kubernetes-release/release/v1.21.3/bin/"${OS}"/${ARCH}/kubectl > ./kubectl && chmod +x ./kubectl) ./kubectl version --client }