-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathbuild-and-test.sh
More file actions
executable file
·242 lines (190 loc) · 15.1 KB
/
build-and-test.sh
File metadata and controls
executable file
·242 lines (190 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/env bash
set -eE -o functrace
failure() {
local lineno=$1
local msg=$2
echo "Failed at $lineno: $msg"
}
trap 'failure ${LINENO} "$BASH_COMMAND"' ERR
# Let's replace the "." by a "-" with some bash magic
export BRANCH_VARIANT="${VARIANT//./-}"
# Build with BuildKit https://docs.docker.com/develop/develop-images/build_enhancements/
export DOCKER_BUILDKIT=1 # Force use of BuildKit
export BUILDKIT_STEP_LOG_MAX_SIZE=10485760 # output log limit fixed to 10MiB
# Let's build the "slim" image.
docker build -t "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-slim-${BRANCH_VARIANT}" --build-arg PHP_VERSION="${PHP_VERSION}" --build-arg GLOBAL_VERSION="${BRANCH}" -f "Dockerfile.slim.${VARIANT}" .
# Post build unit tests
# Let's check that the extensions can be built using the "ONBUILD" statement
docker build -t test/slim_onbuild --build-arg PHP_VERSION="${PHP_VERSION}" --build-arg BRANCH="$BRANCH" --build-arg BRANCH_VARIANT="$BRANCH_VARIANT" tests/slim_onbuild
# This should run ok (the sudo disable environment variables but call to composer proxy does not trigger PHP ini file regeneration)
docker run --rm test/slim_onbuild php -m | grep sockets
docker run --rm test/slim_onbuild php -m | grep pdo_pgsql
docker run --rm test/slim_onbuild php -m | grep pdo_sqlite
docker rmi test/slim_onbuild
# Let's check that the extensions are available for composer using "ARG PHP_EXTENSIONS" statement:
docker build -t test/slim_onbuild_composer --build-arg PHP_VERSION="${PHP_VERSION}" --build-arg BRANCH="$BRANCH" --build-arg BRANCH_VARIANT="$BRANCH_VARIANT" tests/slim_onbuild_composer
docker rmi test/slim_onbuild_composer
# Post build unit tests
if [[ $VARIANT == cli* ]]; then CONTAINER_CWD=/usr/src/app; else CONTAINER_CWD=/var/www/html; fi
# Default user is 1000
RESULT="$(docker run --rm "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-slim-${BRANCH_VARIANT}" id -ur)"
[[ "$RESULT" == "1000" ]]
# If mounted, default user has the id of the mount directory
mkdir user1999 && docker run --rm -v "$(pwd)":/mnt busybox chown 1999:1999 /mnt/user1999
ls -al user1999
RESULT="$(docker run --rm -v "$(pwd)"/user1999:$CONTAINER_CWD "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-slim-${BRANCH_VARIANT}" id -ur)"
[[ "$RESULT" == "1999" ]]
# Also, the default user can write on stdout and stderr
docker run --rm -v "$(pwd)"/user1999:$CONTAINER_CWD "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-slim-${BRANCH_VARIANT}" bash -c "echo TEST > /proc/self/fd/2"
rm -rf user1999
# and it also works for users with existing IDs in the container
mkdir -p user33
cp tests/apache/composer.json user33/
docker run --rm -v "$(pwd)":/mnt busybox chown -R 33:33 /mnt/user33
ls -al user33
RESULT="$(docker run --rm -v "$(pwd)"/user33:$CONTAINER_CWD "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-slim-${BRANCH_VARIANT}" id -ur)"
[[ "$RESULT" == "33" ]]
RESULT="$(docker run --rm -v "$(pwd)"/user33:$CONTAINER_CWD "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-slim-${BRANCH_VARIANT}" composer update -vvv)"
docker run --rm -v "$(pwd)":/mnt busybox rm -rf /mnt/user33
# Let's check that mbstring is enabled by default (they are compiled in PHP)
docker run --rm "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-slim-${BRANCH_VARIANT}" php -m | grep mbstring
docker run --rm "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-slim-${BRANCH_VARIANT}" php -m | grep PDO
#docker run --rm "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-slim-${BRANCH_VARIANT}" php -m | grep pdo_sqlite
if [[ $VARIANT == apache* ]]; then
# Test if environment variables are passed to PHP
DOCKER_CID="$(docker run --rm -e MYVAR=foo -p "81:80" -d -v "$(pwd)":/var/www/html "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-slim-${BRANCH_VARIANT}")"
# Let's wait for Apache to start
sleep 5
RESULT="$(curl http://localhost:81/tests/test.php)"
[[ "$RESULT" == "foo" ]]
docker stop "$DOCKER_CID"
# Test Apache document root (relative)
DOCKER_CID="$(docker run --rm -e MYVAR=foo -p "81:80" -d -v "$(pwd)":/var/www/html -e APACHE_DOCUMENT_ROOT=tests "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-slim-${BRANCH_VARIANT}")"
# Let's wait for Apache to start
sleep 5
RESULT="$(curl http://localhost:81/test.php)"
[[ "$RESULT" == "foo" ]]
docker stop "$DOCKER_CID"
# Test Apache document root (absolute)
DOCKER_CID="$(docker run --rm -e MYVAR=foo -p "81:80" -d -v "$(pwd)":/var/www/foo -e APACHE_DOCUMENT_ROOT=/var/www/foo/tests "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-slim-${BRANCH_VARIANT}")"
# Let's wait for Apache to start
sleep 5
RESULT="$(curl http://localhost:81/test.php)"
[[ "$RESULT" == "foo" ]]
docker stop "$DOCKER_CID"
# Test Apache HtAccess
DOCKER_CID="$(docker run --rm -p "81:80" -d -v "$(pwd)"/tests/testHtAccess:/foo -e APACHE_DOCUMENT_ROOT=/foo "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-slim-${BRANCH_VARIANT}")"
# Let's wait for Apache to start
sleep 5
RESULT="$(curl http://localhost:81/)"
[[ "$RESULT" == "foo" ]]
docker stop "$DOCKER_CID"
# Test PHP_INI_... variables are correctly handled by apache
DOCKER_CID="$(docker run --rm -e MYVAR=foo -p "81:80" -d -v "$(pwd)":/var/www/html -e PHP_INI_MEMORY_LIMIT=2G "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-slim-${BRANCH_VARIANT}")"
# Let's wait for Apache to start
sleep 5
RESULT="$(curl http://localhost:81/tests/apache/echo_memory_limit.php)"
[[ "$RESULT" == "2G" ]]
docker stop "$DOCKER_CID"
fi
if [[ $VARIANT == fpm* ]]; then
# Test if environment starts without errors
DOCKER_CID="$(docker run --rm -p "9000:9000" -d -v "$(pwd)":/var/www/html "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-slim-${BRANCH_VARIANT}")"
# Let's wait for FPM to start
sleep 5
# If the container is still up, it will not fail when stopping.
docker stop "$DOCKER_CID"
fi
# Let's check that the access to cron will fail with a message
set +e
RESULT="$(docker run --rm -e CRON_SCHEDULE_1="* * * * * * *" -e CRON_COMMAND_1="(>&1 echo 'foobar')" "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-slim-${BRANCH_VARIANT}" sleep 1 2>&1 | grep -o 'Cron is not available in this image')"
set -e
[[ "$RESULT" == "Cron is not available in this image" ]]
# Let's check that the configuration is loaded from the correct php.ini (development, production or imported in the image)
RESULT="$(docker run --rm "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-slim-${BRANCH_VARIANT}" php -i | grep error_reporting)"
[[ "$RESULT" == "error_reporting => 32767 => 32767" ]]
RESULT="$(docker run --rm -e TEMPLATE_PHP_INI=production "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-slim-${BRANCH_VARIANT}" php -i | grep error_reporting)"
[[ "$RESULT" == "error_reporting => 22527 => 22527" ]]
RESULT="$(docker run --rm -v "$(pwd)/tests/php.ini:/etc/php/${PHP_VERSION}/cli/php.ini" "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-slim-${BRANCH_VARIANT}" php -i | grep error_reporting)"
[[ "$RESULT" == "error_reporting => 24575 => 24575" ]]
RESULT="$(docker run --rm -e PHP_INI_ERROR_REPORTING="E_ERROR | E_WARNING" "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-slim-${BRANCH_VARIANT}" php -i | grep error_reporting)"
[[ "$RESULT" == "error_reporting => 3 => 3" ]]
# Tests that environment variables with an equal sign are correctly handled
RESULT="$(docker run --rm -e PHP_INI_SESSION__SAVE_PATH="tcp://localhost?auth=yourverycomplex\"passwordhere" "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-slim-${BRANCH_VARIANT}" php -i | grep "session.save_path")"
[[ "$RESULT" == "session.save_path => tcp://localhost?auth=yourverycomplex\"passwordhere => tcp://localhost?auth=yourverycomplex\"passwordhere" ]]
# Tests that the SMTP parameter is set in uppercase
RESULT="$(docker run --rm -e PHP_INI_SMTP="192.168.0.1" "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-slim-${BRANCH_VARIANT}" php -i | grep "^SMTP")"
[[ "$RESULT" == "SMTP => 192.168.0.1 => 192.168.0.1" ]]
# Tests that environment variables are passed to startup scripts when UID is set
RESULT="$(docker run --rm -e FOO="bar" -e STARTUP_COMMAND_1="env" -e UID=0 "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-slim-${BRANCH_VARIANT}" sleep 1 | grep "FOO")"
[[ "$RESULT" == "FOO=bar" ]]
# Tests that multi-commands are correctly executed when UID is set
RESULT="$(docker run --rm -e STARTUP_COMMAND_1="cd / && whoami" -e UID=0 "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-slim-${BRANCH_VARIANT}" sleep 1)"
[[ "$RESULT" == "root" ]]
# Tests that startup.sh is correctly executed
docker run --rm -v "$(pwd)"/tests/startup.sh:/etc/container/startup.sh "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-slim-${BRANCH_VARIANT}" php -m | grep "startup.sh executed"
# Tests that disable_functions is commented in php.ini cli
RESULT="$(docker run --rm "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-slim-${BRANCH_VARIANT}" php -i | grep "disable_functions")"
[[ "$RESULT" == "disable_functions => no value => no value" ]]
#################################
# Let's build the "fat" image
#################################
docker build -t "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-${BRANCH_VARIANT}" --build-arg PHP_VERSION="${PHP_VERSION}" --build-arg GLOBAL_VERSION="${BRANCH}" -f "Dockerfile.${VARIANT}" .
# Let's check that the crons are actually sending logs in the right place
RESULT="$(docker run --rm -e CRON_SCHEDULE_1="* * * * * * *" -e CRON_COMMAND_1="(>&1 echo 'foobar')" "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-${BRANCH_VARIANT}" sleep 1 2>&1 | grep -oP 'msg=foobar' | head -n1)"
[[ "$RESULT" == "msg=foobar" ]]
RESULT="$(docker run --rm -e CRON_SCHEDULE_1="* * * * * * *" -e CRON_COMMAND_1="(>&2 echo 'error')" "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-${BRANCH_VARIANT}" sleep 1 2>&1 | grep -oP 'msg=error' | head -n1)"
[[ "$RESULT" == "msg=error" ]]
# Let's check that the cron with a user different from root is actually run.
RESULT="$(docker run --rm -e CRON_SCHEDULE_1="* * * * * * *" -e CRON_COMMAND_1="whoami" -e CRON_USER_1="docker" "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-${BRANCH_VARIANT}" sleep 1 2>&1 | grep -oP 'msg=docker' | head -n1)"
[[ "$RESULT" == "msg=docker" ]]
# Let's check that 2 commands split with a ; are run by the same user.
RESULT="$(docker run --rm -e CRON_SCHEDULE_1="* * * * * * *" -e CRON_COMMAND_1="whoami;whoami" -e CRON_USER_1="docker" "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-${BRANCH_VARIANT}" sleep 1 2>&1 | grep -oP 'msg=docker' | wc -l)"
[[ "$RESULT" -gt "1" ]]
# Let's check that mbstring cannot extension cannot be disabled
# Disabled because no more used in setup_extensions.php
#set +e
#docker run --rm -e PHP_EXTENSION_MBSTRING=0 thecodingmachine/php:${PHP_VERSION}-${BRANCH}-${BRANCH_VARIANT} php -i
#[[ "$?" = "1" ]]
#set -e
# Let's check that the "xdebug.client_host" contains a value different from "no value"
docker run --rm -e PHP_EXTENSION_XDEBUG=1 "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-${BRANCH_VARIANT}" php -i | grep xdebug.client_host | grep -v "no value"
# Let's check that "xdebug.mode" is set to "debug" by default
docker run --rm -e PHP_EXTENSION_XDEBUG=1 "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-${BRANCH_VARIANT}" php -i | grep xdebug.mode | grep "debug"
# Let's check that "xdebug.mode" is properly overridden
docker run --rm -e PHP_EXTENSION_XDEBUG=1 -e PHP_INI_XDEBUG__MODE=debug,coverage "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-${BRANCH_VARIANT}" php -i | grep xdebug.mode | grep "debug,coverage"
if [[ "${PHP_VERSION}" != "8.1" ]]; then
# Tests that blackfire + xdebug will output an error
RESULT="$(docker run --rm -e PHP_EXTENSION_XDEBUG=1 -e PHP_EXTENSION_BLACKFIRE=1 "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-${BRANCH_VARIANT}" php -v 2>&1 | grep 'WARNING: Both Blackfire and Xdebug are enabled. This is not recommended as the PHP engine may not behave as expected. You should strongly consider disabling Xdebug or Blackfire.')"
[[ "$RESULT" == "WARNING: Both Blackfire and Xdebug are enabled. This is not recommended as the PHP engine may not behave as expected. You should strongly consider disabling Xdebug or Blackfire." ]]
# Check that blackfire can be enabled
docker run --rm -e PHP_EXTENSION_BLACKFIRE=1 "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-${BRANCH_VARIANT}" php -m | grep blackfire
fi
# Let's check that the extensions are enabled when composer is run
docker build -t test/composer_with_gd --build-arg PHP_VERSION="${PHP_VERSION}" --build-arg BRANCH="$BRANCH" --build-arg BRANCH_VARIANT="$BRANCH_VARIANT" tests/composer
# This should run ok (the sudo disables environment variables but call to composer proxy does not trigger PHP ini file regeneration)
docker run --rm test/composer_with_gd sudo composer update
docker rmi test/composer_with_gd
# Let's check that we can send mail directly
TESTMAIL_ID_DIRECT=$RANDOM
docker run --rm -e DMA_BLOCKING=1 "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-${BRANCH_VARIANT}" php -r "mail('[email protected]', 'Hello World ${TESTMAIL_ID_DIRECT}', 'This is an automatic test email, using a direct connection.');"
# Let's check that we can send mail through a smarthost with credentials
# Sending to an MCAST-TEST-NET IP address as that must not appear on the public internet according to RFC 5771, and will thus be unreachable for direct sending.
TESTMAIL_ID_SMARTHOST=$RANDOM
docker run --rm -e DMA_BLOCKING=1 -e DMA_CONF_SMARTHOST=smtp.ethereal.email -e DMA_CONF_PORT=587 -e DMA_CONF_SECURETRANSFER= -e DMA_CONF_STARTTLS= -e [email protected] -e DMA_AUTH_PASSWORD=pjxG3kc3VR31jQUvHz "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-${BRANCH_VARIANT}" php -r "mail('[email protected]', 'Hello World ${TESTMAIL_ID_SMARTHOST}', 'This is an automatic test email, using a smarthost.');"
# Let's check that the mails came through
ETHEREAL_COOKIEJAR=$(mktemp --tmpdir ethereal-cookies.XXXXXXXXXX)
ETHEREAL_CSRF=$(curl https://ethereal.email/login -s --fail -c "${ETHEREAL_COOKIEJAR}" | grep -Pom1 '\bname="_csrf" value="\K[^"]+')
curl https://ethereal.email/login -s --fail -X POST --data-urlencode "[email protected]" --data-urlencode "password=pjxG3kc3VR31jQUvHz" --data-urlencode "_csrf=${ETHEREAL_CSRF}" -b "${ETHEREAL_COOKIEJAR}" -c "${ETHEREAL_COOKIEJAR}" >/dev/null
ETHEREAL_INBOX=$(curl https://ethereal.email/messages -s --fail -b "${ETHEREAL_COOKIEJAR}")
rm "${ETHEREAL_COOKIEJAR}"
echo "${ETHEREAL_INBOX}" | grep -F ">Hello World ${TESTMAIL_ID_DIRECT}<"
echo "${ETHEREAL_INBOX}" | grep -F ">Hello World ${TESTMAIL_ID_SMARTHOST}<"
#################################
# Let's build the "node" images
#################################
docker build -t "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-${BRANCH_VARIANT}-node10" --build-arg PHP_VERSION="${PHP_VERSION}" --build-arg GLOBAL_VERSION="${BRANCH}" -f "Dockerfile.${VARIANT}.node10" .
docker build -t "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-${BRANCH_VARIANT}-node12" --build-arg PHP_VERSION="${PHP_VERSION}" --build-arg GLOBAL_VERSION="${BRANCH}" -f "Dockerfile.${VARIANT}.node12" .
docker build -t "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-${BRANCH_VARIANT}-node14" --build-arg PHP_VERSION="${PHP_VERSION}" --build-arg GLOBAL_VERSION="${BRANCH}" -f "Dockerfile.${VARIANT}.node14" .
docker build -t "thecodingmachine/php:${PHP_VERSION}-${BRANCH}-${BRANCH_VARIANT}-node16" --build-arg PHP_VERSION="${PHP_VERSION}" --build-arg GLOBAL_VERSION="${BRANCH}" -f "Dockerfile.${VARIANT}.node16" .
echo "Tests passed with success"