Skip to content

Commit 6dd7bc4

Browse files
committed
Test unit for standalone certificates
1 parent 8a936cc commit 6dd7bc4

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ nginx.tmpl
99
test/local_test_env.sh
1010
test/tests/docker_api/expected-std-out.txt
1111
test/tests/container_restart/docker_event_out.txt
12+
test/tests/certs_standalone/letsencrypt_user_data

test/config.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ imageTests+=(
1313
certs_single
1414
certs_san
1515
certs_single_domain
16+
certs_standalone
1617
force_renew
1718
certs_validity
1819
container_restart

test/setup/setup-nginx-proxy.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ case $SETUP in
99
--name $NGINX_CONTAINER_NAME \
1010
--env "DHPARAM_BITS=256" \
1111
-v /etc/nginx/vhost.d \
12+
-v /etc/nginx/conf.d \
1213
-v /usr/share/nginx/html \
1314
-v /var/run/docker.sock:/tmp/docker.sock:ro \
1415
--label com.github.jrcs.letsencrypt_nginx_proxy_companion.test_suite \
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Started letsencrypt container for test certs_standalone
2+
Symlink to le1.wtf certificate has been generated.
3+
The link is pointing to the file ./le1.wtf/fullchain.pem
4+
Domain le1.wtf is on certificate.
5+
Symlink to le2.wtf certificate has been generated.
6+
The link is pointing to the file ./le2.wtf/fullchain.pem
7+
Domain le2.wtf is on certificate.
8+
Domain le3.wtf is on certificate.

test/tests/certs_standalone/run.sh

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/bin/bash
2+
3+
## Test for standalone certificates.
4+
5+
if [[ -z $TRAVIS_CI ]]; then
6+
le_container_name="$(basename ${0%/*})_$(date "+%Y-%m-%d_%H.%M.%S")"
7+
else
8+
le_container_name="$(basename ${0%/*})"
9+
fi
10+
11+
# Create the $domains array from comma separated domains in TEST_DOMAINS.
12+
IFS=',' read -r -a domains <<< "$TEST_DOMAINS"
13+
14+
# Cleanup function with EXIT trap
15+
function cleanup {
16+
# Cleanup the files created by this run of the test to avoid foiling following test(s).
17+
docker exec "$le_container_name" bash -c 'rm -rf /etc/nginx/certs/le?.wtf*'
18+
# Stop the LE container
19+
docker stop "$le_container_name" > /dev/null
20+
}
21+
trap cleanup EXIT
22+
23+
# Create letsencrypt_user_data with a single domain cert
24+
cat > ${TRAVIS_BUILD_DIR}/test/tests/certs_standalone/letsencrypt_user_data <<EOF
25+
LETSENCRYPT_STANDALONE_CERTS=('single')
26+
LETSENCRYPT_single_HOST=('${domains[0]}')
27+
EOF
28+
29+
run_le_container ${1:?} "$le_container_name" \
30+
"--volume ${TRAVIS_BUILD_DIR}/test/tests/certs_standalone/letsencrypt_user_data:/app/letsencrypt_user_data"
31+
32+
# Wait for a symlink at /etc/nginx/certs/${domains[0]}.crt
33+
# then grab the certificate in text form ...
34+
wait_for_symlink "${domains[0]}" "$le_container_name"
35+
created_cert="$(docker exec "$le_container_name" \
36+
openssl x509 -in /etc/nginx/certs/${domains[0]}/cert.pem -text -noout)"
37+
38+
# Check if the domain is on the certificate.
39+
if grep -q "${domains[0]}" <<< "$created_cert"; then
40+
echo "Domain ${domains[0]} is on certificate."
41+
else
42+
echo "Domain ${domains[0]} did not appear on certificate."
43+
fi
44+
45+
docker exec "$le_container_name" bash -c "[[ -f /etc/nginx/conf.d/standalone-cert-${domains[0]}.conf ]]" \
46+
&& echo "Standalone configuration for ${domains[0]} wasn't correctly removed."
47+
48+
# Add another (SAN) certificate to letsencrypt_user_data
49+
cat > ${TRAVIS_BUILD_DIR}/test/tests/certs_standalone/letsencrypt_user_data <<EOF
50+
LETSENCRYPT_STANDALONE_CERTS=('single' 'san')
51+
LETSENCRYPT_single_HOST=('${domains[0]}')
52+
LETSENCRYPT_san_HOST=('${domains[1]}' '${domains[2]}')
53+
EOF
54+
55+
# Manually trigger the service loop
56+
docker exec "$le_container_name" /app/signal_le_service > /dev/null
57+
58+
# Wait for a symlink at /etc/nginx/certs/${domains[1]}.crt
59+
# then grab the certificate in text form ...
60+
wait_for_symlink "${domains[1]}" "$le_container_name"
61+
created_cert="$(docker exec "$le_container_name" \
62+
openssl x509 -in /etc/nginx/certs/${domains[1]}/cert.pem -text -noout)"
63+
64+
for domain in "${domains[1]}" "${domains[2]}"; do
65+
# Check if the domain is on the certificate.
66+
if grep -q "$domain" <<< "$created_cert"; then
67+
echo "Domain $domain is on certificate."
68+
else
69+
echo "Domain $domain did not appear on certificate."
70+
fi
71+
done
72+
73+
docker exec "$le_container_name" bash -c "[[ ! -f /etc/nginx/conf.d/standalone-cert-${domains[1]}.conf ]]" \
74+
|| echo "Standalone configuration for ${domains[1]} wasn't correctly removed."

0 commit comments

Comments
 (0)