lib/Mogura/Scanner.pm

filter: Statementstmt 71.8% 212 / 295 Branchbran 50.6% 73 / 144 Conditioncond 52.3% 45 / 86 MC/DCmcdc 27.0% 13 / 48 Subroutinesub 71.0% 27 / 38 Podpod 0.0% 0 / 5 totaltotal 60.0% 370 / 616 CC 118 SCAR 35.5
CC
118
Cov
63%
run
182.0
initialize
110.0
_transfer
56.0
CRAP 34.7
« lib/Mogura/Server/Store.pm ↑ Summary lib/Mogura/Scanner/Client.pm »
1 package Mogura::Scanner;
2 2 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Cwd qw(abs_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Fcntl qw(:flock F_SETFD);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use POSIX qw(WNOHANG);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Time::HiRes qw(time sleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Mojo::File qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Mojo::JSON qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Mogura::Protocol qw(random_id valid_id now);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Mogura::Scanner::Config qw(trusted_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Mogura::Scanner::Spool;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Mogura::Scanner::Disk;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Mogura::Scanner::Scan;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Mogura::Scanner::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 0 ▶has config => sub { Mogura::Scanner::Config->new };
Subroutine __ANON__: not called CC=1 SCAR=1.2
17 0 ▶has client => sub { Mogura::Scanner::Client->new(config => shift->config) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
18 0 ▶has disk => sub { Mogura::Scanner::Disk->new(config => shift->config, app_root => abs_path($0)) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
19 0 ▶has spool => sub { Mogura::Scanner::Spool->new(config => shift->config) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
20 0 ▶has scan => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
21 0 my $self = shift;
22 0 Mogura::Scanner::Scan->new(config => $self->config, store => $self->spool, disk => $self->disk);
23 2 };
24 3 ▶has active => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
25  
26 0 ▶sub initialize {
Subroutine initialize: not called CC=10 SCAR=47.0
Pod undocumented
27 0 my ($self) = @_;
28 0 my $config = $self->config;
29 0 ▶ die 'Scanner requires root' if $>;
Branch: 1 branch
BranchTrueFalseTotal
if $> 0 0 0
30 0 umask 0077;
31 0 ▶ chdir '/' or die "chdir: $!";
Branch: 1 branch
BranchTrueFalseTotal
chdir "/" or die "chdir: $!" 0 0 0
32 0 for my $tool ($config->syft, $config->grype,
33 qw(/usr/bin/lsblk /usr/bin/findmnt /usr/bin/mount /usr/bin/umount /usr/sbin/blockdev /usr/bin/df /usr/bin/prlimit)
34 )
35 {
36 0 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
unless -f $tool and -x _ 0 0 0
Condition0%0/3
-f $tool and -x _
!ll&&!rl&&r
000
MC/DC0%0/2
-f $tool and -x _
-f $tool -x _
Truth table0%0/3
A and B
A= -f $toolB= -x _
ABresult
0X0
100
111
37 "Missing executable $tool. Install Syft and Grype from Anchore releases and the util-linux, mount and coreutils packages.\n"
38 unless -f $tool && -x _;
39 0 trusted_path($tool);
40 }
41 0 for my $d ($config->state, $config->runtime, $config->mounts, path($config->state, 'spool')) {
42 0 my $p = path($d);
43 0 trusted_path($p->dirname);
44 0 $p->make_path({mode => 0700});
45 0 trusted_path($p);
46 0 ▶ die "Private directory required: $p" if (stat($p))[2] & 0077;
Branch: 1 branch
BranchTrueFalseTotal
if (stat $p)[2] & 63 0 0 0
47 }
48 0 trusted_path($config->token_file);
49 0 $self->client->initialize;
50 0 $self->acquire_lock;
51 0 for my $dir (path($config->state, 'spool')->list({dir => 1})->each) {
52 0 ▶ $dir->remove_tree if $dir->basename =~ /\Adelivered-[a-f0-9]{32}\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $dir->basename =~ /\Adelivered-[a-f0-9]{32}\z/u 0 0 0
53 }
54 }
55  
56 4 ▶sub acquire_lock {
Subroutine acquire_lock: called CC=4 SCAR=14.1
Pod undocumented
57 4 my ($self) = @_;
58 4 ▶ open my $fh, '>>', path($self->config->state, 'scanner.lock') or die "Scanner lock: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">>", path($self->config->state, "scanner.lock") or die "Scanner lock: $!" 4 0 4
59 4 ▶ flock($fh, LOCK_EX | LOCK_NB) or die 'Scanner or surviving descendant already running';
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 6 or die "Scanner or surviving descendant already running" 2 2 4
60 2 ▶ fcntl($fh, F_SETFD, 0) or die "Inherit scanner lock: $!";
Branch: 1 branch
BranchTrueFalseTotal
fcntl $fh, 2, 0 or die "Inherit scanner lock: $!" 2 0 2
61 2 $self->{lock} = $fh;
62 }
63  
64 81 ▶sub managed_mounts {
Subroutine managed_mounts: called CC=2 SCAR=9.9
Pod undocumented
65 81 my ($self) = @_;
66 81 my $root = $self->config->mounts;
67 return [
68 0 map { $_->{target} }
69 0 ▶ grep { $_->{target} eq $root || index($_->{target}, "$root/") == 0 }
Condition0%0/3
$_->{'target'} eq $root || index($_->{'target'}, "$root/") == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->{'target'} eq $root || index($_->{'target'}, "$root/") == 0
$_->{'target'} eq $root index($_->{'target'}, "$root/") == 0
Truth table0%0/3
A || B
A= $_->{'target'} eq $rootB= index($_->{'target'}, "$root/") == 0
ABresult
1X1
011
000
70 81 @{$self->disk->mount_table}
71 ];
72 }
73  
74 81 ▶sub _records {
Subroutine _records: called CC=1 SCAR=0.0
75 81 my ($self) = @_;
76 return
77 81 grep { valid_id($_->basename) } path($self->config->state, 'spool')->list({dir => 1})->each;
78 }
79  
80 38 ▶sub _recover {
Subroutine _recover: called CC=13 SCAR=26.0
81 38 my ($self, $intent, $mounts) = @_;
82 38 my $id = $intent->{id};
83 38 ▶ my $scan = $self->spool->get($id) // $self->scan->queued($intent);
Condition66%2/3
$self->spool->get($id) // $self->scan->queued($intent)
l!l&&r!l&&!r
3710
MC/DC0%0/2
$self->spool->get($id) // $self->scan->queued($intent)
$self->spool->get($id) $self->scan->queued($intent)
Truth table66%2/3
A // B
A= $self->spool->get($id)B= $self->scan->queued($intent)
ABresult
1X1
011
000
84 38 my $root = $self->config->mounts . "/$id";
85 38 ▶ my $remaining = grep { $_ eq $root || index($_, "$root/") == 0 } @$mounts;
Condition33%1/3
$_ eq $root || index($_, "$root/") == 0
l!l&&r!l&&!r
100
MC/DC0%0/2
$_ eq $root || index($_, "$root/") == 0
$_ eq $root index($_, "$root/") == 0
Truth table33%1/3
A || B
A= $_ eq $rootB= index($_, "$root/") == 0
ABresult
1X1
011
000
86  
87 # Exec descendants inherit the scan lock even when their supervisor dies.
88 38 my $prior_cleanup = $scan->{cleanup_status};
89 38 my $lock = eval { $self->scan->_acquire_lock("scan-$id") };
90 38 ▶ if (!$lock || $remaining) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $lock or $remaining) { } 2 36 38
elsif ($scan->{'cleanup_status'} eq 'needs_cleanup' or $scan->{'cleanup_status'} eq 'mounted') { } 1 35 36
elsif ($scan->{'phase'} eq 'finished') { } 33 2 35
Condition100%3/3
not $lock or $remaining
l!l&&r!l&&!r
1136
MC/DC100%2/2
not $lock or $remaining
not $lock $remaining
Truth table100%3/3
not(A) or B
A= not $lockB= $remaining
ABresult
1X1
011
000
91 2 $scan->{cleanup_status} = 'needs_cleanup';
92 }
93 ▶ elsif ($scan->{cleanup_status} eq 'needs_cleanup' || $scan->{cleanup_status} eq 'mounted') {
Condition66%2/3
$scan->{"cleanup_status"} eq "needs_cleanup" or $scan->{"cleanup_status"} eq "mounted"
l!l&&r!l&&!r
1035
MC/DC50%1/2
$scan->{"cleanup_status"} eq "needs_cleanup" or $scan->{"cleanup_status"} eq "mounted"
$scan->{"cleanup_status"} eq "needs_cleanup" $scan->{"cleanup_status"} eq "mounted"
Truth table66%2/3
A or B
A= $scan->{"cleanup_status"} eq "needs_cleanup"B= $scan->{"cleanup_status"} eq "mounted"
ABresult
1X1
011
000
94 1 $scan->{cleanup_status} = 'unmounted';
95 }
96 elsif ($scan->{phase} eq 'finished') {
97 33 return $scan;
98 }
99 5 ▶ return $scan if $scan->{phase} eq 'finished' && $prior_cleanup eq $scan->{cleanup_status};
Branch: 1 branch
BranchTrueFalseTotal
if $scan->{"phase"} eq "finished" and $prior_cleanup eq $scan->{"cleanup_status"} 1 4 5
Condition100%3/3
$scan->{"phase"} eq "finished" and $prior_cleanup eq $scan->{"cleanup_status"}
!ll&&!rl&&r
311
MC/DC100%2/2
$scan->{"phase"} eq "finished" and $prior_cleanup eq $scan->{"cleanup_status"}
$scan->{"phase"} eq "finished" $prior_cleanup eq $scan->{"cleanup_status"}
Truth table100%3/3
A and B
A= $scan->{"phase"} eq "finished"B= $prior_cleanup eq $scan->{"cleanup_status"}
ABresult
0X0
100
111
100 $scan->{status} = 'partial'
101 4 ▶ if $scan->{status} eq 'completed' && $scan->{cleanup_status} eq 'needs_cleanup';
Branch: 1 branch
BranchTrueFalseTotal
if $scan->{"status"} eq "completed" and $scan->{"cleanup_status"} eq "needs_cleanup" 0 4 4
Condition33%1/3
$scan->{"status"} eq "completed" and $scan->{"cleanup_status"} eq "needs_cleanup"
!ll&&!rl&&r
400
MC/DC0%0/2
$scan->{"status"} eq "completed" and $scan->{"cleanup_status"} eq "needs_cleanup"
$scan->{"status"} eq "completed" $scan->{"cleanup_status"} eq "needs_cleanup"
Truth table33%1/3
A and B
A= $scan->{"status"} eq "completed"B= $scan->{"cleanup_status"} eq "needs_cleanup"
ABresult
0X0
100
111
102 4 ▶ unless ($scan->{phase} eq 'finished') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($scan->{"phase"} eq "finished") 3 1 4
103 3 $scan->{status} = 'interrupted';
104 3 $scan->{phase} = 'finished';
105 3 $scan->{error} = 'Scan process stopped before completion';
106 3 $scan->{finished_at} = now();
107 }
108 4 return $self->scan->save_finished($scan);
109 }
110  
111 0 ▶sub _transfer {
Subroutine _transfer: not called CC=7 SCAR=40.3
112 0 my ($self, $dir, $intent, $scan) = @_;
113 0 my $route = "/jobs/$intent->{id}/$intent->{run}";
114 0 my $record = $dir->child('delivery.json');
115 0 ▶ my $sent = -e $record ? decode_json($record->slurp) : {};
Branch: 1 branch
BranchTrueFalseTotal
-e $record ? : 0 0 0
116 0 $self->client->request('POST', "$route/progress", $scan);
117 0 for my $kind (qw(syft cdx grype log)) {
118 0 ▶ my $file = $self->spool->artifact($intent->{id}, $kind) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $file = $self->spool->artifact($intent->{"id"}, $kind) or next 0 0 0
119 0 ▶ next if $sent->{$kind};
Branch: 1 branch
BranchTrueFalseTotal
if $sent->{$kind} 0 0 0
120 0 $self->client->request('PUT', "$route/files/$kind", undef, $file);
121 0 $sent->{$kind} = 1;
122 0 $self->spool->write_record($record, $sent);
123 }
124 0 $self->client->request('POST', "$route/complete", $scan);
125 0 ▶ if ($scan->{cleanup_status} eq 'needs_cleanup') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scan->{"cleanup_status"} eq "needs_cleanup") 0 0 0
126 0 $sent->{complete} = $scan->{sequence};
127 0 $self->spool->write_record($record, $sent);
128 0 return;
129 }
130  
131 # Only acknowledged spool files are disposable. Atomically retire the
132 # directory so a stop during deletion cannot resurrect a partial run.
133 0 my $retired = $dir->sibling('delivered-' . $dir->basename);
134 0 ▶ rename $dir, $retired or die "Retire spool: $!";
Branch: 1 branch
BranchTrueFalseTotal
rename $dir, $retired or die "Retire spool: $!" 0 0 0
135 0 $retired->remove_tree;
136 }
137  
138 1 ▶sub _retire_unstarted {
Subroutine _retire_unstarted: called CC=4 SCAR=14.3
139 1 my ($self, $intent, $cancel) = @_;
140 1 my $id = $intent->{id};
141 1 ▶ if ($cancel) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cancel) 1 0 1
142  
143 # A lost claim reply may be retried after cancellation, before any
144 # local work started. Acknowledge before retiring the saved intent.
145 1 $self->client->request(
146 'POST',
147 "/jobs/$id/$intent->{run}/complete",
148 {
149 status => 'cancelled',
150 phase => 'finished',
151 finished_at => now(),
152 cleanup_status => 'not_needed',
153 sequence => 1
154 }
155 );
156 }
157 1 my $dir = $self->spool->directory($id);
158 1 ▶ unlink $dir->child('intent.json') or die "Discard unstarted claim: $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $dir->child("intent.json") or die "Discard unstarted claim: $!" 1 0 1
159 1 ▶ rmdir $dir or die "Remove empty claim directory: $!";
Branch: 1 branch
BranchTrueFalseTotal
rmdir $dir or die "Remove empty claim directory: $!" 1 0 1
160 }
161  
162 7 ▶sub _fork_job {
Subroutine _fork_job: called CC=6 SCAR=27.1
163 7 my ($self, $id, $work, $slot) = @_;
164 7 my $pid = fork;
165 7 ▶ die "fork: $!" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 7 7
166 7 ▶ if (!$pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 0 7 7
167 0 $SIG{$_} = 'DEFAULT' for qw(INT TERM HUP);
168 0 my $ok = eval { $work->(); 1 };
169 0 ▶ warn $@ unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
170 0 ▶ POSIX::_exit($ok ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 0 0 0
171 }
172 7 $self->active->{$id} = {pid => $pid, slot => $slot};
173 }
174  
175 80 ▶sub _capacity {
Subroutine _capacity: called CC=10 SCAR=23.6
176 80 my ($self) = @_;
177 80 my $root = path($self->config->state, 'spool');
178 80 my $used = 0;
179 80 ▶ $used += (-s $_ // 0) for $root->list_tree->each;
Condition50%1/2
-s $_ // 0
l!l
1900
MC/DC0%0/1
-s $_ // 0
-s $_
Truth table50%1/2
A
A= -s $_
Aresult
11
00
180 80 my $available =
181 $self->scan->command->run(['/usr/bin/df', '-B1', '--output=avail', '--', "$root"]);
182 80 ▶ return 0 unless $available =~ /\n\s*(\d+)\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $available =~ /\n\s*(\d+)\s*\z/u 0 80 80
183 80 my $free = $1 - 16 * 1024**2;
184 80 my $reserved = 0;
185 80 ▶ $reserved += ($_->{limit} // 0) * 5 for values %{$self->active};
Condition50%1/2
$_->{"limit"} // 0
l!l
07
MC/DC0%0/1
$_->{"limit"} // 0
$_->{"limit"}
Truth table50%1/2
A
A= $_->{"limit"}
Aresult
11
00
186 80 my $budget = $self->config->spool_limit - $used - $reserved;
187 80 $free -= $reserved;
188 80 ▶ $budget = $free if $free < $budget;
Branch: 1 branch
BranchTrueFalseTotal
if $free < $budget 0 80 80
189  
190 # Divide among concurrent scans and four output streams. prlimit enforces
191 # the reservation in the tools even if the parent loses its connection.
192 80 my $slots = $self->config->jobs - grep { $_->{slot} } values %{$self->active};
193 80 ▶ return 0 unless $slots;
Branch: 1 branch
BranchTrueFalseTotal
unless $slots 0 80 80
194 80 my $limit = int($budget / ($slots * 5));
195 80 ▶ $limit = 4 * 1024**3 if $limit > 4 * 1024**3;
Branch: 1 branch
BranchTrueFalseTotal
if $limit > 4294967296 0 80 80
196 80 ▶ return $limit >= 65536 ? $limit : 0;
Branch: 1 branch
BranchTrueFalseTotal
$limit >= 65536 ? : 79 1 80
197 }
198  
199 3 ▶sub _identify {
Subroutine _identify: called CC=7 SCAR=21.3
200 3 my ($self) = @_;
201 3 my $identity = $self->client->request('GET', '/identity');
202 die 'Invalid scanner identity'
203 unless valid_id($identity->{server})
204 3 ▶ && ($identity->{scanner} // '') =~ /\A[a-zA-Z0-9_-]{1,64}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless valid_id($identity->{"server"}) and ($identity->{"scanner"} // "") =~ /\A[a-zA-Z0-9_-]{1,64}\z/u 0 3 3
Condition40%2/5
valid_id($identity->{"server"}) and ($identity->{"scanner"} // "") =~ /\A[a-zA-Z0-9_-]{1,64}\z/u
!ll&&!rl&&r
003
$identity->{"scanner"} // ""
l!l
30
MC/DC0%0/2
valid_id($identity->{"server"}) and ($identity->{"scanner"} // "") =~ /\A[a-zA-Z0-9_-]{1,64}\z/u
valid_id($identity->{"server"}) $identity->{"scanner"}
Truth table33%1/3
A and B
A= valid_id($identity->{"server"})B= $identity->{"scanner"}
ABresult
0X0
100
111
205 3 my $file = path($self->config->state, 'identity.json');
206 3 ▶ if (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) { } 1 2 3
207 1 my $saved = decode_json($file->slurp);
208 die 'Spool belongs to a different server or scanner'
209 unless $saved->{server} eq $identity->{server}
210 1 ▶ && $saved->{scanner} eq $identity->{scanner};
Branch: 1 branch
BranchTrueFalseTotal
unless $saved->{"server"} eq $identity->{"server"} and $saved->{"scanner"} eq $identity->{"scanner"} 0 1 1
Condition33%1/3
$saved->{"server"} eq $identity->{"server"} and $saved->{"scanner"} eq $identity->{"scanner"}
!ll&&!rl&&r
001
MC/DC0%0/2
$saved->{"server"} eq $identity->{"server"} and $saved->{"scanner"} eq $identity->{"scanner"}
$saved->{"server"} eq $identity->{"server"} $saved->{"scanner"} eq $identity->{"scanner"}
Truth table33%1/3
A and B
A= $saved->{"server"} eq $identity->{"server"}B= $saved->{"scanner"} eq $identity->{"scanner"}
ABresult
0X0
100
111
211 }
212 else {
213 2 $self->spool->write_record($file, $identity);
214 }
215 3 $self->{identified} = 1;
216 }
217  
218 81 ▶sub tick {
Subroutine tick: called CC=6 SCAR=18.0
Pod undocumented
219 81 my ($self) = @_;
220 81 ▶ $self->_identify unless $self->{identified};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"identified"} 3 78 81
221 81 $self->_reap_children;
222 81 my ($pending, $blocked) = $self->_reconcile_spool;
223 $self->_send_inventory($pending, $blocked)
224 81 ▶ if @$blocked || time >= ($self->{inventory_due} // 0);
Branch: 1 branch
BranchTrueFalseTotal
if @$blocked or &Mogura::Scanner::time() >= ($self->{"inventory_due"} // 0) 8 73 81
Condition80%4/5
@$blocked or &Mogura::Scanner::time() >= ($self->{"inventory_due"} // 0)
l!l&&r!l&&!r
0873
$self->{"inventory_due"} // 0
l!l
783
MC/DC50%1/2
@$blocked or &Mogura::Scanner::time() >= ($self->{"inventory_due"} // 0)
@$blocked &Mogura::Scanner::time() >= ($self->{"inventory_due"} // 0)
MC/DC100%1/1
$self->{"inventory_due"} // 0
$self->{"inventory_due"}
Truth table66%2/3
A or B
A= @$blockedB= &Mogura::Scanner::time() >= ($self->{"inventory_due"} // 0)
ABresult
1X1
011
000
Truth table100%2/2
A
A= $self->{"inventory_due"}
Aresult
11
00
225 81 ▶ return if @$blocked;
Branch: 1 branch
BranchTrueFalseTotal
if @$blocked 0 81 81
226 81 $self->_claim_and_start($pending);
227 }
228  
229 81 ▶sub _reap_children {
Subroutine _reap_children: called CC=7 SCAR=19.5
230 81 my ($self) = @_;
231 81 my $active = $self->active;
232 81 for my $id (keys %$active) {
233 11 ▶ next unless waitpid($active->{$id}{pid}, WNOHANG) > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless waitpid($active->{$id}{"pid"}, 1) > 0 5 6 11
234 6 my $child = delete $active->{$id};
235 6 ▶ next if $child->{slot};
Branch: 1 branch
BranchTrueFalseTotal
if $child->{"slot"} 2 4 6
236 4 ▶ my $delay = $? ? ($self->{delivery_delay} || 1) * 2 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$? ? : 1 3 4
Condition50%1/2
$self->{'delivery_delay'} || 1
l!l
01
MC/DC0%0/1
$self->{'delivery_delay'} || 1
$self->{'delivery_delay'}
Truth table50%1/2
A
A= $self->{'delivery_delay'}
Aresult
11
00
237 4 ▶ $self->{delivery_delay} = $delay > 30 ? 30 : $delay;
Branch: 1 branch
BranchTrueFalseTotal
$delay > 30 ? : 0 4 4
238 4 $self->{delivery_due} = time + $self->{delivery_delay};
239 }
240 }
241  
242 81 ▶sub _reconcile_spool {
Subroutine _reconcile_spool: called CC=23 SCAR=34.2
243 81 my ($self) = @_;
244 81 my $active = $self->active;
245 81 my $mounts = $self->managed_mounts;
246 my %owned =
247 81 map { ($self->config->mounts . "/$_") => 1 } grep { $active->{$_}{slot} } keys %$active;
248 0 my @blocked = grep { !$owned{$_} } @$mounts;
249 81 my %pending;
250 81 my $sending = grep { !$_->{slot} } values %$active;
251 81 for my $dir ($self->_records) {
252 42 my $id = $dir->basename;
253 42 my $file = $dir->child('intent.json');
254 42 ▶ next unless -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 42 42
255 42 my $intent = decode_json($file->slurp);
256 die 'Invalid local execution record'
257 42 ▶ unless $intent->{id} eq $id && valid_id($intent->{run});
Branch: 1 branch
BranchTrueFalseTotal
unless $intent->{"id"} eq $id and valid_id($intent->{"run"}) 0 42 42
Condition33%1/3
$intent->{"id"} eq $id and valid_id($intent->{"run"})
!ll&&!rl&&r
0042
MC/DC0%0/2
$intent->{"id"} eq $id and valid_id($intent->{"run"})
$intent->{"id"} eq $id valid_id($intent->{"run"})
Truth table33%1/3
A and B
A= $intent->{"id"} eq $idB= valid_id($intent->{"run"})
ABresult
0X0
100
111
258 die 'Restore the runtime directory recorded for the outstanding scan'
259 42 ▶ unless $intent->{runtime} eq $self->config->runtime;
Branch: 1 branch
BranchTrueFalseTotal
unless $intent->{"runtime"} eq $self->config->runtime 0 42 42
260 42 ▶ unless ($intent->{started}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($intent->{"started"}) 2 40 42
261 2 $pending{$id} = $intent;
262 2 next;
263 }
264 40 ▶ if (my $child = $active->{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $child = $active->{$id}) 5 35 40
265 5 my $scan = $self->spool->get($id);
266 5 ▶ push @blocked, $id if $scan && $scan->{cleanup_status} eq 'needs_cleanup';
Branch: 1 branch
BranchTrueFalseTotal
if $scan and $scan->{"cleanup_status"} eq "needs_cleanup" 0 5 5
Condition33%1/3
$scan and $scan->{"cleanup_status"} eq "needs_cleanup"
!ll&&!rl&&r
050
MC/DC0%0/2
$scan and $scan->{"cleanup_status"} eq "needs_cleanup"
$scan $scan->{"cleanup_status"} eq "needs_cleanup"
Truth table33%1/3
A and B
A= $scanB= $scan->{"cleanup_status"} eq "needs_cleanup"
ABresult
0X0
100
111
267 5 ▶ if ($child->{slot}
Branch: 1 branch
BranchTrueFalseTotal
if ($child->{"slot"} and $scan and $scan->{"phase"} ne "finished" and ($child->{"reported"} // 0) < $scan->{"sequence"}) 1 4 5
Condition81%9/11
$child->{"slot"} and $scan and $scan->{"phase"} ne "finished" and ($child->{"reported"} // 0) < $scan->{"sequence"}
!ll&&!rl&&r
311
$child->{"slot"} and $scan and $scan->{"phase"} ne "finished"
!ll&&!rl&&r
302
$child->{"slot"} and $scan
!ll&&!rl&&r
302
$child->{"reported"} // 0
l!l
11
MC/DC50%2/4
$child->{"slot"} and $scan and $scan->{"phase"} ne "finished" and ($child->{"reported"} // 0) < $scan->{"sequence"}
$child->{"slot"} $scan $scan->{"phase"} ne "finished" $child->{"reported"}
Truth table60%3/5
A and B and C and D
A= $child->{"slot"}B= $scanC= $scan->{"phase"} ne "finished"D= $child->{"reported"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
268 && $scan
269 && $scan->{phase} ne 'finished'
270 && ($child->{reported} // 0) < $scan->{sequence})
271 {
272 1 $self->client->request(
273 'POST',
274 "/jobs/$id/$intent->{run}/progress",
275 Mogura::Scanner::Scan::bounded_report($scan)
276 );
277 1 $child->{reported} = $scan->{sequence};
278 }
279 5 next;
280 }
281 35 my $scan = $self->_recover($intent, $mounts);
282 35 ▶ push @blocked, $id if $scan->{cleanup_status} eq 'needs_cleanup';
Branch: 1 branch
BranchTrueFalseTotal
if $scan->{"cleanup_status"} eq "needs_cleanup" 0 35 35
283 35 my $receipt = $dir->child('delivery.json');
284 next
285 if $scan->{cleanup_status} eq 'needs_cleanup'
286 && -f $receipt
287 35 ▶ && (decode_json($receipt->slurp)->{complete} // 0) == $scan->{sequence};
Branch: 1 branch
BranchTrueFalseTotal
if $scan->{"cleanup_status"} eq "needs_cleanup" and -f $receipt and (decode_json($receipt->slurp)->{"complete"} // 0) == $scan->{"sequence"} 0 35 35
Condition25%2/8
$scan->{"cleanup_status"} eq "needs_cleanup" and -f $receipt and (decode_json($receipt->slurp)->{"complete"} // 0) == $scan->{"sequence"}
!ll&&!rl&&r
3500
$scan->{"cleanup_status"} eq "needs_cleanup" and -f $receipt
!ll&&!rl&&r
3500
decode_json($receipt->slurp)->{"complete"} // 0
l!l
00
MC/DC0%0/3
$scan->{"cleanup_status"} eq "needs_cleanup" and -f $receipt and (decode_json($receipt->slurp)->{"complete"} // 0) == $scan->{"sequence"}
$scan->{"cleanup_status"} eq "needs_cleanup" -f $receipt decode_json($receipt->slurp)->{"complete"}
Truth table25%1/4
A and B and C
A= $scan->{"cleanup_status"} eq "needs_cleanup"B= -f $receiptC= decode_json($receipt->slurp)->{"complete"}
ABCresult
0XX0
10X0
1100
1111
288 35 ▶ next if $sending || time < ($self->{delivery_due} // 0);
Branch: 1 branch
BranchTrueFalseTotal
if $sending or &Mogura::Scanner::time() < ($self->{"delivery_due"} // 0) 31 4 35
Condition80%4/5
$sending or &Mogura::Scanner::time() < ($self->{"delivery_due"} // 0)
l!l&&r!l&&!r
0314
$self->{"delivery_due"} // 0
l!l
332
MC/DC50%1/2
$sending or &Mogura::Scanner::time() < ($self->{"delivery_due"} // 0)
$sending &Mogura::Scanner::time() < ($self->{"delivery_due"} // 0)
MC/DC100%1/1
$self->{"delivery_due"} // 0
$self->{"delivery_due"}
Truth table66%2/3
A or B
A= $sendingB= &Mogura::Scanner::time() < ($self->{"delivery_due"} // 0)
ABresult
1X1
011
000
Truth table100%2/2
A
A= $self->{"delivery_due"}
Aresult
11
00
289 0 ▶ $self->_fork_job($id, sub { $self->_transfer($dir, $intent, $scan) }, 0);
Subroutine __ANON__: not called CC=1 SCAR=1.2
290 4 $sending = 1;
291 }
292 81 return (\%pending, \@blocked);
293 }
294  
295 8 ▶sub _send_inventory {
Subroutine _send_inventory: called CC=10 SCAR=26.4
296 8 my ($self, $pending, $blocked) = @_;
297 my $inventory =
298 8 ▶ eval { $self->disk->inventory } // {disks => [], errors => ["Disk discovery failed: $@"]};
Condition50%1/2
eval { do { $self->disk->inventory } } // {"disks", [], "errors", ["Disk discovery failed: $@"]}
l!l
80
MC/DC0%0/1
eval { do { $self->disk->inventory } } // {"disks", [], "errors", ["Disk discovery failed: $@"]}
eval { do { $self->disk->inventory } }
Truth table50%1/2
A
A= eval { do { $self->disk->inventory } }
Aresult
11
00
299 8 $inventory->{blocked} = $blocked;
300 8 my $reply = $self->client->request('POST', '/inventory', $inventory);
301 8 ▶ for my $request (@{$reply->{cancel} // []}) {
Condition50%1/2
$reply->{"cancel"} // []
l!l
80
MC/DC0%0/1
$reply->{"cancel"} // []
$reply->{"cancel"}
Truth table50%1/2
A
A= $reply->{"cancel"}
Aresult
11
00
302 2 my $id = $request->{id};
303 2 ▶ if (my $intent = $pending->{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $intent = $pending->{$id}) 0 2 2
304 0 ▶ next unless $intent->{run} eq $request->{run};
Branch: 1 branch
BranchTrueFalseTotal
unless $intent->{"run"} eq $request->{"run"} 0 0 0
305 0 $self->_retire_unstarted($intent, 1);
306 0 delete $pending->{$id};
307 0 next;
308 }
309 2 ▶ my $child = $self->active->{$id} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $child = $self->active->{$id} or next 2 0 2
310 2 ▶ next unless $child->{slot} && $child->{run} eq $request->{run};
Branch: 1 branch
BranchTrueFalseTotal
unless $child->{"slot"} and $child->{"run"} eq $request->{"run"} 1 1 2
Condition66%2/3
$child->{"slot"} and $child->{"run"} eq $request->{"run"}
!ll&&!rl&&r
101
MC/DC50%1/2
$child->{"slot"} and $child->{"run"} eq $request->{"run"}
$child->{"slot"} $child->{"run"} eq $request->{"run"}
Truth table66%2/3
A and B
A= $child->{"slot"}B= $child->{"run"} eq $request->{"run"}
ABresult
0X0
100
111
311 1 ▶ next if $child->{stop_sent};
Branch: 1 branch
BranchTrueFalseTotal
if $child->{"stop_sent"} 0 1 1
312  
313 # This direct child cannot have its PID reused before we reap it.
314 1 $child->{stop_sent} = kill 'TERM', $child->{pid};
315 }
316 8 $self->{inventory_due} = time + $self->config->inventory_interval;
317 }
318  
319 81 ▶sub _claim_and_start {
Subroutine _claim_and_start: called CC=10 SCAR=23.5
320 81 my ($self, $pending) = @_;
321 81 my $active = $self->active;
322 81 my %slots = map { $_->{slot} => 1 } values %$active;
323 81 my ($slot) = grep { !$slots{$_} } 1 .. $self->config->jobs;
324 81 ▶ return unless $slot;
Branch: 1 branch
BranchTrueFalseTotal
unless $slot 2 79 81
325 79 ▶ my $limit = $self->_capacity or return;
Branch: 1 branch
BranchTrueFalseTotal
my $limit = $self->_capacity or return 79 0 79
326 79 my ($intent) = values %$pending;
327 79 ▶ unless ($intent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($intent) 77 2 79
328 77 ▶ my $job = $self->client->request('GET', '/jobs')->{job} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $job = $self->client->request("GET", "/jobs")->{"job"} or return 4 73 77
329 $intent = {
330 4 %{$self->scan->validate_request($job)},
331 run => random_id(),
332 runtime => $self->config->runtime,
333 started => 0
334 };
335 4 my $dir = $self->spool->directory($intent->{id})->make_path({mode => 0700});
336 4 ▶ return if -e $dir->child('intent.json');
Branch: 1 branch
BranchTrueFalseTotal
if -e $dir->child("intent.json") 0 4 4
337 4 ▶ $dir->dirname->open('<')->sync or die "Sync spool directory: $!";
Branch: 1 branch
BranchTrueFalseTotal
$dir->dirname->open("<")->sync or die "Sync spool directory: $!" 4 0 4
338 4 $self->spool->write_record($dir->child('intent.json'), $intent);
339 }
340 6 my $id = $intent->{id};
341 6 my $claim = $self->client->request('POST', "/jobs/$id/$intent->{run}/claim", {});
342 return $self->_retire_unstarted($intent, $claim->{cancel})
343 4 ▶ if $claim->{expired} || $claim->{cancel};
Branch: 1 branch
BranchTrueFalseTotal
if $claim->{"expired"} or $claim->{"cancel"} 1 3 4
Condition66%2/3
$claim->{"expired"} or $claim->{"cancel"}
l!l&&r!l&&!r
013
MC/DC50%1/2
$claim->{"expired"} or $claim->{"cancel"}
$claim->{"expired"} $claim->{"cancel"}
Truth table66%2/3
A or B
A= $claim->{"expired"}B= $claim->{"cancel"}
ABresult
1X1
011
000
344 3 ▶ return if $self->{stopping};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stopping"} 0 3 3
345 3 $intent->{started} = 1;
346 3 $self->spool->write_record($self->spool->directory($id)->child('intent.json'), $intent);
347 $self->_fork_job(
348 $id,
349 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
350 0 local $SIG{XFSZ} = 'IGNORE';
351 0 $self->scan->command->run(['/usr/bin/prlimit', '--pid', "$$", "--fsize=$limit:$limit"]);
352 0 $self->scan->cache_slot($slot)->run($intent);
353 },
354 3 $slot
355 );
356 3 $active->{$id}{limit} = $limit;
357 3 $active->{$id}{run} = $intent->{run};
358 }
359  
360 0 ▶sub run {
Subroutine run: not called CC=13 SCAR=52.0
Pod undocumented
361 0 my ($self) = @_;
362 0 my $mounts = $self->managed_mounts;
363 0 for my $dir ($self->_records) {
364 0 my $file = $dir->child('intent.json');
365 0 ▶ next unless -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 0 0
366 0 my $intent = decode_json($file->slurp);
367 die 'Invalid spool execution or runtime'
368 0 ▶ unless $intent->{id} eq $dir->basename && $intent->{runtime} eq $self->config->runtime;
Branch: 1 branch
BranchTrueFalseTotal
unless $intent->{"id"} eq $dir->basename and $intent->{"runtime"} eq $self->config->runtime 0 0 0
Condition0%0/3
$intent->{"id"} eq $dir->basename and $intent->{"runtime"} eq $self->config->runtime
!ll&&!rl&&r
000
MC/DC0%0/2
$intent->{"id"} eq $dir->basename and $intent->{"runtime"} eq $self->config->runtime
$intent->{"id"} eq $dir->basename $intent->{"runtime"} eq $self->config->runtime
Truth table0%0/3
A and B
A= $intent->{"id"} eq $dir->basenameB= $intent->{"runtime"} eq $self->config->runtime
ABresult
0X0
100
111
369 0 ▶ $self->_recover($intent, $mounts) if $intent->{started};
Branch: 1 branch
BranchTrueFalseTotal
if $intent->{"started"} 0 0 0
370 }
371 0 my $delay = 1;
372 0 ▶ local $SIG{INT} = local $SIG{TERM} = local $SIG{HUP} = sub { $self->{stopping} = 1 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
373 0 ▶ until ($self->{stopping}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"stopping"}) 0 0 0
374 0 my $ok = eval { $self->tick; 1 };
375 0 ▶ warn $@ unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
376 0 ▶ $delay = $ok ? 1 : $delay < 16 ? $delay * 2 : 30;
Branch: 2 branches
BranchTrueFalseTotal
$ok ? : 0 0 0
$delay < 16 ? : 0 0 0
377 0 ▶ sleep $delay unless $self->{stopping};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"stopping"} 0 0 0
378 }
379 0 kill 'TERM', $_->{pid} for values %{$self->active};
380 0 waitpid($_->{pid}, 0) for values %{$self->active};
381 }
382 2 1;
« lib/Mogura/Server/Store.pm ↑ Summary lib/Mogura/Scanner/Client.pm »