Skip to content

Commit

Permalink
feat: add bats documentation (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastiaan Verbeek committed Dec 16, 2020
1 parent 4683d58 commit a11cacf
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 37 deletions.
36 changes: 0 additions & 36 deletions bin/tests/bootstrap-integration.bats

This file was deleted.

19 changes: 19 additions & 0 deletions bin/tests/bootstrap-unit.bats → bin/tests/bootstrap.bats
Expand Up @@ -36,4 +36,23 @@ teardown () {
assert_file_exist "$env_path/charts"
assert_file_exist "$env_path/clouds"
assert_file_exist "$env_path/teams"
}

@test "executing bootstrap.sh should pass with new ENV_DIR (otomi-values) folder" {
git init "$ENV_DIR"
run bin/bootstrap.sh
assert_success
}

@test "executing bootstrap.sh should fail without new ENV_DIR (otomi-values) folder" {
unset ENV_DIR
run bin/bootstrap.sh
assert_failure
}

@test "executing bootstrap.sh multiple times should pass with new ENV_DIR (otomi-values)" {
git init "$test_temp_dir"
bin/bootstrap.sh
run bin/bootstrap.sh
assert_success
}
37 changes: 37 additions & 0 deletions docs/BATS.md
@@ -0,0 +1,37 @@
# BATS testing framework

This document is relevant starting from [v0.11.52](https://github.com/redkubes/otomi-core/releases/tag/v0.11.52).

## Where can I find BATS?

The otomi/tools:1.4.10 includes the [bats framework](https://github.com/bats-core/bats-core), including libraries [bats-assert](https://github.com/ztombol/bats-assert), [bats-file](https://github.com/ztombol/bats-support) and [bats-support](https://github.com/ztombol/bats-support). These links include relevant documentation, such as syntax, which won't be discussed on this page.

Example of calling the binary:

`docker run --rm otomi/tools:1.4.10 bats bin/tests`

This example assumes tests exist in the container directory `otomi-core/bin/tests`. You can call bats with a directory as parameter and it will execute any `*.bats` file.

## How should I add tests?

Test files should have the .bats extension, otherwise they will not be executed.

## How should I name the test files?

Usually you will name the test file after the shell file holding the code you are testing, but with the `.bats` extension.

Example with a bootstrap.sh file:

`bootstrap.bats`

## How do I use the BATS libraries?

The tests will run in the otomi-core container, which has bats libs in `/usr/local/lib/bats-*`. The following snippet will include them in a test file:

```
lib_dir="/usr/local/lib"
load "$lib_dir/bats-support/load.bash"
load "$lib_dir/bats-assert/load.bash"
load "$lib_dir/bats-file/load.bash"
```
3 changes: 2 additions & 1 deletion docs/CONTRIBUTING.md
Expand Up @@ -21,7 +21,8 @@ Please make sure to add all the artifacts from the Definition of Done, which inc
1. Spec validation of values happens automatically in the values repo by using `otomi commit`
2. Linting of k8s output (manifests) that are generated from the `.demo/env/*` input happens in the build pipelines.
It tests k8s output from the stack for correct CRs based on their CRDs and OPA rules defined.
Therefor it is very important to always add test data that generates all of your templates (to keep up coverage).
Therefore it is very important to always add test data that generates all of your templates (to keep up coverage).
3. Scripting is done in `bash`, and these shell scripts are available in `bin/*`. We use the `bats` testing framework as explained in [documentation about bats](./BATS.md).

### 2. End-to-end tests

Expand Down

0 comments on commit a11cacf

Please sign in to comment.