Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

bats test framework was added #135

Merged
merged 5 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,28 @@ on:
pull_request:

jobs:
unit-tests:
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ubuntu-18.04, ubuntu-20.04]
steps:

- name: Setup BATS
uses: mig4/setup-bats@v1
with:
bats-version: 1.2.1

- name: Check out code
uses: actions/checkout@v2
with:
submodules: recursive

- name: Unit Tests
working-directory: .
run: bats tests/*.bats

install-ssm-plugin:
runs-on: ${{ matrix.os }}

Expand Down
9 changes: 9 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[submodule "tests/test/libs/bats"]
path = tests/test/libs/bats
url = https://github.com/sstephenson/bats
[submodule "tests/test/libs/bats-assert"]
path = tests/test/libs/bats-assert
url = https://github.com/ztombol/bats-assert
[submodule "tests/test/libs/bats-support"]
path = tests/test/libs/bats-support
url = https://github.com/ztombol/bats-support
1 change: 1 addition & 0 deletions tests/test/libs/bats
Submodule bats added at 036081
1 change: 1 addition & 0 deletions tests/test/libs/bats-assert
Submodule bats-assert added at 9f88b4
1 change: 1 addition & 0 deletions tests/test/libs/bats-support
Submodule bats-support added at 004e70
29 changes: 29 additions & 0 deletions tests/test/libs/helpers/helpers.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# It's kind of a mock function for creating aws credential profile
# in specified place. It should be used in test cases like:
# $> create_aws_creds <specific path>
function create_aws_creds () {
mkdir "${1}/.aws"
cat << EOF > "${1}/.aws/credentials"
[${2}]
aws_access_key_id=XXXXXXXXXXXXXXXXXX
aws_secret_access_key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
gloovers marked this conversation as resolved.
Show resolved Hide resolved
region=us-east-1
gloovers marked this conversation as resolved.
Show resolved Hide resolved
EOF
}

# Function allows developer to create test target with custom rules
# in specified folder
# $> create_makefile <path> <custom rule or rules>
function create_makefile () {
export MAKEFILES="${1}/Makefile"
cat << EOF > "${MAKEFILES}"
ROOT_DIR = $(pwd)/examples/simple
NAMESPACE = bats-test
ICMK_VERSION ?= master

include examples/simple/.infra/icmk/init.mk

TEST:
gloovers marked this conversation as resolved.
Show resolved Hide resolved
${2}
EOF
}
1 change: 1 addition & 0 deletions tests/test/libs/helpers/load.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source "$(dirname "${BASH_SOURCE[0]}")/helpers.bash"
97 changes: 97 additions & 0 deletions tests/tools.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
load 'test/libs/bats-support/load'
load 'test/libs/bats-assert/load'
load 'test/libs/helpers/helpers'

# pre-actions before test cases running
setup () {
# create a temporary folder
export BATS_TEMP_DIR="/tmp/$(date +%s)"
mkdir "${BATS_TEMP_DIR}"
# create AWS creds stub
export test_aws_profile_name="bats_profile"
create_aws_creds "${BATS_TEMP_DIR}" "${test_aws_profile_name}"
}

# post-actions after tests complete
teardown () {
# clean-up after tests
rm -rf ${BATS_TEMP_DIR}
}

@test "Testing AWS docker container: should be successful" {
# Create temp Makefie
create_makefile "${BATS_TEMP_DIR}" "@\$(AWS) \$(arg1)"
# Run TEST target
run make TEST \
arg1="configure list-profiles" \
HOME=${BATS_TEMP_DIR} \
AWS_PROFILE=${test_aws_profile_name} \
# compare result with expected values
assert_success
assert_line "${test_aws_profile_name}"
}

@test "Testing TERRAFORM docker container: should be successful" {
TERRAFORM_VERSION="0.13.5"
gloovers marked this conversation as resolved.
Show resolved Hide resolved
create_makefile "${BATS_TEMP_DIR}" "@\$(TERRAFORM) \$(arg1)"
run make TEST \
arg1="-v" \
HOME=${BATS_TEMP_DIR} \
AWS_PROFILE="${test_aws_profile_name}" \
TERRAFORM_VERSION="${TERRAFORM_VERSION}"
assert_success
assert_line "Terraform v${TERRAFORM_VERSION}"
refute_line -p "The config profile"
}

@test "Testing JQ docker container: should be successful" {
create_makefile "${BATS_TEMP_DIR}" "@echo \$(arg1) | \$(JQ) \$(arg2)"
run make TEST \
arg1="'{\"foo\":\"barzone\"}'" \
arg2="-r '.foo'"
assert_success
assert_line "barzone"
}

@test "Testing CUT docker container: should be successful" {
create_makefile "${BATS_TEMP_DIR}" "@echo \$(arg1) | \$(CUT) \$(arg2)"
run make TEST \
arg1="123:4562020:789" \
arg2="-d ':' -f 2"
assert_success
assert_line "4562020"
}

@test "Testing REV docker container: should be successful" {
create_makefile "${BATS_TEMP_DIR}" "@echo \$(arg1) | \$(REV)"
run make TEST \
arg1="BATS is awesome"
assert_success
assert_line "emosewa si STAB"
}

@test "Testing BASE64 docker container: should be successful" {
create_makefile "${BATS_TEMP_DIR}" "@echo \$(arg1) | \$(BASE64) \$(arg2)"
run make TEST \
arg1="QkFUUyBpcyBoYW5keQo=" \
arg2="-d"
assert_success
assert_line "BATS is handy"
}

@test "Testing AWK docker container: should be successful" {
create_makefile "${BATS_TEMP_DIR}" "@echo \$(arg1) | \$(AWK) \$(arg2)"
run make TEST \
arg1="foo barzone" \
arg2="'{print}'"
assert_success
assert_line "foo barzone"
}

@test "Testing GOMPLATE docker container: should be successful" {
create_makefile "${BATS_TEMP_DIR}" "@\$(GOMPLATE) \$(arg1)"
run make TEST \
arg1="-i 'answer is {{ mul 6 7 }}'"
assert_success
assert_line "answer is 42"
}