From c48276382b16e323392559ae8cbd2cb92fbd478b Mon Sep 17 00:00:00 2001 From: Victor M Date: Wed, 18 Oct 2023 22:22:43 +0200 Subject: [PATCH 01/15] add loader module --- src/bootstrap.sh | 46 ++++--- src/load_module.sh | 10 ++ src/main.sh | 64 ---------- src/shared/common.sh | 33 +++++ src/shared/common_lib.sh | 42 ------- src/shared/{container_lib.sh => container.sh} | 27 ++--- ...{image_builder_lib.sh => image_builder.sh} | 113 +++++++++--------- src/shared/loader.sh | 59 +++++++++ src/shared/log.sh | 27 +++++ test/load_module.bats | 68 +++++++++++ test/main.bats | 69 ----------- 11 files changed, 289 insertions(+), 269 deletions(-) create mode 100644 src/load_module.sh delete mode 100644 src/main.sh create mode 100644 src/shared/common.sh delete mode 100644 src/shared/common_lib.sh rename src/shared/{container_lib.sh => container.sh} (70%) rename src/shared/{image_builder_lib.sh => image_builder.sh} (61%) create mode 100644 src/shared/loader.sh create mode 100644 src/shared/log.sh create mode 100644 test/load_module.bats delete mode 100644 test/main.bats diff --git a/src/bootstrap.sh b/src/bootstrap.sh index d1d1d3dc..d03361aa 100644 --- a/src/bootstrap.sh +++ b/src/bootstrap.sh @@ -1,44 +1,42 @@ #!/usr/bin/env bash -CICD_TOOLS_REPO_ORG="${CICD_TOOLS_REPO_ORG:-RedHatInsights}" -CICD_TOOLS_REPO_BRANCH="${CICD_TOOLS_REPO_BRANCH:-main}" -CICD_TOOLS_ROOTDIR="${CICD_TOOLS_ROOTDIR:-.cicd_tools}" -CICD_TOOLS_SCRIPTS_DIR="${CICD_TOOLS_ROOTDIR}/src" -CICD_TOOLS_SKIP_CLEANUP=${CICD_TOOLS_SKIP_CLEANUP:-} +CICD_BOOTSTRAP_REPO_ORG="${CICD_BOOTSTRAP_REPO_ORG:-RedHatInsights}" +CICD_BOOTSTRAP_REPO_BRANCH="${CICD_BOOTSTRAP_REPO_BRANCH:-main}" +CICD_BOOTSTRAP_ROOTDIR="${CICD_BOOTSTRAP_ROOTDIR:-.cicd_tools}" +CICD_BOOTSTRAP_SKIP_CLEANUP=${CICD_BOOTSTRAP_SKIP_CLEANUP:-} -clone_cicd_tools_repo() { +cicd::bootstrap::clone_cicd_tools_repo() { - if [ -d "${CICD_TOOLS_ROOTDIR}" ]; then - _delete_cicd_tools_rootdir + if [ -d "${CICD_BOOTSTRAP_ROOTDIR}" ]; then + cicd::bootstrap::_delete_rootdir fi git clone -q \ - --branch "$CICD_TOOLS_REPO_BRANCH" \ - "https://github.com/${CICD_TOOLS_REPO_ORG}/cicd-tools.git" "$CICD_TOOLS_ROOTDIR" + --branch "$CICD_BOOTSTRAP_REPO_BRANCH" \ + "https://github.com/${CICD_BOOTSTRAP_REPO_ORG}/cicd-tools.git" "$CICD_BOOTSTRAP_ROOTDIR" } -_delete_cicd_tools_rootdir() { - cicd::debug "Removing existing CICD tools directory: '${CICD_TOOLS_ROOTDIR}'" - rm -rf "${CICD_TOOLS_ROOTDIR}" +cicd::bootstrap::_delete_rootdir() { + cicd::debug "Removing existing CICD tools directory: '${CICD_BOOTSTRAP_ROOTDIR}'" + rm -rf "${CICD_BOOTSTRAP_ROOTDIR}" } -cleanup() { - _delete_cicd_tools_rootdir - unset clone_cicd_tools_repo _delete_cicd_tools_rootdir cleanup +cicd::bootstrap::cleanup() { + cicd::bootstrap::_delete_rootdir + unset cicd::bootstrap::clone_cicd_tools_repo cicd::bootstrap::_delete_rootdir cicd::bootstrap::cleanup + unset CICD_BOOTSTRAP_REPO_ORG CICD_BOOTSTRAP_REPO_BRANCH CICD_BOOTSTRAP_ROOTDIR CICD_BOOTSTRAP_SKIP_CLEANUP } -if [ -z "$CICD_TOOLS_SKIP_GIT_CLONE" ]; then - if ! clone_cicd_tools_repo; then +if [ -z "$CICD_BOOTSTRAP_SKIP_GIT_CLONE" ]; then + if ! cicd::bootstrap::clone_cicd_tools_repo; then echo "couldn't clone cicd-tools repository!" exit 1 fi fi # shellcheck source=src/main.sh -source "$CICD_TOOLS_SCRIPTS_DIR/main.sh" "$@" || exit 1 -if [ -z "$CICD_TOOLS_SKIP_CLEANUP" ]; then - if ! cleanup; then - echo "couldn't perform cicd tools cleanup!" - exit 1 - fi +source "$CICD_BOOTSTRAP_ROOTDIR/src/load_module.sh" "$@" || exit 1 +if [[ -z "$CICD_BOOTSTRAP_SKIP_CLEANUP" ]] && ! cicd::bootstrap::cleanup; then + echo "couldn't perform cicd tools cleanup!" + exit 1 fi diff --git a/src/load_module.sh b/src/load_module.sh new file mode 100644 index 00000000..9347844e --- /dev/null +++ b/src/load_module.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +# https://stackoverflow.com/a/246128 +readonly CICD_LOADER_SCRIPTS_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)" +if ! source "${CICD_LOADER_SCRIPTS_DIR}/shared/loader.sh"; then + echo "Error loading 'loader' module!" + exit 1 +fi + +cicd::loader::load_module "$1" diff --git a/src/main.sh b/src/main.sh deleted file mode 100644 index bad23c04..00000000 --- a/src/main.sh +++ /dev/null @@ -1,64 +0,0 @@ -#!/usr/bin/env bash - -CICD_TOOLS_COMMON_LIB_LOADED=${CICD_TOOLS_COMMON_LIB_LOADED:-1} -CICD_TOOLS_CONTAINER_LIB_LOADED=${CICD_TOOLS_CONTAINER_LIB_LOADED:-1} -CICD_TOOLS_DEBUG="${CICD_TOOLS_DEBUG:-}" -# https://stackoverflow.com/a/246128 -SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)" -CICD_TOOLS_SCRIPTS_DIR="${CICD_TOOLS_SCRIPTS_DIR:-$SCRIPT_DIR}" -LIB_TO_LOAD=${1:-container} - -cicd::debug() { - if cicd::_debug_mode; then - cicd::log "$*" - fi -} - -cicd::_debug_mode() { - [[ -n "$CICD_TOOLS_DEBUG" ]] -} - -cicd::log() { - echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" -} - -cicd::err() { - echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 -} - -cicd::load_library() { - - case $LIB_TO_LOAD in - all) cicd::_load_all ;; - common) cicd::_load_common_lib ;; - container) cicd::_load_container_lib ;; - image_builder) cicd::_load_image_builder_lib ;; - *) cicd::err "Unsupported library: '$LIB_TO_LOAD'" && return 1 ;; - esac -} - -cicd::_load_all() { - cicd::_load_common_lib - cicd::_load_container_lib - cicd::_load_image_builder_lib -} - -cicd::_load_common_lib() { - # shellcheck source=src/shared/common_lib.sh - source "${CICD_TOOLS_SCRIPTS_DIR}/shared/common_lib.sh" -} - -cicd::_load_container_lib() { - cicd::_load_common_lib - # shellcheck source=src/shared/container_lib.sh - source "${CICD_TOOLS_SCRIPTS_DIR}/shared/container_lib.sh" -} - -cicd::_load_image_builder_lib() { - cicd::_load_common_lib - cicd::_load_container_lib - # shellcheck source=src/shared/image_builder_lib.sh - source "${CICD_TOOLS_SCRIPTS_DIR}/shared/image_builder_lib.sh" -} - -cicd::load_library "$LIB_TO_LOAD" diff --git a/src/shared/common.sh b/src/shared/common.sh new file mode 100644 index 00000000..406bd696 --- /dev/null +++ b/src/shared/common.sh @@ -0,0 +1,33 @@ +# Common functions that are shared across the different modules + +if [[ -n "$CICD_COMMON_MODULE_LOADED" ]]; then + cicd::log::debug "common module already loaded, skipping" + return 0 +fi + +if [[ -z "$CICD_LOADER_MODULE_LOADED" ]]; then + echo "loader module not found, please use $(load_module.sh) to load modules." + return 1 +fi + +cicd::log::debug "loading common module" + +cicd::common::command_is_present() { + command -v "$1" >/dev/null 2>&1 +} + +cicd::common::get_7_chars_commit_hash() { + cicd::common::_get_n_chars_commit_hash 7 +} + +cicd::common::_get_n_chars_commit_hash() { + git rev-parse --short="$1" HEAD +} + +cicd::common::is_ci_context() { + [[ "$CI" = "true" ]] +} + +cicd::log::debug "common module loaded" + +CICD_COMMON_MODULE_LOADED='true' diff --git a/src/shared/common_lib.sh b/src/shared/common_lib.sh deleted file mode 100644 index cd494788..00000000 --- a/src/shared/common_lib.sh +++ /dev/null @@ -1,42 +0,0 @@ -#!/usr/bin/env bash - -# Common functions that are shared across the different libraries - -CICD_TOOLS_COMMON_LOADED=${CICD_TOOLS_COMMON_LOADED:-1} -LOCAL_BUILD=${LOCAL_BUILD:-false} - -if [ "$CICD_TOOLS_COMMON_LOADED" -eq 0 ]; then - cicd::debug "common library already loaded, skipping" - return 0 -fi - -if [ -z "$CICD_TOOLS_SCRIPTS_DIR" ]; then - echo "scripts directory not defined, please load through main.sh script" >&2 - return 1 -fi - -cicd::debug "loading common lib" - -cicd::common::command_is_present() { - command -v "$1" > /dev/null 2>&1 -} - -cicd::common::_get_n_chars_commit_hash() { - git rev-parse --short="$1" HEAD -} - -cicd::common::get_7_chars_commit_hash() { - cicd::common::_get_n_chars_commit_hash 7 -} - -cicd::common::is_ci_context() { - [[ "$CI" = "true" ]] -} - -cicd::common::local_build() { - [[ "$LOCAL_BUILD" = true ]] || ! cicd::common::is_ci_context -} - -cicd::debug "common lib loaded" - -CICD_TOOLS_COMMON_LOADED=0 diff --git a/src/shared/container_lib.sh b/src/shared/container.sh similarity index 70% rename from src/shared/container_lib.sh rename to src/shared/container.sh index 9e6388fe..7dd4577f 100644 --- a/src/shared/container_lib.sh +++ b/src/shared/container.sh @@ -1,20 +1,16 @@ -#!/usr/bin/env bash - # container engine helper functions to handle both podman and docker commands -CICD_TOOLS_CONTAINER_ENGINE_LOADED=${CICD_TOOLS_CONTAINER_ENGINE_LOADED:-1} - -if [[ "$CICD_TOOLS_CONTAINER_ENGINE_LOADED" -eq 0 ]]; then - cicd::debug "container engine library already loaded, skipping" +if [[ -n "$CICD_CONTAINER_MODULE_LOADED" ]]; then + cicd::log::debug "container engine module already loaded, skipping" return 0 fi -if [ -z "$CICD_TOOLS_SCRIPTS_DIR" ]; then - echo "scripts directory not defined, please load through main.sh script" >&2 +if [[ -z "$CICD_LOADER_MODULE_LOADED" ]]; then + echo "loader module not found, please use $(load_module.sh) to load modules." return 1 fi -cicd::debug "loading container lib" +cicd::log::debug "loading container module" CONTAINER_ENGINE_CMD='' PREFER_CONTAINER_ENGINE=${PREFER_CONTAINER_ENGINE:-} @@ -40,12 +36,12 @@ cicd::container::_set_container_engine_cmd() { elif cicd::container::_container_engine_available 'docker'; then CONTAINER_ENGINE_CMD='docker' else - cicd::err "ERROR, no container engine found, please install either podman or docker first" + cicd::log::err "ERROR, no container engine found, please install either podman or docker first" return 1 fi fi - cicd::debug "Container engine selected: $CONTAINER_ENGINE_CMD" + cicd::log::debug "Container engine selected: $CONTAINER_ENGINE_CMD" } cicd::container::_preferred_container_engine_available() { @@ -56,7 +52,7 @@ cicd::container::_preferred_container_engine_available() { if cicd::container::_container_engine_available "$PREFER_CONTAINER_ENGINE"; then CONTAINER_ENGINE_AVAILABLE=0 else - cicd::log "WARNING: preferred container engine '${PREFER_CONTAINER_ENGINE}' not present, or isn't supported, finding alternative..." + cicd::log::info "WARNING: preferred container engine '${PREFER_CONTAINER_ENGINE}' not present, or isn't supported, finding alternative..." fi fi @@ -82,7 +78,7 @@ cicd::container::_cmd_exists_and_is_supported() { if cicd::container::_supported_container_engine "$cmd" && cicd::common::command_is_present "$cmd"; then if [[ "$cmd" == 'docker' ]] && cicd::container::_docker_seems_emulated; then - cicd::log "WARNING: docker seems emulated, skipping." + cicd::log::info "WARNING: docker seems emulated, skipping." result=1 fi else @@ -101,11 +97,12 @@ cicd::container::_supported_container_engine() { } cicd::container::_docker_seems_emulated() { - [[ "$(docker 2> /dev/null --version)" =~ podman\ +version ]] + [[ "$(docker 2>/dev/null --version)" =~ podman\ +version ]] } cicd::container::_podman_version_under_4_5_0() { [ "$(echo -en "4.5.0\n$(_podman_version)" | sort -V | head -1)" != "4.5.0" ] } -CICD_TOOLS_CONTAINER_ENGINE_LOADED=0 +cicd::log::debug "container module loaded" +CICD_CONTAINER_MODULE_LOADED='true' diff --git a/src/shared/image_builder_lib.sh b/src/shared/image_builder.sh similarity index 61% rename from src/shared/image_builder_lib.sh rename to src/shared/image_builder.sh index 67a05b06..cb9255b7 100644 --- a/src/shared/image_builder_lib.sh +++ b/src/shared/image_builder.sh @@ -1,31 +1,35 @@ -#!/usr/bin/env bash +# helper functions to build container images -CICD_TOOLS_IMAGE_BUILDER_LOADED=${CICD_TOOLS_IMAGE_BUILDER_LOADED:-1} - -if [[ "$CICD_TOOLS_IMAGE_BUILDER_LOADED" -eq 0 ]]; then +if [[ -n "$CICD_IMAGE_BUILDER_MODULE_LOADED" ]]; then + cicd::log::debug "image builder module already loaded, skipping" return 0 fi -if [ -z "$CICD_TOOLS_SCRIPTS_DIR" ]; then - echo "scripts directory not defined, please load through main.sh script" >&2 +if [[ -z "$CICD_LOADER_MODULE_LOADED" ]]; then + echo "loader module not found, please use $(load_module.sh) to load modules." return 1 fi -cicd::debug "loading image builder library" - -readonly CICD_TOOLS_IMAGE_BUILDER_REDHAT_REGISTRY='registry.redhat.io' -readonly CICD_TOOLS_IMAGE_BUILDER_QUAY_REGISTRY='quay.io' -readonly CICD_TOOLS_IMAGE_BUILDER_QUAY_EXPIRE_TIME=${CICD_TOOLS_IMAGE_BUILDER_QUAY_EXPIRE_TIME:-3d} -readonly CICD_TOOLS_IMAGE_BUILDER_QUAY_USER="${CICD_TOOLS_IMAGE_BUILDER_QUAY_USER:-$QUAY_USER}" -readonly CICD_TOOLS_IMAGE_BUILDER_QUAY_PASSWORD="${CICD_TOOLS_IMAGE_BUILDER_QUAY_PASSWORD:-$QUAY_TOKEN}" -readonly CICD_TOOLS_IMAGE_BUILDER_REDHAT_USER="${CICD_TOOLS_IMAGE_BUILDER_REDHAT_USER:-$RH_REGISTRY_USER}" -readonly CICD_TOOLS_IMAGE_BUILDER_REDHAT_PASSWORD="${CICD_TOOLS_IMAGE_BUILDER_REDHAT_PASSWORD:-$RH_REGISTRY_TOKEN}" -readonly CICD_TOOLS_IMAGE_BUILDER_DEFAULT_BUILD_CONTEXT='.' -readonly CICD_TOOLS_IMAGE_BUILDER_DEFAULT_CONTAINERFILE_PATH='Dockerfile' +cicd::log::debug "loading image builder module" + +readonly CICD_IMAGE_BUILDER_LOCAL_BUILD=${CICD_IMAGE_BUILDER_LOCAL_BUILD:-$LOCAL_BUILD} +readonly CICD_IMAGE_BUILDER_REDHAT_REGISTRY='registry.redhat.io' +readonly CICD_IMAGE_BUILDER_QUAY_REGISTRY='quay.io' +readonly CICD_IMAGE_BUILDER_QUAY_EXPIRE_TIME=${CICD_IMAGE_BUILDER_QUAY_EXPIRE_TIME:-3d} +readonly CICD_IMAGE_BUILDER_QUAY_USER="${CICD_IMAGE_BUILDER_QUAY_USER:-$QUAY_USER}" +readonly CICD_IMAGE_BUILDER_QUAY_PASSWORD="${CICD_IMAGE_BUILDER_QUAY_PASSWORD:-$QUAY_TOKEN}" +readonly CICD_IMAGE_BUILDER_REDHAT_USER="${CICD_IMAGE_BUILDER_REDHAT_USER:-$RH_REGISTRY_USER}" +readonly CICD_IMAGE_BUILDER_REDHAT_PASSWORD="${CICD_IMAGE_BUILDER_REDHAT_PASSWORD:-$RH_REGISTRY_TOKEN}" +readonly CICD_IMAGE_BUILDER_DEFAULT_BUILD_CONTEXT='.' +readonly CICD_IMAGE_BUILDER_DEFAULT_CONTAINERFILE_PATH='Dockerfile' + +cicd::image_builder::local_build() { + [[ "$CICD_IMAGE_BUILDER_LOCAL_BUILD" = true ]] || ! cicd::common::is_ci_context +} cicd::image_builder::build_and_push() { cicd::image_builder::build || return 1 - if ! cicd::common::local_build; then + if ! cicd::image_builder::local_build; then cicd::image_builder::push || return 1 fi } @@ -40,8 +44,8 @@ cicd::image_builder::build() { image_name="$(cicd::image_builder::_get_image_name)" || return 1 default_image_name=$(cicd::image_builder::get_full_image_name) || return 1 - if ! [ -r "$containerfile" ]; then - cicd::err "Containerfile '$containerfile' does not exist or is not readable!" + if ! [[ -r "$containerfile" ]]; then + cicd::log::err "Containerfile '$containerfile' does not exist or is not readable!" return 1 fi @@ -63,17 +67,17 @@ cicd::image_builder::build() { build_params+=("$build_context") if ! cicd::container::cmd build "${build_params[@]}"; then - cicd::err "Error building image" + cicd::log::err "Error building image" return 1 fi } cicd::image_builder::get_containerfile() { - local containerfile="${CICD_TOOLS_IMAGE_BUILDER_CONTAINERFILE_PATH:-"$CONTAINERFILE_PATH"}" + local containerfile="${CICD_IMAGE_BUILDER_CONTAINERFILE_PATH:-"$CONTAINERFILE_PATH"}" if [ -z "$containerfile" ]; then - containerfile=$CICD_TOOLS_IMAGE_BUILDER_DEFAULT_CONTAINERFILE_PATH + containerfile=$CICD_IMAGE_BUILDER_DEFAULT_CONTAINERFILE_PATH fi echo -n "$containerfile" @@ -81,10 +85,10 @@ cicd::image_builder::get_containerfile() { cicd::image_builder::get_build_context() { - local build_context="${CICD_TOOLS_IMAGE_BUILDER_BUILD_CONTEXT:-"$BUILD_CONTEXT"}" + local build_context="${CICD_IMAGE_BUILDER_BUILD_CONTEXT:-"$BUILD_CONTEXT"}" if [ -z "$build_context" ]; then - build_context="$CICD_TOOLS_IMAGE_BUILDER_DEFAULT_BUILD_CONTEXT" + build_context="$CICD_IMAGE_BUILDER_DEFAULT_BUILD_CONTEXT" fi echo -n "$build_context" @@ -92,10 +96,10 @@ cicd::image_builder::get_build_context() { cicd::image_builder::_get_image_name() { - local image_name="${CICD_TOOLS_IMAGE_BUILDER_IMAGE_NAME:-$IMAGE_NAME}" + local image_name="${CICD_IMAGE_BUILDER_IMAGE_NAME:-$IMAGE_NAME}" if [ -z "$image_name" ]; then - cicd::err "Image name not defined, please set IMAGE_NAME environment variable" + cicd::log::err "Image name not defined, please set IMAGE_NAME environment variable" return 1 fi @@ -104,7 +108,7 @@ cicd::image_builder::_get_image_name() { cicd::image_builder::get_image_tag() { - local base_tag="${CICD_TOOLS_IMAGE_BUILDER_IMAGE_TAG:-$IMAGE_TAG}" + local base_tag="${CICD_IMAGE_BUILDER_IMAGE_TAG:-$IMAGE_TAG}" if [[ -z "$base_tag" ]]; then base_tag=$(cicd::image_builder::get_commit_based_image_tag) @@ -118,7 +122,7 @@ cicd::image_builder::get_commit_based_image_tag() { local commit_hash if ! commit_hash=$(cicd::common::get_7_chars_commit_hash); then - cicd::err "Cannot retrieve commit hash!" + cicd::log::err "Cannot retrieve commit hash!" return 1 fi @@ -159,7 +163,7 @@ cicd::image_builder::get_build_id() { cicd::image_builder::get_additional_tags() { - declare -a configured_tags=("${CICD_TOOLS_IMAGE_BUILDER_ADDITIONAL_TAGS[@]:-${ADDITIONAL_TAGS[@]}}") + declare -a configured_tags=("${CICD_IMAGE_BUILDER_ADDITIONAL_TAGS[@]:-${ADDITIONAL_TAGS[@]}}") declare -a additional_tags if cicd::image_builder::_array_empty "${configured_tags[@]}"; then @@ -181,7 +185,7 @@ cicd::image_builder::_array_empty() { cicd::image_builder::get_labels() { - declare -a labels=("${CICD_TOOLS_IMAGE_BUILDER_LABELS[@]:-${LABELS[@]}}") + declare -a labels=("${CICD_IMAGE_BUILDER_LABELS[@]:-${LABELS[@]}}") if cicd::image_builder::_array_empty "${labels[@]}"; then labels=() @@ -195,12 +199,12 @@ cicd::image_builder::get_labels() { } cicd::image_builder::_get_expiry_label() { - echo "quay.expires-after=${CICD_TOOLS_IMAGE_BUILDER_QUAY_EXPIRE_TIME}" + echo "quay.expires-after=${CICD_IMAGE_BUILDER_QUAY_EXPIRE_TIME}" } cicd::image_builder::get_build_args() { - declare -a build_args=("${CICD_TOOLS_IMAGE_BUILDER_BUILD_ARGS[@]:-${BUILD_ARGS[@]}}") + declare -a build_args=("${CICD_IMAGE_BUILDER_BUILD_ARGS[@]:-${BUILD_ARGS[@]}}") if cicd::image_builder::_array_empty "${build_args[@]}"; then build_args=() @@ -217,7 +221,7 @@ cicd::image_builder::tag() { for target_tag in $(cicd::image_builder::get_additional_tags); do if ! cicd::container::cmd tag "$source_image" "${image_name}:${target_tag}"; then - cicd::err "Error tagging '$source_image' as '${image_name}:${target_tag}'" + cicd::log::err "Error tagging '$source_image' as '${image_name}:${target_tag}'" return 1 fi done @@ -237,7 +241,7 @@ cicd::image_builder::push() { for tag in "${image_tags[@]}"; do if ! cicd::container::cmd push "${image_name}:${tag}"; then - cicd::err "Error pushing image: '${image_name}:${tag}'" + cicd::log::err "Error pushing image: '${image_name}:${tag}'" return 1 fi done @@ -255,43 +259,43 @@ cicd::image_builder::get_full_image_name() { cicd::image_builder::_image_builder_setup() { if ! cicd::image_builder::_try_log_in_to_image_registries; then - cicd::err "Error trying to log into the image registries!" + cicd::log::err "Error trying to log into the image registries!" return 1 fi } cicd::image_builder::_try_log_in_to_image_registries() { - if ! cicd::common::local_build; then + if ! cicd::image_builder::local_build; then DOCKER_CONFIG="$(mktemp -d)" export DOCKER_CONFIG - echo -n '{}' > "${DOCKER_CONFIG}/config.json" + echo -n '{}' >"${DOCKER_CONFIG}/config.json" fi if cicd::image_builder::_quay_credentials_found; then if ! cicd::image_builder::_log_in_to_quay_registry; then - cicd::err "Error logging in to Quay.io!" + cicd::log::err "Error logging in to Quay.io!" return 1 fi fi if cicd::image_builder::_redhat_registry_credentials_found; then if ! cicd::image_builder::_log_in_to_redhat_registry; then - cicd::err "Error logging in to Red Hat Registry!" + cicd::log::err "Error logging in to Red Hat Registry!" return 1 fi fi } cicd::image_builder::_quay_credentials_found() { - [ -n "$CICD_TOOLS_IMAGE_BUILDER_QUAY_USER" ] && - [ -n "$CICD_TOOLS_IMAGE_BUILDER_QUAY_PASSWORD" ] + [ -n "$CICD_IMAGE_BUILDER_QUAY_USER" ] && + [ -n "$CICD_IMAGE_BUILDER_QUAY_PASSWORD" ] } cicd::image_builder::_log_in_to_quay_registry() { - cicd::image_builder::_log_in_to_container_registry "$CICD_TOOLS_IMAGE_BUILDER_QUAY_USER" \ - "$CICD_TOOLS_IMAGE_BUILDER_QUAY_PASSWORD" \ - "$CICD_TOOLS_IMAGE_BUILDER_QUAY_REGISTRY" + cicd::image_builder::_log_in_to_container_registry "$CICD_IMAGE_BUILDER_QUAY_USER" \ + "$CICD_IMAGE_BUILDER_QUAY_PASSWORD" \ + "$CICD_IMAGE_BUILDER_QUAY_REGISTRY" } cicd::image_builder::_log_in_to_container_registry() { @@ -300,25 +304,24 @@ cicd::image_builder::_log_in_to_container_registry() { local password="$2" local registry="$3" - cicd::container::cmd login "-u=${username}" "--password-stdin" "$registry" <<< "$password" + cicd::container::cmd login "-u=${username}" "--password-stdin" "$registry" <<<"$password" } cicd::image_builder::_redhat_registry_credentials_found() { - [ -n "$CICD_TOOLS_IMAGE_BUILDER_REDHAT_USER" ] && - [ -n "$CICD_TOOLS_IMAGE_BUILDER_REDHAT_PASSWORD" ] + [ -n "$CICD_IMAGE_BUILDER_REDHAT_USER" ] && + [ -n "$CICD_IMAGE_BUILDER_REDHAT_PASSWORD" ] } cicd::image_builder::_log_in_to_redhat_registry() { - cicd::image_builder::_log_in_to_container_registry "$CICD_TOOLS_IMAGE_BUILDER_REDHAT_USER" \ - "$CICD_TOOLS_IMAGE_BUILDER_REDHAT_PASSWORD" \ - "$CICD_TOOLS_IMAGE_BUILDER_REDHAT_REGISTRY" + cicd::image_builder::_log_in_to_container_registry "$CICD_IMAGE_BUILDER_REDHAT_USER" \ + "$CICD_IMAGE_BUILDER_REDHAT_PASSWORD" \ + "$CICD_IMAGE_BUILDER_REDHAT_REGISTRY" } if ! cicd::image_builder::_image_builder_setup; then - cicd::err "Image builder setup failed!" + cicd::log::err "Image builder setup failed!" return 1 fi -cicd::debug "Image builder lib loaded" - -CICD_TOOLS_IMAGE_BUILDER_LOADED=0 +cicd::log::debug "Image builder module loaded" +CICD_IMAGE_BUILDER_MODULE_LOADED='true' diff --git a/src/shared/loader.sh b/src/shared/loader.sh new file mode 100644 index 00000000..ad8ed127 --- /dev/null +++ b/src/shared/loader.sh @@ -0,0 +1,59 @@ +CICD_LOADER_MODULE_LOADED=${CICD_LOADER_MODULE_LOADED:-1} + +if [[ "$CICD_LOADER_MODULE_LOADED" -eq 0 ]]; then + cicd::log::debug "loader module already loaded, skipping" + return 0 +fi + +if [[ -z "$CICD_LOADER_SCRIPTS_DIR" ]]; then + echo "Error, CICD_LOADER_SCRIPTS_DIR not defined, use $(main.sh) to load this module" + echo "Then, use cicd::loader::load_module to load modules" + return 1 +fi + +if ! source "${CICD_LOADER_SCRIPTS_DIR}/shared/log.sh"; then + echo "Error loading 'log' module!" + return 1 +fi + +cicd::log::debug "loading loader module" + +cicd::loader::load_module() { + + local module_id="${1:-container}" + + case "$module_id" in + all) cicd::loader::_load_all ;; + common) cicd::loader::_load_common_module ;; + container) cicd::loader::_load_container_module ;; + image_builder) cicd::loader::_load_image_builder_module ;; + *) cicd::log::err "Unsupported module: '$module_id'" && return 1 ;; + esac +} + +cicd::loader::_load_all() { + cicd::loader::_load_common_module + cicd::loader::_load_container_module + cicd::loader::_load_image_builder_module +} + +cicd::loader::_load_common_module() { + # shellcheck source=src/shared/common.sh + source "${CICD_LOADER_SCRIPTS_DIR}/shared/common.sh" +} + +cicd::loader::_load_container_module() { + cicd::loader::_load_common_module + # shellcheck source=src/shared/container.sh + source "${CICD_LOADER_SCRIPTS_DIR}/shared/container.sh" +} + +cicd::loader::_load_image_builder_module() { + cicd::loader::_load_common_module + cicd::loader::_load_container_module + # shellcheck source=src/shared/image_builder.sh + source "${CICD_LOADER_SCRIPTS_DIR}/shared/image_builder.sh" +} + +cicd::log::debug "loader module loaded" +CICD_LOADER_MODULE_LOADED=0 diff --git a/src/shared/log.sh b/src/shared/log.sh new file mode 100644 index 00000000..3c511b57 --- /dev/null +++ b/src/shared/log.sh @@ -0,0 +1,27 @@ +CICD_LOG_DEBUG=${CICD_LOG_DEBUG:-} + +if [[ -n "$CICD_LOG_MODULE_LOADED" ]]; then + cicd::log::debug "log module already loaded, skipping" + return 0 +fi + +cicd::log::debug() { + if cicd::log::_debug_mode; then + cicd::log::info "$*" + fi +} + +cicd::log::_debug_mode() { + [[ -n "$CICD_LOG_DEBUG" ]] +} + +cicd::log::info() { + echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" +} + +cicd::log::err() { + echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 +} + +cicd::log::debug "log module loaded" +CICD_LOG_MODULE_LOADED='true' diff --git a/test/load_module.bats b/test/load_module.bats new file mode 100644 index 00000000..053d5cc8 --- /dev/null +++ b/test/load_module.bats @@ -0,0 +1,68 @@ +setup() { + load 'test_helper/common-setup' + _common_setup +} + +@test "Unsupported modules fail to load" { + + run ! source load_module.sh unsupported-foo-module + assert_failure 1 + assert_output --partial "Unsupported module: 'unsupported-foo-module'" +} + +@test "Default main loading sequence runs successfully" { + + CICD_LOG_DEBUG=1 + run source src/load_module.sh '' + assert_success + assert_output --partial "loading common module" + assert_output --partial "loading container module" +} + +@test "loading all work successfully" { + + CICD_LOG_DEBUG=1 + run source load_module.sh all + assert_success + assert_output --partial "loading common module" + assert_output --partial "loading container module" + assert_output --partial "loading image builder module" +} + +@test "loading container helper functions work successfully" { + + podman() { + echo "podman here" + } + run ! cicd::container::cmd + assert_failure + CICD_LOG_DEBUG=1 + run source load_module.sh container + assert_success + assert_output --partial "loading container module" + source load_module.sh container + run cicd::container::cmd + assert_success + assert_output --partial "podman here" +} + +@test "Loading multiple times don't reload modules multiple times" { + + IMAGE_REPOSITORY='FOO' + assert [ -z "$CICD_COMMON_MODULE_LOADED" ] + source load_module.sh all + assert [ -n "$CICD_COMMON_MODULE_LOADED" ] + CICD_LOG_DEBUG=1 + run source load_module.sh "" + assert_success + refute_output --partial "loading common module" + run source load_module.sh all + assert_success + refute_output --partial "loading common module" + run source load_module.sh container + assert_success + refute_output --partial "loading container module" + run source load_module.sh image_builder + assert_success + refute_output --partial "loading image_builder module" +} diff --git a/test/main.bats b/test/main.bats deleted file mode 100644 index 30f20a22..00000000 --- a/test/main.bats +++ /dev/null @@ -1,69 +0,0 @@ -setup() { - load 'test_helper/common-setup' - _common_setup -} - -@test "Unsupported libraries fail to load" { - - run ! source main.sh unsupported-foo-library - assert_failure 1 - assert_output --partial "Unsupported library: 'unsupported-foo-library'" -} - -@test "Default main loading sequence runs successfully" { - - CICD_TOOLS_DEBUG=1 - run source src/main.sh '' - assert_success - assert_output --partial "loading common" - assert_output --partial "loading container lib" -} - -@test "loading all work successfully" { - - CICD_TOOLS_DEBUG=1 - IMAGE_REPOSITORY='foobar' - run source main.sh all - assert_success - assert_output --partial "loading common lib" - assert_output --partial "loading container lib" - assert_output --partial "loading image builder lib" -} - -@test "loading container helper functions work successfully" { - - podman() { - echo "podman here" - } - run ! cicd::container::cmd - assert_failure - CICD_TOOLS_DEBUG=1 - run source main.sh container - assert_success - assert_output --partial "loading container lib" - source main.sh container - run cicd::container::cmd - assert_success - assert_output --partial "podman here" -} - -@test "Loading multiple times don't reload libraries multiple times" { - - IMAGE_REPOSITORY='FOO' - assert [ -z "$CICD_TOOLS_COMMON_LOADED" ] - source main.sh all - assert [ "$CICD_TOOLS_COMMON_LOADED" -eq 0 ] - CICD_TOOLS_DEBUG=1 - run source main.sh "" - assert_success - refute_output --partial "loading common lib" - run source main.sh all - assert_success - refute_output --partial "loading common lib" - run source main.sh container - assert_success - refute_output --partial "loading container lib" - run source main.sh image_builder - assert_success - refute_output --partial "loading image_builder lib" -} From 8f8b6e1f0952cf990e0a5afc5f93ac0b32f284c9 Mon Sep 17 00:00:00 2001 From: Victor M Date: Thu, 19 Oct 2023 00:18:30 +0200 Subject: [PATCH 02/15] wip-before-reverting-loader-module --- src/load_module.sh | 1 + src/shared/common.sh | 2 +- src/shared/container.sh | 38 +++--- src/shared/image_builder.sh | 6 +- src/shared/loader.sh | 22 ++- test/generate_coverage.sh | 4 +- test/load_module.bats | 68 ---------- ...red_common_lib.bats => shared_common.bats} | 52 +++----- ...ntainer_lib.bats => shared_container.bats} | 50 +++---- ...der_lib.bats => shared_image_builder.bats} | 102 ++++++++------ test/shared_loader.bats | 126 ++++++++++++++++++ 11 files changed, 280 insertions(+), 191 deletions(-) delete mode 100644 test/load_module.bats rename test/{shared_common_lib.bats => shared_common.bats} (54%) rename test/{shared_container_lib.bats => shared_container.bats} (79%) rename test/{shared_image_builder_lib.bats => shared_image_builder.bats} (85%) create mode 100644 test/shared_loader.bats diff --git a/src/load_module.sh b/src/load_module.sh index 9347844e..d0883fb6 100644 --- a/src/load_module.sh +++ b/src/load_module.sh @@ -7,4 +7,5 @@ if ! source "${CICD_LOADER_SCRIPTS_DIR}/shared/loader.sh"; then exit 1 fi +# TODO: undo all loader module stuff cicd::loader::load_module "$1" diff --git a/src/shared/common.sh b/src/shared/common.sh index 406bd696..7e512c57 100644 --- a/src/shared/common.sh +++ b/src/shared/common.sh @@ -6,7 +6,7 @@ if [[ -n "$CICD_COMMON_MODULE_LOADED" ]]; then fi if [[ -z "$CICD_LOADER_MODULE_LOADED" ]]; then - echo "loader module not found, please use $(load_module.sh) to load modules." + echo "loader module not found, please use 'load_module.sh' to load modules." return 1 fi diff --git a/src/shared/container.sh b/src/shared/container.sh index 7dd4577f..468deaa5 100644 --- a/src/shared/container.sh +++ b/src/shared/container.sh @@ -6,57 +6,59 @@ if [[ -n "$CICD_CONTAINER_MODULE_LOADED" ]]; then fi if [[ -z "$CICD_LOADER_MODULE_LOADED" ]]; then - echo "loader module not found, please use $(load_module.sh) to load modules." + echo "loader module not found, please use 'load_module.sh' to load modules." return 1 fi cicd::log::debug "loading container module" -CONTAINER_ENGINE_CMD='' -PREFER_CONTAINER_ENGINE=${PREFER_CONTAINER_ENGINE:-} +CICD_CONTAINER_ENGINE='' +CICD_CONTAINER_PREFER_ENGINE=${CICD_CONTAINER_PREFER_ENGINE:-} cicd::container::cmd() { - if [[ -z "$CONTAINER_ENGINE_CMD" ]]; then + if [[ -z "$CICD_CONTAINER_ENGINE" ]]; then if ! cicd::container::_set_container_engine_cmd; then return 1 fi fi - "$CONTAINER_ENGINE_CMD" "$@" + "$CICD_CONTAINER_ENGINE" "$@" } cicd::container::_set_container_engine_cmd() { if cicd::container::_preferred_container_engine_available; then - CONTAINER_ENGINE_CMD="$PREFER_CONTAINER_ENGINE" + CICD_CONTAINER_ENGINE="$CICD_CONTAINER_PREFER_ENGINE" else if cicd::container::_container_engine_available 'podman'; then - CONTAINER_ENGINE_CMD='podman' + CICD_CONTAINER_ENGINE='podman' elif cicd::container::_container_engine_available 'docker'; then - CONTAINER_ENGINE_CMD='docker' + CICD_CONTAINER_ENGINE='docker' else cicd::log::err "ERROR, no container engine found, please install either podman or docker first" return 1 fi fi - cicd::log::debug "Container engine selected: $CONTAINER_ENGINE_CMD" + readonly CICD_CONTAINER_ENGINE + + cicd::log::debug "Container engine selected: $CICD_CONTAINER_ENGINE" } cicd::container::_preferred_container_engine_available() { - local CONTAINER_ENGINE_AVAILABLE=1 + local engine_available=1 - if [ -n "$PREFER_CONTAINER_ENGINE" ]; then - if cicd::container::_container_engine_available "$PREFER_CONTAINER_ENGINE"; then - CONTAINER_ENGINE_AVAILABLE=0 + if [ -n "$CICD_CONTAINER_PREFER_ENGINE" ]; then + if cicd::container::_container_engine_available "$CICD_CONTAINER_PREFER_ENGINE"; then + engine_available=0 else - cicd::log::info "WARNING: preferred container engine '${PREFER_CONTAINER_ENGINE}' not present, or isn't supported, finding alternative..." + cicd::log::info "WARNING: preferred container engine '${CICD_CONTAINER_PREFER_ENGINE}' not present, or isn't supported, finding alternative..." fi fi - return "$CONTAINER_ENGINE_AVAILABLE" + return "$engine_available" } cicd::container::_container_engine_available() { @@ -90,10 +92,10 @@ cicd::container::_cmd_exists_and_is_supported() { cicd::container::_supported_container_engine() { - local CONTAINER_ENGINE_TO_CHECK="$1" + local engine_to_check="$1" - [ "$CONTAINER_ENGINE_TO_CHECK" = 'docker' ] || - [ "$CONTAINER_ENGINE_TO_CHECK" = 'podman' ] + [ "$engine_to_check" = 'docker' ] || + [ "$engine_to_check" = 'podman' ] } cicd::container::_docker_seems_emulated() { diff --git a/src/shared/image_builder.sh b/src/shared/image_builder.sh index cb9255b7..f6807519 100644 --- a/src/shared/image_builder.sh +++ b/src/shared/image_builder.sh @@ -6,11 +6,11 @@ if [[ -n "$CICD_IMAGE_BUILDER_MODULE_LOADED" ]]; then fi if [[ -z "$CICD_LOADER_MODULE_LOADED" ]]; then - echo "loader module not found, please use $(load_module.sh) to load modules." + echo "loader module not found, please use 'load_module.sh' to load modules." return 1 fi -cicd::log::debug "loading image builder module" +cicd::log::debug "loading image_builder module" readonly CICD_IMAGE_BUILDER_LOCAL_BUILD=${CICD_IMAGE_BUILDER_LOCAL_BUILD:-$LOCAL_BUILD} readonly CICD_IMAGE_BUILDER_REDHAT_REGISTRY='registry.redhat.io' @@ -323,5 +323,5 @@ if ! cicd::image_builder::_image_builder_setup; then return 1 fi -cicd::log::debug "Image builder module loaded" +cicd::log::debug "image_builder module loaded" CICD_IMAGE_BUILDER_MODULE_LOADED='true' diff --git a/src/shared/loader.sh b/src/shared/loader.sh index ad8ed127..8f9a6549 100644 --- a/src/shared/loader.sh +++ b/src/shared/loader.sh @@ -6,8 +6,8 @@ if [[ "$CICD_LOADER_MODULE_LOADED" -eq 0 ]]; then fi if [[ -z "$CICD_LOADER_SCRIPTS_DIR" ]]; then - echo "Error, CICD_LOADER_SCRIPTS_DIR not defined, use $(main.sh) to load this module" - echo "Then, use cicd::loader::load_module to load modules" + echo "Error, CICD_LOADER_SCRIPTS_DIR not defined, use 'load_module.sh' to load this module" + echo "Then, use 'cicd::loader::load_module' to load modules" return 1 fi @@ -20,35 +20,53 @@ cicd::log::debug "loading loader module" cicd::loader::load_module() { + # TODO: refactor to source here and leave modules to define their dependencies + local module_id="${1:-container}" case "$module_id" in all) cicd::loader::_load_all ;; + log) cicd::loader::_load_log_module ;; common) cicd::loader::_load_common_module ;; container) cicd::loader::_load_container_module ;; image_builder) cicd::loader::_load_image_builder_module ;; + loader) cicd::loader::_load_loader_module ;; *) cicd::log::err "Unsupported module: '$module_id'" && return 1 ;; esac } cicd::loader::_load_all() { + cicd::loader::_load_log_module + cicd::loader::_load_loader_module cicd::loader::_load_common_module cicd::loader::_load_container_module cicd::loader::_load_image_builder_module } +cicd::loader::_load_log_module() { + # shellcheck source=src/shared/common.sh + source "${CICD_LOADER_SCRIPTS_DIR}/shared/log.sh" +} + +cicd::loader::_load_loader_module() { + # shellcheck source=src/shared/loader.sh + source "${CICD_LOADER_SCRIPTS_DIR}/shared/loader.sh" +} + cicd::loader::_load_common_module() { # shellcheck source=src/shared/common.sh source "${CICD_LOADER_SCRIPTS_DIR}/shared/common.sh" } cicd::loader::_load_container_module() { + # TODO: move dependencies within modules cicd::loader::_load_common_module # shellcheck source=src/shared/container.sh source "${CICD_LOADER_SCRIPTS_DIR}/shared/container.sh" } cicd::loader::_load_image_builder_module() { + # TODO: move dependencies within modules cicd::loader::_load_common_module cicd::loader::_load_container_module # shellcheck source=src/shared/image_builder.sh diff --git a/test/generate_coverage.sh b/test/generate_coverage.sh index 2f3b87bd..041b6633 100755 --- a/test/generate_coverage.sh +++ b/test/generate_coverage.sh @@ -2,7 +2,7 @@ COVERAGE_DIRECTORY="$PWD/coverage" BATS_CMD='bats' -TESTS_DIRECTORY='test' +TESTS_PATH="${1:-test}" IGNORE_TAGS='!no-kcov' KCOV_CMD='kcov' @@ -24,7 +24,7 @@ fi "$COVERAGE_DIRECTORY" \ "$BATS_CMD" \ --filter-tags "$IGNORE_TAGS" \ - "$TESTS_DIRECTORY" + "$TESTS_PATH" if [ "$CI" != 'true' ]; then diff --git a/test/load_module.bats b/test/load_module.bats deleted file mode 100644 index 053d5cc8..00000000 --- a/test/load_module.bats +++ /dev/null @@ -1,68 +0,0 @@ -setup() { - load 'test_helper/common-setup' - _common_setup -} - -@test "Unsupported modules fail to load" { - - run ! source load_module.sh unsupported-foo-module - assert_failure 1 - assert_output --partial "Unsupported module: 'unsupported-foo-module'" -} - -@test "Default main loading sequence runs successfully" { - - CICD_LOG_DEBUG=1 - run source src/load_module.sh '' - assert_success - assert_output --partial "loading common module" - assert_output --partial "loading container module" -} - -@test "loading all work successfully" { - - CICD_LOG_DEBUG=1 - run source load_module.sh all - assert_success - assert_output --partial "loading common module" - assert_output --partial "loading container module" - assert_output --partial "loading image builder module" -} - -@test "loading container helper functions work successfully" { - - podman() { - echo "podman here" - } - run ! cicd::container::cmd - assert_failure - CICD_LOG_DEBUG=1 - run source load_module.sh container - assert_success - assert_output --partial "loading container module" - source load_module.sh container - run cicd::container::cmd - assert_success - assert_output --partial "podman here" -} - -@test "Loading multiple times don't reload modules multiple times" { - - IMAGE_REPOSITORY='FOO' - assert [ -z "$CICD_COMMON_MODULE_LOADED" ] - source load_module.sh all - assert [ -n "$CICD_COMMON_MODULE_LOADED" ] - CICD_LOG_DEBUG=1 - run source load_module.sh "" - assert_success - refute_output --partial "loading common module" - run source load_module.sh all - assert_success - refute_output --partial "loading common module" - run source load_module.sh container - assert_success - refute_output --partial "loading container module" - run source load_module.sh image_builder - assert_success - refute_output --partial "loading image_builder module" -} diff --git a/test/shared_common_lib.bats b/test/shared_common.bats similarity index 54% rename from test/shared_common_lib.bats rename to test/shared_common.bats index 45e97c92..039a3dbb 100644 --- a/test/shared_common_lib.bats +++ b/test/shared_common.bats @@ -6,25 +6,34 @@ setup() { @test "Load directly results in error" { - run ! source "src/shared/common_lib.sh" + run ! source "src/shared/common.sh" assert_failure - assert_output --partial "load through main.sh" + assert_output --partial "please use 'load_module.sh' to load modules." } +@test "Load the module multiple times only loads it once" { + CICD_LOG_DEBUG=1 + source load_module.sh common + run cicd::loader::load_module common + assert_success + assert_output --partial "common module already loaded, skipping" +} + + @test "Sets expected loaded flags" { - assert [ -z "$CICD_TOOLS_COMMON_LOADED" ] + assert [ -z "$CICD_COMMON_MODULE_LOADED" ] - source main.sh common + source load_module.sh common - assert [ "$CICD_TOOLS_COMMON_LOADED" -eq 0 ] + assert [ -n "$CICD_COMMON_MODULE_LOADED" ] } @test "Loading common message is displayed" { - CICD_TOOLS_DEBUG=1 - run source main.sh common + CICD_LOG_DEBUG=1 + run source load_module.sh common assert_success assert_output --partial "loading common" } @@ -35,7 +44,7 @@ setup() { echo "cat exists" } - source main.sh common + source load_module.sh common run ! cicd::common::command_is_present foo assert_failure @@ -46,38 +55,15 @@ setup() { @test "get_7_chars_commit_hash works" { - source main.sh common + source load_module.sh common run cicd::common::get_7_chars_commit_hash assert_success assert_output --regexp '^[0-9a-f]{7}$' } -@test "local build check" { - - unset CI - - assert [ -z "$LOCAL_BUILD" ] - assert [ -z "$CI" ] - source src/main.sh common - run cicd::common::local_build - assert_success - CI='true' - run cicd::common::local_build - assert_failure - assert_output "" - LOCAL_BUILD='true' - run cicd::common::local_build - assert_output "" - assert_success - unset LOCAL_BUILD - run ! cicd::common::local_build - assert_output "" - assert_failure -} - @test "is_ci_context" { - source src/main.sh common + source src/load_module.sh common unset CI assert [ -z "$CI" ] diff --git a/test/shared_container_lib.bats b/test/shared_container.bats similarity index 79% rename from test/shared_container_lib.bats rename to test/shared_container.bats index bce10902..ac8e8acf 100644 --- a/test/shared_container_lib.bats +++ b/test/shared_container.bats @@ -6,28 +6,28 @@ setup() { @test "Load directly results in error" { - run ! source "src/shared/container_lib.sh" + run ! source "src/shared/container.sh" assert_failure - assert_output --partial "load through main.sh" + assert_output --partial "please use 'load_module.sh' to load modules." } @test "Sets expected loaded flags" { - assert [ -z "$CICD_TOOLS_COMMON_LOADED" ] - assert [ -z "$CICD_TOOLS_CONTAINER_ENGINE_LOADED" ] + assert [ -z "$CICD_COMMON_MODULE_LOADED" ] + assert [ -z "$CICD_CONTAINER_MODULE_LOADED" ] - source main.sh container + source load_module.sh container - assert [ -n "$CICD_TOOLS_COMMON_LOADED" ] - assert [ -n "$CICD_TOOLS_CONTAINER_ENGINE_LOADED" ] + assert [ -n "$CICD_COMMON_MODULE_LOADED" ] + assert [ -n "$CICD_CONTAINER_MODULE_LOADED" ] } -@test "Loading common message is displayed" { +@test "Loading message is displayed" { - CICD_TOOLS_DEBUG=1 - run source main.sh container + CICD_LOG_DEBUG=1 + run source load_module.sh container assert_success - assert_output --partial "loading container lib" + assert_output --partial "loading container module" } @test "container engine cmd is set once" { @@ -39,13 +39,13 @@ setup() { echo "podman version 1" } - PREFER_CONTAINER_ENGINE="docker" + CICD_CONTAINER_PREFER_ENGINE="docker" run ! cicd::container::cmd assert_failure assert_output --partial "cicd::container::cmd: command not found" - source src/main.sh container + source load_module.sh container cicd::container::cmd --version run cicd::container::cmd --version @@ -53,8 +53,8 @@ setup() { assert_success assert_output "docker version 1" - unset PREFER_container - source main.sh container + unset CICD_CONTAINER_PREFER_ENGINE + source load_module.sh container run cicd::container::cmd --version assert_success assert_output "docker version 1" @@ -73,7 +73,7 @@ setup() { assert_failure assert_output --partial "cicd::container::cmd: command not found" - source src/main.sh container + source src/load_module.sh container cicd::container::cmd --version @@ -83,7 +83,7 @@ setup() { assert_success assert_output "podman version 1" - source main.sh container + source load_module.sh container run cicd::container::cmd --version assert_success assert_output "podman version 1" @@ -95,7 +95,7 @@ setup() { echo "podman version 1" } - source main.sh container + source load_module.sh container run cicd::container::cmd --version assert_output --partial "podman version 1" } @@ -112,7 +112,7 @@ setup() { echo 'podman version 1' } - source main.sh container + source load_module.sh container run cicd::container::cmd --version assert_output --regexp "WARNING.*docker.*seems emulated" assert_output --partial "podman version 1" @@ -120,7 +120,7 @@ setup() { @test "if no container engine found, fails" { - source main.sh container + source load_module.sh container OLDPATH="$PATH" PATH=':' run ! cicd::container::cmd --version @@ -136,7 +136,7 @@ setup() { docker() { echo 'docker version 1' } - source main.sh container + source load_module.sh container OLDPATH="$PATH" PATH=':' @@ -157,7 +157,7 @@ setup() { date() { echo -n "Thu Sep 21 06:25:51 PM CEST 2023" } - source main.sh container + source load_module.sh container OLDPATH="$PATH" PATH=':' @@ -177,7 +177,7 @@ setup() { docker() { echo 'docker version 1' } - source main.sh container + source load_module.sh container run cicd::container::cmd --version assert_success @@ -194,7 +194,7 @@ setup() { docker() { echo 'docker version 1' } - source main.sh container + source load_module.sh container run cicd::container::cmd --version assert_success assert_output --partial "docker version 1" @@ -211,7 +211,7 @@ setup() { podman() { echo 'podman version 1' } - source main.sh container + source load_module.sh container run cicd::container::cmd --version assert_success assert_output --regexp "WARNING.*'cat'.*isn't supported" diff --git a/test/shared_image_builder_lib.bats b/test/shared_image_builder.bats similarity index 85% rename from test/shared_image_builder_lib.bats rename to test/shared_image_builder.bats index b38d93c4..bdeaf270 100644 --- a/test/shared_image_builder_lib.bats +++ b/test/shared_image_builder.bats @@ -4,30 +4,54 @@ setup() { _common_setup } + @test "Load directly results in error" { - run ! source "src/shared/image_builder_lib.sh" + run ! source "src/shared/image_builder.sh" assert_failure - assert_output --partial "load through main.sh" + assert_output --partial "please use 'load_module.sh' to load modules." } @test "Does not fail sourcing the library" { - run source main.sh image_builder + run source load_module.sh image_builder + assert_success +} + +@test "local build check" { + + unset CI + + assert [ -z "$LOCAL_BUILD" ] + assert [ -z "$CI" ] + source src/load_module.sh image_builder + run cicd::image_builder::local_build assert_success + CI='true' + run cicd::image_builder::local_build + assert_failure + assert_output "" + LOCAL_BUILD='true' + run cicd::image_builder::local_build + assert_output "" + assert_success + unset LOCAL_BUILD + run ! cicd::image_builder::local_build + assert_output "" + assert_failure } @test "Sets expected loaded flags" { - assert [ -z "$CICD_TOOLS_COMMON_LOADED" ] - assert [ -z "$CICD_TOOLS_CONTAINER_ENGINE_LOADED" ] - assert [ -z "$CICD_TOOLS_IMAGE_BUILDER_LOADED" ] + assert [ -z "$CICD_COMMON_MODULE_LOADED" ] + assert [ -z "$CICD_CONTAINER_ENGINE_MODULE_LOADED" ] + assert [ -z "$CICD_IMAGE_BUILDER_MODULE_LOADED" ] - source main.sh image_builder + source load_module.sh image_builder - assert [ -n "$CICD_TOOLS_COMMON_LOADED" ] - assert [ -n "$CICD_TOOLS_CONTAINER_ENGINE_LOADED" ] - assert [ -n "$CICD_TOOLS_IMAGE_BUILDER_LOADED" ] + assert [ -n "$CICD_COMMON_MODULE_LOADED" ] + assert [ -n "$CICD_CONTAINER_ENGINE_MODULE_LOADED" ] + assert [ -n "$CICD_IMAGE_BUILDER_MODULE_LOADED" ] } @test "Image tag outside of a change request context" { @@ -37,7 +61,7 @@ setup() { echo "1abcdef" } - source main.sh image_builder + source load_module.sh image_builder run cicd::image_builder::get_image_tag assert_success assert_output '1abcdef' @@ -51,7 +75,7 @@ setup() { } ghprbPullId=123 - source main.sh image_builder + source load_module.sh image_builder run cicd::image_builder::get_image_tag @@ -68,7 +92,7 @@ setup() { gitlabMergeRequestId=4321 - source main.sh image_builder + source load_module.sh image_builder run cicd::image_builder::get_image_tag @@ -89,7 +113,7 @@ setup() { echo "1abcdef" } - source main.sh image_builder + source load_module.sh image_builder EXPECTED_CONTAINERFILE_PATH='Dockerfile' IMAGE_NAME='quay.io/foo/bar' @@ -113,7 +137,7 @@ setup() { echo "1abcdef" } - source main.sh image_builder + source load_module.sh image_builder run ! cicd::image_builder::build assert_failure @@ -134,7 +158,7 @@ setup() { } IMAGE_NAME='quay.io/foo/bar' - source main.sh image_builder + source load_module.sh image_builder run ! cicd::image_builder::build assert_failure @@ -154,7 +178,7 @@ setup() { echo "1abcdef" } - source main.sh image_builder + source load_module.sh image_builder EXPECTED_CONTAINERFILE_PATH='Dockerfile' IMAGE_NAME='quay.io/foo/bar' @@ -182,7 +206,7 @@ setup() { echo "1abcdef" } - source main.sh image_builder + source load_module.sh image_builder IMAGE_NAME='quay.io/my-awesome-org/my-awesome-app' CICD_TOOLS_IMAGE_BUILDER_LABELS=("LABEL1=FOO" "LABEL2=bar") @@ -219,7 +243,7 @@ setup() { echo "1abcdef" } - source main.sh image_builder + source load_module.sh image_builder ghprbPullId="123" IMAGE_NAME="someimage" @@ -245,7 +269,7 @@ setup() { echo "1abcdef" } - source main.sh image_builder + source load_module.sh image_builder IMAGE_NAME="someimage" CONTAINERFILE_PATH='test/data/Containerfile.test' @@ -268,7 +292,7 @@ setup() { CICD_TOOLS_IMAGE_BUILDER_QUAY_USER="username1" CICD_TOOLS_IMAGE_BUILDER_QUAY_PASSWORD="secr3t" - run source main.sh image_builder + run source load_module.sh image_builder assert_success assert_output --regexp "^login.*quay.io" @@ -277,7 +301,7 @@ setup() { CICD_TOOLS_IMAGE_BUILDER_REDHAT_USER="username2" CICD_TOOLS_IMAGE_BUILDER_REDHAT_PASSWORD="secr3t" - run source main.sh image_builder + run source load_module.sh image_builder assert_success assert_output --regexp "^login.*registry.redhat.io" assert_output --partial "-u=username2" @@ -293,7 +317,7 @@ setup() { CICD_TOOLS_IMAGE_BUILDER_QUAY_USER="wrong-user" CICD_TOOLS_IMAGE_BUILDER_QUAY_PASSWORD="secr3t" - run ! source main.sh image_builder + run ! source load_module.sh image_builder assert_failure assert_output --partial "Image builder setup failed!" @@ -310,7 +334,7 @@ setup() { CICD_TOOLS_IMAGE_BUILDER_REDHAT_USER="wrong-user" CICD_TOOLS_IMAGE_BUILDER_REDHAT_PASSWORD="wrong-password" - run ! source main.sh image_builder + run ! source load_module.sh image_builder assert_failure assert_output --partial "Image builder setup failed!" @@ -325,7 +349,7 @@ setup() { echo "1abcdef" } - source main.sh image_builder + source load_module.sh image_builder IMAGE_NAME="someimage" ADDITIONAL_TAGS=("foo" "bar" "baz") @@ -353,7 +377,7 @@ setup() { echo "$@" } - source main.sh image_builder + source load_module.sh image_builder IMAGE_NAME="someimage" ADDITIONAL_TAGS=("target1" "target2" "target3") @@ -379,7 +403,7 @@ setup() { return 1 } - source main.sh image_builder + source load_module.sh image_builder IMAGE_NAME="someimage" ADDITIONAL_TAGS=("target1") @@ -402,7 +426,7 @@ setup() { echo "$@" } - source main.sh image_builder + source load_module.sh image_builder IMAGE_NAME="someimage" ADDITIONAL_TAGS=("tag1" "tag2") @@ -426,7 +450,7 @@ setup() { echo "$@" } - source main.sh image_builder + source load_module.sh image_builder IMAGE_NAME="someimage" ghprbPullId="123" @@ -452,7 +476,7 @@ setup() { return 1 } - source main.sh image_builder + source load_module.sh image_builder IMAGE_NAME="someimage" ADDITIONAL_TAGS=("target1") @@ -479,7 +503,7 @@ setup() { unset CI fi - source main.sh image_builder + source load_module.sh image_builder IMAGE_NAME="someimage" ADDITIONAL_TAGS=("target1") @@ -507,7 +531,7 @@ setup() { CI="true" fi - source main.sh image_builder + source load_module.sh image_builder IMAGE_NAME="someimage" ADDITIONAL_TAGS=("target1" "target2") @@ -536,7 +560,7 @@ setup() { echo "$@" } - source main.sh image_builder + source load_module.sh image_builder ghprbPullId='123' IMAGE_NAME="someimage" @@ -557,7 +581,7 @@ setup() { unset DOCKER_CONFIG unset CI - source main.sh image_builder + source load_module.sh image_builder assert [ -z "$DOCKER_CONFIG" ] } @@ -566,7 +590,7 @@ setup() { CI="true" unset DOCKER_CONFIG - source main.sh image_builder + source load_module.sh image_builder assert [ -n "$DOCKER_CONFIG" ] assert [ -w "${DOCKER_CONFIG}/config.json" ] } @@ -577,7 +601,7 @@ setup() { echo -n "abcdef1" } - source main.sh image_builder + source load_module.sh image_builder expected_tag="abcdef1" run cicd::image_builder::get_image_tag @@ -596,7 +620,7 @@ setup() { echo "$@" } - source main.sh image_builder + source load_module.sh image_builder export CICD_TOOLS_IMAGE_BUILDER_IMAGE_TAG='custom-tag1' export CICD_TOOLS_IMAGE_BUILDER_IMAGE_NAME='foobar' @@ -625,7 +649,7 @@ setup() { echo "$@" } - source main.sh image_builder + source load_module.sh image_builder export CICD_TOOLS_IMAGE_BUILDER_IMAGE_TAG='custom-tag1' export ghprbPullId=123 @@ -643,7 +667,7 @@ setup() { echo "$@" } - source main.sh image_builder + source load_module.sh image_builder export CICD_TOOLS_IMAGE_BUILDER_IMAGE_NAME='foobar' export CICD_TOOLS_IMAGE_BUILDER_IMAGE_TAG='custom-tag1' diff --git a/test/shared_loader.bats b/test/shared_loader.bats new file mode 100644 index 00000000..80cdc48a --- /dev/null +++ b/test/shared_loader.bats @@ -0,0 +1,126 @@ +setup() { + load "test_helper/common-setup" + _common_setup +} + +@test "Load directly results in error" { + + run ! source "src/shared/loader.sh" + assert_failure + assert_output --partial "use 'load_module.sh' to load this module" + assert_output --partial "use 'cicd::loader::load_module' to load modules" +} + +@test "Load the module multiple times only loads it once" { + CICD_LOG_DEBUG=1 + run source load_module.sh loader + assert_success + assert_output --partial "loading loader module" + assert_output --partial "loader module loaded" + assert_output --partial "loader module already loaded, skipping" + + source load_module.sh loader + run cicd::loader::load_module "loader" + assert_output --regexp ".*loader module already loaded, skipping" +} + +@test "Sets expected loaded flags" { + + assert [ -z "$CICD_LOADER_MODULE_LOADED" ] + assert [ -z "$CICD_LOADER_SCRIPTS_DIR" ] + + source load_module.sh loader + + assert [ -n "$CICD_LOADER_MODULE_LOADED" ] + assert [ -n "$CICD_LOADER_SCRIPTS_DIR" ] +} + +@test "Loading message is displayed" { + + CICD_LOG_DEBUG=1 + run source load_module.sh loader + assert_success + assert_output --partial "loading loader module" +} + +@test "Unsupported modules fail to load" { + + run ! source load_module.sh unsupported-foo-module + assert_failure 1 + assert_output --partial "Unsupported module: 'unsupported-foo-module'" +} + +@test "Default main loading sequence runs successfully" { + + CICD_LOG_DEBUG=1 + run source load_module.sh '' + assert_success + assert_output --partial "loading common module" + assert_output --partial "loading container module" +} + +@test "loading log module works successfully" { + + CICD_LOG_DEBUG=1 + run source load_module.sh log + assert_success + assert_output --partial "log module loaded" +} + +@test "loading common module works successfully" { + + CICD_LOG_DEBUG=1 + run source load_module.sh common + assert_success + assert_output --partial "common module loaded" +} + +@test "loading all work successfully" { + + CICD_LOG_DEBUG=1 + run source load_module.sh all + assert_success + assert_output --partial "log module loaded" + assert_output --partial "loader module loaded" + assert_output --partial "common module loaded" + assert_output --partial "container module loaded" + assert_output --partial "image_builder module loaded" +} + +@test "loading container helper functions work successfully" { + + podman() { + echo "podman here" + } + run ! cicd::container::cmd + assert_failure + CICD_LOG_DEBUG=1 + run source load_module.sh container + assert_success + assert_output --partial "loading container module" + source load_module.sh container + run cicd::container::cmd + assert_success + assert_output --partial "podman here" +} + +@test "Loading multiple times don't reload modules multiple times" { + + IMAGE_REPOSITORY='FOO' + assert [ -z "$CICD_COMMON_MODULE_LOADED" ] + source load_module.sh all + assert [ -n "$CICD_COMMON_MODULE_LOADED" ] + CICD_LOG_DEBUG=1 + run source load_module.sh "" + assert_success + refute_output --partial "loading common module" + run source load_module.sh all + assert_success + refute_output --partial "loading common module" + run source load_module.sh container + assert_success + refute_output --partial "loading container module" + run source load_module.sh image_builder + assert_success + refute_output --partial "loading image_builder module" +} From e9cf127047ec28ce25aaa0e42f2c0c363da88f93 Mon Sep 17 00:00:00 2001 From: Victor M Date: Thu, 19 Oct 2023 10:41:07 +0200 Subject: [PATCH 03/15] fixed container module tests --- src/load_module.sh | 5 +++- src/shared/container.sh | 20 ++++++++----- src/shared/image_builder.sh | 6 ++-- test/shared_container.bats | 57 +++++++++++++++++++++++-------------- 4 files changed, 55 insertions(+), 33 deletions(-) diff --git a/src/load_module.sh b/src/load_module.sh index d0883fb6..41b94c98 100644 --- a/src/load_module.sh +++ b/src/load_module.sh @@ -1,7 +1,10 @@ #!/bin/bash # https://stackoverflow.com/a/246128 -readonly CICD_LOADER_SCRIPTS_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)" +if [ -z "$CICD_LOADER_SCRIPTS_DIR" ]; then + readonly CICD_LOADER_SCRIPTS_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)" +fi + if ! source "${CICD_LOADER_SCRIPTS_DIR}/shared/loader.sh"; then echo "Error loading 'loader' module!" exit 1 diff --git a/src/shared/container.sh b/src/shared/container.sh index 468deaa5..cd696efe 100644 --- a/src/shared/container.sh +++ b/src/shared/container.sh @@ -16,13 +16,6 @@ CICD_CONTAINER_ENGINE='' CICD_CONTAINER_PREFER_ENGINE=${CICD_CONTAINER_PREFER_ENGINE:-} cicd::container::cmd() { - - if [[ -z "$CICD_CONTAINER_ENGINE" ]]; then - if ! cicd::container::_set_container_engine_cmd; then - return 1 - fi - fi - "$CICD_CONTAINER_ENGINE" "$@" } @@ -106,5 +99,18 @@ cicd::container::_podman_version_under_4_5_0() { [ "$(echo -en "4.5.0\n$(_podman_version)" | sort -V | head -1)" != "4.5.0" ] } +cicd::container::_module_setup() { + + if ! cicd::container::_set_container_engine_cmd; then + cicd::log::err "Error configuring a container engine!" + return 1 + fi +} + +if ! cicd::container::_module_setup; then + cicd::log::err "container module setup failed!" + return 1 +fi + cicd::log::debug "container module loaded" CICD_CONTAINER_MODULE_LOADED='true' diff --git a/src/shared/image_builder.sh b/src/shared/image_builder.sh index f6807519..6bc518b6 100644 --- a/src/shared/image_builder.sh +++ b/src/shared/image_builder.sh @@ -256,7 +256,7 @@ cicd::image_builder::get_full_image_name() { echo -n "${image_name}:${image_tag}" } -cicd::image_builder::_image_builder_setup() { +cicd::image_builder::_module_setup() { if ! cicd::image_builder::_try_log_in_to_image_registries; then cicd::log::err "Error trying to log into the image registries!" @@ -318,8 +318,8 @@ cicd::image_builder::_log_in_to_redhat_registry() { "$CICD_IMAGE_BUILDER_REDHAT_REGISTRY" } -if ! cicd::image_builder::_image_builder_setup; then - cicd::log::err "Image builder setup failed!" +if ! cicd::image_builder::_module_setup; then + cicd::log::err "image_builder module setup failed!" return 1 fi diff --git a/test/shared_container.bats b/test/shared_container.bats index ac8e8acf..2f1c03fd 100644 --- a/test/shared_container.bats +++ b/test/shared_container.bats @@ -47,7 +47,6 @@ setup() { source load_module.sh container - cicd::container::cmd --version run cicd::container::cmd --version assert_success @@ -77,7 +76,7 @@ setup() { cicd::container::cmd --version - PREFER_CONTAINER_ENGINE="docker" + CICD_CONTAINER_PREFER_ENGINE="docker" run cicd::container::cmd --version assert_success @@ -102,7 +101,8 @@ setup() { @test "if forcing docker as container engine but is emulated, keeps looking and uses podman if found" { - PREFER_CONTAINER_ENGINE="docker" + export CICD_CONTAINER_PREFER_ENGINE="docker" + export CICD_LOG_DEBUG='1' docker() { podman @@ -112,58 +112,71 @@ setup() { echo 'podman version 1' } - source load_module.sh container - run cicd::container::cmd --version + run source load_module.sh container assert_output --regexp "WARNING.*docker.*seems emulated" - assert_output --partial "podman version 1" + assert_output --partial "engine selected: podman" } @test "if no container engine found, fails" { - source load_module.sh container + # date mock required by log module + date() { + echo -n "Thu Sep 21 06:25:51 PM CEST 2023" + } + # force the test not to find the container engines OLDPATH="$PATH" PATH=':' - run ! cicd::container::cmd --version + CICD_LOADER_SCRIPTS_DIR="$(pwd)/src" + run ! source src/load_module.sh container PATH="$OLDPATH" assert_failure assert_output --partial "ERROR, no container engine found" + assert_output --partial "container module setup failed" } @test "if forcing podman but not found, uses docker if found and not emulated" { - PREFER_CONTAINER_ENGINE="podman" + export CICD_CONTAINER_PREFER_ENGINE="podman" + export CICD_LOG_DEBUG='1' docker() { echo 'docker version 1' } - source load_module.sh container + # date mock required by log module + date() { + echo -n "Thu Sep 21 06:25:51 PM CEST 2023" + } + # force the test not to find the container engines OLDPATH="$PATH" PATH=':' - run cicd::container::cmd --version + CICD_LOADER_SCRIPTS_DIR="$(pwd)/src" + run source src/load_module.sh container PATH="$OLDPATH" + assert_output --regexp "WARNING.*podman.*not present" - assert_output --partial "docker version 1" + assert_output --partial "engine selected: docker" } @test "if forcing podman but not found and docker is emulated it fails" { - PREFER_CONTAINER_ENGINE="podman" + export CICD_CONTAINER_PREFER_ENGINE="podman" + export CICD_LOADER_SCRIPTS_DIR="$(pwd)/src" docker() { echo 'podman version 1' } + # date mock required by log module date() { echo -n "Thu Sep 21 06:25:51 PM CEST 2023" } - source load_module.sh container + OLDPATH="$PATH" PATH=':' - run cicd::container::cmd --version + run ! source src/load_module.sh container PATH="$OLDPATH" - assert [ $status -eq 1 ] assert_output --regexp "WARNING.*docker seems emulated" assert_output --regexp "WARNING.*podman.*not present" assert_output --partial "no container engine found" @@ -186,7 +199,7 @@ setup() { @test "Docker can be set as preferred over podman if both are available" { - PREFER_CONTAINER_ENGINE='docker' + export CICD_CONTAINER_PREFER_ENGINE='docker' podman() { echo 'podman version 1' @@ -202,8 +215,8 @@ setup() { @test "cat is not a supported container engine" { - PREFER_CONTAINER_ENGINE='cat' - + export CICD_CONTAINER_PREFER_ENGINE='cat' + export CICD_LOG_DEBUG='1' cat() { echo "not an awesome container engine" @@ -211,9 +224,9 @@ setup() { podman() { echo 'podman version 1' } - source load_module.sh container - run cicd::container::cmd --version + run source load_module.sh container assert_success assert_output --regexp "WARNING.*'cat'.*isn't supported" - assert_output --partial "podman version 1" + assert_output --partial "finding alternative" + assert_output --partial "engine selected: podman" } From 60e72c94d7aab476a5dd23f94fbb2a1dba108c58 Mon Sep 17 00:00:00 2001 From: Victor M Date: Thu, 19 Oct 2023 11:15:51 +0200 Subject: [PATCH 04/15] Fix tests --- test/shared_image_builder.bats | 104 ++++++++++++++++++--------------- 1 file changed, 56 insertions(+), 48 deletions(-) diff --git a/test/shared_image_builder.bats b/test/shared_image_builder.bats index bdeaf270..f9c08620 100644 --- a/test/shared_image_builder.bats +++ b/test/shared_image_builder.bats @@ -18,40 +18,48 @@ setup() { assert_success } -@test "local build check" { +@test "Sets expected loaded flags" { + + assert [ -z "$CICD_COMMON_MODULE_LOADED" ] + assert [ -z "$CICD_CONTAINER_MODULE_LOADED" ] + assert [ -z "$CICD_IMAGE_BUILDER_MODULE_LOADED" ] + + source load_module.sh image_builder + + assert [ -n "$CICD_COMMON_MODULE_LOADED" ] + assert [ -n "$CICD_CONTAINER_MODULE_LOADED" ] + assert [ -n "$CICD_IMAGE_BUILDER_MODULE_LOADED" ] +} + +@test "local build check outside of CI" { unset CI - assert [ -z "$LOCAL_BUILD" ] + assert [ -z "$CICD_IMAGE_BUILDER_LOCAL_BUILD" ] assert [ -z "$CI" ] - source src/load_module.sh image_builder + source load_module.sh image_builder run cicd::image_builder::local_build assert_success - CI='true' - run cicd::image_builder::local_build - assert_failure assert_output "" - LOCAL_BUILD='true' +} + +@test "local build check in CI" { + + export CI='true' + source load_module.sh image_builder run cicd::image_builder::local_build - assert_output "" - assert_success - unset LOCAL_BUILD - run ! cicd::image_builder::local_build - assert_output "" assert_failure + assert_output "" } -@test "Sets expected loaded flags" { - - assert [ -z "$CICD_COMMON_MODULE_LOADED" ] - assert [ -z "$CICD_CONTAINER_ENGINE_MODULE_LOADED" ] - assert [ -z "$CICD_IMAGE_BUILDER_MODULE_LOADED" ] +@test "local build check forced with variable" { + export CI='true' + export CICD_IMAGE_BUILDER_LOCAL_BUILD='true' source load_module.sh image_builder - - assert [ -n "$CICD_COMMON_MODULE_LOADED" ] - assert [ -n "$CICD_CONTAINER_ENGINE_MODULE_LOADED" ] - assert [ -n "$CICD_IMAGE_BUILDER_MODULE_LOADED" ] + run cicd::image_builder::local_build + assert_success + assert_output "" } @test "Image tag outside of a change request context" { @@ -209,11 +217,11 @@ setup() { source load_module.sh image_builder IMAGE_NAME='quay.io/my-awesome-org/my-awesome-app' - CICD_TOOLS_IMAGE_BUILDER_LABELS=("LABEL1=FOO" "LABEL2=bar") - CICD_TOOLS_IMAGE_BUILDER_ADDITIONAL_TAGS=("test1" "additional-label-2" "security") - CICD_TOOLS_IMAGE_BUILDER_BUILD_ARGS=("BUILD_ARG1=foobar" "BUILD_ARG2=bananas") - CICD_TOOLS_IMAGE_BUILDER_BUILD_CONTEXT='another/context' - CICD_TOOLS_IMAGE_BUILDER_CONTAINERFILE_PATH='test/data/Containerfile.test' + CICD_IMAGE_BUILDER_LABELS=("LABEL1=FOO" "LABEL2=bar") + CICD_IMAGE_BUILDER_ADDITIONAL_TAGS=("test1" "additional-label-2" "security") + CICD_IMAGE_BUILDER_BUILD_ARGS=("BUILD_ARG1=foobar" "BUILD_ARG2=bananas") + CICD_IMAGE_BUILDER_BUILD_CONTEXT='another/context' + CICD_IMAGE_BUILDER_CONTAINERFILE_PATH='test/data/Containerfile.test' run cicd::image_builder::build @@ -289,8 +297,8 @@ setup() { echo "$@" } - CICD_TOOLS_IMAGE_BUILDER_QUAY_USER="username1" - CICD_TOOLS_IMAGE_BUILDER_QUAY_PASSWORD="secr3t" + CICD_IMAGE_BUILDER_QUAY_USER="username1" + CICD_IMAGE_BUILDER_QUAY_PASSWORD="secr3t" run source load_module.sh image_builder @@ -298,8 +306,8 @@ setup() { assert_output --regexp "^login.*quay.io" assert_output --partial "-u=username1" - CICD_TOOLS_IMAGE_BUILDER_REDHAT_USER="username2" - CICD_TOOLS_IMAGE_BUILDER_REDHAT_PASSWORD="secr3t" + CICD_IMAGE_BUILDER_REDHAT_USER="username2" + CICD_IMAGE_BUILDER_REDHAT_PASSWORD="secr3t" run source load_module.sh image_builder assert_success @@ -314,13 +322,13 @@ setup() { return 1 } - CICD_TOOLS_IMAGE_BUILDER_QUAY_USER="wrong-user" - CICD_TOOLS_IMAGE_BUILDER_QUAY_PASSWORD="secr3t" + CICD_IMAGE_BUILDER_QUAY_USER="wrong-user" + CICD_IMAGE_BUILDER_QUAY_PASSWORD="secr3t" run ! source load_module.sh image_builder assert_failure - assert_output --partial "Image builder setup failed!" + assert_output --partial "image_builder module setup failed!" assert_output --partial "Error logging in to Quay.io" } @@ -331,13 +339,13 @@ setup() { return 1 } - CICD_TOOLS_IMAGE_BUILDER_REDHAT_USER="wrong-user" - CICD_TOOLS_IMAGE_BUILDER_REDHAT_PASSWORD="wrong-password" + CICD_IMAGE_BUILDER_REDHAT_USER="wrong-user" + CICD_IMAGE_BUILDER_REDHAT_PASSWORD="wrong-password" run ! source load_module.sh image_builder assert_failure - assert_output --partial "Image builder setup failed!" + assert_output --partial "image_builder module setup failed!" assert_output --partial "Error logging in to Red Hat Registry" } @@ -607,8 +615,8 @@ setup() { run cicd::image_builder::get_image_tag assert_output "$expected_tag" - export CICD_TOOLS_IMAGE_BUILDER_IMAGE_TAG='some-cool-tag' - expected_tag="$CICD_TOOLS_IMAGE_BUILDER_IMAGE_TAG" + export CICD_IMAGE_BUILDER_IMAGE_TAG='some-cool-tag' + expected_tag="$CICD_IMAGE_BUILDER_IMAGE_TAG" run cicd::image_builder::get_image_tag assert_output "$expected_tag" } @@ -622,11 +630,11 @@ setup() { source load_module.sh image_builder - export CICD_TOOLS_IMAGE_BUILDER_IMAGE_TAG='custom-tag1' - export CICD_TOOLS_IMAGE_BUILDER_IMAGE_NAME='foobar' + export CICD_IMAGE_BUILDER_IMAGE_TAG='custom-tag1' + export CICD_IMAGE_BUILDER_IMAGE_NAME='foobar' export CONTAINERFILE_PATH='test/data/Containerfile.test' - expected_tag="$CICD_TOOLS_IMAGE_BUILDER_IMAGE_TAG" + expected_tag="$CICD_IMAGE_BUILDER_IMAGE_TAG" run cicd::image_builder::get_image_tag assert_output "$expected_tag" @@ -635,7 +643,7 @@ setup() { assert_output --regexp "^build.*-t foobar:custom-tag1" - export CICD_TOOLS_IMAGE_BUILDER_IMAGE_TAG='custom-tag2' + export CICD_IMAGE_BUILDER_IMAGE_TAG='custom-tag2' run cicd::image_builder::build assert_output --regexp "^build.*-t foobar:custom-tag2" @@ -651,10 +659,10 @@ setup() { source load_module.sh image_builder - export CICD_TOOLS_IMAGE_BUILDER_IMAGE_TAG='custom-tag1' + export CICD_IMAGE_BUILDER_IMAGE_TAG='custom-tag1' export ghprbPullId=123 - expected_tag="pr-123-$CICD_TOOLS_IMAGE_BUILDER_IMAGE_TAG" + expected_tag="pr-123-$CICD_IMAGE_BUILDER_IMAGE_TAG" run cicd::image_builder::get_image_tag assert_output "$expected_tag" @@ -669,13 +677,13 @@ setup() { source load_module.sh image_builder - export CICD_TOOLS_IMAGE_BUILDER_IMAGE_NAME='foobar' - export CICD_TOOLS_IMAGE_BUILDER_IMAGE_TAG='custom-tag1' + export CICD_IMAGE_BUILDER_IMAGE_NAME='foobar' + export CICD_IMAGE_BUILDER_IMAGE_TAG='custom-tag1' export CONTAINERFILE_PATH='test/data/Containerfile.test' - export CICD_TOOLS_IMAGE_BUILDER_ADDITIONAL_TAGS=("extra1" "extra2") + export CICD_IMAGE_BUILDER_ADDITIONAL_TAGS=("extra1" "extra2") export ghprbPullId=123 - expected_tag="pr-123-$CICD_TOOLS_IMAGE_BUILDER_IMAGE_TAG" + expected_tag="pr-123-$CICD_IMAGE_BUILDER_IMAGE_TAG" run cicd::image_builder::get_image_tag assert_output "$expected_tag" From 4af3a9d2dd5c8d25e5177ed058e3a73c18e74cac Mon Sep 17 00:00:00 2001 From: Victor M Date: Thu, 19 Oct 2023 11:42:44 +0200 Subject: [PATCH 05/15] fix e2e tests --- test/e2e/test_bootstrap_script_main.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/e2e/test_bootstrap_script_main.sh b/test/e2e/test_bootstrap_script_main.sh index 9a0a768c..58b156fe 100755 --- a/test/e2e/test_bootstrap_script_main.sh +++ b/test/e2e/test_bootstrap_script_main.sh @@ -11,7 +11,7 @@ docker() { load_cicd_helper_functions() { - local PREFER_CONTAINER_ENGINE='docker' + local CICD_CONTAINER_PREFER_ENGINE='docker' local LIBRARY_TO_LOAD=${1:-all} local CICD_TOOLS_URL="https://raw.githubusercontent.com/RedHatInsights/cicd-tools/main/src/bootstrap.sh" source <(curl -sSL "$CICD_TOOLS_URL") "$LIBRARY_TO_LOAD" From f951a72fb1c9425b08ecccad4404cf1e6f841d3a Mon Sep 17 00:00:00 2001 From: Victor M Date: Thu, 19 Oct 2023 11:45:03 +0200 Subject: [PATCH 06/15] fix e2e tests --- src/bootstrap.sh | 3 ++- ..._e2e_load_library_from_bootstrap_script.sh | 22 ++++++++++++------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/bootstrap.sh b/src/bootstrap.sh index d03361aa..aed48eab 100644 --- a/src/bootstrap.sh +++ b/src/bootstrap.sh @@ -4,6 +4,7 @@ CICD_BOOTSTRAP_REPO_ORG="${CICD_BOOTSTRAP_REPO_ORG:-RedHatInsights}" CICD_BOOTSTRAP_REPO_BRANCH="${CICD_BOOTSTRAP_REPO_BRANCH:-main}" CICD_BOOTSTRAP_ROOTDIR="${CICD_BOOTSTRAP_ROOTDIR:-.cicd_tools}" CICD_BOOTSTRAP_SKIP_CLEANUP=${CICD_BOOTSTRAP_SKIP_CLEANUP:-} +CICD_BOOTSTRAP_SKIP_GIT_CLONE=${CICD_BOOTSTRAP_SKIP_GIT_CLONE:-} cicd::bootstrap::clone_cicd_tools_repo() { @@ -24,7 +25,7 @@ cicd::bootstrap::_delete_rootdir() { cicd::bootstrap::cleanup() { cicd::bootstrap::_delete_rootdir unset cicd::bootstrap::clone_cicd_tools_repo cicd::bootstrap::_delete_rootdir cicd::bootstrap::cleanup - unset CICD_BOOTSTRAP_REPO_ORG CICD_BOOTSTRAP_REPO_BRANCH CICD_BOOTSTRAP_ROOTDIR CICD_BOOTSTRAP_SKIP_CLEANUP + unset CICD_BOOTSTRAP_REPO_ORG CICD_BOOTSTRAP_REPO_BRANCH CICD_BOOTSTRAP_ROOTDIR CICD_BOOTSTRAP_SKIP_CLEANUP CICD_BOOTSTRAP_SKIP_GIT_CLONE } if [ -z "$CICD_BOOTSTRAP_SKIP_GIT_CLONE" ]; then diff --git a/test/e2e/test_e2e_load_library_from_bootstrap_script.sh b/test/e2e/test_e2e_load_library_from_bootstrap_script.sh index 0f9c1592..7595f58c 100755 --- a/test/e2e/test_e2e_load_library_from_bootstrap_script.sh +++ b/test/e2e/test_e2e_load_library_from_bootstrap_script.sh @@ -11,21 +11,27 @@ docker() { load_cicd_helper_functions() { - local PREFER_CONTAINER_ENGINE='docker' + local CICD_CONTAINER_PREFER_ENGINE='docker' local LIBRARY_TO_LOAD=${1:-all} if [ "CI" != "true" ]; then - CICD_TOOLS_ROOTDIR=. - CICD_TOOLS_SKIP_GIT_CLONE=1 - CICD_TOOLS_SKIP_CLEANUP=1 + CICD_BOOTSTRAP_ROOTDIR=. + CICD_BOOTSTRAP_SKIP_GIT_CLONE=1 + CICD_BOOTSTRAP_SKIP_CLEANUP=1 source src/bootstrap.sh "$LIBRARY_TO_LOAD" else if [ "$GITHUB_HEAD_REF" != "main" ]; then - CICD_TOOLS_ROOTDIR=. - source src/bootstrap.sh "$LIBRARY_TO_LOAD" - else - source <(curl -sSL "$CICD_TOOLS_URL") "$LIBRARY_TO_LOAD" + CICD_BOOTSTRAP_SKIP_GIT_CLONE=1 + CICD_BOOTSTRAP_ROOTDIR=. fi + source src/bootstrap.sh "$LIBRARY_TO_LOAD" +# if [ "$GITHUB_HEAD_REF" != "main" ]; then +# CICD_BOOTSTRAP_ROOTDIR=. +# CICD_BOOTSTRAP_SKIP_GIT_CLONE=1 +# source src/bootstrap.sh "$LIBRARY_TO_LOAD" +# else +# source <(curl -sSL "$CICD_URL") "$LIBRARY_TO_LOAD" +# fi fi # required to persist container preferrence From c8f81df951edce8e6b88a48c57f3135c26efdc1e Mon Sep 17 00:00:00 2001 From: Victor M Date: Thu, 19 Oct 2023 15:12:48 +0200 Subject: [PATCH 07/15] trying to fix e2e tests --- .../e2e/test_e2e_load_library_from_bootstrap_script.sh | 10 ---------- test/e2e/test_e2e_load_library_from_function.sh | 5 +++-- test/e2e/test_e2e_load_library_locally.sh | 8 ++------ 3 files changed, 5 insertions(+), 18 deletions(-) diff --git a/test/e2e/test_e2e_load_library_from_bootstrap_script.sh b/test/e2e/test_e2e_load_library_from_bootstrap_script.sh index 7595f58c..5ed3d6d0 100755 --- a/test/e2e/test_e2e_load_library_from_bootstrap_script.sh +++ b/test/e2e/test_e2e_load_library_from_bootstrap_script.sh @@ -25,17 +25,7 @@ load_cicd_helper_functions() { CICD_BOOTSTRAP_ROOTDIR=. fi source src/bootstrap.sh "$LIBRARY_TO_LOAD" -# if [ "$GITHUB_HEAD_REF" != "main" ]; then -# CICD_BOOTSTRAP_ROOTDIR=. -# CICD_BOOTSTRAP_SKIP_GIT_CLONE=1 -# source src/bootstrap.sh "$LIBRARY_TO_LOAD" -# else -# source <(curl -sSL "$CICD_URL") "$LIBRARY_TO_LOAD" -# fi fi - - # required to persist container preferrence - cicd::container::cmd --version } load_cicd_helper_functions container diff --git a/test/e2e/test_e2e_load_library_from_function.sh b/test/e2e/test_e2e_load_library_from_function.sh index 0dfc0e05..b343282d 100755 --- a/test/e2e/test_e2e_load_library_from_function.sh +++ b/test/e2e/test_e2e_load_library_from_function.sh @@ -12,12 +12,13 @@ docker() { load_common_helper_cicd_tools() { - local PREFER_CONTAINER_ENGINE='docker' + local CICD_CONTAINER_PREFER_ENGINE='docker' local LIBRARY_TO_LOAD=${1:-all} - local MAIN_SCRIPT='./src/main.sh' + local MAIN_SCRIPT='./src/load_module.sh' source "$MAIN_SCRIPT" "$LIBRARY_TO_LOAD" cicd::container::cmd --version } + load_common_helper_cicd_tools container EXPECTED_OUTPUT=$(cicd::container::cmd --version) diff --git a/test/e2e/test_e2e_load_library_locally.sh b/test/e2e/test_e2e_load_library_locally.sh index a05dc658..bf87b199 100755 --- a/test/e2e/test_e2e_load_library_locally.sh +++ b/test/e2e/test_e2e_load_library_locally.sh @@ -9,14 +9,10 @@ docker() { echo "Docker version 99" } -PREFER_CONTAINER_ENGINE='docker' +export CICD_CONTAINER_PREFER_ENGINE='docker' LIBRARY_TO_LOAD=${1:-all} -MAIN_SCRIPT='./src/main.sh' - +MAIN_SCRIPT='./src/load_module.sh' source "$MAIN_SCRIPT" "$LIBRARY_TO_LOAD" - -cicd::container::cmd --version >/dev/null - EXPECTED_OUTPUT=$(cicd::container::cmd --version) # Assert there's an actual output From 9c4d3b57a0c79cea9b2a5e621a012dcabaafc590 Mon Sep 17 00:00:00 2001 From: Victor M Date: Thu, 19 Oct 2023 15:33:46 +0200 Subject: [PATCH 08/15] Fix Shellcheck tips --- src/load_module.sh | 8 +++++--- src/shared/common.sh | 2 ++ src/shared/container.sh | 2 ++ src/shared/image_builder.sh | 2 ++ src/shared/loader.sh | 7 ++++++- src/shared/log.sh | 4 ++++ 6 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/load_module.sh b/src/load_module.sh index 41b94c98..c008214a 100644 --- a/src/load_module.sh +++ b/src/load_module.sh @@ -2,12 +2,14 @@ # https://stackoverflow.com/a/246128 if [ -z "$CICD_LOADER_SCRIPTS_DIR" ]; then - readonly CICD_LOADER_SCRIPTS_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)" + CICD_LOADER_SCRIPTS_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)" fi +readonly CICD_LOADER_SCRIPTS_DIR + if ! source "${CICD_LOADER_SCRIPTS_DIR}/shared/loader.sh"; then - echo "Error loading 'loader' module!" - exit 1 + echo "Error loading 'loader' module!" + exit 1 fi # TODO: undo all loader module stuff diff --git a/src/shared/common.sh b/src/shared/common.sh index 7e512c57..7adda630 100644 --- a/src/shared/common.sh +++ b/src/shared/common.sh @@ -1,3 +1,5 @@ +#!/bin/bash + # Common functions that are shared across the different modules if [[ -n "$CICD_COMMON_MODULE_LOADED" ]]; then diff --git a/src/shared/container.sh b/src/shared/container.sh index cd696efe..65115a55 100644 --- a/src/shared/container.sh +++ b/src/shared/container.sh @@ -1,3 +1,5 @@ +#!/bin/bash + # container engine helper functions to handle both podman and docker commands if [[ -n "$CICD_CONTAINER_MODULE_LOADED" ]]; then diff --git a/src/shared/image_builder.sh b/src/shared/image_builder.sh index 6bc518b6..a9595c88 100644 --- a/src/shared/image_builder.sh +++ b/src/shared/image_builder.sh @@ -1,3 +1,5 @@ +#!/bin/bash + # helper functions to build container images if [[ -n "$CICD_IMAGE_BUILDER_MODULE_LOADED" ]]; then diff --git a/src/shared/loader.sh b/src/shared/loader.sh index 8f9a6549..30dd7727 100644 --- a/src/shared/loader.sh +++ b/src/shared/loader.sh @@ -1,3 +1,7 @@ +#!/bin/bash + +# Internal module to provide module loading helper functions + CICD_LOADER_MODULE_LOADED=${CICD_LOADER_MODULE_LOADED:-1} if [[ "$CICD_LOADER_MODULE_LOADED" -eq 0 ]]; then @@ -11,6 +15,7 @@ if [[ -z "$CICD_LOADER_SCRIPTS_DIR" ]]; then return 1 fi +# shellcheck source=src/shared/log.sh if ! source "${CICD_LOADER_SCRIPTS_DIR}/shared/log.sh"; then echo "Error loading 'log' module!" return 1 @@ -44,7 +49,7 @@ cicd::loader::_load_all() { } cicd::loader::_load_log_module() { - # shellcheck source=src/shared/common.sh + # shellcheck source=src/shared/log.sh source "${CICD_LOADER_SCRIPTS_DIR}/shared/log.sh" } diff --git a/src/shared/log.sh b/src/shared/log.sh index 3c511b57..85db13ca 100644 --- a/src/shared/log.sh +++ b/src/shared/log.sh @@ -1,3 +1,7 @@ +#!/bin/bash + +# logging helper funtions + CICD_LOG_DEBUG=${CICD_LOG_DEBUG:-} if [[ -n "$CICD_LOG_MODULE_LOADED" ]]; then From 02e2bbbbf4ba12c00d8ee0363651851d25ff4d3b Mon Sep 17 00:00:00 2001 From: Victor M Date: Thu, 19 Oct 2023 15:44:28 +0200 Subject: [PATCH 09/15] fix shellcheck references --- src/bootstrap.sh | 4 ++-- src/load_module.sh | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/bootstrap.sh b/src/bootstrap.sh index aed48eab..21aa24c2 100644 --- a/src/bootstrap.sh +++ b/src/bootstrap.sh @@ -18,7 +18,7 @@ cicd::bootstrap::clone_cicd_tools_repo() { } cicd::bootstrap::_delete_rootdir() { - cicd::debug "Removing existing CICD tools directory: '${CICD_BOOTSTRAP_ROOTDIR}'" + cicd::log::debug "Removing existing CICD tools directory: '${CICD_BOOTSTRAP_ROOTDIR}'" rm -rf "${CICD_BOOTSTRAP_ROOTDIR}" } @@ -35,7 +35,7 @@ if [ -z "$CICD_BOOTSTRAP_SKIP_GIT_CLONE" ]; then fi fi -# shellcheck source=src/main.sh +# shellcheck source=src/load_module.sh source "$CICD_BOOTSTRAP_ROOTDIR/src/load_module.sh" "$@" || exit 1 if [[ -z "$CICD_BOOTSTRAP_SKIP_CLEANUP" ]] && ! cicd::bootstrap::cleanup; then echo "couldn't perform cicd tools cleanup!" diff --git a/src/load_module.sh b/src/load_module.sh index c008214a..77c5854e 100644 --- a/src/load_module.sh +++ b/src/load_module.sh @@ -7,10 +7,10 @@ fi readonly CICD_LOADER_SCRIPTS_DIR +# shellcheck source=src/shared/loader.sh if ! source "${CICD_LOADER_SCRIPTS_DIR}/shared/loader.sh"; then echo "Error loading 'loader' module!" exit 1 fi -# TODO: undo all loader module stuff cicd::loader::load_module "$1" From 7b6915483fa3b2c1dbb800dd483c71c55ee6288e Mon Sep 17 00:00:00 2001 From: Victor M Date: Thu, 26 Oct 2023 13:34:46 +0200 Subject: [PATCH 10/15] Updated documentation --- README.md | 35 +++-- docs/cicd_tools/common.md | 69 +++++++++ .../{container_lib.md => container.md} | 36 +++-- docs/cicd_tools/image_builder.md | 146 +++++++++++++----- docs/cicd_tools/loader.md | 40 +++++ docs/cicd_tools/log.md | 61 ++++++++ 6 files changed, 324 insertions(+), 63 deletions(-) create mode 100644 docs/cicd_tools/common.md rename docs/cicd_tools/{container_lib.md => container.md} (81%) create mode 100644 docs/cicd_tools/loader.md create mode 100644 docs/cicd_tools/log.md diff --git a/README.md b/README.md index 0dde420d..c7d89a07 100644 --- a/README.md +++ b/README.md @@ -31,13 +31,26 @@ file [here](examples/unit_test_example.sh). The collection of helper libraries are expected to be loaded using the provided [src/bootstrap.sh](bootstrap) script. -Currently, there are 2 supported modules. To read more about what each of the modules provide, +The bootstrap's script responsibilities are to get a local copy of this repository, initialize +the `loader` module, and load + +The `loader` module is a special one, and it's main function is to serve as an entrypoint to the +library. +It provides the `cicd::loader::load_module` functions, that must be used to load the different +modules. + +This is all handed by the boostrap script + +To read more about what each of the modules provide, please read through the documents linked through the library IDs in the following table: -| Library ID | Description | -|-------------------------------------------------------|----------------------------------------------------------------------------| -| [container](docs/cicd_tools/container_lib.md) | Provides wrapper functions for invoking container engine agnostic commands | -| [image_builder](docs/cicd_tools/image_builder_lib.md) | Provides helper functions to simplify the image building process | +| Library ID | Description | +|-------------------------------------|----------------------------------------------------------------------------| +| [container](docs/cicd_tools/container.md) | Provides wrapper functions for invoking container engine agnostic commands | +| [image_builder](docs/cicd_tools/image_builder.md) | Provides helper functions to simplify the image building process | +| [common](docs/cicd_tools/common.md) | Provides helper functions to simplify the image building process | +| [log](docs/cicd_tools/log.md) | Provides wrapper functions for invoking container engine agnostic commands | +| [loader](docs/cicd_tools/loader.md) | Provides wrapper functions for invoking container engine agnostic commands | ### How to use the helper libraries @@ -46,12 +59,13 @@ centralized way. This should be helpful to reduce the amount of code needed to w operations in a pipeline for routine tasks, such as operating with containers or building container images. -The [src/main.sh](main.sh) script is the main entrypoint and should be used to load the modules +The [src/load_module.sh](load_module.sh) script is the main entrypoint and should be used to load +the modules included in this library. This script requires all the other scripts available in a local directory following the same structure in this repository. -To use any of the provided libraries, you must source the [src/main.sh](main.sh) script -and pass the unique library ID to be loaded as a parameter. +To use any of the provided libraries, you must source the [src/load_module.sh](load_module.sh) +script and pass the unique library ID to be loaded as a parameter. There's two different approaches for loading these scripts, depending on if you're a contributor or an end user. @@ -61,13 +75,14 @@ an end user. This is the intended way when developing new modules for this library. The recommended approach for contributing is to create a new fork and then open a pull request against the `main` branch. -When working with a local copy of the repository, you should source the [src/main.sh](main.sh) +When working with a local copy of the repository, you should source +the [src/load_module.sh](load_module.sh) script directly. #### Using the library from other scripts There is an existing helper script named [src/bootstrap.sh](bootstrap) to help with sourcing the -[src/main.sh](main.sh) script if you're not contributing to this repo. +[src/load_module.sh](load_module.sh) script if you're not contributing to this repo. **This is the intended way of using this library from external projects**. diff --git a/docs/cicd_tools/common.md b/docs/cicd_tools/common.md new file mode 100644 index 00000000..c9d42372 --- /dev/null +++ b/docs/cicd_tools/common.md @@ -0,0 +1,69 @@ +# Common module + +This module exposes helper functions that may be used (and shared) across all modules, or that don't +serve a specific module's purpose to fit within it. + +## Definition + +The module ID is `common` + +All functions exposed by this module will use the namespaced prefix: + +``` +cicd::common:: +``` + +## Usage + +Use the `common` id to load the module + +``` +CICD_TOOLS_URL="https://raw.githubusercontent.com/RedHatInsights/cicd-tools/main/src/bootstrap.sh" +# shellcheck source=/dev/null +source <(curl -sSL "$CICD_TOOLS_URL") common +``` + +This should load the function: + +``` +cicd::container::cmd +``` + +which serves as a wrapper to the container engine of choice. You should be able to safely replace +your invocations to `docker` or `podman` commands with this function + +### Container engine detection and order choice + +The library favors `podman` in case both container engines are available in the user's PATH. + +### Override container engine selection + +If you want to force the library to stick to a preferred container engine, you can do so by setting +the `CICD_CONTAINER_PREFER_ENGINE` to one of the supported container engines available * +*before** loading the library. + +The container engine is selected when the library is loaded and is not possible to update it +afterward. + +``` +export CICD_CONTAINER_PREFER_ENGINE=docker +``` + +**Please note:** If you set your preference to be `docker` and the library detects that `docker` is +actually mocked as a wrapper to `podman` the library will ignore the preference and will try to +set `podman` as the selected container engine. + +## Dependencies + +This module requires one of the supported container engines to be present in the session's `PATH`. +The currently supported container engines are: + +- `docker` +- `podman` + +### Public functions + +#### cicd::container::cmd + +Should be used instead of a container engine command to support container-engine agnostic commands ( +among the supported container engines) \ No newline at end of file diff --git a/docs/cicd_tools/container_lib.md b/docs/cicd_tools/container.md similarity index 81% rename from docs/cicd_tools/container_lib.md rename to docs/cicd_tools/container.md index 4e8c6f0d..4b7250a2 100644 --- a/docs/cicd_tools/container_lib.md +++ b/docs/cicd_tools/container.md @@ -13,19 +13,10 @@ All functions exposed by this module will use the namespaced prefix: cicd::container:: ``` -## Dependencies - -None - -### Public functions - -#### cicd::container::cmd - -Should be used instead of a container engine command to support container-engine agnostic commands (among the supported container engines) - -## How to use +## Usage -Use the `container` id to load the module +Use the `container` id to load the module via the `loader` module, using either the `load_module.sh` +or the `bootstrap.sh` script ``` CICD_TOOLS_URL="https://raw.githubusercontent.com/RedHatInsights/cicd-tools/main/src/bootstrap.sh" @@ -42,10 +33,6 @@ cicd::container::cmd which serves as a wrapper to the container engine of choice. You should be able to safely replace your invocations to `docker` or `podman` commands with this function -### Supported container engines - -We currently support `docker` and `podman` - ### Container engine detection and order choice The library favors `podman` in case both container engines are available in the user's PATH. @@ -57,7 +44,7 @@ the `CICD_TOOLS_CONTAINER_PREFER_ENGINE` to one of the supported container engin *before** loading the library. The container engine is selected when the library is loaded and is not possible to update it -afterwards. +afterward. ``` export CICD_TOOLS_CONTAINER_PREFER_ENGINE=docker @@ -66,3 +53,18 @@ export CICD_TOOLS_CONTAINER_PREFER_ENGINE=docker **Please note:** If you set your preference to be `docker` and the library detects that `docker` is actually mocked as a wrapper to `podman` the library will ignore the preference and will try to set `podman` as the selected container engine. + +## Dependencies + +This module requires one of the supported container engines to be present in the session's `PATH`. +The currently supported container engines are: + +- `docker` +- `podman` + +### Public functions + +#### cicd::container::cmd + +Should be used instead of a container engine command to support container-engine agnostic commands ( +among the supported container engines) \ No newline at end of file diff --git a/docs/cicd_tools/image_builder.md b/docs/cicd_tools/image_builder.md index 49b99734..d3145e77 100644 --- a/docs/cicd_tools/image_builder.md +++ b/docs/cicd_tools/image_builder.md @@ -13,45 +13,35 @@ All functions exposed by this module will use the namespaced prefix: cicd::image_builder:: ``` -### Public functions - -#### cicd::container::build_and_push +## Usage -This will build a container image and conditionally push it if not in a change request context using -the provided configuration - -#### cicd::container::build - -This will build a container image using the provided configuration - -#### cicd::container::tag - -This function will create local tags using the provided configuration - -#### cicd::container::push - -This function will push all configured tags +Use the `image_builder` id to load the module -## How to use +``` +CICD_TOOLS_URL="https://raw.githubusercontent.com/RedHatInsights/cicd-tools/main/src/bootstrap.sh" +# shellcheck source=/dev/null +source <(curl -sSL "$CICD_TOOLS_URL") image_builder +``` This module uses the following variables to configure the image building requirements: -| Variable name | Description | Default value | Type | Mandatory | -|---------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|----------------------|--------|-----------| -| CICD_TOOLS_IMAGE_BUILDER_IMAGE_NAME | The Image name to be used, in format: 'imageregistry/org/image_name' | `""` | string | Yes | -| CICD_TOOLS_IMAGE_BUILDER_IMAGE_TAG | The main image tag to be used. If not provided the 7 first chars of the current repository's git commit hash will be used instead | `""` | string | No | -| CICD_TOOLS_IMAGE_BUILDER_ADDITIONAL_TAGS | The additional tags (if any) to be created in array format: ("tag1" "tag2" "latest") | `()` | Array | No | -| CICD_TOOLS_IMAGE_BUILDER_LABELS | The labels (if any) to add to the image being built in array format: ("label1=Value1" "label2=value2") | `()` | Array | No | -| CICD_TOOLS_IMAGE_BUILDER_BUILD_ARGS | The build arguments to be provided when building the image (if any) in array format: ("buildarg1=Value1" "buildarg2=value2") | `()` | Array | No | -| CICD_TOOLS_IMAGE_BUILDER_BUILD_CONTEXT | The build context path to use when building the image. | `.` | None | string | No| -| CICD_TOOLS_IMAGE_BUILDER_CONTAINERFILE_PATH | The Containerfile path to use when building the image. | `Dockerfile` | None | string | No| -| CICD_TOOLS_IMAGE_BUILDER_QUAY_EXPIRE_TIME | The expire time value to be set for Quay expires labels, used only in change request contexts. | `3d` | string | No | -| CICD_TOOLS_IMAGE_BUILDER_QUAY_USER | The username to use when logging in to Quay.io | `$QUAY_USER` | string | No | -| CICD_TOOLS_IMAGE_BUILDER_QUAY_PASSWORD | The password to use when logging in to Quay.io | `$QUAY_TOKEN` | string | No | -| CICD_TOOLS_IMAGE_BUILDER_REDHAT_USER | The username to use when logging in to the Red Hat Registry | `$RH_REGISTRY_USER` | string | No | -| CICD_TOOLS_IMAGE_BUILDER_REDHAT_PASSWORD | The password to use when logging in to the Red Hat Registry | `$RH_REGISTRY_TOKEN` | string | No | - -The only required variable is `CICD_TOOLS_IMAGE_BUILDER_IMAGE_NAME`, the rest of them are optional +| Variable name | Description | Default value | Type | Mandatory | +|---------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|----------------------|--------|-----------| +| CICD_IMAGE_BUILDER_IMAGE_NAME | The Image name to be used, in format: 'imageregistry/org/image_name' | `""` | string | Yes | +| CICD_IMAGE_BUILDER_LOCAL_BUILD | Override local build detection | `$LOCAL_BUILD` | string | No | +| CICD_IMAGE_BUILDER_IMAGE_TAG | The main image tag to be used. If not provided the 7 first chars of the current repository's git commit hash will be used instead | `""` | string | No | +| CICD_IMAGE_BUILDER_ADDITIONAL_TAGS | The additional tags (if any) to be created in array format: ("tag1" "tag2" "latest") | `()` | Array | No | +| CICD_IMAGE_BUILDER_LABELS | The labels (if any) to add to the image being built in array format: ("label1=Value1" "label2=value2") | `()` | Array | No | +| CICD_IMAGE_BUILDER_BUILD_ARGS | The build arguments to be provided when building the image (if any) in array format: ("buildarg1=Value1" "buildarg2=value2") | `()` | Array | No | +| CICD_IMAGE_BUILDER_BUILD_CONTEXT | The build context path to use when building the image. | `.` | None | string | No| +| CICD_IMAGE_BUILDER_CONTAINERFILE_PATH | The Containerfile path to use when building the image. | `Dockerfile` | None | string | No| +| CICD_IMAGE_BUILDER_QUAY_EXPIRE_TIME | The expire time value to be set for Quay expires labels, used only in change request contexts. | `3d` | string | No | +| CICD_IMAGE_BUILDER_QUAY_USER | The username to use when logging in to Quay.io | `$QUAY_USER` | string | No | +| CICD_IMAGE_BUILDER_QUAY_PASSWORD | The password to use when logging in to Quay.io | `$QUAY_TOKEN` | string | No | +| CICD_IMAGE_BUILDER_REDHAT_USER | The username to use when logging in to the Red Hat Registry | `$RH_REGISTRY_USER` | string | No | +| CICD_IMAGE_BUILDER_REDHAT_PASSWORD | The password to use when logging in to the Red Hat Registry | `$RH_REGISTRY_TOKEN` | string | No | + +The only required variable is `CICD_IMAGE_BUILDER_IMAGE_NAME`, the rest of them are optional and should not be needed for the majority of use cases as the provided default values should be enough. @@ -81,9 +71,93 @@ expected to be created, considering if: | YES | awesome-tag | N/A | `pr-${BUILD_ID}-awesome-tag` | | YES | awesome-tag | `("tag1" "latest")` | `pr-${BUILD_ID}-awesome-tag pr-${BUILD_ID}-tag1 pr-${BUILD_ID}-latest` | -**Important:** This module will **only** try to authenticate against either registry if it finds the -variables present in the environment. In addition, in a CI context the `DOCKER_CONFIG` file will be +## Override the default image tag + +If for some reason the "GIT_SHA" default value, the `CICD_IMAGE_BUILDER_IMAGE_TAG` can be used to override it. +The value of the default tag will still be modified in change requests context though, to include the `buildID` and the `pr` prefix + +Example: + +``` +export CICD_IMAGE_BUILDER_IMAGE_TAG=awesome-tag +echo $(cicd::image_builder::get_image_tag) +``` +Will output the value `awesome-tag` or the `pr-123-awaesome-tag` value for Change Request contexts + + +## Dependencies + +This module depends on the [container](/docs/cicd_tools/container.md) module to build images. + +**NOTE**: About logging in against image registries: + +This module has a setup phase that creates a unique container config file to handle container +registry credentials + +This module will **only** try to authenticate against an image registry if it finds the variables +present in the environment. In addition, in a CI context the `DOCKER_CONFIG` file will be recreated using a temporary file, so it is expected that the required registry credentials are provided as environment variables. +### Public functions + +#### cicd::container::build_and_push + +This will build a container image and conditionally push it if not in a change request context using +the provided configuration + +#### cicd::container::build + +This will build a container image using the provided configuration + +#### cicd::container::tag + +This function will create local tags using the provided configuration + +#### cicd::container::push + +This function will push all configured tags + +#### cicd::image_builder::local_build + +Whether if the context is considered a local build or not. This is done by evaluating the +environment variables to decide if it's being run in a CI Job. It can be overriden with +the `CICD_IMAGE_BUILDER_LOCAL_BUILD` variable + +#### cicd::image_builder::get_containerfile + +Returns the Containerfile path that will be used + +#### cicd::image_builder::get_build_context + +Returns the build context that will be used + +#### cicd::image_builder::get_image_tag + +Returns the default image tag that will be used based on the current configuration. There are 2 +factors that can affect the value returned, whether if it's a CI context, and whether if it's a +change request context. Please refer to the previous +section `Expected Image tags based on the environment` for a reference of the expected tags + +#### cicd::image_builder::get_build_id + +If on a change request context, returns the build ID (the unique identifier of the change request +ID) + +#### cicd::image_builder::get_additional_tags + +Returns the list of additional tags to be added when building and pushing an image. + +#### cicd::image_builder::get_labels + +Returns the list of labels to be applied when building a new image + +#### cicd::image_builder::get_build_args + +Returns the list of build arguments when building a new image + +#### cicd::image_builder::get_full_image_name + +Returns the Image name including the default tag in the format +`registry/repository_org/repository_name:default_tag` \ No newline at end of file diff --git a/docs/cicd_tools/loader.md b/docs/cicd_tools/loader.md new file mode 100644 index 00000000..752a0971 --- /dev/null +++ b/docs/cicd_tools/loader.md @@ -0,0 +1,40 @@ +# loader module + +This module provides functions to help loading the different modules included in this library. + +## Definition + +The module ID is `loader` + +All functions exposed by this module will use the namespaced prefix: + +``` +cicd::loader:: +``` + +## Usage + +**Note:** This is an **internal module** and is not meant to be loaded directly, you should try to +use +*the [src/bootstrap.sh](load_module.sh) script as it already provides means to load this module. + +To load this module, the `CICD_LOADER_SCRIPTS_DIR` must be set with the path to the root directory +where the different modules to be loaded are located. + +## Configuration and public functions + +### Dependencies + +- `CICD_LOADER_SCRIPTS_DIR` variable is **required** to be set to the path containing the modules to + be loaded + +### Configuration Variables + +- `CICD_LOADER_SCRIPTS_DIR` The path to the root directory containing all the library's modules. + +### Public functions + +#### cicd::loader::load_module + +receives a module id and sources it. All the module source scripts are relative to the directory +defined in the `CICD_LOADER_SCRIPTS_DIR` variable \ No newline at end of file diff --git a/docs/cicd_tools/log.md b/docs/cicd_tools/log.md new file mode 100644 index 00000000..135a804e --- /dev/null +++ b/docs/cicd_tools/log.md @@ -0,0 +1,61 @@ +# Log module + +This module provides functions to help write log messages. + +## Definition + +The module ID is `log` + +All functions exposed by this module will use the namespaced prefix: + +``` +cicd::log:: +``` + +## Usage + +Use the `log` id to load the module via the `loader` module, using either the `load_module.sh` +or the `bootstrap.sh` script + +### Enabling debug output + +enabling the debug output requires to export the `CICD_LOG_DEBUG` variable with a non-empty value. + +``` +export CICD_LOG_DEBUG=1 +``` + +## Configuration and public functions + +### Dependencies + +None + +### Configuration Variables + +- `CICD_LOG_DEBUG` if defined to a non-empty value enables the messages printed through + the `cicd::log::debug` function + +### Public functions + +#### cicd::log::debug + +echoes a message via standard output (STDOUT) with the timestamp if the environment +varible `CICD_DEBUG_LOG` is defined and not empty + +#### cicd::log::info + +echoes a message via standard output stream (STDOUT) with the timestamp if the environment +varible `CICD_DEBUG_LOG` is defined and not empty + +``` +[2021-06-30T10:50:38+0200]: Error output! +``` + +#### cicd::log::err + +echoes a message via standard error stream (STDERR) with the timestamp. Example format: + +``` +[2021-06-30T10:50:38+0200]: Error output! +``` \ No newline at end of file From 0410df83f37d7fa9901b4c36767de11e80eadb07 Mon Sep 17 00:00:00 2001 From: Victor M Date: Thu, 26 Oct 2023 14:41:11 +0200 Subject: [PATCH 11/15] Feedback from PR review --- README.md | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index c7d89a07..a68debf1 100644 --- a/README.md +++ b/README.md @@ -26,31 +26,24 @@ file [here](examples/unit_test_example.sh). | smoke_test.sh | **DEPRECATED**, use [cji_smoke_test.sh](cji_smoke_test.sh) | | iqe_pod | **DEPRECATED**, use [cji_smoke_test.sh](cji_smoke_test.sh) | -## Bash script helper scripts usage +## Bash script library usage The collection of helper libraries are expected to be loaded using the provided [src/bootstrap.sh](bootstrap) script. -The bootstrap's script responsibilities are to get a local copy of this repository, initialize -the `loader` module, and load +The [src/bootstrap.sh](/src/bootstrap.sh) script pulls a local copy of this repo and initializes +the loader module, which serves as an entrypoint into the library. The loader module provides +the ` cicd::loader::load_module` functions that enable loading different modules. -The `loader` module is a special one, and it's main function is to serve as an entrypoint to the -library. -It provides the `cicd::loader::load_module` functions, that must be used to load the different -modules. +See the table below for information on the modules: -This is all handed by the boostrap script - -To read more about what each of the modules provide, -please read through the documents linked through the library IDs in the following table: - -| Library ID | Description | -|-------------------------------------|----------------------------------------------------------------------------| -| [container](docs/cicd_tools/container.md) | Provides wrapper functions for invoking container engine agnostic commands | -| [image_builder](docs/cicd_tools/image_builder.md) | Provides helper functions to simplify the image building process | -| [common](docs/cicd_tools/common.md) | Provides helper functions to simplify the image building process | -| [log](docs/cicd_tools/log.md) | Provides wrapper functions for invoking container engine agnostic commands | -| [loader](docs/cicd_tools/loader.md) | Provides wrapper functions for invoking container engine agnostic commands | +| Library ID | Description | +|---------------------------------------------------|----------------------------------------------------| +| [container](docs/cicd_tools/container.md) | container engine agnostic commands | +| [image_builder](docs/cicd_tools/image_builder.md) | Simplify image building process | +| [common](docs/cicd_tools/common.md) | Generic helper functions shared across all modules | +| [log](docs/cicd_tools/log.md) | logging tools | +| [loader](docs/cicd_tools/loader.md) | Module loading functions | ### How to use the helper libraries @@ -65,7 +58,17 @@ included in this library. This script requires all the other scripts available i following the same structure in this repository. To use any of the provided libraries, you must source the [src/load_module.sh](load_module.sh) -script and pass the unique library ID to be loaded as a parameter. +script and pass the unique library ID to be loaded as a parameter. For example: + +``` +module_id='container' +source src/load_module.sh "$module_id" + + +$ cicd::container::cmd --version + +podman version 4.7.0 +``` There's two different approaches for loading these scripts, depending on if you're a contributor or an end user. From 257c6726f2a713585a74f22d656cc657ea177070 Mon Sep 17 00:00:00 2001 From: Victor M Date: Thu, 26 Oct 2023 14:52:24 +0200 Subject: [PATCH 12/15] Feedback from PR review --- docs/cicd_tools/common.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/cicd_tools/common.md b/docs/cicd_tools/common.md index c9d42372..4e001cf3 100644 --- a/docs/cicd_tools/common.md +++ b/docs/cicd_tools/common.md @@ -1,7 +1,6 @@ # Common module -This module exposes helper functions that may be used (and shared) across all modules, or that don't -serve a specific module's purpose to fit within it. +Helper functions that are shared and used by other modules ## Definition @@ -66,4 +65,4 @@ The currently supported container engines are: #### cicd::container::cmd Should be used instead of a container engine command to support container-engine agnostic commands ( -among the supported container engines) \ No newline at end of file +among the supported container engines) From 55e7fbae408c5765410a55d228266dcf010a403a Mon Sep 17 00:00:00 2001 From: Victor M Date: Thu, 26 Oct 2023 14:58:13 +0200 Subject: [PATCH 13/15] Add common module docs --- docs/cicd_tools/common.md | 45 +++++++++++---------------------------- 1 file changed, 13 insertions(+), 32 deletions(-) diff --git a/docs/cicd_tools/common.md b/docs/cicd_tools/common.md index 4e001cf3..62eac9ef 100644 --- a/docs/cicd_tools/common.md +++ b/docs/cicd_tools/common.md @@ -22,47 +22,28 @@ CICD_TOOLS_URL="https://raw.githubusercontent.com/RedHatInsights/cicd-tools/main source <(curl -sSL "$CICD_TOOLS_URL") common ``` -This should load the function: - -``` -cicd::container::cmd -``` - -which serves as a wrapper to the container engine of choice. You should be able to safely replace -your invocations to `docker` or `podman` commands with this function - -### Container engine detection and order choice +## Dependencies -The library favors `podman` in case both container engines are available in the user's PATH. +This module requires one of the supported container engines to be present in the session's `PATH`. +The currently supported container engines are: -### Override container engine selection +- `docker` +- `podman` -If you want to force the library to stick to a preferred container engine, you can do so by setting -the `CICD_CONTAINER_PREFER_ENGINE` to one of the supported container engines available * -*before** loading the library. +### Public functions -The container engine is selected when the library is loaded and is not possible to update it -afterward. +#### cicd::common::command_is_present -``` -export CICD_CONTAINER_PREFER_ENGINE=docker -``` +returns 0 (true) if the command passed as a parameter is found in the session's PATH -**Please note:** If you set your preference to be `docker` and the library detects that `docker` is -actually mocked as a wrapper to `podman` the library will ignore the preference and will try to -set `podman` as the selected container engine. +#### cicd::common::get_7_chars_commit_hash -## Dependencies +Returns the first 7 characters from the latest commit of current directory's git HEAD -This module requires one of the supported container engines to be present in the session's `PATH`. -The currently supported container engines are: +#### cicd::common::is_ci_context -- `docker` -- `podman` +Returns 0 (true) if the current context is considered a CI context (i.e - running in a job on a CI +system) -### Public functions -#### cicd::container::cmd -Should be used instead of a container engine command to support container-engine agnostic commands ( -among the supported container engines) From be2b35033abc9133e91050732c3780d3e13f3fd8 Mon Sep 17 00:00:00 2001 From: Victor M Date: Thu, 26 Oct 2023 17:21:32 +0200 Subject: [PATCH 14/15] Improved image_builder based on PR feedback --- docs/cicd_tools/image_builder.md | 215 ++++++++++++++++++++++++++++--- 1 file changed, 196 insertions(+), 19 deletions(-) diff --git a/docs/cicd_tools/image_builder.md b/docs/cicd_tools/image_builder.md index d3145e77..8d3fda3b 100644 --- a/docs/cicd_tools/image_builder.md +++ b/docs/cicd_tools/image_builder.md @@ -25,26 +25,201 @@ source <(curl -sSL "$CICD_TOOLS_URL") image_builder This module uses the following variables to configure the image building requirements: -| Variable name | Description | Default value | Type | Mandatory | -|---------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|----------------------|--------|-----------| -| CICD_IMAGE_BUILDER_IMAGE_NAME | The Image name to be used, in format: 'imageregistry/org/image_name' | `""` | string | Yes | -| CICD_IMAGE_BUILDER_LOCAL_BUILD | Override local build detection | `$LOCAL_BUILD` | string | No | -| CICD_IMAGE_BUILDER_IMAGE_TAG | The main image tag to be used. If not provided the 7 first chars of the current repository's git commit hash will be used instead | `""` | string | No | -| CICD_IMAGE_BUILDER_ADDITIONAL_TAGS | The additional tags (if any) to be created in array format: ("tag1" "tag2" "latest") | `()` | Array | No | -| CICD_IMAGE_BUILDER_LABELS | The labels (if any) to add to the image being built in array format: ("label1=Value1" "label2=value2") | `()` | Array | No | -| CICD_IMAGE_BUILDER_BUILD_ARGS | The build arguments to be provided when building the image (if any) in array format: ("buildarg1=Value1" "buildarg2=value2") | `()` | Array | No | -| CICD_IMAGE_BUILDER_BUILD_CONTEXT | The build context path to use when building the image. | `.` | None | string | No| -| CICD_IMAGE_BUILDER_CONTAINERFILE_PATH | The Containerfile path to use when building the image. | `Dockerfile` | None | string | No| -| CICD_IMAGE_BUILDER_QUAY_EXPIRE_TIME | The expire time value to be set for Quay expires labels, used only in change request contexts. | `3d` | string | No | -| CICD_IMAGE_BUILDER_QUAY_USER | The username to use when logging in to Quay.io | `$QUAY_USER` | string | No | -| CICD_IMAGE_BUILDER_QUAY_PASSWORD | The password to use when logging in to Quay.io | `$QUAY_TOKEN` | string | No | -| CICD_IMAGE_BUILDER_REDHAT_USER | The username to use when logging in to the Red Hat Registry | `$RH_REGISTRY_USER` | string | No | -| CICD_IMAGE_BUILDER_REDHAT_PASSWORD | The password to use when logging in to the Red Hat Registry | `$RH_REGISTRY_TOKEN` | string | No | +### Configuration variables The only required variable is `CICD_IMAGE_BUILDER_IMAGE_NAME`, the rest of them are optional and should not be needed for the majority of use cases as the provided default values should be enough. +Here are a few examples (considering `abcdef1` is the first 7 chars of the current directory git +HEAD): + +``` +export CICD_IMAGE_BUILDER_IMAGE_NAME='quay.io/awesome_org/awesome_project' +source <(curl -sSL "$CICD_TOOLS_URL") image_builder + +cicd::image_builder::get_full_image_name + +# returns quay.io/awesome_org/awesome_project:abcdef1 +``` + +The following is a list of all the configuration variables available with a description and examples + +#### CICD_IMAGE_BUILDER_IMAGE_NAME + +Type: "string", Default value: "" + +Image name to be used, in format: 'imageregistry/org/image_name'. This variable is **mandatory**. +Several functions rely on this variable to work properly. + +```shell +export CICD_IMAGE_BUILDER_IMAGE_NAME='quay.io/cloudservices/awesomerepo' + +cicd::image_builder::get_full_image_name + +# quay.io/cloudservices/awesomerepo:abcdef1 +``` + +### CICD_IMAGE_BUILDER_LOCAL_BUILD + +Type: "string", Default value: "" + +Force "local build context" mode. The library autodetects the mode based on the environment +variables to decide if on a local build context or not. Some functions change their behavior +depending on whether it's a local build or not. + +For example, `cicd::image_builder::build_and_push` will not actually `push` if on local mode. This +is to prevent accidental push events and to facilitate local debugging. + +Setting this variable to a non-empty value will force the local mode regardless of the environment +variables present + +```shell + +export CICD_IMAGE_BUILDER_LOCAL_MODE=1 + +if cicd::image_builder::local_build then; + echo "forcing local mode!" +fi +``` + +### CICD_IMAGE_BUILDER_IMAGE_TAG + +Type: "string", Default value: "" + +Define a static default tag. If not defined, the 7 first chars of the current repository's git +commit hash will be used instead + +```shell + +cicd::image_builder::get_image_tag +# prints abcdef1 + +export CICD_IMAGE_BUILDER_IMAGE_TAG='awesome_tag' +cicd::image_builder::get_image_tag +# prints awesome_tag +``` + +### CICD_IMAGE_BUILDER_ADDITIONAL_TAGS + +Type: "array", Default value: "()" + +The additional tags (if any) to be created in array format: ("tag1" "tag2" "latest") + +```shell + +cicd::image_builder::get_additional_tags +# prints "" + +export CICD_IMAGE_BUILDER_ADDITIONAL_TAGS=("apple" "banana" "orange") +cicd::image_builder::get_additional_tags +# prints "apple" "banana" "orange" + +export CICD_IMAGE_BUILDER_IMAGE_NAME='quay.io/cloudservices/awesome' +cicd::image_builder::build +# will attempt to build the following tags: +#quay.io/cloudservices/awesome:abcdef1 +#quay.io/cloudservices/awesome:apple +#quay.io/cloudservices/awesome:banana +#quay.io/cloudservices/awesome:orange + +``` + +### CICD_IMAGE_BUILDER_LABELS + +Type: "array", Default value: "()" + +The additional labels (if any) to be created in array format: ("label1=Value1" "label2=value2") + +```shell +export CICD_IMAGE_BUILDER_LABELS=("label1=value1" "label2=value2") + +cicd::image_builder::build + +# will add --label label1=value1 --label2=value2 to the image build command +``` + +### CICD_IMAGE_BUILDER_BUILD_ARGS + +Type: "array", Default value: "()" + +The additional labels (if any) to be created in array format: ("buildarg1=Value1" " +buildarg2=value2") + +```shell +export CICD_IMAGE_BUILDER_BUILD_ARGS=("buildarg1=value1" "buildarg2=value2") + +cicd::image_builder::build + +# will add --build-arg buildarg1=value1 --build-arg buildarg2=value2 to the image build command +``` + +### CICD_IMAGE_BUILDER_BUILD_CONTEXT + +Type: "string", Default value: "." + +Defines the current build context for when building the container images + +```shell +export CICD_IMAGE_BUILDER_BUILD_CONTEXT='./container' + +cicd::image_builder::build + +# will try to run the build command using the provided context +# docker build -f Dockerfile ./contaienr +``` + +### CICD_IMAGE_BUILDER_CONTAINERFILE_PATH + +Type: "string", Default value: "Dockerfile" + +Defines the containerfiule path to build images. + +```shell +export CICD_IMAGE_BUILDER_CONTAINERFILE_PATH='deployment/test.Dockerfile' + +cicd::image_builder::build + +# will try to run the build command using the provided containerfile +# docker build -f deployment/test.Dockerfile . +``` + +### CICD_IMAGE_BUILDER_QUAY_EXPIRE_TIME + +Type: "string", Default value: "3d" + +Defines the default expiry time for change request images pushed to Quay. + +```shell +export CICD_IMAGE_BUILDER_QUAY_EXPIRE_TIME='1h' + +# in a change request context, will build using the label: quay.expires-after=1h +``` + +### CICD_IMAGE_BUILDER_QUAY_USER + +Type: "string", Default value: `$QUAY_USER` + +The username used to log in to Quay.io + +### CICD_IMAGE_BUILDER_QUAY_PASSWORD + +Type: "string", Default value: `$QUAY_TOKEN` + +The password used to log in to Quay.io + +### CICD_IMAGE_BUILDER_REDHAT_USER + +Type: "string", Default value: `$RH_REGISTRY_USER` + +The username used to log in to the Red Hat Registry + +### CICD_IMAGE_BUILDER_QUAY_PASSWORD + +Type: "string", Default value: `$RH_REGISTRY_TOKEN` + +The password used to log in to Red Hat Registry + It is important to consider that the behavior of the different image builder functions may be affected by the context where it runs. The context include a CI context (for example, while running in a pipeline), and running in a Change @@ -73,8 +248,10 @@ expected to be created, considering if: ## Override the default image tag -If for some reason the "GIT_SHA" default value, the `CICD_IMAGE_BUILDER_IMAGE_TAG` can be used to override it. -The value of the default tag will still be modified in change requests context though, to include the `buildID` and the `pr` prefix +If for some reason the "GIT_SHA" default value, the `CICD_IMAGE_BUILDER_IMAGE_TAG` can be used to +override it. +The value of the default tag will still be modified in change requests context though, to include +the `buildID` and the `pr` prefix Example: @@ -82,8 +259,8 @@ Example: export CICD_IMAGE_BUILDER_IMAGE_TAG=awesome-tag echo $(cicd::image_builder::get_image_tag) ``` -Will output the value `awesome-tag` or the `pr-123-awaesome-tag` value for Change Request contexts +Will output the value `awesome-tag` or the `pr-123-awaesome-tag` value for Change Request contexts ## Dependencies @@ -160,4 +337,4 @@ Returns the list of build arguments when building a new image Returns the Image name including the default tag in the format -`registry/repository_org/repository_name:default_tag` \ No newline at end of file +`registry/repository_org/repository_name:default_tag` From 88338c4dfcd9a84b41aeaa576bd362c25b835ab2 Mon Sep 17 00:00:00 2001 From: Victor M Date: Thu, 26 Oct 2023 19:49:33 +0200 Subject: [PATCH 15/15] rework loader text from PR feedback --- docs/cicd_tools/image_builder.md | 2 +- docs/cicd_tools/loader.md | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/docs/cicd_tools/image_builder.md b/docs/cicd_tools/image_builder.md index 8d3fda3b..dd4017f0 100644 --- a/docs/cicd_tools/image_builder.md +++ b/docs/cicd_tools/image_builder.md @@ -337,4 +337,4 @@ Returns the list of build arguments when building a new image Returns the Image name including the default tag in the format -`registry/repository_org/repository_name:default_tag` +`registry/repository_org/repository_name:default_tag` \ No newline at end of file diff --git a/docs/cicd_tools/loader.md b/docs/cicd_tools/loader.md index 752a0971..efc7f1d9 100644 --- a/docs/cicd_tools/loader.md +++ b/docs/cicd_tools/loader.md @@ -1,6 +1,12 @@ # loader module -This module provides functions to help loading the different modules included in this library. +Loads other modules provided by the library. + +**Internal use only** + +This is an internal API for the CICD tools library and not intended for public use. Library users +should use the [/src/bootstrap.sh](/src/bootstrap.sh) script in lieu of calling the loader module +itself ## Definition @@ -14,10 +20,6 @@ cicd::loader:: ## Usage -**Note:** This is an **internal module** and is not meant to be loaded directly, you should try to -use -*the [src/bootstrap.sh](load_module.sh) script as it already provides means to load this module. - To load this module, the `CICD_LOADER_SCRIPTS_DIR` must be set with the path to the root directory where the different modules to be loaded are located.