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 May 2, 2023
1 parent 05a20a8 commit de61be0
Show file tree
Hide file tree
Showing 21 changed files with 209 additions and 127 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"
}
19 changes: 19 additions & 0 deletions bats/tests/helpers/info.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load load.bash

# This file exists to print information about the configuration just once, e.g.
#
# bats test/helpers/info.bash tests/*
#
# This can't be done during normal `load` operation because then it would be
# printed for every single test run.

# The info output is wrapped in a dummy test because bats doesn't execute files
# that don't contain at least a single test case.
#
# Also use bash-compatible @test syntax so shellcheck doesn't complain. The file
# uses a `.bash` extension so it is not matched by `tests/*` when running all
# tests; you'll want to run it before all the other tests.

info() { # @test
echo "Using '$RD_LOCATION' install; resources '$PATH_RESOURCES'" >&3
}
29 changes: 20 additions & 9 deletions bats/tests/helpers/load.bash
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
set -o errexit -o nounset -o pipefail

# Get absolute path names for current and top directories
PATH_BATS_HELPERS=$(
cd "$(dirname "${BASH_SOURCE[0]}")"
pwd
)
PATH_BATS_ROOT=$(
cd "$PATH_BATS_HELPERS/../.."
pwd
)
absolute_path() {
(
cd "$1"
pwd
)
}

PATH_BATS_HELPERS=$(absolute_path "$(dirname "${BASH_SOURCE[0]}")")
PATH_BATS_ROOT=$(absolute_path "$PATH_BATS_HELPERS/../..")

# 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"
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
}
93 changes: 69 additions & 24 deletions bats/tests/helpers/paths.bash
Original file line number Diff line number Diff line change
@@ -1,17 +1,67 @@
# PATH_BATS_ROOT and PATH_BATS_HELPERS are already set by load.bash

PATH_REPO_ROOT=$(absolute_path "$PATH_BATS_ROOT/..")

inside_repo_clone() {
[ -d "$PATH_REPO_ROOT/pkg/rancher-desktop" ]
}

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 "$dist" ]; then
RD_LOCATION=dist
elif inside_repo_clone; then
RD_LOCATION=npm
else
(
echo "Couldn't locate Rancher Desktop in"
echo "- \"$system\""
echo "- \"$user\""
echo "- \"$dist\""
echo "and 'npm run dev' is unavailable outside repo clone"
) >&3
exit 1
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"

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

if is_linux; then
Expand All @@ -21,13 +71,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 +100,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]}")/.."
Loading

0 comments on commit de61be0

Please sign in to comment.