App-Actions

 view release on metacpan or  search on metacpan

t/check.t  view on Meta::CPAN

use strict;
use warnings;
use Test::Most;
use Capture::Tiny qw(capture);
use File::Basename qw(dirname);
use File::Path qw(make_path);
use File::Spec;
use File::Temp qw(tempdir);

use JSON::PP ();

use App::Actions::Check;

my $fake_docker = File::Spec->rel2abs(
    File::Spec->catfile(dirname(__FILE__), 'bin', 'fake-docker'),
);

# Windows writes CRLF when output is redirected to a file, and several checks
# match against the end of a line.
sub useful_text {
    my ($text) = @_;
    return '' unless defined $text;
    $text =~ s/\r\n/\n/g;
    return $text;
}

sub run_check {
    my (%options) = @_;

    my $state = tempdir(CLEANUP => 1);
    my $log   = File::Spec->catfile($state, 'docker.log');
    local $ENV{FAKE_DOCKER_STATE}   = $state;
    local $ENV{FAKE_DOCKER_LOG}     = $log;
    local $ENV{FAKE_NO_DAEMON}      = $options{no_daemon} ? 1 : 0;
    local $ENV{FAKE_OS_TYPE}        = $options{os_type};
    local $ENV{FAKE_BAD_MOUNT}      = $options{bad_mount} ? 1 : 0;
    local $ENV{FAKE_MOUNT_FAILURE}  = $options{mount_failure} ? 1 : 0;
    local $ENV{FAKE_ENTRY_COUNT}    = $options{entry_count};

    my $check = App::Actions::Check->new(
        docker_bin => defined $options{docker_bin} ? $options{docker_bin} : $fake_docker,
        directory  => $options{directory},
        (%{ $options{args} || {} }),
    );

    my ($stdout, $stderr, $exit) = capture { $check->run };

    my $log_text = '';
    if (-f $log) {
        open my $handle, '<', $log or die $!;
        local $/;
        $log_text = <$handle>;
        close $handle;
        $log_text = '' unless defined $log_text;
    }

    return {
        stdout => useful_text($stdout),
        stderr => useful_text($stderr),
        exit   => $exit,
        log    => useful_text($log_text),
    };
}

sub write_file {
    my ($path, $contents) = @_;

    open my $handle, '>', $path or die $!;
    print {$handle} $contents;
    close $handle;

    return $path;
}

is(App::Actions::Check->new->image, 'alpine:3.20', 'the probe image defaults to a small one');
is(App::Actions::Check->new(image => 'perl:5.38')->image, 'perl:5.38', 'the probe image can be chosen');

my $ready = run_check();
is($ready->{exit}, 0, 'a working environment is ready');
like($ready->{stdout}, qr/^action-run check$/m, 'the report names the command');
like($ready->{stdout}, qr/^ {2}host\s+\S.*perl \d/m, 'the host platform is reported');
like($ready->{stdout}, qr/^ {2}docker\s+\S/m, 'the Docker command is reported');
like($ready->{stdout}, qr/^ {2}client\s+26\.0\.0$/m, 'the client version is reported');
like($ready->{stdout}, qr/^ {2}daemon\s+26\.0\.0, linux containers$/m,
    'the daemon version and container type are reported');
like($ready->{stdout}, qr{^ {2}mount\s+\S.* -> /action-run-check: OK$}m,
    'the temporary directory is mounted and read back');
like($ready->{stdout}, qr/^Result: ready$/m, 'the result is reported');

# A report pasted into an issue says which contract its lines were written
# against, and the version comes before the verdict.
my $version = App::Actions::Check->json_schema_version;
like(
    $ready->{stdout},
    qr/^ {2}schema {4}version \Q$version\E \(action-run schema check\)$/m,
    'the report names the schema it was written against',
);
my $schema_at = index($ready->{stdout}, 'schema    version');
my $verdict_at = index($ready->{stdout}, 'Result: ready');
ok(
    $schema_at >= 0 && $schema_at < $verdict_at,
    'the schema is named, and before the verdict',
);

my $untold = run_check(args => { schema_line => 0 });
unlike($untold->{stdout}, qr/^ {2}schema/m,
    'a caller that prints its own contract can leave the line out');

unlike($ready->{stdout}, qr{/check-project}, 'no project directory is probed unless asked');
unlike($ready->{stdout}, qr/^ {2}remedy/m, 'a ready environment suggests nothing');
is($ready->{stderr}, '', 'nothing is written to standard error');

my $project = tempdir(CLEANUP => 1);
write_file(File::Spec->catfile($project, 'first.txt'), "one\n");
write_file(File::Spec->catfile($project, 'second.txt'), "two\n");
make_path(File::Spec->catdir($project, 'subdirectory'));

my $with_directory = run_check(directory => $project);
is($with_directory->{exit}, 0, 'a project directory is ready');
like($with_directory->{stdout}, qr{-> /check-project: OK \(3 entries\)$}m,
    'the project directory is visible inside the container');

my $hidden = run_check(directory => $project, entry_count => 0);
is($hidden->{exit}, 2, 'a project the container cannot see is not ready');
like($hidden->{stdout}, qr/^ {2}problem\s+'.*' -> \/check-project sees 0 entries, the host has 3$/m,
    'the entry counts that disagree are reported');
like($hidden->{stdout}, qr/^ {2}remedy\s+\S/m, 'a directory that is not visible comes with a remedy');
like(
    $hidden->{stdout},
    $^O eq 'MSWin32'
        ? qr/^ {2}remedy\s+share the drive that holds/m
        : qr/^ {2}remedy\s+choose a directory the daemon can see/m,
    'the remedy matches the host platform',
);
like($with_directory->{log}, qr/^run\t--rm\t--network\tnone\t/m,
    'the probe container is short-lived and uses no network');
like($with_directory->{log}, qr{\t-v\t[^\t]+:/check-project:ro\t},
    'the project directory is mounted read-only');

my $with_platform = run_check(
    directory => $project,
    args      => { platform => 'linux/amd64' },
);
like($with_platform->{log}, qr/\t--platform\tlinux\/amd64\t/,
    'the requested Docker platform is passed to the probe');

my $windows = run_check(os_type => 'windows');
is($windows->{exit}, 2, 'a Windows-only daemon is not ready');
like($windows->{stdout}, qr/^ {2}daemon\s+26\.0\.0, windows containers$/m,
    'the container type is reported');
like($windows->{stdout}, qr/problem\s+the daemon runs 'windows' containers; action-run needs Linux containers/,
    'the problem is explained');
like($windows->{stdout}, qr/^ {2}remedy\s+\S/m, 'the container type problem comes with a remedy');
like($windows->{stdout}, qr/^Result: not ready \(1 problem\)$/m, 'the result is not ready');
unlike($windows->{stdout}, qr{/action-run-check}, 'no mount probe runs without Linux containers');

my $down = run_check(no_daemon => 1);
is($down->{exit}, 2, 'an unreachable daemon is not ready');
like($down->{stdout}, qr/^ {2}client\s+26\.0\.0$/m, 'the client is still reported');
like($down->{stdout}, qr/problem\s+the daemon is not reachable/, 'the daemon problem is explained');
like($down->{stdout}, qr/^ {2}remedy\s+\S/m, 'the daemon problem comes with a remedy');
like($down->{stdout}, qr/^Result: not ready \(1 problem\)$/m, 'the result is not ready');

my $missing = run_check(
    docker_bin => File::Spec->catfile(tempdir(CLEANUP => 1), 'no-such-docker'),
);
is($missing->{exit}, 2, 'a missing Docker command is not ready');
like($missing->{stdout}, qr/problem\s+the Docker command '.*' could not be run/,
    'the missing command is explained');
like($missing->{stdout}, qr/^ {2}remedy\s+.*--docker/m,
    'the missing command suggests --docker');



( run in 1.008 second using v1.01-cache-2.11-cpan-60884a8df76 )