Dist-Zilla-PluginBundle-Author-GETTY
view release on metacpan or search on metacpan
t/binary-provenance.t view on Meta::CPAN
for my $n (readdir $dh) {
next if $n =~ /\Agetconf(?:\.exe)?\z/i || $seen{$n}++ || -e "$bin/$n"; # getconf.exe: Cygwin
symlink "$p/$n", "$bin/$n" if -x "$p/$n" && !-d "$p/$n";
}
}
ok(!-e "$bin/getconf" && !-e "$bin/getconf.exe", 'facts_machine_nogetconf: the restricted PATH has no getconf');
my ($rc, $out, $err) = run_cmd(['sh', script('facts.sh'), 'machine', 'x_'], env => { PATH => $bin });
is($rc, 0, 'facts_machine_nogetconf: exit 0 without getconf') or diag $err;
my ($kv) = parse_part($out);
is_deeply([sort keys %$kv], [sort map { "x_$_" } qw( os architecture abi kernel kernel_release )], 'facts_machine_nogetconf: still the five keys');
}
# fake docker and gh, as perl scripts on a PATH directory
my $fakes = tempdir(CLEANUP => 1);
spew("$fakes/docker", <<"PERL", 0755);
#!$^X
use strict; use warnings; use JSON::PP;
if (defined \$ENV{FAKE_LOG}) { open my \$l, '>>', \$ENV{FAKE_LOG} or die; print \$l join(' ', \@ARGV), "\\n"; close \$l }
my \@a = \@ARGV;
shift \@a if \@a && \$a[0] eq 'image';
if (\@a && \$a[0] eq 'inspect') {
exit 1 if \$ENV{FAKE_INSPECT_FAIL};
my \@dig = split /,/, (\$ENV{FAKE_DIGESTS} // '');
my %img = (Id => \$ENV{FAKE_IMAGE_ID}, Os => 'linux', Architecture => 'amd64', Variant => '', RepoDigests => \\\@dig);
my \$fmt;
for my \$i (0 .. \$#a) { \$fmt = \$a[\$i + 1] if \$a[\$i] =~ /\\A(?:-f|--format)\\z/; \$fmt = \$1 if \$a[\$i] =~ /\\A--format=(.*)\\z/s }
unless (defined \$fmt) { print JSON::PP->new->pretty->canonical->encode([\\%img]); exit 0 }
\$fmt =~ s/\\{\\{range \\.RepoDigests\\}\\}(.*?)\\{\\{end\\}\\}/join '', map { my \$d = \$_; (my \$b = \$1) =~ s{\\{\\{println \\.\\}\\}}{\$d\\n}g; \$b =~ s{\\{\\{\\.\\}\\}}{\$d}g; \$b =~ s{\\{\\{"\\\\n"\\}\\}}{\\n}g; \$b } \@dig/gse;
\$fmt =~ s/\\{\\{json \\.\\}\\}/JSON::PP->new->canonical->encode(\\%img)/ge;
\$fmt =~ s/\\{\\{json \\.(\\w+)\\}\\}/JSON::PP->new->allow_nonref->canonical->encode(\$img{\$1})/ge;
\$fmt =~ s/\\{\\{join \\.RepoDigests "(.*?)"\\}\\}/join \$1, \@dig/ge;
\$fmt =~ s/\\{\\{index \\.RepoDigests (\\d+)\\}\\}/\$dig[\$1] \\/\\/ ''/ge;
\$fmt =~ s/\\{\\{\\.(\\w+)\\}\\}/\$img{\$1} \\/\\/ ''/ge;
\$fmt =~ s/\\{\\{"\\\\n"\\}\\}/\\n/g;
if (\$fmt =~ /\\{\\{/) { print STDERR "fake docker: unsupported --format template: \$fmt\\n"; exit 2 }
print \$fmt, (\$fmt =~ /\\n\\z/ ? '' : "\\n");
exit 0;
}
if (\@a && \$a[0] eq 'run') {
my \@t = ('target_os=almalinux 8.10', 'target_architecture=x86_64', 'target_abi=glibc 2.28', 'target_kernel=Linux', 'target_kernel_release=6.8.0');
print "\$_\\n" for \@t;
exit 0;
}
exit 1;
PERL
spew("$fakes/gh", <<"PERL", 0755);
#!$^X
use strict; use warnings; use JSON::PP;
if (defined \$ENV{FAKE_LOG}) { open my \$l, '>>', \$ENV{FAKE_LOG} or die; print \$l join(' ', \@ARGV), "\\n"; close \$l }
my \@a = \@ARGV;
shift \@a if \@a && \$a[0] eq 'api';
my (\$ep, \$jq);
while (\@a) {
my \$x = shift \@a;
if (\$x =~ /\\A(?:-H|--header|-X|--method|-f|-F|--hostname|--cache)\\z/) { shift \@a }
elsif (\$x =~ /\\A(?:-q|--jq)\\z/) { \$jq = shift \@a }
elsif (\$x =~ /\\A--jq=(.*)\\z/s) { \$jq = \$1 }
elsif (\$x =~ /\\A-/) { }
else { \$ep //= \$x }
}
die "fake gh: no endpoint\\n" unless defined \$ep;
\$ep =~ s{\\A/}{};
(my \$file = "\$ENV{FAKE_GH}/\$ep.json") =~ s{(?<=\\A\$ENV{FAKE_GH}/)(.*)\\.json\\z}{ (my \$n = \$1) =~ s{/}{_}g; "\$n.json" }e;
unless (-f \$file) { print STDERR "gh: Not Found (HTTP 404)\\n"; exit 1 }
open my \$fh, '<', \$file or die; my \$json = do { local \$/; <\$fh> };
my \$data = JSON::PP->new->decode(\$json);
unless (defined \$jq) { print JSON::PP->new->canonical->encode(\$data), "\\n"; exit 0 }
sub path { my (\$p) = \@_; my \$v = \$data; for my \$k (grep { length } split /\\./, \$p) { \$v = ref \$v eq 'HASH' ? \$v->{\$k} : undef } return defined \$v ? \$v : '' }
my \$out;
if (\$jq =~ /\\A"(.*)"\\z/s) { (\$out = \$1) =~ s/\\\\\\(([.\\w]+)\\)/path(\$1)/ge }
elsif (\$jq =~ /\\A[.\\w]+\\z/) { \$out = path(\$jq) }
elsif (\$jq =~ /\\A(?:\\S+|"[^"]*")(?:\\s+\\+\\s+(?:\\S+|"[^"]*"))+\\z/) {
\$out = join '', map { /\\A"(.*)"\\z/s ? \$1 : path(\$_) } split /\\s+\\+\\s+/, \$jq;
}
else { print STDERR "fake gh: unsupported --jq expression: \$jq\\n"; exit 2 }
print "\$out\\n";
PERL
sub fake_env {
my (%e) = @_;
my $log = tempdir(CLEANUP => 1) . '/calls.log';
return ({ PATH => "$fakes:$ENV{PATH}", FAKE_LOG => $log, %e }, $log);
}
{ # smoke, linux, with SMOKE set
my $d = tempdir(CLEANUP => 1);
spew("$d/bins/foo-1.0-linux-x86_64", "the binary\n");
my $id = 'sha256:' . ('d' x 64);
my @dig = ('docker.io/lib@sha256:' . ('b' x 64), 'docker.io/lib@sha256:' . ('a' x 64));
my ($env, $log) = fake_env(FAKE_IMAGE_ID => $id, FAKE_DIGESTS => join(',', @dig),
PLATFORM => 'linux-x86_64', BIN_NAME => 'foo', ASSET => 'foo-1.0-linux-x86_64', SMOKE => 'foo --version',
IMAGE => 'almalinux:8', RUNNER_LABEL => 'ubuntu-24.04', BUNDLE_COMMIT => $WF, GITHUB_RUN_ATTEMPT => '1',
ImageOS => 'ubuntu24', ImageVersion => '20261110.1', RUNNER_OS => 'Linux', RUNNER_ARCH => 'X64');
my ($rc, $out, $err) = facts($env, 'smoke', "$d/smoke-0.txt", "$d/bins/foo-1.0-linux-x86_64");
is($rc, 0, 'facts_smoke: exit 0') or diag $err;
my ($kv, $bad) = parse_part(-f "$d/smoke-0.txt" ? slurp("$d/smoke-0.txt") : '');
is_deeply($bad, [], 'facts_smoke: every line of the part is KEY=VALUE');
is(one($kv, 'kind'), 'smoke', 'facts_smoke: kind=smoke');
is(one($kv, $_->[0]), $_->[1], "facts_smoke: $_->[0]") for
[platform => 'linux-x86_64'], [binary => 'foo'], [asset => 'foo-1.0-linux-x86_64'], [smoked => 'true'],
[sha256 => sha256_hex("the binary\n")], [runner => 'ubuntu-24.04'], [bundle_commit => $WF], [run_attempt => '1'],
[runner_image_os => 'ubuntu24'], [runner_image_version => '20261110.1'], [runner_os => 'Linux'], [runner_arch => 'X64'],
[image => 'almalinux:8'], [image_id => $id], [image_platform => 'linux/amd64'],
[target_os => 'almalinux 8.10'], [target_architecture => 'x86_64'], [target_abi => 'glibc 2.28'], [target_kernel => 'Linux'];
is_deeply([sort @{ $kv->{image_repo_digest} || [] }], [sort @dig], 'facts_smoke: image_repo_digest repeats once per digest');
ok(exists $kv->{$_}, "facts_smoke: $_ is written") for map { "host_$_" } qw( os architecture abi kernel kernel_release );
my $calls = -f $log ? slurp($log) : '';
like($calls, qr/inspect/, 'facts_smoke: docker image inspect was asked');
like($calls, qr/\brun\b.*\Q$id\E/, 'facts_smoke: the in-image facts run uses the image id, not the tag');
like($calls, qr/machine/, 'facts_smoke: the in-image run is `facts.sh machine ...`');
like($calls, qr/target_/, 'facts_smoke: ... with the target_ prefix');
}
{ # smoke without a smoke command: no docker call at all
my $d = tempdir(CLEANUP => 1);
spew("$d/bins/foo-1.0-linux-x86_64", "the binary\n");
my ($env, $log) = fake_env(PLATFORM => 'linux-x86_64', BIN_NAME => 'foo', ASSET => 'foo-1.0-linux-x86_64', SMOKE => '',
IMAGE => 'almalinux:8', RUNNER_LABEL => 'ubuntu-24.04', BUNDLE_COMMIT => $WF, GITHUB_RUN_ATTEMPT => '1',
ImageOS => 'ubuntu24', ImageVersion => '20261110.1', RUNNER_OS => 'Linux', RUNNER_ARCH => 'X64');
my ($rc, $out, $err) = facts($env, 'smoke', "$d/smoke-0.txt", "$d/bins/foo-1.0-linux-x86_64");
is($rc, 0, 'facts_smoke_false: exit 0') or diag $err;
my ($kv) = parse_part(-f "$d/smoke-0.txt" ? slurp("$d/smoke-0.txt") : '');
is(one($kv, 'smoked'), 'false', 'facts_smoke_false: smoked=false');
is(one($kv, 'sha256'), sha256_hex("the binary\n"), 'facts_smoke_false: the binary is hashed all the same');
is($kv->{$_} ? $kv->{$_}[0] : '', '', "facts_smoke_false: $_ is null") for qw( image image_id target_os );
ok(!-e $log, 'facts_smoke_false: docker was not called');
}
{ # smoke: inspect failing is a failure
my $d = tempdir(CLEANUP => 1);
spew("$d/bins/foo-1.0-linux-x86_64", "the binary\n");
my ($env) = fake_env(FAKE_INSPECT_FAIL => 1, PLATFORM => 'linux-x86_64', BIN_NAME => 'foo', ASSET => 'foo-1.0-linux-x86_64',
SMOKE => 'foo --version', IMAGE => 'almalinux:8', RUNNER_LABEL => 'ubuntu-24.04', BUNDLE_COMMIT => $WF, GITHUB_RUN_ATTEMPT => '1',
ImageOS => 'ubuntu24', ImageVersion => '1', RUNNER_OS => 'Linux', RUNNER_ARCH => 'X64');
my ($rc) = facts($env, 'smoke', "$d/smoke-0.txt", "$d/bins/foo-1.0-linux-x86_64");
is($rc, 1, 'facts_smoke_inspect_fails: docker image inspect failing makes facts.sh exit 1');
}
{ my ($rc) = facts({}, 'bogus'); is($rc, 1, 'facts_usage: an unknown subcommand exits 1') }
# plan: gh is faked, the tag chain comes from canned API answers
sub gh_dir {
my (%ep) = @_; # endpoint => data
my $d = tempdir(CLEANUP => 1);
for my $e (keys %ep) { (my $n = $e) =~ s{/}{_}g; spew("$d/$n.json", JSON::PP->new->encode($ep{$e})) }
return $d;
}
sub plan_env {
my (%e) = @_;
# the superset of names the design leaves open for BUNDLE_*/WORKFLOW_*/CALLER_*
return (
TAG => 'v1.0', VERSION_SLOT => '1.0', GH_TOKEN => 'x',
GITHUB_SERVER_URL => 'https://github.com', GITHUB_REPOSITORY => 'Getty/Foo-Bar', GITHUB_RUN_ID => '4242',
GITHUB_RUN_ATTEMPT => '1', GITHUB_EVENT_NAME => 'push', GITHUB_REF => 'refs/tags/v1.0', GITHUB_SHA => $RUNSHA,
CALLER_WORKFLOW_REF => 'Getty/Foo-Bar/.github/workflows/binaries.yml@refs/tags/v1.0', CALLER_WORKFLOW_SHA => $RUNSHA,
WORKFLOW_REPOSITORY => $WFREPO, WORKFLOW_PATH => '.github/workflows/dzil-binary.yml', WORKFLOW_FILE_PATH => '.github/workflows/dzil-binary.yml',
WORKFLOW_REF => 'x@refs/tags/binary-v1', WORKFLOW_SHA => $WF, WORKFLOW_COMMIT => $WF,
BUNDLE_REF_INPUT => '', BUNDLE_REF => '', BUNDLE_SOURCE => 'workflow', BUNDLE_COMMIT => $WF,
%e);
}
sub plan_run {
my ($gh, %e) = @_;
my $d = tempdir(CLEANUP => 1);
my ($env, $log) = fake_env(FAKE_GH => $gh, plan_env(%e));
my ($rc, $out, $err) = facts($env, 'plan', "$d/plan.txt");
my ($kv, $bad) = parse_part(-f "$d/plan.txt" ? slurp("$d/plan.txt") : '');
return ($rc, $kv, $bad, $err, $log);
}
my $TAGGED = hex40('tagged');
{ # annotated tag: ref -> tag object -> commit
my $gh = gh_dir(
'repos/Getty/Foo-Bar/git/ref/tags/v1.0' => { object => { type => 'tag', sha => $TAGOBJ } },
"repos/Getty/Foo-Bar/git/tags/$TAGOBJ" => { object => { type => 'commit', sha => $TAGGED } },
);
my ($rc, $kv, $bad, $err) = plan_run($gh);
is($rc, 0, 'facts_plan_annotated: exit 0') or diag $err;
is_deeply($bad, [], 'facts_plan_annotated: every line is KEY=VALUE');
is(one($kv, 'kind'), 'plan', 'facts_plan_annotated: kind=plan');
is(one($kv, 'mode'), 'release', 'facts_plan_annotated: mode=release');
is(one($kv, 'source_commit'), $TAGGED, 'facts_plan_annotated: source_commit is the commit the tag object points at');
is(one($kv, 'source_tag_object'), $TAGOBJ, 'facts_plan_annotated: source_tag_object is the tag object');
is(one($kv, 'tag'), 'v1.0', 'facts_plan_annotated: tag');
is(one($kv, 'version_slot'), '1.0', 'facts_plan_annotated: version_slot');
is(one($kv, 'run_id'), '4242', 'facts_plan_annotated: run_id');
}
{ # lightweight tag
my $gh = gh_dir('repos/Getty/Foo-Bar/git/ref/tags/v1.0' => { object => { type => 'commit', sha => $TAGGED } });
my ($rc, $kv, undef, $err) = plan_run($gh);
is($rc, 0, 'facts_plan_lightweight: exit 0') or diag $err;
is(one($kv, 'source_commit'), $TAGGED, 'facts_plan_lightweight: source_commit');
is($kv->{source_tag_object} ? $kv->{source_tag_object}[0] : '', '', 'facts_plan_lightweight: no tag object');
}
{ # tag of a tag
my $t2 = hex40('second');
my $gh = gh_dir(
'repos/Getty/Foo-Bar/git/ref/tags/v1.0' => { object => { type => 'tag', sha => $TAGOBJ } },
"repos/Getty/Foo-Bar/git/tags/$TAGOBJ" => { object => { type => 'tag', sha => $t2 } },
"repos/Getty/Foo-Bar/git/tags/$t2" => { object => { type => 'commit', sha => $TAGGED } },
);
my ($rc, $kv, undef, $err) = plan_run($gh);
is($rc, 0, 'facts_plan_chain: a tag of a tag is followed') or diag $err;
is(one($kv, 'source_commit'), $TAGGED, 'facts_plan_chain: it ends at the commit');
( run in 0.856 second using v1.01-cache-2.11-cpan-eb134014566 )