Kubernetes/PortForwardSession.pm

filter: Statementstmt 100% 87 / 87 Branchbran 80.5% 58 / 72 Conditioncond 37.0% 10 / 27 MC/DCmcdc 6.6% 1 / 15 Subroutinesub 100% 14 / 14 Podpod 88.8% 8 / 9 totaltotal 79.4% 178 / 224 CC 47 SCAR 21.7
CC
47
Cov
83%
close
12.7
_port_events
12.0
resize
10.8
CRAP 8.7
« Kubernetes.pm ↑ Summary Kubernetes/Controller.pm »
1 package Net::Async::Kubernetes::PortForwardSession;
2 # ABSTRACT: Duplex websocket session for pod port-forward, exec and attach
3 54 our $VERSION = '0.012';
4 54 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 54 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 54 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 24 ▶sub new {
Subroutine new: called CC=7 SCAR=20.4
Pod documented
9 24 my ($class, %args) = @_;
10 24 ▶ croak "ws_client required" unless $args{ws_client};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"ws_client"} 0 24 24
11 24 ▶ if (defined $args{ports}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"ports"}) 20 4 24
12 croak "ports must be a non-empty array reference"
13 20 ▶ unless ref($args{ports}) eq 'ARRAY' && @{$args{ports}};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"ports"} eq "ARRAY" and @{$args{"ports"};} 0 20 20
Condition33%1/3
ref $args{"ports"} eq "ARRAY" and @{$args{"ports"};}
!ll&&!rl&&r
0020
MC/DC0%0/2
ref $args{"ports"} eq "ARRAY" and @{$args{"ports"};}
ref $args{"ports"} eq "ARRAY" @{$args{"ports"};}
Truth table33%1/3
A and B
A= ref $args{"ports"} eq "ARRAY"B= @{$args{"ports"};}
ABresult
0X0
100
111
14 20 $args{ports} = [@{$args{ports}}];
15 20 my $i = 0;
16 20 for my $port (@{$args{ports}}) {
17 26 ▶ $args{_port_index}{$port} //= $i;
Condition33%1/3
$args{"_port_index"}{$port} //= $i
l!l&&r!l&&!r
0260
MC/DC0%0/2
$args{"_port_index"}{$port} //= $i
$args{"_port_index"}{$port} $i
Truth table33%1/3
A //= B
A= $args{"_port_index"}{$port}B= $i
ABresult
1X1
011
000
18 26 $i++;
19 }
20 }
21 24 return bless \%args, $class;
22 }
23  
24  
25 44 ▶sub ws_client { $_[0]->{ws_client} }
Subroutine ws_client: called CC=1 SCAR=0.0
Pod undocumented
26  
27 40 ▶sub is_port_forward { $_[0]->{ports} ? 1 : 0 }
Subroutine is_port_forward: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'ports'} ? : 39 1 40
28  
29  
30 2 ▶sub ports { $_[0]->{ports} ? [@{$_[0]->{ports}}] : undef }
Subroutine ports: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'ports'} ? : 1 1 2
31  
32  
33 # The events one incoming frame of a port-forward session stands for, after
34 # the two-byte port preamble every channel starts with is checked and
35 # stripped: ['data', $port, $bytes], ['port_error', $port, $message] or
36 # ['error', $message] (what Net::Async::Kubernetes hands to on_data,
37 # on_port_error and on_error). A channel whose preamble was wrong delivers
38 # nothing further.
39 38 ▶sub _port_events {
Subroutine _port_events: called CC=12 SCAR=24.9
40 38 my ($self, $channel, $payload) = @_;
41 38 my @ports = @{$self->{ports}};
42  
43 38 ▶ return ['error', 'port-forward frame on channel ' . $channel
Branch: 1 branch
BranchTrueFalseTotal
if $channel >= 2 * @ports 1 37 38
44 . ' but ' . scalar(@ports) . ' port(s) requested use channels 0 to '
45 . (2 * @ports - 1)]
46 if $channel >= 2 * @ports;
47  
48 37 my $port = $ports[int($channel / 2)];
49 37 my $is_err = $channel % 2;
50 37 ▶ my $what = 'channel ' . $channel . ' (' . ($is_err ? 'error' : 'data')
Branch: 1 branch
BranchTrueFalseTotal
$is_err ? : 12 25 37
51 . ' of port ' . $port . ')';
52  
53 37 my $state = $self->{_channel_state}{$channel};
54 37 ▶ unless ($state) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state) 22 15 37
55 22 my $problem;
56 22 ▶ if (length($payload) != 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $payload != 2) { } 3 19 22
elsif ((my $got = unpack('v', $payload)) != $port) { } 2 17 19
57 3 ▶ $problem = length($payload) > 2
Branch: 1 branch
BranchTrueFalseTotal
length $payload > 2 ? : 2 1 3
58 ? 'first frame carries data after the port number'
59 : 'first frame is not a 2-byte port number';
60 3 $problem .= ' (' . length($payload) . ' bytes)';
61 } elsif ((my $got = unpack('v', $payload)) != $port) {
62 2 $problem = 'port preamble says ' . $got;
63 }
64 22 ▶ $self->{_channel_state}{$channel} = defined $problem ? 'broken' : 'open';
Branch: 1 branch
BranchTrueFalseTotal
defined $problem ? : 5 17 22
65 22 ▶ return defined $problem ? (['error', $what . ': ' . $problem]) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $problem ? : 5 17 22
66 }
67 15 ▶ return if $state eq 'broken';
Branch: 1 branch
BranchTrueFalseTotal
if $state eq "broken" 1 14 15
68 14 ▶ return unless length $payload;
Branch: 1 branch
BranchTrueFalseTotal
unless length $payload 0 14 14
69 14 ▶ return $is_err ? (['port_error', $port, $payload]) : (['data', $port, $payload]);
Branch: 1 branch
BranchTrueFalseTotal
$is_err ? : 3 11 14
70 }
71  
72 14 ▶sub write_port {
Subroutine write_port: called CC=4 SCAR=13.9
Pod documented
73 14 my ($self, $port, $payload) = @_;
74  
75 14 ▶ croak "write_port is only for port-forward sessions" unless $self->{ports};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ports"} 1 13 14
76 13 ▶ croak "port required for write_port" unless defined $port;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $port 1 12 13
77 12 my $i = $self->{_port_index}{$port};
78 12 ▶ croak "port '$port' is not forwarded by this session" unless defined $i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i 1 11 12
79  
80 11 return $self->write_channel(2 * $i, $payload);
81 }
82  
83  
84 15 ▶sub write_channel {
Subroutine write_channel: called CC=7 SCAR=23.2
Pod documented
85 15 my ($self, $channel, $payload) = @_;
86  
87 15 ▶ croak "channel required for write_channel" unless defined $channel;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $channel 0 15 15
88 15 ▶ croak "invalid channel '$channel' for write_channel"
Branch: 1 branch
BranchTrueFalseTotal
unless $channel =~ /^\d+$/ and $channel >= 0 and $channel <= 255 0 15 15
Condition33%2/6
$channel =~ /^\d+$/ and $channel >= 0 and $channel <= 255
!ll&&!rl&&r
0015
$channel =~ /^\d+$/ and $channel >= 0
!ll&&!rl&&r
0015
MC/DC0%0/3
$channel =~ /^\d+$/ and $channel >= 0 and $channel <= 255
$channel =~ /^\d+$/ $channel >= 0 $channel <= 255
Truth table25%1/4
A and B and C
A= $channel =~ /^\d+$/B= $channel >= 0C= $channel <= 255
ABCresult
0XX0
10X0
1100
1111
89 unless $channel =~ /^\d+$/ && $channel >= 0 && $channel <= 255;
90  
91 15 ▶ $payload = '' unless defined $payload;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $payload 0 15 15
92 15 ▶ croak "payload must be a plain string for write_channel" if ref($payload);
Branch: 1 branch
BranchTrueFalseTotal
if ref $payload 0 15 15
93  
94 15 return $self->ws_client->send_binary_frame(chr($channel) . $payload);
95 }
96  
97  
98 4 ▶sub write_stdin {
Subroutine write_stdin: called CC=2 SCAR=6.9
Pod documented
99 4 my ($self, $payload) = @_;
100 croak "write_stdin is not available on a port-forward session (use write_port)"
101 4 ▶ if $self->{ports};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ports"} 3 1 4
102 1 return $self->write_channel(0, $payload);
103 }
104  
105  
106 {
107 54 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
108 54 *write = \&write_channel;
109 54 *stdin = \&write_stdin;
110 }
111  
112 5 ▶sub resize {
Subroutine resize: called CC=10 SCAR=23.8
Pod documented
113 5 my ($self, %args) = @_;
114 5 ▶ croak "resize is not available on a port-forward session" if $self->{ports};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ports"} 2 3 5
115  
116 3 ▶ my $width = exists($args{width}) ? $args{width} : $args{cols};
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'width'} ? : 3 0 3
117 3 ▶ my $height = exists($args{height}) ? $args{height} : $args{rows};
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'height'} ? : 2 1 3
118  
119 3 ▶ croak "width required for resize" unless defined $width;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $width 0 3 3
120 3 ▶ croak "height required for resize" unless defined $height;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $height 1 2 3
121 2 ▶ croak "invalid width '$width' for resize"
Branch: 1 branch
BranchTrueFalseTotal
unless $width =~ /^\d+$/ and $width > 0 1 1 2
Condition66%2/3
$width =~ /^\d+$/ and $width > 0
!ll&&!rl&&r
011
MC/DC50%1/2
$width =~ /^\d+$/ and $width > 0
$width =~ /^\d+$/ $width > 0
Truth table66%2/3
A and B
A= $width =~ /^\d+$/B= $width > 0
ABresult
0X0
100
111
122 unless $width =~ /^\d+$/ && $width > 0;
123 1 ▶ croak "invalid height '$height' for resize"
Branch: 1 branch
BranchTrueFalseTotal
unless $height =~ /^\d+$/ and $height > 0 0 1 1
Condition33%1/3
$height =~ /^\d+$/ and $height > 0
!ll&&!rl&&r
001
MC/DC0%0/2
$height =~ /^\d+$/ and $height > 0
$height =~ /^\d+$/ $height > 0
Truth table33%1/3
A and B
A= $height =~ /^\d+$/B= $height > 0
ABresult
0X0
100
111
124 unless $height =~ /^\d+$/ && $height > 0;
125  
126 1 my $payload = sprintf('{"Width":%d,"Height":%d}', $width, $height);
127 1 return $self->write_channel(4, $payload);
128 }
129  
130  
131 9 ▶sub close {
Subroutine close: called CC=9 SCAR=25.4
Pod documented
132 9 my ($self, %args) = @_;
133 9 my $code = $args{code};
134 9 ▶ my $payload = exists $args{payload} ? $args{payload} : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'payload'} ? : 1 8 9
135  
136 9 ▶ croak "payload must be a plain string for close" if ref($payload);
Branch: 1 branch
BranchTrueFalseTotal
if ref $payload 0 9 9
137 9 ▶ croak "invalid websocket close code '$code'"
Branch: 1 branch
BranchTrueFalseTotal
if defined $code and !($code =~ /^\d+$/) || $code < 1000 || $code > 4999 0 9 9
Condition33%3/9
defined $code and !($code =~ /^\d+$/) || $code < 1000 || $code > 4999
!ll&&!rl&&r
090
not $code =~ /^\d+$/ or $code < 1000 or $code > 4999
l!l&&r!l&&!r
009
not $code =~ /^\d+$/ or $code < 1000
l!l&&r!l&&!r
009
MC/DC0%0/4
defined $code and !($code =~ /^\d+$/) || $code < 1000 || $code > 4999
defined $code not $code =~ /^\d+$/ $code < 1000 $code > 4999
Truth table20%1/5
A and not(B) or C or D
A= defined $codeB= not $code =~ /^\d+$/C= $code < 1000D= $code > 4999
ABCDresult
0XXX0
10000
11XX1
101X1
10011
138 if defined($code) && ($code !~ /^\d+$/ || $code < 1000 || $code > 4999);
139  
140 9 ▶ my $close_payload = defined($code) ? pack('n', $code) . $payload : $payload;
Branch: 1 branch
BranchTrueFalseTotal
defined $code ? : 9 0 9
141 9 my $ret = $self->ws_client->send_close_frame($close_payload);
142 9 ▶ $self->ws_client->close_when_empty if $self->ws_client->can('close_when_empty');
Branch: 1 branch
BranchTrueFalseTotal
if $self->ws_client->can("close_when_empty") 9 0 9
143 9 return $ret;
144 }
145  
146  
147 54 1;
148  
149 __END__
« Kubernetes.pm ↑ Summary Kubernetes/Controller.pm »