| 1 | package Test::BDD::Cucumber::Harness::Nagios; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 1 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | # ABSTRACT: Nagios output for Test::BDD::Cucumber | ||||||||||||||
| 6 | 1 | our $VERSION = '1.002'; # VERSION | |||||||||||||
| 7 | |||||||||||||||
| 8 | 1 | ▶ | use Time::HiRes qw ( time ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 1 | ▶ | use Time::Piece; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 1 | ▶ | use Getopt::Long; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 1 | extends 'Test::BDD::Cucumber::Harness::Data'; | |||||||||||||
| 14 | |||||||||||||||
| 15 | 1 | ▶ | use Test::BDD::Cucumber::Harness::Nagios::Result; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | |||||||||||||||
| 17 | 0 | ▶ | has all_scenarios => ( is => 'ro', isa => 'ArrayRef', default => sub { [] } ); | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||
| 18 | 1 | has current_feature => ( is => 'rw', isa => 'HashRef' ); | |||||||||||||
| 19 | 1 | has current_scenario => ( is => 'rw', isa => 'HashRef' ); | |||||||||||||
| 20 | 1 | has step_start_at => ( is => 'rw', isa => 'Num' ); | |||||||||||||
| 21 | |||||||||||||||
| 22 | 0 | ▶ | sub scenario { | ||||||||||||
|
Subroutine
scenario: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 23 | 0 | my ( $self, $scenario, $dataset ) = @_; | |||||||||||||
| 24 | 0 | $self->current_scenario( $self->format_scenario($scenario) ); | |||||||||||||
| 25 | 0 | push @{ $self->all_scenarios }, $self->current_scenario; | |||||||||||||
| 26 | } | ||||||||||||||
| 27 | |||||||||||||||
| 28 | 0 | ▶ | sub step { | ||||||||||||
|
Subroutine
step: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 29 | 0 | my ( $self, $context ) = @_; | |||||||||||||
| 30 | 0 | $self->step_start_at( time() ); | |||||||||||||
| 31 | } | ||||||||||||||
| 32 | |||||||||||||||
| 33 | 0 | ▶ | sub step_done { | ||||||||||||
|
Subroutine
step_done: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 34 | 0 | my ( $self, $context, $result ) = @_; | |||||||||||||
| 35 | 0 | my $duration = time() - $self->step_start_at; | |||||||||||||
| 36 | 0 | my $step_data = $self->format_step( $context, $result, $duration ); | |||||||||||||
| 37 | 0 | my $status = $step_data->{'result'}->{'status'}; | |||||||||||||
| 38 | |||||||||||||||
| 39 | 0 | push @{ $self->current_scenario->{steps} }, $step_data; | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | |||||||||||||||
| 42 | |||||||||||||||
| 43 | 0 | ▶ | sub get_keyword { | ||||||||||||
|
Subroutine
get_keyword: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 44 | 0 | my ( $self, $line_ref ) = @_; | |||||||||||||
| 45 | 0 | my ($keyword) = $line_ref->content =~ /^(\w+)/; | |||||||||||||
| 46 | 0 | return $keyword; | |||||||||||||
| 47 | } | ||||||||||||||
| 48 | |||||||||||||||
| 49 | 0 | ▶ | sub format_tags { | ||||||||||||
|
Subroutine
format_tags: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 50 | 0 | my ( $self, $tags_ref ) = @_; | |||||||||||||
| 51 | 0 | return [ map { { name => '@' . $_ } } @$tags_ref ]; | |||||||||||||
| 52 | } | ||||||||||||||
| 53 | |||||||||||||||
| 54 | 0 | ▶ | sub format_description { | ||||||||||||
|
Subroutine
format_description: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 55 | 0 | my ( $self, $feature ) = @_; | |||||||||||||
| 56 | 0 | return join "\n", map { $_->content } @{ $feature->satisfaction }; | |||||||||||||
| 57 | } | ||||||||||||||
| 58 | |||||||||||||||
| 59 | 0 | ▶ | sub format_scenario { | ||||||||||||
|
Subroutine
format_scenario: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 60 | 0 | my ( $self, $scenario, $dataset ) = @_; | |||||||||||||
| 61 | return { | ||||||||||||||
| 62 | 0 | ▶ | keyword => $self->get_keyword( $scenario->line ), | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 63 | id => "scenario-" . int($scenario), | ||||||||||||||
| 64 | name => $scenario->name, | ||||||||||||||
| 65 | line => $scenario->line->number, | ||||||||||||||
| 66 | tags => $self->format_tags( $scenario->tags ), | ||||||||||||||
| 67 | type => $scenario->background ? 'background' : 'scenario', | ||||||||||||||
| 68 | steps => [], | ||||||||||||||
| 69 | }; | ||||||||||||||
| 70 | } | ||||||||||||||
| 71 | |||||||||||||||
| 72 | 0 | ▶ | sub format_step { | ||||||||||||
|
Subroutine
format_step: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 73 | 0 | my ( $self, $step_context, $result, $duration ) = @_; | |||||||||||||
| 74 | 0 | my $step = $step_context->step; | |||||||||||||
| 75 | 0 | my $rand = int( rand() * 10000000); | |||||||||||||
| 76 | return { | ||||||||||||||
| 77 | 0 | ▶ | keyword => $step ? $step->verb_original : $step_context->verb, | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 78 | id => "step-".$rand, | ||||||||||||||
| 79 | name => $step_context->text, | ||||||||||||||
| 80 | background => $step_context->background, | ||||||||||||||
| 81 | line => $step ? $step->line->number : 0, | ||||||||||||||
| 82 | result => $self->format_result( $result, $duration ) | ||||||||||||||
| 83 | }; | ||||||||||||||
| 84 | } | ||||||||||||||
| 85 | |||||||||||||||
| 86 | has '_output_status' => ( is => 'ro', isa => 'HashRef', lazy => 1, | ||||||||||||||
| 87 | 0 | ▶ | default => sub { { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 88 | 0 | passing => 'passed', | |||||||||||||
| 89 | failing => 'failed', | ||||||||||||||
| 90 | pending => 'pending', | ||||||||||||||
| 91 | undefined => 'skipped', | ||||||||||||||
| 92 | } }, | ||||||||||||||
| 93 | 1 | ); | |||||||||||||
| 94 | |||||||||||||||
| 95 | 0 | ▶ | sub format_result { | ||||||||||||
|
Subroutine
format_result: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 96 | 0 | my ( $self, $result, $duration ) = @_; | |||||||||||||
| 97 | 0 | my $ret; | |||||||||||||
| 98 | |||||||||||||||
| 99 | 0 | ▶ | if( $result ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 100 | $ret = { | ||||||||||||||
| 101 | 0 | ▶ | status => $self->_output_status->{ $result->result }, | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 102 | error_message => $result->output, | ||||||||||||||
| 103 | defined $duration | ||||||||||||||
| 104 | ? ( duration => int( $duration * 1_000_000_000 ) ) | ||||||||||||||
| 105 | : (), # nanoseconds | ||||||||||||||
| 106 | }; | ||||||||||||||
| 107 | } else { | ||||||||||||||
| 108 | 0 | $ret = { status => "undefined" }; | |||||||||||||
| 109 | } | ||||||||||||||
| 110 | |||||||||||||||
| 111 | 0 | return $ret; | |||||||||||||
| 112 | } | ||||||||||||||
| 113 | |||||||||||||||
| 114 | 0 | ▶ | sub generate_steps_output { | ||||||||||||
|
Subroutine
generate_steps_output: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 115 | 0 | my ( $self, $scenario ) = @_; | |||||||||||||
| 116 | 0 | my $message = ''; | |||||||||||||
| 117 | 0 | foreach my $step ( @{$scenario->{'steps'}} ) { | |||||||||||||
| 118 | # append error message, but limit to 10 lines | ||||||||||||||
| 119 | 0 | ▶ | if( $step->{'result'}->{'status'} eq 'failed' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 120 | $message .= 'line '.$step->{'line'}.': ' | ||||||||||||||
| 121 | .$step->{'keyword'}.' '.$step->{'name'}.': ' | ||||||||||||||
| 122 | 0 | .$step->{'result'}->{'status'}."\n"; | |||||||||||||
| 123 | 0 | my $err = $step->{'result'}->{'error_message'}; | |||||||||||||
| 124 | 0 | $err =~ s/^/ /mg; | |||||||||||||
| 125 | 0 | $message .= $err; | |||||||||||||
| 126 | } | ||||||||||||||
| 127 | } | ||||||||||||||
| 128 | 0 | return( $message ); | |||||||||||||
| 129 | } | ||||||||||||||
| 130 | |||||||||||||||
| 131 | 0 | ▶ | sub result { | ||||||||||||
|
Subroutine
result: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||
| 132 | 0 | my ($self) = @_; | |||||||||||||
| 133 | 0 | my $total = scalar @{$self->all_scenarios}; | |||||||||||||
| 134 | 0 | my $failed_critical = 0; | |||||||||||||
| 135 | 0 | my $failed_warn = 0; | |||||||||||||
| 136 | 0 | my $output = ''; | |||||||||||||
| 137 | 0 | my $code = 2; | |||||||||||||
| 138 | 0 | foreach my $scenario ( @{$self->all_scenarios} ) { | |||||||||||||
| 139 | 0 | ▶ | if( grep { $_->{'result'}->{'status'} ne 'passed' } @{$scenario->{'steps'}} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 140 | 0 | ▶ | if( grep { $_->{'name'} eq '@warn' } @{$scenario->{'tags'}} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 141 | 0 | $failed_warn++; | |||||||||||||
| 142 | } else { | ||||||||||||||
| 143 | 0 | $failed_critical++; | |||||||||||||
| 144 | } | ||||||||||||||
| 145 | 0 | $output .= $self->generate_steps_output( $scenario ); | |||||||||||||
| 146 | } | ||||||||||||||
| 147 | } | ||||||||||||||
| 148 | 0 | ▶ | if( $failed_critical ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 149 | 0 | $output .= "CRITICAL - $failed_critical (critical) / $failed_warn (warn) out of $total scenarios failed\n"; | |||||||||||||
| 150 | 0 | $code = 2; | |||||||||||||
| 151 | } elsif ( $failed_warn ) { | ||||||||||||||
| 152 | 0 | $output .= "WARN - :$failed_critical (critical) / $failed_warn (warn) out of $total scenarios failed\n"; | |||||||||||||
| 153 | 0 | $code = 1; | |||||||||||||
| 154 | } else { | ||||||||||||||
| 155 | 0 | $output .= "OK - all $total scenarios passed\n"; | |||||||||||||
| 156 | 0 | $code = 0; | |||||||||||||
| 157 | } | ||||||||||||||
| 158 | |||||||||||||||
| 159 | 0 | return Test::BDD::Cucumber::Harness::Nagios::Result->new( | |||||||||||||
| 160 | nagios_code => $code, | ||||||||||||||
| 161 | output => $output, | ||||||||||||||
| 162 | ); | ||||||||||||||
| 163 | } | ||||||||||||||
| 164 | |||||||||||||||
| 165 | 1 | 1; | |||||||||||||
| 166 | |||||||||||||||
| 167 | __END__ | ||||||||||||||