API-Docker

 view release on metacpan or  search on metacpan

t/entity_container.t  view on Meta::CPAN

  is_deeply \%seen, { start => 1, stop => 1, remove => 1 },
    'each reached its own endpoint under the id of the object it was called on';

  # inspect is the one that changes shape underneath the caller.
  my $full = $c->inspect;
  isa_ok $full, $INSPECT,
    'a summary inspects into the inspect class, not into another summary';
};

subtest 'is_running reads whichever of the two shapes it is on' => sub {
  is $SUMMARY->new(State => 'running')->is_running, 1, 'summary: the string';
  is $SUMMARY->new(State => 'exited')->is_running,  0, 'summary: a stopped one';
  is $SUMMARY->new(State => 'RUNNING')->is_running, 1,
    'summary: the comparison is case-insensitive';
  is $SUMMARY->new(Id => 'x')->is_running, 0,
    'summary: no State at all is not running';

  is $INSPECT->new(State => { Running => 1 })->is_running, 1,
    'inspect: the ContainerState object';
  is $INSPECT->new(State => { Running => 0, Status => 'exited' })->is_running, 0,
    'inspect: a stopped one';
  is $INSPECT->new(State => { Status => 'created' })->is_running, 0,
    'inspect: a State that never says Running is not running';
  is $INSPECT->new(Id => 'x')->is_running, 0,
    'inspect: no State at all is not running';
};

# "a method forwards the entity own id to the resource class" above covers
# start/stop/remove/inspect; kill, logs, pause, restart, top, stats and
# unpause were only ever proved with can() (karr k109). Call each and assert
# what actually reached the mock route table.
subtest 'the remaining container methods forward the entity own id too' => sub {
  plan skip_all => 'route assertions are fixture-only' if is_live();

  my $multiplexed = load_fixture_raw('containers_logs_multiplexed.bin');

  my %seen;
  my $docker = test_docker(
    'GET /containers/json'              => [ { Id => 'deadbeef', State => 'running' } ],
    'POST /containers/deadbeef/kill'    => sub {
      my ($method, $path, %opts) = @_;
      $seen{kill} = $opts{params};
      return undef;
    },
    'GET /containers/deadbeef/logs'     => sub { $multiplexed },
    'POST /containers/deadbeef/pause'   => sub { $seen{pause}++;   return undef },
    'POST /containers/deadbeef/restart' => sub { $seen{restart}++; return undef },
    'GET /containers/deadbeef/top'      => { Titles => [], Processes => [] },
    'GET /containers/deadbeef/stats'    => { cpu_stats => { cpu_usage => { total_usage => 1 } } },
    'POST /containers/deadbeef/unpause' => sub { $seen{unpause}++; return undef },
  );

  my ($c) = @{ $docker->containers->list };

  $c->kill(signal => 'SIGTERM');
  is_deeply $seen{kill}, { signal => 'SIGTERM' },
    "kill reached POST /containers/deadbeef/kill under the entity's own id";

  is_deeply $c->logs, [
    { stream => 'stdout', data => "OUT\n" },
    { stream => 'stderr', data => "ERR\n" },
  ], 'logs reached GET /containers/deadbeef/logs and demultiplexed the frames';

  is $c->pause, 1, 'pause reached POST /containers/deadbeef/pause';
  ok $seen{pause}, 'and was actually requested';

  is $c->restart, 1, 'restart reached POST /containers/deadbeef/restart';
  ok $seen{restart}, 'and was actually requested';

  is_deeply $c->top, { Titles => [], Processes => [] },
    'top reached GET /containers/deadbeef/top';

  is_deeply $c->stats, { cpu_stats => { cpu_usage => { total_usage => 1 } } },
    'stats reached GET /containers/deadbeef/stats';

  is $c->unpause, 1, 'unpause reached POST /containers/deadbeef/unpause';
  ok $seen{unpause}, 'and was actually requested';
};

done_testing;



( run in 0.808 second using v1.01-cache-2.11-cpan-54e63673c56 )