Skip to content

Commit c24194b

Browse files
committed
Initial add of bats tests
1 parent e28964a commit c24194b

File tree

14 files changed

+170
-156
lines changed

14 files changed

+170
-156
lines changed

.github/workflows/release.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Source: https://github.com/panubo/reference-github-actions/blob/master/github-release.yml
2+
3+
on:
4+
push:
5+
tags:
6+
- '*'
7+
8+
name: GitHub Release
9+
10+
jobs:
11+
build:
12+
name: Create GitHub Release
13+
runs-on: ubuntu-latest
14+
steps:
15+
16+
- name: Checkout code
17+
uses: actions/checkout@v2
18+
with:
19+
fetch-depth: 0
20+
21+
- name: Get Release Notes
22+
id: get_release_notes
23+
run: |
24+
NOTES=$(git log --pretty=format:%s $(git tag --sort=-refname | head -1)...$(git tag --sort=-refname | head -2 | tail -1) | awk '{ print "-", $0 }')
25+
NOTES="${NOTES//'%'/'%25'}"
26+
NOTES="${NOTES//$'\n'/'%0A'}"
27+
NOTES="${NOTES//$'\r'/'%0D'}"
28+
echo "NOTES: ${NOTES}"
29+
echo "::set-output name=notes::${NOTES}"
30+
31+
- name: Create Release
32+
id: create_release
33+
uses: actions/create-release@v1
34+
env:
35+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
36+
with:
37+
tag_name: ${{ github.ref }}
38+
release_name: ${{ github.ref }}
39+
body: |
40+
Changes since last release:
41+
42+
${{ steps.get_release_notes.outputs.notes }}
43+
44+
draft: true
45+
prerelease: false

.github/workflows/test.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
on:
2+
push:
3+
4+
name: Run Tests
5+
6+
jobs:
7+
build:
8+
name: Tests
9+
runs-on: ubuntu-latest
10+
steps:
11+
12+
- name: Setup BATS
13+
uses: mig4/setup-bats@v1
14+
with:
15+
bats-version: 1.3.0
16+
17+
- name: Checkout code
18+
uses: actions/checkout@v2
19+
with:
20+
fetch-depth: 0
21+
22+
- name: Build image
23+
run: |
24+
make build
25+
26+
- name: Tests
27+
run: |
28+
make test

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ build-quick:
1111
docker build -t $(IMAGE_NAME):$(TAG) .
1212

1313
test:
14-
./tests/dind-runner.sh
14+
bats -r tests/
1515

1616
push:
1717
docker push $(IMAGE_NAME):$(TAG)

tests/00-smoke-test.bats

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load test_functions.bash
2+
3+
@test "smoke test - select 1" {
4+
run docker run --rm \
5+
-e DATABASE_HOST=${postgres_container_ip} \
6+
-e DATABASE_USERNAME=postgres \
7+
-e DATABASE_PASSWORD=password \
8+
panubo/postgres-toolbox psql -- -c 'SELECT 1;'
9+
diag "${output}"
10+
[[ "${status}" -eq 0 ]]
11+
}

tests/config.sh

Lines changed: 0 additions & 4 deletions
This file was deleted.

tests/create-user-db.bats

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
load test_functions.bash
2+
3+
@test "create-user-db" {
4+
run docker run --rm \
5+
-e DATABASE_HOST=${postgres_container_ip} \
6+
-e DATABASE_USERNAME=postgres \
7+
-e DATABASE_PASSWORD=password \
8+
panubo/postgres-toolbox create-user-db myuser myuserpassword
9+
diag "${output}"
10+
[[ "${status}" -eq 0 ]]
11+
12+
run docker run --rm \
13+
-e DATABASE_HOST=${postgres_container_ip} \
14+
-e DATABASE_USERNAME=myuser \
15+
-e DATABASE_PASSWORD=myuserpassword \
16+
panubo/postgres-toolbox psql -- -c 'SELECT current_database();'
17+
diag "${output}"
18+
[[ "${status}" -eq 0 ]]
19+
}
20+
21+
# echo "===> Test create-user-db command"
22+
# docker run -d --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=password postgres:$POSTGRES_VERSION > /dev/null
23+
# sleep 5
24+
# docker run -t -i --name $TEST_NAME --link postgres $TEST_CONTAINER create-user-db foo foopass
25+
# cleanup postgres $TEST_NAME

tests/drop-user-db.bats

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# echo "===> Test delete-user-db command"
2+
# docker run -d --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=password postgres:$POSTGRES_VERSION > /dev/null
3+
# sleep 5
4+
# docker run -t -i --name ${TEST_NAME}-create --link postgres $TEST_CONTAINER create-user-db foo
5+
# docker run -t -i --name ${TEST_NAME}-delete --link postgres $TEST_CONTAINER delete-user-db foo
6+
# cleanup postgres ${TEST_NAME}-create ${TEST_NAME}-delete

tests/functions.sh

Lines changed: 0 additions & 73 deletions
This file was deleted.

tests/load.bats

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# echo "===> Test load command"
2+
# docker run -d --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=password postgres:$POSTGRES_VERSION > /dev/null
3+
# sleep 5
4+
# docker run -t -i --name ${TEST_NAME}-create1 --link postgres $TEST_CONTAINER create-user-db foo
5+
# docker run -t -i --name ${TEST_NAME}-save --link postgres -e DUMP_DIR="/srv" -v /srv:/srv $TEST_CONTAINER save
6+
# docker run -t -i --name ${TEST_NAME}-delete --link postgres $TEST_CONTAINER delete-user-db foo
7+
# docker run -t -i --name ${TEST_NAME}-create2 --link postgres $TEST_CONTAINER create-user-db foo
8+
# docker run -t -i --name ${TEST_NAME}-load --link postgres -e DUMP_DIR="/srv" -v /srv:/srv $TEST_CONTAINER load foo
9+
# cleanup postgres ${TEST_NAME}-create1 ${TEST_NAME}-save ${TEST_NAME}-delete ${TEST_NAME}-load ${TEST_NAME}-create2

tests/runner.sh

Lines changed: 0 additions & 24 deletions
This file was deleted.

tests/save.bats

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# echo "===> Test save command"
2+
# docker run -d --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=password postgres:$POSTGRES_VERSION > /dev/null
3+
# sleep 5
4+
# docker run -t -i --name ${TEST_NAME}-create --link postgres $TEST_CONTAINER create-user-db foo
5+
# docker run -t -i --name ${TEST_NAME}-save --link postgres -e DUMP_DIR="/srv" $TEST_CONTAINER save
6+
# cleanup postgres ${TEST_NAME}-create ${TEST_NAME}-save

tests/test.sh

Lines changed: 0 additions & 54 deletions
This file was deleted.

tests/test_functions.bash

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
TOOLBOX_IMAGE="panubo/postgres-toolbox:latest"
2+
POSTGRES_TARGET_VERSION="12.7"
3+
4+
diag() {
5+
echo "$@" | sed -e 's/^/# /' >&3 ;
6+
}
7+
8+
setup() {
9+
# setup runs before each test
10+
# Important: we aren't exposing port etc so running tests in parallel works
11+
postgres_container="$(docker run -d -e POSTGRES_PASSWORD=password postgres:${POSTGRES_TARGET_VERSION})"
12+
postgres_container_ip="$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' ${postgres_container})"
13+
docker run --rm -e DATABASE_HOST=${postgres_container_ip} \
14+
-e DATABASE_USERNAME=postgres \
15+
-e DATABASE_PASSWORD=password \
16+
${TOOLBOX_IMAGE} bash -c '. /panubo-functions.sh; wait_postgres ${DATABASE_HOST}'
17+
}
18+
19+
teardown() {
20+
# teardown runs after each test
21+
docker rm -f "${postgres_container}"
22+
}

tests/vaccumdb.bats

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
load test_functions.bash
2+
3+
@test "vacuum" {
4+
run docker run --rm \
5+
-e DATABASE_HOST=${postgres_container_ip} \
6+
-e DATABASE_USERNAME=postgres \
7+
-e DATABASE_PASSWORD=password \
8+
panubo/postgres-toolbox vacuumdb -- --maintenance-db=postgres --all
9+
diag "${output}"
10+
[[ "${status}" -eq 0 ]]
11+
}
12+
13+
# echo "===> Test vacuum command"
14+
# docker run -d --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=password postgres:$POSTGRES_VERSION > /dev/null
15+
# sleep 5
16+
# docker run -t -i --name $TEST_NAME --link postgres $TEST_CONTAINER vacuum
17+
# cleanup postgres $TEST_NAME

0 commit comments

Comments
 (0)