Debian Codegraph @

bin/test_sandbox

raw ◀ ▶
1 Debian patch touch this file
1 #!/usr/bin/perl
2 # test_sandbox
3 # The MySQL Sandbox
4 # Copyright (C) 2006-2018 Giuseppe Maxia
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 use strict;
19 use warnings;
20 use Data::Dumper;
21 use Getopt::Long qw(:config no_ignore_case );
22 use MySQL::Sandbox qw(use_env greater_version exists_in_path);
23 use English qw( -no_match_vars );
24 use Carp;
25
26 my $DEBUG = $MySQL::Sandbox::DEBUG;
27
28 for my $prog (qw( make_sandbox
29 make_replication_sandbox
30 make_multiple_sandbox
31 make_multiple_custom_sandbox
32 sbtool ) ) {
33 unless ( exists_in_path ($prog) ) {
34 die "script <$prog> not found\n";
35 }
36 }
37
38 sub tprint;
39 sub tprintf;
40
41 sub get_exec_result ;
42 my $use_open3 = 0;
43
44 eval "use IPC::Open3";
45 if ($@) {
46 $use_open3 =0;
47 }
48
49 #
50 # defaults
51 #
52 # my @versions = ( '5.0.51', '5.0.64', '5.1.23', '5.1.24', '5.1.25', '5.1.26', '6.0.6' );
•53 my @versions = ( );
54 my $verbose = $DEBUG || $ENV{'VERBOSE'} || 0;
55
56 my %tests = (
57 tarball => 1, # 70
58 single => 1, # 29
59 replication => 1, # 31
60 circular => 1, # 28
61 multiple => 1, # 26
62 custom => 1, # 22
63 tuning => 1, # 6
64 sbtool => 0, # 34
65 smoke => 0, # 19
66 user => 0,
67 );
68 my %skip_tests = () ;
69
70 my %expected_scripts = (
71 single => {
72 executables => [qw(start stop clear msb send_kill status change_ports change_paths my restart use json_in_db)],
73 texts => [qw( grants.mysql my.sandbox.cnf connection.json default_connection.json README)],
74 },
75 replication => {
76 executables =>
77 [qw(start_all stop_all clear_all send_kill_all status_all check_slaves restart_all use_all initialize_slaves m s1 s2 )],
78 texts => [qw(connection.json default_connection.json README)],
79 },
80 multiple_custom => {
81 executables =>
82 [qw(start_all stop_all clear_all send_kill_all status_all restart_all use_all n1 )],
83 texts => [qw(connection.json default_connection.json README)],
84 },
85 multiple => {
86 executables =>
87 [qw(start_all stop_all clear_all send_kill_all status_all restart_all use_all n1 n2 n3 )],
88 texts => [qw(connection.json default_connection.json README)],
89 },
90 circular => {
91 executables =>
92 [qw(start_all stop_all clear_all send_kill_all status_all check_slaves restart_all use_all set_circular_replication.sh n1 n2 n3 )],
93 texts => [qw(connection.json default_connection.json README)],
94 },
95 );
96
97
98 # my %custom_tests = ();
99
100 my ($user_tests, $user_versions, $user_defined_test, $get_help, $preserve_tests, $skip_tests);
101
102
103 GetOptions(
104 "tarball|versions=s" => \$user_versions,
105 "tests=s" => \$user_tests,
106 "skip_tests=s" => \$skip_tests,
107 "user_test=s"=> \$user_defined_test,
108 "verbose" => \$verbose,
109 "preserve_tests" => \$preserve_tests,
110 "help|h" => \$get_help,
111 ) or help();
112
113 help() if $get_help;
114
•115 my $detected_mysql_version;
116 my @mysql_config_locations = ( $ENV{"HOME"} . '/bin/mysql_config', '/usr/local/bin/mysql_config', '/usr/bin/mysql_config' );
117 for my $mysql_config_location (@mysql_config_locations) {
118 $detected_mysql_version = qx(mysql_config --version) if -x '/usr/bin/mysql_config';
119 if ($detected_mysql_version) {
120 chomp $detected_mysql_version;
121 print "autodetected version $detected_mysql_version from $mysql_config_location\n";
122 push @versions, $detected_mysql_version;
123 last;
124 }
125 }
126
127 if ($user_versions) {
128 my @new_versions = grep {$_} split /,/, $user_versions;
129 die "at least one version is required\n" unless @new_versions;
130 @versions =();
131 # ensuring that each version is used only once.
132 @new_versions =
133 map { $_->[0] } # sorting with the Schwartzian Transform
134 sort { $a->[1] cmp $b->[1] }
135 map { /(\d+)\.(\d+)\.(\d+)/;
136 [ $_, sprintf('%02d-%02d-%02d',$1,$2,$3)] }
137 keys %{{ map { $_, 1} @new_versions }};
138 for my $ver (@new_versions) {
139 push @versions, $ver;
140 }
141 }
•142 die "at least one version is required\n" unless @versions;
143 for my $ver (@versions) {
•144 print "using version $ver\n";
145 $ver =~ s/^~/$ENV{'HOME'}/;
146 unless (( -d "/opt/mysql/$ver")
147 or ( -d "$ENV{'HOME'}/opt/mysql/$ver")
148 or ( -f $ver )) {
149 print "version $ver not found in either /opt/mysql or $ENV{'HOME'}/opt/mysql\n";
150 die "use --versions to list the versions you want to test\n";
151 }
152 }
153
154 if ($user_defined_test and !$user_tests) {
155 $user_tests = 'user';
156 }
157
158 if ($skip_tests) {
159 my @tests_to_skip = grep {$_} split /,/, $skip_tests;
160 for my $st (@tests_to_skip)
161 {
162 if ($tests{$st})
163 {
164 $tests{$st} = 0;
165 }
166 }
167 }
168
169 if ($user_tests) {
170 my @todo = grep {$_} split /,/, $user_tests;
171 die "at least one test is required\n" unless @todo;
172 my %new_tests;
173 for my $t (@todo) {
174 if (exists $tests{$t} ) {
175 $new_tests{$t}++;
176 }
177 else {
178 die "unrecognized test <$t>\n";
179 }
180 }
181 for my $t (keys %tests) {
182 if ( exists $new_tests{$t} ) {
183 $tests{$t} = 1;
184 }
185 else {
186 $tests{$t} = 0;
187 }
188 }
189 }
190
191 my $sandbox_home = "$ENV{'HOME'}/sandboxes";
192 my %test_results = (
193 run => 0,
194 passed => 0,
195 failed => 0,
196 skipped => 0,
197 );
198 #
199 # cleaning up existing sandbox directory
200 #
201 if ( $ENV{'SANDBOX_HOME'} ) {
202 $sandbox_home = $ENV{'SANDBOX_HOME'};
203 }
204 else {
205 $ENV{'SANDBOX_HOME'} = $sandbox_home;
206 }
207 my $sh_stop_all = "$sandbox_home/stop_all";
208
209 if ( -x $sh_stop_all ) {
210 system("$sh_stop_all > /dev/null 2>&1");
211 }
212
213 #
214 # setting the current sandbox directory for this test
215 #
216 $sandbox_home = $ENV{'TEST_SANDBOX_HOME'} || "$ENV{'HOME'}/test_sb";
217 $ENV{'SANDBOX_HOME'} = $sandbox_home;
218 $sh_stop_all = "$sandbox_home/stop_all";
219 my $sh_clear_all = "$sandbox_home/clear_all";
220 my $sh_start_all = "$sandbox_home/start_all";
221 my $sh_use_all = "$sandbox_home/use_all";
222
223 if ($tests{'smoke'} and $tests{'sbtool'}) {
224 die "test <smoke> and test <sbtool> should not run together\n";
225 }
226
227 #
228 # cleaning up the test directory if exists
229 #
230 if ( -x $sh_stop_all ) {
231 system("$sh_stop_all > /dev/null 2>&1");
232 system qq(rm -rf $sandbox_home) ;
233 }
234
235 #
236 # checking if there are other servers running
237 #
238 my $how_many_mysqld = get_number_of_processes('mysqld');
239 my $how_many_mysqld_safe = get_number_of_processes('mysqld_safe');
240 tprintf "** currently there are (%d) mysqld processes and (%d) mysqld_safe processes\n",
241 $how_many_mysqld,
242 $how_many_mysqld_safe;
243
244 #
245 # starting the tests
246 #
247 for my $ver (@versions) {
248 my ($bare_version, $version) = get_bare_version ($ver);
249
250 if ($tests{'tarball'}) {
251 # print "run_tarball_test($ver, $version, $bare_version)\n";
252 run_tarball_test($ver, $version, $bare_version);
253 }
254
255 if ($tests{'single'}) {
256 # print "run_single_test($ver, $version, $bare_version)\n";
257 run_single_test($ver, $version, $bare_version);
258 }
259
260 if ($tests{'replication'} ) {
261 run_replication_test($ver, $version, $bare_version);
262 }
263
264 if ($tests{'circular'}) {
265 run_circular_test($ver, $version, $bare_version);
266 }
267
268 if ($tests{'multiple'}) {
269 run_multiple_test($ver, $version, $bare_version);
270 }
271 }
272
273 if ($tests{'custom'}) {
274 run_mcustom_test();
275 }
276
277 if ($tests{'user'}) {
278 die "test file required\n" unless $user_defined_test;
279 run_user_defined_test($user_defined_test);
280 }
281
282 my $summaries = 0;
283 for my $test (keys %tests) {
284 if ($tests{$test} && ($test !~ /^(?:tuning|smoke|sbtool|user)$/)) {
285 $summaries = 1;
286 last;
287 }
288 }
289
290 if ($summaries) {
291 run_summary_tests();
292 }
293
294 if ($tests{'tuning'}) {
295 run_tuning_test();
296 }
297
298 if ($tests{'smoke'}) {
299 for my $ver ( @versions ) {
300 if (-f "$sandbox_home/stop_all") {
301 system qq("$sandbox_home/stop_all > /dev/null 2>&1");
302 system qq(rm -rf $sandbox_home/*) ;
303 }
304 run_smoke_test($ver);
305 }
306 }
307
308 if ($tests{'sbtool'}) {
309 for my $ver (@versions) {
310 if ( -d $sandbox_home ) {
311 system "$sandbox_home/stop_all > /dev/null 2>&1";
312 system "rm -rf $sandbox_home";
313 }
314 run_sbtool_test ($ver);
315 }
316 }
317
318
319 tprintf "*** Executed %d tests. Passed %d (%5.2f%%). Failed %d (%5.2f%%)\n",
320 $test_results{'run'},
321 $test_results{'passed'},
322 $test_results{'run'} ? $test_results{'passed'} / $test_results{'run'} * 100 : 0,
323 $test_results{'failed'},
324 $test_results{'run'} ? $test_results{'failed'} / $test_results{'run'} * 100 : 0
325 ;
326
327 if ($test_results{'failed'}) {
328 exit 1;
329 }
330
331 #
332 # ROUTINES
333 #
334
335 #
336 # get_exec_result
337 #
338 # runs a shell command and returns the output
339 #
340 sub get_exec_result {
341 if ($use_open3) {
342 return get_exec_result_open3(@_);
343 }
344 else {
345 return get_exec_result_qx(@_);
346 }
347 }
348
349 sub get_exec_result_qx {
350 my ($cmd) = @_;
351 print "qx(shell) $cmd\n" if $verbose;
352 my $output = qx($cmd 2>/tmp/err$$);
353 if ($?) {
354 die ("error executing $cmd ($!)\n");
355 }
356 if ( -f "/tmp/err$$")
357 {
358 open my $efh, '<', "/tmp/err$$"
359 or die "can't open /tmp/err$$ ($!)\n";
360 my $err_output='';
361 while (my $line = <$efh>)
362 {
363 next if $line =~ /Warning: Using a password/;
364 $err_output .= $line;
365 }
366 close $efh;
367 unlink "/tmp/err$$";
368 #if ($err_output)
369 #{
370 # warn "# error executing $cmd\n";
371 # warn "#$err_output";
372 #}
373 }
374 print "qx(shell) $output\n" if $verbose;
375 return $output;
376 }
377
378 sub get_exec_result_open3 {
379 my ($cmd) = @_;
380 print "o3(shell) $cmd\n" if $verbose;
381 my ($out, $in, $err) = (undef, undef, 1);
382 my $result = open3($in, $out, $err, $cmd );
383 my $output ='';
384 my $err_output ='';
385 while (my $line = <$out>) {
386 $output .= $line;
387 }
388 while (my $line = <$err>) {
389 $err_output .= $line;
390 }
391 if ($? or $err_output) {
392 die ("error executing $cmd ($! - $err_output)\n");
393 }
394 print "o3(shell) $output - $err_output\n" if $verbose;
395 return $output;
396 }
397
398 #
399 # get_sql_result
400 #
401 # runs a SQL command and returns the output
402 #
403 sub get_sql_result {
404 my ($sb, $query) = @_;
405 print "(sql) $query\n" if $verbose;
406 if ( -f "$sb/use" ) {
407 # print qq(<echo "$query" | $sb/use -N -B >\n);
408 my $command = 'use -N -B ';
409 if ($query =~ /show\s+slave\s+status.*\\G/i) {
410 $command = 'use -B';
411 }
412 my $output = qx(echo "$query" | $sb/$command );
413 if (defined $output) {
414 chomp $output;
415 }
416 else {
417 $output = '';
418 }
419 if ($verbose && $verbose > 1) {
420 print "$output\n";
421 }
422 die "error executing query $query on sandbox $sb\n" if $?;
423 return $output;
424 }
425 else {
426 die "can't find a 'use' command on $sb\n";
427 }
428 }
429
430
431 #
432 # get_number_of_processes
433 #
434 # returns the number of processes for a given name
435 #
436 sub get_number_of_processes {
437 my ($proc_name) = @_;
438
439 my $grep_cmd = 'ps -ef | grep -w %s | grep -v "grep -w %s" | wc -l ';
440 my $cmd = sprintf($grep_cmd, $proc_name, $proc_name );
441 tprint "$cmd\n" if ($verbose && ($verbose > 1));
442 my $how_many = get_exec_result($cmd, 0);
443 return $how_many;
444 }
445
446 #
447 # help
448 #
449 # displays option for this program
450 #
451 sub help {
452 my $HELP = <<"HELP";
453
454 test for MySQL Sandbox
455 usage: $0 [options]
456 --versions=version1[,version2,version3]
457 uses specific versions for testing.
458 currently: (@{[join ",", @versions]})
459
460 --tarball=/path/to/tarball
461 it's an alias for --versions
462
463 --tests=testname[,testname,testname]
464 executes specific tests.
465 currently: (@{[join ",", grep {$tests{$_}} keys %tests]})
466
467 --user_test=filename
468 executes user defined tests from given file
469 (implies --tests=user)
470
471 --preserve_tests
472 does not remove sandboxes at the end of the suite.
473
474 --verbose
475 shows the commands executed during tests
476
477 --help
478 shows this help
479
480 HELP
481 print $HELP;
482 exit(1);
483 }
484
485 #
486 # TEST ROUTINES
487 #
488
489 #
490 # ok
491 #
492 # evaluates a condition and prints a ok/not ok message
493 #
494 sub ok ($;$) {
495 my ($condition, $msg) = @_;
496 # print Dumper \@_;
497 $msg = '***' unless defined $msg;
498 $test_results{'run'}++;
499 if ($condition) {
500 $test_results{'passed'}++;
501 }
502 else {
503 print "not ";
504 $test_results{'failed'}++;
505 }
506 print "ok $test_results{'run'} - $msg\n";
507 if ($verbose) { die "halting the test on verbose\n" unless $condition; }
508 return $condition;
509 }
510
511 sub check_sandbox_files
512 {
513 my ($sandbox_type, $sandbox_path, $version) = @_;
514 for my $executable_script (@{ $expected_scripts{$sandbox_type}{executables} })
515 {
516 ok( -x "$sandbox_path/$executable_script", "executable script $executable_script file found");
517 }
518
519 for my $text_script (@{$expected_scripts{$sandbox_type}{texts} })
520 {
521 my $filename = "$sandbox_path/$text_script";
522
523 ok( -f $filename, "text script $text_script file found");
524 if ($filename =~ /\.json$/)
525 {
526 my $is_json = MySQL::Sandbox::validate_json_object($filename);
527 if ($is_json && ($is_json == -1))
528 {
529 ok( 1, " - skipped $filename validation");
530 }
531 else
532 {
533 ok($is_json, "JSON validation $filename");
534 }
535 }
536 }
537 if ($version)
538 {
539 my $expected_tool = 'mysql_install_db';
540 if ( greater_version($version, '5.7.6') && greater_version('10.0.0', $version))
541 {
542 $expected_tool = 'mysqld.*initialize-insecure';
543 }
544 my $trace_file = "$sandbox_path/trace_install.txt";
545 ok ( (-f $trace_file) || 0, "Trace file $trace_file found" );
546 open my $FH , '<', $trace_file
547 or die "Can't open $trace_file ($!)\n";
548 my @lines = <$FH>;
549 my $text = "@lines";
550 close $FH;
551 ok (($text =~ $expected_tool) || 0, "tool $expected_tool found in $trace_file");
552 }
553 }
554
555 sub run_tarball_test{
556 my ($ver, $version, $bare_version) = @_;
557 # print "ver $ver - version $version - bare_version $bare_version\n"; exit;
558 # ver ma10.0.13 - version ma10_0_13 - bare_version 10.0.13
559 # ver 5.7.8 - version 5_7_8 - bare_version 5.7.8
560 # ver ps5.6.40 - version ps5_6_40 - bare_version 5.6.40
561 my $prefix='';
562 my $prefix_option='';
563 my $tmpdir = $ENV{TMPDIR} || '/tmp';
564 if (! -d $tmpdir)
565 {
566 die "Can't find temporary directory $tmpdir\n";
567 }
568 $tmpdir =~ s{/$}{};
569 my $tardir="$tmpdir/tmp$$";
570 mkdir $tardir;
571 if ( ! -d $tardir)
572 {
573 die "Error creating $tardir\n";
574 }
575 my @name_patterns = (
576 'Percona-Server-_VER_-rel61.0.tar.gz',
577 'mariadb-_VER_-osx10.9-x86_64.tar.gz',
578 'mysql-_VER_-osx10.6-x86_64.tar.gz',
579 'mysql-_VER_-m11-osx10.6-x86_64.tar.gz',
580 'mysql-advanced-_VER_-osx10.7-x86_64.tar.gz',
581 'mysql-_VER_-labs-multi-src-rep.tar.gz',
582 '13242627.mysql-_VER_-rc-osx10.8-x86_64.tar.gz',
583 );
584 unless ($ENV{SANDBOX_BINARY})
585 {
586 die "Variable SANDBOX_BINARY not set\n";
587 }
588 unless (-d $ENV{SANDBOX_BINARY})
589 {
590 die "Directory $ENV{SANDBOX_BINARY} not found\n";
591 }
592 my $curdir = $ENV{PWD};
593 if ($ver =~ /^(\D+)/)
594 {
595 $prefix = $1;
596 }
597 for my $pattern (@name_patterns)
598 {
599 $pattern =~ s/_VER_/$ver/;
600 chdir $tardir;
601 my $pattern_dir= $pattern;
602 $pattern_dir =~ s/\.tar.gz//;
603 $pattern_dir =~ s{^\d+\.}{};
604
605 system "cp -R $ENV{SANDBOX_BINARY}/$ver $pattern_dir" ;
606 ok(-d "$tardir/$pattern_dir", "$tardir/$pattern_dir exists") or exit 1;
607 system "tar -c $pattern_dir | gzip -c > $pattern";
608 system "rm -rf $pattern_dir";
609 chdir $curdir;
610 ok(! -d "$tardir/$pattern_dir", "$tardir/$pattern_dir was removed") or exit 1;
611 ok( -f "$tardir/$pattern", "$tardir/$pattern exists") or exit 1;
612 if ($prefix)
613 {
614 $prefix_option= "--add_prefix=$prefix";
615 }
616 ok_exec( {
617 command => "make_sandbox $prefix_option $tardir/$pattern -- --no_confirm",
618 expected => 'sandbox server started',
619 msg => "single tarball SB ($bare_version) started"
620 });
621 ok(-d "$tardir/$ver", "$tardir/$ver was created") or exit 1;
622 ok(-d "$sandbox_home/msb_$version", "$sandbox_home/$version was created") or exit 1;
623 # check_sandbox_files( 'single', "$sandbox_home/msb_$version");
624 ok_sql( {
625 path => "$sandbox_home/msb_$version",
626 query => 'select version(), @@server_id',
627 expected => $bare_version,
628 msg => "single tarball SB version ($bare_version) SQL"
629 });
630 ok_exec( {
631 command => "sbtool -o delete -s $sandbox_home/msb_$version",
632 expected => 'has been removed',
633 msg => "single tarball SB ($bare_version) removed"
634 }) or exit 1;
635 system "rm -rf $tardir/$ver";
636 ok(! -d "$tardir/$bare_version", "$tardir/$bare_version was removed") or exit 1;
637 system "rm -f $tardir/$pattern";
638 ok(! -f "$tardir/$pattern", "$tardir/$pattern was removed") or exit 1;
639 }
640 system "rm -rf $tardir";
641 }
642
643 sub run_single_test {
644 my ($ver, $version, $bare_version) = @_;
645 my $save_trace_install = $ENV{TRACE_INSTALL};
646 $ENV{TRACE_INSTALL} = 1;
647 ok_exec( {
648 command => "make_sandbox $ver -- --no_confirm",
649 expected => 'sandbox server started',
650 msg => "single SB ($bare_version) started"
651 });
652 check_sandbox_files( 'single', "$sandbox_home/msb_$version", $ver);
653 $ENV{TRACE_INSTALL} = $save_trace_install;
654 ok( -f "$sandbox_home/plugin.conf", "plugin template file found");
655 ok_exec( {
656 command => "perl -c $sandbox_home/plugin.conf > /dev/null 2>&1",
657 expected => 'ok',
658 msg => "plugin template compiles OK"
659 });
660 ok_sql( {
661 path => "$sandbox_home/msb_$version",
662 query => 'select version(), @@server_id',
663 expected => $bare_version,
664 msg => "single SB ($bare_version) SQL"
665 });
666 get_sql_result(
667 "$sandbox_home/msb_$version",
668 'create database \`a-a\`', # Bug#278394 - this will fail on 'clear' if not fixed
669 $verbose );
670 }
671
672 sub ok_sql {
673 my ($params, $wanted_result) = @_;
674 for my $p (qw(path query expected msg)) {
675 unless (defined $params->{$p}) {
676 croak "parameter <$p> not defined\n";
677 }
678 }
679 my $result = get_sql_result(
680 $params->{'path'},
681 $params->{'query'},
682 $verbose);
683 $ENV{SQL_RESULT} = $result;
684 if ($wanted_result && ref($wanted_result) eq 'SCALAR') {
685 $$wanted_result = $result;
686 }
687 if (! ref($params->{'expected'}) ) {
688 if ($params->{'expected'} =~ /^ok$/i) {
689 return ok($CHILD_ERROR == 0 , $params->{'msg'});
690 }
691 elsif ($params->{'expected'} =~ s/^\!//) {
692 return ok( $result !~ /$params->{'expected'}/i , $params->{'msg'});
693 }
694 else {
695 return ok( $result =~ /$params->{'expected'}/i , $params->{'msg'});
696 }
697 }
698 elsif (ref($params->{'expected'}) eq 'ARRAY') {
699 my $count = 0;
700 for my $expected (@{$params->{'expected'}}) {
701 $count++;
702 if ($expected =~ /^ok$/i) {
703 ok($CHILD_ERROR == 0 , $params->{'msg'} . "-[$count]");
704 }
705 elsif ( $expected =~ s/^!//) {
706 ok( $result !~ /$expected/i , $params->{'msg'} . "-[$count]");
707 }
708 else {
709 ok( $result =~ /$expected/i , $params->{'msg'} . "-[$count]");
710 }
711 }
712 }
713 else {
714 croak "parameter 'expected' is neither a scalar nor an array reference\n";
715 }
716 }
717
718 sub ok_exec {
719 my ($params) = @_;
720 for my $p (qw(command expected msg)) {
721 unless (defined $params->{$p}) {
722 croak "parameter <$p> not defined\n";
723 }
724 }
725 my $return_code_wanted = 0;
726
727 if (ref $params->{'expected'}) {
728 $return_code_wanted = grep { /^ok$/i} @{ $params->{'expected'}};
729 }
730 else {
731 $return_code_wanted = $params->{'expected'} =~ /^ok$/i;
732 }
733 my $result;
734 if ($return_code_wanted) {
735 $result = get_exec_result_qx($params->{'command'});
736 }
737 else {
738 $result = get_exec_result($params->{'command'});
739 }
740 if ($verbose) {
741 print $result, "\n";
742 }
743 $ENV{EXEC_RESULT} = $result;
744 if (! ref $params->{'expected'} ) {
745 if ($return_code_wanted) {
746 return ok($CHILD_ERROR == 0 , $params->{'msg'});
747 }
748 elsif ($params->{'expected'} =~ s/^\!//) {
749 return ok( $result !~ /\Q$params->{'expected'}\E/, $params->{'msg'} );
750 }
751 else {
752 return ok( $result =~ /\Q$params->{'expected'}\E/, $params->{'msg'} );
753 }
754 }
755 elsif (ref( $params->{'expected'}) eq 'ARRAY') {
756 my $count =0;
757 for my $expected (@{$params->{'expected'}}) {
758 $count++;
759 if ($expected =~ /^ok$/i) {
760 ok( $CHILD_ERROR == 0 ,
761 $params->{'msg'} . "-[$count]");
762 }
763 elsif ($expected =~ s/^\!//) {
764 ok( $result !~ /\Q$expected\E/, $params->{'msg'} . "-[$count]" );
765 }
766 else {
767 ok( $result =~ /\Q$expected\E/, $params->{'msg'} . "-[$count]" );
768 }
769 }
770 }
771 else {
772 croak "parameter 'expected' is neither a scalar nor an array reference\n";
773 }
774 }
775
776 sub run_replication_test {
777 my ($ver, $version, $bare_version) = @_;
778
779 ok_exec({ command => "make_replication_sandbox $ver",
780 expected => '!not started yet',
781 msg => "replication sandbox ($bare_version) started"
782 });
783
784 check_sandbox_files( 'replication', "$sandbox_home/rsandbox_$version");
785
786 ok_sql ({ path => "$sandbox_home/rsandbox_$version/master",
787 query => 'select version(), @@server_id',
788 expected => [ $bare_version, '\b1\s*$'],
789 msg => "replication SB master ($bare_version) SQL - version - server_id"
790 });
791
792 sleep 1;
793
794 for my $node (1, 2) {
795 ok_sql ({ path => "$sandbox_home/rsandbox_$version/node$node",
796 query => 'select version(), @@server_id',
797 expected => [ $bare_version, '\b10' . $node . '\s*$'],
798 msg => "replication SB slave$node ($bare_version) SQL - version - server_id"
799 });
800 }
801
802 ok_sql ({ path => "$sandbox_home/rsandbox_$version/master",
803 query => q{drop table if exists test.t1; create table test.t1 (id int); show tables from test},
804 expected => 't1',
805 msg => "replication SB - table created on master"
806 });
807
808 for my $node (1, 2) {
809 ok_sql ({ path => "$sandbox_home/rsandbox_$version/node$node",
810 query => q{show tables from test},
811 expected => 't1',
812 msg => "replication SB - table exists on slave$node"
813 });
814 }
815 }
816
817 sub run_circular_test {
818 my ($ver, $version, $bare_version) = @_;
819
820 ok_exec({ command => "make_replication_sandbox --how_many_slaves=3 --topology=circular $ver",
821 expected => '!not started yet',
822 msg => "circular replication sandbox ($bare_version) started",
823 });
824
825 check_sandbox_files( 'circular', "$sandbox_home/rcsandbox_$version");
826 for my $node ( 1 .. 3) {
827 ok_sql ({ path => "$sandbox_home/rcsandbox_$version/node$node",
828 query => 'show slave status\G',
829 expected => [ 'IO_Running.+Yes', 'SQL_Running.+Yes' ],
830 msg => "circular replication SB node$node ($bare_version) SQL - Running"
831 }
832 );
833 sleep 1;
834 }
835 }
836
837 sub run_multiple_test {
838 my ($ver, $version, $bare_version) = @_;
839
840 ok_exec({ command => "make_multiple_sandbox $ver",
841 expected => 'group directory installed',
842 msg => "multiple sandbox ($bare_version) started"
843 });
844
845 check_sandbox_files( 'multiple', "$sandbox_home/multi_msb_$version");
846 for my $node (1 .. 3 ) {
847
848 ok_sql({ path => "$sandbox_home/multi_msb_$version/node$node",
849 query => 'select version(), @@server_id',
850 expected => [ $bare_version, '\b10' . $node . '\s*$' ],
851 msg => "multiple SB node $node ($bare_version) SQL - version - server_id"
852 });
853 }
854 }
855
856 sub run_mcustom_test {
857
858 my $gdname = 'multi_cmsb_' . get_bare_version($versions[0]) ;
859 #$custom_dir =~ s/ /-/g;
860 #$custom_dir =~ s/\./_/g;
861 # $DEBUG=2; $verbose=2;
862 $gdname =~ s/\./_/g;
863 my $custom_dir = "$sandbox_home/$gdname";
864 ok_exec({ command => "make_multiple_custom_sandbox --group_directory=$gdname @versions ",
865 expected => [
866 '!not started yet',
867 "group directory installed in " . use_env($custom_dir)
868 ],
869 msg => "custom sandbox ( ". use_env($custom_dir)." ) started"
870 });
871 ok(-d $custom_dir, 'custom group directory exists')
872 or die "custom group directory not created\n";
873 check_sandbox_files('multiple_custom', $custom_dir);
874 my $counter = 0;
875 for my $ver (@versions) {
876 my $bare_version = get_bare_version($ver);
877 $counter++;
878 if ( -d "$custom_dir/node$counter" ) {
879 ok_sql({ path => "$custom_dir/node$counter",
880 query => 'select version(), @@server_id',
881 expected => [$bare_version, '\b10' . $counter . '\s*$'],
882 msg => "multiple custom SB node $counter ($bare_version) SQL - version - server_id"
883 });
884 }
885 else {
886 ok(1, 'test skipped');
887 }
888 }
889 }
890
891 sub run_summary_tests {
892 my $new_mysqld_procs = get_number_of_processes('mysqld');
893 my $new_mysqld_safe_procs = get_number_of_processes('mysqld_safe');
894
895 tprintf "** created (%d) mysqld processes and (%d) mysqld_safe processes\n",
896 $new_mysqld_procs - $how_many_mysqld,
897 $new_mysqld_safe_procs - $how_many_mysqld_safe;
898
899 my $instances =
900 ( 1 * $tests{'single'} # single
901 + 3 * $tests{'replication'} # replicated
902 + 3 * $tests{'circular'} # circular
903 + 3 * $tests{'multiple'} # multiple
904 );
905 my $expected_processes =
906 $instances * scalar(@versions)
907 + (scalar(@versions) * $tests{'custom'}) ; # custom counts only once
908
909 ok( $expected_processes == ($new_mysqld_safe_procs - $how_many_mysqld_safe),
910 "expected processes ($expected_processes)" );
911
912 tprint "** stopping all - please wait\n";
913 my $stop_all = get_exec_result("$sandbox_home/stop_all");
914
915 $new_mysqld_procs = get_number_of_processes('mysqld');
916 $new_mysqld_safe_procs = get_number_of_processes('mysqld_safe');
917
918 tprintf "** (%d) mysqld processes and (%d) mysqld_safe processes\n",
919 $new_mysqld_procs - $how_many_mysqld,
920 $new_mysqld_safe_procs - $how_many_mysqld_safe;
921 ok( ($new_mysqld_safe_procs - $how_many_mysqld_safe ) == 0,
922 'expected processes (0)' );
923
924 tprint "** starting all - please wait\n";
925 my $start_all = get_exec_result("$sandbox_home/start_all");
926
927 $new_mysqld_procs = get_number_of_processes('mysqld');
928 $new_mysqld_safe_procs = get_number_of_processes('mysqld_safe');
929 tprintf "** created (%d) mysqld processes and (%d) mysqld_safe processes\n",
930 $new_mysqld_procs - $how_many_mysqld,
931 $new_mysqld_safe_procs - $how_many_mysqld_safe;
932
933 ok( $expected_processes == ($new_mysqld_safe_procs - $how_many_mysqld_safe),
934 "expected processes ($expected_processes)" );
935
936 unless ($ENV{'PRESERVE_TESTS'} or $preserve_tests) {
937 tprint "** cleaning up - please wait\n";
938 my $clear_all = get_exec_result("$sandbox_home/clear_all");
939
940 $new_mysqld_procs = get_number_of_processes('mysqld');
941 $new_mysqld_safe_procs = get_number_of_processes('mysqld_safe');
942
943 tprintf "** (%d) mysqld processes and (%d) mysqld_safe processes\n",
944 $new_mysqld_procs - $how_many_mysqld,
945 $new_mysqld_safe_procs - $how_many_mysqld_safe;
946 ok( ($new_mysqld_safe_procs - $how_many_mysqld_safe ) == 0,
947 'expected processes (0)' )
948 or die "can't continue without a clean environment\n";
949
950 system qq(rm -rf $sandbox_home/*) ;
951 }
952 }
953
954 sub run_tuning_test {
955 my $ver;
956 for my $v (@versions) {
957 $ver = $v;
958 if ($ver =~ /^[^34]/) {
959 last;
960 }
961 }
962 if ($ver =~ /^[34]/) {
963 print "skipping tuning test. It requires version >=5\n";
964 return;
965 }
966 my ($bare_version, $version) = get_bare_version($ver);
967
968 if (($bare_version =~ /^5\.([567])/ ) || ($bare_version =~ /^8/ ) || ($bare_version =~ /^10/ )) {
969 for my $i ( 1 .. 6) {
970 print "ok - skipped (this test is not available in MySQL $bare_version)\n";
971 }
972 return;
973 }
974
975 ok_exec({ command => "make_sandbox $ver -- --no_confirm -c skip-innodb -c sql_mode=strict_all_tables",
976 expected => 'sandbox server started',
977 msg => 'tuning server created',
978 });
979
980 ok_sql({ path => "$sandbox_home/msb_$version",
981 query => 'show engines',
982 expected => '!innodb\s*yes',
983 msg => "single SB with option skip-innodb ($bare_version) SQL"
984 });
985
986 ok_sql({ path => "$sandbox_home/msb_$version",
987 query => q(show variables like 'SQL_MODE'),
988 expected => 'STRICT_ALL_TABLES',
989 msg => "single SB with option sql_mode ($bare_version) SQL "
990 });
991
992 ok_sql({ path => "$sandbox_home/msb_$version",
993 query => q(create database xyz; show databases like 'xyz'),
994 expected => 'xyz',
995 msg => "single SB ($bare_version) - create database"
996 });
997
998 ok_exec({ command => "$sandbox_home/msb_$version/clear",
999 expected => '!error',
1000 msg => "single SB ($bare_version) - clear result"
1001 });
1002
1003 # my $sandbox_dirs = get_exec_result("ls -d $sandbox_home/msb_$version/data/*/ | wc -l ");
1004 my $sandbox_dirs = how_many_dirs("$sandbox_home/msb_$version/data");
1005
1006 ok($sandbox_dirs == ($bare_version ge '5.5'? 3 : 2) , "single SB ($bare_version) - effective clear ");
1007
1008 tprint "** cleaning up - please wait\n";
1009 my $clear_all = get_exec_result("$sandbox_home/clear_all");
1010 }
1011
1012 sub get_bare_version {
1013 my ($ver) = @_;
1014 $ver =~ s{.*/}{};
1015 if ($ver =~ /((?:\w+)?(\d+)\.(\d+)\.(\d+))/) {
1016 my $bv = $1;
1017 my $major = $2;
1018 my $minor = $3;
1019 my $rev = $4;
1020 my $underscored_version = $bv;
1021 $underscored_version =~ s/\./_/g;
1022 $bv =~ s/^\D+//;
1023 # print STDERR "$ver, $bv, $underscored_version \n"; exit;
1024 if (wantarray) {
1025 return ($bv, $underscored_version, $major, $minor, $rev);
1026 }
1027 else {
1028 return $bv;
1029 }
1030 }
1031 else {
1032 die "'$ver' does not contain a valid version\n";
1033 }
1034 }
1035
1036 sub run_smoke_test {
1037 my ($ver) = @_;
1038 if ($ver =~ /^[34]/) {
1039 tprint "skipping smoke test. It requires version >=5\n";
1040 return;
1041 }
1042 my ($bare_version, $version) = get_bare_version($ver);
1043 $version =~ s/\./_/g;
1044 if ($ver =~ m{(.+)/[^/]+(?:tgz|tar\.gz)$}) {
1045 my $bindir = $1;
1046 if ( -d "$bindir/$bare_version" ) {
1047 system "rm -rf $bindir/$bare_version";
1048 }
1049 }
1050
1051 my $previous_mysqld = get_number_of_processes('mysqld');
1052 my $previous_mysqld_safe = get_number_of_processes('mysqld_safe');
1053
1054 ok_exec({ command => "make_sandbox $ver -- --no_confirm",
1055 expected => 'sandbox server started',
1056 msg => "single SB ($bare_version) started"
1057 });
1058
1059 my $first_mysqld = get_number_of_processes('mysqld');
1060 my $first_mysqld_safe = get_number_of_processes('mysqld_safe');
1061
1062 ok($first_mysqld_safe > $previous_mysqld_safe, 'mysqld_safe started') ;
1063 ok($first_mysqld > $previous_mysqld, 'mysqld started') ;
1064
1065 my $sql_result = get_sql_result(
1066 "$sandbox_home/msb_$version",
1067 q{show variables like 'pid_file'},
1068 $verbose);
1069 my $pid_file ;
1070 if ($sql_result =~ m{\s*(\S+\.pid)} ) {
1071 $pid_file = $1;
1072 }
1073 ok ($pid_file , 'pid_file found')
1074 or die "can't find pid file\n";
1075
1076 my $pid = get_pid($pid_file)
1077 or die "can't get PID from $pid_file\n" ;
1078 my $pid_ts1 = get_pid_timestamp($pid_file)
1079 or die "can't get timestamp for file $pid_file\n";
1080
1081 my $kill_result = get_exec_result("kill -9 $pid");
1082 ok (! $kill_result, "mysqld killed");
1083
1084 my $timeout = 20;
1085 my $counter = 0;
1086 my $second_mysqld = 0;
1087 my $started ;
1088 while (! $started ) {
1089 $counter++;
1090 if ($counter >= $timeout) {
1091 last;
1092 }
1093 $second_mysqld = get_number_of_processes('mysqld');
1094 $started = $second_mysqld >= $first_mysqld;
1095 unless ($started) {
1096 tprint "-- waiting for mysqld to restart ($counter)\n";
1097 sleep 1;
1098 }
1099 }
1100 my $pid_ts2 = get_pid_timestamp($pid_file);
1101 $counter = 0;
1102 # print "PID_TS ==== $pid_ts1 $pid_ts2\n";
1103 while ((!$pid_ts2) or ($pid_ts2 eq $pid_ts1)) {
1104 $counter++;
1105 tprint ">> waiting for mysqld to restart ($counter)\n";
1106 sleep 1;
1107 $pid_ts2 = get_pid($pid_file);
1108 if ($counter > $timeout) {
1109 die "error recovering killed process\n";
1110 }
1111 }
1112 ok ($second_mysqld >= $first_mysqld, "new mysqld process created (1)") ;
1113 my $secondpid = get_pid($pid_file)
1114 or die "can't get PID from $pid_file\n" ;
1115
1116 chomp $secondpid;
1117 tprint "previous pid ($pid) current pid ($secondpid)\n" if $verbose;
1118 ok ($secondpid ne $pid, "new mysqld process created (2)");
1119
1120 ok_sql({ path => "$sandbox_home/msb_$version",
1121 query => 'select version()',
1122 expected => $bare_version,
1123 msg => "single SB ($bare_version) version (1)"
1124 });
1125
1126 my $visual_version ;
1127
1128 ok_sql({ path => "$sandbox_home/msb_$version",
1129 query => q{show variables like 'version'},
1130 expected => $bare_version,
1131 msg => "single SB ($bare_version) version (2)"
1132 }, \$visual_version);
1133
1134 chomp $visual_version;
1135 $sql_result = get_sql_result(
1136 "$sandbox_home/msb_$version",
1137 'select @@version_comment limit 1',
1138 $verbose );
1139 my $visual_comment = $sql_result;
1140 chomp $visual_comment;
1141
1142 $sql_result = get_sql_result(
1143 "$sandbox_home/msb_$version",
1144 'HELP SELECT',
1145 $verbose );
1146 # print "HELP: $sql_result\n";
1147 ok( $sql_result && (! ( $sql_result =~ /nothing\s*found/i)),
1148 "single SB ($bare_version) HELP tables filled " );
1149
1150 ok_sql ({ path => "$sandbox_home/msb_$version",
1151 query => q(create database install_test; show databases like 'install_test'),
1152 expected => 'install_test',
1153 msg => "single SB ($bare_version) - create database"
1154 });
1155
1156 my @test_engines = (
1157 {
1158 min_version => 5 ,
1159 engine => 'innodb'
1160 },
1161 #{
1162 # min_version => 6 ,
1163 # engine => 'innodb'
1164 #}
1165 );
1166
1167 for my $test_engine (@test_engines) {
1168 my $major_version = 0;
1169 if ($version =~ /(\d+)/) {
1170 $major_version = $1;
1171 }
1172 if ($major_version < $test_engine->{'min_version'} ) {
1173 next;
1174 }
1175 my $engine = $test_engine->{'engine'};
1176
1177 ok_sql ({ path => "$sandbox_home/msb_$version",
1178 query =>
1179 qq{drop table if exists install_test.testib;
1180 create table install_test.testib (
1181 id int UNSIGNED NOT NULL AUTO_INCREMENT ,
1182 nr int UNSIGNED NOT NULL ,
1183 PRIMARY KEY ( id)
1184 ) engine=$engine; show tables from install_test },
1185 expected => 'testib',
1186 msg => "single SB ($bare_version) - create table"
1187 });
1188
1189 ok_sql ({ path => "$sandbox_home/msb_$version",
1190 query => q{select engine from information_schema.tables
1191 where table_schema='install_test'
1192 and table_name = 'testib' },
1193 expected => $engine,
1194 msg => "single SB ($bare_version) - create table - check engine ($engine)"
1195 });
1196
1197 ok_sql ({ path => "$sandbox_home/msb_$version",
1198 query => q{alter table install_test.testib add index nr (nr);
1199 insert into install_test.testib(id,nr) values ( '1','1');
1200 insert into install_test.testib(id,nr) values ( '2','2');
1201 insert into install_test.testib(id,nr) values ( '3','2');
1202 select * from install_test.testib where nr=2 order by id asc;},
1203 expected => '2\t2\s*3\t2',
1204 msg => "smoke 1"
1205 });
1206
1207 }
1208
1209 ok_sql ({ path => "$sandbox_home/msb_$version",
1210 query => q{select * from install_test.testib where nr=2 order by id desc;},
1211 expected => '3\t2\s*2\t2',
1212 msg => "smoke 2"
1213 });
1214
1215 ok_sql ({ path => "$sandbox_home/msb_$version",
1216 query => q{select count(*) from install_test.testib},
1217 expected => '^3$',
1218 msg => "rows in table"
1219 });
1220
1221 ok_sql ({ path => "$sandbox_home/msb_$version",
1222 query => q{truncate table install_test.testib;}
1223 . q{select count(*) from install_test.testib},
1224 expected => '^0$',
1225 msg => "table truncation"
1226 });
1227
1228 ok_sql ({ path => "$sandbox_home/msb_$version",
1229 query => q{drop table install_test.testib;}
1230 . q{select count(*) from information_schema.tables }
1231 . q{where table_schema='install_test'},
1232 expected => '^0$',
1233 msg => "tables in install_test "
1234 });
1235 $sql_result = get_sql_result(
1236 "$sandbox_home/msb_$version",
1237 q{ drop database install_test;},
1238 $verbose);
1239 my $sandbox_dirs = how_many_dirs("$sandbox_home/msb_$version/data");
1240 my $expected_dirs = 2;
1241 if (greater_version( $bare_version, '5.7.6') && ( $bare_version !~ /^10/))
1242 {
1243 $expected_dirs = 4;
1244 }
1245 elsif ($bare_version =~ /^10/)
1246 {
1247 $expected_dirs = 3;
1248 }
1249 elsif ($bare_version ge '5.5')
1250 {
1251 $expected_dirs = 3;
1252 }
1253
1254 #ok($sandbox_dirs == ((($bare_version ge '5.5') or ($bare_version =~ /^10/)) ? 3 : 2) , "single SB ($bare_version) - effective clean up ");
1255 ok($sandbox_dirs == $expected_dirs, "single SB ($bare_version) - effective clean up ");
1256
1257 tprint "** cleaning up - please wait\n";
1258 my $clear = get_exec_result("$sandbox_home/msb_$version/clear");
1259 if ($ver =~ m{(.+)/[^/]+(?:tgz|tar\.gz)$}) {
1260 my $bindir = $1;
1261 if ( -d "$bindir/$bare_version" ) {
1262 system "rm -rf $bindir/$bare_version";
1263 system "rm -rf $sandbox_home/msb_$version";
1264 }
1265 }
1266
1267 tprint "\n[VISUAL IDENTIFICATION]\n";
1268 tprint "\tcheck version: <$visual_version>\n";
1269 tprint "\tcheck comment: <$visual_comment>\n\n";
1270 }
1271
1272
1273 sub run_sbtool_test {
1274 my ($ver) = @_;
1275
1276 #
1277 # preparing sandboxes
1278 #
1279 my $first_sandbox = get_exec_result(
1280 "make_sandbox $ver -- --no_confirm --sandbox_port=5100 --sandbox_directory=first_sb",
1281 $verbose);
1282 ok( $first_sandbox =~ /sandbox server started/,
1283 "first SB (5100) started" );
1284
1285
1286 my $second_sandbox = get_exec_result(
1287 "make_sandbox $ver -- --no_confirm --sandbox_port=5200 --sandbox_directory=second_sb",
1288 $verbose);
1289 ok( $second_sandbox =~ /sandbox server started/,
1290 "second SB (5200) started" );
1291
1292 #
1293 # sbtool -o move
1294 #
1295 my $move_sandbox = get_exec_result("sbtool -o move -s $sandbox_home/first_sb -d $sandbox_home/xxx");
1296 ok( $move_sandbox =~ /The old scripts have been saved/, 'first SB moved to xxx');
1297
1298 $first_sandbox = get_exec_result("$sandbox_home/xxx/start");
1299 ok( $first_sandbox =~ /sandbox server started/, "moved sandbox started");
1300
1301 $move_sandbox = get_exec_result("sbtool -o move -d $sandbox_home/first_sb -s $sandbox_home/xxx");
1302 ok( $move_sandbox =~ /The old scripts have been saved/, 'xxx moved back to first SB');
1303
1304 $first_sandbox = get_exec_result("$sandbox_home/first_sb/start");
1305 ok( $first_sandbox =~ /sandbox server started/, "moved back sandbox started");
1306
1307 #
1308 # sbtool -o copy
1309 #
1310 my $sql_result = get_sql_result(
1311 "$sandbox_home/first_sb",
1312 q{drop table if exists test.t1; create table test.t1 (id int); show tables from test},
1313 $verbose);
1314 ok( ($sql_result =~ /t1/) ,
1315 "first SB - table created" );
1316
1317 my $copy_sandbox = get_exec_result("sbtool -o copy -s $sandbox_home/first_sb -d $sandbox_home/second_sb");
1318 ok( $CHILD_ERROR == 0 , 'first SB copied to second SB');
1319
1320 $second_sandbox = get_exec_result("$sandbox_home/second_sb/start");
1321 ok( $second_sandbox =~ /sandbox server started/, "copied sandbox started");
1322
1323 $sql_result = get_sql_result(
1324 "$sandbox_home/second_sb",
1325 q{show tables from test},
1326 $verbose);
1327 ok( ($sql_result =~ /t1/) ,
1328 "second SB - table copied" );
1329
1330 #
1331 # sbtool -o ports
1332 #
1333 my $get_ports = get_exec_result("sbtool -o ports");
1334 ok ($get_ports =~ /5100\s+0/ && $get_ports =~ /5200\s+1/, 'get ports');
1335 $get_ports = get_exec_result("sbtool --only_used -o ports");
1336 ok ($get_ports !~ /5100\s+0/ && $get_ports =~ /5200\s+1/, 'get used ports');
1337
1338 $get_ports = get_exec_result("sbtool --format=perl -o ports");
1339 # print $get_ports, "\n";
1340 my $ports = undef;
1341 eval "$get_ports";
1342 ok ( $ports && !$ports->{'5100'} && $ports->{'5200'}, 'get ports (perl format)');
1343
1344 #
1345 # sbtool -o info
1346 #
1347 my $get_info = get_exec_result("sbtool -o info");
1348 my $all_info = undef;
1349 eval "$get_info";
1350 ok($all_info && $all_info->{'5100'}{'opt'}{'port'} == 5100, 'get info 1');
1351 ok($all_info && $all_info->{'5200'}{'opt'}{'port'} == 5200, 'get info 2');
1352
1353 #
1354 # sbtool -o port
1355 # (change port)
1356 #
1357 my $change_ports = get_exec_result("sbtool -o port --new_port=9000 -s $sandbox_home/first_sb");
1358
1359 ok( $change_ports =~ /The old scripts have been saved/, 'port changed to first SB');
1360
1361 $change_ports = get_exec_result("$sandbox_home/first_sb/start");
1362 ok( $change_ports =~ /sandbox server started/, 'first SB restarted after port changed');
1363
1364 $sql_result = get_sql_result( "$sandbox_home/first_sb", q{show variables like 'port'});
1365 ok($sql_result =~ /\b9000\b/, 'wanted port assigned to first SB');
1366
1367 #
1368 # sbtool -o range
1369 #
1370 my $ports_range = get_exec_result("sbtool -o range");
1371 ok ($ports_range =~ /5010/, 'ports range');
1372 $ports_range = get_exec_result("sbtool -o range -i 5200");
1373 ok ($ports_range =~ /5201/, 'ports range 2');
1374 system("$sandbox_home/stop_all > /dev/null 2>&1");
1375 system("$sandbox_home/second_sb/start > /dev/null 2>&1");
1376
1377 #
1378 # sbtool -o delete
1379 #
1380 my $delete_sandbox = get_exec_result("sbtool -o delete -s $sandbox_home/first_sb");
1381 ok($delete_sandbox =~ /has been removed/, 'delete stopped sandbox');
1382 $delete_sandbox = get_exec_result("sbtool -o delete -s $sandbox_home/second_sb");
1383 ok($delete_sandbox =~ /has been removed/, 'delete active sandbox');
1384
1385 $second_sandbox = get_exec_result(
1386 "make_sandbox $ver -- --no_confirm --sandbox_port=5200 --sandbox_directory=second_sb",
1387 $verbose);
1388 ok( $second_sandbox =~ /sandbox server started/,
1389 "second SB (5200) started" );
1390
1391 #
1392 # sbtool -o preserve
1393 #
1394 my $preserve_sandbox = get_exec_result("sbtool -o preserve -s $sandbox_home/second_sb");
1395 ok($preserve_sandbox =~ /is now permanent/, 'preserve active sandbox');
1396 $delete_sandbox = get_exec_result("sbtool -o delete -s $sandbox_home/second_sb");
1397 ok($delete_sandbox =~ /can't be deleted/, '(not) deleting permanent sandbox');
1398
1399 my $unpreserve_sandbox = get_exec_result("sbtool -o unpreserve -s $sandbox_home/second_sb");
1400 ok($unpreserve_sandbox =~ /is now NOT PERMANENT/, 'unpreserve active sandbox');
1401
1402 $delete_sandbox = get_exec_result("sbtool -o delete -s $sandbox_home/second_sb");
1403 ok($delete_sandbox =~ /has been removed/, 'deleting unpreserved sandbox');
1404
1405 #
1406 # sbtool -o tree
1407 #
1408 my $group_sandbox = get_exec_result(
1409 "make_multiple_sandbox --group_directory=tree_sb --how_many_nodes=9 $ver",
1410 $verbose);
1411
1412 ok( $group_sandbox =~ /installing node 9/,
1413 "group SB (node 9) installed" );
1414
1415 ok( $group_sandbox =~ /group directory installed/,
1416 "group SB installed" );
1417
1418 # workaround for MySQL 5.7.7 behavioral change with relay nodes
1419 #$sql_result = get_exec_result(
1420 # "$sandbox_home/tree_sb/use_all 'reset master'",
1421 # $verbose);
1422
1423 my $tree_sandbox = get_exec_result("sbtool -o tree --tree_dir=$sandbox_home/tree_sb"
1424 . " --tree_nodes='1-2 3 4-5 6|7|8 9'" );
1425 ok($tree_sandbox =~ 'node 1 is master', 'tree SB master');
1426 ok($tree_sandbox =~ 'node 2 is slave of node 1', 'tree SB - level 1');
1427 ok($tree_sandbox =~ 'node 7 is slave of node 3', 'tree SB - level 2');
1428
1429 $sql_result = get_sql_result(
1430 "$sandbox_home/tree_sb/node1",
1431 q{drop table if exists test.t1; create table test.t1 (id int); show tables from test},
1432 $verbose);
1433 ok( ($sql_result =~ /t1/) ,
1434 "tree SB - table created" );
1435
1436 sleep 1;
1437 $sql_result = get_sql_result(
1438 "$sandbox_home/tree_sb/node8",
1439 q{show tables from test},
1440 $verbose);
1441 #print "$sql_result\n";
1442 ok( ($sql_result =~ /t1/) ,
1443 "tree SB - table replicated to level 3" );
1444
1445 system("$sandbox_home/send_kill_all > /dev/null 2>&1");
1446 system("$sandbox_home/clear_all > /dev/null 2>&1");
1447 }
1448
1449 sub get_pid {
1450 my ($pfile) = @_;
1451 my $timeout = 5;
1452 my $counter = 0;
1453 my $PFILE;
1454 while (! $PFILE) {
1455 eval {
1456 open( $PFILE, q{<}, $pfile)
1457 or die;
1458 };
1459 next if $@;
1460 last if $PFILE;
1461 if ($counter >= $timeout) {
1462 die "can't open $pfile\n";
1463 }
1464 else {
1465 sleep 1;
1466 }
1467 $counter++;
1468 }
1469 print Dumper $PFILE if $verbose;
1470 die "can't open $pfile\n" unless $PFILE;
1471 my $pid = <$PFILE>;
1472 close $PFILE;
1473 chomp $pid if $pid;
1474 if ($pid && ($pid =~ /^\d+$/) ) {
1475 return $pid;
1476 }
1477 return 0;
1478 }
1479
1480 sub get_pid_timestamp {
1481 my ($pidfile) = @_;
1482
1483 my @stats = stat $pidfile
1484 or return 0;
1485 # or die "can't get timestamp for file $pidfile ($!)\n";
1486 return $stats[8];
1487 }
1488
1489 sub how_many_dirs {
1490 my ($path) = @_;
1491 my @subdirs = glob("$path/*/");
1492 my $dir_count =0;
1493 # warn Dumper \@subdirs;
1494 for (@subdirs) {
1495 $dir_count++ if -d $_;
1496 }
1497 return $dir_count;
1498 }
1499
1500 sub tprint {
1501 unless ($ENV{'TAP_MODE'}) {
1502 print @_;
1503 }
1504 }
1505
1506 sub tprintf {
1507 unless ($ENV{'TAP_MODE'}) {
1508 printf @_;
1509 }
1510 }
1511
1512 sub run_user_defined_test {
1513 my ($test_file) = @_;
1514 if ($test_file =~ /\.sb\.pl$/) {
1515 return run_user_defined_perl_test($test_file);
1516 }
1517 my %test_structs = (
1518 shell => {required => [qw(command expected msg)], rec => undef},
1519 sql => {required => [qw(path query expected msg)], rec => undef},
1520 );
1521 my @user_tests = ();
1522 my $current_struct = undef;
1523 open my $TFILE, q{<}, $test_file
1524 or die "can't open $test_file ($!)\n";
1525 while (my $line = <$TFILE>) {
1526 next if $line =~ /^\s*$/;
1527 next if $line =~ /^\s*#/;
1528 chomp($line);
1529 if ($line =~ /(\w+):/) {
1530 my $struct = $1;
1531 unless ($test_structs{$struct}) {
1532 die "unrecognized test type\n"
1533 }
1534 if ($current_struct) {
1535 my %rec = %{$test_structs{$current_struct}{rec}};
1536 push @user_tests, [$current_struct,\%rec];
1537 $test_structs{$current_struct}{rec} = undef;
1538 }
1539 $current_struct = $struct;
1540 }
1541 elsif ( $line =~ /(\w+)\s*=\s*(.*)/ ) {
1542 my $key = $1;
1543 my $value = $2;
1544 $value =~ s/\$(\w+)/$ENV{$1}/g;
1545 $test_structs{$current_struct}{rec}{$key} = $value;
1546 }
1547 else {
1548 die "error parsing line <$.> at file <$test_file>\n";
1549 }
1550 }
1551 close $TFILE;
1552 if ($current_struct) {
1553 my %rec = %{$test_structs{$current_struct}{rec}};
1554 push @user_tests, [$current_struct,\%rec];
1555 $test_structs{$current_struct}{rec} = undef;
1556 }
1557 for my $test (@user_tests) {
1558 for my $req (@{ $test_structs{$test->[0]}{required}}) {
1559 unless (defined $test->[1]{$req}) {
1560 for my $key (keys %{ $test->[1] } ) {
1561 tprintf "%20s => %s\n", $key, $test->[1]{$key};
1562 }
1563 die "incomplete test. Missing required <$req> label\n";
1564 }
1565 }
1566 }
1567 # print Dumper \@user_tests; exit;
1568 for my $test (@user_tests) {
1569 my $rec = $test->[1];
1570 # print Dumper $test;
1571 if ($test->[0] eq 'shell') {
1572 ok_exec({ command => $rec->{command},
1573 expected => $rec->{expected},
1574 msg => $rec->{msg},
1575 });
1576 }
1577 elsif ($test->[0] eq 'sql') {
1578 ok_sql({ path => $rec->{path},
1579 query => $rec->{query},
1580 expected => $rec->{expected},
1581 msg => $rec->{msg},
1582 });
1583
1584 }
1585 else {
1586 die "unhandled test <$test->[0]>\n";
1587 }
1588 }
1589
1590 }
1591
1592 sub run_user_defined_perl_test {
1593 my ($test_file) = @_;
1594 open my $FH, q{<}, $test_file
1595 or die "can't open $test_file ($!)\n";
1596 my $test_contents ='';
1597 while (my $line = <$FH>) {
1598 $test_contents .= $line;
1599 }
1600 close ($FH);
1601 eval $test_contents;
1602 if ($@) {
1603 die "failed test $test_file\n$@\n";
1604 }
1605 }
1606