Skip to content

Commit

Permalink
Merge pull request #4991 from jandubois/bats-profile
Browse files Browse the repository at this point in the history
Deployment profile testing with BATS
  • Loading branch information
jandubois authored Jul 11, 2023
2 parents e27d477 + 1a74210 commit f606f06
Show file tree
Hide file tree
Showing 17 changed files with 595 additions and 74 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ jobs:
- run: npm run build
- run: npm run lint:nofix
- name: Install shfmt
run: go install mvdan.cc/sh/v3/cmd/shfmt@v3.6.0
run: go install mvdan.cc/sh/v3/cmd/shfmt@v3.7.0
- run: make -C bats lint
- run: npm test
3 changes: 1 addition & 2 deletions bats/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ containers:
# https://www.shellcheck.net/wiki/SC1091 -- Not following: xxx was not specified as input (see shellcheck -x)
# https://www.shellcheck.net/wiki/SC2034 -- xxx appears unused. Verify use (or export if used externally)
# https://www.shellcheck.net/wiki/SC2154 -- xxx is referenced but not assigned
# https://www.shellcheck.net/wiki/SC2155 -- Declare and assign separately to avoid masking return values

SC_EXCLUDES ?= SC1091,SC2034,SC2154,SC2155
SC_EXCLUDES ?= SC1091,SC2034,SC2154

.PHONY: lint
lint:
Expand Down
16 changes: 0 additions & 16 deletions bats/tests/containers/factory-reset.bats
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
load '../helpers/load'
assert=assert
refute=refute

@test 'factory reset' {
factory_reset
Expand Down Expand Up @@ -156,20 +154,6 @@ rdctl_factory_reset() {
fi
}

refute_failure() {
assert_success
}

refute_not_exists() {
assert_exists "$@"
}

before() {
assert=refute
refute=assert
"$@"
}

check_directories() {
# Check if all expected directories are created after starting application/ are deleted after a factory reset
delete_dir=("$PATH_APP_HOME" "$PATH_CONFIG")
Expand Down
2 changes: 1 addition & 1 deletion bats/tests/extensions/allow-list.bats
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ local_setup() {
}

write_allow_list() { # list
local list=${1-}
local list=${1:-}
local allowed=true

if [ -z "$list" ]; then
Expand Down
3 changes: 2 additions & 1 deletion bats/tests/extensions/containers.bats
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ encoded_id() { # variant
}

@test 'compose - with a long name' {
local name="$(id vm-compose)-with-an-unusually-long-name-yes-it-is-very-long"
local name
name="$(id vm-compose)-with-an-unusually-long-name-yes-it-is-very-long"

ctrctl tag "$(id vm-compose)" "$name"
rdctl extension install "$name"
Expand Down
5 changes: 3 additions & 2 deletions bats/tests/extensions/install.bats
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ assert_file_contents_equal() { # $have $want
assert_file_exist "$have"
assert_file_exist "$want"

local have_hash="$(md5sum "$have" | cut -d ' ' -f 1)"
local want_hash="$(md5sum "$want" | cut -d ' ' -f 1)"
local have_hash want_hash
have_hash="$(md5sum "$have" | cut -d ' ' -f 1)"
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" |
Expand Down
9 changes: 0 additions & 9 deletions bats/tests/helpers/commands.bash
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ else
CONTAINER_ENGINE_SERVICE=docker
fi

if is_unix; then
RC_SERVICE=rc-service
elif is_windows; then
RC_SERVICE=wsl-service
fi

if is_macos; then
CRED_HELPER="$PATH_RESOURCES/$PLATFORM/bin/docker-credential-osxkeychain"
elif is_linux; then
Expand Down Expand Up @@ -86,6 +80,3 @@ rdshell() {
rdsudo() {
rdshell sudo "$@"
}
rc_service() {
rdsudo "$RC_SERVICE" "$@"
}
21 changes: 20 additions & 1 deletion bats/tests/helpers/defaults.bash
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,28 @@ using_ghcr_images() {
is_true "$RD_USE_GHCR_IMAGES"
}

########################################################################
: "${RD_DELETE_PROFILES:=true}"

deleting_profiles() {
is_true "$RD_DELETE_PROFILES"
}

########################################################################
: "${RD_USE_IMAGE_ALLOW_LIST:=false}"

using_image_allow_list() {
is_true "$RD_USE_IMAGE_ALLOW_LIST"
}

########################################################################
# RD_USE_PROFILE is for internal use. It uses a profile instead of
# settings.json to set initial values for WSL integrations and allowed
# images list because when settings.json exists the default profile is
# ignored.

: "${RD_USE_PROFILE:=false}"

########################################################################
: "${RD_USE_VZ_EMULATION:=false}"

Expand Down Expand Up @@ -96,7 +111,7 @@ if using_networking_tunnel && ! is_windows; then
fi

########################################################################
if ! is_unix && [ -n "${RD_MOUNT_TYPE-}" ]; then
if ! is_unix && [ -n "${RD_MOUNT_TYPE:-}" ]; then
fatal "RD_MOUNT_TYPE only works on Linux and macOS"
fi

Expand Down Expand Up @@ -126,6 +141,10 @@ validate_enum RD_9P_PROTOCOL_VERSION 9p2000 9p2000.u 9p2000.L

validate_enum RD_9P_SECURITY_MODEL passthrough mapped-xattr mapped-file none

########################################################################
# Use RD_PROTECTED_DOT in profile settings for WSL distro names
: "${RD_PROTECTED_DOT:=·}"

########################################################################
# RD_LOCATION specifies the location where Rancher Desktop is installed
# system: default system-wide install location shared for all users
Expand Down
16 changes: 10 additions & 6 deletions bats/tests/helpers/load.bash
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ source "$PATH_BATS_HELPERS/paths.bash"
# and PATH_* variables from paths.bash
source "$PATH_BATS_HELPERS/commands.bash"

# profile.bash uses is_xxx() from os.bash
source "$PATH_BATS_HELPERS/profile.bash"

# vm.bash uses various PATH_* variables from paths.bash,
# rdctl from commands.bash, and jq_output from utils.bash
source "$PATH_BATS_HELPERS/vm.bash"
Expand All @@ -59,7 +62,8 @@ export PATH="$PATH_BATS_ROOT/bin/${OS/windows/linux}:$PATH"

# If called from foo() this function will call local_foo() if it exist.
call_local_function() {
local func="local_$(calling_function)"
local func
func="local_$(calling_function)"
if [ "$(type -t "$func")" = "function" ]; then
eval "$func"
fi
Expand All @@ -74,14 +78,14 @@ setup_file() {
}

teardown_file() {
call_local_function

capture_logs

# On Linux & Windows if we don't shutdown Rancher Desktop bats test don't terminate
# On Linux & Windows if we don't shutdown Rancher Desktop bats tests don't terminate
if is_linux || is_windows; then
run rdctl shutdown
fi

call_local_function
}

setup() {
Expand All @@ -95,9 +99,9 @@ setup() {
}

teardown() {
call_local_function

if [ -z "$BATS_TEST_SKIPPED" ] && [ -z "$BATS_TEST_COMPLETED" ]; then
take_screenshot
fi

call_local_function
}
6 changes: 3 additions & 3 deletions bats/tests/helpers/os.bash
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ Linux)
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
Loading

0 comments on commit f606f06

Please sign in to comment.