App-Netdisco

 view release on metacpan or  search on metacpan

xt/192-discover-vrf-collection.t  view on Meta::CPAN

  local $stored_vrfs = stored_like_fixture();
  $stored_vrfs->[1]{rt_import} = ['64512:99'];
  run_with_vrfs();
  is scalar(grep { $_ eq 'recompute' } @events), 1, 'recomputed';
};

subtest 'discover__isid_changed__recompute' => sub {
  local $stored_vrfs = stored_like_fixture();
  $stored_vrfs->[2]{l3_isid} = 4100;
  run_with_vrfs();
  is scalar(grep { $_ eq 'recompute' } @events), 1, 'recomputed';
};

subtest 'discover__vrf_removed__recompute' => sub {
  local $stored_vrfs = stored_like_fixture(extra => 1);
  run_with_vrfs();
  ok !(grep { $_->{vrf} eq 'green' } @{ (removals_of('DeviceVrf'))[0][2] }), 'green removed';
  is scalar(grep { $_ eq 'recompute' } @events), 1, 'recomputed';
};

subtest 'discover__vrf_added__recompute' => sub {
  local $stored_vrfs = stored_like_fixture();
  splice @$stored_vrfs, 2, 1;
  run_with_vrfs();
  is scalar(grep { $_ eq 'recompute' } @events), 1, 'recomputed';
};

subtest 'discover__switch_dies__default_context_restored' => sub {
  my ($job) = run_with_vrfs(die_on_update_to => 'blue');
  is $job->status, 'error', 'the job reports the failure';
  is_deeply $last_snmp->{contexts_asked}, [ 'blue', '' ], 'the guard restored after the failed switch';
  is $last_snmp->{context}, '', 'session back in the default context';
};

# SNMP::Info's error() returns whatever error is pending since it was last
# read, so a VRF read that set none must not log one left by an earlier call
{ package ErrorPendingSnmp;
  sub new { my ($class, $pending) = @_; return bless { error => $pending }, $class }
  sub error { my $self = shift; my $error = $self->{error}; $self->{error} = undef; return $error }
  sub vrf_read_complete { return 0 } }
{ package ErrorLogDevice; sub ip { '192.0.2.1' } }

sub incomplete_read_log {
  my ($pending, $read_sets) = @_;
  my $snmp = ErrorPendingSnmp->new($pending);
  my @logged;
  local *App::Netdisco::Util::VRF::debug = sub { push @logged, @_ };
  App::Netdisco::Util::VRF::each_vrf_instance(bless({}, 'ErrorLogDevice'), $snmp,
    { default => { contexts => [] } },
    sub { $snmp->{error} = $read_sets if defined $read_sets; return {} });
  return join "\n", grep { /read incomplete/ } @logged;
}

subtest 'each_vrf_instance__incomplete_read_with_stale_earlier_error__log_omits_stale_error' => sub {
  my $log = incomplete_read_log('_global(serial1) NOSUCHOBJECT', undef);
  like $log, qr/VRF default read incomplete, keeping its rows$/, 'logged with no error appended';
  unlike $log, qr/NOSUCHOBJECT/, 'the earlier call\'s error is not reported';
};

subtest 'each_vrf_instance__incomplete_read_that_set_an_error__log_includes_it' => sub {
  my $log = incomplete_read_log('_global(serial1) NOSUCHOBJECT', 'Timeout on vrf walk');
  like $log, qr/VRF default read incomplete, keeping its rows: Timeout on vrf walk$/, 'the read\'s own error is appended';
};

# the address and subnet filters of the VRF path, as the flat path applies
# them; the /32 alias subnet is stored as master's flat path stores one
subtest 'discover__vrf_path_unusable_and_ignored_entries__left_out' => sub {
  my %fixture = vrf_fixture();
  $fixture{instances}{red} = instance('red', rd => undef);
  delete @{ $fixture{instances}{red} }{qw/rt_import rt_export/};
  $fixture{context_interfaces} = { red => undef, blue => { 8 => 'Vlan8' } };
  $fixture{vrf}{red}{addresses} = {
    address(7, '10.40.0.1', 24),
    address(7, '10.42.0.1', 32),
    address(7, '0.0.0.0', 8),
    address(7, '0:0:0:0:0:0:0:0', 64),
    address(7, '127.0.0.5', 8),
    address(7, 'fe80:0000:0000:0000:0000:0000:0000:0001', 64),
    'x.bogus'  => { ifindex => 7, ip => '10.0.0.256', prefix_len => 24, family => 'ipv4' },
    'x.noip'   => { ifindex => 7, ip => undef, family => 'ipv4' },
    'x.noport' => { ip => '10.43.0.1', family => 'ipv4' },
  };
  $fixture{vrf}{red}{subnets} = {
    subnet('10.40.0.0', 24, 7),
    subnet('127.0.0.0', 8, 7),
    subnet('fe80:0000:0000:0000:0000:0000:0000:0000', 64, 7),
    'x.nolen'  => { prefix => '10.44.0.0' },
    'x.noprefix' => { len => 24 },
    'x.bogus'  => { prefix => 'zz::zz::zz', len => 24 },
  };
  my ($job) = with_settings(sub { run_discover(%fixture, interfaces => undef) },
    store_vrfs => 1, ignore_private_nets => 0);
  is $job->status, 'done', 'discover finished';
  my @red = grep { $_->{vrf} eq 'red' } upserted('DeviceIp');
  is_deeply [ sort map { $_->{alias} } @red ], [ '10.40.0.1', '10.42.0.1', '10.43.0.1' ],
    'unspecified, loopback, link-local and unparsable addresses left out';
  my ($host) = grep { $_->{alias} eq '10.42.0.1' } @red;
  is $host->{subnet}, '10.42.0.1/32', 'a /32 address keeps its /32 subnet, as on master';
  my ($bare) = grep { $_->{alias} eq '10.43.0.1' } @red;
  is $bare->{subnet}, undef, 'no prefix length, no subnet';
  is $bare->{port}, undef, 'no ifindex, no port';
  my ($polled) = grep { $_->{alias} eq '192.0.2.1' } upserted('DeviceIp');
  is $polled->{port}, undef, 'no default interfaces, no default port';
  is_deeply [ nets_in('red') ], [ '10.40.0.0/24' ], 'only the usable red subnet stored';
  my ($red_vrf) = grep { $_->{vrf} eq 'red' } upserted('DeviceVrf');
  is_deeply [ @$red_vrf{qw/rd rt_import rt_export/} ], [ undef, [], [] ], 'absent RD and RT lists stored empty';

  %{ $fixture{vrf}{red}{addresses} } = (%{ $fixture{vrf}{red}{addresses} }, address(7, '203.0.113.1', 24));
  ($job) = with_settings(sub { run_discover(%fixture) }, store_vrfs => 1, ignore_private_nets => 1);
  is $job->status, 'done', 'discover with ignore_private_nets finished';
  is_deeply [ map { $_->{alias} } grep { $_->{vrf} eq 'red' } upserted('DeviceIp') ], [ '203.0.113.1' ],
    'ignore_private_nets drops the RFC 1918 aliases, keeps the public one';
  is_deeply [ nets_in('red') ], [], 'and the RFC 1918 subnet';
};

done_testing;



( run in 0.902 second using v1.01-cache-2.11-cpan-234fa220139 )