Kubernetes-REST
view release on metacpan or search on metacpan
t/16_cli_watch.t view on Meta::CPAN
my $output = '';
open my $oldfh, '>&', \*STDOUT;
{
local *STDOUT;
open STDOUT, '>', \$output;
$w->_print_text($event);
}
open STDOUT, '>&', $oldfh;
like $output, qr/ERROR\(410\)/, 'error code shown';
like $output, qr/Gone/, 'error message shown';
};
subtest '_print_text - pod with phase status' => sub {
my $w = make_watcher(timestamp_format => 'epoch');
my $event = make_event(
type => 'MODIFIED',
name => 'running-pod',
namespace => 'default',
status => { phase => 'Running' },
);
my $output = '';
open my $oldfh, '>&', \*STDOUT;
{
local *STDOUT;
open STDOUT, '>', \$output;
$w->_print_text($event);
}
open STDOUT, '>&', $oldfh;
like $output, qr/Running/, 'status hint shows phase';
};
# ============================================================================
# run() - error handling
# ============================================================================
subtest 'run - no kind dies' => sub {
my $w = make_watcher();
throws_ok { $w->run(undef) } qr/Usage: kube_watch/,
'run without kind dies with usage';
};
subtest 'run - empty kind dies' => sub {
my $w = make_watcher();
throws_ok { $w->run('') } qr/Usage: kube_watch/,
'run with empty kind dies with usage';
};
# ============================================================================
# argv handling - Kind is the first positional after new_with_options
#
# bin/kube_watch reads the Kind with `shift @ARGV` after new_with_options.
# MooX::Options must strip the parsed options from @ARGV (protect_argv => 0),
# otherwise `kube_watch -n default Pod` shifts off '-n' instead of 'Pod'.
# ============================================================================
subtest 'new_with_options - options before Kind, @ARGV left with Kind first' => sub {
local @ARGV = ('-n', 'default', 'Pod');
my $w = Kubernetes::REST::CLI::Watch->new_with_options;
is $w->namespace, 'default', 'namespace option parsed';
# This is exactly what bin/kube_watch does to obtain the Kind
my $kind = shift @ARGV;
is $kind, 'Pod', 'Kind is Pod, not the consumed -n option';
};
subtest 'new_with_options - Kind after options, mixed short options' => sub {
local @ARGV = ('-n', 'default', '-o', 'json', 'Deployment');
my $w = Kubernetes::REST::CLI::Watch->new_with_options;
is $w->namespace, 'default', 'namespace parsed';
is $w->output, 'json', 'output parsed';
my $kind = shift @ARGV;
is $kind, 'Deployment', 'Kind is Deployment after consumed options';
};
subtest 'new_with_options - Kind before options still works' => sub {
local @ARGV = ('Pod', '-n', 'kube-system');
my $w = Kubernetes::REST::CLI::Watch->new_with_options;
is $w->namespace, 'kube-system', 'namespace parsed';
my $kind = shift @ARGV;
is $kind, 'Pod', 'Kind is Pod when given before options';
};
# ============================================================================
# _name_re - regex compilation
# ============================================================================
subtest '_name_re - valid regex' => sub {
my $w = make_watcher(names => '^nginx-\d+');
my $re = $w->_name_re;
ok $re, 'regex compiled';
ok 'nginx-123' =~ $re, 'regex matches expected string';
ok 'redis-123' !~ $re, 'regex rejects non-matching string';
};
subtest '_name_re - invalid regex dies' => sub {
my $w = make_watcher(names => '(?invalid');
throws_ok { $w->_name_re } qr/Invalid --names regex/,
'invalid regex dies';
};
subtest '_name_re - no names returns undef' => sub {
my $w = make_watcher();
ok !defined $w->_name_re, 'no names returns undef';
};
# ============================================================================
# _type_filter
# ============================================================================
subtest '_type_filter - no filter returns empty hash' => sub {
my $w = make_watcher();
my $tf = $w->_type_filter;
is ref $tf, 'HASH', 'returns hashref';
is scalar keys %$tf, 0, 'empty when no event_type set';
};
subtest '_type_filter - parses comma-separated types' => sub {
my $w = make_watcher(event_type => 'ADDED,DELETED');
my $tf = $w->_type_filter;
ok $tf->{ADDED}, 'ADDED in filter';
ok $tf->{DELETED}, 'DELETED in filter';
ok !$tf->{MODIFIED}, 'MODIFIED not in filter';
};
subtest '_type_filter - uppercases types' => sub {
my $w = make_watcher(event_type => 'added,modified');
my $tf = $w->_type_filter;
ok $tf->{ADDED}, 'added uppercased to ADDED';
ok $tf->{MODIFIED}, 'modified uppercased to MODIFIED';
};
# ============================================================================
# CLI::Role::Connection - _build_api
# ============================================================================
subtest 'CLI::Role::Connection - api attribute with kubeconfig' => sub {
( run in 2.435 seconds using v1.01-cache-2.11-cpan-145b269f2f3 )