cyberark/secretless-broker

View on GitHub
bin/test_unit

Summary

Maintainability
Test Coverage
#!/bin/bash

set -eo pipefail

. bin/build_utils

current_dir=$("$(dirname "$0")"/abspath)
toplevel_dir=$current_dir/..
junit_output_dir=test/unit-test-output

function main() {
  retrieve_cyberark_ca_cert
  build_ut_docker_image
  run_unit_tests
}

function build_ut_docker_image() {
  echo "Building unit test image..."
  docker build "$toplevel_dir" \
               -t secretless-unit-test-runner:latest \
               -f "$toplevel_dir"/Dockerfile.test
}

function run_unit_tests() {
  echo "Running unit tests..."
    mkdir -p "$junit_output_dir"
    rm -f "$junit_output_dir/*"

    # TODO: Use `go vet` too
    # NOTE: We're leaving out cmd/shared because it was causing
    #       a "g++": executable file not found in $PATH error, and
    #       the code is not being used anyway (was part of an Envoy POC)
    # NOTE: Exclude autogenerated code in pkg/k8sclient from being included in
    #       the coverage report.
    docker run --rm -t \
      --volume "$toplevel_dir"/:/secretless \
      secretless-unit-test-runner:latest \
      go test \
        -v \
        -timeout 3m \
        -vet=off \
        -coverprofile="./$junit_output_dir/c.out" \
        ./cmd/secretless-broker... \
        ./cmd/summon2... \
        ./internal/... \
        ./pkg/apis/... \
        ./pkg/secretless/... \
      | tee -a "./$junit_output_dir/junit.output"
    echo "Unit test exit status: $?"
}

main