Skip to content

Commit ac112f8

Browse files
authored
Merge pull request #3 from panubo/add_tests
Add tests & fixes
2 parents e28964a + 41e5ab1 commit ac112f8

21 files changed

+348
-190
lines changed

.github/workflows/release.yml

+45
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

+28
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-with-cache
25+
26+
- name: Tests
27+
run: |
28+
make test

Makefile

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ build:
1010
build-quick:
1111
docker build -t $(IMAGE_NAME):$(TAG) .
1212

13+
build-with-cache:
14+
# Used by CI to speed up build and test process
15+
docker pull $(IMAGE_NAME):$(TAG)
16+
docker build -t $(IMAGE_NAME):$(TAG) --cache-from $(IMAGE_NAME):$(TAG) .
17+
1318
test:
14-
./tests/dind-runner.sh
19+
bats -r tests/
1520

1621
push:
1722
docker push $(IMAGE_NAME):$(TAG)

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ Or alternatively specify the environment variables:
3939

4040
Some subcommands require additional environment parameters.
4141

42+
## Testing
43+
44+
[bats](https://bats-core.readthedocs.io/en/stable/index.html) is used for testing. To test the image and commands bats and docker are required. Use the following commands to run all of the tests.
45+
46+
```
47+
make build-with-cache # or make build
48+
make test
49+
```
50+
51+
All tests are kept in `tests/` and all of the extension `.bats`. `test_functions.bash` is also loaded by each test. The functions include a setup and teardown (see bats docs) which creates and destroys a postgres target server.
52+
53+
Using bats setup and teardown and avoiding exposing postgres ports etc should allow tests to be run in parallel.
54+
4255
## Status
4356

4457
Feature incomplete. Work in progress.

commands/common.sh

+9-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ parse_options() {
8686

8787
# Options and long options
8888
local options="h:p:U:d:W:"
89-
local longopts="host:,port:,username:,dbname:,password:,password-file:,no-create-database,no-revoke-public-create,drop-database,format:,compression:,skip-globals,skip-database:,skip-analyze,full-count"
89+
local longopts="host:,port:,username:,dbname:,password:,password-file:,no-create-database,no-revoke-public-create,drop-database,format:,compression:,umask:,skip-globals,skip-database:,skip-analyze,full-count,aws-args:"
9090
local parsed
9191

9292
# Parse with getopt (not getopts)
@@ -138,6 +138,10 @@ parse_options() {
138138
format="${2}"
139139
shift 2
140140
;;
141+
--umask)
142+
umask="${2}"
143+
shift 2
144+
;;
141145
--compression)
142146
compression="${2}"
143147
shift 2
@@ -154,6 +158,10 @@ parse_options() {
154158
full_count="true"
155159
shift
156160
;;
161+
--aws-args)
162+
IFS=$' \n\t' read -r -a aws_args <<<"${2}"
163+
shift 2
164+
;;
157165
--)
158166
shift
159167
break

commands/create-user-db

+4-5
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ wait_postgres
3333

3434
# Is this a bad idea?
3535
# Required for the GRANT below
36-
current_user="${user:-${PGUSER:-${USER}}}"
37-
36+
current_user="${username:-${PGUSER:-${USER}}}"
3837

3938
echoerr ">>> Creating new user (ROLE)"
4039
# Check is new user already exists
@@ -44,7 +43,7 @@ if [[ "$(/usr/bin/psql "${connection[@]}" -tAc "SELECT 1 FROM pg_roles WHERE rol
4443
new_password="$(genpasswd)"
4544
echoerr ">>> Generated a random password: ${new_password}"
4645
else
47-
new_password="${args[2]}"
46+
new_password="${args[1]}"
4847
fi
4948
/usr/bin/psql "${connection[@]}" -c "CREATE ROLE ${new_user} WITH LOGIN PASSWORD '${new_password}';"
5049
else
@@ -70,9 +69,9 @@ else
7069
# shellcheck disable=SC2034
7170
dbname="${new_user}"
7271
connection=()
73-
for item in host port user dbname; do
72+
for item in host port username dbname; do
7473
if [[ -n "${!item:-}" ]]; then
75-
connection+=("--${item}" "${!item}")
74+
connection+=("--${item}=${!item}")
7675
fi
7776
done
7877

commands/save

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ usage() {
2828
echoerr ""
2929
echoerr "Options:"
3030
echoerr " --pgdump-args NOT IMPLEMENTED"
31+
echoerr " --aws-args OPTIONS"
3132
echoerr " --format plain|custom"
3233
echoerr " --compression gzip|lz4|bz2|none"
3334
echoerr " --date-format %Y%m%d%H%M%S"
@@ -61,7 +62,7 @@ case "${dest}" in
6162
;;
6263
s3://*)
6364
echo ">> aws s3"
64-
save_cmd=( "aws" "s3" "sync" "--no-progress" )
65+
save_cmd=( "aws" "${aws_args[@]}" "s3" "sync" "--no-progress" )
6566
dest_type="s3"
6667
;;
6768
file://*|/*|./*)
@@ -195,6 +196,7 @@ if [[ "${dest_type}" == "file" ]]; then
195196
ls -l "${full_path}"
196197
else
197198
echo ">> Uploading to ${full_path}"
199+
echoerr "${save_cmd[@]}" "${save_path}" "${full_path}"
198200
"${save_cmd[@]}" "${save_path}" "${full_path}"
199201

200202
# Clean up the save path if successful and not a file type save

tests/00-smoke-test.bats

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

tests/config.sh

-4
This file was deleted.

tests/create-user-db.bats

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
load test_functions.bash
2+
load standard_setup.bash
3+
4+
@test "create-user-db" {
5+
run docker run --rm \
6+
-e DATABASE_HOST=${postgres_container_ip} \
7+
-e DATABASE_USERNAME=postgres \
8+
-e DATABASE_PASSWORD=password \
9+
panubo/postgres-toolbox create-user-db myuser myuserpassword
10+
diag "${output}"
11+
[[ "${status}" -eq 0 ]]
12+
13+
run docker run --rm \
14+
-e DATABASE_HOST=${postgres_container_ip} \
15+
-e DATABASE_USERNAME=myuser \
16+
-e DATABASE_PASSWORD=myuserpassword \
17+
panubo/postgres-toolbox psql -- -c 'SELECT current_database();'
18+
diag "${output}"
19+
[[ "${status}" -eq 0 ]]
20+
# check the output of the second last line
21+
[[ "${lines[-2]}" = " myuser" ]]
22+
}

tests/dind-runner.sh

-27
This file was deleted.

tests/drop-user-db.bats

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
load test_functions.bash
2+
load standard_setup.bash
3+
4+
@test "drop-user-db" {
5+
# create the user
6+
run docker run --rm \
7+
-e DATABASE_HOST=${postgres_container_ip} \
8+
-e DATABASE_USERNAME=postgres \
9+
-e DATABASE_PASSWORD=password \
10+
panubo/postgres-toolbox create-user-db myuser myuserpassword
11+
diag "${output}"
12+
[[ "${status}" -eq 0 ]]
13+
14+
# drop the user
15+
run docker run --rm \
16+
-e DATABASE_HOST=${postgres_container_ip} \
17+
-e DATABASE_USERNAME=postgres \
18+
-e DATABASE_PASSWORD=password \
19+
panubo/postgres-toolbox drop-user-db --drop-database myuser
20+
diag "${output}"
21+
[[ "${status}" -eq 0 ]]
22+
23+
# check the user and db do not exist
24+
run docker run --rm \
25+
-e DATABASE_HOST=${postgres_container_ip} \
26+
-e DATABASE_USERNAME=postgres \
27+
-e DATABASE_PASSWORD=password \
28+
panubo/postgres-toolbox psql -- -c 'SELECT count(*) FROM pg_database WHERE datname='"'myuser'"';'
29+
diag "${output}"
30+
[[ "${lines[-2]}" -eq "0" ]]
31+
}

tests/functions.sh

-73
This file was deleted.

0 commit comments

Comments
 (0)