diff --git a/playbooks/system-test.yaml b/playbooks/system-test.yaml index c2eff3f0d..07749966f 100644 --- a/playbooks/system-test.yaml +++ b/playbooks/system-test.yaml @@ -28,5 +28,6 @@ environment: PODMAN: '/usr/bin/podman' TOOLBOX: '{{ toolbox_bin }}' + TOOLBOX_TESTS_SYSTEM_CONFIG: 1 args: chdir: '{{ zuul.project.src_dir }}' diff --git a/test/system/000-setup.bats b/test/system/000-setup.bats index 019b03594..86e5b8187 100644 --- a/test/system/000-setup.bats +++ b/test/system/000-setup.bats @@ -1,8 +1,11 @@ #!/usr/bin/env bats +load 'libs/bats-support/load' load 'libs/helpers' @test "test suite: Setup" { + _setup_runtime_dir || fail "Failed to setup runtime dir ${RUNTIME_DIR}" + _setup_toolbox_configs || fail "Failed to setup config files" # Cache the default image for the system _pull_and_cache_distro_image $(get_system_id) $(get_system_version) || die # Cache all images that will be needed during the tests diff --git a/test/system/101-create.bats b/test/system/101-create.bats index fc8d4153f..d7e1986b1 100644 --- a/test/system/101-create.bats +++ b/test/system/101-create.bats @@ -5,6 +5,7 @@ load 'libs/bats-assert/load' load 'libs/helpers' setup() { + setup_config_files cleanup_containers } @@ -78,3 +79,28 @@ teardown() { assert_line --index 1 "If it was a private image, log in with: podman login foo.org" assert_line --index 2 "Use 'toolbox --verbose ...' for further details." } + +@test "create: Create a container with overriden distro option (system-wide)" { + skip_if_system_config_disabled +} + +@test "create: Create a container with overriden distro option (user-specific)" { +} + +@test "create: Create a container with overriden release option (system-wide)" { + skip_if_system_config_disabled +} + +@test "create: Create a container with overriden release option (user-specific)" { +} + +@test "create: Create a container with overriden image option (system-wide)" { + skip_if_system_config_disabled +} + +@test "create: Create a container with overriden image option (user-specific)" { +} + +@test "create: Create a container with overriden image option (both system-wide + user-specific)" { + skip_if_system_config_disabled +} diff --git a/test/system/libs/helpers.bash b/test/system/libs/helpers.bash index 0ad7a29ec..0704b1012 100644 --- a/test/system/libs/helpers.bash +++ b/test/system/libs/helpers.bash @@ -8,7 +8,12 @@ readonly TOOLBOX=${TOOLBOX:-toolbox} readonly SKOPEO=$(command -v skopeo) # Helpful globals +readonly RUNTIME_DIR="${XDG_RUNTIME_DIR:-"/run"}/toolbox/system-tests" readonly IMAGE_CACHE_DIR="${BATS_RUN_TMPDIR}/image-cache" +readonly TEST_HOME_DIR="${RUNTIME_DIR}/home" + +readonly SYSTEM_CONFIG="/etc/containers/toolbox.conf" +readonly USER_CONFIG="~/.config/containers/toolbox.conf" # Images declare -Ag IMAGES=([busybox]="docker.io/library/busybox" \ @@ -26,6 +31,28 @@ function cleanup_containers() { } +# Backups writable configuration files +function setup_config_files() { + if [ ${TOOLBOX_TESTS_SYSTEM_CONFIG} -eq 1 ]; then + if ! _is_system_config_writable; then + echo "System config file is not writable. Tests configuring system config file will fail." + return 1 + fi + + rm ${TOOLBOX_SYSTEM_CONFIG} + fi + + export HOME=${TEST_HOME_DIR} + + if ! _is_user_config_writable; then + echo "User config file is not writable. Tests configuring user config file will fail." + return 1 + fi + + rm ${TOOLBOX_USER_CONFIG} +} + + # Pulls an image using Podman and saves it to a image dir using Skopeo # # Parameters @@ -96,6 +123,39 @@ function _clean_cached_images() { } +function _is_system_config_writable() { + return is_path_writable "$TOOLBOX_SYSTEM_CONFIG" +} + + +function _is_user_config_writable() { + return is_path_writable "$TOOLBOX_USER_CONFIG" +} + + +# Restores writable configuration files +function _restore_toolbox_configs() { + if _is_system_config_writable; then + mv $BACKUP_SYSTEM_CONFIG $TOOLBOX_SYSTEM_CONFIG + fi + + if _is_user_config_writable; then + mv $BACKUP_USER_CONFIG $TOOLBOX_USER_CONFIG + fi +} + + +function _setup_runtime_dir() { + local user_id=$(id -u) + + if [ -z "${XDG_RUNTIME_DIR}" ] && [ "${user_id}" -ne 0 ]; then + echo "XDG_RUNTIME_DIR is not set and the user is not root. Runtime dir ${RUNTIME_DIR} will probably be inaccessible." + fi + + mkdir -p "${RUNTIME_DIR}" +} + + # Copies an image from local storage to Podman's image store # # Call before creating any container. Network failures are not nice. @@ -270,3 +330,46 @@ function get_system_version() { echo $(awk -F= '/VERSION_ID/ {print $2}' $os_release | head -n 1) } + + +# Checks if path is writable +# +# Parameters: +# =========== +# - path - the checked location +function is_path_writable() { + local path="$1" + + if [ -w "$path" ]; then + return 0 + fi + + if ! [ -f "$path" ]; then + if ! mkdir -p $(dirname "$path"); then + echo "Failed to create directory $(dirname "$path")" + return 1 + fi + + if ! touch "$path"; then + echo "Failed to create file $path" + return 1 + fi + + rm "$path" + return 0 + fi + + return 1 +} + +function skip_if_system_config_unwritable() { + if ! _is_system_config_writable; then + skip "$TOOLBOX_SYSTEM_CONFIG is unwritable" + fi +} + +function skip_if_user_config_unwritable() { + if ! _is_user_config_writable; then + skip "$TOOLBOX_USER_CONFIG is unwritable" + fi +}