lib/Ubic/Service/Skeleton.pm

filter: Statementstmt 90.6% 68 / 75 Branchbran 78.1% 25 / 32 Conditioncond 71.4% 10 / 14 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 81.2% 13 / 16 Podpod 100% 7 / 7 totaltotal 83.6% 128 / 153 SCAR 31.6
CC
22
Cov
85%
_do_start
10.2
_do_stop
9.3
start
4.0
CRAP 23.6
« lib/Ubic/Watchdog.pm ↑ Summary lib/Ubic/Daemon/OS/POSIX.pm »
1 package Ubic::Service::Skeleton;
2 150 $Ubic::Service::Skeleton::VERSION = '1.59';
3 150 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 150 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: skeleton of any service with common start/stop logic
7  
8 150 ▶use Ubic::Result qw(result);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 150 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 150 ▶use Time::HiRes qw(sleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 150 ▶use Ubic::Service::Utils qw(wait_for_status);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 150 ▶use parent qw(Ubic::Service);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub status {
16 263 ▶ my ($self) = @_;
Subroutine status: called CC=2 SCAR=6.9
Pod documented
17 263 my $result = $self->status_impl;
18 259 ▶ $result ||= 'unknown';
Condition100%2/2
$result ||= "unknown"
l!l
2527
MC/DC100%1/1
$result ||= "unknown"
$result
Truth table100%2/2
A
A= $result
Aresult
11
00
19 259 $result = result($result);
20 259 return $result;
21 }
22  
23 sub start {
24 61 ▶ my ($self) = @_;
Subroutine start: called CC=4 SCAR=14.0
Pod documented
25  
26 61 my $status = $self->status;
27 61 ▶ if ($status->status eq 'running') {
Branch: 3 branches
BranchTrueFalseTotal
if ($status->status eq 'running') { } 2 59 61
elsif ($status->status eq 'not running') { } 57 2 59
elsif ($status->status eq 'broken') { } 2 0 2
28 2 return 'already running'; # TODO - update $status field instead?
29 }
30 elsif ($status->status eq 'not running') {
31 57 return $self->_do_start;
32 }
33 elsif ($status->status eq 'broken') {
34 # checks inside _do_start and _do_stop guarantee correct status
35 2 $self->_do_stop;
36 1 return $self->_do_start;
37 }
38 else {
39 0 die result('unknown', "wrong status '$status'");
40 }
41 }
42  
43 sub stop {
44 68 ▶ my ($self) = @_;
Subroutine stop: called CC=2 SCAR=6.9
Pod documented
45  
46 68 my $status = $self->status;
47 64 ▶ if ($status->status eq 'not running') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status->status eq "not running") 23 41 64
48 23 return 'not running';
49 }
50  
51 41 return $self->_do_stop;
52 }
53  
54 sub status_impl {
55 0 ▶ die 'not implemented';
Subroutine status_impl: not called CC=1 SCAR=6.9
Pod documented
56 }
57  
58 sub start_impl {
59 0 ▶ die 'not implemented';
Subroutine start_impl: not called CC=1 SCAR=6.9
Pod documented
60 }
61  
62 sub stop_impl {
63 0 ▶ die 'not implemented';
Subroutine stop_impl: not called CC=1 SCAR=6.9
Pod documented
64 }
65  
66 sub timeout_options {
67 66 ▶ return {};
Subroutine timeout_options: called CC=1 SCAR=0.0
Pod documented
68 }
69  
70  
71 ##### internal methods ######
72  
73 sub _do_start {
74 58 ▶ my ($self) = @_;
Subroutine _do_start: called CC=9 SCAR=23.2
75  
76 58 my $status;
77  
78 58 my $start_result = $self->start_impl;
79 41 ▶ if (blessed($start_result) and $start_result->isa('Ubic::Result::Class')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($start_result) and $start_result->isa("Ubic::Result::Class")) 2 39 41
Condition66%2/3
builtin::blessed($start_result) and $start_result->isa("Ubic::Result::Class")
!ll&&!rl&&r
3902
MC/DC50%1/2
builtin::blessed($start_result) and $start_result->isa("Ubic::Result::Class")
builtin::blessed($start_result) $start_result->isa("Ubic::Result::Class")
Truth table66%2/3
A and B
A= builtin::blessed($start_result)B= $start_result->isa("Ubic::Result::Class")
ABresult
0X0
100
111
80 2 $status = $start_result;
81 }
82  
83 41 ▶ if (not $status or $status->type eq 'starting') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $status or $status->type eq "starting") 39 2 41
Condition66%2/3
not $status or $status->type eq "starting"
l!l&&r!l&&!r
3902
MC/DC50%1/2
not $status or $status->type eq "starting"
not $status $status->type eq "starting"
Truth table66%2/3
not(A) or B
A= not $statusB= $status->type eq "starting"
ABresult
1X1
011
000
84 $status = wait_for_status({
85 service => $self,
86 expect_status => ['running', 'not running'],
87 39 ▶ %{ $self->timeout_options->{start} || {} },
Branch: 1 branch
BranchTrueFalseTotal
$self->timeout_options->{"start"} or {} 39 0 39
88 });
89 39 ▶ if ($status->status eq 'running') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status->status eq "running") 39 0 39
90 39 $status->type('started'); # fake status to report correct action (hopefully)
91 }
92 }
93  
94 41 ▶ if (not $status) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status) 0 41 41
95 0 die result('unknown', 'no result');
96 }
97 41 ▶ if ($status->status eq 'running') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status->status eq 'running') { } 41 0 41
98 41 return $status;
99 }
100 else {
101 0 die result($status, 'start failed');
102 }
103 }
104  
105 sub _do_stop {
106 43 ▶ my ($self) = @_;
Subroutine _do_stop: called CC=9 SCAR=22.3
107 43 my $status;
108  
109 43 my $stop_result = $self->stop_impl;
110 42 ▶ if (blessed($stop_result) and $stop_result->isa('Ubic::Result::Class')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($stop_result) and $stop_result->isa("Ubic::Result::Class")) 2 40 42
Condition66%2/3
builtin::blessed($stop_result) and $stop_result->isa("Ubic::Result::Class")
!ll&&!rl&&r
4002
MC/DC50%1/2
builtin::blessed($stop_result) and $stop_result->isa("Ubic::Result::Class")
builtin::blessed($stop_result) $stop_result->isa("Ubic::Result::Class")
Truth table66%2/3
A and B
A= builtin::blessed($stop_result)B= $stop_result->isa("Ubic::Result::Class")
ABresult
0X0
100
111
111 2 $status = $stop_result;
112 }
113  
114 42 ▶ if (not $status or $status->type eq 'stopping') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $status or $status->type eq "stopping") 40 2 42
Condition66%2/3
not $status or $status->type eq "stopping"
l!l&&r!l&&!r
4002
MC/DC50%1/2
not $status or $status->type eq "stopping"
not $status $status->type eq "stopping"
Truth table66%2/3
not(A) or B
A= not $statusB= $status->type eq "stopping"
ABresult
1X1
011
000
115 $status = wait_for_status({
116 service => $self,
117 expect_status => ['not running'],
118 40 ▶ %{ $self->timeout_options->{stop} || {} },
Branch: 1 branch
BranchTrueFalseTotal
$self->timeout_options->{"stop"} or {} 40 0 40
119 });
120 40 ▶ if ($status->status eq 'not running') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status->status eq "not running") 38 2 40
121 38 $status->type('stopped'); # fake status to report correct action (hopefully)
122 }
123 }
124  
125 42 ▶ if (not $status) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status) 0 42 42
126 0 die result('unknown', 'no result');
127 }
128 42 ▶ if ($status->status eq 'not running') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status->status eq 'not running') { } 40 2 42
129 40 return $status;
130 }
131 else {
132 2 die result($status, 'stop failed');
133 }
134 }
135  
136 150 1;
137  
138 __END__
« lib/Ubic/Watchdog.pm ↑ Summary lib/Ubic/Daemon/OS/POSIX.pm »