Commit 29631591 authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

Merge branch 'feat/e2e-tests' into 'main'

Add end2end tests

See merge request to-be-continuous/tools/tbc-commons!7
parents b9910041 95f61bf2
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -24,3 +24,6 @@

### TBC ###
reports/
.cache/
.cfg/
gitlab.env
+113 −0
Original line number Diff line number Diff line
@@ -4,3 +4,116 @@ include:
      shellcheck-files: "*.sh"
      bats-enabled: true
      bats-libraries: "bats-support@https://github.com/bats-core/bats-support/archive/v0.3.0.zip bats-assert@https://github.com/bats-core/bats-assert/archive/v2.0.0.zip"

.e2e-scripts: &e2e-scripts |
  # BEGSCRIPT
  set -e

  function log_info() {
    >&2 echo -e "[\\e[1;94mINFO\\e[0m] $*"
  }

  function log_warn() {
    >&2 echo -e "[\\e[1;93mWARN\\e[0m] $*"
  }

  function log_error() {
    >&2 echo -e "[\\e[1;91mERROR\\e[0m] $*"
  }

  function assert_defined() {
    if [[ -z "${!1}" ]]
    then
      log_error "Variable '$1' should be defined"
      TBC_TEST_FAILS=$((TBC_TEST_FAILS + 1))
    fi
  }

  function refute_defined() {
    if [[ "${!1}" ]]
    then
      log_error "Variable '$1' should not be defined"
      TBC_TEST_FAILS=$((TBC_TEST_FAILS + 1))
    fi
  }

  function assert_equals() {
    if [[ "$1" != "$2" ]]
    then
      log_error "actual '$2' should be expected '$1'"
      TBC_TEST_FAILS=$((TBC_TEST_FAILS + 1))
    fi
  }

  function refute_defined() {
    if [[ "$1" == "$2" ]]
    then
      log_error "actual '$2' should not be expected '$1'"
      TBC_TEST_FAILS=$((TBC_TEST_FAILS + 1))
    fi
  }

  # ENDSCRIPT

e2e:
  stage: test
  needs: []
  image: $IMAGE
  parallel:
    matrix:
      # - IMAGE: "fedora"
      - IMAGE: "debian"
      - IMAGE: "alpine"
  variables:
    # enable cache
    XDG_CACHE_HOME: $CI_PROJECT_DIR/.cache
    # secrets
    B64_SECRET: "@b64@YmFzZTY0IHJvY2tzIQo="
    HEX_SECRET: "@hex@6865786120697320746865206B6579"
    URL_SECRET: "@url@$CI_PROJECT_URL/-/raw/$CI_COMMIT_REF_NAME/tests/fixtures/secret.txt"
    # scoped variables
    scoped__TESTVAR1__if__CI_JOB_STAGE__equals__test: "stage is 'test'"
    scoped__TESTVAR2__if__CI_JOB_STAGE__equals__abcde: "stage is 'abcde'"
    scoped__TESTVAR3__if__CI_JOB_STAGE__in__build__test: "stage is 'build' or 'test'"
    scoped__TESTVAR4__if__CI_JOB_STAGE__in__package__package_test: "stage is 'package' or 'package-test'"
    scoped__TESTVAR5__if__CI_JOB_STAGE__startswith__te: "stage starts with 'te'"
    scoped__TESTVAR6__if__CI_JOB_STAGE__startswith__ab: "stage starts with 'ab'"
    scoped__TESTVAR7__if__CI_JOB_STAGE__endswith__st: "stage ends with 'st'"
    scoped__TESTVAR8__if__CI_JOB_STAGE__endswith__de: "stage ends with 'de'"
    scoped__TESTVAR9__if__CI_JOB_STAGE__contains__es: "stage contains 'es'"
    scoped__TESTVAR10__if__CI_JOB_STAGE__contains__bcd: "stage contains 'bcd'"
  cache:
    key: "$IMAGE-$CI_COMMIT_REF_SLUG"
    paths:
      - $XDG_CACHE_HOME
    when: always
  before_script:
    - source tbc_base.sh
    - source tbc_ca_certs.sh
    - source tbc_envsubst.sh
    - source tbc_eval_secrets.sh
    - source tbc_exec_hook.sh
    - source tbc_maybe_install_packages.sh
    - source tbc_unscope_vars.sh
    - !reference [.e2e-scripts]
  script:
    - maybe_install_packages wget ca-certificates git

    - unscope_variables
    - assert_equals "stage is 'test'" "$TESTVAR1"
    - refute_defined TESTVAR2
    - assert_equals "stage is 'build' or 'test'" "$TESTVAR3"
    - refute_defined TESTVAR4
    - assert_equals "stage starts with 'te'" "$TESTVAR5"
    - refute_defined TESTVAR6
    - assert_equals "stage ends with 'st'" "$TESTVAR7"
    - refute_defined TESTVAR8
    - assert_equals "stage contains 'es'" "$TESTVAR9"
    - refute_defined TESTVAR10

    - eval_all_secrets
    - assert_equals "$B64_SECRET" "base64 rocks!"
    - assert_equals "$HEX_SECRET" "hexa is the key"
    - assert_equals "$URL_SECRET" "hidden secret"

    - exit $TBC_TEST_FAILS
+34 −0
Original line number Diff line number Diff line
@@ -2,3 +2,37 @@

This project centralizes, tests, incubates some `to-be-continuous` common stuff (mainly scripts).

## Test TBC scripts in containers

1. run the container (alpine, debian, ~~fedora~~)
    ```bash
    docker run --rm -it --entrypoint /bin/sh --volume $(pwd):/app --workdir /app alpine
    ```
2. in the container
    ```bash
    export XDG_CACHE_HOME=/app/.cache
    set +e

    # test maybe_install_packages
    source tbc_base.sh
    source tbc_maybe_install_packages.sh
    maybe_install_packages wget ca-certificates git

    # test unscope_variables
    source tbc_base.sh
    source tbc_unscope_vars.sh
    export CI_JOB_STAGE=test
    export scoped__TESTVAR1__if__CI_JOB_STAGE__equals__test="stage is 'test'"
    export scoped__TESTVAR2__if__CI_JOB_STAGE__equals__abcde="stage is 'abcde'"
    export scoped__TESTVAR3__if__CI_JOB_STAGE__in__build__test="stage is 'build' or 'test'"
    export scoped__TESTVAR4__if__CI_JOB_STAGE__in__package__package_test="stage is 'package' or 'package-test'"
    export scoped__TESTVAR5__if__CI_JOB_STAGE__startswith__te="stage starts with 'te'" # problème sur Alpine - non sélectionné
    export scoped__TESTVAR6__if__CI_JOB_STAGE__startswith__ab="stage starts with 'ab'"
    export scoped__TESTVAR7__if__CI_JOB_STAGE__endswith__st="stage ends with 'st'"
    export scoped__TESTVAR8__if__CI_JOB_STAGE__endswith__de="stage ends with 'de'"
    export scoped__TESTVAR9__if__CI_JOB_STAGE__contains__es="stage contains 'es'" # problème sur Alpine - tests: unknown operand
    export scoped__TESTVAR10__if__CI_JOB_STAGE__contains__bcd="stage contains 'bcd'"
    unscope_variables

    ...
    ```
+6 −5
Original line number Diff line number Diff line
@@ -11,13 +11,14 @@ function log_warn() {
}

function log_error() {
  >&2 echo -e "[\\e[1;91mERROR\\e[0m] $*"lighthouse
  >&2 echo -e "[\\e[1;91mERROR\\e[0m] $*"
}

function fail() {
  log_error "$*"
  exit 1
}
# commented out to avoid collision with bats-assert
# function fail() {
#   log_error "$*"
#   exit 1
# }

function assert_defined() {
  if [[ -z "$1" ]]
+29 −0
Original line number Diff line number Diff line
#!/usr/bin/env bash

set -e

# This function installs packages if required.
# If abstracts the underlying the Linux distro by managing several package systems (Debian and Alpine).
# Determines whether the package is present either by checking the presence of the provided package(s).
# Limitations: assumes the package(s) to install have the same name on all distros
  function maybe_install_packages() {
    if command -v apt-get > /dev/null
    then
      # Debian
      if ! dpkg --status "$@" > /dev/null
      then
        apt-get update
        apt-get install --no-install-recommends --yes --quiet "$@"
      fi
    elif command -v apk > /dev/null
    then
      # Alpine
      if ! apk info --installed "$@" > /dev/null
      then
        apk add --no-cache "$@"
      fi
    else
      log_error "... didn't find any supported package manager to install $*"
      exit 1
    fi
  }
Loading