package NACL::Plugin::TestLifeCycle::ASUPTrigger; use v5.14; use strict; use warnings FATAL => 'all'; use Carp qw/confess/; use NATE::Log qw/log_global/; use NACL::ServiceAPI::ASUPTrigger qw/asuptriggerapi_global/; use NATE::ServiceAPI::KeyVal qw/keyval_global/; use NACL::Plugin::TestLifeCycle::ASUPTrigger::Deployment qw/is_enabled/; use parent qw/NATE::Plugin::TestLifeCycle/; # @Enabled contains two flags, a hard and soft disable. THe first flag # disable the plugin entirely. The second flag only disables the triggering # ASUPs from the ASUPTrigger service # Those flags are used for the progressive roll-out of the plugin my @Enabled; my $Normal_Test_Started = 0; my $KeyVal; sub pre_job_started { _warm_keyval(); } sub pre_test_started { _warm_keyval(); } sub co_job_started { _warm_keyval(); } # The main point of before_normal_test_start is to start the # Service and the Worker before any normal test start. This is to # prevent starvation issues seen on small SCS clients # before_normal_test_start and normal_test_started # are almost copies of each other before we don't know what # version of ntest is going to call us. Older versions will only # call normal_test_started (aka the "mixed stack" issue) sub before_normal_test_start { my ($self,%args) = @_; my $test = $args{test} // confess("Missing test argument"); my $computed_is_enabled = 0; if (!@Enabled){ @Enabled = is_enabled(); $computed_is_enabled = 1; # Report globally that the plugin is on if ($Enabled[0]){ $KeyVal //= keyval_global(); if ($Enabled[0]){ $KeyVal->hset( hash_name => __PACKAGE__, key => 'plugin_enabled', val => 1, ); } if ($Enabled[0] && $Enabled[1]){ $KeyVal->hset( hash_name => __PACKAGE__, key => 'asup_enabled', val => 1, ); } } } return if (!$Enabled[0]); if (!$Enabled[1]){ asuptriggerapi_global()->disable_asup_triggers; } $Normal_Test_Started ++; asuptriggerapi_global()->before_normal_test_start( test_fqname => $test->fqname, test_name => $test->name, ); if ($computed_is_enabled){ # Figuring how to talk to graylog isnt simple and is already done in # the service. For now, only report the stats if the service is enabled asuptriggerapi_global()->report_comment_to_syslog( 'Deployment statistics', NACL::Plugin::TestLifeCycle::ASUPTrigger::Deployment::get_statistics(), ); } } sub normal_test_started { my ($self,%args) = @_; my $testobj = delete $args{test}; my $computed_is_enabled = 0; if (!@Enabled){ @Enabled = is_enabled(); $computed_is_enabled = 1; # Report globally that the plugin is on if ($Enabled[0]){ $KeyVal //= keyval_global(); if ($Enabled[0]){ $KeyVal->hset( hash_name => __PACKAGE__, key => 'plugin_enabled', val => 1, ); } if ($Enabled[0] && $Enabled[1]){ $KeyVal->hset( hash_name => __PACKAGE__, key => 'asup_enabled', val => 1, ); } } } return if (!$Enabled[0]); if (!$Enabled[1]){ asuptriggerapi_global()->disable_asup_triggers; } # See if filer information was passed in as test params instead of globals if ($testobj){ my %filer_values; my @filer_params = qw/NODES FILERS FILER FILERA FILERB FILER_SRC FILER_DST/; my $testobj_params = $testobj->params; for my $p ( @filer_params ){ if ($testobj_params->{$p}){ my $filer_param_value = $testobj_params->{$p}; log_global()->comment("Found a value for param '$p' of test with runid '$args{subtest_runid}': '$filer_param_value'"); my @splits = grep { $_ } map { s/\s+//g; $_ } split(/:|,/, $filer_param_value); @filer_values{@splits} = (1) x scalar(@splits); } } $args{nodes_as_params} = [keys %filer_values] if ( scalar keys %filer_values ); } $Normal_Test_Started ++; asuptriggerapi_global()->normal_test_started(%args); if ($computed_is_enabled){ # Figuring how to talk to graylog isnt simple and is already done in # the service. For now, only report the stats if the service is enabled asuptriggerapi_global()->report_comment_to_syslog( 'Deployment statistics', NACL::Plugin::TestLifeCycle::ASUPTrigger::Deployment::get_statistics(), ); } } sub normal_test_ended { my ($self,%args) = @_; @Enabled = is_enabled() if (!@Enabled); return if (!$Enabled[0]); # Rob, in his infinite wisdom, is also passing us the worst result object # but the service message passing infrastructure doesn't support encoding objects # for whatever reason. Only pass what we need asuptriggerapi_global()->normal_test_ended( subtest_id => $args{subtest_id}, subtest_runid => $args{subtest_runid}, ); } sub testcase_started { my $self = shift; @Enabled = is_enabled() if (!@Enabled); return if (!$Enabled[0]); asuptriggerapi_global()->testcase_started(@_); } sub testcase_ended { my $self = shift; @Enabled = is_enabled() if (!@Enabled); return if (!$Enabled[0]); asuptriggerapi_global()->testcase_ended(@_); } sub post_job_started { my $self = shift; # Don't bother doing anything if we didnt see a single NORMAL TEST if (!$Normal_Test_Started){ log_global()->comment("Skipping hook 'post_job_started' since hook 'normal_test_started' was never processed during this job"); return; } @Enabled = is_enabled() if (!@Enabled); return if (!$Enabled[0]); asuptriggerapi_global()->post_job_started(); } sub all_tests_ended { my $self = shift; # Don't bother doing anything if we didnt see a single NORMAL TEST if (!$Normal_Test_Started){ log_global()->comment("Skipping hook 'all_tests_ended' since hook 'normal_test_started' was never processed during this job"); return; } @Enabled = is_enabled() if (!@Enabled); return if (!$Enabled[0]); asuptriggerapi_global()->all_tests_ended(); } sub _warm_keyval { return if ( $KeyVal ); log_global()->comment("Ensuring KeyVal service is started"); $KeyVal = keyval_global(); $KeyVal->hset( hash_name => __PACKAGE__, key => 'keyval_warmup', val => 1, ); return; } 1;