App-Netdisco

 view release on metacpan or  search on metacpan

xt/51-updated-device-hook.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; # fake device/deviceport rows
use App::Netdisco::Backend::Job;
use App::Netdisco::Transport::SNMP ();
use App::Netdisco::Worker::Plugin::Discover::Properties ();
use App::Netdisco::Worker::Plugin::Discover::Hooks ();
use App::Netdisco::Worker::Plugin::Discover::Properties::Tags ();
use aliased 'App::Netdisco::Worker::Status';

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

# configure logging to force console output
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';
}

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# This exercises the real Discover::Properties and Discover::Hooks worker
# code (not a reimplementation of their logic) so that the
# updated_device_hook_ignored_fields diffing is genuinely tested. Both
# plugins normally need a live SNMP connection and a live Postgres, neither
# of which is available to xt/ tests (there is no DB service in CI), so
# below we stub the few points where they touch SNMP/storage and let
# everything else run for real. Overrides are installed with
# "local *Package::sub = sub {...}" AFTER the target package is `use`d
# above, because installing them first would just get clobbered when
# Module::Load::load'ing the real plugin during the job compiles its own
# sub/import into that same glob slot.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

config->{'device_auth'} = [{driver => 'snmp'}];
config->{'enable_field_protection'} = 0; # unrelated feature, keep it out of the way
config->{'enable_incomplete_walk_protection'} = 1;
config->{'ignore_deviceports'} = [];     # ditto - avoid its device_ips() DB lookup
config->{'dns'}->{'no'} = ['0.0.0.0/0']; # skip real DNS resolution of the alias worker

package FakeSNMP;
our $AUTOLOAD;
# the port tags worker runs inside discover::properties; its clear writes nothing here
{ package NoopTagsSchema; sub resultset { return $_[0] } sub search { return $_[0] } sub update { return 0 } }

sub new { my ($class, %vals) = @_; return bless { walk_failures => [], %vals }, $class; }
sub walk_failures { $_[0]{walk_failures} }
sub walk_status { $_[0]{walk_status}{$_[1]} }
sub AUTOLOAD {
  my $self = shift;
  (my $method = $AUTOLOAD) =~ s/.*:://;
  return if $method eq 'DESTROY';
  return $self->{$method};
}

package FakePortsRS; # stands in for a DBIC ResultSet of DevicePort rows
sub new { my ($class, @rows) = @_; return bless { rows => [@rows] }, $class; }
sub reset { return $_[0] }
sub all { return @{ $_[0]->{rows} } }

package FakeLockedRow; # the device row the ports write locks
sub new { return bless {}, shift }
sub first { return $_[0] }

package FakeSchema;
sub txn_do { my ($self, $code) = @_; return $code->() }
sub resultset { my ($self, $name) = @_; return App::Netdisco::DB->resultset($name) }

package main;

# baseline "everything already matches" fixture: a discover of this device
# with this SNMP data should find zero meaningful changes
my %base_device_cols = (
  ip => '192.0.2.10', uptime => 900, description => 'desc', name => 'switch1',
  layers => 2, mac => '00:00:00:00:00:01', vendor => 'cisco', os => 'ios', os_ver => '1.0',
  model => 'cat', serial => 'SN1', chassis_id => 'chassis1', contact => '',
  location => 'Room A', num_ports => 1, snmp_class => 'SNMP::Info::Layer2',
  snmp_engineid => '', custom_fields => '{}', snmp_ver => 2,
);

my %base_snmp = (
  vtp_d_name => undef, vtp_d_mode => undef,
  snmp_ver => 2, description => 'desc', uptime => 900, name => 'switch1',
  layers => 2, mac => '00:00:00:00:00:01', vendor => 'cisco', os => 'ios', os_ver => '1.0',
  model => 'cat', serial => 'SN1', serial1 => 'chassis1', contact => '', location => 'Room A',
  ports => 1, class => 'SNMP::Info::Layer2', snmpEngineID => '', id => undef,
  load_uptime => 900,
  interfaces => { 1 => '1' },
  i_type => { 1 => '6' }, i_ignore => {}, i_description => { 1 => 'desc1' },
  i_mtu => { 1 => 1500 }, i_speed => { 1 => '1000000000' }, i_speed_admin => { 1 => '1000000000' },
  i_mac => { 1 => '00:11:22:33:44:55' }, i_up => { 1 => 'up' }, i_up_admin => { 1 => 'up' },
  i_name => { 1 => 'GigabitEthernet1/1' }, i_duplex => { 1 => 'full' }, i_duplex_admin => { 1 => 'full' },
  i_stp_state => { 1 => 'forwarding' }, i_vlan => { 1 => '10' }, i_lastchange => { 1 => 0 },
  agg_ports => {}, i_subinterfaces => {},
);

my %base_old_port = (
  ip => '192.0.2.10', port => '1', descr => 'desc1', up => 'up', up_admin => 'up',
  mac => '00:11:22:33:44:55', speed => '1000000000', speed_admin => '1000000000', mtu => 1500,
  name => 'GigabitEthernet1/1', duplex => 'full', duplex_admin => 'full', stp => 'forwarding',
  type => '6', vlan => '10', pvid => '10', has_subinterfaces => 'false', is_master => 'false',
  slave_of => undef, lastchange => 0, custom_fields => '{}',
);



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