Skip to content

Commit

Permalink
Add support for picking RD_LOCATION for BATS
Browse files Browse the repository at this point in the history
Choose from system/user/dist/npm or pick first available.
dist and npm only work when running from inside a git clone.
npm is not yet supported on Windows.

Also move "load ../helpers/load" from setup() to top level,
so we can abort on fatal errors.

Predefine teardown_file() with `rdctl shutdown` so it doesn't
have to be repeated in every test file.

Signed-off-by: Jan Dubois <jan.dubois@suse.com>
  • Loading branch information
jandubois committed Apr 28, 2023
1 parent 05a20a8 commit e43a256
Show file tree
Hide file tree
Showing 20 changed files with 181 additions and 123 deletions.
12 changes: 2 additions & 10 deletions bats/tests/containers/allowed-images.bats
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
setup() {
load '../helpers/load'
RD_USE_IMAGE_ALLOW_LIST=true
}

teardown_file() {
load '../helpers/load'
run rdctl shutdown
assert_nothing
}
load '../helpers/load'
RD_USE_IMAGE_ALLOW_LIST=true

@test 'start' {
factory_reset
Expand Down
12 changes: 2 additions & 10 deletions bats/tests/containers/catch-duplicate-api-patterns.bats
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
setup() {
load '../helpers/load'
RD_USE_IMAGE_ALLOW_LIST=true
}

teardown_file() {
load '../helpers/load'
run rdctl shutdown
assert_nothing
}
load '../helpers/load'
RD_USE_IMAGE_ALLOW_LIST=true

@test 'catch attempts to add duplicate patterns via the API with enabled on' {
factory_reset
Expand Down
8 changes: 3 additions & 5 deletions bats/tests/containers/factory-reset.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
setup() {
load '../helpers/load'
assert=assert
refute=refute
}
load '../helpers/load'
assert=assert
refute=refute

@test 'factory reset' {
factory_reset
Expand Down
4 changes: 1 addition & 3 deletions bats/tests/containers/host-connectivity.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
# between the host and the container. Please check the below link for instructions.
# https://docs.rancherdesktop.io/faq#q-can-containers-reach-back-to-host-services-via-hostdockerinternal

setup() {
load '../helpers/load'
}
load '../helpers/load'

@test 'factory reset' {
factory_reset
Expand Down
4 changes: 1 addition & 3 deletions bats/tests/containers/platform.bats
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
setup() {
load '../helpers/load'
}
load '../helpers/load'

@test 'factory reset' {
factory_reset
Expand Down
4 changes: 1 addition & 3 deletions bats/tests/containers/run-rancher.bats
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
setup() {
load '../helpers/load'
}
load '../helpers/load'

@test 'factory reset' {
factory_reset
Expand Down
12 changes: 2 additions & 10 deletions bats/tests/containers/switch-engines.bats
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
# Test case 20

setup() {
load '../helpers/load'
RD_CONTAINER_ENGINE=moby
}

teardown_file() {
load '../helpers/load'
run rdctl shutdown
assert_nothing
}
load '../helpers/load'
RD_CONTAINER_ENGINE=moby

switch_container_engine() {
local name=$1
Expand Down
13 changes: 2 additions & 11 deletions bats/tests/extensions/containers.bats
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
setup_file() {
load '../helpers/load'
}
load '../helpers/load'

setup() {
load '../helpers/load'

TESTDATA_DIR="${PATH_TEST_ROOT}/extensions/testdata/"
TESTDATA_DIR="${PATH_BATS_ROOT}/tests/extensions/testdata/"

if using_containerd; then
namespace_arg=('--namespace=rancher-desktop-extensions')
Expand All @@ -14,11 +10,6 @@ setup() {
fi
}

teardown_file() {
run rdctl shutdown
assert_nothing
}

id() { # variant
echo "rd/extension/$1"
}
Expand Down
13 changes: 2 additions & 11 deletions bats/tests/extensions/install.bats
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
setup_file() {
load '../helpers/load'
}
load '../helpers/load'

setup() {
load '../helpers/load'

TESTDATA_DIR="${PATH_TEST_ROOT}/extensions/testdata/"
TESTDATA_DIR="${PATH_BATS_ROOT}/tests/extensions/testdata/"

if using_containerd; then
namespace_arg=('--namespace=rancher-desktop-extensions')
Expand All @@ -14,11 +10,6 @@ setup() {
fi
}

teardown_file() {
run rdctl shutdown
assert_nothing
}

assert_file_contents_equal() { # $have $want
local have="$1" want="$2"
assert_file_exist "$have"
Expand Down
28 changes: 28 additions & 0 deletions bats/tests/helpers/defaults.bash
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
validate_enum() {
local var=$1
shift
for value in "$@"; do
if [ "${!var}" = "$value" ]; then
return
fi
done
fatal "$var=${!var} is not a valid setting; select from [$*]"
}

: "${RD_CONTAINER_ENGINE:=containerd}"
validate_enum RD_CONTAINER_ENGINE containerd moby

: "${RD_KUBERNETES_VERSION:=1.23.6}"
: "${RD_KUBERNETES_PREV_VERSION:=1.22.7}"
: "${RD_RANCHER_IMAGE_TAG:=v2.7.0}"

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

# RD_LOCATION specifies the location where Rancher Desktop is installed
# system: default system-wide install location shared for all users
# user: per-user install location
# dist: use the result of `npm run package` in ../dist
# npm: dev mode; start app with `cd ..; npm run dev`
# "": use first location from the list above that contains the app

: "${RD_LOCATION:=}"
validate_enum RD_LOCATION system user dist npm ""

using_npm_run_dev() {
[ "$RD_LOCATION" = "npm" ]
}

using_image_allow_list() {
is_true "$RD_USE_IMAGE_ALLOW_LIST"
}

using_windows_exe() {
is_true "$RD_USE_WINDOWS_EXE"
}
11 changes: 11 additions & 0 deletions bats/tests/helpers/load.bash
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ PATH_BATS_ROOT=$(
pwd
)

# Use fatal() to abort loading helpers; don't run any tests
fatal() {
echo " $1" >&3
exit 1
}

source "$PATH_BATS_ROOT/bats-support/load.bash"
source "$PATH_BATS_ROOT/bats-assert/load.bash"
source "$PATH_BATS_ROOT/bats-file/load.bash"
Expand All @@ -27,3 +33,8 @@ source "$PATH_BATS_HELPERS/commands.bash"

# Use Linux utilities (like jq) on WSL
export PATH="$PATH_BATS_ROOT/bin/${OS/windows/linux}:$PATH"

# On Linux if we don't shutdown Rancher Desktop the bats test doesn't terminate
teardown_file() {
run rdctl shutdown
}
90 changes: 66 additions & 24 deletions bats/tests/helpers/paths.bash
Original file line number Diff line number Diff line change
@@ -1,17 +1,64 @@
# PATH_BATS_ROOT and PATH_BATS_HELPERS are already set by load.bash

PATH_REPO_ROOT=$(
# We are running with `set -o errexit`, so no need for explicit error checking
# shellcheck disable=SC2164
cd "$PATH_BATS_ROOT/.."
pwd
)

set_path_resources() {
local system=$1
local user=$2
local dist=$3
local subdir=$4

if [ -z "$RD_LOCATION" ]; then
if [ -d "$system" ]; then
RD_LOCATION=system
elif [ -d "$user" ]; then
RD_LOCATION=user
elif [ -d "$PATH_REPO_ROOT/pkg/rancher-desktop" ]; then
# dist and npm only work when inside a repo clone
if [ -d "$dist" ]; then
RD_LOCATION=dist
else
RD_LOCATION=npm
fi
else
fatal "Could not locate Rancher Desktop installation"
fi
fi
if using_npm_run_dev; then
if is_windows; then
fatal "npm operation not yet implemented for Windows"
fi
PATH_RESOURCES="$PATH_REPO_ROOT/resources"
else
PATH_RESOURCES="${!RD_LOCATION}/${subdir}"
fi
if [ ! -d "$PATH_RESOURCES" ]; then
fatal "App resource directory '$PATH_RESOURCES' does not exist"
fi
}

if is_macos; then
PATH_APP_HOME="$HOME/Library/Application Support/rancher-desktop"
PATH_CONFIG="$HOME/Library/Preferences/rancher-desktop"
PATH_CACHE="$HOME/Library/Caches/rancher-desktop"
PATH_LOGS="$HOME/Library/Logs/rancher-desktop"
PATH_EXTENSIONS="$PATH_APP_HOME/extensions"
if test -d "/Applications/Rancher Desktop.app"; then
PATH_EXECUTABLE="/Applications/Rancher Desktop.app/Contents/MacOS/Rancher Desktop"
PATH_RESOURCES="/Applications/Rancher Desktop.app/Contents/Resources/resources"
else
PATH_EXECUTABLE="$(dirname "${BASH_SOURCE[0]}")/../../../dist/mac-$(uname -m)/Rancher Desktop.app/Contents/MacOS/Rancher Desktop"
PATH_RESOURCES="$(dirname "${BASH_SOURCE[0]}")/../../../dist/mac-$(uname -m)/Rancher Desktop.app/Contents/Resources/resources"
fi
LIMA_HOME="$PATH_APP_HOME/lima"

DIST_ARCH="mac"
if is_macos arm64; then
DIST_ARCH="mac-arm64"
fi
set_path_resources \
"/Applications/Rancher Desktop.app" \
"$HOME/Applications/Rancher Desktop.app" \
"$PATH_REPO_ROOT/dist/$DIST_ARCH/Rancher Desktop.app" \
"Contents/Resources/resources"
fi

if is_linux; then
Expand All @@ -21,13 +68,13 @@ if is_linux; then
PATH_DATA="$HOME/.local/share/rancher-desktop"
PATH_LOGS="$PATH_DATA/logs"
PATH_EXTENSIONS="$PATH_DATA/extensions"
PATH_EXECUTABLE="" # TODO
if test -d "/opt/rancher-desktop"; then
PATH_RESOURCES="/opt/rancher-desktop/resources/resources"
else
PATH_RESOURCES="$(dirname "${BASH_SOURCE[0]}")/../../../dist/linux-unpacked/resources/resources"
fi
LIMA_HOME="$PATH_DATA/lima"

set_path_resources \
"/opt/rancher-desktop" \
"/no user location on linux" \
"$PATH_REPO_ROOT/dist/linux-unpacked" \
"resources/resources"
fi

win32env() {
Expand All @@ -50,17 +97,12 @@ if is_windows; then
PATH_DISTRO="$PATH_DATA/distro"
PATH_DISTRO_DATA="$PATH_DATA/distro-data"
PATH_EXTENSIONS="$PATH_DATA/extensions"
if test -d "$PROGRAMFILES/Rancher Desktop"; then
PATH_EXECUTABLE="$PROGRAMFILES/Rancher Desktop/Rancher Desktop.exe"
PATH_RESOURCES="$PROGRAMFILES/Rancher Desktop/resources/resources"
elif test -d "$LOCALAPPDATA/Programs/Rancher Desktop"; then
PATH_EXECUTABLE="$LOCALAPPDATA/Programs/Rancher Desktop/Rancher Desktop.exe"
PATH_RESOURCES="$LOCALAPPDATA/Programs/Rancher Desktop/resources/resources"
else
PATH_EXECUTABLE="$(dirname "${BASH_SOURCE[0]}")/../../../dist/win-unpacked/Rancher Desktop.exe"
PATH_RESOURCES="$(dirname "${BASH_SOURCE[0]}")/../../../dist/win-unpacked/resources/resources"
fi

set_path_resources \
"$PROGRAMFILES/Rancher Desktop" \
"$LOCALAPPDATA/Programs/Rancher Desktop" \
"$PATH_REPO_ROOT/dist/win-unpacked" \
"resources/resources"
fi

PATH_CONFIG_FILE="$PATH_CONFIG/settings.json"
PATH_TEST_ROOT="$(dirname "${BASH_SOURCE[0]}")/.."
52 changes: 47 additions & 5 deletions bats/tests/helpers/vm.bash
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ wait_for_shell() {
factory_reset() {
rdctl factory-reset

if is_unix; then
if pgrep -f rancher-desktop; then
pkill -f rancher-desktop
# Re-run because RD was not stopped before deleting the data
rdctl factory-reset
fi
else
# TODO: kill `npm run dev` instance on Windows
true
fi

if is_windows; then
run sudo ip link delete docker0
run sudo ip link delete nerdctl0
Expand All @@ -21,11 +32,32 @@ factory_reset() {
fi
}

# Turn `rdctl start` arguments into `npm run dev` arguments
apify_arg() {
# TODO this should be done via autogenerated code from command-api.yaml
perl -w - "$1" <<'EOF'
# don't modify the value part after the first '=' sign
($_, my $value) = split /=/, shift, 2;
if (/^--/) {
# turn "--virtual-machine.memory-in-gb" into "--virtualMachine.memoryInGb"
s/(\w)-(\w)/$1\U$2/g;
# fixup acronyms
s/memoryInGb/memoryInGB/;
s/numberCpus/numberCPUs/;
s/socketVmnet/socketVMNet/;
s/msizeInKb/msizeInKB/;
s/--wsl/--WSL/;
}
print;
print "=$value" if $value;
EOF
}

start_container_engine() {
local args=(
--application.updater.enabled=false
--container-engine="$RD_CONTAINER_ENGINE"
--kubernetes-enabled=false
--container-engine.name="$RD_CONTAINER_ENGINE"
--kubernetes.enabled=false
)
if is_unix; then
args+=(
Expand Down Expand Up @@ -56,9 +88,19 @@ start_container_engine() {
}
EOF

# Detach `rdctl start` because on Windows the process may not exit until
# Rancher Desktop itself quits.
rdctl start "${args[@]}" "$@" &
if using_npm_run_dev; then
# translated args back into the internal API format
api_args=()
for arg in "${args[@]}"; do
api_args+=("$(apify_arg "$arg")")
done

npm run dev -- "${api_args[@]}" "$@" &
else
# Detach `rdctl start` because on Windows the process may not exit until
# Rancher Desktop itself quits.
rdctl start "${args[@]}" "$@" &
fi
}

# shellcheck disable=SC2120
Expand Down
Loading

0 comments on commit e43a256

Please sign in to comment.