Skip to content

Commit

Permalink
Add shfmt to enforce consistent formatting of bats tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Dubois <[email protected]>
  • Loading branch information
jandubois committed Mar 29, 2023
1 parent 897ce97 commit 1f58f26
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 108 deletions.
2 changes: 0 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,3 @@ indent_style = tab

[*.{sh,bash,bats}]
indent_size = 4
# shfmt option
keep_padding = true
17 changes: 9 additions & 8 deletions bats/tests/containers/factory-reset.bats
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ start_application() {
rdctl_factory_reset() {
rdctl factory-reset "$@"

if [[ "$1" == "--remove-kubernetes-cache=true" ]]; then
if [[ $1 == "--remove-kubernetes-cache=true" ]]; then
assert_not_exist "$PATH_CACHE"
else
assert_exists "$PATH_CACHE"
Expand All @@ -65,7 +65,7 @@ check_installation() {
local assert=assert
local refute=refute

if [ "${1:-}" == "before" ]; then
if [ "${1-}" == "before" ]; then
assert=refute
refute=assert
fi
Expand Down Expand Up @@ -100,16 +100,17 @@ check_installation() {
# Check if ./rd/bin was removed from the path
if is_unix; then
# TODO add check for config.fish
env_profiles=("$HOME/.bashrc"
"$HOME/.zshrc"
"$HOME/.cshrc"
"$HOME/.tcshrc"
)
env_profiles=(
"$HOME/.bashrc"
"$HOME/.zshrc"
"$HOME/.cshrc"
"$HOME/.tcshrc"
)
for candidate in .bash_profile .bash_login .profile; do
if [ -e "$HOME/$candidate" ]; then
env_profiles+=("$HOME/$candidate")
# Only the first candidate that exists will be modified
if [ "${1:-}" = "before" ]; then
if [ "${1-}" = "before" ]; then
break
fi
fi
Expand Down
6 changes: 3 additions & 3 deletions bats/tests/extensions/containers.bats
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace_arg() {

@test 'build extension testing images' {
local extension
for extension in vm-image vm-compose ; do
for extension in vm-image vm-compose; do
ctrctl build \
"$(namespace_arg)" \
--tag rd/extension/$extension \
Expand All @@ -54,7 +54,7 @@ namespace_arg() {

run rdctl api /v1/extensions
assert_success
output="$(jq ".[\"$(id vm-image)\"]" <<< "${output}")"
output="$(jq ".[\"$(id vm-image)\"]" <<<"${output}")"
assert_output true
}

Expand All @@ -77,7 +77,7 @@ namespace_arg() {

run rdctl api /v1/extensions
assert_success
output="$(jq ".[\"$(id vm-compose)\"]" <<< "${output}")"
output="$(jq ".[\"$(id vm-compose)\"]" <<<"${output}")"
assert_output true
}

Expand Down
6 changes: 3 additions & 3 deletions bats/tests/extensions/install.bats
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ assert_file_contents_equal() { # $have $want
local want_hash="$(md5sum "$want" | cut -d ' ' -f 1)"
if [ "$have_hash" != "$want_hash" ]; then
printf "expected : %s (%s)\nactual : %s (%s)" \
"$want" "$want_hash" "$have" "$have_hash" \
| batslib_decorate "files are different" \
| fail
"$want" "$want_hash" "$have" "$have_hash" |
batslib_decorate "files are different" |
fail
fi
}

Expand Down
2 changes: 1 addition & 1 deletion bats/tests/helpers/kubernetes.bash
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
wait_for_apiserver() {
local desired_version="${1:-$RD_KUBERNETES_PREV_VERSION}"
while true; do
until kubectl get --raw /readyz &> /dev/null; do sleep 1; done
until kubectl get --raw /readyz &>/dev/null; do sleep 1; done
sleep 1
run kubectl get node -o jsonpath="{.items[0].status.nodeInfo.kubeletVersion}"
if [ "$status" -eq 0 ]; then
Expand Down
37 changes: 19 additions & 18 deletions bats/tests/helpers/os.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,40 @@ UNAME=$(uname)
ARCH=$(uname -m)

case $UNAME in
Darwin)
# OS matches the directory name of the PATH_RESOURCES directory,
# so uses "darwin" and not "macos".
OS=darwin
;;
Linux)
if [[ $(uname -a) =~ microsoft ]]; then
OS=windows
else
OS=linux
fi
;;
*)
echo "Unexpected uname: $UNAME" >&2
exit 1
Darwin)
# OS matches the directory name of the PATH_RESOURCES directory,
# so uses "darwin" and not "macos".
OS=darwin
;;
Linux)
if [[ $(uname -a) =~ microsoft ]]; then
OS=windows
else
OS=linux
fi
;;
*)
echo "Unexpected uname: $UNAME" >&2
exit 1
;;
esac

is_linux() {
if [ -z "${1:-}" ]; then
if [ -z "${1-}" ]; then
test "$OS" = linux
else
test "$OS" = linux -a "$ARCH" = "$1"
fi
}
is_macos() {
if [ -z "${1:-}" ]; then
if [ -z "${1-}" ]; then
test "$OS" = darwin
else
test "$OS" = darwin -a "$ARCH" = "$1"
fi
}
is_windows() {
if [ -z "${1:-}" ]; then
if [ -z "${1-}" ]; then
test "$OS" = windows
else
test "$OS" = windows -a "$ARCH" = "$1"
Expand Down
2 changes: 1 addition & 1 deletion bats/tests/helpers/paths.bash
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ win32env() {
# The cmd.exe _sometimes_ returns an empty string when invoked in a subshell
# wslpath "$(cmd.exe /c "echo %$1%" 2>/dev/null)" | tr -d "\r"
# Let's see if powershell.exe avoids this issue
wslpath "$(powershell.exe -Command "Write-Output \${Env:$1}")" | tr -d "\r"
wslpath "$(powershell.exe -Command "Write-Output \${Env:$1}")" | tr -d "\r"
}

if is_windows; then
Expand Down
36 changes: 18 additions & 18 deletions bats/tests/helpers/utils.bash
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
is_true() {
# case-insensitive check; false values: '', '0', 'no', and 'false'
local value="$(echo "$1" | tr '[:upper:]' '[:lower:]')"
if [[ "$value" =~ ^(0|no|false)?$ ]]; then
if [[ $value =~ ^(0|no|false)?$ ]]; then
false
else
true
Expand Down Expand Up @@ -31,22 +31,22 @@ try() {
local delay=5
while [[ $# -gt 0 ]] && [[ $1 == -* ]]; do
case "$1" in
--max)
max=$2
shift
;;
--delay)
delay=$2
shift
;;
--)
shift
break
;;
*)
printf "Usage error: unknown flag '%s'" "$1" >&2
return 1
;;
--max)
max=$2
shift
;;
--delay)
delay=$2
shift
;;
--)
shift
break
;;
*)
printf "Usage error: unknown flag '%s'" "$1" >&2
return 1
;;
esac
shift
done
Expand All @@ -55,6 +55,6 @@ try() {
run "$@"
[ "$status" -eq 0 ] && return
sleep "$delay"
count=$(( count + 1 ))
count=$((count + 1))
done
}
10 changes: 5 additions & 5 deletions bats/tests/helpers/vm.bash
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ factory_reset() {
fi

mkdir -p "$PATH_CONFIG"
cat <<EOF > "$PATH_CONFIG_FILE"
cat <<EOF >"$PATH_CONFIG_FILE"
{
"version": 5,
"application": {
Expand Down Expand Up @@ -63,10 +63,10 @@ start_container_engine() {
# Detach `rdctl start` because on Windows the process may not exit until
# Rancher Desktop itself quits.
rdctl start \
--application.updater.enabled=false \
--container-engine="$RD_CONTAINER_ENGINE" \
--kubernetes-enabled=false \
"$@" &
--application.updater.enabled=false \
--container-engine="$RD_CONTAINER_ENGINE" \
--kubernetes-enabled=false \
"$@" &
}

start_kubernetes() {
Expand Down
6 changes: 3 additions & 3 deletions bats/tests/k8s/helm-install-rancher.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
setup() {
load '../helpers/load'

# TODO - Consider implementing a function to check for sudo permissions before running tests that require them.
# TODO - Consider implementing a function to check for sudo permissions before running tests that require them.
# If sudo permissions are not present, these tests should be skipped.
if is_linux; then
run sudo sysctl -w net.ipv4.ip_unprivileged_port_start=443
Expand All @@ -21,7 +21,7 @@ setup() {
}

@test 'add helm repo' {
helm repo add jetstack https://charts.jetstack.io
helm repo add jetstack https://charts.jetstack.io
helm repo add rancher-latest https://releases.rancher.com/server-charts/latest
helm repo update
}
Expand All @@ -44,7 +44,7 @@ get_host() {
--create-namespace
helm upgrade \
--install rancher rancher-latest/rancher \
--namespace cattle-system \
--namespace cattle-system \
--set hostname="$(get_host)" \
--wait \
--timeout=10m \
Expand Down
46 changes: 23 additions & 23 deletions bats/tests/k8s/traefik.bats
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ get_host() {
assert_traefik_pods_are_down() {
run kubectl get --all-namespaces pods

if [[ "$output" != *"connection refused"* ]] && \
[[ "$output" != *"No resources found"* ]] && \
[[ "$output" != *"ContainerCreating"* ]] && \
[[ "$output" != *"Pending"* ]] && \
[[ "$output" != *"Completed"* ]] && \
[[ "$output" != *"Terminating"* ]] && \
[[ "$output" != *"traefik"* ]]; then
if [[ $output != *"connection refused"* ]] &&
[[ $output != *"No resources found"* ]] &&
[[ $output != *"ContainerCreating"* ]] &&
[[ $output != *"Pending"* ]] &&
[[ $output != *"Completed"* ]] &&
[[ $output != *"Terminating"* ]] &&
[[ $output != *"traefik"* ]]; then
return 0
else
return 1
Expand All @@ -45,12 +45,12 @@ assert_traefik_pods_are_down() {
assert_traefik_pods_are_up() {
run kubectl get --all-namespaces pods

if [[ "$output" != *"connection refused"* ]] && \
[[ "$output" != *"No resources found"* ]] && \
[[ "$output" != *"ContainerCreating"* ]] && \
[[ "$output" != *"Pending"* ]] && \
[[ "$output" != *"Terminating"* ]] && \
[[ "$output" =~ "traefik" ]]; then
if [[ $output != *"connection refused"* ]] &&
[[ $output != *"No resources found"* ]] &&
[[ $output != *"ContainerCreating"* ]] &&
[[ $output != *"Pending"* ]] &&
[[ $output != *"Terminating"* ]] &&
[[ $output =~ "traefik" ]]; then
return 0
else
return 1
Expand All @@ -70,14 +70,14 @@ assert_traefik_pods_are_up() {
}

@test 'enable traefik' {
# Enable traefik
rdctl set --kubernetes.options.traefik=TRUE
wait_for_apiserver
# Check if the traefik pods come up
try --max 30 --delay 10 assert_traefik_pods_are_up
assert_success
run curl "http://$(get_host):80"
[ "$status" -ne 0 ]
run curl -k "https://$(get_host):443"
[ "$status" -ne 0 ]
# Enable traefik
rdctl set --kubernetes.options.traefik=TRUE
wait_for_apiserver
# Check if the traefik pods come up
try --max 30 --delay 10 assert_traefik_pods_are_up
assert_success
run curl "http://$(get_host):80"
[ "$status" -ne 0 ]
run curl -k "https://$(get_host):443"
[ "$status" -ne 0 ]
}
12 changes: 6 additions & 6 deletions bats/tests/k8s/up-downgrade-k8s.bats
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ verify_images() {
}

verify_nginx_after_change_k8s() {
run curl http://localhost:8686
assert_failure
assert_output --partial "Failed to connect to localhost port 8686"
run curl http://localhost:8686
assert_failure
assert_output --partial "Failed to connect to localhost port 8686"

run curl http://localhost:8585
assert_success
assert_output --partial "Welcome to nginx!"
run curl http://localhost:8585
assert_success
assert_output --partial "Welcome to nginx!"
}

@test 'verify nginx after upgrade' {
Expand Down
10 changes: 5 additions & 5 deletions bats/tests/k8s/wordpress.bats
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ setup() {

@test 'deploy wordpress' {
helm install wordpress bitnami/wordpress \
--wait \
--timeout 20m \
--set service.type=NodePort \
--set volumePermissions.enabled=true \
--set mariadb.volumePermissions.enabled=true
--wait \
--timeout 20m \
--set service.type=NodePort \
--set volumePermissions.enabled=true \
--set mariadb.volumePermissions.enabled=true
}

@test 'verify wordpress was deployed' {
Expand Down
Loading

0 comments on commit 1f58f26

Please sign in to comment.