App-Actions

 view release on metacpan or  search on metacpan

t/bin/fake-docker  view on Meta::CPAN

    }

    open my $marker, '>', _image_marker($image) or die "fake-docker: $!";
    binmode $marker;
    print {$marker} $image;
    close $marker;
    print "Pulled $image\n";
    _timed_log('pull-end', $image) if _timing();
    exit 0;
}

if ($command eq 'network') {
    my $subcommand = shift @args || '';
    exit 0;
}

if ($command eq 'rm') {
    my $index = 0;
    $index++ while $index < @args && $args[$index] =~ /^-/;
    my $name = $args[$index];
    unlink _container_file($name) if defined $name;
    exit 0;
}

if ($command eq 'inspect') {
    exit 1 if $ENV{FAKE_INSPECT_FAILURE};

    my $format = _option_value(\@args, '--format') || '';
    my $name   = $args[-1];

    if ($format =~ /ExitCode/) {
        print _container_died($name) ? _died_status() : 0, "\n";
        exit 0;
    }

    if ($format =~ /RestartCount/) {
        print _restart_count($name), "\n";
        exit 0;
    }

    if ($format =~ /State\.Status/) {
        print _container_state($name), "\n";
        exit 0;
    }

    print _container_died($name) ? "false\n" : "true\n";
    exit 0;
}

if ($command eq 'logs') {
    my $name = $args[-1];

    # FAKE_CONTAINER_EXITS names a container that has stopped, and its last
    # output is what a run is expected to quote back: FAKE_CONTAINER_LOGS on
    # standard output, FAKE_CONTAINER_ERR on standard error, so a capture that
    # loses one of the two streams is noticed.  A restarting container prints
    # the same thing after every attempt, which is why its policy cannot cure
    # it, so it quotes the same output.
    if (_container_died($name) || _restarting_container($name)) {
        my $stdout = $ENV{FAKE_CONTAINER_LOGS};
        my $stderr = $ENV{FAKE_CONTAINER_ERR};

        # The two streams are written to the same file description when a
        # caller merges them, so the buffered one has to go first for the
        # lines to keep the order the container printed them in.
        local $| = 1;
        print $stdout if defined $stdout;
        print STDERR $stderr if defined $stderr && length $stderr;
        exit 0;
    }

    # A container that is still up has printed nothing this fake did not see:
    # an unset FAKE_CONTAINER_LOGS means no output, which is also how a test
    # asks for none - Windows cannot hold an empty string in the environment,
    # so setting one there deletes the variable instead.
    exit 0;
}

if ($command eq 'run') {
    my @run_args = @args;

    # A real daemon cannot start a container from an image it does not have:
    # it pulls the image first and gives up with 125 when that fails.
    # FAKE_RUN_NEEDS_IMAGE turns that on, which is how a probe whose image
    # cannot be pulled fails here the way it does on a real daemon.
    if ($ENV{FAKE_RUN_NEEDS_IMAGE}) {
        my $image = _run_image(\@run_args);
        if (defined $image && !-e _image_marker($image)) {
            my $broken = $ENV{FAKE_PULL_FAILURE};
            if (defined $broken && length $broken
                && ($broken eq '*' || $broken eq $image)) {
                print STDERR "Unable to find image '$image' locally\n";
                exit 125;
            }

            open my $marker, '>', _image_marker($image)
                or die "fake-docker: $!";
            binmode $marker;
            print {$marker} $image;
            close $marker;
        }
    }

    my $name = _option_value(\@run_args, '--name');

    # A daemon that refuses to start a container says why on standard error and
    # exits non-zero, which is the only account of a bad image or a missing
    # entrypoint a run gets.  FAKE_START_FAILURE holds a substring of the name,
    # FAKE_START_ERROR what the daemon says, and FAKE_START_EXIT the status it
    # gives: 125 when it could not create the container, 127 when the runtime
    # could not exec the entrypoint.
    if (_start_fails($name)) {
        my $error = $ENV{FAKE_START_ERROR};
        print STDERR $error if defined $error && length $error;

        my $status = $ENV{FAKE_START_EXIT};
        $status = 125 unless defined $status && $status =~ /\A-?\d+\z/;
        exit $status + 0;
    }

    if (defined $name) {
        open my $state, '>', _container_file($name) or die "fake-docker: $!";
        binmode $state;
        print {$state} join("\t", @run_args);
        close $state;
        print "fake-container-$name\n";
        exit 0;
    }



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