Skip to content

Commit 2bb3225

Browse files
authored
Merge pull request #553 from brianredbeard/brb-pr553
fix(shell): Add error handling, strict mode, and proper quoting to shell scripts
2 parents 6f3d5d8 + 806c7ee commit 2bb3225

File tree

2 files changed

+44
-33
lines changed

2 files changed

+44
-33
lines changed

pattern.sh

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
#!/bin/bash
2+
set -euo pipefail
23

34
function is_available {
4-
command -v $1 >/dev/null 2>&1 || { echo >&2 "$1 is required but it's not installed. Aborting."; exit 1; }
5+
command -v "$1" >/dev/null 2>&1 || { echo >&2 "$1 is required but it's not installed. Aborting."; exit 1; }
56
}
67

78
function version {
8-
echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'
9+
echo "$1" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'
910
}
1011

11-
if [ -z "$PATTERN_UTILITY_CONTAINER" ]; then
12+
if [ -z "${PATTERN_UTILITY_CONTAINER:-}" ]; then
1213
PATTERN_UTILITY_CONTAINER="quay.io/validatedpatterns/utility-container"
1314
fi
1415
# If PATTERN_DISCONNECTED_HOME is set it will be used to populate both PATTERN_UTILITY_CONTAINER
1516
# and PATTERN_INSTALL_CHART automatically
16-
if [ -n "${PATTERN_DISCONNECTED_HOME}" ]; then
17+
if [ -n "${PATTERN_DISCONNECTED_HOME:-}" ]; then
1718
PATTERN_UTILITY_CONTAINER="${PATTERN_DISCONNECTED_HOME}/utility-container"
1819
PATTERN_INSTALL_CHART="oci://${PATTERN_DISCONNECTED_HOME}/pattern-install"
1920
echo "PATTERN_DISCONNECTED_HOME is set to ${PATTERN_DISCONNECTED_HOME}"
@@ -23,10 +24,10 @@ if [ -n "${PATTERN_DISCONNECTED_HOME}" ]; then
2324
fi
2425

2526
readonly commands=(podman)
26-
for cmd in ${commands[@]}; do is_available "$cmd"; done
27+
for cmd in "${commands[@]}"; do is_available "$cmd"; done
2728

2829
UNSUPPORTED_PODMAN_VERSIONS="1.6 1.5"
29-
PODMAN_VERSION_STR=$(podman --version)
30+
PODMAN_VERSION_STR=$(podman --version) || { echo "Failed to get podman version"; exit 1; }
3031
for i in ${UNSUPPORTED_PODMAN_VERSIONS}; do
3132
# We add a space
3233
if echo "${PODMAN_VERSION_STR}" | grep -q -E "\b${i}"; then
@@ -41,19 +42,20 @@ done
4142
PODMAN_VERSION=$(echo "${PODMAN_VERSION_STR}" | awk '{ print $NF }')
4243

4344
# podman < 4.3.0 do not support keep-id:uid=...
44-
if [ $(version "${PODMAN_VERSION}") -lt $(version "4.3.0") ]; then
45-
PODMAN_ARGS="-v ${HOME}:/root"
45+
PODMAN_ARGS=()
46+
if [ "$(version "${PODMAN_VERSION}")" -lt "$(version "4.3.0")" ]; then
47+
PODMAN_ARGS=(-v "${HOME}:/root")
4648
else
4749
# We do not rely on bash's $UID and $GID because on MacOSX $GID is not set
4850
MYNAME=$(id -n -u)
4951
MYUID=$(id -u)
5052
MYGID=$(id -g)
51-
PODMAN_ARGS="--passwd-entry ${MYNAME}:x:${MYUID}:${MYGID}::/pattern-home:/bin/bash --user ${MYUID}:${MYGID} --userns keep-id:uid=${MYUID},gid=${MYGID}"
52-
53+
PODMAN_ARGS=(--passwd-entry "${MYNAME}:x:${MYUID}:${MYGID}::/pattern-home:/bin/bash" --user "${MYUID}:${MYGID}" --userns "keep-id:uid=${MYUID},gid=${MYGID}")
5354
fi
5455

55-
if [ -n "$KUBECONFIG" ]; then
56-
if [[ ! "${KUBECONFIG}" =~ ^$HOME* ]]; then
56+
if [ -n "${KUBECONFIG:-}" ]; then
57+
# Check if KUBECONFIG path starts with HOME directory
58+
if [[ ! "${KUBECONFIG}" =~ ^"${HOME}" ]]; then
5759
echo "${KUBECONFIG} is pointing outside of the HOME folder, this will make it unavailable from the container."
5860
echo "Please move it somewhere inside your $HOME folder, as that is what gets bind-mounted inside the container"
5961
exit 1
@@ -62,20 +64,26 @@ fi
6264

6365
# Detect if we use podman machine. If we do not then we bind mount local host ssl folders
6466
# if we are using podman machine then we do not bind mount anything (for now!)
65-
REMOTE_PODMAN=$(podman system connection list | tail -n +2 | wc -l)
66-
if [ $REMOTE_PODMAN -eq 0 ]; then # If we are not using podman machine we check the hosts folders
67+
REMOTE_PODMAN=$(podman system connection list | tail -n +2 | wc -l) || REMOTE_PODMAN=0
68+
PKI_HOST_MOUNT_ARGS=()
69+
if [ "${REMOTE_PODMAN}" -eq 0 ]; then # If we are not using podman machine we check the hosts folders
6770
# We check /etc/pki/tls because on ubuntu /etc/pki/fwupd sometimes
6871
# exists but not /etc/pki/tls and we do not want to bind mount in such a case
6972
# as it would find no certificates at all.
7073
if [ -d /etc/pki/tls ]; then
71-
PKI_HOST_MOUNT_ARGS="-v /etc/pki:/etc/pki:ro"
74+
PKI_HOST_MOUNT_ARGS=(-v /etc/pki:/etc/pki:ro)
7275
elif [ -d /etc/ssl ]; then
73-
PKI_HOST_MOUNT_ARGS="-v /etc/ssl:/etc/ssl:ro"
76+
PKI_HOST_MOUNT_ARGS=(-v /etc/ssl:/etc/ssl:ro)
7477
else
75-
PKI_HOST_MOUNT_ARGS="-v /usr/share/ca-certificates:/usr/share/ca-certificates:ro"
78+
PKI_HOST_MOUNT_ARGS=(-v /usr/share/ca-certificates:/usr/share/ca-certificates:ro)
7679
fi
77-
else
78-
PKI_HOST_MOUNT_ARGS=""
80+
fi
81+
82+
# Parse EXTRA_ARGS into an array if set
83+
EXTRA_ARGS_ARRAY=()
84+
if [ -n "${EXTRA_ARGS:-}" ]; then
85+
# shellcheck disable=SC2206
86+
EXTRA_ARGS_ARRAY=(${EXTRA_ARGS})
7987
fi
8088

8189
# Copy Kubeconfig from current environment. The utilities will pick up ~/.kube/config if set so it's not mandatory
@@ -106,12 +114,12 @@ podman run -it --rm --pull=newer \
106114
-e TOKEN_SECRET \
107115
-e UUID_FILE \
108116
-e VALUES_SECRET \
109-
${PKI_HOST_MOUNT_ARGS} \
117+
"${PKI_HOST_MOUNT_ARGS[@]}" \
110118
-v "$(pwd -P)":"$(pwd -P)" \
111119
-v "${HOME}":"${HOME}" \
112120
-v "${HOME}":/pattern-home \
113-
${PODMAN_ARGS} \
114-
${EXTRA_ARGS} \
121+
"${PODMAN_ARGS[@]}" \
122+
"${EXTRA_ARGS_ARRAY[@]}" \
115123
-w "$(pwd -P)" \
116124
"$PATTERN_UTILITY_CONTAINER" \
117-
$@
125+
"$@"

tests/interop/run_tests.sh

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,39 @@
11
#!/usr/bin/bash
2+
set -euo pipefail
23

34
export EXTERNAL_TEST="true"
45
export PATTERN_NAME="MultiCloudGitops"
56
export PATTERN_SHORTNAME="mcgitops"
67

7-
if [ -z "${KUBECONFIG}" ]; then
8+
if [ -z "${KUBECONFIG:-}" ]; then
89
echo "No kubeconfig file set for hub cluster"
910
exit 1
1011
fi
1112

12-
if [ -z "${KUBECONFIG_EDGE}" ]; then
13+
if [ -z "${KUBECONFIG_EDGE:-}" ]; then
1314
echo "No kubeconfig file set for edge cluster"
1415
exit 1
1516
fi
1617

17-
if [ -z "${INFRA_PROVIDER}" ]; then
18+
if [ -z "${INFRA_PROVIDER:-}" ]; then
1819
echo "INFRA_PROVIDER is not defined"
1920
exit 1
2021
fi
2122

22-
if [ -z "${WORKSPACE}" ]; then
23-
export WORKSPACE=/tmp
23+
if [ -z "${WORKSPACE:-}" ]; then
24+
WORKSPACE=$(mktemp -d)
25+
export WORKSPACE
26+
echo "WORKSPACE not set, using temporary directory: ${WORKSPACE}"
2427
fi
2528

26-
pytest -lv --disable-warnings test_subscription_status_hub.py --kubeconfig $KUBECONFIG --junit-xml $WORKSPACE/test_subscription_status_hub.xml
29+
pytest -lv --disable-warnings test_subscription_status_hub.py --kubeconfig "$KUBECONFIG" --junit-xml "$WORKSPACE/test_subscription_status_hub.xml"
2730

28-
pytest -lv --disable-warnings test_subscription_status_edge.py --kubeconfig $KUBECONFIG_EDGE --junit-xml $WORKSPACE/test_subscription_status_edge.xml
31+
pytest -lv --disable-warnings test_subscription_status_edge.py --kubeconfig "$KUBECONFIG_EDGE" --junit-xml "$WORKSPACE/test_subscription_status_edge.xml"
2932

30-
pytest -lv --disable-warnings test_validate_hub_site_components.py --kubeconfig $KUBECONFIG --junit-xml $WORKSPACE/test_validate_hub_site_components.xml
33+
pytest -lv --disable-warnings test_validate_hub_site_components.py --kubeconfig "$KUBECONFIG" --junit-xml "$WORKSPACE/test_validate_hub_site_components.xml"
3134

32-
pytest -lv --disable-warnings test_validate_edge_site_components.py --kubeconfig $KUBECONFIG_EDGE --junit-xml $WORKSPACE/test_validate_edge_site_components.xml
35+
pytest -lv --disable-warnings test_validate_edge_site_components.py --kubeconfig "$KUBECONFIG_EDGE" --junit-xml "$WORKSPACE/test_validate_edge_site_components.xml"
3336

34-
pytest -lv --disable-warnings test_modify_web_content.py --kubeconfig $KUBECONFIG --junit-xml $WORKSPACE/test_modify_web_content.xml
37+
pytest -lv --disable-warnings test_modify_web_content.py --kubeconfig "$KUBECONFIG" --junit-xml "$WORKSPACE/test_modify_web_content.xml"
3538

3639
python3 create_ci_badge.py

0 commit comments

Comments
 (0)