Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 0 additions & 165 deletions .github/workflows/build.yaml

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
| `conf/dockerhub.yml` | Texts of the Docker Hub pages: per repository the short description, the introduction, the example and the links, or for an image that is no longer built the month of its last build and its replacement (source) |
| `docs/dockerhub/<repository>.md` | Docker Hub pages, generated by `ci/console generate:dockerhub`, never edited by hand; the job `dockerhub-description` in `.github/workflows/build.yaml` uploads them, see [Building in CI](CONTRIBUTING.md#building-in-ci) |
| `ci/image-tests/` | Image tests (PHPUnit): a test class per image in `Images/`, the checks as traits in `Checks/`; `ci/console test:image` runs them against a container of the image |
| `tests/` | container-structure-test definitions run by the CI |
| `docs/` | User documentation |

`webdevops/php-official` is a deprecated name of `webdevops/php`: the CI publishes the image of `docker/php/<tag>` under that name too, but only for the tags listed under `docker: aliases` in `conf/console.yml`. Do not add new tags there. The tags that are built are the `-t "webdevops/..."` lines in `.github/workflows/build.yaml`.
Expand All @@ -33,7 +32,7 @@ Only these commands are known to work:
- In an image test, a file that a service writes as `application` and a command of the test writes as root (`docker exec`) belongs in `/app`, not in `/tmp`. The runners of GitHub Actions set `fs.protected_regular`, under which even root may not open a file of another user in a sticky directory with `O_CREAT`; a Docker host without it, as many local ones, lets such a test pass that then fails in the CI.
- `make generate`, `make check` and `make test` run as your user, so the files they write belong to you. Do not write into the checkout as root.

Besides `generate`, `check` and `test`, the Makefile has `structure-test`. `make structure-test` runs container-structure-test against your local images of `bootstrap`, `base`, `base-app`, `php`, `php-dev` and `php-nginx`, the images that have a suite in `tests/structure-test`. It tests only the tags in `.github/workflows/build.yaml` and fails if a test fails, if `docker` fails or if no such image exists locally; run it inside `webdevops/dockerfile-build-env` with the Docker socket mounted. The CI runs the image tests for every image except `toolbox`, and container-structure-test for the six images above; to check a change by hand, start the image and try it as described below.
The CI runs the image tests for every image except `toolbox`; they are the only tests of the images. To check a change by hand, start the image and try it as described below.

### Packages come with tests

Expand Down
13 changes: 3 additions & 10 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ It does not update the actions in `.github/workflows/build.yaml`, because that f

A new push to a pull request cancels the run of its previous commit. Runs on `master`, the nightly run and runs started by hand are never cancelled.

GitHub Actions builds every image independently and runs the image tests and structure tests
GitHub Actions builds every image independently and runs the image tests
defined for it before pushing it to Docker Hub. The CI also uses `ghcr.io/webdevops/`, but only
internally; pull the images from Docker Hub.

Expand Down Expand Up @@ -90,7 +90,7 @@ Build all PHP images of one version (default `8.5`), in the order they depend on

Then try an image with the [quick start](README.md#quick-start).

The CI runs the image tests for every image except `toolbox`, and the structure tests for `bootstrap`, `base`, `base-app`, `php`, `php-dev` and `php-nginx`.
The CI runs the image tests for every image except `toolbox`.
To run the image tests against an image you built locally:

```
Expand All @@ -100,18 +100,11 @@ make test-image IMAGE=webdevops/php-nginx:8.5
`REFERENCE` tests another image with the tests and settings of `IMAGE`, e.g. `REFERENCE=my-php-nginx`.
The tests use `/var/run/docker.sock`, in rootless Docker the socket of the current Docker context; set `DOCKER_SOCKET` for another one.

To run the structure tests against the images you built locally:

```
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v $PWD:/app -w /app webdevops/dockerfile-build-env make structure-test
```

It tests the local tags that the CI builds and fails if no such image exists.

## Image tests

The image tests are PHPUnit tests in `ci/image-tests`. They start a container of the image with its own command, so supervisord starts the services as in production, and check it:
files and their owner and mode, commands and their output, PHP extensions and settings, the running services and their ports, and requests through Nginx or Apache to PHP-FPM over http and https.
What the image sets for every container, its environment variables with their defaults, entrypoint, command and exposed ports, they read with `docker image inspect`.
Most tests of an image run against one container. Its root directory gets the files of `ci/image-tests/Fixtures` first, so `/app` holds the pages that the requests ask for.
The tests of environment variables, commands and hooks start more containers of the image: the environment container, which starts with every variable of `ENVIRONMENT` in `ImageTestCase` set to a value other than its default, containers of their own for variables that would get in the way of other tests, e.g. the process manager of PHP-FPM, which `startTestContainer()` removes after their test, and short runs of one command through the entrypoint, e.g. `root id -un`.
They check what a variable does, not only that its value ends up in a configuration file: a request that hits the memory limit, a cron job that runs at the hour of `TZ`, a connection of Xdebug to a listener.
Expand Down
5 changes: 1 addition & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
MAKEFLAGS += --silent
.PHONY: generate check test test-image test-image-runner test-image-run structure-test
.PHONY: generate check test test-image test-image-runner test-image-run

# The containers run as the user who calls make, so that the files they write belong to them. In rootless Docker,
# root in the container already is that user. sh replaces the entrypoint of webdevops/php, which would switch to root.
Expand Down Expand Up @@ -42,6 +42,3 @@ test-image-runner:

test-image-run:
docker run --rm --volume "$(DOCKER_SOCKET)":/var/run/docker.sock webdevops-image-tests $(IMAGE) $(if $(REFERENCE),--reference $(REFERENCE))

structure-test:
cd tests/structure-test && ./run.sh
62 changes: 62 additions & 0 deletions ci/image-tests/Checks/BaseAppChecks.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

namespace Webdevops\Build\ImageTests\Checks;

use PHPUnit\Framework\Attributes\DataProvider;
use Webdevops\Build\ImageTests\Docker\ImageConfiguration;
use Webdevops\Build\ImageTests\ImageTestCase;

use function array_keys;
use function escapeshellarg;
use function explode;
use function trim;

Expand Down Expand Up @@ -78,4 +82,62 @@ public function testMoreutils(): void

self::assertSame("/usr/bin/sponge\n", $this->stdout('command -v sponge'));
}

public function testApplicationVariablesOfTheImage(): void
{
[$name, $id] = ['application', '1000'];
$expected = [
'APPLICATION_USER' => $name,
'APPLICATION_GROUP' => $name,
'APPLICATION_PATH' => '/app',
'APPLICATION_UID' => $id,
'APPLICATION_GID' => $id,
];
self::assertSame($expected, ImageConfiguration::inspect(self::image()->reference)->variables(array_keys($expected)));
}

/**
* The user application has the UID 1000, its primary group application the GID 1000.
*/
public function testIdOfTheApplicationUser(): void
{
self::assertMatchesRegularExpression(
'/^uid=1000\(application\) gid=1000\(application\) groups=1000\(application\)/',
$this->stdout('id application'),
);
}

/**
* /app belongs to UID and GID 1000, with the mode 0755.
*/
public function testApplicationPathPermissions(): void
{
$path = '/app';
self::assertSame('drwxr-xr-x', $this->file($path)->permissions());
self::assertSame("1000:1000\n", $this->stdout('stat -c %u:%g ' . $path));
}

/**
* @return array<string, array{string}>
*/
public static function applicationTools(): array
{
return self::cases(['zip', 'unzip', 'bzip2', 'nslookup', 'ssh', 'rsync', 'git', 'patch']);
}

#[DataProvider('applicationTools')]
public function testApplicationToolIsOnThePath(string $tool): void
{
self::assertSame('/usr/bin/' . $tool . "\n", $this->stdout('command -v ' . escapeshellarg($tool)));
}

/**
* docker-run-bootstrap ran the scripts in /opt/docker/provision/bootstrap.d at the build and deleted them, e.g.
* 10-user-application.sh and 20-app.sh, which create the user application and /app, and in the PHP images
* 10-php-init.sh and 11-php-conf.sh.
*/
public function testBuildScriptsWereRunAndDeleted(): void
{
self::assertSame('', $this->stdout("find /opt/docker/provision/bootstrap.d -name '*.sh'"));
}
}
67 changes: 67 additions & 0 deletions ci/image-tests/Checks/BaseChecks.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use function escapeshellarg;
use function in_array;
use function trim;

/**
* Checks of the images based on webdevops/base: the layout under /opt/docker, supervisor and the tools.
Expand Down Expand Up @@ -159,4 +160,70 @@ public function testDockerCronjobAddsAJobThatCronRunsAsItsUser(): void
self::assertSame("nobody\n", $this->execute('cat /tmp/image-test-cronjob 2>&1', $container)->stdout);
}, 30, 3);
}

/**
* The commands of webdevops/base, where the distribution has them: netstat and sed are in /bin in Alpine.
*
* @return iterable<string, array{string, string}>
*/
public static function baseCommands(): iterable
{
$commands = [
'supervisord' => '#^/usr/bin/supervisord$#',
'wget' => '#^/usr/bin/wget$#',
'curl' => '#^/usr/bin/curl$#',
'zic' => '#^/usr/sbin/zic$#',
'netstat' => '#^/(usr/)?bin/netstat$#',
'sed' => '#^/(usr/)?bin/sed$#',
];
foreach ($commands as $command => $path) {
yield $command => [$command, $path];
}
}

#[DataProvider('baseCommands')]
public function testBaseCommandIsOnThePath(string $command, string $path): void
{
self::assertMatchesRegularExpression($path, trim($this->stdout('command -v ' . escapeshellarg($command))));
}

/**
* The scripts and the configuration of webdevops/base have exactly the modes 0755 and 0644: no setuid, setgid or
* sticky bit.
*
* @return iterable<string, array{string, string}>
*/
public static function basePermissions(): iterable
{
$scripts = [
'/opt/docker/bin/bootstrap.sh',
'/opt/docker/bin/config.sh',
'/opt/docker/bin/control.sh',
'/opt/docker/bin/entrypoint.sh',
'/opt/docker/bin/service.d/supervisor.sh',
];
foreach ($scripts as $path) {
yield $path => [$path, '-rwxr-xr-x'];
}

$configuration = [
'/opt/docker/etc/supervisor.conf',
'/opt/docker/etc/supervisor.d/cron.conf',
'/opt/docker/etc/supervisor.d/dnsmasq.conf',
'/opt/docker/etc/supervisor.d/postfix.conf',
'/opt/docker/etc/supervisor.d/ssh.conf',
'/opt/docker/etc/supervisor.d/syslog.conf',
'/opt/docker/etc/logrotate.d/syslog-ng',
'/opt/docker/etc/syslog-ng/syslog-ng.conf',
];
foreach ($configuration as $path) {
yield $path => [$path, '-rw-r--r--'];
}
}

#[DataProvider('basePermissions')]
public function testBasePermissions(string $path, string $permissions): void
{
self::assertSame($permissions, $this->file($path)->permissions(), $path);
}
}
62 changes: 62 additions & 0 deletions ci/image-tests/Checks/BootstrapChecks.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace Webdevops\Build\ImageTests\Checks;

use PHPUnit\Framework\Attributes\DataProvider;
use Webdevops\Build\ImageTests\Docker\ImageConfiguration;
use Webdevops\Build\ImageTests\ImageTestCase;

use function array_keys;
use function dirname;
use function implode;
use function preg_quote;
Expand Down Expand Up @@ -142,4 +144,64 @@ public function testApkInstallsFromTheCache(): void
self::assertStringNotContainsString('ERROR', $result->stdout);
self::assertStringNotContainsString('unsatisfiable constraints', $result->stdout);
}

/**
* The variables of the locale and the terminal that webdevops/bootstrap and the PHP images set, and the PATH of the
* distribution and of the official PHP image.
*/
public function testLocaleTerminalAndPathOfTheImage(): void
{
$expected = [
'LC_ALL' => 'C.UTF-8',
'LANG' => 'C.UTF-8',
'TERM' => 'xterm',
'PATH' => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
];
self::assertSame($expected, ImageConfiguration::inspect(self::image()->reference)->variables(array_keys($expected)));
}

/**
* The commands from provisioning/baselayout, which every image copies from webdevops/toolbox.
*
* @return array<string, array{string}>
*/
public static function bootstrapCommands(): array
{
return self::cases([
'/usr/local/bin/apk-install',
'/usr/local/bin/apk-upgrade',
'/usr/local/bin/apt-add-repository',
'/usr/local/bin/apt-install',
'/usr/local/bin/apt-update',
'/usr/local/bin/apt-upgrade',
'/usr/local/bin/docker-image-cleanup',
'/usr/local/bin/docker-image-info',
'/usr/local/bin/generate-locales',
'/usr/local/bin/generate-dockerimage-info',
'/usr/local/bin/docker-run-bootstrap',
'/usr/local/bin/go-replace',
]);
}

/**
* Programs of root with exactly the mode 0755: no setuid, setgid or sticky bit.
*/
#[DataProvider('bootstrapCommands')]
public function testBootstrapCommandPermissions(string $path): void
{
$this->assertRootScript($path);
self::assertSame('-rwxr-xr-x', $this->file($path)->permissions(), $path);
}

/**
* gosu, with which the entrypoint runs commands as another user, is a program of root with the mode 0755 in the
* running container. webdevops/base and the PHP images build it with the setuid and setgid bits, so that their
* entrypoint can switch to root when the container starts as another user, and the entrypoint removes them.
*/
public function testGosuPermissions(): void
{
$gosu = $this->file('/sbin/gosu');
$this->assertOwnedByRoot($gosu);
self::assertSame('-rwxr-xr-x', $gosu->permissions());
}
}
1 change: 0 additions & 1 deletion ci/image-tests/Checks/BuildEnvChecks.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public static function buildTools(): iterable
{
yield 'make' => ['make --version', '/\AGNU Make \d+\.\d+/'];
yield 'docker' => ['docker --version', '/\ADocker version \d+\.\d+\.\d+/'];
yield 'container-structure-test' => ['container-structure-test version', '/\Av\d+\.\d+\.\d+$/m'];
yield 'git' => ['git --version', '/\Agit version \d+\.\d+/'];
}

Expand Down
15 changes: 15 additions & 0 deletions ci/image-tests/Checks/EntrypointChecks.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
namespace Webdevops\Build\ImageTests\Checks;

use Webdevops\Build\ImageTests\Docker\Container;
use Webdevops\Build\ImageTests\Docker\ImageConfiguration;
use Webdevops\Build\ImageTests\ImageTestCase;

use function array_filter;
use function array_keys;
use function array_values;
use function str_contains;
use function trim;
Expand Down Expand Up @@ -71,4 +73,17 @@ private function commandLineOfProcess1(Container $container): string
{
return trim($this->stdout('tr "\\000" " " < /proc/1/cmdline', $container));
}

/**
* The image runs supervisord through the entrypoint, and sets the variables that the entrypoint reads:
* DOCKER_CONF_HOME, and LOG_STDOUT and LOG_STDERR, empty by default.
*/
public function testEntrypointCommandAndLogVariablesOfTheImage(): void
{
$configuration = ImageConfiguration::inspect(self::image()->reference);
self::assertSame(['/entrypoint'], $configuration->entrypoint);
self::assertSame(['supervisord'], $configuration->command);
$expected = ['DOCKER_CONF_HOME' => '/opt/docker/', 'LOG_STDOUT' => '', 'LOG_STDERR' => ''];
self::assertSame($expected, $configuration->variables(array_keys($expected)));
}
}
Loading
Loading