Skip to content

Commit b63f86a

Browse files
authored
test: add integration tests (#271)
1 parent ea90d63 commit b63f86a

15 files changed

+203
-0
lines changed

.github/workflows/test.yaml

+76
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,82 @@ jobs:
2929
cache: true
3030
- run: make test
3131

32+
integration:
33+
runs-on: ubuntu-latest
34+
services:
35+
consul:
36+
image: hashicorp/consul
37+
ports:
38+
- 8500:8500
39+
vault:
40+
image: hashicorp/vault
41+
env:
42+
SKIP_SETCAP: "true"
43+
VAULT_DEV_ROOT_TOKEN_ID: vault-auth-token
44+
ports:
45+
- 8200:8200
46+
env:
47+
SERVICE_NAME: my-test-service
48+
SERVICE_PRODUCT: test
49+
steps:
50+
- uses: actions/checkout@v4
51+
- uses: actions/setup-go@v5
52+
with:
53+
go-version-file: go.mod
54+
check-latest: true
55+
cache: true
56+
- name: Setup
57+
run: |
58+
source tests/setup
59+
60+
consul global/env_vars/FOO foo
61+
consul global/env_vars/BAR bar
62+
consul global/dev/env_vars/DEV dev
63+
consul services/my-test-service/env_vars/FOO foobar
64+
consul services/my-test-service/env_vars/BAZ baz
65+
consul services/my-test-service/dev/env_vars/FOOBAR test
66+
67+
vault global/env_vars/SECRET my-secret
68+
vault global/dev/env_vars/SECRET my-dev-secret
69+
vault global/dev/env_vars/DEV vdev
70+
vault services/my-test-service/env_vars/FOO vfoo
71+
vault services/my-test-service/dev/env_vars/FOOBAR vfoobar
72+
- name: Test no config
73+
run: scripts/test -s 0 -e tests/no-config.stderr.txt true
74+
- name: Test no command
75+
run: scripts/test -s 1 -e tests/no-command.stderr.txt
76+
- name: Test load all dev
77+
run: scripts/test -s 0 -o tests/dev.stdout.txt -e tests/dev.stderr.txt tests/print-env FOO BAR BAZ DEV TEST SECRET
78+
env:
79+
CONSUL_ADDR: http://127.0.0.1:8500
80+
VAULT_ADDR: http://127.0.0.1:8200
81+
VAULT_TOKEN: vault-auth-token
82+
- name: Test load all
83+
run: scripts/test -s 0 -o tests/full.stdout.txt tests/print-env FOO BAR BAZ DEV TEST SECRET
84+
env:
85+
CONSUL_ADDR: http://127.0.0.1:8500
86+
VAULT_ADDR: http://127.0.0.1:8200
87+
VAULT_TOKEN: vault-auth-token
88+
SERVICE_ENV: stage
89+
- name: Test load consul
90+
run: scripts/test -s 0 -o tests/consul.stdout.txt -e tests/consul.stderr.txt tests/print-env FOO BAR BAZ DEV TEST
91+
env:
92+
CONSUL_ADDR: http://127.0.0.1:8500
93+
- name: Test load vault
94+
run: scripts/test -s 0 -o tests/vault.stdout.txt -e tests/vault.stderr.txt tests/print-env FOO BAR BAZ DEV TEST SECRET
95+
env:
96+
VAULT_ADDR: http://127.0.0.1:8200
97+
VAULT_TOKEN: vault-auth-token
98+
SERVICE_ENV: stage
99+
- name: Test load no service
100+
run: scripts/test -d -s 0 -o tests/no-service.stdout.txt -e tests/no-service.stderr.txt tests/print-env FOO BAR BAZ DEV TEST SECRET
101+
env:
102+
SERVICE_NAME: ""
103+
CONSUL_ADDR: http://127.0.0.1:8500
104+
VAULT_ADDR: http://127.0.0.1:8200
105+
VAULT_TOKEN: vault-auth-token
106+
SERVICE_ENV: stage
107+
32108
shellcheck:
33109
runs-on: ubuntu-latest
34110
steps:

scripts/test

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env bash
2+
3+
show_help() {
4+
cat << EOF
5+
Usage: ${0##*/} [-hd] [-s STATUS] [-o OUTPUT] [-e ERROR] [COMMAND]
6+
7+
Run the bootstrap and compare the result with given inputs
8+
9+
-h display this help and exit
10+
-d print out stdout and stderr before assertions
11+
-s STATUS ensure STATUS matches the bootstrap exit code
12+
-o OUTPUT ensure the OUTPUT matches the bootstrap STDOUT (can be a file)
13+
-e ERROR ensure the ERROR matches the bootstrap STDERR (can be a file)
14+
EOF
15+
}
16+
17+
while getopts "hds:o:e:" opt; do
18+
case "$opt" in
19+
h)
20+
show_help
21+
exit 0
22+
;;
23+
s)
24+
status=$OPTARG
25+
;;
26+
o)
27+
output=$OPTARG
28+
[ -f "$output" ] && output="$(cat "$output")"
29+
;;
30+
e)
31+
error=$OPTARG
32+
[ -f "$error" ] && error="$(cat "$error")"
33+
;;
34+
d)
35+
debug=true
36+
;;
37+
*)
38+
show_help >&2
39+
exit 1
40+
;;
41+
esac
42+
done
43+
44+
shift "$((OPTIND-1))"
45+
46+
out=$(mktemp)
47+
err=$(mktemp)
48+
49+
go run . "$@" 1> "$out" 2> "$err"
50+
rc="$?"
51+
52+
assert() {
53+
local expected="$1"
54+
local actual="$2"
55+
local message="$3"
56+
57+
test -z "$expected" -o "$expected" = "$actual" && return
58+
59+
echo "$message: expected $expected, got $actual" >&2
60+
exit 1
61+
}
62+
63+
if [ -n "$debug" ]; then
64+
echo "==stdout=="
65+
cat "$out"
66+
echo "==stderr=="
67+
cat "$err"
68+
echo "=="
69+
fi
70+
71+
assert "$status" "$rc" "status"
72+
assert "$output" "$(cat "$out")" "stdout"
73+
assert "$error" "$(jq -c 'del(.time)' "$err")" "stderr"

tests/consul.stderr.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{"level":"WARN","msg":"SERVICE_ENV is not set, defaulting to dev"}
2+
{"level":"WARN","msg":"Not loading values from Vault. VAULT_ADDR is not set","env":"dev","service":"my-test-service","product":"test","region":"us-east-1","program":"docker-bootstrap"}

tests/consul.stdout.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
BAR=bar
2+
BAZ=baz
3+
DEV=dev
4+
FOO=foobar
5+
FOOBAR=test

tests/dev.stderr.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"level":"WARN","msg":"SERVICE_ENV is not set, defaulting to dev"}

tests/dev.stdout.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
BAR=bar
2+
BAZ=baz
3+
DEV=vdev
4+
FOO=foobar
5+
FOOBAR=vfoobar
6+
SECRET=my-dev-secret

tests/full.stdout.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
BAR=bar
2+
BAZ=baz
3+
FOO=vfoo
4+
SECRET=my-secret

tests/no-command.stderr.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{"level":"WARN","msg":"SERVICE_ENV is not set, defaulting to dev"}
2+
{"level":"ERROR","msg":"Missing command","env":"dev","service":"my-test-service","product":"test","region":"us-east-1","program":"docker-bootstrap"}

tests/no-config.stderr.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{"level":"WARN","msg":"SERVICE_ENV is not set, defaulting to dev"}
2+
{"level":"WARN","msg":"Not loading values from Consul. CONSUL_ADDR is not set","env":"dev","service":"my-test-service","product":"test","region":"us-east-1","program":"docker-bootstrap"}
3+
{"level":"WARN","msg":"Not loading values from Vault. VAULT_ADDR is not set","env":"dev","service":"my-test-service","product":"test","region":"us-east-1","program":"docker-bootstrap"}

tests/no-service.stderr.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"level":"WARN","msg":"SERVICE_NAME is not set, will not load service values"}

tests/no-service.stdout.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
BAR=bar
2+
FOO=foo
3+
SECRET=my-secret

tests/print-env

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
env | sort | grep -E "^($(IFS="|" ; echo "$*"))"

tests/setup

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
consul() {
5+
local key="$1"
6+
local value="$2"
7+
8+
curl -X PUT --data "$value" "http://127.0.0.1:8500/v1/kv/$key"
9+
}
10+
11+
vault() {
12+
local key="$1"
13+
local value="$2"
14+
15+
curl -X POST --header "X-Vault-Token: vault-auth-token" --data "{\"value\":\"$value\"}" "http://127.0.0.1:8200/v1/secret/$key"
16+
}
17+
18+
# Turn off kv v2 and enable kv v1
19+
curl -X DELETE --header "X-Vault-Token: vault-auth-token" http://127.0.0.1:8200/v1/sys/mounts/secret
20+
curl -X POST --header "X-Vault-Token: vault-auth-token" --data '{"type":"kv","path":"secret/","options":{"version":"1"}}' http://127.0.0.1:8200/v1/sys/mounts/secret

tests/vault.stderr.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"level":"WARN","msg":"Not loading values from Consul. CONSUL_ADDR is not set","env":"stage","service":"my-test-service","product":"test","region":"us-east-1","program":"docker-bootstrap"}

tests/vault.stdout.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FOO=vfoo
2+
SECRET=my-secret

0 commit comments

Comments
 (0)