Skip to content

Commit

Permalink
fix: improve script robustness and logging in several utility scripts
Browse files Browse the repository at this point in the history
- NMO-571 fix: unable to find image failure on build server
- NMO-550 fix: Dockerfile.bats-core-code-isolation formating warning
- NMO-559 ci(N2ST): improve IS_TEAMCITY_RUN env var logic
- NMO-560 refactor: replace tmp cwd logic with pushd/popd
- NMO-570 ci: add n2st specific image label
  • Loading branch information
RedLeader962 committed Aug 6, 2024
1 parent 569509b commit bad76e4
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 24 deletions.
5 changes: 2 additions & 3 deletions import_norlab_shell_script_tools_lib.bash
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ MSG_ERROR_FORMAT="\033[1;31m"
MSG_END_FORMAT="\033[0m"

function n2st::source_lib(){
local TMP_CWD
TMP_CWD=$(pwd)
pushd "$(pwd)" >/dev/null || exit 1

# Note: can handle both sourcing cases
# i.e. from within a script or from an interactive terminal session
Expand All @@ -46,7 +45,7 @@ function n2st::source_lib(){
# PATH=$PATH:${N2ST_ROOT_DIR}/src/utility_scripts

# ....Teardown...................................................................................
cd "${TMP_CWD}"
popd >/dev/null || exit 1
}

# ::::Main:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Expand Down
7 changes: 5 additions & 2 deletions src/function_library/teamcity_utilities.bash
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ source ./prompt_utilities.bash

# =================================================================================================
# Check if the script is executed in JetBrains TeamCity continuous integration/deployment server
# and set the IS_TEAMCITY_RUN environment variable accordingly
# and set the IS_TEAMCITY_RUN environment variable accordingly, 'true' or 'false'
#
# Usage:
# $ n2st::set_is_teamcity_run_environment_variable
Expand All @@ -41,8 +41,11 @@ source ./prompt_utilities.bash
# =================================================================================================
function n2st::set_is_teamcity_run_environment_variable() {
if [[ ${TEAMCITY_VERSION} ]] ; then
IS_TEAMCITY_RUN=true && export IS_TEAMCITY_RUN
IS_TEAMCITY_RUN=true
else
IS_TEAMCITY_RUN=false
fi
export IS_TEAMCITY_RUN
}


Expand Down
14 changes: 10 additions & 4 deletions src/utility_scripts/install_docker_tools.bash
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# $ bash ./install_docker_tools.bash
#
# =================================================================================================
pushd "$(pwd)" >/dev/null || exit 1


# ....Pre-condition................................................................................
if [[ "$(basename "$(pwd)")" != "utility_scripts" ]]; then
Expand All @@ -20,12 +22,14 @@ if [[ "$(basename "$(pwd)")" != "utility_scripts" ]]; then
fi

# ....Load helper function.........................................................................
TMP_CWD=$(pwd)
pushd "$(pwd)" >/dev/null || exit 1
source ../../.env.n2st
cd ../function_library || exit 1
source ./prompt_utilities.bash
source ./docker_utilities.bash
cd "${TMP_CWD}"
popd >/dev/null || exit 1



# ====Begin========================================================================================
n2st::print_formated_script_header 'install_docker_tools.bash'
Expand Down Expand Up @@ -75,6 +79,8 @@ echo

n2st::add_user_to_the_docker_group "$(whoami)"

n2st::print_formated_script_footer 'install_docker_tools.bash'
# ====Teardown=====================================================================================
cd "${TMP_CWD}" || exit 1
n2st::print_formated_script_footer 'install_docker_tools.bash'
popd >/dev/null || exit 1


7 changes: 3 additions & 4 deletions src/utility_scripts/which_python_version.bash
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# write 'PYTHON3_VERSION'
#
# =================================================================================================

pushd "$(pwd)" >/dev/null || exit 1

# ....Pre-condition................................................................................
if [[ "$(basename "$(pwd)")" != "utility_scripts" ]]; then
Expand All @@ -24,7 +24,6 @@ if [[ "$(basename "$(pwd)")" != "utility_scripts" ]]; then
fi

# ....Load helper function.........................................................................
TMP_CWD=$(pwd)

# (Priority) ToDo: validate!! (ref task NMO-388 fix: explicitly sourcing .env.n2st cause conflicting problem when the repo is used as a lib)
if [[ -z $PROJECT_PROMPT_NAME ]] && [[ -z $PROJECT_GIT_NAME ]] ; then
Expand All @@ -36,8 +35,8 @@ fi
cd ../function_library || exit 1
source ./general_utilities.bash

cd "${TMP_CWD}"

# ====Begin========================================================================================
n2st::set_which_python3_version

# ====Teardown=====================================================================================
popd >/dev/null || exit 1
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ ENV N2ST_BATS_TESTING_TOOLS_RELATIVE_PATH=${N2ST_BATS_TESTING_TOOLS_RELATIVE_PAT
ARG TEAMCITY_VERSION
ENV TEAMCITY_VERSION=${TEAMCITY_VERSION}

# (Priority) ToDo: implement >> next bloc ↓↓ (ref task NMO-570)
ARG N2ST_VERSION
LABEL norlab.tools.norlab-shell-script-tools.tester="${N2ST_VERSION:?err}"
LABEL org.opencontainers.image.authors="luc.coupal.1@ulaval.ca"

SHELL ["/bin/bash", "-e", "-c"]
ARG DEBIAN_FRONTEND=noninteractive

ENV TERM=${TERM:-"xterm-256color"}
ENV COLUMNS=80

# Note:
# Image purpose › source code isolation for CI build
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# Image purpose › source code isolation for CI build
# This container use a copy project source code strategy instead of using the mounting volume

FROM bats/bats:latest as bats-core-base-img
FROM bats/bats:latest AS bats-core-base-img

FROM ubuntu:latest as final
FROM ubuntu:latest AS final

ARG CONTAINER_PROJECT_ROOT_NAME
ENV CONTAINER_PROJECT_ROOT_NAME=${CONTAINER_PROJECT_ROOT_NAME:?'Build argument needs to be set and non-empty.'}
Expand All @@ -16,13 +16,14 @@ ENV N2ST_BATS_TESTING_TOOLS_RELATIVE_PATH=${N2ST_BATS_TESTING_TOOLS_RELATIVE_PAT
ARG TEAMCITY_VERSION
ENV TEAMCITY_VERSION=${TEAMCITY_VERSION}

ARG N2ST_VERSION
LABEL norlab.tools.norlab-shell-script-tools.tester="${N2ST_VERSION:?err}"
LABEL org.opencontainers.image.authors="luc.coupal.1@ulaval.ca"

SHELL ["/bin/bash", "-e", "-c"]
ARG DEBIAN_FRONTEND=noninteractive

ENV TERM=${TERM:-"xterm-256color"}
#RUN tput -T $TERM init
ENV COLUMNS=80

# ====Begin========================================================================================
Expand Down
25 changes: 20 additions & 5 deletions tests/bats_testing_tools/run_bats_tests_in_docker.bash
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,17 @@ PROJECT_GIT_REMOTE_URL=$(git remote get-url origin)
PROJECT_GIT_NAME=$(basename "${PROJECT_GIT_REMOTE_URL}" .git)
REPO_ROOT=$(pwd)
N2ST_BATS_TESTING_TOOLS_ABS_PATH="$( cd "$( dirname "${0}" )" &> /dev/null && pwd )"
N2ST_BATS_TESTING_TOOLS_RELATIVE_PATH=".${N2ST_BATS_TESTING_TOOLS_ABS_PATH/$REPO_ROOT/}"

# ToDo: assessment › harccoding the relative path is more robust. Since the location wont change anymore, the version with string substitution is irelevant.
#N2ST_BATS_TESTING_TOOLS_RELATIVE_PATH=".${N2ST_BATS_TESTING_TOOLS_ABS_PATH/$REPO_ROOT/}"
N2ST_BATS_TESTING_TOOLS_RELATIVE_PATH="tests/bats_testing_tools"

N2ST_PATH=$( git rev-parse --show-toplevel )
#N2ST_PATH="${N2ST_BATS_TESTING_TOOLS_RELATIVE_PATH}/../.."
test -d "${N2ST_PATH}" || exit 1
#tree -a -L 1 ${N2ST_PATH}
N2ST_VERSION="$(cat "${N2ST_PATH}"/version.txt)"


if [[ $(basename "$REPO_ROOT") != ${PROJECT_CLONE_GIT_NAME} ]]; then
echo -e "\n[\033[1;31mERROR\033[0m] $0 must be executed from the project root!\nCurrent wordir: $(pwd)" 1>&2
Expand All @@ -38,7 +48,8 @@ if [[ $(basename "$REPO_ROOT") != ${PROJECT_CLONE_GIT_NAME} ]]; then
exit 1
fi


test -d "${N2ST_BATS_TESTING_TOOLS_ABS_PATH}" || exit 1
test -f "${N2ST_BATS_TESTING_TOOLS_RELATIVE_PATH}/bats_helper_functions.bash" || exit 1

# Do not load MSG_BASE nor MSG_BASE_TEAMCITY from there .env file so that tested logic does not leak in that file
_MSG_BASE="\033[1m[${PROJECT_GIT_NAME}]\033[0m"
Expand All @@ -61,11 +72,15 @@ docker build \
--build-arg BUILDKIT_CONTEXT_KEEP_GIT_DIR=1 \
--build-arg N2ST_BATS_TESTING_TOOLS_RELATIVE_PATH="$N2ST_BATS_TESTING_TOOLS_RELATIVE_PATH" \
--build-arg "TEAMCITY_VERSION=${TEAMCITY_VERSION}" \
--build-arg "N2ST_VERSION=${N2ST_VERSION:?err}" \
--file "${N2ST_BATS_TESTING_TOOLS_ABS_PATH}/Dockerfile.bats-core-code-isolation.${BATS_DOCKERFILE_DISTRO}" \
--tag n2st-bats-test-code-isolation/"${PROJECT_GIT_NAME}" \
--platform "linux/$(uname -m)" \
--load \
.
"${REPO_ROOT}"

# done: NMO-571 fix: unable to find image faillure on build server › Cause: image were build using docker-container builder because of the --platform flag
# (NICE TO HAVE) ToDo: assessment >> Not sure its relevant to have multiarch build logic since test are executed at runtime not build time.
# --platform "linux/$(uname -m)" \
# --load \

if [[ ${TEAMCITY_VERSION} ]]; then
echo -e "##teamcity[blockClosed name='${_MSG_BASE_TEAMCITY} Build custom bats-core docker image']"
Expand Down
1 change: 1 addition & 0 deletions tests/test_dotenv_files.bats
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ function source_dotenv_project() {
cd ..
assert_equal "$(pwd)" "/code"


git clone "https://github.com/norlab-ulaval/${SUPERPROJECT_NAME}.git"
assert_dir_exist "${SUPERPROJECT_NAME}"

Expand Down
12 changes: 12 additions & 0 deletions tests/test_teamcity_utilities.bats
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ teardown() {
}

@test "n2st::set_is_teamcity_run_environment_variable ok" {

# Case › not run in TeamCity
run n2st::set_is_teamcity_run_environment_variable
assert_success
n2st::set_is_teamcity_run_environment_variable
assert_not_empty "$IS_TEAMCITY_RUN"

# Reset state
unset IS_TEAMCITY_RUN
assert_empty "$IS_TEAMCITY_RUN"

# Case › run in TeamCity
fake_IS_TEAMCITY_RUN
run n2st::set_is_teamcity_run_environment_variable
assert_success
Expand Down
5 changes: 2 additions & 3 deletions tests/tests_template/run_bats_core_test_in_n2st.bash
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ fi


function n2st::run_n2st_testsing_tools(){
local TMP_CWD
TMP_CWD=$(pwd)
pushd "$(pwd)" >/dev/null || exit 1

# ....Project root logic.........................................................................
SUPERPROJECT_PATH=$(git rev-parse --show-toplevel)
Expand All @@ -43,7 +42,7 @@ function n2st::run_n2st_testsing_tools(){
bash "${N2ST_PATH}/tests/bats_testing_tools/run_bats_tests_in_docker.bash" $PARAMS

# ....Teardown...................................................................................
cd "$TMP_CWD"
popd >/dev/null || exit 1
}

n2st::run_n2st_testsing_tools
Expand Down

0 comments on commit bad76e4

Please sign in to comment.