lib/Langertha/Skeid/Registry.pm

filter: Statementstmt 100% 80 / 80 Branchbran 83.3% 35 / 42 Conditioncond 66.6% 22 / 33 MC/DCmcdc 35.2% 6 / 17 Subroutinesub 100% 15 / 15 Podpod 100% 8 / 8 totaltotal 85.1% 166 / 195 CC 35 SCAR 27.5
CC
35
Cov
88%
reading_from_snapshot
27.2
verify
5.2
_num
4.1
CRAP 15.7
« blib/lib/Langertha/Skeid/CapacityProbe/Registry.pm ↑ Summary blib/lib/Langertha/Skeid/UsageStore/DBI.pm »
1 package Langertha::Skeid::Registry;
2 87 our $VERSION = '0.004';
3 # ABSTRACT: Signed Skeid-to-Skeid capacity snapshots: encoding, signing, verification, mapping
4 87 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 87 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 87 ▶use Digest::SHA qw(hmac_sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 87 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 87 ▶use Langertha::Skeid::Secret;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 87 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12  
13 45 ▶sub SIGNATURE_HEADER { 'X-Skeid-Registry-Signature' }
Subroutine SIGNATURE_HEADER: called CC=1 SCAR=0.0
Pod documented
14  
15  
16 22 ▶sub SCHEMA_VERSION { 1 }
Subroutine SCHEMA_VERSION: called CC=1 SCAR=0.0
Pod documented
17  
18  
19 40 ▶sub MIN_SECRET_BYTES { 32 }
Subroutine MIN_SECRET_BYTES: called CC=1 SCAR=0.0
Pod documented
20  
21 87 my $JSON = JSON::MaybeXS->new(canonical => 1, utf8 => 1);
22  
23  
24 32 ▶sub encode {
Subroutine encode: called CC=1 SCAR=0.0
Pod documented
25 32 my ($class, $snapshot) = @_;
26 32 return $JSON->encode($snapshot);
27 }
28  
29  
30 60 ▶sub sign {
Subroutine sign: called CC=1 SCAR=0.0
Pod documented
31 60 my ($class, $body, $secret) = @_;
32 60 return 'sha256=' . hmac_sha256_hex($body, $secret);
33 }
34  
35  
36 20 ▶sub signed_snapshot {
Subroutine signed_snapshot: called CC=3 SCAR=11.3
Pod documented
37 20 my ($class, $skeid) = @_;
38 20 ▶ my $secret = $skeid->registry_secret // '';
Condition50%1/2
$skeid->registry_secret // ""
l!l
200
MC/DC0%0/1
$skeid->registry_secret // ""
$skeid->registry_secret
Truth table50%1/2
A
A= $skeid->registry_secret
Aresult
11
00
39 20 ▶ die "registry secret is not set; a snapshot is never published unsigned\n" unless length $secret;
Branch: 1 branch
BranchTrueFalseTotal
unless length $secret 0 20 20
40 20 my $body = $class->encode($skeid->registry_snapshot);
41 19 return ($body, $class->sign($body, $secret));
42 }
43  
44  
45 29 ▶sub verify {
Subroutine verify: called CC=5 SCAR=16.4
Pod documented
46 29 my ($class, $body, $header, $secret) = @_;
47 29 ▶ return 0 unless defined($body) && defined($header) && defined($secret) && length($secret);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body and defined $header and defined $secret and length $secret 1 28 29
Condition66%6/9
defined $body and defined $header and defined $secret and length $secret
!ll&&!rl&&r
1028
defined $body and defined $header and defined $secret
!ll&&!rl&&r
1028
defined $body and defined $header
!ll&&!rl&&r
0128
MC/DC25%1/4
defined $body and defined $header and defined $secret and length $secret
defined $body defined $header defined $secret length $secret
Truth table40%2/5
A and B and C and D
A= defined $bodyB= defined $headerC= defined $secretD= length $secret
ABCDresult
0XXX0
10XX0
110X0
11100
11111
48 28 my $want = $class->sign($body, $secret);
49 28 $header =~ s/\A\s+|\s+\z//g;
50 28 return Langertha::Skeid::Secret->equal($header, $want);
51 }
52  
53  
54 27 ▶sub reading_from_snapshot {
Subroutine reading_from_snapshot: called CC=26 SCAR=33.0
Pod documented
55 27 my ($class, $snapshot, %args) = @_;
56 27 ▶ my @want = map { lc } grep { defined && length } @{ $args{tags} || [] };
Condition60%3/5
defined $_ && length $_
!ll&&!rl&&r
001
$args{'tags'} || []
l!l
207
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
MC/DC100%1/1
$args{'tags'} || []
$args{'tags'}
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
Truth table100%2/2
A
A= $args{'tags'}
Aresult
11
00
57 27 my $now = Time::HiRes::time();
58  
59 27 my ($limit, $free, $selected) = (0, 0, 0);
60 27 ▶ for my $node (@{ $snapshot->{nodes} || [] }) {
Condition50%1/2
$snapshot->{'nodes'} || []
l!l
270
MC/DC0%0/1
$snapshot->{'nodes'} || []
$snapshot->{'nodes'}
Truth table50%1/2
A
A= $snapshot->{'nodes'}
Aresult
11
00
61 44 ▶ next unless ref($node) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node eq "HASH" 0 44 44
62 44 ▶ next unless $node->{healthy};
Branch: 1 branch
BranchTrueFalseTotal
unless $node->{"healthy"} 2 42 44
63 42 ▶ if (@want) {
Branch: 1 branch
BranchTrueFalseTotal
if (@want) 2 40 42
64 2 ▶ my %have = map { lc($_) => 1 } grep { defined } @{ ref($node->{tags}) eq 'ARRAY' ? $node->{tags} : [] };
Branch: 1 branch
BranchTrueFalseTotal
ref $node->{'tags'} eq 'ARRAY' ? : 2 0 2
65 2 ▶ next if grep { !$have{$_} } @want;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $have{$_};} @want 1 1 2
66 }
67 41 $selected++;
68  
69 41 my $max = _num($node->{max_conns});
70 41 my $inflight = _num($node->{inflight});
71 41 ▶ my $cap = ref($node->{capacity}) eq 'HASH' ? $node->{capacity} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $node->{'capacity'} eq 'HASH' ? : 2 39 41
72 41 ▶ my $cap_limit = $cap ? _num($cap->{limit}) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$cap ? : 2 39 41
73 41 ▶ my $backoff = ($cap && $cap->{retry_after} && _num($cap->{retry_after}) > $now) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$cap && $cap->{'retry_after'} && _num($cap->{'retry_after'}) > $now ? : 1 40 41
Condition83%5/6
$cap and $cap->{"retry_after"} and _num($cap->{"retry_after"}) > $now
!ll&&!rl&&r
4001
$cap and $cap->{"retry_after"}
!ll&&!rl&&r
3911
MC/DC66%2/3
$cap and $cap->{"retry_after"} and _num($cap->{"retry_after"}) > $now
$cap $cap->{"retry_after"} _num($cap->{"retry_after"}) > $now
Truth table75%3/4
A and B and C
A= $capB= $cap->{"retry_after"}C= _num($cap->{"retry_after"}) > $now
ABCresult
0XX0
10X0
1100
1111
74  
75 41 ▶ my $node_limit = $max > 0 ? $max : $cap_limit;
Branch: 1 branch
BranchTrueFalseTotal
$max > 0 ? : 40 1 41
76 41 ▶ if ($node_limit <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node_limit <= 0) 1 40 41
77 # Nothing bounds this node, so nothing bounds the downstream -- unless it is backing off,
78 # in which case it simply has no slots to offer.
79 1 ▶ next if $backoff;
Branch: 1 branch
BranchTrueFalseTotal
if $backoff 0 1 1
80 1 return { used => undef, limit => undef };
81 }
82  
83 40 ▶ my $node_free = $max > 0 ? $max - $inflight : $node_limit;
Branch: 1 branch
BranchTrueFalseTotal
$max > 0 ? : 40 0 40
84 40 ▶ if ($cap_limit > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cap_limit > 0) 1 39 40
85 1 my $cap_free = $cap_limit - _num($cap->{used});
86 1 ▶ $node_free = $cap_free if $cap_free < $node_free;
Branch: 1 branch
BranchTrueFalseTotal
if $cap_free < $node_free 1 0 1
87 }
88 40 ▶ $node_free = 0 if $backoff;
Branch: 1 branch
BranchTrueFalseTotal
if $backoff 1 39 40
89 40 ▶ $node_free = 0 if $node_free < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $node_free < 0 1 39 40
90 40 ▶ $node_free = $node_limit if $node_free > $node_limit;
Branch: 1 branch
BranchTrueFalseTotal
if $node_free > $node_limit 0 40 40
91  
92 40 $limit += $node_limit;
93 40 $free += $node_free;
94 }
95  
96 26 ▶ return { used => 1, limit => 1 } if !$selected || $limit <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if not $selected or $limit <= 0 1 25 26
Condition66%2/3
not $selected or $limit <= 0
l!l&&r!l&&!r
1025
MC/DC50%1/2
not $selected or $limit <= 0
not $selected $limit <= 0
Truth table66%2/3
not(A) or B
A= not $selectedB= $limit <= 0
ABresult
1X1
011
000
97 25 return { used => $limit - $free, limit => $limit };
98 }
99  
100 86 ▶sub _num {
Subroutine _num: called CC=4 SCAR=14.1
101 86 my ($value) = @_;
102 86 ▶ return 0 unless defined $value && !ref $value && $value =~ /\A-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value and $value =~ /\A-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?\z/ 1 85 86
Condition66%4/6
defined $value and not ref $value and $value =~ /\A-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?\z/
!ll&&!rl&&r
1085
defined $value and not ref $value
!ll&&!rl&&r
1085
MC/DC33%1/3
defined $value and not ref $value and $value =~ /\A-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?\z/
defined $value not ref $value $value =~ /\A-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?\z/
Truth table50%2/4
A and not(B) and C
A= defined $valueB= not ref $valueC= $value =~ /\A-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?\z/
ABCresult
0XX0
10X0
1100
1111
103 85 return 0 + $value;
104 }
105  
106  
107 87 1;
108  
109 __END__
« blib/lib/Langertha/Skeid/CapacityProbe/Registry.pm ↑ Summary blib/lib/Langertha/Skeid/UsageStore/DBI.pm »