Skip to content

Commit 16f7e19

Browse files
committed
tests: per-container Pre-Hooks and Post-Hooks
1 parent 084766d commit 16f7e19

File tree

1 file changed

+73
-25
lines changed

1 file changed

+73
-25
lines changed

test/tests/acme_hooks/run.sh

Lines changed: 73 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
#!/bin/bash
22

33
## Test for the hooks of acme.sh
4-
pre_hook_file="/tmp/prehook"
5-
pre_hook_command="touch $pre_hook_file"
6-
post_hook_file="/tmp/posthook"
7-
post_hook_command="touch $post_hook_file"
84

5+
default_pre_hook_file="/tmp/default_prehook"
6+
default_pre_hook_command="touch $default_pre_hook_file"
7+
default_post_hook_file="/tmp/default_posthook"
8+
default_post_hook_ommand="touch $default_post_hook_file"
99

10+
percontainer_pre_hook_file="/tmp/percontainer_prehook"
11+
percontainer_pre_hook_command="touch $percontainer_pre_hook_file"
12+
percontainer_post_hook_file="/tmp/percontainer_posthook"
13+
percontainer_post_hook_command="touch $percontainer_post_hook_file"
1014

1115
if [[ -z $GITHUB_ACTIONS ]]; then
1216
le_container_name="$(basename "${0%/*}")_$(date "+%Y-%m-%d_%H.%M.%S")"
1317
else
1418
le_container_name="$(basename "${0%/*}")"
1519
fi
16-
run_le_container "${1:?}" "$le_container_name" --cli-args "--env ACME_PRE_HOOK=$pre_hook_command" --cli-args "--env ACME_POST_HOOK=$post_hook_command"
20+
run_le_container "${1:?}" "$le_container_name" \
21+
--cli-args "--env ACME_PRE_HOOK=$default_pre_hook_command" \
22+
--cli-args "--env ACME_POST_HOOK=$default_post_hook_ommand"
1723

1824
# Create the $domains array from comma separated domains in TEST_DOMAINS.
1925
IFS=',' read -r -a domains <<< "$TEST_DOMAINS"
@@ -29,43 +35,85 @@ function cleanup {
2935
}
3036
trap cleanup EXIT
3137

32-
# Run an nginx container for ${domains[0]} with LETSENCRYPT_EMAIL set.
3338
container_email="contact@${domains[0]}"
34-
run_nginx_container --hosts "${domains[0]}" --cli-args "--env LETSENCRYPT_EMAIL=${container_email}"
39+
40+
# Run an nginx container for ${domains[0]} with LETSENCRYPT_EMAIL set.
41+
run_nginx_container --hosts "${domains[0]}" \
42+
--cli-args "--env LETSENCRYPT_EMAIL=${container_email}"
43+
44+
# Run an nginx container for ${domains[1]} with LETSENCRYPT_EMAIL, ACME_PRE_HOOK and ACME_POST_HOOK set.
45+
run_nginx_container --hosts "${domains[1]}" \
46+
--cli-args "--env LETSENCRYPT_EMAIL=${container_email}" \
47+
--cli-args "--env ACME_PRE_HOOK=$percontainer_pre_hook_command" \
48+
--cli-args "--env ACME_POST_HOOK=$percontainer_post_hook_command"
3549

3650
# Wait for a symlink at /etc/nginx/certs/${domains[0]}.crt
3751
wait_for_symlink "${domains[0]}" "$le_container_name"
3852

39-
##Check if the command is deliverd properly in /etc/acme.sh
53+
acme_pre_hook_key="Le_PreHook="
54+
acme_post_hook_key="Le_PostHook="
55+
acme_base64_start="'__ACME_BASE64__START_"
56+
acme_base64_end="__ACME_BASE64__END_'"
57+
58+
# Check if the default command is deliverd properly in /etc/acme.sh
4059
if docker exec "$le_container_name" [[ ! -d "/etc/acme.sh/$container_email" ]]; then
4160
echo "The /etc/acme.sh/$container_email folder does not exist."
4261
elif docker exec "$le_container_name" [[ ! -d "/etc/acme.sh/$container_email/${domains[0]}" ]]; then
4362
echo "The /etc/acme.sh/$container_email/${domains[0]} folder does not exist."
4463
elif docker exec "$le_container_name" [[ ! -f "/etc/acme.sh/$container_email/${domains[0]}/${domains[0]}.conf" ]]; then
4564
echo "The /etc/acme.sh/$container_email/${domains[0]}/${domains[0]}.conf file does not exist."
4665
fi
47-
acme_pre_hook_key="Le_PreHook="
48-
acme_post_hook_key="Le_PostHook="
49-
acme_base64_start="'__ACME_BASE64__START_"
50-
acme_base64_end="__ACME_BASE64__END_'"
51-
pre_hook_command_base64=$(echo -n "$pre_hook_command" | base64)
52-
post_hook_command_base64=$(echo -n "$post_hook_command" | base64)
5366

54-
acme_pre_hook="$(docker exec "$le_container_name" grep "$acme_pre_hook_key" "/etc/acme.sh/$container_email/${domains[0]}/${domains[0]}.conf")"
55-
acme_post_hook="$(docker exec "$le_container_name" grep "$acme_post_hook_key" "/etc/acme.sh/$container_email/${domains[0]}/${domains[0]}.conf")"
67+
default_pre_hook_command_base64="${acme_pre_hook_key}${acme_base64_start}$(echo -n "$default_pre_hook_command" | base64)${acme_base64_end}"
68+
default_post_hook_command_base64="${acme_post_hook_key}${acme_base64_start}$(echo -n "$default_post_hook_ommand" | base64)${acme_base64_end}"
69+
70+
default_acme_pre_hook="$(docker exec "$le_container_name" grep "$acme_pre_hook_key" "/etc/acme.sh/$container_email/${domains[0]}/${domains[0]}.conf")"
71+
default_acme_post_hook="$(docker exec "$le_container_name" grep "$acme_post_hook_key" "/etc/acme.sh/$container_email/${domains[0]}/${domains[0]}.conf")"
72+
73+
if [[ "$default_pre_hook_command_base64" != "$default_acme_pre_hook" ]]; then
74+
echo "Default prehook command not saved properly"
75+
fi
76+
if [[ "$default_post_hook_command_base64" != "$default_acme_post_hook" ]]; then
77+
echo "Default posthook command not saved properly"
78+
fi
79+
80+
81+
# Check if the default action is performed
82+
if docker exec "$le_container_name" [[ ! -f "$default_pre_hook_file" ]]; then
83+
echo "Default prehook action failed"
84+
fi
85+
if docker exec "$le_container_name" [[ ! -f "$default_post_hook_file" ]]; then
86+
echo "Default posthook action failed"
87+
fi
88+
89+
# Wait for a symlink at /etc/nginx/certs/${domains[1]}.crt
90+
wait_for_symlink "${domains[1]}" "$le_container_name"
91+
92+
# Check if the per-container command is deliverd properly in /etc/acme.sh
93+
if docker exec "$le_container_name" [[ ! -d "/etc/acme.sh/$container_email/${domains[1]}" ]]; then
94+
echo "The /etc/acme.sh/$container_email/${domains[1]} folder does not exist."
95+
elif docker exec "$le_container_name" [[ ! -f "/etc/acme.sh/$container_email/${domains[1]}/${domains[1]}.conf" ]]; then
96+
echo "The /etc/acme.sh/$container_email/${domains[1]}/${domains[1]}.conf file does not exist."
97+
fi
98+
99+
percontainer_pre_hook_command_base64="${acme_pre_hook_key}${acme_base64_start}$(echo -n "$percontainer_pre_hook_command" | base64)${acme_base64_end}"
100+
percontainer_post_hook_command_base64="${acme_post_hook_key}${acme_base64_start}$(echo -n "$percontainer_post_hook_command" | base64)${acme_base64_end}"
101+
102+
percontainer_acme_pre_hook="$(docker exec "$le_container_name" grep "$acme_pre_hook_key" "/etc/acme.sh/$container_email/${domains[1]}/${domains[1]}.conf")"
103+
percontainer_acme_post_hook="$(docker exec "$le_container_name" grep "$acme_post_hook_key" "/etc/acme.sh/$container_email/${domains[1]}/${domains[1]}.conf")"
56104

57-
if [[ "$acme_pre_hook_key$acme_base64_start$pre_hook_command_base64$acme_base64_end" != "$acme_pre_hook" ]]; then
58-
echo "Prehook command not saved properly"
105+
if [[ "$percontainer_pre_hook_command_base64" != "$percontainer_acme_pre_hook" ]]; then
106+
echo "Per-container prehook command not saved properly"
59107
fi
60-
if [[ "$acme_post_hook_key$acme_base64_start$post_hook_command_base64$acme_base64_end" != "$acme_post_hook" ]]; then
61-
echo "Posthook command not saved properly"
108+
if [[ "$percontainer_post_hook_command_base64" != "$percontainer_acme_post_hook" ]]; then
109+
echo "Per-container posthook command not saved properly"
62110
fi
63111

64112

65-
## Check if the action ist performed
66-
if docker exec "$le_container_name" [[ ! -f "$pre_hook_file" ]]; then
67-
echo "Prehook action failed"
113+
# Check if the percontainer action is performed
114+
if docker exec "$le_container_name" [[ ! -f "$percontainer_pre_hook_file" ]]; then
115+
echo "Per-container prehook action failed"
68116
fi
69-
if docker exec "$le_container_name" [[ ! -f "$post_hook_file" ]]; then
70-
echo "Posthook action failed"
117+
if docker exec "$le_container_name" [[ ! -f "$percontainer_post_hook_file" ]]; then
118+
echo "Per-container posthook action failed"
71119
fi

0 commit comments

Comments
 (0)