diff --git a/bats/tests/containers/allowed-images.bats b/bats/tests/containers/allowed-images.bats index c380006f5b2..c9a9e89174a 100644 --- a/bats/tests/containers/allowed-images.bats +++ b/bats/tests/containers/allowed-images.bats @@ -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 diff --git a/bats/tests/containers/catch-duplicate-api-patterns.bats b/bats/tests/containers/catch-duplicate-api-patterns.bats index 72def8cf55c..e066b30cb70 100644 --- a/bats/tests/containers/catch-duplicate-api-patterns.bats +++ b/bats/tests/containers/catch-duplicate-api-patterns.bats @@ -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 diff --git a/bats/tests/containers/factory-reset.bats b/bats/tests/containers/factory-reset.bats index c62da5cbe16..1a4ea05c04d 100644 --- a/bats/tests/containers/factory-reset.bats +++ b/bats/tests/containers/factory-reset.bats @@ -1,8 +1,6 @@ -setup() { - load '../helpers/load' - assert=assert - refute=refute -} +load '../helpers/load' +assert=assert +refute=refute @test 'factory reset' { factory_reset diff --git a/bats/tests/containers/host-connectivity.bats b/bats/tests/containers/host-connectivity.bats index a5cc3ec7a97..b6f448afd30 100644 --- a/bats/tests/containers/host-connectivity.bats +++ b/bats/tests/containers/host-connectivity.bats @@ -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 diff --git a/bats/tests/containers/platform.bats b/bats/tests/containers/platform.bats index 902935d18cd..6e08cb5c4f1 100644 --- a/bats/tests/containers/platform.bats +++ b/bats/tests/containers/platform.bats @@ -1,6 +1,4 @@ -setup() { - load '../helpers/load' -} +load '../helpers/load' @test 'factory reset' { factory_reset diff --git a/bats/tests/containers/run-rancher.bats b/bats/tests/containers/run-rancher.bats index ac7bb5c6116..23f00106e10 100644 --- a/bats/tests/containers/run-rancher.bats +++ b/bats/tests/containers/run-rancher.bats @@ -1,6 +1,4 @@ -setup() { - load '../helpers/load' -} +load '../helpers/load' @test 'factory reset' { factory_reset diff --git a/bats/tests/containers/switch-engines.bats b/bats/tests/containers/switch-engines.bats index d94fd0e7743..f1d0d897d32 100644 --- a/bats/tests/containers/switch-engines.bats +++ b/bats/tests/containers/switch-engines.bats @@ -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 diff --git a/bats/tests/extensions/containers.bats b/bats/tests/extensions/containers.bats index 456541f24ac..a3970d56d59 100644 --- a/bats/tests/extensions/containers.bats +++ b/bats/tests/extensions/containers.bats @@ -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') @@ -14,11 +10,6 @@ setup() { fi } -teardown_file() { - run rdctl shutdown - assert_nothing -} - id() { # variant echo "rd/extension/$1" } diff --git a/bats/tests/extensions/install.bats b/bats/tests/extensions/install.bats index f63614eeaad..628cc413dc7 100644 --- a/bats/tests/extensions/install.bats +++ b/bats/tests/extensions/install.bats @@ -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') @@ -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" diff --git a/bats/tests/helpers/defaults.bash b/bats/tests/helpers/defaults.bash index 5381b41e754..bfbc132b2f4 100644 --- a/bats/tests/helpers/defaults.bash +++ b/bats/tests/helpers/defaults.bash @@ -1,4 +1,17 @@ +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}" @@ -6,9 +19,24 @@ : "${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" } diff --git a/bats/tests/helpers/info.bash b/bats/tests/helpers/info.bash new file mode 100644 index 00000000000..3d65093f62f --- /dev/null +++ b/bats/tests/helpers/info.bash @@ -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 +} diff --git a/bats/tests/helpers/load.bash b/bats/tests/helpers/load.bash index f51e05f2781..c25cc382968 100644 --- a/bats/tests/helpers/load.bash +++ b/bats/tests/helpers/load.bash @@ -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" @@ -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 +} diff --git a/bats/tests/helpers/paths.bash b/bats/tests/helpers/paths.bash index f250f100cfe..4504c10fbcb 100644 --- a/bats/tests/helpers/paths.bash +++ b/bats/tests/helpers/paths.bash @@ -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 @@ -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() { @@ -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]}")/.." diff --git a/bats/tests/helpers/vm.bash b/bats/tests/helpers/vm.bash index b38838175a4..d6e9d924cb5 100644 --- a/bats/tests/helpers/vm.bash +++ b/bats/tests/helpers/vm.bash @@ -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 @@ -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+=( @@ -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 diff --git a/bats/tests/k8s/enable-disable-k8s.bats b/bats/tests/k8s/enable-disable-k8s.bats index 25004d949f8..86a1e522259 100644 --- a/bats/tests/k8s/enable-disable-k8s.bats +++ b/bats/tests/k8s/enable-disable-k8s.bats @@ -1,8 +1,6 @@ # Test case 8, 13, 22 -setup() { - load '../helpers/load' -} +load '../helpers/load' @test 'factory reset' { factory_reset diff --git a/bats/tests/k8s/helm-install-rancher.bats b/bats/tests/k8s/helm-install-rancher.bats index aa9b0a8d21f..06a69004356 100644 --- a/bats/tests/k8s/helm-install-rancher.bats +++ b/bats/tests/k8s/helm-install-rancher.bats @@ -1,8 +1,8 @@ # Test case 11 & 12 -setup() { - load '../helpers/load' +load '../helpers/load' +setup() { # 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 diff --git a/bats/tests/k8s/traefik.bats b/bats/tests/k8s/traefik.bats index 9a192449912..6caa2cfa84a 100644 --- a/bats/tests/k8s/traefik.bats +++ b/bats/tests/k8s/traefik.bats @@ -5,9 +5,7 @@ # Test case 25 & 26 -setup() { - load '../helpers/load' -} +load '../helpers/load' @test 'factory reset' { factory_reset diff --git a/bats/tests/k8s/up-downgrade-k8s.bats b/bats/tests/k8s/up-downgrade-k8s.bats index 2329e36f8fb..ce2de5e4b09 100644 --- a/bats/tests/k8s/up-downgrade-k8s.bats +++ b/bats/tests/k8s/up-downgrade-k8s.bats @@ -1,9 +1,7 @@ # Test cases 8, 13, 19 -setup() { - load '../helpers/load' - ARCH_FOR_KUBERLR=amd64 -} +load '../helpers/load' +ARCH_FOR_KUBERLR=amd64 @test 'factory reset' { factory_reset diff --git a/bats/tests/k8s/verify-cached-images.bats b/bats/tests/k8s/verify-cached-images.bats index d2ebff7e7cc..d187f99592e 100644 --- a/bats/tests/k8s/verify-cached-images.bats +++ b/bats/tests/k8s/verify-cached-images.bats @@ -1,8 +1,6 @@ # Test cases 57, 58 -setup() { - load '../helpers/load' -} +load '../helpers/load' @test 'factory reset' { factory_reset diff --git a/bats/tests/k8s/wordpress.bats b/bats/tests/k8s/wordpress.bats index 566af251b8e..d309adec8bc 100644 --- a/bats/tests/k8s/wordpress.bats +++ b/bats/tests/k8s/wordpress.bats @@ -1,9 +1,8 @@ -setup() { - load '../helpers/load' - if is_macos arm64; then - skip "The bitnami wordpress image is not available for arm64 architecture. Skipping..." - fi -} +load '../helpers/load' + +if is_macos arm64; then + fatal "The bitnami wordpress image is not available for arm64 architecture. Skipping..." +fi @test 'factory reset' { factory_reset diff --git a/bats/tests/registry/creds.bats b/bats/tests/registry/creds.bats index 9b2df221b4c..cbdd5a603be 100644 --- a/bats/tests/registry/creds.bats +++ b/bats/tests/registry/creds.bats @@ -1,12 +1,6 @@ -teardown_file() { - load '../helpers/load' - # On Linux if we don't shutdown Rancher Desktop the bats test doesn't shutdown. - run rdctl shutdown - assert_nothing -} +load '../helpers/load' setup() { - load '../helpers/load' REGISTRY_IMAGE="registry:2.8.1" REGISTRY_PORT="5050" DOCKER_CONFIG_FILE="$HOME/.docker/config.json"