Sandbox.pm

filter: Statementstmt 14.3% 57 / 398 Branchbran 4.2% 9 / 214 Conditioncond 2.9% 2 / 68 MC/DCmcdc 0.0% 0 / 39 Subroutinesub 28.2% 11 / 39 Podpod 0.0% 0 / 28 totaltotal 10.0% 79 / 786 CC 144 SCAR 44.2
CC
144
Cov
10%
fix_server_uuid
272.0
get_help
240.0
get_json_from_dirs
110.0
CRAP 82.9
↑ Summary Sandbox/Scripts.pm »
1 package MySQL::Sandbox;
2 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 24 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 24 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 24 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 24 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 24 ▶use base qw( Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 24 our @ISA= qw(Exporter);
12 24 our @EXPORT_OK= qw( is_port_open
13 runs_as_root
14 mylogin_cnf_exists
15 exists_in_path
16 is_a_sandbox
17 find_safe_port_and_directory
18 first_unused_port
19 get_sandbox_params
20 is_sandbox_running
21 get_sb_info
22 get_ports
23 get_ranges
24 use_env
25 sbinstr
26 get_json_from_dirs
27 get_option_file_contents
28 validate_json_object
29 fix_server_uuid
30 greater_version
31 split_version
32 deprecation_notice
33 ) ;
34  
35 24 our $VERSION=q{3.2.18};
36 24 our $DEBUG;
37  
38 24 ▶BEGIN {
Subroutine BEGIN: called CC=14 SCAR=32.9
39 24 ▶ $DEBUG = $ENV{'SBDEBUG'} || $ENV{'SBVERBOSE'} || 0;
Condition40%2/5
$ENV{'SBDEBUG'} || $ENV{'SBVERBOSE'} || 0
l!l
024
$ENV{"SBDEBUG"} or $ENV{"SBVERBOSE"}
l!l&&r!l&&!r
0024
MC/DC0%0/2
$ENV{'SBDEBUG'} || $ENV{'SBVERBOSE'} || 0
$ENV{"SBDEBUG"} $ENV{"SBVERBOSE"}
Truth table33%1/3
A or B
A= $ENV{"SBDEBUG"}B= $ENV{"SBVERBOSE"}
ABresult
1X1
011
000
40 24 ▶ if (! $ENV{'USER'})
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"USER"}) 24 0 24
41 {
42 24 my $user = qx/whoami/;
43 24 chomp $user;
44 24 $ENV{'USER'} = $user;
45 }
46 24 for my $var (qw( HOME USER PWD ))
47 {
48 72 ▶ unless ($ENV{$var}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{$var}) 0 72 72
49 0 die "The variable \$$var is undefined - aborting\n";
50 }
51 }
52 24 ▶ if ($ENV{HOME} =~ /\s/)
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"HOME"} =~ /\s/) 0 24 24
53 {
54 0 die "# The variable \$HOME contains spaces. Please fix this problem before continuing\n(HOME='$ENV{HOME}')\n";
55 }
56 24 ▶ unless ( $ENV{SANDBOX_HOME} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"SANDBOX_HOME"}) 24 0 24
57 24 $ENV{SANDBOX_HOME} = "$ENV{HOME}/sandboxes";
58 }
59  
60 24 ▶ unless ($ENV{TMPDIR})
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"TMPDIR"}) 24 0 24
61 {
62 24 $ENV{TMPDIR} = '/tmp';
63 }
64 24 ▶ unless ( -d $ENV{TMPDIR})
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $ENV{"TMPDIR"}) 0 24 24
65 {
66 0 die "could not find $ENV{TMPDIR}\n";
67 }
68  
69 24 ▶ if ( -d "$ENV{HOME}/sandboxes" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d qq[$ENV{"HOME"}/sandboxes]) 0 24 24
70 0 ▶ $ENV{SANDBOX_HOME} = $ENV{SANDBOX_HOME} || "$ENV{HOME}/sandboxes";
Condition0%0/2
$ENV{'SANDBOX_HOME'} || "$ENV{'HOME'}/sandboxes"
l!l
00
MC/DC0%0/1
$ENV{'SANDBOX_HOME'} || "$ENV{'HOME'}/sandboxes"
$ENV{'SANDBOX_HOME'}
Truth table0%0/2
A
A= $ENV{'SANDBOX_HOME'}
Aresult
11
00
71 }
72  
73 24 ▶ unless ( $ENV{SANDBOX_BINARY} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"SANDBOX_BINARY"}) 24 0 24
74 24 ▶ if ( -d "$ENV{HOME}/opt/mysql") {
Branch: 1 branch
BranchTrueFalseTotal
if (-d "$ENV{'HOME'}/opt/mysql") { } 0 24 24
75 0 $ENV{SANDBOX_BINARY} = "$ENV{HOME}/opt/mysql";
76 }
77 else
78 {
79 24 $ENV{SANDBOX_BINARY} = '';
80 }
81 }
82 }
83  
84 24 my @supported_versions = qw( 3.23 4.0 4.1 5.0 5.1 5.2 5.3 5.4
85 5.5 5.6 5.7 6.0 7.0 7.1 7.2 7.3 7.4 7.5 7.6 8.0 10.0 10.1 10.2 10.3 10.4 );
86  
87 24 our $sandbox_options_file = "my.sandbox.cnf";
88 # our $sandbox_current_options = "current_options.conf";
89  
90 24 our %default_base_port = (
91 replication => 11000,
92 circular => 14000,
93 multiple => 7000,
94 custom => 5000,
95 );
96  
97 24 our %default_users = (
98 db_user => 'msandbox',
99 remote_access => '127.%',
100 db_password => 'msandbox',
101 ro_user => 'msandbox_ro',
102 rw_user => 'msandbox_rw',
103 repl_user => 'rsandbox',
104 repl_password => 'rsandbox',
105 );
106  
107 24 our $SBINSTR_SH_TEXT =<<'SBINSTR_SH_TEXT';
108 if [ -f "$SBINSTR" ]
109 then
110 echo "[`basename $0`] - `date "+%Y-%m-%d %H:%M:%S"` - $@" >> $SBINSTR
111 fi
112 SBINSTR_SH_TEXT
113  
114  
115  
116 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
117 0 my ($class) = @_;
118 0 my $self = bless {
119 parse_options => undef,
120 options => undef,
121 }, $class;
122 # my $version = get_version( $install_dir);
123 # $self->{version} = $VERSION;
124 0 return $self;
125 }
126  
127 0 ▶sub parse_options {
Subroutine parse_options: not called CC=3 SCAR=24.8
Pod undocumented
128 0 my ($self, $opt ) = @_;
129 # print "<", ref($opt) , ">\n";
130 0 ▶ unless (ref($opt) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $opt eq "HASH") 0 0 0
131 0 confess "parse_options must be a hash reference\n";
132 }
133 0 ▶ if ($opt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt) 0 0 0
134 0 $self->{parse_options} = $opt;
135 }
136 0 my %options = map { $_ , $opt->{$_}{'value'}} keys %{$opt};
137 0 $self->{options} = \%options;
138  
139 0 return $self->{options};
140 }
141  
142 0 ▶sub find_safe_port_and_directory {
Subroutine find_safe_port_and_directory: not called CC=4 SCAR=30.0
Pod undocumented
143 0 my ($wanted_port, $wanted_dir, $upper_directory) = @_;
144 0 my $chosen_port = $wanted_port;
145 0 my ($ports, undef) = get_sb_info( $ENV{SANDBOX_HOME}, undef);
146 # print Dumper($ports);
147 0 ▶ while ( is_port_open($chosen_port) or exists $ports->{$chosen_port}) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_port_open($chosen_port) or exists $ports->{$chosen_port}) 0 0 0
Condition0%0/3
is_port_open($chosen_port) or exists $ports->{$chosen_port}
l!l&&r!l&&!r
000
MC/DC0%0/2
is_port_open($chosen_port) or exists $ports->{$chosen_port}
is_port_open($chosen_port) exists $ports->{$chosen_port}
Truth table0%0/3
A or B
A= is_port_open($chosen_port)B= exists $ports->{$chosen_port}
ABresult
1X1
011
000
148 0 $chosen_port++;
149 0 $chosen_port = first_unused_port($chosen_port);
150 # print "checking -> $chosen_port\n";
151 }
152 0 my $suffix = 'a';
153 0 my $chosen_dir = $wanted_dir;
154 0 ▶ while ( -d "$upper_directory/$chosen_dir" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d "$upper_directory/$chosen_dir") 0 0 0
155 # print "checking -> $chosen_dir\n";
156 0 $chosen_dir = $wanted_dir . '_' . $suffix;
157 0 $suffix++;
158 }
159 0 return ($chosen_port, $chosen_dir);
160 }
161  
162 0 ▶sub deprecation_notice {
Subroutine deprecation_notice: not called CC=1 SCAR=6.9
Pod undocumented
163 0 print "#############################################################################\n";
164 0 print "# IMPORTANT NOTICE #\n";
165 0 print "# ------------------------------------------------------------------------- #\n";
166 0 print "# MySQL-Sandbox is NOT MAINTAINED anymore. #\n";
167 0 print "# It may fail on recent MySQL versions. #\n";
168 0 print "# You should use **dbdeployer** (https://github.com/datacharmer/dbdeployer) #\n";
169 0 print "#############################################################################\n";
170 0 print "\n";
171 }
172  
173 0 ▶sub get_help {
Subroutine get_help: not called CC=15 SCAR=54.8
Pod undocumented
174 0 my ($self, $msg) = @_;
175 0 ▶ if ($msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg) 0 0 0
176 0 warn "[***] $msg\n\n";
177 }
178  
179 0 my $HELP_MSG = q{};
180 0 for my $op (
181 0 sort { $self->{parse_options}->{$a}{so} <=> $self->{parse_options}->{$b}{so} }
182 0 grep { $self->{parse_options}->{$_}{parse}} keys %{ $self->{parse_options} } ) {
183 0 my $param = $self->{parse_options}->{$op}{parse};
184 0 my $param_str = q{ };
185 0 my ($short, $long ) = $param =~ / (?: (\w) \| )? (\S+) /x;
186 0 ▶ if ($short) {
Branch: 1 branch
BranchTrueFalseTotal
if ($short) 0 0 0
187 0 $param_str .= q{-} . $short . q{ };
188 }
189 0 $long =~ s/ = s \@? / = name/x;
190 0 $long =~ s/ = i / = number/x;
191 0 $param_str .= q{--} . $long;
192 0 my $lparam = 40 - length($param_str);
193 0 ▶ if ($lparam < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($lparam < 0) 0 0 0
194 {
195 0 $lparam = 0;
196 }
197 0 $param_str .= (q{ } x $lparam );
198 0 my $text_items = $self->{parse_options}->{$op}{help};
199 0 for my $titem (@{$text_items}) {
200 0 $HELP_MSG .= $param_str . $titem . "\n";
201 0 $param_str = q{ } x 40;
202 }
203 0 ▶ if (@{$text_items} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$text_items;} > 1) 0 0 0
204 0 $HELP_MSG .= "\n";
205 }
206 # $HELP_MSG .= "\n";
207 }
208  
209 my $VAR_HELP =
210 "\nVARIABLES affecting this program: \n"
211 . "\t\$SBDEBUG : DEBUG LEVEL ("
212 . ($ENV{SBDEBUG} || 0) . ")\n"
213 . "\t\$SBVERBOSE : DEBUG LEVEL (same as \$SBDEBUG) ("
214 . ($ENV{SBVERBOSE} || 0) . ")\n"
215  
216 . "\t\$SANDBOX_HOME : root of all sandbox installations ("
217 . use_env($ENV{SANDBOX_HOME}) . ")\n"
218  
219 . "\t\$SANDBOX_BINARY : where to search for binaries ("
220 0 ▶ . use_env($ENV{SANDBOX_BINARY}) . ")\n"
Condition0%0/4
$ENV{'SBDEBUG'} || 0
l!l
00
$ENV{'SBVERBOSE'} || 0
l!l
00
MC/DC0%0/1
$ENV{'SBDEBUG'} || 0
$ENV{'SBDEBUG'}
MC/DC0%0/1
$ENV{'SBVERBOSE'} || 0
$ENV{'SBVERBOSE'}
Truth table0%0/2
A
A= $ENV{'SBDEBUG'}
Aresult
11
00
Truth table0%0/2
A
A= $ENV{'SBVERBOSE'}
Aresult
11
00
221 ;
222  
223 0 ▶ if ( $PROGRAM_NAME =~ /replication|multiple/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MySQL::Sandbox::PROGRAM_NAME =~ /replication|multiple/) 0 0 0
224 $VAR_HELP .=
225 "\t\$NODE_OPTIONS : options to pass to all node installations ("
226 0 ▶ . ($ENV{NODE_OPTIONS} || '') . ")\n"
Condition0%0/2
$ENV{'NODE_OPTIONS'} || ''
l!l
00
MC/DC0%0/1
$ENV{'NODE_OPTIONS'} || ''
$ENV{'NODE_OPTIONS'}
Truth table0%0/2
A
A= $ENV{'NODE_OPTIONS'}
Aresult
11
00
227 }
228  
229 0 ▶ if ( $PROGRAM_NAME =~ /replication/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MySQL::Sandbox::PROGRAM_NAME =~ /replication/) 0 0 0
230 $VAR_HELP .=
231 "\t\$MASTER_OPTIONS : options to pass to the master installation ("
232 . ($ENV{MASTER_OPTIONS} || '') . ")\n"
233  
234 . "\t\$SLAVE_OPTIONS : options to pass to all slave installations ("
235 0 ▶ . ($ENV{SLAVE_OPTIONS} || '' ) . ")\n"
Condition0%0/4
$ENV{'MASTER_OPTIONS'} || ''
l!l
00
$ENV{'SLAVE_OPTIONS'} || ''
l!l
00
MC/DC0%0/1
$ENV{'MASTER_OPTIONS'} || ''
$ENV{'MASTER_OPTIONS'}
MC/DC0%0/1
$ENV{'SLAVE_OPTIONS'} || ''
$ENV{'SLAVE_OPTIONS'}
Truth table0%0/2
A
A= $ENV{'MASTER_OPTIONS'}
Aresult
11
00
Truth table0%0/2
A
A= $ENV{'SLAVE_OPTIONS'}
Aresult
11
00
236 }
237 0 my $target = '';
238 0 ▶ if ( grep {$PROGRAM_NAME =~ /$_/ }
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$MySQL::Sandbox::PROGRAM_NAME =~ /$_/;} "make_sandbox", "make_replication_sandbox", "make_multiple_sandbox", "make_multiple_sandbox") 0 0 0
239 qw( make_sandbox make_replication_sandbox
240 make_multiple_sandbox make_multiple_sandbox ) )
241 {
242 0 $target = '{tarball|dir|version}';
243 0 $HELP_MSG =
244 "tarball = the full path to a MySQL binary tarball\n"
245 . "dir = the path to an expanded MySQL binary tarball\n"
246 . "version = the simple version number of the expanded tarball\n"
247 . " if it is under \$SANDBOX_BINARY and renamed as the\n "
248 . " version number.\n\n"
249 . $HELP_MSG;
250 }
251 0 deprecation_notice();
252 0 print $self->credits(),
253 "syntax: $PROGRAM_NAME [options] $target \n",
254 $HELP_MSG,
255 $VAR_HELP;
256 # This example is only relevant for a single sandbox, but it is
257 # wrong for a multiple sandbox.
258 #,
259 #"\nExample:\n",
260 #" $PROGRAM_NAME --my_file=large --sandbox_directory=my_sandbox\n\n";
261  
262 0 deprecation_notice();
263 0 exit(1);
264 }
265  
266 1 ▶sub credits {
Subroutine credits: called CC=1 SCAR=0.0
Pod undocumented
267 1 my ($self) = @_;
268 1 my $CREDITS =
269 qq( The MySQL Sandbox, version $VERSION\n)
270 . qq( (C) 2006-2018 Giuseppe Maxia\n);
271 1 return $CREDITS;
272 }
273  
274 sub split_version
275 0 ▶{
Subroutine split_version: not called CC=2 SCAR=17.9
Pod undocumented
276 0 my ($v) = @_;
277 #if ($v =~ /(?<major>\d+)\.(?<minor>\d+)\.(?<rev>\d+)/ )
278 0 ▶ if ($v =~ /(\d+)\.(\d+)\.(\d+)/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ /(\d+)\.(\d+)\.(\d+)/) { } 0 0 0
279 {
280 #return ($+{major}, $+{minor}, $+{rev})
281 0 return ($1, $2, $3)
282  
283 }
284 else
285 {
286 0 die "# Split version: could not get components from <$v>\n";
287 }
288 }
289  
290 sub greater_version
291 0 ▶{
Subroutine greater_version: not called CC=7 SCAR=40.3
Pod undocumented
292 0 my ($v1, $v2) = @_;
293  
294 0 my ($v1_major, $v1_minor, $v1_rev) = split_version($v1);
295 0 my ($v2_major, $v2_minor, $v2_rev) = split_version($v2);
296 0 ▶ if ( $v1_major > $v2_major)
Branch: 3 branches
BranchTrueFalseTotal
if ($v1_major > $v2_major) { } 0 0 0
elsif ($v1_major == $v2_major and $v1_minor > $v2_minor) { } 0 0 0
elsif ($v1_major == $v2_major and $v1_minor == $v2_minor and $v1_rev > $v2_rev) { } 0 0 0
297 {
298 0 return 1;
299 }
300 ▶ elsif ( ($v1_major == $v2_major) && ($v1_minor > $v2_minor))
Condition0%0/3
$v1_major == $v2_major and $v1_minor > $v2_minor
!ll&&!rl&&r
000
MC/DC0%0/2
$v1_major == $v2_major and $v1_minor > $v2_minor
$v1_major == $v2_major $v1_minor > $v2_minor
Truth table0%0/3
A and B
A= $v1_major == $v2_majorB= $v1_minor > $v2_minor
ABresult
0X0
100
111
301 {
302 0 return 1;
303 }
304 ▶ elsif ( ($v1_major == $v2_major) && ($v1_minor == $v2_minor) && ($v1_rev > $v2_rev) )
Condition0%0/6
$v1_major == $v2_major and $v1_minor == $v2_minor and $v1_rev > $v2_rev
!ll&&!rl&&r
000
$v1_major == $v2_major and $v1_minor == $v2_minor
!ll&&!rl&&r
000
MC/DC0%0/3
$v1_major == $v2_major and $v1_minor == $v2_minor and $v1_rev > $v2_rev
$v1_major == $v2_major $v1_minor == $v2_minor $v1_rev > $v2_rev
Truth table0%0/4
A and B and C
A= $v1_major == $v2_majorB= $v1_minor == $v2_minorC= $v1_rev > $v2_rev
ABCresult
0XX0
10X0
1100
1111
305 {
306 0 return 1;
307 }
308 0 return 0
309 }
310  
311  
312 sub fix_server_uuid
313 0 ▶{
Subroutine fix_server_uuid: not called CC=16 SCAR=56.1
Pod undocumented
314 0 my ($server_id, $version, $port, $sandbox_directory) = @_;
315 0 ▶ if ($version =~ /(\d+)\.(\d+)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($version =~ /(\d+)\.(\d+)/) 0 0 0
316 {
317 0 my ($major, $minor ) = ($1, $2);
318 0 ▶ unless ( ($major == 8) or ( ($major == 5) && ($minor >=6)) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($major == 8 or $major == 5 and $minor >= 6) 0 0 0
Condition0%0/6
$major == 8 or $major == 5 and $minor >= 6
l!l&&r!l&&!r
000
$major == 5 and $minor >= 6
!ll&&!rl&&r
000
MC/DC0%0/3
$major == 8 or $major == 5 and $minor >= 6
$major == 8 $major == 5 $minor >= 6
Truth table0%0/4
A or B and C
A= $major == 8B= $major == 5C= $minor >= 6
ABCresult
1XX1
0111
00X0
0100
319 {
320 0 return;
321 }
322 }
323 0 my $current_dir = $ENV{PWD};
324 0 my $increase_id = 0;
325 0 $sandbox_directory =~ s{/$}{};
326 0 my $operation_dir= "$sandbox_directory/data";
327 0 ▶ if ( ! -d $operation_dir)
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $operation_dir) 0 0 0
328 {
329 0 die "<$operation_dir> not found\n";
330 }
331 0 chdir $operation_dir;
332 0 ▶ print "# operation_dir is $operation_dir\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $MySQL::Sandbox::DEBUG 0 0 0
333 0 ▶ if ( ($operation_dir =~ m{/node\d/data$}) && (-d "../../master"))
Branch: 1 branch
BranchTrueFalseTotal
if ($operation_dir =~ m[/node\d/data$] and -d "../../master") 0 0 0
Condition0%0/3
$operation_dir =~ m[/node\d/data$] and -d "../../master"
!ll&&!rl&&r
000
MC/DC0%0/2
$operation_dir =~ m[/node\d/data$] and -d "../../master"
$operation_dir =~ m[/node\d/data$] -d "../../master"
Truth table0%0/3
A and B
A= $operation_dir =~ m[/node\d/data$]B= -d "../../master"
ABresult
0X0
100
111
334 {
335 0 $increase_id =1;
336 }
337 # 12345678 1234 1234 1234 123456789012
338 # my $new_uuid='00000000-0000-0000-0000-000000000000';
339 0 my $group1 = sprintf('%08d', $port);
340  
341 0 my $group2= sprintf('%04d-%04d-%04d-%012d', 0,0,0,0);
342 0 ▶ if ($server_id < 10)
Branch: 2 branches
BranchTrueFalseTotal
if ($server_id < 10) { } 0 0 0
elsif ($server_id >= 100 and $server_id < 109) { } 0 0 0
343 {
344 0 $group2 =~ s/\d/$server_id/g;
345 }
346 ▶ elsif (($server_id >= 100) && ($server_id < 109))
Condition0%0/3
$server_id >= 100 and $server_id < 109
!ll&&!rl&&r
000
MC/DC0%0/2
$server_id >= 100 and $server_id < 109
$server_id >= 100 $server_id < 109
Truth table0%0/3
A and B
A= $server_id >= 100B= $server_id < 109
ABresult
0X0
100
111
347 {
348 0 $server_id -= 100;
349 0 ▶ $server_id += 1 if $increase_id; # 101 => 2
Branch: 1 branch
BranchTrueFalseTotal
if $increase_id 0 0 0
350 0 $group2 =~ s/\d/$server_id/g;
351 }
352 else
353 {
354 0 my $second_id = $server_id;
355 0 ▶ if ($second_id > 9999)
Branch: 1 branch
BranchTrueFalseTotal
if ($second_id > 9999) 0 0 0
356 {
357 0 $second_id = 9999;
358 }
359 0 $group2 = sprintf( '%04d-%04d-%04d-%012d', $second_id, $second_id, $second_id, $server_id );
360 }
361 0 my $new_uuid= "$group1-$group2";
362 0 ▶ open my $FH, '>', 'auto.cnf'
Branch: 1 branch
BranchTrueFalseTotal
open my $FH, ">", "auto.cnf" or die "Error updating 'auto.cnf' ($!)\n" 0 0 0
363 or die "Error updating 'auto.cnf' ($!)\n";
364 0 print $FH "[auto]\n";
365 0 print $FH "server-uuid=$new_uuid\n";
366 0 close $FH;
367 0 chdir $current_dir;
368 0 ▶ print "New UUID=$new_uuid\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $MySQL::Sandbox::DEBUG 0 0 0
369 }
370  
371 0 ▶sub validate_json_object {
Subroutine validate_json_object: not called CC=9 SCAR=45.0
Pod undocumented
372 0 my ($json_filename, $json_text) = @_;
373  
374 0 my $JSON_module = undef;
375 0 for my $module ( 'JSON', 'JSON::PP', 'JSON::XS')
376 {
377 0 eval "use $module;";
378 0 ▶ if (! $@)
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
379 {
380 0 ▶ print "# Using $module\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $MySQL::Sandbox::DEBUG 0 0 0
381 0 $JSON_module=$module;
382 0 last;
383 }
384 }
385 0 ▶ unless ($JSON_module)
Branch: 1 branch
BranchTrueFalseTotal
unless ($JSON_module) 0 0 0
386 {
387 0 ▶ print "# JSON modules not installed - skipped evaluation\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $MySQL::Sandbox::DEBUG 0 0 0
388 0 return -1;
389 }
390  
391 0 ▶ unless ($json_text)
Branch: 1 branch
BranchTrueFalseTotal
unless ($json_text) 0 0 0
392 {
393 0 $json_text = slurp($json_filename);
394 }
395 0 my $json = $JSON_module->new->allow_nonref;
396  
397 0 my $perl_value;
398 0 eval {
399 0 $perl_value = $json->decode( $json_text );
400 };
401 0 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
402 {
403 0 ▶ print "error decoding json object\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $MySQL::Sandbox::DEBUG 0 0 0
404 0 return ;
405 }
406 0 return 1;
407 }
408  
409 0 ▶sub slurp {
Subroutine slurp: not called CC=9 SCAR=45.0
Pod undocumented
410 0 my ($filename, $skip_blanks, $skip_comments ) = @_;
411 0 ▶ open my $FH , q{<}, $filename
Branch: 1 branch
BranchTrueFalseTotal
open my $FH, "<", $filename or die "file '${filename}' not found\n" 0 0 0
412 or die "file '$filename' not found\n";
413 0 my @text_array = ();
414 0 my $text='';
415 0 ▶ while (my $line = <$FH>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $FH)) 0 0 0
416 {
417 0 ▶ if ($skip_blanks)
Branch: 1 branch
BranchTrueFalseTotal
if ($skip_blanks) 0 0 0
418 {
419 0 ▶ next if $line =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 0 0 0
420 }
421 0 ▶ if ($skip_comments)
Branch: 1 branch
BranchTrueFalseTotal
if ($skip_comments) 0 0 0
422 {
423 0 ▶ next if $line =~ /^\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*#/ 0 0 0
424 }
425 0 ▶ if (wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
426 {
427 0 push @text_array, $line;
428 }
429 else
430 {
431 0 $text .= $line;
432 }
433 }
434 0 close $FH;
435 0 ▶ if (wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
436 {
437 0 return @text_array;
438 }
439 else
440 {
441 0 return $text;
442 }
443 }
444  
445 0 ▶sub get_json_from_dirs {
Subroutine get_json_from_dirs: not called CC=10 SCAR=47.0
Pod undocumented
446 0 my ($directories, $json_file) = @_;
447 0 my $collective_json = '';
448 0 my $indent = ' ';
449 0 for my $dir (@$directories)
450 {
451 0 my $filename = "$dir/$json_file";
452 0 ▶ if ($collective_json)
Branch: 1 branch
BranchTrueFalseTotal
if ($collective_json) { } 0 0 0
453 {
454 0 $collective_json .= ",\n"
455 }
456 else
457 {
458 0 $collective_json = "{\n";
459 }
460 0 $collective_json .= qq("$dir": \n);
461 0 ▶ if ( -f $filename)
Branch: 1 branch
BranchTrueFalseTotal
if (-f $filename) { } 0 0 0
462 {
463 # get the contents
464 0 my @json_lines = slurp($filename, 'skip_blanks');
465 0 for my $jl (@json_lines)
466 {
467 0 $collective_json .= $indent . $jl;
468 }
469 }
470 else
471 {
472 0 ▶ if ($DEBUG)
Branch: 1 branch
BranchTrueFalseTotal
if ($MySQL::Sandbox::DEBUG) 0 0 0
473 {
474 0 warn "# No connection.json found in $dir\n";
475 0 my ($package, $filename, $line) = caller;
476 0 warn "# called from $package - $filename - $line \n";
477 }
478 0 $collective_json .= "{}";
479 }
480 }
481 0 $collective_json .= "}";
482 0 my $is_valid_json = validate_json_object(undef, $collective_json);
483 0 ▶ if ($is_valid_json && ($is_valid_json == -1))
Branch: 2 branches
BranchTrueFalseTotal
if ($is_valid_json and $is_valid_json == -1) { } 0 0 0
elsif (not $is_valid_json) { } 0 0 0
Condition0%0/3
$is_valid_json and $is_valid_json == -1
!ll&&!rl&&r
000
MC/DC0%0/2
$is_valid_json and $is_valid_json == -1
$is_valid_json $is_valid_json == -1
Truth table0%0/3
A and B
A= $is_valid_jsonB= $is_valid_json == -1
ABresult
0X0
100
111
484 {
485 0 ▶ if ($DEBUG)
Branch: 1 branch
BranchTrueFalseTotal
if ($MySQL::Sandbox::DEBUG) 0 0 0
486 {
487 0 warn "# Could not validate JSON object\n";
488 }
489 }
490 elsif ( ! $is_valid_json)
491 {
492 0 warn "Invalid JSON object in $ENV{PWD} from [@$directories] \n";
493 0 $collective_json = qq({ "comment": "WARNING: invalid JSON object", "original" : )
494 . $collective_json
495 . "\n}";
496 }
497 0 return $collective_json;
498 }
499  
500 #sub get_version {
501 # my ($install_dir) = @_;
502 # open my $VER , q{<}, "$install_dir/VERSION"
503 # #open my $VER , q{<}, "VERSION"
504 # or die "file 'VERSION' not found\n";
505 # my $version = <$VER>;
506 # chomp $version;
507 # close $VER;
508 # return $version;
509 #}
510  
511 0 ▶sub write_to {
Subroutine write_to: not called CC=4 SCAR=30.0
Pod undocumented
512 0 my ($self, $fname, $mode, $contents) = @_;
513 0 ▶ open my $FILE, $mode, $fname
Branch: 1 branch
BranchTrueFalseTotal
open my $FILE, $mode, $fname or die "can't open file $fname\n" 0 0 0
514 or die "can't open file $fname\n";
515 0 print $FILE $contents, "\n";
516 0 ▶ if (($mode eq '>') && ( $contents =~ m/\#!\/bin\/sh/ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq ">" and $contents =~ m[\#!/bin/sh]) 0 0 0
Condition0%0/3
$mode eq ">" and $contents =~ m[\#!/bin/sh]
!ll&&!rl&&r
000
MC/DC0%0/2
$mode eq ">" and $contents =~ m[\#!/bin/sh]
$mode eq ">" $contents =~ m[\#!/bin/sh]
Truth table0%0/3
A and B
A= $mode eq ">"B= $contents =~ m[\#!/bin/sh]
ABresult
0X0
100
111
517 0 print $FILE $SBINSTR_SH_TEXT;
518 }
519 0 close $FILE;
520 }
521  
522 0 ▶sub supported_versions {
Subroutine supported_versions: not called CC=1 SCAR=6.9
Pod undocumented
523 0 return \@supported_versions;
524 }
525  
526 0 ▶sub is_port_open {
Subroutine is_port_open: not called CC=6 SCAR=37.4
Pod undocumented
527 0 my ($port) = @_;
528 0 ▶ die "No port" unless $port;
Branch: 1 branch
BranchTrueFalseTotal
unless $port 0 0 0
529 0 my ($host, $iaddr, $paddr, $proto);
530  
531 0 $host = '127.0.0.1';
532 0 ▶ $iaddr = inet_aton($host)
Branch: 1 branch
BranchTrueFalseTotal
$iaddr = inet_aton($host) or die "no host: $host" 0 0 0
533 or die "no host: $host";
534 0 $paddr = sockaddr_in($port, $iaddr);
535  
536 0 $proto = getprotobyname('tcp');
537 0 ▶ socket(SOCK, PF_INET, SOCK_STREAM, $proto)
Branch: 1 branch
BranchTrueFalseTotal
socket SOCK, 2, 1, $proto or die "error creating test socket for port ${port}: $!" 0 0 0
538 or die "error creating test socket for port $port: $!";
539 0 ▶ if (connect(SOCK, $paddr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (connect SOCK, $paddr) 0 0 0
540 0 ▶ close (SOCK)
Branch: 1 branch
BranchTrueFalseTotal
close SOCK or die "error closing test socket: $!" 0 0 0
541 or die "error closing test socket: $!";
542 0 return 1;
543 }
544 0 return 0;
545 }
546  
547 0 ▶sub first_unused_port {
Subroutine first_unused_port: not called CC=3 SCAR=24.8
Pod undocumented
548 0 my ($port) = @_;
549 0 ▶ while (is_port_open($port)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_port_open($port)) 0 0 0
550 0 $port++;
551 0 ▶ if ($port > 0xFFF0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($port > 65520) 0 0 0
552 0 die "no ports available\n";
553 }
554 }
555 0 return $port;
556 }
557  
558 ##
559 # SBtool
560 #
561 0 ▶sub get_sandbox_params {
Subroutine get_sandbox_params: not called CC=6 SCAR=37.4
Pod undocumented
562 0 my ($dir, $skip_strict) = @_;
563 0 ▶ confess "directory name required\n" unless $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $dir 0 0 0
564 0 ▶ confess "directory $dir doesn't exist\n" unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
565 0 ▶ unless (is_a_sandbox($dir)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_a_sandbox($dir)) 0 0 0
566 0 ▶ confess "directory <$dir> must be a sandbox\n" unless $skip_strict;
Branch: 1 branch
BranchTrueFalseTotal
unless $skip_strict 0 0 0
567 }
568 0 my %params = (
569 opt => undef,
570 conf => undef
571 );
572 0 ▶ if ( -f "$dir/$sandbox_options_file" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f "$dir/$MySQL::Sandbox::sandbox_options_file") { } 0 0 0
573 0 $params{opt} = get_option_file_contents("$dir/$sandbox_options_file");
574 }
575 else {
576 # warn "options file $dir not found\n";
577 0 return;
578 }
579 # if ( -f "$dir/$sandbox_current_options" ) {
580 # $params{conf} =
581 # get_option_file_contents("$dir/$sandbox_current_options");
582 # }
583 # else {
584 # # warn "current conf file not found\n";
585 # return;
586 # }
587 0 return \%params;
588 }
589  
590 0 ▶sub get_option_file_contents {
Subroutine get_option_file_contents: not called CC=8 SCAR=42.8
Pod undocumented
591 0 my ($file) = @_;
592 0 ▶ confess "file name required\n" unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 0 0
593 0 ▶ confess "file $file doesn't exist\n" unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
594 0 my %options;
595 0 ▶ open my $RFILE, q{<}, $file
Branch: 1 branch
BranchTrueFalseTotal
open my $RFILE, "<", $file or confess("can't open file $file\n") 0 0 0
596 or confess "can't open file $file\n";
597 0 ▶ while ( my $line = <$RFILE> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $RFILE)) 0 0 0
598 0 ▶ next if $line =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 0 0 0
599 0 ▶ next if $line =~ /^\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*#/ 0 0 0
600 0 ▶ next if $line =~ /^\s*\[/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*\[/ 0 0 0
601 0 chomp $line;
602 0 my ( $key, $val ) = split /\s*=\s*/, $line;
603 0 $key =~ s/-/_/g;
604 0 $options{$key} = $val;
605 }
606 0 close $RFILE;
607 # print Dumper(\%options) ; exit;
608 0 return \%options;
609 }
610  
611 0 ▶sub get_sb_info {
Subroutine get_sb_info: not called CC=2 SCAR=17.9
Pod undocumented
612 0 my ($search_path, $options) = @_;
613 0 my %ports = ();
614 0 my %all_info = ();
615 0 my $seen_dir = '';
616  
617 find(
618 {
619 no_chdir => 1,
620 follow => 1,
621 0 ▶ wanted => sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
622 0 ▶ if ( $seen_dir eq $File::Find::dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen_dir eq $File::Find::dir) 0 0 0
623 0 return;
624 }
625 0 my $params;
626 0 ▶ if ( $params = get_sandbox_params($File::Find::dir, 1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params = get_sandbox_params($File::Find::dir, 1)) 0 0 0
627 0 $seen_dir = $File::Find::dir;
628 0 my $port = $params->{opt}{port};
629 0 ▶ if ( -f $params->{opt}{pid_file}
Branch: 1 branch
BranchTrueFalseTotal
if (-f $params->{'opt'}{'pid_file'} and -e $params->{'opt'}{'socket'}) { } 0 0 0
Condition0%0/3
-f $params->{"opt"}{"pid_file"} and -e $params->{"opt"}{"socket"}
!ll&&!rl&&r
000
MC/DC0%0/2
-f $params->{"opt"}{"pid_file"} and -e $params->{"opt"}{"socket"}
-f $params->{"opt"}{"pid_file"} -e $params->{"opt"}{"socket"}
Truth table0%0/3
A and B
A= -f $params->{"opt"}{"pid_file"}B= -e $params->{"opt"}{"socket"}
ABresult
0X0
100
111
630 && -e $params->{opt}{socket} )
631 {
632 0 $ports{$port} = 1;
633 0 ▶ $all_info{$port} = $params if $options->{all_info};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"all_info"} 0 0 0
634 }
635 else {
636 0 ▶ unless ( $options->{only_used} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{"only_used"}) 0 0 0
637 0 $ports{$port} = 0;
638 0 ▶ $all_info{$port} = $params if $options->{all_info};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"all_info"} 0 0 0
639 }
640 }
641 }
642 }
643 },
644 $search_path || $options->{search_path}
645 0 ▶ );
Condition0%0/3
$search_path || $options->{'search_path'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$search_path || $options->{'search_path'}
$search_path $options->{'search_path'}
Truth table0%0/3
A || B
A= $search_pathB= $options->{'search_path'}
ABresult
1X1
011
000
646 0 return ( \%ports, \%all_info );
647 }
648  
649 0 ▶sub is_a_sandbox {
Subroutine is_a_sandbox: not called CC=4 SCAR=26.1
Pod undocumented
650 0 my ($dir) = @_;
651 0 ▶ unless ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dir) 0 0 0
652 0 confess "directory missing\n";
653 }
654 0 $dir =~ s{/$}{};
655 0 ▶ my %sandbox_files = map {s{.*/}{}; $_, 1 } glob("$dir/*");
Subroutine BEGIN: called CC=1 SCAR=1.2
656 0 my @required = (qw(data start stop send_kill clear use restart),
657 # $sandbox_current_options,
658 $sandbox_options_file );
659 0 for my $req (@required) {
660 0 ▶ unless (exists $sandbox_files{$req}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $sandbox_files{$req}) 0 0 0
661 0 return;
662 }
663 }
664 0 return 1;
665 }
666  
667 0 ▶sub is_sandbox_running {
Subroutine is_sandbox_running: not called CC=7 SCAR=40.3
Pod undocumented
668 0 my ($sandbox) = @_;
669 0 ▶ unless ( -d $sandbox ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $sandbox) 0 0 0
670 0 confess "Can't see if it's running. <$sandbox> is not a sandbox\n";
671 }
672 0 my $sboptions = get_sandbox_params($sandbox);
673 0 ▶ unless ($sboptions->{opt}
Branch: 1 branch
BranchTrueFalseTotal
unless ($sboptions->{"opt"} and $sboptions->{"opt"}{"pid_file"} and $sboptions->{"opt"}{"socket"}) 0 0 0
Condition0%0/6
$sboptions->{"opt"} and $sboptions->{"opt"}{"pid_file"} and $sboptions->{"opt"}{"socket"}
!ll&&!rl&&r
000
$sboptions->{"opt"} and $sboptions->{"opt"}{"pid_file"}
!ll&&!rl&&r
000
MC/DC0%0/3
$sboptions->{"opt"} and $sboptions->{"opt"}{"pid_file"} and $sboptions->{"opt"}{"socket"}
$sboptions->{"opt"} $sboptions->{"opt"}{"pid_file"} $sboptions->{"opt"}{"socket"}
Truth table0%0/4
A and B and C
A= $sboptions->{"opt"}B= $sboptions->{"opt"}{"pid_file"}C= $sboptions->{"opt"}{"socket"}
ABCresult
0XX0
10X0
1100
1111
674 && $sboptions->{opt}{'pid_file'}
675 && $sboptions->{opt}{'socket'}) {
676 # print Dumper($sboptions);
677 0 confess "<$sandbox> is not a single sandbox\n";
678 }
679 0 ▶ if ( ( -f $sboptions->{opt}{'pid_file'} )
Branch: 1 branch
BranchTrueFalseTotal
if (-f $sboptions->{'opt'}{'pid_file'} and -e $sboptions->{'opt'}{'socket'}) { } 0 0 0
Condition0%0/3
-f $sboptions->{"opt"}{"pid_file"} and -e $sboptions->{"opt"}{"socket"}
!ll&&!rl&&r
000
MC/DC0%0/2
-f $sboptions->{"opt"}{"pid_file"} and -e $sboptions->{"opt"}{"socket"}
-f $sboptions->{"opt"}{"pid_file"} -e $sboptions->{"opt"}{"socket"}
Truth table0%0/3
A and B
A= -f $sboptions->{"opt"}{"pid_file"}B= -e $sboptions->{"opt"}{"socket"}
ABresult
0X0
100
111
680 && ( -e $sboptions->{opt}{'socket'}) ) {
681 0 return (1, $sboptions);
682 }
683 else {
684 0 return (0, $sboptions);
685 }
686 }
687  
688 0 ▶sub get_ranges {
Subroutine get_ranges: not called CC=9 SCAR=45.0
Pod undocumented
689 0 my ($options, $silent ) = @_;
690 0 my ( $ports, $all_info ) = get_sb_info(undef, $options);
691 0 my $minimum_port = $options->{min_range};
692 0 my $maximum_port = $options->{max_range};
693 0 my $range_size = $options->{range_size};
694 0 ▶ if ( $minimum_port >= $maximum_port ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($minimum_port >= $maximum_port) 0 0 0
695 0 croak "minimum range must be lower than the maximum range\n";
696 }
697 0 ▶ if ( ( $minimum_port + $range_size ) > $maximum_port ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($minimum_port + $range_size > $maximum_port) 0 0 0
698 0 croak "range too wide for given boundaries\n";
699 }
700 0 my $range_found = 0;
701 range_search:
702 0 ▶ while ( !$range_found ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($range_found) 0 0 0
703 0 ▶ if ( $minimum_port >= $maximum_port ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($minimum_port >= $maximum_port) 0 0 0
704 0 croak "can't find a range of $range_size "
705 . "free ports between "
706 . "$options->{min_range} and $options->{max_range}\n";
707 }
708 0 for my $i ( $minimum_port .. $minimum_port + $range_size ) {
709 0 ▶ if ( exists $ports->{$i} or ( $i >= $maximum_port ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ports->{$i} or $i >= $maximum_port) 0 0 0
Condition0%0/3
exists $ports->{$i} or $i >= $maximum_port
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $ports->{$i} or $i >= $maximum_port
exists $ports->{$i} $i >= $maximum_port
Truth table0%0/3
A or B
A= exists $ports->{$i}B= $i >= $maximum_port
ABresult
1X1
011
000
710 0 $minimum_port = $i + 1;
711 0 next range_search;
712 }
713 }
714 0 $range_found = 1;
715 }
716 0 ▶ unless ($silent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($silent) 0 0 0
717 0 printf "%5d - %5d\n", $minimum_port , $minimum_port + $range_size;
718 }
719 0 return $minimum_port;
720 }
721  
722 0 ▶sub get_ports {
Subroutine get_ports: not called CC=5 SCAR=34.0
Pod undocumented
723 0 my ($options) = @_;
724 0 my ( $ports, $all_info ) = get_sb_info(undef, $options);
725  
726 0 ▶ if ( $options->{format} eq 'perl' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($options->{'format'} eq 'perl') { } 0 0 0
elsif ($options->{'format'} eq 'text') { } 0 0 0
727 0 print Data::Dumper->Dump( [$ports], ['ports'] );
728 print Data::Dumper->Dump( [$all_info], ['all_info'] )
729 0 ▶ if $options->{all_info};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"all_info"} 0 0 0
730 }
731 elsif ( $options->{format} eq 'text' ) {
732 0 for my $port ( sort { $a <=> $b } keys %$ports ) {
733 0 printf "%5d %2d\n", $port, $ports->{$port};
734 }
735 }
736 else {
737 0 croak "unrecognized format -> $options->{format}\n";
738 }
739 0 return ( $ports, $all_info );
740 }
741  
742 0 ▶sub exists_in_path {
Subroutine exists_in_path: not called CC=3 SCAR=24.8
Pod undocumented
743 0 my ($cmd) = @_;
744 0 my @path_directories = split /:/, $ENV{PATH}; ## no critic
745 0 for my $dir (@path_directories) {
746 0 ▶ if ( -x "$dir/$cmd") {
Branch: 1 branch
BranchTrueFalseTotal
if (-x "$dir/$cmd") 0 0 0
747 0 return "$dir/$cmd";
748 }
749 }
750 0 return ;
751 }
752  
753 0 ▶sub runs_as_root {
Subroutine runs_as_root: not called CC=4 SCAR=30.0
Pod undocumented
754 0 ▶ if ( ($REAL_USER_ID == 0) or ($EFFECTIVE_USER_ID == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MySQL::Sandbox::REAL_USER_ID == 0 or $MySQL::Sandbox::EFFECTIVE_USER_ID == 0) 0 0 0
Condition0%0/3
$MySQL::Sandbox::REAL_USER_ID == 0 or $MySQL::Sandbox::EFFECTIVE_USER_ID == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$MySQL::Sandbox::REAL_USER_ID == 0 or $MySQL::Sandbox::EFFECTIVE_USER_ID == 0
$MySQL::Sandbox::REAL_USER_ID == 0 $MySQL::Sandbox::EFFECTIVE_USER_ID == 0
Truth table0%0/3
A or B
A= $MySQL::Sandbox::REAL_USER_ID == 0B= $MySQL::Sandbox::EFFECTIVE_USER_ID == 0
ABresult
1X1
011
000
755 0 ▶ unless ($ENV{SANDBOX_AS_ROOT}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"SANDBOX_AS_ROOT"}) 0 0 0
756 0 die "MySQL Sandbox should not run as root\n"
757 . "\n"
758 . "If you know what you are doing and want to\n "
759 . "run as root nonetheless, please set the environment\n"
760 . "variable 'SANDBOX_AS_ROOT' to a nonzero value\n";
761 }
762 }
763 }
764  
765 0 ▶sub mylogin_cnf_exists {
Subroutine mylogin_cnf_exists: not called CC=3 SCAR=24.8
Pod undocumented
766 0 my $mylogin_cnf = "$ENV{HOME}/.mylogin.cnf";
767 0 ▶ if ( -r $mylogin_cnf) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $mylogin_cnf) 0 0 0
768 0 ▶ unless ($ENV{IGNORE_MYLOGIN_CNF}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"IGNORE_MYLOGIN_CNF"}) 0 0 0
769 0 die "MySQL Sandbox does not work with \$HOME/.mylogin.cnf,\n"
770 . "which is a file created by mysql_config_editor.\n"
771 . "Either remove the file or make it not readable by the current user.\n"
772 . "If you know what you are doing, you can skip this check by\n"
773 . "setting the variable IGNORE_MYLOGIN_CNF to a nonzero value.\n"
774 . "Be aware that having \$HOME/.mylogin.cnf can disrupt MySQL-Sandbox.\n"
775 . "Use it at your own risk.\n"
776 }
777 }
778 }
779  
780 #
781 # Replaces a path portion with an environment variable name
782 # if a match is found
783 #
784 0 ▶sub use_env{
Subroutine use_env: not called CC=4 SCAR=30.0
Pod undocumented
785 0 my ($path) = @_;
786 0 my @vars = (
787 'HOME',
788 'SANDBOX_HOME',
789 );
790 0 ▶ return '' unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 0 0
791 0 for my $var (@vars) {
792 0 ▶ if ($path =~ /^$ENV{$var}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /^$ENV{$var}/) 0 0 0
793 0 $path =~ s/$ENV{$var}/\$$var/;
794 0 return $path;
795 }
796 }
797 0 return $path;
798 }
799  
800 0 ▶sub sbinstr {
Subroutine sbinstr: not called CC=4 SCAR=30.0
Pod undocumented
801 0 my ($msg) = @_;
802 0 ▶ unless ($ENV{SBINSTR}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"SBINSTR"}) 0 0 0
803 0 return;
804 }
805 0 my $pname = $PROGRAM_NAME;
806 0 ▶ unless ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($MySQL::Sandbox::DEBUG) 0 0 0
807 0 $pname =~ s{.*/}{};
808 }
809 open my $FH, '>>', $ENV{SBINSTR}
810 0 ▶ or die "can't write to $ENV{SBINSTR} ($!)\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $FH, ">>", $ENV{"SBINSTR"} or die qq[can't write to $ENV{"SBINSTR"} ($!)\n] 0 0 0
811 0 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
812 0 $mon++;
813 0 $year +=1900;
814 0 print $FH "[$pname] - ",
815 sprintf('%4d-%02d%02d %02d:%02d:%02d',
816 $year, $mon, $mday, $hour, $min, $sec),
817 " - $msg \n";
818 0 close $FH;
819 }
820  
821 24 1;
822 __END__
↑ Summary Sandbox/Scripts.pm »