Skip to content

Commit

Permalink
[WIP] test/system: Test the use of configuration files
Browse files Browse the repository at this point in the history
Follow up to:
- containers#828
- containers#851
  • Loading branch information
HarryMichal committed Jul 29, 2021
1 parent 04c673d commit 426ea7e
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
1 change: 1 addition & 0 deletions playbooks/system-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
environment:
PODMAN: '/usr/bin/podman'
TOOLBOX: '{{ toolbox_bin }}'
TOOLBOX_TESTS_SYSTEM_CONFIG: 1
args:
chdir: '{{ zuul.project.src_dir }}'
2 changes: 2 additions & 0 deletions test/system/000-setup.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/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}"
# 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
Expand Down
26 changes: 26 additions & 0 deletions test/system/101-create.bats
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ load 'libs/bats-assert/load'
load 'libs/helpers'

setup() {
setup_config_files
cleanup_containers
}

Expand Down Expand Up @@ -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
}
105 changes: 105 additions & 0 deletions test/system/libs/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
Expand All @@ -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
Expand Down Expand Up @@ -96,6 +123,41 @@ function _clean_cached_images() {
}


function _is_system_config_writable() {
is_path_writable "$SYSTEM_CONFIG"
return $?
}


function _is_user_config_writable() {
is_path_writable "$USER_CONFIG"
return $?
}


# Restores writable configuration files
function _restore_toolbox_configs() {
if _is_system_config_writable; then
mv $BACKUP_SYSTEM_CONFIG $SYSTEM_CONFIG
fi

if _is_user_config_writable; then
mv $BACKUP_USER_CONFIG $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.
Expand Down Expand Up @@ -270,3 +332,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 "$SYSTEM_CONFIG is unwritable"
fi
}

function skip_if_user_config_unwritable() {
if ! _is_user_config_writable; then
skip "$USER_CONFIG is unwritable"
fi
}

0 comments on commit 426ea7e

Please sign in to comment.