Skip to content

Commit

Permalink
refactor: execute NBS docker install and buildx create related logic …
Browse files Browse the repository at this point in the history
…instead of re-implementing them. Add bats test
  • Loading branch information
RedLeader962 committed Dec 14, 2023
1 parent ef58437 commit bcef968
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,17 @@
# usage:
# $ bash ./lpm_utility_script/lpm_create_multiarch_docker_builder.bash
#

function lpm::create_multiarch_docker_builder() {
local TMP_CWD
TMP_CWD=$(pwd)

# ....Project root logic.........................................................................
LPM_PATH=$(git rev-parse --show-toplevel)

# ....Load environment variables from file.......................................................
cd "${LPM_PATH}/build_system" || exit
set -o allexport
source .env
set +o allexport

# ....Helper function............................................................................
# import shell functions from utilities library
N2ST_PATH=${N2ST_PATH:-"${LPM_PATH}/build_system/utilities/norlab-shell-script-tools"}
source "${N2ST_PATH}/import_norlab_shell_script_tools_lib.bash"

# ====Begin======================================================================================
print_formated_script_header 'lpm_create_multiarch_docker_builder.bash'

# ...............................................................................................
echo
print_msg "Create a multi-architecture docker builder"
echo

docker buildx create \
--name local-builder-multiarch-virtual \
--driver docker-container \
--platform linux/amd64,linux/arm64 \
--bootstrap \
--use

docker buildx ls
# ====Begin=====================================================================================
cd "${LPM_PATH}"/build_system/utilities/norlab-build-system/install_scripts/ \
&& bash nbs_create_multiarch_docker_builder.bash

print_formated_script_footer 'lpm_create_multiarch_docker_builder.bash' "${MSG_LINE_CHAR_UTIL}"
# ====Teardown===================================================================================
cd "${TMP_CWD}"
}
Expand Down
16 changes: 3 additions & 13 deletions build_system/lpm_utility_script/lpm_install_docker_tools.bash
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,9 @@ function lpm::install_docker_tools() {
# ....Project root logic.........................................................................
LPM_PATH=$(git rev-parse --show-toplevel)

# ....Load environment variables from file.......................................................
cd "${LPM_PATH}/build_system" || exit
set -o allexport
source .env
set +o allexport

# ....Helper function............................................................................
# import shell functions from utilities library
N2ST_PATH=${N2ST_PATH:-"${LPM_PATH}/build_system/utilities/norlab-shell-script-tools"}
source "${N2ST_PATH}/import_norlab_shell_script_tools_lib.bash"

# # ====Begin=====================================================================================
cd "${N2ST_PATH}"/src/utility_scripts/ && bash install_docker_tools.bash
# ====Begin=====================================================================================
cd "${LPM_PATH}"/build_system/utilities/norlab-build-system/install_scripts/ \
&& bash nbs_install_docker_tools.bash

# ====Teardown===================================================================================
cd "${TMP_CWD}"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env bats
#
# Usage in docker container
# $ REPO_ROOT=$(pwd) && RUN_TESTS_IN_DIR='tests'
# $ docker run -it --rm -v "$REPO_ROOT:/code" bats/bats:latest "$RUN_TESTS_IN_DIR"
#
# Note: "/code" is the working directory in the bats official image
#
# bats-core ref:
# - https://bats-core.readthedocs.io/en/stable/tutorial.html
# - https://bats-core.readthedocs.io/en/stable/writing-tests.html
# - https://opensource.com/article/19/2/testing-bash-bats
# ↳ https://github.com/dmlond/how_to_bats/blob/master/test/build.bats
#
# Helper library:
# - https://github.com/bats-core/bats-assert
# - https://github.com/bats-core/bats-support
# - https://github.com/bats-core/bats-file
#

BATS_HELPER_PATH=/usr/lib/bats
if [[ -d ${BATS_HELPER_PATH} ]]; then
load "${BATS_HELPER_PATH}/bats-support/load"
load "${BATS_HELPER_PATH}/bats-assert/load"
load "${BATS_HELPER_PATH}/bats-file/load"
load "${SRC_CODE_PATH}/${N2ST_BATS_TESTING_TOOLS_RELATIVE_PATH}/bats_helper_functions"
#load "${BATS_HELPER_PATH}/bats-detik/load" # << Kubernetes support
else
echo -e "\n[\033[1;31mERROR\033[0m] $0 path to bats-core helper library unreachable at \"${BATS_HELPER_PATH}\"!"
echo '(press any key to exit)'
read -r -n 1
exit 1
fi

# ====Setup========================================================================================
TESTED_FILE="lpm_install_docker_tools.bash"
TESTED_FILE2="lpm_create_multiarch_docker_builder.bash"
TESTED_FILE_PATH="./build_system/lpm_utility_script"

# executed once before starting the first test (valide for all test in that file)
setup_file() {
BATS_DOCKER_WORKDIR=$(pwd) && export BATS_DOCKER_WORKDIR

## Uncomment the following for debug, the ">&3" is for printing bats msg to stdin
# pwd >&3 && tree -L 1 -a -hug >&3
# printenv >&3
}

# executed before each test
setup() {
cd "$TESTED_FILE_PATH" || exit
}

# ====Teardown=====================================================================================

# executed after each test
teardown() {
bats_print_run_env_variable_on_error
}

## executed once after finishing the last test (valide for all test in that file)
#teardown_file() {
#}

# ====Test casses==================================================================================

@test "run $TESTED_FILE from root › expect pass" {
cd "${BATS_DOCKER_WORKDIR}"

run bash "./${TESTED_FILE_PATH}/$TESTED_FILE"
assert_success
run docker --version
assert_output --regexp "Docker version".*"build".*
}

@test "run $TESTED_FILE2 (mock 'docker buildx create' step) › expect pass" {
cd "${BATS_DOCKER_WORKDIR}"

mock_docker_command_exit_ok

run source "./${TESTED_FILE_PATH}/$TESTED_FILE2"
assert_success
}

0 comments on commit bcef968

Please sign in to comment.