Skip to content

Commit

Permalink
POSIX compatibility for test scripts (#548)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Kimmo Lehto <[email protected]>
  • Loading branch information
twz123 and kke authored Sep 7, 2023
1 parent f9d8305 commit 889148d
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 60 deletions.
14 changes: 7 additions & 7 deletions smoke-test/smoke-backup-restore.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env sh

K0SCTL_CONFIG=${K0SCTL_CONFIG:-"k0sctl.yaml"}

Expand All @@ -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
}
Expand All @@ -19,24 +19,24 @@ 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

# Reset the controller
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
Expand Down
5 changes: 2 additions & 3 deletions smoke-test/smoke-basic.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env sh

K0SCTL_CONFIG=${K0SCTL_CONFIG:-"k0sctl.yaml"}

Expand Down Expand Up @@ -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"

6 changes: 3 additions & 3 deletions smoke-test/smoke-dynamic.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env sh

K0SCTL_CONFIG=${K0SCTL_CONFIG:-"k0sctl-dynamic.yaml"}

Expand All @@ -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"
Expand Down
61 changes: 30 additions & 31 deletions smoke-test/smoke-files.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env sh

K0SCTL_TEMPLATE=${K0SCTL_TEMPLATE:-"k0sctl.yaml.tpl"}

Expand All @@ -15,8 +15,7 @@ createCluster
remoteCommand() {
local userhost="$1"
shift
local cmd="$@"
footloose ssh "${userhost}" -- ${cmd}
footloose ssh "${userhost}" -- "$@"
}

remoteFileExist() {
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion smoke-test/smoke-init.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env sh

set -e

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

K0SCTL_CONFIG=${K0SCTL_CONFIG:-"k0sctl.yaml"}

Expand Down
6 changes: 2 additions & 4 deletions smoke-test/smoke-upgrade.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env sh

K0SCTL_CONFIG=${K0SCTL_CONFIG:-"k0sctl.yaml"}

Expand All @@ -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
20 changes: 10 additions & 10 deletions smoke-test/smoke.common.sh
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 889148d

Please sign in to comment.