App-Netdisco

 view release on metacpan or  search on metacpan

xt/194-arpnip-subnets-vrf.t  view on Meta::CPAN

#!/usr/bin/env perl

use strict; use warnings;
no warnings 'once'; # local *glob = sub {} overrides below are single-use by design

BEGIN { $ENV{DANCER_ENVDIR} = '/dev/null'; }

use Test::More 0.88;
use Test::File::ShareDir::Dist { 'App-Netdisco' => 'share/' };

use lib 'xt/lib';

use App::Netdisco;
use App::Netdisco::DB;
use App::Netdisco::Backend::Job;
use App::Netdisco::Transport::SNMP ();
use App::Netdisco::Worker::Plugin::Arpnip::Subnets ();

use Try::Tiny;
use Dancer qw/:moose :script !pass/;

# The SNMP arpnip subnets worker run through the Runner as xt/193 runs the
# nodes worker. SNMP is faked and the Subnet resultset records the
# upsert_subnet calls it is given.

my $CONFIG = config();
$CONFIG->{logger} = 'console';
$CONFIG->{log} = ($ENV{'DANCER_DEBUG'} ? 'debug' : 'error');
Dancer::Logger->init('console', $CONFIG);

{
  package MyWorker;
  use Moo;
  with 'App::Netdisco::Worker::Runner';
}

config->{'device_auth'} = [{driver => 'snmp'}];
config->{'enable_incomplete_walk_protection'} = 1;

package FakeSNMP;
our $AUTOLOAD;

sub new {
  my ($class, %vals) = @_;
  return bless { walk_failures => [], context => '', contexts_asked => [], %vals }, $class;
}
sub walk_failures { $_[0]{walk_failures} }
sub walk_status { undef }
sub snmp_ver { $_[0]{snmp_ver} || 2 }
sub snmp_comm { 'public' }
sub update {
  my ($self, %args) = @_;
  $self->{context} = exists $args{Context} ? $args{Context}
    : ($args{Community} =~ m/\@(.+)$/ ? $1 : '');
  push @{ $self->{contexts_asked} }, $self->{context};
  return 1;
}

sub _context_of {
  my ($self, $name) = @_;
  my $ctx = $self->{instances}{$name}{contexts}[0];
  return '' if not $ctx or ($ctx->{kind} || '') eq 'none';
  return $ctx->{id};
}
sub vrf_instances {
  my $self = shift;
  $self->{reads}{vrf_instances}++;
  return $self->{context} eq '' ? $self->{instances} : undef;
}
sub vrf_instances_complete {
  my $self = shift;
  return $self->{context} eq '' ? $self->{instances_complete} : undef;
}
sub _vrf_answer {
  my ($self, $what, $name) = @_;
  push @{ $self->{vrf_reads}{$name} }, $what;
  return undef unless $self->{context} eq $self->_context_of($name);
  return $self->{vrf}{$name}{$what};
}
sub vrf_subnets { $_[0]->_vrf_answer('subnets', $_[1]) }
sub vrf_read_complete { $_[0]->_vrf_answer('complete', $_[1]) }

sub AUTOLOAD {
  my $self = shift;
  (my $method = $AUTOLOAD) =~ s/.*:://;
  return if $method eq 'DESTROY';
  $self->{reads}{$method}++;
  return $self->{$method};
}

package FakeSubnetRs;
sub new { bless {}, shift }
sub upsert_subnet { my $self = shift; push @main::upserts, [ @_ ]; return 1 }
sub resultset { return $_[0] }

package main;

my %device_cols = (
  ip => '192.0.2.1', name => 'router1', layers => '00000100', snmp_ver => 2,
  snmp_class => 'SNMP::Info::Layer3',
);

# today's table: a public subnet, a private one, a /32, a loopback and 0.0.0.0
my %base_snmp = (
  class => 'SNMP::Info::Layer3',
  ip_netmask => {
    '198.51.100.1' => '255.255.255.0',
    '10.1.2.3'     => '255.255.255.0',
    '198.51.100.9' => '255.255.255.255',
    '127.0.0.1'    => '255.0.0.0',
    '0.0.0.0'      => '0.0.0.0',



( run in 1.254 second using v1.01-cache-2.11-cpan-145b269f2f3 )