Skip to content

Commit 19952a6

Browse files
author
Sebastiaan Verbeek
authored
feat: bats test framework (#216)
1 parent 3ecc99b commit 19952a6

File tree

5 files changed

+114
-11
lines changed

5 files changed

+114
-11
lines changed

Dockerfile

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,43 @@
11
FROM node:14-slim as npm
22

3-
ARG SKIP_TESTS='false'
4-
ENV CI=true
5-
63
ENV APP_HOME=/home/app/stack
74
RUN mkdir -p $APP_HOME
85
WORKDIR $APP_HOME
96

7+
ARG SKIP_TESTS='false'
8+
ENV CI=true
9+
1010
COPY . .
1111
COPY ./.cspell.json .
1212

1313
RUN if [ "$SKIP_TESTS" = 'false' ]; then \
1414
npm install cspell && npm run spellcheck; fi
1515

1616
#-----------------------------
17-
FROM otomi/tools:1.4.10 as prod
18-
19-
ARG SKIP_TESTS='false'
20-
ENV CI=true
17+
FROM otomi/tools:1.4.10 as test
2118

2219
ENV APP_HOME=/home/app/stack
2320
RUN mkdir -p $APP_HOME
2421
WORKDIR $APP_HOME
2522

23+
ARG SKIP_TESTS='false'
24+
ENV CI=true
25+
2626
COPY . .
2727

2828
RUN if [ "$SKIP_TESTS" = 'false' ]; then \
2929
cp -r .demo/ env/ && \
3030
bin/validate-values.sh && \
3131
bin/validate-templates.sh && \
32-
rm -rf env/*; fi
32+
bats bin/tests; fi
33+
34+
#-----------------------------
35+
FROM otomi/tools:1.4.10 as prod
36+
37+
ENV APP_HOME=/home/app/stack
38+
RUN mkdir -p $APP_HOME
39+
WORKDIR $APP_HOME
40+
41+
COPY . .
3342

3443
CMD ["bin/otomi"]

bin/bootstrap.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ else
1111
fi
1212

1313
has_otomi='false'
14-
skip_demo_files=${1-'false'}
1514
[ -f $ENV_DIR/bin/otomi ] && has_otomi='true'
1615

1716
function generate_loose_schema() {
@@ -44,8 +43,8 @@ done
4443
for f in '.gitignore' '.prettierrc.yml' 'README.md'; do
4544
cp $PWD/.values/$f $ENV_DIR/
4645
done
47-
if [ "$skip_demo_files" = "false" ]; then
48-
echo "Installing demo files"
46+
if [ -z "$(ls -A $ENV_DIR/env)" ]; then
47+
echo "No files found in env, installing demo files"
4948
cp -r $PWD/.demo/env $ENV_DIR/env
5049
fi
5150
cp -f $PWD/bin/hooks/pre-commit $ENV_DIR/.git/hooks/

bin/tests/bootstrap-integration.bats

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/local/bin/bats
2+
3+
lib_dir="/usr/local/lib"
4+
5+
load "$lib_dir/bats-support/load.bash"
6+
load "$lib_dir/bats-assert/load.bash"
7+
load "$lib_dir/bats-file/load.bash"
8+
9+
setup () {
10+
test_temp_dir="$(temp_make --prefix "otomi-values-")"
11+
export ENV_DIR="$test_temp_dir"
12+
}
13+
14+
teardown () {
15+
temp_del "$test_temp_dir"
16+
unset ENV_DIR
17+
}
18+
19+
@test "executing bootstrap.sh should pass with new ENV_DIR (otomi-values) folder" {
20+
git init "$ENV_DIR"
21+
run bin/bootstrap.sh
22+
assert_success
23+
}
24+
25+
@test "executing bootstrap.sh should fail without new ENV_DIR (otomi-values) folder" {
26+
unset ENV_DIR
27+
run bin/bootstrap.sh
28+
assert_failure
29+
}
30+
31+
@test "executing bootstrap.sh multiple times should pass with new ENV_DIR (otomi-values)" {
32+
git init "$test_temp_dir"
33+
bin/bootstrap.sh
34+
run bin/bootstrap.sh
35+
assert_success
36+
}

bin/tests/bootstrap-unit.bats

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/local/bin/bats
2+
3+
lib_dir="/usr/local/lib"
4+
5+
load "$lib_dir/bats-support/load.bash"
6+
load "$lib_dir/bats-assert/load.bash"
7+
load "$lib_dir/bats-file/load.bash"
8+
9+
setup () {
10+
test_temp_dir="$(temp_make --prefix 'otomi-values-')"
11+
export ENV_DIR="$test_temp_dir"
12+
env_path="$ENV_DIR/env"
13+
}
14+
15+
teardown () {
16+
temp_del "$test_temp_dir"
17+
unset ENV_DIR
18+
unset env_path
19+
}
20+
21+
@test "the env folder should not be overwritten" {
22+
git init "$ENV_DIR"
23+
cluster_path="$env_path/clusters.yaml"
24+
mkdir -p "$env_path" && touch $cluster_path && echo "clouds: please-do-not-remove" > $cluster_path
25+
bin/bootstrap.sh
26+
27+
assert_file_not_exist "$env_path/charts"
28+
assert_file_not_exist "$env_path/clouds"
29+
assert_file_not_exist "$env_path/teams"
30+
}
31+
32+
@test "the env folder should be created after bootstrap.sh" {
33+
git init "$ENV_DIR"
34+
bin/bootstrap.sh
35+
36+
assert_file_exist "$env_path/charts"
37+
assert_file_exist "$env_path/clouds"
38+
assert_file_exist "$env_path/teams"
39+
}

tools/Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ ARG KUBEVAL_VERSION=0.15.0
1818

1919
ARG HELM_FILE_NAME=helm-${HELM_VERSION}-linux-amd64.tar.gz
2020

21+
# https://github.com/ztombol/bats-assert/releases https://github.com/ztombol/bats-support
22+
ARG BATS_VERSION=1.2.1
23+
ARG BATS_ASSERT_VERSION=0.3.0
24+
ARG BATS_SUPPORT_VERSION=0.3.0
25+
ARG BATS_FILE_VERSION=0.2.0
26+
2127
WORKDIR /
2228

2329
RUN apt-get update -qq \
@@ -80,6 +86,20 @@ RUN curl -sL https://deb.nodesource.com/setup_lts.x | bash -
8086
RUN apt-get install -y nodejs && \
8187
npm install -g ajv-cli
8288

89+
# bats
90+
ADD https://codeload.github.com/bats-core/bats-core/tar.gz/v${BATS_VERSION} /tmp
91+
RUN tar -zxvf /tmp/v${BATS_VERSION} -C /tmp && /tmp/bats-core-${BATS_VERSION}/install.sh /usr/local
92+
93+
# bats libs
94+
ADD https://codeload.github.com/ztombol/bats-assert/tar.gz/v${BATS_ASSERT_VERSION} /tmp
95+
RUN tar -zxvf /tmp/v${BATS_ASSERT_VERSION} -C /tmp && mv /tmp/bats-assert-${BATS_ASSERT_VERSION} /usr/local/lib/bats-assert
96+
97+
ADD https://codeload.github.com/ztombol/bats-support/tar.gz/v${BATS_SUPPORT_VERSION} /tmp
98+
RUN tar -zxvf /tmp/v${BATS_SUPPORT_VERSION} -C /tmp && mv /tmp/bats-support-${BATS_SUPPORT_VERSION} /usr/local/lib/bats-support
99+
100+
ADD https://codeload.github.com/ztombol/bats-file/tar.gz/v${BATS_FILE_VERSION} /tmp
101+
RUN tar -zxvf /tmp/v${BATS_FILE_VERSION} -C /tmp && mv /tmp/bats-file-${BATS_FILE_VERSION} /usr/local/lib/bats-file
102+
83103
RUN chown -R app:app /home/app
84104
USER app
85105

0 commit comments

Comments
 (0)