1#!/usr/bin/env perl 2 3use strict; use warnings; 4 5BEGIN { $ENV{DANCER_ENVDIR} = '/dev/null'; } 6 7use Test::More 0.88; 8use Test::File::ShareDir::Dist { 'App-Netdisco' => 'share/' }; 9 10use lib 'xt/lib'; 11 12use App::Netdisco; 13use App::Netdisco::DB; # fake device row 14use App::Netdisco::Backend::Job; 15 16use Try::Tiny; 17use Dancer qw/:moose :script !pass/; 18 19# configure logging to force console output 20my $CONFIG = config(); 21$CONFIG->{logger} = 'console'; 22$CONFIG->{log} = 'error'; 23Dancer::Logger->init('console', $CONFIG); 24 25{ 26 package MyWorker; 27 use Moo; 28 with 'App::Netdisco::Worker::Runner'; 29} 30 31# clear user device_auth and set our own 32config->{'device_auth'} = [{driver => 'snmp'}, {driver => 'cli'}]; 33 34# TESTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 35 36my $j1 = do_job('TestOne'); 37is($j1->status, 'done', 'status is done'); 38is($j1->log, 'OK: SNMP driver is successful.', 39 'workers are run in decreasing priority until done'); 40 41my $j2 = do_job('TestTwo'); 42is($j2->status, 'done', 'status is done'); 43is($j2->log, 'OK: CLI driver is successful.', 44 'lower priority driver not run if higher is successful'); 45 46config->{'device_auth'} = []; 47 48my $j3 = do_job('TestOne'); 49is($j3->status, 'defer', 'status is defer'); 50is($j3->log, 'deferred job with no device creds', 51 'no matching config for workers'); 52 53config->{'device_auth'} = [{driver => 'snmp'}]; 54 55my $j4 = do_job('TestThree'); 56is($j4->status, 'done', 'status is done'); 57is($j4->log, 'OK: SNMP driver is successful.', 58 'respect user config filtering the driver'); 59 60config->{'device_auth'} = [ 61 {driver => 'snmp', action => 'testthree'}, 62 {driver => 'cli', action => 'foo'}, 63]; 64 65my $j5 = do_job('TestThree'); 66is($j5->status, 'done', 'status is done'); 67is($j5->log, 'OK: SNMP driver is successful.', 68 'respect user config filtering the action'); 69 70config->{'device_auth'} = [ 71 {driver => 'snmp', action => 'testthree::_base_'}, 72 {driver => 'cli', action => 'testthree::foo'}, 73]; 74 75my $j6 = do_job('TestThree'); 76is($j6->status, 'done', 'status is done'); 77is($j6->log, 'OK: SNMP driver is successful.', 78 'respect user config filtering the namespace'); 79 80config->{'device_auth'} = [{driver => 'snmp'}]; 81 82my $j7 = do_job('TestFour'); 83is($j7->status, 'done', 'status is done'); 84is($j7->log, 'OK: custom driver is successful.', 85 'override an action'); 86 87config->{'device_auth'} = [{driver => 'snmp'}]; 88 89my $j8 = do_job('TestFive'); 90is($j8->status, 'done', 'status is done'); 91is((scalar @{$j8->_statuslist}), 2, 'two workers ran'); 92is($j8->_last_priority, 100, 'priority is for snmp'); 93is($j8->log, 'OK: SNMP driver is successful.', 94 'add to an action'); 95 96done_testing; 97 98# TESTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 99 100sub do_job { 101 my $pkg = shift; 102 103 # include local plugins 104 config->{'extra_worker_plugins'} = ["X::${pkg}"]; 105 106 my $job = App::Netdisco::Backend::Job->new({ 107 job => 0, 108 device => App::Netdisco::DB->resultset('Device')->new_result({ip => '192.0.2.1'}), 109 action => lc($pkg), 110 }); 111 112 try { 113 #info sprintf 'test: started at %s', scalar localtime; 114 MyWorker->new()->run($job); 115 #info sprintf 'test: %s: %s', $job->status, $job->log; 116 } 117 catch { 118 $job->status('error'); 119 $job->log("error running job: $_"); 120 }; 121 122 return $job; 123} 124