bin/racktables-check

filter: Statementstmt 24.8% 50 / 201 Branchbran 0.8% 1 / 122 Conditioncond 0.0% 0 / 74 MC/DCmcdc 0.0% 0 / 35 Subroutinesub 73.6% 14 / 19 Podpod n/a totaltotal 14.4% 65 / 451 CC 91 SCAR 82.8
CC
91
Cov
13%
process
5700.0
property_value
72.0
run
56.0
CRAP 3931.8
↑ Summary bin/cfengine-tags »
1 #!/usr/bin/env perl
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use List::MoreUtils qw< uniq >;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use RackMan;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use RackMan::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use RackMan::Tasks;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use RackMan::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 1 $::PROGRAM = "RackTables consistency check";
18 1 $::VERSION = "1.02";
19 1 $::COMMAND = basename($0);
20  
21  
22 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 CONFIG_SECTION => "racktables-check",
24  
25 OP_EQUAL => "=",
26 OP_REGEXP => "~",
27 OP_FUNCTION => "%",
28  
29 PROP_ATTRIBUTE => "attribute",
30 PROP_GATEWAY => "default_ipv4_gateway",
31 PROP_INTERFACES => "interfaces",
32 PROP_IPV4ADDRS => "regular_ipv4addrs",
33 PROP_MACADDRS => "regular_mac_addrs",
34 1 };
35  
36 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 RE_ALL_OPS => join("|", OP_EQUAL, OP_REGEXP, OP_FUNCTION),
38 1 };
39  
40 1 my %alias = (
41 addresses => PROP_IPV4ADDRS,
42 addrs => PROP_IPV4ADDRS,
43 attr => PROP_ATTRIBUTE,
44 gateway => PROP_GATEWAY,
45 interfaces => PROP_INTERFACES,
46 ipv4_addrs => PROP_IPV4ADDRS,
47 mac_addrs => PROP_MACADDRS,
48 tag => "explicit_tags",
49 type => "object_type",
50 );
51  
52 1 my %check_func = (
53 check_dns => \&check_dns,
54 );
55  
56  
57 #
58 # main
59 #
60 MAIN: {
61 1 ▶ if (not caller()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller()) 0 1 1
62 0 run();
63 0 exit RackMan->status;
64 }
65 }
66  
67  
68 #
69 # run()
70 # ---
71 0 ▶sub run {
Subroutine run: not called CC=7 SCAR=40.3
72 # detect if stdout is connected to a terminal
73 0 ▶ (-t STDOUT ? $Term::ANSIColor::AUTORESET : $ENV{ANSI_COLORS_DISABLED}) = 1;
Branch: 1 branch
BranchTrueFalseTotal
-t STDOUT ? : 0 0 0
74 0 $|++;
75  
76 # default options
77 0 my %options = (
78 config => "/usr/local/etc/rack.conf",
79 format => "yaml",
80 );
81  
82 # parse options
83 0 Getopt::Long::Configure(qw< no_auto_abbrev no_ignore_case >);
84 0 ▶ GetOptions(\%options, qw{
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(\%options, ("help|usage|h!", "man!", "version|V!", "verbose|v!", "config|c=s", "filter|F=s", "output|o=s", "format|t=s")) or pod2usage(0) 0 0 0
85 help|usage|h! man! version|V!
86 verbose|v! config|c=s filter|F=s output|o=s format|t=s
87 }) or pod2usage(0);
88  
89 # handle --version, --help and --man
90 0 ▶ $options{man} and pod2usage(2);
Branch: 1 branch
BranchTrueFalseTotal
$options{"man"} and pod2usage(2) 0 0 0
91 0 ▶ $options{help} and pod2usage(1);
Branch: 1 branch
BranchTrueFalseTotal
$options{"help"} and pod2usage(1) 0 0 0
92 0 ▶ $options{version} and print "$::PROGRAM v$::VERSION\n" and exit;
Branch: 1 branch
BranchTrueFalseTotal
$options{"version"} and print "$main::PROGRAM v$main::VERSION\n" and exit 0 0 0
Condition0%0/3
$options{"version"} and print "$main::PROGRAM v$main::VERSION\n"
!ll&&!rl&&r
000
MC/DC0%0/2
$options{"version"} and print "$main::PROGRAM v$main::VERSION\n"
$options{"version"} print "$main::PROGRAM v$main::VERSION\n"
Truth table0%0/3
A and B
A= $options{"version"}B= print "$main::PROGRAM v$main::VERSION\n"
ABresult
0X0
100
111
93  
94 # read configuration file
95 0 my $config = RackMan::Config->new(-file => $options{config});
96  
97 # instanciate the backend object
98 0 my $rackman = RackMan->new({ options => \%options, config => $config });
99  
100 # do the actual work
101 0 process($rackman, @ARGV);
102 }
103  
104  
105 #
106 # pod2usage()
107 # ---------
108 0 ▶sub pod2usage {
Subroutine pod2usage: not called CC=1 SCAR=6.9
109 0 my ($n) = @_;
110 0 require Pod::Usage;
111 0 Pod::Usage::pod2usage({ -exitval => 0, -verbose => $n, -noperldoc => 1 });
112 }
113  
114  
115 #
116 # process()
117 # -------
118 0 ▶sub process {
Subroutine process: not called CC=75 SCAR=86.5
119 0 my ($rackman, @args) = @_;
120  
121 0 my @results;
122 my $n;
123  
124 # parse the rules
125 0 my @rule_defs = $rackman->config->val(CONFIG_SECTION, "rule");
126 0 my @rules;
127  
128 0 for my $rule_def (@rule_defs) {
129 0 ▶ next if not defined $rule_def or $rule_def =~ /^\s+$/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $rule_def or $rule_def =~ /^\s+$/ 0 0 0
Condition0%0/3
not defined $rule_def or $rule_def =~ /^\s+$/
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $rule_def or $rule_def =~ /^\s+$/
not defined $rule_def $rule_def =~ /^\s+$/
Truth table0%0/3
not(A) or B
A= not defined $rule_defB= $rule_def =~ /^\s+$/
ABresult
1X1
011
000
130 0 my $rule = $rule_def;
131 0 my (%enforce, %selector);
132  
133 # extract an optional selector
134 0 ▶ if ($rule =~ s/\s*\[\s*([^\]]+)\s*\]\s*//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule =~ s/\s*\[\s*([^\]]+)\s*\]\s*//) 0 0 0
135 0 my ($prop, $value) = split / *= */, $1, 2;
136 0 my @values = split /\|/, $value;
137 0 ▶ $prop = ":$prop" unless index($prop, ":") >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless index($prop, ":") != -1 0 0 0
138 0 my ($type, $name) = split /:/, $prop;
139 %selector = (
140 proptype => $alias{$type} || $type || "",
141 propname => $alias{$name} || $name,
142 0 ▶ propval => { map { $_ => 1 } @values },
Condition0%0/8
$alias{$type} || $type || ''
l!l
00
$alias{$type} or $type
l!l&&r!l&&!r
000
$alias{$name} || $name
l!l&&r!l&&!r
000
MC/DC0%0/2
$alias{$type} || $type || ''
$alias{$type} $type
MC/DC0%0/2
$alias{$name} || $name
$alias{$name} $name
Truth table0%0/3
A or B
A= $alias{$type}B= $type
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $alias{$name}B= $name
ABresult
1X1
011
000
143 );
144 }
145  
146 # extract an optional operator and second operand
147 0 ▶ if ($rule =~ s/\s*(@{[RE_ALL_OPS]})\s*(.+)$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule =~ s/\s*(@{["=|~|%"];})\s*(.+)$//) 0 0 0
148 0 ($enforce{operator}, $enforce{operand}) = ($1, $2);
149  
150 # compile the regexp to detect errors early
151 0 ▶ if ($enforce{operator} eq OP_REGEXP) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enforce{"operator"} eq "~") 0 0 0
152 0 ($enforce{operand}, my $err) = parse_regexp($enforce{operand});
153 0 ▶ RackMan->error("invalid regexp: $err") if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
154 }
155  
156 # verify that the check function is actually known
157 0 ▶ if ($enforce{operator} eq OP_FUNCTION) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enforce{"operator"} eq "%") 0 0 0
158 RackMan->error("unknown check function '$enforce{operand}'")
159 0 ▶ unless exists $check_func{$enforce{operand}};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $check_func{$enforce{"operand"}} 0 0 0
160 }
161 }
162  
163 # what is left should be the property to check
164 0 s/^\s+//g, s/\s+$//g for $rule;
165 0 ▶ $rule = ":$rule" unless index($rule, ":") >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless index($rule, ":") != -1 0 0 0
166 0 my ($type, $name) = split /:/, $rule;
167 0 ▶ $enforce{proptype} = $alias{$type} || $type || "";
Condition0%0/5
$alias{$type} || $type || ''
l!l
00
$alias{$type} or $type
l!l&&r!l&&!r
000
MC/DC0%0/2
$alias{$type} || $type || ''
$alias{$type} $type
Truth table0%0/3
A or B
A= $alias{$type}B= $type
ABresult
1X1
011
000
168 0 ▶ $enforce{propname} = $alias{$name} || $name;
Condition0%0/3
$alias{$name} || $name
l!l&&r!l&&!r
000
MC/DC0%0/2
$alias{$name} || $name
$alias{$name} $name
Truth table0%0/3
A || B
A= $alias{$name}B= $name
ABresult
1X1
011
000
169  
170 0 push @rules, {
171 string => $rule_def,
172 enforce => \%enforce,
173 (selector => \%selector)x!! keys %selector,
174 };
175 }
176  
177 # construct the filter
178 0 my %filter;
179 my $filter = $rackman->options->{filter}
180 0 ▶ || $rackman->config->val(CONFIG_SECTION, "filter", "");
Condition0%0/3
$rackman->options->{'filter'} || $rackman->config->val('racktables-check', 'filter', '')
l!l&&r!l&&!r
000
MC/DC0%0/2
$rackman->options->{'filter'} || $rackman->config->val('racktables-check', 'filter', '')
$rackman->options->{'filter'} $rackman->config->val('racktables-check', 'filter', '')
Truth table0%0/3
A || B
A= $rackman->options->{'filter'}B= $rackman->config->val('racktables-check', 'filter', '')
ABresult
1X1
011
000
181 0 ▶ if ($filter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter) 0 0 0
182 0 for my $token (split / *, */, $filter) {
183 0 ▶ if (index($token, "tag:") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($token, "tag:") == 0) 0 0 0
184 0 $filter{tag}{ lc substr($token, 4) } = 1;
185 next
186 0 }
187  
188 0 ▶ if (index($token, "=") > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($token, "=") > 0) 0 0 0
189 0 my ($name, $value) = split /=/, $token;
190 0 push @{ $filter{attr}{$name} }, $value;
191 next
192 0 }
193 }
194 }
195  
196 0 my $enforce_interface_match
197 = $rackman->config->val(CONFIG_SECTION, "enforce_interface_match");
198  
199 # fetch the list of devices
200 0 ▶ my @devices = @args
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 0 0 0
201 ? map +{ name => $_ }, @args
202 : RackMan::Tasks->task_list({ rackman => $rackman, type => "all" });
203  
204 DEVICE:
205 0 for my $device (@devices) {
206 0 ▶ next if $device->{name} eq "(null)";
Branch: 1 branch
BranchTrueFalseTotal
if $device->{"name"} eq "(null)" 0 0 0
207 my $rackdev = exists $device->{id}
208 ? $rackman->device_by_id($device->{id})
209 0 ▶ : $rackman->device_by_name($device->{name});
Branch: 1 branch
BranchTrueFalseTotal
exists $device->{'id'} ? : 0 0 0
210 0 my $type = $rackdev->object_type;
211 0 my @problems;
212  
213 # fetch the tags for this RackObject, filter them if needed
214 0 my @tags = map { s/\W+/_/g; $_ } map lc,
215 0 @{ $rackdev->explicit_tags },
216 0 @{ $rackdev->implicit_tags };
217 0 ▶ next unless @tags;
Branch: 1 branch
BranchTrueFalseTotal
unless @tags 0 0 0
218  
219 # fetch the attributes of this RackObject
220 0 my %attr = %{ $rackdev->attributes };
221  
222 # filter out devices without the requested tags or attributes
223 0 ▶ if ($filter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter) 0 0 0
224 # filter on tags
225 0 ▶ if (exists $filter{tag}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $filter{"tag"}) 0 0 0
226 0 ▶ next unless grep $filter{tag}{$_}, @tags;
Branch: 1 branch
BranchTrueFalseTotal
unless grep $filter{"tag"}{$_}, @tags 0 0 0
227 }
228  
229 # filter on attributes
230 0 ▶ if (exists $filter{attr}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $filter{"attr"}) 0 0 0
231 0 my $keep = 0;
232  
233 ATTR:
234 0 for my $name (keys %{ $filter{attr} }) {
235 0 for my $value (@{ $filter{attr}{$name} }) {
236 0 ▶ next unless exists $attr{$name} and defined $attr{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attr{$name} and defined $attr{$name} 0 0 0
Condition0%0/3
exists $attr{$name} and defined $attr{$name}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $attr{$name} and defined $attr{$name}
exists $attr{$name} defined $attr{$name}
Truth table0%0/3
A and B
A= exists $attr{$name}B= defined $attr{$name}
ABresult
0X0
100
111
237 0 ▶ $keep ||= $value eq $attr{$name};
Condition0%0/3
$keep ||= $value eq $attr{$name}
l!l&&r!l&&!r
000
MC/DC0%0/2
$keep ||= $value eq $attr{$name}
$keep $value eq $attr{$name}
Truth table0%0/3
A ||= B
A= $keepB= $value eq $attr{$name}
ABresult
1X1
011
000
238 0 ▶ last ATTR if $keep;
Branch: 1 branch
BranchTrueFalseTotal
if $keep 0 0 0
239 }
240 }
241  
242 0 ▶ next unless $keep;
Branch: 1 branch
BranchTrueFalseTotal
unless $keep 0 0 0
243 }
244 }
245  
246 RULE:
247 0 for my $rule (@rules) {
248 # apply selector filter, if any
249 0 ▶ if (exists $rule->{selector}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rule->{"selector"}) 0 0 0
250 0 ▶ my $propval = property_value($rackdev, $rule->{selector}) || "";
Condition0%0/2
property_value($rackdev, $rule->{'selector'}) || ''
l!l
00
MC/DC0%0/1
property_value($rackdev, $rule->{'selector'}) || ''
property_value($rackdev, $rule->{'selector'})
Truth table0%0/2
A
A= property_value($rackdev, $rule->{'selector'})
Aresult
11
00
251 0 ▶ next RULE unless $rule->{selector}{propval}{$propval};
Branch: 1 branch
BranchTrueFalseTotal
unless $rule->{"selector"}{"propval"}{$propval} 0 0 0
252 }
253  
254 # fetch the property values
255 0 my $enforce = $rule->{enforce};
256 0 my $propname = $enforce->{propname};
257 0 my @propvals = property_value($rackdev, $enforce);
258  
259 VALUE:
260 0 for my $propval (@propvals) {
261 # check if the property is defined
262 0 ▶ push @problems, "property '$propname' not defined"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $propval 0 0 0
Condition0%0/5
defined $propval || push(@problems, "property '${propname}' not defined") && (next RULE)
l!l&&r!l&&!r
000
push @problems, "property '${propname}' not defined" and next RULE
!ll
00
MC/DC0%0/2
defined $propval || push(@problems, "property '${propname}' not defined") && (next RULE)
defined $propval push @problems, "property '${propname}' not defined"
Truth table0%0/3
A || B
A= defined $propvalB= push @problems, "property '${propname}' not defined"
ABresult
1X1
011
000
263 and next RULE unless defined $propval;
264  
265 # if there is an operation to apply, do it
266 0 ▶ if (exists $enforce->{operator}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $enforce->{"operator"}) 0 0 0
267 0 my $op = $enforce->{operator};
268 0 my $val = $enforce->{operand};
269  
270 0 ▶ if ($op eq OP_EQUAL) {
Branch: 3 branches
BranchTrueFalseTotal
if ($op eq '=') { } 0 0 0
elsif ($op eq '~') { } 0 0 0
elsif ($op eq '%') { } 0 0 0
271 0 ▶ push @problems, "property '$propname' value '$propval'"
Branch: 1 branch
BranchTrueFalseTotal
unless $propval eq $val 0 0 0
Condition0%0/5
$propval eq $val || push(@problems, "property '${propname}' value '${propval}'" . " != '${val}'") && (next)
l!l&&r!l&&!r
000
push @problems, "property '${propname}' value '${propval}'" . " != '${val}'" and next
!ll
00
MC/DC0%0/2
$propval eq $val || push(@problems, "property '${propname}' value '${propval}'" . " != '${val}'") && (next)
$propval eq $val push @problems, "property '${propname}' value '${propval}'" . " != '${val}'"
Truth table0%0/3
A || B
A= $propval eq $valB= push @problems, "property '${propname}' value '${propval}'" . " != '${val}'"
ABresult
1X1
011
000
272 . " != '$val'"
273 and next unless $propval eq $val;
274 }
275 elsif ($op eq OP_REGEXP) {
276 0 ▶ push @problems, "property '$propname' value '$propval'"
Branch: 1 branch
BranchTrueFalseTotal
unless $propval =~ /$val/ 0 0 0
Condition0%0/5
$propval =~ /$val/ || push(@problems, "property '${propname}' value '${propval}'" . " does not match $val") && (next)
l!l&&r!l&&!r
000
push @problems, "property '${propname}' value '${propval}'" . " does not match $val" and next
!ll
00
MC/DC0%0/2
$propval =~ /$val/ || push(@problems, "property '${propname}' value '${propval}'" . " does not match $val") && (next)
$propval =~ /$val/ push @problems, "property '${propname}' value '${propval}'" . " does not match $val"
Truth table0%0/3
A || B
A= $propval =~ /$val/B= push @problems, "property '${propname}' value '${propval}'" . " does not match $val"
ABresult
1X1
011
000
277 . " does not match $val"
278 and next unless $propval =~ /$val/;
279 }
280 elsif ($op eq OP_FUNCTION) {
281 # $val contains the function name
282 0 my @errs = $check_func{$val}->($propval);
283 0 ▶ push @problems, map "property '$propname' $_", @errs
Branch: 1 branch
BranchTrueFalseTotal
if @errs 0 0 0
Condition0%0/5
@errs && (push(@problems, map(("property '${propname}' $_"), @errs)) && (next))
!ll&&!rl&&r
000
push @problems, map(("property '${propname}' $_"), @errs) and next
!ll
00
MC/DC0%0/2
@errs && (push(@problems, map(("property '${propname}' $_"), @errs)) && (next))
@errs push @problems, map(("property '${propname}' $_"), @errs)
Truth table0%0/3
A && B
A= @errsB= push @problems, map(("property '${propname}' $_"), @errs)
ABresult
0X0
100
111
284 and next if @errs;
285 }
286 }
287 }
288 }
289  
290 # specific rule: check if each network interface is associated
291 # (at least, logically) with a port of the same name
292 0 ▶ if ($enforce_interface_match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enforce_interface_match) 0 0 0
293 0 my %port = map { $_->{name} => $_ } @{ $rackdev->ports };
294  
295 0 for my $interface ($rackdev->physical_interfaces) {
296 0 my $name = $interface->{iface};
297  
298 push @problems,
299 "interface '$name' has no associated port"
300 0 ▶ and next unless exists $port{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $port{$name} 0 0 0
Condition0%0/5
exists $port{$name} || push(@problems, "interface '${name}' has no associated port") && (next)
l!l&&r!l&&!r
000
push @problems, "interface '${name}' has no associated port" and next
!ll
00
MC/DC0%0/2
exists $port{$name} || push(@problems, "interface '${name}' has no associated port") && (next)
exists $port{$name} push @problems, "interface '${name}' has no associated port"
Truth table0%0/3
A || B
A= exists $port{$name}B= push @problems, "interface '${name}' has no associated port"
ABresult
1X1
011
000
301  
302 push @problems,
303 "interface '$name' associated port has no MAC address"
304 and next unless defined $port{$name}{l2address}
305 0 ▶ and $port{$name}{l2address} =~ /^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $port{$name}{"l2address"} and $port{$name}{"l2address"} =~ /^\w+$/ 0 0 0
Condition0%0/8
defined $port{$name}{'l2address'} && $port{$name}{'l2address'} =~ /^\w+$/ || push(@problems, "interface '${name}' associated port has no MAC address") && (next)
l!l&&r!l&&!r
000
defined $port{$name}{"l2address"} and $port{$name}{"l2address"} =~ /^\w+$/
!ll&&!rl&&r
000
push @problems, "interface '${name}' associated port has no MAC address" and next
!ll
00
MC/DC0%0/3
defined $port{$name}{'l2address'} && $port{$name}{'l2address'} =~ /^\w+$/ || push(@problems, "interface '${name}' associated port has no MAC address") && (next)
defined $port{$name}{"l2address"} $port{$name}{"l2address"} =~ /^\w+$/ push @problems, "interface '${name}' associated port has no MAC address"
Truth table0%0/5
A and B || C
A= defined $port{$name}{"l2address"}B= $port{$name}{"l2address"} =~ /^\w+$/C= push @problems, "interface '${name}' associated port has no MAC address"
ABCresult
11X1
0X11
1011
0X00
1000
306  
307 push @problems,
308 "interface '$name' associated port is not linked"
309 and next unless defined $port{$name}{peer_port_id}
310 0 ▶ and $port{$name}{peer_port_id} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $port{$name}{"peer_port_id"} and $port{$name}{"peer_port_id"} > 0 0 0 0
Condition0%0/8
defined $port{$name}{'peer_port_id'} && $port{$name}{'peer_port_id'} > 0 || push(@problems, "interface '${name}' associated port is not linked") && (next)
l!l&&r!l&&!r
000
defined $port{$name}{"peer_port_id"} and $port{$name}{"peer_port_id"} > 0
!ll&&!rl&&r
000
push @problems, "interface '${name}' associated port is not linked" and next
!ll
00
MC/DC0%0/3
defined $port{$name}{'peer_port_id'} && $port{$name}{'peer_port_id'} > 0 || push(@problems, "interface '${name}' associated port is not linked") && (next)
defined $port{$name}{"peer_port_id"} $port{$name}{"peer_port_id"} > 0 push @problems, "interface '${name}' associated port is not linked"
Truth table0%0/5
A and B || C
A= defined $port{$name}{"peer_port_id"}B= $port{$name}{"peer_port_id"} > 0C= push @problems, "interface '${name}' associated port is not linked"
ABCresult
11X1
0X11
1011
0X00
1000
311 }
312 }
313  
314 0 $n += @problems;
315 push @results, {
316 name => $device->{name},
317 type => $device->{type},
318 0 ▶ problems => \@problems,
Branch: 1 branch
BranchTrueFalseTotal
if @problems 0 0 0
319 } if @problems;
320 }
321  
322 # print the results
323 0 ▶ if ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) { } 0 0 0
324 0 my $out;
325 0 my $format = $rackman->options->{format};
326 0 my $path = $rackman->options->{output};
327  
328 0 ▶ if ($format eq "yaml") {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'yaml') { } 0 0 0
elsif ($format eq 'json') { } 0 0 0
329 0 ▶ eval "use YAML::XS; 1" or RackMan->error("can't load YAML::XS");
Branch: 1 branch
BranchTrueFalseTotal
eval "use YAML::XS; 1" or "RackMan"->error("can't load YAML::XS") 0 0 0
330 0 $out = YAML::XS::Dump(\@results);
331 }
332 elsif ($format eq "json") {
333 0 ▶ eval "use JSON::XS; 1" or RackMan->error("can't load JSON::XS");
Branch: 1 branch
BranchTrueFalseTotal
eval "use JSON::XS; 1" or "RackMan"->error("can't load JSON::XS") 0 0 0
334 0 $out = encode_json(\@results);
335 }
336  
337 0 ▶ if ($path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path) { } 0 0 0
338 0 print "$n problems detected.\n";
339 0 ▶ open my $fh, ">", $path
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $path or "RackMan"->error("can't write to ${path}: $!") 0 0 0
340 or RackMan->error("can't write to $path: $!");
341 0 print {$fh} $out;
342 0 close $fh;
343 }
344 else {
345 0 print $out;
346 }
347 }
348 else {
349 0 print "You have been well trained, my young apprentice.\n",
350 "They will be no match for you.\n"
351 }
352 }
353  
354  
355 #
356 # property_value()
357 # --------------
358 0 ▶sub property_value {
Subroutine property_value: not called CC=8 SCAR=42.8
359 0 my ($rackdev, $prop) = @_;
360 0 my $proptype = $prop->{proptype};
361 0 my $propname = $prop->{propname};
362  
363 0 ▶ if ($proptype eq PROP_ATTRIBUTE) {
Branch: 2 branches
BranchTrueFalseTotal
if ($proptype eq 'attribute') { } 0 0 0
elsif (defined $propname) { } 0 0 0
364 0 return $rackdev->attributes->{$propname}
365 }
366 elsif (defined $propname) {
367 0 ▶ if ($propname eq PROP_GATEWAY) {
Branch: 5 branches
BranchTrueFalseTotal
if ($propname eq 'default_ipv4_gateway') { } 0 0 0
elsif ($propname eq 'interfaces') { } 0 0 0
elsif ($propname eq 'regular_ipv4addrs') { } 0 0 0
elsif ($propname eq 'regular_mac_addrs') { } 0 0 0
elsif ($rackdev->can($propname)) { } 0 0 0
368 return $rackdev->$propname->{addr}
369 0 }
370 elsif ($propname eq PROP_INTERFACES) {
371 0 return map $_->{iface}, $rackdev->physical_interfaces
372 }
373 elsif ($propname eq PROP_IPV4ADDRS) {
374 0 return map $_->{addr}, $rackdev->$propname
375 }
376 elsif ($propname eq PROP_MACADDRS) {
377 0 return map $_->{l2address_text}, $rackdev->$propname
378 }
379 elsif ($rackdev->can($propname)) {
380 0 return $rackdev->$propname
381 }
382 else {
383 0 RackMan->error("unknown property '$propname'");
384 }
385 }
386 else {
387 0 RackMan->error("empty property name");
388 }
389 }
390  
391  
392 #
393 # check_dns()
394 # ---------
395 0 ▶sub check_dns {
Subroutine check_dns: not called CC=4 SCAR=30.0
396 0 my ($name) = @_;
397  
398 # try to resolve the given name
399 0 my (undef, undef, $addrtype, $length, @addrs) = gethostbyname($name);
400 0 ▶ return "- '$name' can't be resolved" unless @addrs;
Branch: 1 branch
BranchTrueFalseTotal
unless @addrs 0 0 0
401  
402 # try to reverse resolve the address
403 0 my $fqdn = gethostbyaddr($addrs[0], $addrtype);
404 0 my $addr_str = inet_ntoa($addrs[0]);
405 0 ▶ return "- $addr_str has no PTR" unless $fqdn;
Branch: 1 branch
BranchTrueFalseTotal
unless $fqdn 0 0 0
406  
407 # check that the resulting FQDN matches the original name
408 0 ▶ return "- '$name' -> $addr_str -> '$fqdn'" unless $fqdn eq $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $fqdn eq $name 0 0 0
409  
410 return
411 0 }
412  
413  
414 1 1
415  
416 __END__
↑ Summary bin/cfengine-tags »