Skip to content

Commit

Permalink
fix: refactor filesystem tests (#283)
Browse files Browse the repository at this point in the history
* refactor: add test overwrites

* refactor: add is_root function

* test: refactor filesystem test to test all users
  • Loading branch information
Chumper authored Feb 6, 2022
1 parent b566793 commit 04f16d1
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 31 deletions.
9 changes: 9 additions & 0 deletions src/usr/local/buildpack/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,12 @@ ignore_tool() {
local tools=${IGNORED_TOOLS,,}
[[ $tools =~ (^|,)$TOOL_NAME($|,) ]] && echo 1 || echo 0
}

# Checks if the current caller is root or not
function is_root () {
if [[ $EUID -ne 0 ]]; then
echo 1
else
echo 0
fi
}
2 changes: 1 addition & 1 deletion src/usr/local/buildpack/utils/environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function reset_tool_env () {
function find_tool_env () {
local install_dir
install_dir=$(get_install_dir)
if [[ -z "${TOOL_NAME+x}" ]]; then
if [[ -z "${TOOL_NAME}" ]]; then
echo "No TOOL_NAME defined - skipping: ${TOOL_NAME}" >&2
exit 1;
fi
Expand Down
7 changes: 5 additions & 2 deletions src/usr/local/buildpack/utils/filesystem.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#!/bin/bash

# Constants
ROOT_DIR=/usr/local

function get_install_dir () {
if [[ $EUID -eq 0 ]]; then
echo /usr/local
if [ "$(is_root)" -eq 0 ]; then
echo "${ROOT_DIR}"
else
# shellcheck disable=SC2153
echo "${USER_HOME}"
Expand Down
82 changes: 54 additions & 28 deletions test/bash/filesystem.bats
Original file line number Diff line number Diff line change
Expand Up @@ -4,85 +4,111 @@ setup() {

TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" >/dev/null 2>&1 && pwd)"
TEST_ROOT_DIR=$(mktemp -u)
USER_HOME=$TEST_ROOT_DIR

load "$TEST_DIR/../../src/usr/local/buildpack/util.sh"

# load test overwrites
load "$TEST_DIR/util.sh"

# set directories for test
ROOT_DIR="${TEST_ROOT_DIR}/root"
USER_HOME="${TEST_ROOT_DIR}/user"

# set default test user
TEST_ROOT_USER=1000
}

teardown() {
rm -rf "${TEST_ROOT_DIR}"
}

@test "gets the default install dir" {
unset TEST_ROOT_DIR
TEST_ROOT_USER=1000 \
run get_install_dir
assert_output "${TEST_ROOT_DIR}/user"

if [[ $EUID -eq 0 ]]; then
assert_output "/opt/buildpack"
else
assert_output "${USER_HOME}"
fi
TEST_ROOT_USER=0 \
run get_install_dir
assert_output "${TEST_ROOT_DIR}/root"
}

@test "can create a versioned tool path as user" {
local install_dir=$(get_install_dir)

local TOOL_NAME=foo
local TOOL_VERSION=1.2.3

local TEST_ROOT_USER=1000
run create_versioned_tool_path

assert_output "${TEST_ROOT_DIR}/user/foo/1.2.3"
assert [ -d "${TEST_ROOT_DIR}/user/foo/1.2.3" ]
}

@test "can create a versioned tool path as root" {
local TEST_ROOT_USER=0

local TOOL_NAME=foo
local TOOL_VERSION=1.2.3

run setup_directories
run create_versioned_tool_path

assert_output "${install_dir}/foo/1.2.3"
assert [ -d "${install_dir}/foo/1.2.3" ]
assert_output "${TEST_ROOT_DIR}/root/foo/1.2.3"
assert [ -d "${TEST_ROOT_DIR}/root/foo/1.2.3" ]
}

@test "finds the versioned tool path" {
local install_dir=$(get_install_dir)

local TOOL_NAME=foo
local TOOL_VERSION=1.2.3

run find_versioned_tool_path
assert_output ""

# user
run create_versioned_tool_path
run find_versioned_tool_path
assert_output "${TEST_ROOT_DIR}/user/foo/1.2.3"

# root
local TEST_ROOT_USER=0
run create_versioned_tool_path
run find_versioned_tool_path
assert_output "${install_dir}/foo/1.2.3"
assert_output "${TEST_ROOT_DIR}/root/foo/1.2.3"

}

@test "finds the tool path" {
local install_dir=$(get_install_dir)

local TOOL_NAME=foo
local TOOL_VERSION=1.2.3

run setup_directories
run find_tool_path
assert_success
assert_output ""

mkdir -p "${install_dir}/foo"
# user
mkdir -p "${TEST_ROOT_DIR}/user/foo"
run find_tool_path
assert_success
assert_output "${install_dir}/foo"
assert_output "${TEST_ROOT_DIR}/user/foo"

# root
local TEST_ROOT_USER=0
mkdir -p "${TEST_ROOT_DIR}/root/foo"
run find_tool_path
assert_success
assert_output "${TEST_ROOT_DIR}/root/foo"
}

@test "finds the tool env path" {
local install_dir=$(get_install_dir)

local TOOL_NAME=foo
local TOOL_VERSION=1.2.3

run setup_directories
# user
run find_tool_env
assert_output "${TEST_ROOT_DIR}/user/env.d/foo.sh"

# root
local TEST_ROOT_USER=0
run find_tool_env
assert_output "${install_dir}/env.d/foo.sh"
assert_output "${TEST_ROOT_DIR}/root/env.d/foo.sh"

# TODO(Chumper): This should fail
TOOL_NAME= run find_tool_env
# assert_failure
assert_success
assert_failure
}
13 changes: 13 additions & 0 deletions test/bash/util.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# Will overwrite certain util functions to make them testable

# Overwrite is_root function to check a test root user
# instead of the effective caller
function is_root () {
if [[ $TEST_ROOT_USER -ne 0 ]]; then
echo 1
else
echo 0
fi
}

0 comments on commit 04f16d1

Please sign in to comment.