Fake/Master.pm

filter: Statementstmt 34.3% 34 / 99 Branchbran 0.0% 0 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 57.8% 11 / 19 Podpod 100% 7 / 7 totaltotal 35.3% 52 / 147 CC 14 SCAR 27.7
CC
14
Cov
28%
run_due_tests
42.0
handle_dead_children
30.0
runloop
20.0
CRAP 16.0
↑ Summary Fake/Testmachine.pm »
1 ## no critic (RequireUseStrict)
2 package Tapper::Fake::Master;
3 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 $Tapper::Fake::Master::AUTHORITY = 'cpan:TAPPER';
5 }
6 {
7 2 $Tapper::Fake::Master::VERSION = '4.1.1';
8 }
9 # ABSTRACT: Fake Tapper::MCP::Master for testing purpose
10  
11 2 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 extends 'Tapper::Fake';
13 2 ▶ use Devel::Backtrace;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶ use POSIX ":sys_wait_h";
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶ use UNIVERSAL;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶ use Tapper::Fake::Child;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶ use Tapper::MCP::Scheduler::Controller;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶ use Tapper::Model 'model';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22  
23 1 ▶ has hosts => (is => 'rw', isa => 'ArrayRef', default => sub {[]});
Subroutine __ANON__: called CC=1 SCAR=0.0
24  
25  
26  
27 2 has dead_child => (is => 'rw', default => 0);
28  
29  
30 1 ▶ has child => (is => 'rw', isa => 'HashRef', default => sub {{}});
Subroutine __ANON__: called CC=1 SCAR=0.0
31  
32  
33 2 has scheduler => (is => 'rw', isa => 'Tapper::MCP::Scheduler::Controller');
34  
35  
36 sub BUILD
37 1 ▶{
Subroutine BUILD: called CC=1 SCAR=0.0
Pod documented
38 1 my $self = shift;
39 1 $self->scheduler(Tapper::MCP::Scheduler::Controller->new());
40 }
41  
42  
43 sub set_interrupt_handlers
44 0 ▶ {
Subroutine set_interrupt_handlers: not called CC=1 SCAR=6.9
Pod documented
45 0 my ($self) = @_;
46 0 ▶ $SIG{CHLD} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
47 0 $self->dead_child($self->dead_child + 1);
48 0 };
49  
50 # give me a stack trace when ^C
51 0 ▶ $SIG{INT} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 0 $SIG{INT}='ignore'; # not reentrant, don't handle signal twice
53 0 my $backtrace = Devel::Backtrace->new(-start=>2, -format => '%I. %s');
54  
55 0 print $backtrace;
56  
57 0 exit -1;
58 0 };
59 0 return 0;
60 }
61  
62  
63  
64 sub handle_dead_children
65 0 ▶ {
Subroutine handle_dead_children: not called CC=5 SCAR=34.0
Pod documented
66 0 my ($self) = @_;
67 0 ▶ CHILD: while ($self->dead_child) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dead_child) 0 0 0
68 0 $self->log->debug("Number of dead children is ".$self->dead_child);
69 0 my $dead_pid = waitpid(-1, WNOHANG); # don't use wait(); qx() sends a SIGCHLD and increases $self->deadchild, but wait() for the return value and thus our wait would block
70 0 ▶ if ($dead_pid <= 0) { # got here because of qx()
Branch: 1 branch
BranchTrueFalseTotal
if ($dead_pid <= 0) 0 0 0
71 0 $self->dead_child($self->dead_child - 1);
72 0 next CHILD;
73 }
74 0 CHILDREN_CHECK: foreach my $this_child (keys %{$self->child})
75 {
76 0 ▶ if ($self->child->{$this_child}->{pid} == $dead_pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->child->{$this_child}{"pid"} == $dead_pid) 0 0 0
77 0 $self->log->debug("$this_child finished");
78 0 $self->scheduler->mark_job_as_finished( $self->child->{$this_child}->{job} );
79 0 delete $self->child->{$this_child};
80 0 $self->dead_child($self->dead_child - 1);
81 0 last CHILDREN_CHECK;
82 }
83 }
84 }
85 }
86  
87  
88  
89  
90 sub run_due_tests
91 0 ▶ {
Subroutine run_due_tests: not called CC=6 SCAR=37.4
Pod documented
92 0 my ($self, $job) = @_;
93  
94 0 my $system = $job->host->name;
95 0 my $id = $job->testrun->id;
96 0 my $queue = $job->queue->name;
97 0 my $msg = "start testrun $id, queue $queue on $system";
98 0 ▶ $msg .= "; testrun name: ".$job->testrun->shortname if $job->testrun->shortname;
Branch: 1 branch
BranchTrueFalseTotal
if $job->testrun->shortname 0 0 0
99  
100 0 $self->log->error($msg);
101 # check if this system is already active, just for error handling
102 0 ▶ $self->handle_dead_children() if $self->child->{$system};
Branch: 1 branch
BranchTrueFalseTotal
if $self->child->{$system} 0 0 0
103  
104 0 $self->scheduler->mark_job_as_running($job);
105  
106 0 my $pid = fork();
107 0 ▶ die "fork failed: $!" if (not defined $pid);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
108  
109 # hello child
110 0 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) { } 0 0 0
111  
112 0 my $child = Tapper::Fake::Child->new( $id );
113 0 my $retval = $child->runtest_handling( $system );
114 0 ▶ if ($retval) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retval) { } 0 0 0
115 0 $self->log->error("An error occured while trying to run testrun $id on $system: $retval");
116 } else {
117 0 $self->log->info("Runtest $id finished successfully");
118 }
119 0 exit 0;
120 } else {
121 0 $self->child->{$system}->{pid} = $pid;
122 0 $self->child->{$system}->{test_run} = $id;
123 0 $self->child->{$system}->{job} = $job;
124 }
125 0 return 0;
126  
127 }
128  
129  
130  
131 sub runloop
132 0 ▶ {
Subroutine runloop: not called CC=4 SCAR=30.0
Pod documented
133 0 my ($self, $lastrun) = @_;
134 0 my $timeout = $lastrun + $self->cfg->{times}{poll_intervall} - time();
135  
136 # sleep $timeout;
137 0 ▶ $self->handle_dead_children() if $self->dead_child;
Branch: 1 branch
BranchTrueFalseTotal
if $self->dead_child 0 0 0
138  
139 0 ▶ while ( my @jobs = $self->scheduler->get_next_job() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@jobs) = $self->scheduler->get_next_job) 0 0 0
140 0 foreach my $job (@jobs) {
141 0 $self->run_due_tests($job);
142 }
143 }
144 }
145  
146  
147  
148 sub prepare_server
149 0 ▶ {
Subroutine prepare_server: not called CC=2 SCAR=17.9
Pod documented
150 0 my ($self) = @_;
151 # these sets are used by select()
152  
153 0 my $allhosts = model('HardwareDB')->resultset('Systems')->search({active => 1, current_owner => {like => '%tapper%'}});
154 0 ▶ while (my $thishost = $allhosts->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $thishost = $allhosts->next) 0 0 0
155 0 push(@{$self->hosts}, $thishost->systemname);
156 }
157  
158 0 return 0;
159 }
160  
161  
162  
163 sub run
164 0 ▶ {
Subroutine run: not called CC=1 SCAR=6.9
Pod documented
165 0 my ($self) = @_;
166 0 $self->set_interrupt_handlers();
167 0 $self->prepare_server();
168 0 $self->log->debug('starting run');
169 0 while (1) {
170 0 my $lastrun = time();
171 0 $self->runloop($lastrun);
172 }
173  
174 }
175  
176 2 1;
177  
178 __END__
↑ Summary Fake/Testmachine.pm »