Skip to content

Commit 2eade4d

Browse files
authored
Merge pull request #403 from Scalingo/feat/364/testing
feat(tests): test default modules availability
2 parents dab569d + 8aaa01b commit 2eade4d

File tree

5 files changed

+549
-164
lines changed

5 files changed

+549
-164
lines changed
File renamed without changes.

test/helpers

+105-10
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,119 @@
22

33
### Helpers functions
44

5-
test::helpers::get_php_version() {
6-
local old_home
5+
test::helpers::test_deploy() {
6+
# Deploys a PHP app step by step and tests that everything is working as
7+
# expected.
8+
# To do so, this function runs the buildpack scripts and mimicks the
9+
# platform behavior wherever needed.
10+
# The different fixtures allow us to test different setup and conditions.
11+
#
12+
# Globals:
13+
# STD_OUT
14+
# default_modules
15+
#
16+
# Arguments:
17+
# $1 > $fixture_name: Name of the fixture to deploy.
18+
# The fixture sets the deployment conditions.
19+
# In fact, this is the name of a directory that must exist in
20+
# `BUILDPACK_DIR/test/fixtures/` and that contains the fixture code.
21+
# See `test::utils::setupFixture` for more details.
22+
#
23+
# $2 > $detect_expectation: Expected output of `bin/compile`.
24+
# The value of this variable is compared to the actual output of the
25+
# `bin/detect` script (and should match).
26+
#
27+
# $3 > $version: Expected version of PHP.
28+
# The value of this variable is compared to the version of PHP
29+
# that has actually been installed by the buildpack.
30+
# PHP version numbers follow the semver standard (i.e.
31+
# `maj.min.patch`).
32+
# Consequently, to test that PHP 8 is installed, one must set this
33+
# variable value to `8.` (notice the ending `.`) to make sure the test
34+
# fails if any other version starting with a `8` is installed (for
35+
# example, PHP `80.1.0`).
36+
# The logic is the same with minor version: to test that PHP 8.2 is
37+
# installed, set this variable to `8.2.` to make sure the test fails
38+
# if PHP 8.20.1 is installed, for example.
39+
# To test a full version, please add a trailing space, i.e. `8.1.15␣`,
40+
# so that `8.1.156` doesn't make the test pass.
741

8-
old_home="${HOME}"
42+
local fixture_name="${1}"
43+
local detect_expectation="${2}"
44+
local version="${3}"
945

10-
HOME="/app"
11-
export HOME
46+
# Setup the fixture:
47+
test::utils::setupFixture "${fixture_name}"
1248

13-
pushd "${HOME}" > /dev/null
49+
# Test that bin/detect works as expected:
50+
test::utils::detect
51+
test::utils::assertCapturedSuccess
52+
test::utils::assertCapturedEquals "${detect_expectation}"
1453

15-
cp --archive --recursive "${BUILD_DIR}"/* "${HOME}/"
54+
# Test that bin/compile works as expected:
55+
test::utils::compile
56+
# We can't use assertCapturedSuccess here:
57+
# With an empty composer.json file, composer will use stderr to warn us
58+
# that nothing had to be installed, causing the test to fail for no
59+
# reason -_-
60+
test::utils::assertSuccess
61+
62+
# Switch environment:
63+
test::helpers::enter_prod
64+
65+
# Test that PHP has the awaited version:
66+
test::helpers::get_php_version
67+
test::utils::assertCapturedStartswith "PHP ${version}"
68+
69+
# Test that all default PHP modules are available:
70+
test::helpers::list_php_modules
71+
for module in "${default_modules[@]}"; do
72+
test::utils::assertFileContains "${module}" "${STD_OUT}"
73+
done
74+
}
75+
76+
test::helpers::get_php_version() {
77+
# Captures the output of `php --version` so we can check that the version
78+
# installed is the expected one.
1679

1780
test::utils::capture ./vendor/php/bin/php --version
81+
}
82+
83+
test::helpers::list_php_modules() {
84+
# Captures the output of `php --modules` so we can check that all default
85+
# modules are indeed available.
86+
87+
test::utils::capture ./vendor/php/bin/php --modules
88+
}
1889

19-
popd > /dev/null
90+
test::helpers::enter_prod() {
91+
# Helper to switch to a production-like environment:
92+
# - $HOME is set to /app
93+
# - Working directory is set to $HOME
94+
# - The result of the `compile` script is copied to $HOME
95+
#
96+
# This environment is automatically reverted back, thanks to the `tearDown`
97+
# function override in `test/run`.
98+
# It would have been nice to put that in a `setUp` override. Unfortunately,
99+
# we can't because the switch of environment should only happen **during**
100+
# the test (after several steps have been reached), not before.
101+
#
102+
# /!\ This function is meant to be called after a successful call to
103+
# to `test::utils::compile` to further test the result of the
104+
# bin/compile call. It makes no sense to call it in another context.
105+
#
106+
# Globals:
107+
# HOME
108+
# PREV_HOME
109+
# BUILD_DIR
20110

21-
rm -Rf "${HOME}"/*
111+
PREV_HOME="${HOME}"
112+
export PREV_HOME
22113

23-
HOME="${old_home}"
114+
HOME="/app"
24115
export HOME
116+
117+
pushd "${HOME}" > /dev/null
118+
119+
cp --archive --recursive "${BUILD_DIR}"/* "${HOME}/"
25120
}

test/run

+19-4
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,35 @@ oneTimeTearDown() {
1616
}
1717

1818
setUp() {
19-
export OUTPUT_DIR="$( mktemp --directory "${SHUNIT_TMPDIR}/output.XXXX" )"
19+
OUTPUT_DIR="$( mktemp --directory "${SHUNIT_TMPDIR}/output.XXXX" )"
2020

2121
STD_OUT="${OUTPUT_DIR}/stdout"
2222
STD_ERR="${OUTPUT_DIR}/stderr"
2323

24-
export BUILD_DIR="${OUTPUT_DIR}/build"
25-
export CACHE_DIR="${OUTPUT_DIR}/cache"
26-
export HOME="${BUILD_DIR}"
24+
BUILD_DIR="${OUTPUT_DIR}/build"
25+
CACHE_DIR="${OUTPUT_DIR}/cache"
26+
HOME="${BUILD_DIR}"
27+
BUILDPACK_DIR="$( mktemp --directory "/tmp/test-XXXXXX" )"
2728

2829
mkdir -p "${OUTPUT_DIR}" "${BUILD_DIR}" "${CACHE_DIR}"
30+
export OUTPUT_DIR STD_OUT STD_ERR BUILD_DIR CACHE_DIR HOME BUILDPACK_DIR
31+
32+
# Copy the buildpack code into BUILDPACK_DIR:
33+
cp --archive "$( pwd )"/* "${BUILDPACK_DIR}/"
2934
}
3035

3136
tearDown() {
37+
# Empty OUTPUT_DIR and BUILDPACK_DIR
3238
rm -rf "${OUTPUT_DIR}"
39+
rm -rf "${BUILDPACK_DIR}"
40+
41+
unset BUILDPACK_DIR BUILD_DIR CACHE_DIR PHP_VERSION
42+
43+
# We may have changed working dir, let's switch back to the initial one:
44+
popd > /dev/null 2>&1
45+
46+
# Make sure /app is empty:
47+
rm -rf "/app"/*
3348
}
3449

3550
# Load shUnit2, which also run the tests:

test/tests

+93-64
Original file line numberDiff line numberDiff line change
@@ -3,95 +3,124 @@
33
source "$( pwd )/test/utils"
44
source "$( pwd )/test/helpers"
55

6-
test::detect_legacy_default() {
7-
# Test the buildpack detect script with a single simple .php file
8-
test::utils::detect "legacy_default"
9-
test::utils::assertCapturedSuccess
10-
test::utils::assertCapturedEquals "PHP (classic)"
6+
# List of modules that we want to ship with every PHP setup.
7+
# For now, users can't specify a version for these modules, we just ship
8+
# what we have. Hence no specific versions constraints here.
9+
readonly default_modules=(
10+
"apcu" "bcmath" "bz2" "Core" "ctype" "curl" "date" "dom" "exif" "fileinfo"
11+
"filter" "gd" "hash" "iconv" "intl" "json" "libxml" "mbstring" "mongodb"
12+
"mysqli" "mysqlnd" "openssl" "pcntl" "pcre" "PDO" "pdo_mysql" "pdo_pgsql"
13+
"pdo_sqlite" "pgsql" "Phar" "posix" "readline" "redis" "Reflection"
14+
"session" "shmop" "SimpleXML" "soap" "sockets" "SPL" "sqlite3" "standard"
15+
"tokenizer" "xml" "xmlreader" "xmlwriter" "xsl" "Zend OPcache" "zip" "zlib"
16+
)
17+
18+
19+
test::classic::defaults() {
20+
# Test a deployment of a classic app (not using Composer)
21+
# With default settings
22+
23+
test::helpers::test_deploy "classic_default" "PHP (classic)" "8.1."
1124
}
1225

13-
test::compile_legacy_default() {
14-
# Test the buildpack compile script with a single simple .php file
15-
test::utils::compile "legacy_default"
16-
test::utils::assertCapturedSuccess
26+
test::classic::php80() {
27+
# Test a deployment of a classic app (not using Composer)
28+
# Specifying we want PHP 8.0.x via environment
1729

18-
test::utils::resetCapture
30+
PHP_VERSION="8.0"
31+
export PHP_VERSION
1932

20-
test::helpers::get_php_version
21-
test::utils::assertCapturedStartswith "PHP 8.1."
33+
# PHP 8.0 is only available on stack `scalingo-20`:
34+
if [[ "${STACK}" != "scalingo-20" ]]; then
35+
echo "[skipping] PHP 8.0 is not available on scalingo-22"
36+
startSkipping
37+
fi
38+
39+
test::helpers::test_deploy "classic_default" "PHP (classic)" "8.0."
2240
}
2341

24-
test::detect_composer_default() {
25-
test::utils::detect "composer_default"
26-
test::utils::assertCapturedSuccess
27-
test::utils::assertCapturedEquals "PHP (composer.json)"
42+
test::classic::php81() {
43+
# Test a deployment of a classic app (not using Composer)
44+
# Specifying we want PHP 8.1.x via environment
45+
46+
PHP_VERSION="8.1"
47+
export PHP_VERSION
48+
49+
test::helpers::test_deploy "classic_default" "PHP (classic)" "8.1."
2850
}
2951

30-
test::compile_composer_default() {
31-
test::utils::compile "composer_default"
32-
# We can't use assertCapturedSuccess here:
33-
# Even with an empty composer.json file, composer will use stderr to warn
34-
# us that nothing had to be installed, causing the test to fail for no
35-
# reason -_-
36-
test::utils::assertSuccess
52+
test::classic::php82() {
53+
# Test a deployment of a classic app (not using Composer)
54+
# Specifying we want PHP 8.2.x via environment
3755

38-
test::utils::resetCapture
56+
PHP_VERSION="8.2"
57+
export PHP_VERSION
3958

40-
test::helpers::get_php_version
41-
test::utils::assertCapturedStartswith "PHP 8.1."
59+
test::helpers::test_deploy "classic_default" "PHP (classic)" "8.2."
4260
}
4361

44-
test::compile_composer_php80() {
45-
# PHP 8.0 is only available on stack `scalingo-20`:
46-
[[ "${STACK}" != "scalingo-20" ]] && startSkipping
62+
test::classic::php83() {
63+
# Test a deployment of a classic app (not using Composer)
64+
# Specifying we want PHP 8.3.x via environment
65+
66+
PHP_VERSION="8.3"
67+
export PHP_VERSION
4768

48-
test::utils::compile "composer_php80"
49-
test::utils::assertSuccess
69+
test::helpers::test_deploy "classic_default" "PHP (classic)" "8.3."
70+
}
5071

51-
test::utils::resetCapture
72+
test::composer::defaults() {
73+
# Test a deployment of a PHP app using Composer
74+
# With default settings
5275

53-
test::helpers::get_php_version
54-
test::utils::assertCapturedSuccess
55-
test::utils::assertCapturedStartswith "PHP 8.0."
76+
test::helpers::test_deploy "composer_default" "PHP (composer.json)" "8.1."
5677
}
5778

58-
test::compile_composer_php81() {
59-
test::utils::compile "composer_php81"
60-
test::utils::assertSuccess
61-
test::utils::resetCapture
79+
test::composer::php80() {
80+
# Test a deployment of a PHP app using Composer
81+
# Specifying we want PHP 8.0.x in composer.json
6282

63-
test::helpers::get_php_version
64-
test::utils::assertCapturedSuccess
65-
test::utils::assertCapturedStartswith "PHP 8.1."
83+
# PHP 8.0 is only available on stack `scalingo-20`:
84+
if [[ "${STACK}" != "scalingo-20" ]]; then
85+
echo "[skipping] PHP 8.0 is not available on scalingo-22"
86+
startSkipping
87+
fi
88+
89+
test::helpers::test_deploy "composer_php80" "PHP (composer.json)" "8.0."
90+
}
91+
92+
test::composer::php81() {
93+
# Test a deployment of a PHP app using Composer
94+
# Specifying we want PHP 8.1.x in composer.json
95+
96+
test::helpers::test_deploy "composer_php81" "PHP (composer.json)" "8.1."
6697
}
6798

68-
test::compile_composer_php82() {
69-
test::utils::compile "composer_php82"
70-
test::utils::assertSuccess
71-
test::utils::resetCapture
99+
test::composer::php82() {
100+
# Test a deployment of a PHP app using Composer
101+
# Specifying we want PHP 8.2.x in composer.json
72102

73-
test::helpers::get_php_version
74-
test::utils::assertCapturedSuccess
75-
test::utils::assertCapturedStartswith "PHP 8.2."
103+
test::helpers::test_deploy "composer_php82" "PHP (composer.json)" "8.2."
76104
}
77105

78-
test::compile_composer_php83() {
79-
test::utils::compile "composer_php83"
80-
test::utils::assertSuccess
81-
test::utils::resetCapture
106+
test::composer::php83() {
107+
# Test a deployment of a PHP app using Composer
108+
# Specifying we want PHP 8.3.x in composer.json
82109

83-
test::helpers::get_php_version
84-
test::utils::assertCapturedSuccess
85-
test::utils::assertCapturedStartswith "PHP 8.3."
110+
test::helpers::test_deploy "composer_php83" "PHP (composer.json)" "8.3."
86111
}
87112

113+
88114
# Add these functions to the test suite:
89115

90-
suite_addTest test::detect_legacy_default
91-
suite_addTest test::compile_legacy_default
92-
suite_addTest test::detect_composer_default
93-
suite_addTest test::compile_composer_default
94-
suite_addTest test::compile_composer_php80
95-
suite_addTest test::compile_composer_php81
96-
suite_addTest test::compile_composer_php82
97-
suite_addTest test::compile_composer_php83
116+
suite_addTest test::classic::defaults
117+
suite_addTest test::classic::php80
118+
suite_addTest test::classic::php81
119+
suite_addTest test::classic::php82
120+
suite_addTest test::classic::php83
121+
122+
suite_addTest test::composer::defaults
123+
suite_addTest test::composer::php80
124+
suite_addTest test::composer::php81
125+
suite_addTest test::composer::php82
126+
suite_addTest test::composer::php83

0 commit comments

Comments
 (0)