Remote/Config.pm

filter: Statementstmt 80.0% 56 / 70 Branchbran 46.4% 13 / 28 Conditioncond 25.0% 2 / 8 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 69.3% 86 / 124 CC 17 SCAR 20.6
CC
17
Cov
67%
gethostname
13.0
get_local_data
10.6
get_tapper_host
8.6
CRAP 7.8
« Remote/Net.pm ↑ Summary Remote.pm »
1 package Tapper::Remote::Config;
2 3 our $AUTHORITY = 'cpan:TAPPER';
3 3 $Tapper::Remote::Config::VERSION = '5.0.0';
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Net::TFTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Socket; # for inet_aton and gethostbyname
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use YAML::Syck;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15  
16  
17  
18 sub get_tapper_host
19 1 ▶{
Subroutine get_tapper_host: called CC=5 SCAR=21.5
Pod documented
20 1 my ($host, $port);
21 # try kernel command line
22 1 open my $FH,'<','/proc/cmdline';
23 1 my $cmd_line = <$FH>;
24 1 close $FH;
25 1 ($host,undef,$port) = $cmd_line =~ m/tapper_host\s*=\s*(\w+)(:(\d+))?/;
26 1 ▶ return($host,$port) if $host;
Branch: 1 branch
BranchTrueFalseTotal
if $host 0 1 1
27  
28 1 ▶ if (my ($ip) = $cmd_line =~ m/tapper_ip\s*=\s*(\S+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($ip) = $cmd_line =~ /tapper_ip\s*=\s*(\S+)/) 0 1 1
29 0 my $iaddr = inet_aton($ip);
30 0 $host = gethostbyaddr($iaddr, AF_INET);
31  
32 0 ($port) = $cmd_line =~ m/tapper_port\s*=\s*(\d+)/;
33 0 ▶ return($host,$port) if $host;
Branch: 1 branch
BranchTrueFalseTotal
if $host 0 0 0
34 }
35  
36  
37 # try %ENV
38 1 ▶ if ($ENV{TAPPER_MCP_SERVER}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"TAPPER_MCP_SERVER"}) 0 1 1
39 0 $host = $ENV{TAPPER_MCP_SERVER};
40 0 $port = $ENV{TAPPER_MCP_PORT};
41 0 return ($host, $port);
42 }
43  
44 # try multicast
45 }
46  
47  
48  
49 sub gethostname
50 2 ▶{
Subroutine gethostname: called CC=5 SCAR=25.7
Pod documented
51 2 my ($self) = @_;
52 2 my $hostname = Sys::Hostname::hostname();
53 2 ▶ if ($hostname =~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hostname =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) { } 0 2 2
elsif ($hostname =~ /^([^\.]+)\./) { } 0 2 2
54 0 ▶ ($hostname) = gethostbyaddr(inet_aton($hostname), AF_INET) or ( print("Can't get hostname: $!") and exit 1);
Branch: 1 branch
BranchTrueFalseTotal
($hostname) = gethostbyaddr(inet_aton($hostname), 2) or print "Can't get hostname: $!" and exit 1 0 0 0
Condition0%0/5
(($hostname) = gethostbyaddr(inet_aton($hostname), 2)) || print("Can't get hostname: $!") && exit 1
l!l&&r!l&&!r
000
print "Can't get hostname: $!" and exit 1
!ll
00
MC/DC0%0/2
(($hostname) = gethostbyaddr(inet_aton($hostname), 2)) || print("Can't get hostname: $!") && exit 1
(($hostname) = gethostbyaddr(inet_aton($hostname), 2)) print "Can't get hostname: $!"
Truth table0%0/3
A || B
A= (($hostname) = gethostbyaddr(inet_aton($hostname), 2))B= print "Can't get hostname: $!"
ABresult
1X1
011
000
55 0 $hostname =~ s/^(\w+?)\..+$/$1/;
56 0 system("hostname", "$hostname");
57 } elsif ($hostname =~ m/^([^\.]+)\./) {
58 0 $hostname = $1;
59 }
60 2 return $hostname;
61 }
62  
63  
64  
65  
66 sub get_local_data
67 2 ▶{
Subroutine get_local_data: called CC=9 SCAR=23.6
Pod documented
68 2 my ($self, $state) = @_;
69 # logger will usually be initialised by caller
70 2 my $tmpcfg={};
71  
72 2 my $config_file_name = '/etc/tapper';
73 2 ▶ $config_file_name = $ENV{TAPPER_CONFIG} if $ENV{TAPPER_CONFIG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TAPPER_CONFIG"} 0 2 2
74  
75 2 my $hostname = $self->gethostname();
76  
77  
78 2 my ($server, $port, $help);
79 2 Getopt::Long::GetOptions("host=s" => \$server,
80 "port=s" => \$port,
81 "config=s" => \$config_file_name,
82 "help|h" => \$help,);
83 2 ▶ die "Usage: $0 [--host=host --port=port --config=file]\n" if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 2 2
84  
85 2 ▶ if ($state eq 'install' or (not -e $config_file_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "install" or not -e $config_file_name) 1 1 2
Condition66%2/3
$state eq "install" or not -e $config_file_name
l!l&&r!l&&!r
101
MC/DC50%1/2
$state eq "install" or not -e $config_file_name
$state eq "install" not -e $config_file_name
Truth table66%2/3
A or not(B)
A= $state eq "install"B= not -e $config_file_name
ABresult
1X1
011
000
86 1 ▶ ($server, $port) = $self->get_tapper_host() if not $server;
Branch: 1 branch
BranchTrueFalseTotal
unless $server 1 0 1
87 1 my $tftp = Net::TFTP->new($server);
88 1 ▶ $tftp->get("$hostname-$state", $config_file_name) or return("Can't get local data.",$tftp->error);
Branch: 1 branch
BranchTrueFalseTotal
$tftp->get("$hostname-$state", $config_file_name) or return "Can't get local data.", $tftp->error 0 1 1
89 0 $tmpcfg->{server} = $server;
90 0 $tmpcfg->{port} = $port;
91 0 $tmpcfg->{hostname} = $hostname;
92 }
93  
94 1 ▶ my $config = YAML::Syck::LoadFile($config_file_name) or return ("Can't parse config received from server");
Branch: 1 branch
BranchTrueFalseTotal
my $config = YAML::Syck::LoadFile($config_file_name) or return "Can't parse config received from server" 1 0 1
95 1 $config->{filename} = $config_file_name;
96 1 ▶ $config->{hostname} = $hostname unless $config->{hostname};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"hostname"} 0 1 1
97 1 %$config=(%$config, %$tmpcfg);
98  
99 1 return $config;
100 }
101  
102 3 1;
103  
104 __END__
« Remote/Net.pm ↑ Summary Remote.pm »