App-Actions
view release on metacpan or search on metacpan
local $/;
my $source = <$handle>;
close $handle;
$source = '' unless defined $source;
# A checkout on Windows can carry CRLF line endings, and the module is
# found by the line that holds the trailing "1;". The copy is a scratch
# file, so it is normalised to LF and written back byte for byte; the
# module it is made from is not touched.
$source =~ s/\r\n/\n/g;
my $injection = <<"CODE";
{
no warnings 'redefine';
my \$original = \\&${package}::json_schema;
*${package}::json_schema = sub {
my \$schema = \$original->(\@_);
push \@\{ \$schema->{required} }, 'drifted_key';
return \$schema;
};
}
CODE
$source =~ s/\n1;\n/\n$injection\n1;\n/
or die "cannot inject a drifted schema into '$path'";
open my $out, '>', $path or die "Unable to write '$path': $!";
binmode $out;
print {$out} $source;
close $out;
return;
}
# The validator is the one the commands themselves use, so a change to it is
# exercised here in the same form as at run time.
sub load_json {
my ($path) = @_;
open my $handle, '<', $path or die "Unable to read '$path': $!";
local $/;
my $text = <$handle>;
close $handle;
return JSON::PP->new->decode($text);
}
sub write_file {
my ($path, $text) = @_;
open my $handle, '>', $path or die "Unable to write '$path': $!";
print {$handle} $text;
close $handle;
return;
}
sub run_cli {
my (@args) = @_;
my ($stdout, $stderr, $status) = capture {
system($^X, '-I' . $lib, $script, @args);
};
return ($stdout, $stderr, $status >> 8);
}
# The schemas come from the modules, which is where the command prints them
# from, so these tests hold the contract a CI job fetches rather than a copy
# of it that could drift.
my $check_schema = App::Actions::Check->json_schema;
my $preflight_schema = App::Actions::Runner->json_schema;
my $cache_schema = App::Actions::Cache->json_schema;
my $logs_schema = App::Actions::Logs->json_schema;
my %shipped_schema = (
check => $check_schema,
preflight => $preflight_schema,
cache => $cache_schema,
logs => $logs_schema,
);
# The version of a shape is written in three places: the schema's own version,
# the const its schema_version property promises, and the constant the module
# reports. A bump that reaches one or two of them changes what the contract
# says without changing the number a caller compares against, and a document
# that then fails would name a key rather than the version that moved. So the
# schemas are held to themselves here, before any document is read.
my %schema_source = (
check => [ 'App::Actions::Check', 'json_schema_version' ],
preflight => [ 'App::Actions::Runner', 'preflight_schema_version' ],
cache => [ 'App::Actions::Cache', 'json_schema_version' ],
logs => [ 'App::Actions::Logs', 'json_schema_version' ],
);
for my $document (sort keys %shipped_schema) {
my ($module, $reported_by) = @{ $schema_source{$document} };
my $schema = $shipped_schema{$document};
my $version = $schema->{version};
my $const = $schema->{properties}{schema_version}{const};
my $reported = $module->$reported_by;
my $show = sub {
my ($value) = @_;
return defined $value ? "$value" : 'nothing';
};
my @problems;
push @problems, sprintf(
'the %s schema says version %s, but its schema_version const says %s',
$document, $show->($version), $show->($const),
) unless defined $const && defined $version && "$const" eq "$version";
push @problems, sprintf(
'the %s schema says version %s, but %s->%s returns %s',
$document, $show->($version), $module, $reported_by, $show->($reported),
) unless defined $version && defined $reported && "$version" eq "$reported";
is_deeply(
\@problems,
[],
"the $document schema version, its const key and the module that prints it agree",
);
diag "$_\n" for @problems;
}
my $check_example = load_json(File::Spec->catfile($fixtures, 'check.json'));
my $preflight_example = load_json(File::Spec->catfile($fixtures, 'preflight.json'));
my $cache_example = load_json(File::Spec->catfile($fixtures, 'cache.json'));
my $logs_example = load_json(File::Spec->catfile($fixtures, 'logs.json'));
is_deeply(
[ validate($check_example, $check_schema) ],
[],
'the checked-in check example matches its schema',
);
is_deeply(
[ validate($preflight_example, $preflight_schema) ],
[],
'the checked-in preflight example matches its schema',
);
is_deeply(
[ validate($cache_example, $cache_schema) ],
[],
'the checked-in cache example matches its schema',
);
is_deeply(
[ validate($logs_example, $logs_schema) ],
[],
'the checked-in logs example matches its schema',
);
# CI should not have to read this file to find the contract, so the command
# prints it: the same document the tests above hold the examples to.
for my $document (qw(check preflight cache logs)) {
my ($out, $err, $exit) = run_cli('schema', $document);
is($exit, 0, "schema $document exits 0");
is($err, '', "schema $document writes nothing to standard error");
my $printed = eval { JSON::PP->new->decode($out) };
ok($printed, "schema $document prints one JSON object") or diag "stderr: $err";
my $module = $shipped_schema{$document};
is(
JSON::PP->new->canonical->encode($printed),
JSON::PP->new->canonical->encode($module),
"schema $document prints the schema the module produces",
);
}
my (undef, $no_name_err, $no_name_exit) = run_cli('schema');
is($no_name_exit, 2, 'schema without a document is a usage error');
like($no_name_err, qr/Usage: action-run \[options\] schema <check\|preflight\|cache\|logs>/,
'the usage error says which documents can be named');
my (undef, $unknown_err, $unknown_exit) = run_cli('schema', 'workflow');
is($unknown_exit, 2, 'an unknown document is a usage error');
like($unknown_err, qr/Unknown document 'workflow'; expected check, preflight, cache or logs/,
'the error names the documents that exist');
unlike($unknown_err, qr/ at \S+ line \d+/, 'the error reads as a sentence');
my (undef, $json_err, $json_exit) = run_cli('--json', 'schema', 'check');
is($json_exit, 2, '--json with the schema command is a usage error');
like($json_err, qr/only used by the check, preflight, cache and logs commands/,
'the schema command needs no --json');
is($check_example->{schema_version}, $check_schema->{version},
'the check example carries the version it was written for');
is($preflight_example->{schema_version}, $preflight_schema->{version},
'the preflight example carries the version it was written for');
is($cache_example->{schema_version}, $cache_schema->{version},
'the cache example carries the version it was written for');
is($logs_example->{schema_version}, $logs_schema->{version},
'the logs example carries the version it was written for');
# A validator that accepted everything would pass the tests above, so it is
# shown to reject the ways the document can drift.
{
my $extra = { %$check_example, surprise => 1 };
ok(scalar validate($extra, $check_schema),
'a key the schema does not name is rejected');
}
{
my $missing = { %$check_example };
delete $missing->{issues};
ok(scalar validate($missing, $check_schema),
'a key the schema requires is not allowed to go missing');
}
{
my $wrong = { %$check_example, problems => 'none' };
my @errors = validate($wrong, $check_schema);
is(scalar @errors, 1, 'a value of the wrong type is rejected');
is($errors[0], "\$.problems: expected integer, got 'none'",
'the message shows the value that is wrong, not a blank');
}
{
my $newer = { %$check_example, schema_version => $check_schema->{version} + 1 };
my @errors = validate($newer, $check_schema);
is(scalar @errors, 1, 'another schema version is rejected');
is($errors[0], '$' . ".schema_version: expected $check_schema->{version}, got "
'the misshapen fixture is not grouped as an example',
);
is(scalar @$refused, 2, 'the misshapen fixture is rejected beside the malformed one');
like(
join("\n", @$refused),
qr/^\Q$misshapen\E does not match the check schema: \$\.\w+: (?:missing|unexpected key) \(and 1 more\)$/m,
'the rejection names the misshapen fixture and what is wrong with it',
);
unlink File::Spec->catfile($copied, $misshapen);
# A stale version is still reported as a version, not as a shape problem
# with schema_version, and the file is still grouped as before.
my $stale = 'zz-temporary-stale-check.json';
write_file(
File::Spec->catfile($copied, $stale),
JSON::PP->new->canonical->pretty->encode(
{ %$check_example, schema_version => $check_schema->{version} - 1 },
),
);
my (undef, $kept, $stale_problems) = discover_examples($copied);
my @stale = grep { /\A\Q$stale\E / } @$stale_problems;
is(scalar @stale, 1, 'a fixture with an old version gets one message');
like($stale[0], qr/^\Q$stale\E declares schema version \d+, but App::Actions::Check ships \d+$/,
'and it is about the version');
ok((grep { $_ eq $stale } @{ $kept->{check} }), 'and the fixture is still grouped');
}
# Now the documents the commands actually print, against the same schemas.
my $project = tempdir(CLEANUP => 1);
make_path(File::Spec->catdir($project, '.github', 'workflows'));
my $workflow = File::Spec->catfile($project, '.github', 'workflows', 'test.yml');
write_file($workflow, <<'YAML');
name: Schema
on: push
jobs:
test:
runs-on: ubuntu-latest
container:
image: perl:5.38
services:
db:
image: postgres:15
steps:
- id: pick
run: |
FAKE_OUTPUT: image=alpine:3.20
- uses: docker://${{ steps.pick.outputs.image }}
YAML
local $ENV{FAKE_DOCKER_STATE} = tempdir(CLEANUP => 1);
my ($check_out, $check_err, $check_exit) = run_cli(
'--docker', $fake, 'check', '--json', $project,
);
is($check_exit, 0, 'check --json exits 0 against the fake daemon');
my $check_doc = eval { JSON::PP->new->decode($check_out) };
ok($check_doc, 'the check document decodes') or diag "stderr: $check_err";
is_deeply([ validate($check_doc, $check_schema) ], [],
'the check document the command prints matches the schema');
is($check_doc->{schema_version}, App::Actions::Check->json_schema_version,
'the printed check document carries the module version');
is_deeply([ sort keys %$check_doc ], [ sort keys %$check_example ],
'the printed check document has the keys the example shows');
my ($preflight_out, $preflight_err, $preflight_exit) = run_cli(
'--docker', $fake, 'preflight', '--json', $workflow, $project,
);
is($preflight_exit, 0, 'preflight --json exits 0 against the fake daemon');
my $preflight_doc = eval { JSON::PP->new->decode($preflight_out) };
ok($preflight_doc, 'the preflight document decodes') or diag "stderr: $preflight_err";
is_deeply([ validate($preflight_doc, $preflight_schema) ], [],
'the preflight document the command prints matches the schema');
is($preflight_doc->{schema_version}, App::Actions::Runner->preflight_schema_version,
'the printed preflight document carries the module version');
is_deeply([ sort keys %$preflight_doc ], [ sort keys %$preflight_example ],
'the printed preflight document has the keys the example shows');
{
# A cache document needs a record and a daemon that knows the image; the
# fake daemon reports a size for a marker, which is what makes the size
# fields more than null here.
my $record = File::Spec->catfile(tempdir(CLEANUP => 1), 'cache.json');
local $ENV{ACTION_RUN_CACHE_FILE} = $record;
App::Actions::Cache->new(file => $record)->remember('alpine:3.20');
my $marker = File::Spec->catfile($ENV{FAKE_DOCKER_STATE}, 'image-alpine_3.20');
open my $marker_handle, '>', $marker
or die "Unable to write '$marker': $!";
print {$marker_handle} 'alpine:3.20';
close $marker_handle;
my ($cache_out, $cache_err, $cache_exit) = run_cli(
'cache', '--json', '--docker', $fake,
);
is($cache_exit, 0, 'cache --json exits 0 against the fake daemon');
my $cache_doc = eval { JSON::PP->new->decode($cache_out) };
ok($cache_doc, 'the cache document decodes') or diag "stderr: $cache_err";
is_deeply([ validate($cache_doc, $cache_schema) ], [],
'the cache document the command prints matches the schema');
is($cache_doc->{schema_version}, App::Actions::Cache->json_schema_version,
'the printed cache document carries the module version');
cmp_ok($cache_doc->{generated_at}, '>=', $cache_doc->{images}[0]{last_used},
'the printed cache document carries the clock its image ages use');
is_deeply([ sort keys %$cache_doc ], [ sort keys %$cache_example ],
'the printed cache document has the keys the example shows');
is($cache_doc->{cache_file}, $record, 'the document names the record it read');
is($cache_doc->{images}[0]{image}, 'alpine:3.20', 'the recorded image is listed');
is($cache_doc->{images}[0]{size}, 123_456_789,
'the size the daemon reports is in the document');
ok($cache_doc->{images}[0]{present}, 'a present image is marked present');
is($cache_doc->{prune}, undef, 'a listing has no prune report');
}
{
# The logs document is read from the filesystem, so a run directory and a
# log in it are enough to make the fields more than empty arrays and zeroes.
my $logs_dir = File::Spec->catdir(tempdir(CLEANUP => 1), 'logs');
local $ENV{ACTION_RUN_LOG_DIR} = $logs_dir;
my $run = File::Spec->catdir($logs_dir, '20261021-120000-abc123');
make_path($run);
write_file(File::Spec->catfile($run, 'job-1-test.log'), 'x' x 512);
my ($logs_out, $logs_err, $logs_exit) = run_cli('logs', '--json');
is($logs_exit, 0, 'logs --json exits 0');
my $logs_doc = eval { JSON::PP->new->decode($logs_out) };
ok($logs_doc, 'the logs document decodes') or diag "stderr: $logs_err";
is_deeply([ validate($logs_doc, $logs_schema) ], [],
'the logs document the command prints matches the schema');
is($logs_doc->{schema_version}, App::Actions::Logs->json_schema_version,
'the printed logs document carries the module version');
is_deeply([ sort keys %$logs_doc ], [ sort keys %$logs_example ],
'the printed logs document has the keys the example shows');
is($logs_doc->{logs_directory}, $logs_dir, 'the document names the directory it read');
is($logs_doc->{logs}[0]{run}, '20261021-120000-abc123', 'the run is named');
is($logs_doc->{logs}[0]{name}, 'job-1-test.log', 'the log is named');
is($logs_doc->{logs}[0]{size}, 512, 'the document carries the size rather than text');
is($logs_doc->{totals}{runs}, 1, 'the document counts the runs');
is($logs_doc->{totals}{logs}, 1, 'the document counts the logs');
is($logs_doc->{totals}{size}, 512, 'the document totals the size');
is($logs_doc->{prune}, undef, 'a listing has no prune report');
($logs_out, $logs_err, $logs_exit) = run_cli('logs', '--prune', '--json');
is($logs_exit, 0, 'logs --prune --json exits 0');
$logs_doc = eval { JSON::PP->new->decode($logs_out) };
ok($logs_doc, 'the prune document decodes') or diag "stderr: $logs_err";
is_deeply([ validate($logs_doc, $logs_schema) ], [],
'the prune document matches the schema');
is($logs_doc->{prune}{removed}, 1, 'the document says what the prune removed');
is($logs_doc->{prune}{freed}, 512, 'the document says what the prune freed');
is_deeply($logs_doc->{prune}{failed}, [], 'the document says every removal worked');
is_deeply($logs_doc->{logs}, [], 'the prune left no logs in the document');
}
# The step the fake daemon answers decides an image name, so this document has
# a deferred record too; the example shows that shape, and the schema allows
# the optional expression and error only where they belong.
my @deferred = grep { $_->{status} eq 'deferred' } @{ $preflight_doc->{images} };
is(scalar @deferred, 1, 'the deferred image is in the printed document');
is($deferred[0]{image}, undef, 'a deferred image has no name');
like($deferred[0]{expression}, qr/\Qsteps.pick.outputs.image\E/,
'a deferred image reports the expression it could not resolve');
my @statuses = map { $_->{status} } @{ $preflight_example->{images} };
is_deeply(
[ grep { $_ eq 'deferred' } @statuses ],
[ 'deferred' ],
'the example shows the deferred shape the command can print',
);
# A document that says the run is not ready is the same shape with other
# values, which is what the schema claims.
{
local $ENV{FAKE_OS_TYPE} = 'windows';
my ($broken_out, $broken_err, $broken_exit) = run_cli(
'--docker', $fake, 'preflight', '--json', $workflow, $project,
);
is($broken_exit, 2, 'a preflight on a broken environment exits 2');
my $broken = eval { JSON::PP->new->decode($broken_out) };
ok($broken, 'the failing preflight document decodes') or diag "stderr: $broken_err";
is_deeply([ validate($broken, $preflight_schema) ], [],
'the failing preflight document matches the schema');
ok(!$broken->{ready}, 'the failing document is not ready');
cmp_ok($broken->{problems}, '>', 0, 'the failing document counts a problem');
is_deeply([ sort keys %$broken ], [ sort keys %$preflight_example ],
'a failing document has the keys the example shows');
}
# The same for an image that could not be pulled: the failed shape carries an
# error and leaves the run unready.
{
local $ENV{FAKE_PULL_FAILURE} = 'postgres:15';
my ($failed_out, $failed_err, $failed_exit) = run_cli(
'--pull', '--docker', $fake, 'preflight', '--json', $workflow, $project,
);
is($failed_exit, 2, 'a preflight with an unpullable image exits 2');
my $failed = eval { JSON::PP->new->decode($failed_out) };
ok($failed, 'the failed preflight document decodes') or diag "stderr: $failed_err";
is_deeply([ validate($failed, $preflight_schema) ], [],
'the failed preflight document matches the schema');
my @failed = grep { $_->{status} eq 'failed' } @{ $failed->{images} };
is(scalar @failed, 1, 'the image that could not be pulled is marked');
like($failed[0]{error}, qr/Failed to pull Docker image 'postgres:15'/,
'a failed image reports the reason');
}
# A preflight that skips the probes still reports the host it can see locally,
# so its document has the shape the schema claims rather than empty objects.
{
my ($skipped_out, $skipped_err, $skipped_exit) = run_cli(
'--skip-check', '--docker', $fake, 'preflight', '--json', $workflow, $project,
);
is($skipped_exit, 0, 'a preflight with --skip-check exits 0');
my $skipped = eval { JSON::PP->new->decode($skipped_out) };
ok($skipped, 'the skipped-probe document decodes') or diag "stderr: $skipped_err";
is_deeply([ validate($skipped, $preflight_schema) ], [],
'the skipped-probe document matches the schema');
is($skipped->{host}{os}, $^O, 'the host is reported even when it was not probed');
}
# The commands hold themselves to the contract rather than only being tested
# against it: a document that no longer matches the shipped schema is not
# printed at all. Replacing the schema for the length of a block is the drift
# a forgotten schema change would cause.
{
my $shipped = \&App::Actions::Check::json_schema;
no warnings 'redefine';
local *App::Actions::Check::json_schema = sub {
my ($class) = @_;
my $schema = $shipped->($class);
delete $schema->{properties}{ready};
return $schema;
};
my $printed = '';
open my $sink, '>', \$printed or die $!;
my $check = App::Actions::Check->new(
docker_bin => $fake,
directory => $project,
json => 1,
output => $sink,
);
my $ran = eval { $check->run; 1 };
my $error = $@;
ok(!$ran, 'a check document that does not match the schema is not printed');
ok(ref $error && $error->isa('App::Actions::Schema::Invalid'),
'the failure is the schema exception, not a plain string');
like(
"$error",
qr/does not match App::Actions::Check->json_schema: \$\.ready: unexpected key/,
'the error names the document, the schema and the key at fault',
);
is($error->message, "$error", 'the exception message is what it stringifies to')
if ref $error;
unlike("$error", qr/ at \S+ line \d+/, 'the error reads as a sentence');
is($printed, '', 'nothing is printed when the document does not match');
}
{
my $shipped = \&App::Actions::Runner::json_schema;
no warnings 'redefine';
local *App::Actions::Runner::json_schema = sub {
my ($class) = @_;
my $schema = $shipped->($class);
delete $schema->{properties}{pull_concurrency};
return $schema;
};
my $actions = App::Actions->new(
workflow_file => $workflow,
project_dir => $project,
docker_bin => $fake,
);
( run in 1.049 second using v1.01-cache-2.11-cpan-60884a8df76 )