From 840b6b406960e70dec3798703d9a7b89e4b9dfba Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Sun, 12 Nov 2017 20:38:35 +0000 Subject: [PATCH] add tests --- lib/App/Netdisco/Backend/Job.pm | 6 +- lib/App/Netdisco/Worker/Runner.pm | 6 +- xt/30-backend-workers.t | 57 ++++++++++++++++++- .../App/NetdiscoX/Worker/Plugin/TestFive.pm | 17 ++++++ .../App/NetdiscoX/Worker/Plugin/TestFour.pm | 17 ++++++ .../App/NetdiscoX/Worker/Plugin/TestThree.pm | 17 ++++++ 6 files changed, 113 insertions(+), 7 deletions(-) create mode 100644 xt/lib/App/NetdiscoX/Worker/Plugin/TestFive.pm create mode 100644 xt/lib/App/NetdiscoX/Worker/Plugin/TestFour.pm create mode 100644 xt/lib/App/NetdiscoX/Worker/Plugin/TestThree.pm diff --git a/lib/App/Netdisco/Backend/Job.pm b/lib/App/Netdisco/Backend/Job.pm index 919471f7..7811b53f 100644 --- a/lib/App/Netdisco/Backend/Job.pm +++ b/lib/App/Netdisco/Backend/Job.pm @@ -72,8 +72,8 @@ sub finalise_status { my $max_level = Status->error()->level; foreach my $status (reverse @{ $job->_statuslist }) { - next unless $status->phase - and $status->phase =~ m/^(?:check|early|main)$/; + next if $status->phase + and $status->phase !~ m/^(?:check|early|main)$/; next if $status->phase eq 'check' and $status->level eq Status->done()->level; @@ -154,7 +154,7 @@ status cache. Phase slot of the Status will be set to the current phase. sub add_status { my ($job, $status) = @_; return unless ref $status eq 'App::Netdisco::Worker::Status'; - $status->phase( $job->_current_phase ); + $status->phase( $job->_current_phase || '' ); push @{ $job->_statuslist }, $status; debug $status->log if $status->log and (($status->phase eq 'check') diff --git a/lib/App/Netdisco/Worker/Runner.pm b/lib/App/Netdisco/Worker/Runner.pm index 61184d1e..ee2de7de 100644 --- a/lib/App/Netdisco/Worker/Runner.pm +++ b/lib/App/Netdisco/Worker/Runner.pm @@ -28,6 +28,9 @@ sub run { $job->device( get_device($job->device) ); Module::Load::load 'App::Netdisco::Worker' => $job->action; + # finalise job status when we exit + my $statusguard = guard { $job->finalise_status }; + my @newuserconf = (); my @userconf = @{ setting('device_auth') || [] }; @@ -52,9 +55,6 @@ sub run { my $configguard = guard { set(device_auth => \@userconf) }; set(device_auth => \@newuserconf); - # finalise job status when we exit - my $statusguard = guard { $job->finalise_status }; - # run check phase and if there are workers then one MUST be successful $self->run_workers('nd2_core_check'); return if not $job->check_passed; diff --git a/xt/30-backend-workers.t b/xt/30-backend-workers.t index 76bdcdef..139b5ddd 100644 --- a/xt/30-backend-workers.t +++ b/xt/30-backend-workers.t @@ -23,6 +23,9 @@ Dancer::Logger->init('console', $CONFIG); with 'App::Netdisco::Worker::Runner'; } +# clear user device_auth and set our own +config->{'device_auth'} = [{driver => 'snmp'}, {driver => 'cli'}]; + # TESTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ my $j1 = do_job('TestOne'); @@ -35,10 +38,60 @@ is($j2->status, 'done', 'status is done'); is($j2->log, 'OK: CLI driver is successful.', 'lower priority driver not run if higher is successful'); -# TESTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +config->{'device_auth'} = []; + +my $j3 = do_job('TestOne'); +is($j3->status, 'defer', 'status is defer'); +is($j3->log, 'deferred job with no device creds', + 'no matching config for workers'); + +config->{'device_auth'} = [{driver => 'snmp'}]; + +my $j4 = do_job('TestThree'); +is($j4->status, 'done', 'status is done'); +is($j4->log, 'OK: SNMP driver is successful.', + 'respect user config filtering the driver'); + +config->{'device_auth'} = [ + {driver => 'snmp', action => 'testthree'}, + {driver => 'cli', action => 'foo'}, +]; + +my $j5 = do_job('TestThree'); +is($j5->status, 'done', 'status is done'); +is($j5->log, 'OK: SNMP driver is successful.', + 'respect user config filtering the action'); + +config->{'device_auth'} = [ + {driver => 'snmp', action => 'testthree::_base_'}, + {driver => 'cli', action => 'testthree::foo'}, +]; + +my $j6 = do_job('TestThree'); +is($j6->status, 'done', 'status is done'); +is($j6->log, 'OK: SNMP driver is successful.', + 'respect user config filtering the namespace'); + +config->{'device_auth'} = [{driver => 'snmp'}]; + +my $j7 = do_job('TestFour'); +is($j7->status, 'done', 'status is done'); +is($j7->log, 'OK: custom driver is successful.', + 'override an action'); + +config->{'device_auth'} = [{driver => 'snmp'}]; + +my $j8 = do_job('TestFive'); +is($j8->status, 'done', 'status is done'); +is((scalar @{$j8->_statuslist}), 2, 'two workers ran'); +is($j8->_last_priority, 100, 'priority is for snmp'); +is($j8->log, 'OK: SNMP driver is successful.', + 'add to an action'); done_testing; +# TESTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + sub do_job { my $pkg = shift; @@ -46,10 +99,12 @@ sub do_job { config->{'extra_worker_plugins'} = ["X::${pkg}"]; # clear out any previous installed hooks + # TODO: do this in Worker.pm on every reimport ? Dancer::Factory::Hook->init( Dancer::Factory::Hook->instance() ); my $job = App::Netdisco::Backend::Job->new({ job => 0, + device => '192.0.2.1', action => lc($pkg), }); diff --git a/xt/lib/App/NetdiscoX/Worker/Plugin/TestFive.pm b/xt/lib/App/NetdiscoX/Worker/Plugin/TestFive.pm new file mode 100644 index 00000000..6c89926a --- /dev/null +++ b/xt/lib/App/NetdiscoX/Worker/Plugin/TestFive.pm @@ -0,0 +1,17 @@ +package App::NetdiscoX::Worker::Plugin::TestFive; + +use Dancer ':syntax'; +use App::Netdisco::Worker::Plugin; +use aliased 'App::Netdisco::Worker::Status'; + +# info 'test: add to an action'; + +register_worker({ phase => 'main', driver => 'snmp' }, sub { + return Status->error('NOT OK: additional worker at SNMP level.'); +}); + +register_worker({ phase => 'main', driver => 'snmp' }, sub { + return Status->done('OK: SNMP driver is successful.'); +}); + +true; diff --git a/xt/lib/App/NetdiscoX/Worker/Plugin/TestFour.pm b/xt/lib/App/NetdiscoX/Worker/Plugin/TestFour.pm new file mode 100644 index 00000000..2ea142fe --- /dev/null +++ b/xt/lib/App/NetdiscoX/Worker/Plugin/TestFour.pm @@ -0,0 +1,17 @@ +package App::NetdiscoX::Worker::Plugin::TestFour; + +use Dancer ':syntax'; +use App::Netdisco::Worker::Plugin; +use aliased 'App::Netdisco::Worker::Status'; + +# info 'test: override an action'; + +register_worker({ phase => 'main', driver => 'snmp' }, sub { + return Status->done('NOT OK: SNMP driver should NOT be run.'); +}); + +register_worker({ phase => 'main', priority => 120 }, sub { + return Status->done('OK: custom driver is successful.'); +}); + +true; diff --git a/xt/lib/App/NetdiscoX/Worker/Plugin/TestThree.pm b/xt/lib/App/NetdiscoX/Worker/Plugin/TestThree.pm new file mode 100644 index 00000000..0cda2cd0 --- /dev/null +++ b/xt/lib/App/NetdiscoX/Worker/Plugin/TestThree.pm @@ -0,0 +1,17 @@ +package App::NetdiscoX::Worker::Plugin::TestThree; + +use Dancer ':syntax'; +use App::Netdisco::Worker::Plugin; +use aliased 'App::Netdisco::Worker::Status'; + +# info 'test: respect user config filtering the driver, action and namespace'; + +register_worker({ phase => 'main', driver => 'cli' }, sub { + return Status->done('NOT OK: CLI driver should NOT be run.'); +}); + +register_worker({ phase => 'main', driver => 'snmp' }, sub { + return Status->done('OK: SNMP driver is successful.'); +}); + +true;