App-Actions

 view release on metacpan or  search on metacpan

t/smoke.t  view on Meta::CPAN

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

my $root = File::Spec->rel2abs(File::Spec->catdir(dirname(__FILE__), '..'));
my $lib  = File::Spec->catdir($root, 'lib');
my $script = File::Spec->catfile($root, 'bin', 'action-run-smoke');
my $fake_docker = File::Spec->catfile($root, 't', 'bin', 'fake-docker');

my $state   = tempdir(CLEANUP => 1);
my $project = tempdir(CLEANUP => 1);

# Object::Configure reads the developer's own configuration files (~/.conf
# and the like), where a UNIVERSAL logger would take the messages these tests
# read off standard error.  An empty directory leaves the defaults in force.
local $ENV{CONFIG_DIR} = tempdir(CLEANUP => 1);

# The smoke script runs workflows, which record the images they keep.
local $ENV{ACTION_RUN_CACHE_FILE}
    = File::Spec->catfile($state, 'cache.json');
local $ENV{ACTION_RUN_LOG_DIR}
    = File::Spec->catdir($state, 'logs');

# The fake Docker keeps its state in FAKE_DOCKER_STATE and is handed to the
# smoke script through --docker, as a user would hand it their own wrapper.
local $ENV{FAKE_DOCKER_STATE} = $state;

# Windows writes CRLF when output is redirected to a file, and the checks
# below 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_smoke {
    my (@args) = @_;

    my ($stdout, $stderr, $status) = capture {
        system($^X, '-I' . $lib, $script, @args);
    };

    # $? carries the POSIX-compatible status on Windows too.
    return (useful_text($stdout), useful_text($stderr), $status >> 8);
}

my ($version_out, undef, $version_exit) = run_smoke('--version');
is($version_exit, 0, '--version exits 0');
like($version_out, qr/^action-run-smoke 0\.002\.1$/m, '--version prints the version');

my ($help_out, undef, $help_exit) = run_smoke('--help');
is($help_exit, 0, '--help exits 0');
like($help_out, qr/Usage:/, '--help prints usage');
like($help_out, qr/--directory/, '--help lists the options');
like($help_out, qr/Docker Desktop/, '--help explains the Windows setup');

my (undef, $extra_err, $extra_exit) = run_smoke('unexpected');
is($extra_exit, 2, 'an unexpected argument is a usage error');
like($extra_err, qr/Usage: action-run-smoke/, 'the usage error explains itself');

my ($out, $err, $exit) = run_smoke(
    '--docker', $fake_docker, '--directory', $project,
);
is($exit, 0, 'a ready environment passes the smoke test');
like($out, qr/^action-run-smoke$/m, 'the report names itself');
like($out, qr/^ {2}host\s+\S.*perl \d/m, 'the host platform is reported');
like($out, qr/^ {2}daemon\s+26\.0\.0, linux containers$/m, 'the daemon is reported');
like($out, qr{^ {2}mount\s+\S.* -> /check-project: OK \(\d+ entr}m,
    'the directory is mounted');
like($out, qr/^ {2}image {5}\S+ \(job 'smoke'\): (?:pulled|present)$/m,
    'the image the workflow needs is reported');
like($out, qr/^ {2}workflow\s+one job, four steps/m, 'the workflow is described');
like($out, qr/Workflow succeeded/, 'the workflow runs against Docker');
like($out, qr/^Result: smoke test passed$/m, 'the result is reported once');
unlike($out, qr/^ {2}schema/m,
    'the smoke report names no document contract, because it prints none');
unlike($out, qr/^Result: ready$/m, 'the environment report leaves the verdict alone');
is($err, '', 'a passing smoke test writes nothing to standard error');

{
    local $ENV{FAKE_OS_TYPE} = 'windows';
    my ($windows_out, undef, $windows_exit) = run_smoke(
        '--docker', $fake_docker, '--directory', $project,
    );
    is($windows_exit, 2, 'a Windows-only daemon fails the smoke test');
    like($windows_out, qr/^ {2}problem\s+the daemon runs 'windows' containers/m,
        'the container type problem is explained');
    like($windows_out, qr/^ {2}problem\s+the environment is not ready/m,
        'the environment is named as the cause');
    like($windows_out, qr/^ {2}remedy\s+\S/m, 'the check supplies the remedy');
    like($windows_out, qr/^Result: smoke test failed$/m, 'the failure is reported');
    unlike($windows_out, qr/^ {2}workflow\s/, 'no workflow runs without Linux containers');
}

{
    local $ENV{FAKE_NO_DAEMON} = 1;
    my ($down_out, undef, $down_exit) = run_smoke(
        '--docker', $fake_docker, '--directory', $project,
    );
    is($down_exit, 2, 'an unreachable daemon fails the smoke test');
    like($down_out, qr/^ {2}problem\s+the daemon is not reachable/m,
        'the daemon problem is explained');
}



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