Skip to content
Draft
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
37 changes: 37 additions & 0 deletions ci/image-tests/Checks/BaseChecks.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
*/
trait BaseChecks
{
/** The environment of cron for a job, without MAILTO */
private const string CRONJOB_SHELL_ENV = 'env -i PATH=/usr/bin:/bin';

/** A command for docker-cronjob-shell, as cron runs it: it prints CRONJOB_SHELL_OUTPUT and ends with 3 */
private const string CRONJOB_SHELL_COMMAND = '/usr/local/bin/docker-cronjob-shell -c "echo image-test-cronjob-shell | tr a-z A-Z; exit 3"';

private const string CRONJOB_SHELL_OUTPUT = 'IMAGE-TEST-CRONJOB-SHELL';

/**
* @return array<string, array{string}>
*/
Expand Down Expand Up @@ -140,6 +148,35 @@ public function testBasePackageIsInstalled(string $package): void
$this->assertPackageInstalled($package);
}

/**
* With MAILTO, which a crontab file sets for its jobs, docker-cronjob-shell only runs the command: its output stays
* with cron, which mails it as before, see docs/customization.md#cron-jobs. The exit code stays that of the command.
*/
public function testDockerCronjobShellLeavesTheOutputToCronWithMailto(): void
{
$result = $this->execute(self::CRONJOB_SHELL_ENV . ' MAILTO=root ' . self::CRONJOB_SHELL_COMMAND);
self::assertSame([3, self::CRONJOB_SHELL_OUTPUT . "\n"], [$result->exitCode, $result->stdout], $result->describe());
}

/**
* Without MAILTO, docker-cronjob-shell sends the output of the command to syslog, which syslog-ng writes to the
* container log; in the images without syslog-ng, the output stays with cron. The exit code stays that of the
* command.
*/
public function testDockerCronjobShellSendsTheOutputToSyslog(): void
{
$result = $this->execute(self::CRONJOB_SHELL_ENV . ' ' . self::CRONJOB_SHELL_COMMAND);
if (!in_array('syslog', self::services(), true)) {
self::assertSame([3, self::CRONJOB_SHELL_OUTPUT . "\n"], [$result->exitCode, $result->stdout], $result->describe());
return;
}

self::assertSame([3, ''], [$result->exitCode, $result->stdout], $result->describe());
$this->eventually(function (): void {
self::assertMatchesRegularExpression('/ cron-job\[\d+\]: ' . self::CRONJOB_SHELL_OUTPUT . '$/m', self::container()->logs());
});
}

/**
* docker-cronjob adds a job in the system crontab format, with its user, see docs/customization.md#cron-jobs, and
* cron runs it as that user, also in Alpine, whose BusyBox crond reads no /etc/cron.d. In a container of its own,
Expand Down
31 changes: 29 additions & 2 deletions ci/image-tests/Checks/ContainerEnvironmentChecks.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
*/
trait ContainerEnvironmentChecks
{
private const string NO_CRON = 'The image does not start cron.';

/**
* TZ=Europe/Berlin: date shows the time zone, and supervisord writes local time to the container log.
*/
Expand All @@ -48,7 +50,7 @@ public function testTimeZoneFromTheEnvironment(): void
public function testCronRunsJobsInTheTimeZoneFromTheEnvironment(): void
{
if (!in_array('cron', self::services(), true)) {
self::markTestSkipped('The image does not start cron.');
self::markTestSkipped(self::NO_CRON);
}

$container = $this->environmentContainer();
Expand All @@ -67,7 +69,7 @@ public function testCronRunsJobsInTheTimeZoneFromTheEnvironment(): void
public function testCronLogsAtTheLevelFromTheEnvironment(): void
{
if (!in_array('cron', self::services(), true)) {
self::markTestSkipped('The image does not start cron.');
self::markTestSkipped(self::NO_CRON);
}

$container = $this->environmentContainer();
Expand Down Expand Up @@ -98,6 +100,31 @@ public function testSyslogMessagesReachTheFileFromTheEnvironment(): void
});
}

/**
* The output of a cron job that docker-cronjob added, from Hooks/, reaches the file of LOG_STDOUT in Debian and
* Ubuntu. Cron there would mail it, or discard it without a mail server: docker-cronjob-shell passes both lines to
* syslog-ng, which writes them to the file of LOG_STDOUT. Only in the Debian images that start cron;
* BusyBox crond on Alpine passes the output on itself.
*/
public function testCronJobOutputReachesTheFileFromTheEnvironment(): void
{
if (!in_array('cron', self::services(), true)) {
self::markTestSkipped(self::NO_CRON);
}

if (self::image()->isAlpine()) {
self::markTestSkipped('BusyBox crond passes the output of a job on itself.');
}

$container = $this->environmentContainer();
// the job writes the lines in upper case, so they differ from its command, which cron logs
$this->waitFor(function () use ($container): void {
$stdout = $this->stdout('cat ' . static::ENVIRONMENT['LOG_STDOUT'], $container);
self::assertMatchesRegularExpression('/ cron-job\[\d+\]: IMAGE-TEST-CRON-OUTPUT$/m', $stdout);
self::assertMatchesRegularExpression('/ cron-job\[\d+\]: IMAGE-TEST-CRON-ERROR$/m', $stdout);
}, 25, 3);
}

/**
* /docker.stdout and /docker.stderr, where the services of the image write their logs, point to the files of
* LOG_STDOUT and LOG_STDERR.
Expand Down
8 changes: 8 additions & 0 deletions ci/image-tests/Hooks/entrypoint.d/70-image-test-cron.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Image tests: with IMAGE_TEST_CRON set to hours, e.g. "9,10", adds a cron job for these hours before cron starts, which
# marks that it ran, see ContainerEnvironmentChecks. The tests set the current and the next hour in Berlin, worked out
# outside the image, so cron runs the job only if it uses the time zone of TZ.
# It also adds a job with docker-cronjob that writes a line to stdout and one to stderr every minute, in upper case, so
# that the lines differ from the command, which cron logs.
if [[ -n "${IMAGE_TEST_CRON:-}" ]]; then
if [[ -d /etc/crontabs ]]; then
# BusyBox crond on Alpine
Expand All @@ -9,4 +11,10 @@ if [[ -n "${IMAGE_TEST_CRON:-}" ]]; then
echo "* ${IMAGE_TEST_CRON} * * * root echo ran > /tmp/image-tests/cron-job" > /etc/cron.d/image-test
chmod 644 /etc/cron.d/image-test
fi

# where docker-cronjob fails, as on Alpine before it wrote to /etc/crontabs, the container starts all the same; the
# test of the output fails then
if id application > /dev/null 2>&1; then
docker-cronjob '* * * * * application echo image-test-cron-output | tr a-z A-Z; echo image-test-cron-error | tr a-z A-Z >&2' || true
fi
fi
6 changes: 6 additions & 0 deletions docker/php/8.2-alpine/conf/bin/usr-bin/docker-cronjob
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ fi
touch /etc/cron.d/webdevops-docker
chmod 0644 /etc/cron.d/webdevops-docker

# Cron in Debian mails the output of a job: the jobs of this file run with docker-cronjob-shell instead, which
# sends their output to syslog-ng and with it to the container log, unless a MAILTO line asks for the mail
if [[ ! -s /etc/cron.d/webdevops-docker ]]; then
echo 'SHELL=/usr/local/bin/docker-cronjob-shell' >> /etc/cron.d/webdevops-docker
fi

for CRONJOB_LINES in "$@"; do
echo "$CRONJOB_LINES" >> /etc/cron.d/webdevops-docker
done
Expand Down
34 changes: 34 additions & 0 deletions docker/php/8.2-alpine/conf/bin/usr-bin/docker-cronjob-shell
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash
# The shell of the cron jobs that docker-cronjob adds in Debian: docker-cronjob sets SHELL to this script in
# /etc/cron.d/webdevops-docker, so cron runs each job as "docker-cronjob-shell -c <command>". Cron in Debian
# mails the output of a job, and discards it where no mail server is installed. This script runs the command with
# /bin/sh, as cron would, and sends its output, stdout and stderr, to syslog, from where syslog-ng writes it to the
# container log. The exit code stays that of the command.
# With MAILTO set in the crontab file, cron mails the output as before: the script then only runs the command.

# the command runs with the shell that cron would use
export SHELL=/bin/sh

# MAILTO asks for the mail of cron; an empty MAILTO turns it off, so the output goes to syslog
if [[ -n "${MAILTO:-}" ]]; then
exec /bin/sh "$@"
fi

# Cron runs @reboot jobs as soon as it starts, at the same time as syslog-ng: wait up to 10 seconds for the socket of
# syslog-ng if supervisord starts it
if [[ ! -S /dev/log ]] && grep -q '^autostart *= *true' /opt/docker/etc/supervisor.d/syslog.conf 2> /dev/null; then
for _ in {1..20}; do
[[ -S /dev/log ]] && break
sleep 0.5
done
fi

# without syslog-ng, the output goes to cron as before
if [[ ! -S /dev/log ]]; then
exec /bin/sh "$@"
fi

# each line, and each KiB of a longer line, as a message of its own, tagged with the process id of the job; should
# logger end early, cat reads the rest, so the command does not end on a broken pipe
/bin/sh "$@" 2>&1 | { logger -t "cron-job[$$]" -p cron.info || cat > /dev/null; }
exit "${PIPESTATUS[0]}"
6 changes: 6 additions & 0 deletions docker/php/8.2/conf/bin/usr-bin/docker-cronjob
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ fi
touch /etc/cron.d/webdevops-docker
chmod 0644 /etc/cron.d/webdevops-docker

# Cron in Debian mails the output of a job: the jobs of this file run with docker-cronjob-shell instead, which
# sends their output to syslog-ng and with it to the container log, unless a MAILTO line asks for the mail
if [[ ! -s /etc/cron.d/webdevops-docker ]]; then
echo 'SHELL=/usr/local/bin/docker-cronjob-shell' >> /etc/cron.d/webdevops-docker
fi

for CRONJOB_LINES in "$@"; do
echo "$CRONJOB_LINES" >> /etc/cron.d/webdevops-docker
done
Expand Down
34 changes: 34 additions & 0 deletions docker/php/8.2/conf/bin/usr-bin/docker-cronjob-shell
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash
# The shell of the cron jobs that docker-cronjob adds in Debian: docker-cronjob sets SHELL to this script in
# /etc/cron.d/webdevops-docker, so cron runs each job as "docker-cronjob-shell -c <command>". Cron in Debian
# mails the output of a job, and discards it where no mail server is installed. This script runs the command with
# /bin/sh, as cron would, and sends its output, stdout and stderr, to syslog, from where syslog-ng writes it to the
# container log. The exit code stays that of the command.
# With MAILTO set in the crontab file, cron mails the output as before: the script then only runs the command.

# the command runs with the shell that cron would use
export SHELL=/bin/sh

# MAILTO asks for the mail of cron; an empty MAILTO turns it off, so the output goes to syslog
if [[ -n "${MAILTO:-}" ]]; then
exec /bin/sh "$@"
fi

# Cron runs @reboot jobs as soon as it starts, at the same time as syslog-ng: wait up to 10 seconds for the socket of
# syslog-ng if supervisord starts it
if [[ ! -S /dev/log ]] && grep -q '^autostart *= *true' /opt/docker/etc/supervisor.d/syslog.conf 2> /dev/null; then
for _ in {1..20}; do
[[ -S /dev/log ]] && break
sleep 0.5
done
fi

# without syslog-ng, the output goes to cron as before
if [[ ! -S /dev/log ]]; then
exec /bin/sh "$@"
fi

# each line, and each KiB of a longer line, as a message of its own, tagged with the process id of the job; should
# logger end early, cat reads the rest, so the command does not end on a broken pipe
/bin/sh "$@" 2>&1 | { logger -t "cron-job[$$]" -p cron.info || cat > /dev/null; }
exit "${PIPESTATUS[0]}"
6 changes: 6 additions & 0 deletions docker/php/8.3-alpine/conf/bin/usr-bin/docker-cronjob
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ fi
touch /etc/cron.d/webdevops-docker
chmod 0644 /etc/cron.d/webdevops-docker

# Cron in Debian mails the output of a job: the jobs of this file run with docker-cronjob-shell instead, which
# sends their output to syslog-ng and with it to the container log, unless a MAILTO line asks for the mail
if [[ ! -s /etc/cron.d/webdevops-docker ]]; then
echo 'SHELL=/usr/local/bin/docker-cronjob-shell' >> /etc/cron.d/webdevops-docker
fi

for CRONJOB_LINES in "$@"; do
echo "$CRONJOB_LINES" >> /etc/cron.d/webdevops-docker
done
Expand Down
34 changes: 34 additions & 0 deletions docker/php/8.3-alpine/conf/bin/usr-bin/docker-cronjob-shell
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash
# The shell of the cron jobs that docker-cronjob adds in Debian: docker-cronjob sets SHELL to this script in
# /etc/cron.d/webdevops-docker, so cron runs each job as "docker-cronjob-shell -c <command>". Cron in Debian
# mails the output of a job, and discards it where no mail server is installed. This script runs the command with
# /bin/sh, as cron would, and sends its output, stdout and stderr, to syslog, from where syslog-ng writes it to the
# container log. The exit code stays that of the command.
# With MAILTO set in the crontab file, cron mails the output as before: the script then only runs the command.

# the command runs with the shell that cron would use
export SHELL=/bin/sh

# MAILTO asks for the mail of cron; an empty MAILTO turns it off, so the output goes to syslog
if [[ -n "${MAILTO:-}" ]]; then
exec /bin/sh "$@"
fi

# Cron runs @reboot jobs as soon as it starts, at the same time as syslog-ng: wait up to 10 seconds for the socket of
# syslog-ng if supervisord starts it
if [[ ! -S /dev/log ]] && grep -q '^autostart *= *true' /opt/docker/etc/supervisor.d/syslog.conf 2> /dev/null; then
for _ in {1..20}; do
[[ -S /dev/log ]] && break
sleep 0.5
done
fi

# without syslog-ng, the output goes to cron as before
if [[ ! -S /dev/log ]]; then
exec /bin/sh "$@"
fi

# each line, and each KiB of a longer line, as a message of its own, tagged with the process id of the job; should
# logger end early, cat reads the rest, so the command does not end on a broken pipe
/bin/sh "$@" 2>&1 | { logger -t "cron-job[$$]" -p cron.info || cat > /dev/null; }
exit "${PIPESTATUS[0]}"
6 changes: 6 additions & 0 deletions docker/php/8.3/conf/bin/usr-bin/docker-cronjob
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ fi
touch /etc/cron.d/webdevops-docker
chmod 0644 /etc/cron.d/webdevops-docker

# Cron in Debian mails the output of a job: the jobs of this file run with docker-cronjob-shell instead, which
# sends their output to syslog-ng and with it to the container log, unless a MAILTO line asks for the mail
if [[ ! -s /etc/cron.d/webdevops-docker ]]; then
echo 'SHELL=/usr/local/bin/docker-cronjob-shell' >> /etc/cron.d/webdevops-docker
fi

for CRONJOB_LINES in "$@"; do
echo "$CRONJOB_LINES" >> /etc/cron.d/webdevops-docker
done
Expand Down
34 changes: 34 additions & 0 deletions docker/php/8.3/conf/bin/usr-bin/docker-cronjob-shell
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash
# The shell of the cron jobs that docker-cronjob adds in Debian: docker-cronjob sets SHELL to this script in
# /etc/cron.d/webdevops-docker, so cron runs each job as "docker-cronjob-shell -c <command>". Cron in Debian
# mails the output of a job, and discards it where no mail server is installed. This script runs the command with
# /bin/sh, as cron would, and sends its output, stdout and stderr, to syslog, from where syslog-ng writes it to the
# container log. The exit code stays that of the command.
# With MAILTO set in the crontab file, cron mails the output as before: the script then only runs the command.

# the command runs with the shell that cron would use
export SHELL=/bin/sh

# MAILTO asks for the mail of cron; an empty MAILTO turns it off, so the output goes to syslog
if [[ -n "${MAILTO:-}" ]]; then
exec /bin/sh "$@"
fi

# Cron runs @reboot jobs as soon as it starts, at the same time as syslog-ng: wait up to 10 seconds for the socket of
# syslog-ng if supervisord starts it
if [[ ! -S /dev/log ]] && grep -q '^autostart *= *true' /opt/docker/etc/supervisor.d/syslog.conf 2> /dev/null; then
for _ in {1..20}; do
[[ -S /dev/log ]] && break
sleep 0.5
done
fi

# without syslog-ng, the output goes to cron as before
if [[ ! -S /dev/log ]]; then
exec /bin/sh "$@"
fi

# each line, and each KiB of a longer line, as a message of its own, tagged with the process id of the job; should
# logger end early, cat reads the rest, so the command does not end on a broken pipe
/bin/sh "$@" 2>&1 | { logger -t "cron-job[$$]" -p cron.info || cat > /dev/null; }
exit "${PIPESTATUS[0]}"
6 changes: 6 additions & 0 deletions docker/php/8.4-alpine/conf/bin/usr-bin/docker-cronjob
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ fi
touch /etc/cron.d/webdevops-docker
chmod 0644 /etc/cron.d/webdevops-docker

# Cron in Debian mails the output of a job: the jobs of this file run with docker-cronjob-shell instead, which
# sends their output to syslog-ng and with it to the container log, unless a MAILTO line asks for the mail
if [[ ! -s /etc/cron.d/webdevops-docker ]]; then
echo 'SHELL=/usr/local/bin/docker-cronjob-shell' >> /etc/cron.d/webdevops-docker
fi

for CRONJOB_LINES in "$@"; do
echo "$CRONJOB_LINES" >> /etc/cron.d/webdevops-docker
done
Expand Down
34 changes: 34 additions & 0 deletions docker/php/8.4-alpine/conf/bin/usr-bin/docker-cronjob-shell
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash
# The shell of the cron jobs that docker-cronjob adds in Debian: docker-cronjob sets SHELL to this script in
# /etc/cron.d/webdevops-docker, so cron runs each job as "docker-cronjob-shell -c <command>". Cron in Debian
# mails the output of a job, and discards it where no mail server is installed. This script runs the command with
# /bin/sh, as cron would, and sends its output, stdout and stderr, to syslog, from where syslog-ng writes it to the
# container log. The exit code stays that of the command.
# With MAILTO set in the crontab file, cron mails the output as before: the script then only runs the command.

# the command runs with the shell that cron would use
export SHELL=/bin/sh

# MAILTO asks for the mail of cron; an empty MAILTO turns it off, so the output goes to syslog
if [[ -n "${MAILTO:-}" ]]; then
exec /bin/sh "$@"
fi

# Cron runs @reboot jobs as soon as it starts, at the same time as syslog-ng: wait up to 10 seconds for the socket of
# syslog-ng if supervisord starts it
if [[ ! -S /dev/log ]] && grep -q '^autostart *= *true' /opt/docker/etc/supervisor.d/syslog.conf 2> /dev/null; then
for _ in {1..20}; do
[[ -S /dev/log ]] && break
sleep 0.5
done
fi

# without syslog-ng, the output goes to cron as before
if [[ ! -S /dev/log ]]; then
exec /bin/sh "$@"
fi

# each line, and each KiB of a longer line, as a message of its own, tagged with the process id of the job; should
# logger end early, cat reads the rest, so the command does not end on a broken pipe
/bin/sh "$@" 2>&1 | { logger -t "cron-job[$$]" -p cron.info || cat > /dev/null; }
exit "${PIPESTATUS[0]}"
6 changes: 6 additions & 0 deletions docker/php/8.4/conf/bin/usr-bin/docker-cronjob
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ fi
touch /etc/cron.d/webdevops-docker
chmod 0644 /etc/cron.d/webdevops-docker

# Cron in Debian mails the output of a job: the jobs of this file run with docker-cronjob-shell instead, which
# sends their output to syslog-ng and with it to the container log, unless a MAILTO line asks for the mail
if [[ ! -s /etc/cron.d/webdevops-docker ]]; then
echo 'SHELL=/usr/local/bin/docker-cronjob-shell' >> /etc/cron.d/webdevops-docker
fi

for CRONJOB_LINES in "$@"; do
echo "$CRONJOB_LINES" >> /etc/cron.d/webdevops-docker
done
Expand Down
Loading