App-Netdisco
view release on metacpan or search on metacpan
xt/206-vrf-helper-branches.t view on Meta::CPAN
# reach: malformed or absent values, and the fallbacks for them. Each helper
# is called directly, with no database and no device.
my $CONFIG = config();
$CONFIG->{logger} = 'console';
$CONFIG->{log} = ($ENV{'DANCER_DEBUG'} ? 'debug' : 'error');
Dancer::Logger->init('console', $CONFIG);
my $device = App::Netdisco::DB->resultset('Device')->new_result({ ip => '192.0.2.1' });
sub with_settings {
my ($code, %settings) = @_;
my %saved = map { $_ => setting($_) } keys %settings;
set($_ => $settings{$_}) for keys %settings;
my @result = eval { $code->() };
my $error = $@;
set($_ => $saved{$_}) for keys %saved;
die $error if $error;
return wantarray ? @result : $result[0];
}
# an Expect stand-in serving one FakeSSH fixture, past the login prompt as
# the session openers leave it
sub fake_expect {
my $text = shift;
my $ssh = Test::Netdisco::FakeSSH->new(text => $text);
my $exp = Test::Netdisco::FakeSSH::Expect->new($ssh);
$exp->{buffer} = '';
return ($exp, $ssh);
}
subtest 'vrf_skip_test__default_table_names_and_scalar_setting__not_skipped' => sub {
with_settings(sub {
my $skipped = vrf_skip_test($device);
is $skipped->(undef), 0, 'undef name';
is $skipped->(''), 0, 'empty name';
is $skipped->('default'), 0, 'default';
is $skipped->('red'), 0, 'scalar setting never matches';
}, vrf_no_devicevrfs => '192.0.2.1:red');
};
subtest 'netdisco_vrf__undef__undef' => sub {
is netdisco_vrf(undef), undef, 'undef stays undef';
is netdisco_vrf('default'), '', 'default is the empty name';
is netdisco_vrf('Default'), 'Default', 'only the exact name';
};
subtest 'vrf_instances_for__supported_class_without_instances__empty_hash' => sub {
my $snmp = bless {}, 'NoInstancesSNMP';
{ package NoInstancesSNMP; sub vrf_instances_complete { 0 } sub vrf_instances { undef } }
my $got = with_settings(sub { vrf_instances_for($device, $snmp) }, store_vrfs => 1);
is_deeply $got, {}, 'no instances, not undef';
};
{
package ContextSNMP;
sub new { bless { updates => [] }, shift }
sub snmp_ver { 2 }
sub snmp_comm { 'public' }
sub class { 'SNMP::Info::Layer3' }
sub update { my ($self, %args) = @_; push @{ $self->{updates} }, \%args; return 1 }
}
subtest 'snmp_vrf_context__context_without_kind_or_name__switched_or_refused' => sub {
my $snmp = ContextSNMP->new;
is snmp_vrf_context($snmp, $device, { contexts => [{ v2c => '%s@red' }] }), 1,
'a context with no kind and no instance name is switched to';
is_deeply $snmp->{updates}, [{ Community => 'public@red' }], 'with its template';
is snmp_vrf_context($snmp, $device, { name => 'blue', contexts => [{ kind => 'vrf' }] }), undef,
'no template for the version, no switch, undef as for a refused suffix';
is scalar @{ $snmp->{updates} }, 1, 'nothing more sent';
};
{
package NoVrfPlatform;
sub new { bless {}, shift }
}
subtest 'ssh_session_wrappers__platform_without_by_vrf__no_call_false' => sub {
my $session = MySession->new(platform => NoVrfPlatform->new, host => '192.0.2.1', ssh => 'SSH', auth => {});
ok !$session->arpnip_by_vrf, 'arpnip_by_vrf answers false';
ok !$session->subnets_by_vrf, 'subnets_by_vrf answers false';
};
subtest 'label_rows_by_domain__row_without_vrf__reads_default_when_multi_domain' => sub {
is_deeply label_rows_by_domain([ { ip => '192.0.2.9' } ], 1), [ { ip => '192.0.2.9', vrf => 'default' } ],
'absent vrf is the default table';
is_deeply label_rows_by_domain([ { ip => '192.0.2.9', vrf => 'red' } ], 0), [ { ip => '192.0.2.9' } ],
'single domain drops the vrf';
};
subtest 'probe_connect_info__connect_info_pass_and_no_attributes__ignore_version_added' => sub {
my $probe = \&App::Netdisco::Util::PostgresFloor::_probe_connect_info;
is_deeply [ $probe->({ connect_info => [ 'dbi:Pg:dbname=nd', 'nd', 'old' ], pass => 'new' }) ],
[ 'dbi:Pg:dbname=nd', 'nd', 'new', { ignore_version => 1 } ], 'connect_info, pass wins, attributes added';
is_deeply [ $probe->({ connect_info => [ 'dbi:Pg:dbname=nd', 'nd', 'pw', { AutoCommit => 1 } ] }) ],
[ 'dbi:Pg:dbname=nd', 'nd', 'pw', { AutoCommit => 1, ignore_version => 1 } ], 'trailing attributes kept';
is_deeply [ $probe->({ dsn => 'dbi:Pg:dbname=nd', user => 'nd', password => 'pw' }) ],
[ 'dbi:Pg:dbname=nd', 'nd', 'pw', undef, { ignore_version => 1 } ], 'dsn form with no options';
};
subtest 'deploy_schema__no_label_and_alias__netdisco_label_alias_options_ignore_version' => sub {
my @calls;
local *DBICx::Sugar::config = sub { };
local *DBICx::Sugar::schema = sub { push @calls, [ @_ ]; return 'schema' };
my $plugins = { DBIC => {
netdisco => { dsn => 'dbi:Pg:dbname=nd', options => { AutoCommit => 1 } },
other => { alias => 'netdisco' },
} };
with_settings(sub {
is App::Netdisco::DB::Deploy::deploy_schema(), 'schema', 'returns the schema';
App::Netdisco::DB::Deploy::deploy_schema('other');
App::Netdisco::DB::Deploy::deploy_schema('missing');
}, plugins => $plugins);
is $calls[0][0], 'netdisco', 'no label means netdisco';
is_deeply $calls[0][1]{options}, { AutoCommit => 1, ignore_version => 1 }, 'options copied with ignore_version';
is $calls[1][1]{dsn}, 'dbi:Pg:dbname=nd', 'an alias takes its target settings';
is_deeply $calls[2][1], { options => { ignore_version => 1 } }, 'an unknown label still skips the check';
is_deeply $plugins->{DBIC}{netdisco}{options}, { AutoCommit => 1 }, 'the shared settings are not changed';
};
( run in 0.566 second using v1.01-cache-2.11-cpan-145b269f2f3 )