Skip to content

Commit 2f4b655

Browse files
authored
test: add test for separate_server option (#102)
1 parent 0adcc02 commit 2f4b655

File tree

6 files changed

+75
-6
lines changed

6 files changed

+75
-6
lines changed

.env.jinja

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ DB_ROOT_PASSWORD="{{ db_root_password }}"
2222
DB_USE_TLS={{ db_use_tls | int }}
2323
{% if use_custom_certs -%}
2424
DB_CERTS=/certs/db-certs.crt
25+
{%- else -%}
26+
DB_CERTS=/etc/ssl/certs/ca-certificates.crt
2527
{%- endif %}
2628

2729
SOURCE_SYSTEM_HOST=

.github/workflows/ci.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ jobs:
5050
version: "0.9.26"
5151
- name: Test template using copier-template-tester
5252
run: |
53-
docker container ps
5453
uv run --with copier --with copier-templates-extensions --with bcrypt --with copier-template-tester ctt
5554
- name: Show running containers
5655
run: |

compose.yaml.jinja

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ services:
5757
{% if db_host_type == "container" -%}
5858
depends_on:
5959
- db
60-
{%- elif db_host_type == "same_server" -%}
60+
{%- elif db_host_type == "same_server" or is_test -%}
6161
extra_hosts:
6262
- "host.docker.internal:host-gateway"
6363
{%- endif %}
@@ -98,7 +98,7 @@ services:
9898
{% if db_host_type == "container" -%}
9999
depends_on:
100100
- db
101-
{%- elif db_host_type == "same_server" -%}
101+
{%- elif db_host_type == "same_server" or is_test -%}
102102
extra_hosts:
103103
- "host.docker.internal:host-gateway"
104104
{%- endif %}
@@ -135,7 +135,7 @@ services:
135135
{% if db_host_type == "container" -%}
136136
depends_on:
137137
- db
138-
{%- elif db_host_type == "same_server" -%}
138+
{%- elif db_host_type == "same_server" or is_test -%}
139139
extra_hosts:
140140
- "host.docker.internal:host-gateway"
141141
{%- endif %}
@@ -170,7 +170,7 @@ services:
170170
{% if db_host_type == "container" -%}
171171
depends_on:
172172
- db
173-
{%- elif db_host_type == "same_server" -%}
173+
{%- elif db_host_type == "same_server" or is_test -%}
174174
extra_hosts:
175175
- "host.docker.internal:host-gateway"
176176
{%- endif %}

copier.yaml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,12 @@ db_use_adminer:
162162
use_custom_certs:
163163
type: bool
164164
help: Do you need to use a custom CA file to verify HTTPS and DB connections?
165-
default: false
165+
default: |-
166+
{% if certificate_type == 'file' or db_use_tls -%}
167+
{{ UNSET }}
168+
{%- else -%}
169+
false
170+
{%- endif %}
166171
when: "{{ certificate_type == 'file' or db_use_tls }}"
167172

168173
use_ofelia:
@@ -257,6 +262,15 @@ _message_after_copy: |
257262
258263
_tasks:
259264
# validation
265+
- "echo Checking that the compose file has no warnings"
266+
- command: |
267+
if docker compose config --quiet 2>&1 | grep --quiet level=; then
268+
echo compose config has errors or warnings
269+
docker compose config --quiet 2>&1
270+
exit 1
271+
else
272+
echo compose config has no errors or warnings
273+
fi
260274
- "echo Checking that required files are provided"
261275
- command: |
262276
echo Firebase admin key missing at {{ extra_files + '/firebase-admin-key.json' }}
@@ -382,4 +396,5 @@ _exclude:
382396
- "zizmor.yml"
383397
# - "README.md"
384398
- extensions
399+
- "{% if not is_test %}scripts/cleanup_db.sh{% endif %}"
385400
- "{% if not is_test %}tests{% endif %}"

ctt.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,22 @@ db_host_type = "same_server"
4545
db_port = 3307
4646
db_root_user = "root"
4747
db_root_password = "root-password"
48+
_extra_tasks = [
49+
# need to clean up on existing DB server
50+
"ENVIRONMENT='{{ environment }}' DB_ROOT_USER='{{ db_root_user }}' DB_ROOT_PASSWORD='{{ db_root_password }}' DB_HOST='{{ db_host }}' DB_PORT='{{ db_port }}' DB_USER='{{ db_user }}' DB_NAME=admin ./scripts/cleanup_db.sh",
51+
"docker compose down",
52+
]
53+
54+
# DB on a different server requires a db_host
55+
[output.".ctt/db_different_server"]
56+
db_host_type = "separate_server"
57+
# use the service container to pretend it is a separate server
58+
# very tricky to test in CI with a dedicated hostname
59+
db_host = "host.docker.internal"
60+
db_port = 3307
61+
db_root_user = "root"
62+
db_root_password = "root-password"
63+
# force no TLS during test, requires certificate otherwise
64+
# TODO: add test case with TLS enabled
65+
db_use_tls = 0
66+
use_custom_certs = false

scripts/cleanup_db.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
# renovate: datasource=docker depName=alpine
5+
ALPINE_VERSION="3.23.2"
6+
7+
echo "Running container for mysql-client..."
8+
docker run --rm --interactive \
9+
--env DB_ROOT_USER=${DB_ROOT_USER} \
10+
--env DB_ROOT_PASSWORD=${DB_ROOT_PASSWORD} \
11+
--env DB_HOST=${DB_HOST} \
12+
--env DB_USER=${DB_USER} \
13+
--env DB_NAME=${DB_NAME} \
14+
--network opal-${ENVIRONMENT} \
15+
--add-host "host.docker.internal:host-gateway" \
16+
alpine:${ALPINE_VERSION} sh -s << EOF
17+
set -euo pipefail
18+
apk add --no-cache mysql-client
19+
echo "Connecting to DB server on ${DB_HOST}:${DB_PORT}..."
20+
echo "Dropping databases..."
21+
MYSQL_PWD=${DB_ROOT_PASSWORD} mariadb --protocol tcp --skip-ssl --user ${DB_ROOT_USER} --host ${DB_HOST} --port ${DB_PORT} <<'EOIF'
22+
DROP DATABASE IF EXISTS \`$DB_NAME\`;
23+
DROP DATABASE IF EXISTS \`OpalDB\`;
24+
DROP DATABASE IF EXISTS \`QuestionnaireDB\`;
25+
EOIF
26+
echo "Successfully dropped databases"
27+
echo "Dropping DB user ${DB_USER}..."
28+
MYSQL_PWD=${DB_ROOT_PASSWORD} mariadb --protocol tcp --skip-ssl --user ${DB_ROOT_USER} --host ${DB_HOST} --port ${DB_PORT} <<'EOIF'
29+
DROP USER IF EXISTS '$DB_USER'@'%';
30+
FLUSH PRIVILEGES;
31+
EOIF
32+
echo "Successfully dropped DB user ${DB_USER}"
33+
EOF
34+
echo "Done!"

0 commit comments

Comments
 (0)