summaryrefslogtreecommitdiff
path: root/mysql-test/lib/t
diff options
context:
space:
mode:
Diffstat (limited to 'mysql-test/lib/t')
-rw-r--r--mysql-test/lib/t/Base.t27
-rw-r--r--mysql-test/lib/t/Find.t33
-rw-r--r--mysql-test/lib/t/SafeProcess.t102
-rwxr-xr-xmysql-test/lib/t/SafeProcessStress.pl149
-rw-r--r--mysql-test/lib/t/copytree.t34
-rw-r--r--mysql-test/lib/t/dummyd.pl38
-rw-r--r--mysql-test/lib/t/rmtree.t52
-rwxr-xr-xmysql-test/lib/t/testMyConfig.t131
-rwxr-xr-xmysql-test/lib/t/testMyConfigFactory.t98
-rwxr-xr-xmysql-test/lib/t/test_child.pl21
10 files changed, 685 insertions, 0 deletions
diff --git a/mysql-test/lib/t/Base.t b/mysql-test/lib/t/Base.t
new file mode 100644
index 00000000000..6ca7657d421
--- /dev/null
+++ b/mysql-test/lib/t/Base.t
@@ -0,0 +1,27 @@
+# -*- cperl -*-
+use Test::More qw(no_plan);
+use strict;
+
+use_ok ("My::SafeProcess::Base");
+
+
+my $count= 0;
+for (1..100){
+ my $pid= My::SafeProcess::Base::_safe_fork();
+ exit unless $pid;
+ (waitpid($pid, 0) == $pid) and $count++;
+}
+ok($count == 100, "safe_fork");
+
+# A nice little forkbomb
+SKIP: {
+ skip("forkbomb", 1);
+ eval {
+ while(1){
+ my $pid= My::SafeProcess::Base::_safe_fork();
+ exit unless $pid;
+ }
+ };
+ ok($@, "forkbomb");
+}
+
diff --git a/mysql-test/lib/t/Find.t b/mysql-test/lib/t/Find.t
new file mode 100644
index 00000000000..90489ba06dd
--- /dev/null
+++ b/mysql-test/lib/t/Find.t
@@ -0,0 +1,33 @@
+# -*- cperl -*-
+use Test::More qw(no_plan);
+use strict;
+
+use_ok ("My::Find");
+my $basedir= "../..";
+
+print "=" x 40, "\n";
+my $mysqld_exe= my_find_bin($basedir,
+ ["sql", "bin"],
+ ["mysqld", "mysqld-debug"]);
+print "mysqld_exe: $mysqld_exe\n";
+print "=" x 40, "\n";
+my $mysql_exe= my_find_bin($basedir,
+ ["client", "bin"],
+ "mysql");
+print "mysql_exe: $mysql_exe\n";
+print "=" x 40, "\n";
+
+my $mtr_build_dir= $ENV{MTR_BUILD_DIR};
+$ENV{MTR_BUILD_DIR}= "debug";
+my $mysql_exe= my_find_bin($basedir,
+ ["client", "bin"],
+ "mysql");
+print "mysql_exe: $mysql_exe\n";
+$ENV{MTR_BUILD_DIR}= $mtr_build_dir;
+print "=" x 40, "\n";
+
+my $charset_dir= my_find_dir($basedir,
+ ["share/mysql", "sql/share", "share"],
+ "charsets");
+print "charset_dir: $charset_dir\n";
+print "=" x 40, "\n";
diff --git a/mysql-test/lib/t/SafeProcess.t b/mysql-test/lib/t/SafeProcess.t
new file mode 100644
index 00000000000..d4a62ff8cca
--- /dev/null
+++ b/mysql-test/lib/t/SafeProcess.t
@@ -0,0 +1,102 @@
+# -*- cperl -*-
+
+use strict;
+use FindBin;
+use IO::File;
+
+use Test::More qw(no_plan);
+use_ok ("My::SafeProcess");
+
+
+my $perl_path= $^X;
+
+{
+ # Test exit codes
+ my $count= 32;
+ my $ok_count= 0;
+ for my $code (0..$count-1) {
+
+ my $args= [ "$FindBin::Bin/test_child.pl", "--exit-code=$code" ];
+ my $proc= My::SafeProcess->new
+ (
+ path => $perl_path,
+ args => \$args,
+ output => "/dev/null",
+ error => "/dev/null",
+ );
+ # Wait max 10 seconds for the process to finish
+ $ok_count++ if ($proc->wait_one(10) == 0 and
+ $proc->exit_status() == $code);
+ }
+ ok($count == $ok_count, "check exit_status, $ok_count");
+}
+
+
+{
+ # spawn a number of concurrent processes
+ my $count= 16;
+ my $ok_count= 0;
+ my %procs;
+ for my $code (0..$count-1) {
+
+ my $args= [ "$FindBin::Bin/test_child.pl", "--exit-code=$code" ];
+ $procs{$code}= My::SafeProcess->new
+ (
+ path => $perl_path,
+ args => \$args,
+ output => "/dev/null",
+ error => "/dev/null",
+ );
+ }
+
+ for my $code (0..$count-1) {
+ $ok_count++ if ($procs{$code}->wait_one(10) == 0 and
+ $procs{$code}->exit_status() == $code);
+ }
+ ok($count == $ok_count, "concurrent, $ok_count");
+}
+
+
+#
+# Test stdout, stderr
+#
+{
+ use File::Temp qw / tempdir /;
+ my $dir = tempdir( CLEANUP => 1 );
+
+ my $args= [ "$FindBin::Bin/test_child.pl" ];
+ my $proc= My::SafeProcess->new
+ (
+ path => $perl_path,
+ args => \$args,
+ output => "$dir/output.txt",
+ error => "$dir/error.txt",
+ );
+
+ $proc->wait_one(2); # Wait max 2 seconds for the process to finish
+
+ my $fh= IO::File->new("$dir/output.txt");
+ my @text= <$fh>;
+ ok(grep(/Hello stdout/, @text), "check stdout");
+ $fh= IO::File->new("$dir/error.txt");
+ my @text= <$fh>;
+ ok(grep(/Hello stderr/, @text), "check stderr");
+
+ # To same file
+ $proc= My::SafeProcess->new
+ (
+ path => $perl_path,
+ args => \$args,
+ output => "$dir/output.txt",
+ error => "$dir/output.txt",
+ debug => 1,
+ );
+
+ $proc->wait_one(2); # Wait max 2 seconds for the process to finish
+
+ my $fh= IO::File->new("$dir/output.txt");
+ my @text= <$fh>;
+ ok((grep(/Hello stdout/, @text) and grep(/Hello stderr/, @text)),
+ "check stdout and stderr");
+
+}
diff --git a/mysql-test/lib/t/SafeProcessStress.pl b/mysql-test/lib/t/SafeProcessStress.pl
new file mode 100755
index 00000000000..a31f3031262
--- /dev/null
+++ b/mysql-test/lib/t/SafeProcessStress.pl
@@ -0,0 +1,149 @@
+#!/usr/bin/perl
+# -*- cperl -*-
+
+use strict;
+use FindBin;
+use My::SafeProcess;
+
+#
+# Test longterm running of SafeProcess
+#
+
+my $perl_path= $^X;
+my $verbose= 0;
+my $loops= 1000;
+
+print "kill one and wait for one\n";
+for (1...$loops){
+ use File::Temp qw / tempdir /;
+ my $dir = tempdir( CLEANUP => 1 );
+
+ my @procs;
+ for (1..10){
+
+ my $args= [ "$FindBin::Bin/dummyd.pl", "--vardir=$dir" ];
+ my $proc= My::SafeProcess->new
+ (
+ path => $perl_path,
+ args => \$args,
+ verbose => $verbose,
+ );
+ push(@procs, $proc);
+ }
+
+ foreach my $proc (@procs) {
+ $proc->kill();
+ # dummyd will always be kiled and thus
+ # exit_status should have been set to 1
+ die "oops, exit_status: ", $proc->exit_status()
+ unless $proc->exit_status() == 1;
+ }
+
+ print "=" x 60, "\n";
+}
+
+
+print "With 1 second sleep in dummyd\n";
+for (1...$loops){
+ use File::Temp qw / tempdir /;
+ my $dir = tempdir( CLEANUP => 1 );
+
+ my @procs;
+ for (1..10){
+
+ my $args= [ "$FindBin::Bin/dummyd.pl",
+ "--vardir=$dir",
+ "--sleep=1" ];
+ my $proc= My::SafeProcess->new
+ (
+ path => $perl_path,
+ args => \$args,
+ verbose => $verbose,
+ );
+ push(@procs, $proc);
+ }
+
+ foreach my $proc (@procs) {
+ $proc->kill();
+ }
+
+ print "=" x 60, "\n";
+}
+
+print "kill all and wait for one\n";
+for (1...$loops){
+ use File::Temp qw / tempdir /;
+ my $dir = tempdir( CLEANUP => 1 );
+
+ my @procs;
+ for (1..10){
+
+ my $args= [ "$FindBin::Bin/dummyd.pl", "--vardir=$dir" ];
+ my $proc= My::SafeProcess->new
+ (
+ path => $perl_path,
+ args => \$args,
+ verbose => $verbose,
+ );
+ push(@procs, $proc);
+ }
+
+ foreach my $proc (@procs) {
+ $proc->start_kill();
+ }
+
+ foreach my $proc (@procs) {
+ $proc->wait_one();
+ }
+
+ print "=" x 60, "\n";
+}
+
+print "kill all using shutdown without callback\n";
+for (1...$loops){
+ use File::Temp qw / tempdir /;
+ my $dir = tempdir( CLEANUP => 1 );
+
+ my @procs;
+ for (1..10){
+
+ my $args= [ "$FindBin::Bin/dummyd.pl", "--vardir=$dir" ];
+ my $proc= My::SafeProcess->new
+ (
+ path => $perl_path,
+ args => \$args,
+ verbose => $verbose,
+ );
+ push(@procs, $proc);
+ }
+
+ My::SafeProcess::shutdown(2, @procs);
+
+ print "=" x 60, "\n";
+}
+
+print "kill all using shutdown\n";
+for (1...$loops){
+ use File::Temp qw / tempdir /;
+ my $dir = tempdir( CLEANUP => 1 );
+
+ my @procs;
+ for (1..10){
+
+ my $args= [ "$FindBin::Bin/dummyd.pl", "--vardir=$dir" ];
+ my $proc= My::SafeProcess->new
+ (
+ path => $perl_path,
+ args => \$args,
+ verbose => $verbose,
+ shutdown => sub { }, # Does nothing
+ );
+ push(@procs, $proc);
+ }
+
+ My::SafeProcess::shutdown(2, @procs);
+
+ print "=" x 60, "\n";
+}
+
+exit(0);
diff --git a/mysql-test/lib/t/copytree.t b/mysql-test/lib/t/copytree.t
new file mode 100644
index 00000000000..15e4d1a7b1b
--- /dev/null
+++ b/mysql-test/lib/t/copytree.t
@@ -0,0 +1,34 @@
+#!/usr/bin/perl
+# -*- cperl -*-
+
+use strict;
+
+use My::File::Path;
+
+use Test::Simple tests => 7;
+use File::Temp qw / tempdir /;
+my $dir = tempdir( CLEANUP => 1 );
+my $testdir="$dir/test";
+my $test_todir="$dir/to";
+
+my $subdir= "$testdir/test1/test2/test3";
+
+#
+# 1. Create, copy and remove a directory structure
+#
+mkpath($subdir);
+ok( -d $subdir, "Check '$subdir' is created");
+
+copytree($testdir, $test_todir);
+ok( -d $test_todir, "Check '$test_todir' is created");
+ok( -d "$test_todir/test1", "Check 'test1' is created");
+ok( -d "$test_todir/test1/test2", "Check 'test2' is created");
+ok( -d "$test_todir/test1/test2/test3", "Check 'test3' is created");
+
+
+rmtree($testdir);
+ok( ! -d $testdir, "Check '$testdir' is gone");
+
+rmtree($test_todir);
+ok( ! -d $test_todir, "Check '$test_todir' is gone");
+
diff --git a/mysql-test/lib/t/dummyd.pl b/mysql-test/lib/t/dummyd.pl
new file mode 100644
index 00000000000..07336e3c2d2
--- /dev/null
+++ b/mysql-test/lib/t/dummyd.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/perl
+# -*- cperl -*-
+
+use strict;
+use Getopt::Long;
+use IO::File;
+
+my $vardir;
+my $randie= 0;
+my $sleep= 0;
+GetOptions
+ (
+ # Directory where to write files
+ 'vardir=s' => \$vardir,
+ 'die-randomly' => \$randie,
+ 'sleep=i' => \$sleep,
+ );
+
+die("invalid vardir ") unless defined $vardir and -d $vardir;
+
+my $pid= $$;
+while(1){
+ for my $i (1..64){
+ # Write to file
+ my $name= "$vardir/$pid.$i.tmp";
+ my $F= IO::File->new($name, "w")
+ or warn "$$, Could not open $name: $!" and next;
+ print $F rand($.) for (1..1000);
+ $F->close();
+ sleep($sleep);
+ die "ooops!" if $randie and rand() < 0.0001
+ }
+}
+
+
+exit (0);
+
+
diff --git a/mysql-test/lib/t/rmtree.t b/mysql-test/lib/t/rmtree.t
new file mode 100644
index 00000000000..08c9077d001
--- /dev/null
+++ b/mysql-test/lib/t/rmtree.t
@@ -0,0 +1,52 @@
+#!/usr/bin/perl
+# -*- cperl -*-
+
+use strict;
+
+use My::File::Path;
+
+use Test::Simple tests => 8;
+use File::Temp qw / tempdir /;
+my $dir = tempdir( CLEANUP => 1 );
+my $testdir="$dir/test";
+
+my $subdir= "$testdir/test1/test2/test3";
+
+#
+# 1. Create and remove a directory structure
+#
+mkpath($subdir);
+ok( -d $subdir, "Check '$subdir' is created");
+
+rmtree($testdir);
+ok( ! -d $testdir, "Check '$testdir' is gone");
+
+#
+# 2. Create and remove a directory structure
+# where one directory is chmod to 0000
+#
+mkpath($subdir);
+ok( -d $subdir, "Check '$subdir' is created");
+
+ok( chmod(0000, $subdir) == 1 , "Check one dir was chmoded");
+
+rmtree($testdir);
+ok( ! -d $testdir, "Check '$testdir' is gone");
+
+#
+# 3. Create and remove a directory structure
+# where one file is chmod to 0000
+#
+mkpath($subdir);
+ok( -d $subdir, "Check '$subdir' is created");
+
+my $testfile= "$subdir/test.file";
+open(F, ">", $testfile) or die;
+print F "hello\n";
+close(F);
+
+ok( chmod(0000, $testfile) == 1 , "Check one file was chmoded");
+
+rmtree($testdir);
+ok( ! -d $testdir, "Check '$testdir' is gone");
+
diff --git a/mysql-test/lib/t/testMyConfig.t b/mysql-test/lib/t/testMyConfig.t
new file mode 100755
index 00000000000..da08cb8b4d1
--- /dev/null
+++ b/mysql-test/lib/t/testMyConfig.t
@@ -0,0 +1,131 @@
+#!/usr/bin/perl
+# -*- cperl -*-
+
+use strict;
+use warnings;
+use File::Temp qw / tempdir /;
+my $dir = tempdir( CLEANUP => 1 );
+
+use Test::More qw(no_plan);
+
+BEGIN { use_ok ( "My::Config" ) };
+
+my $test_cnf= "$dir/test.cnf";
+
+# Write test config file
+open(OUT, ">", $test_cnf) or die;
+print $test_cnf, "\n";
+
+print OUT <<EOF
+[mysqld]
+# Comment
+option1=values2
+option2= value4
+option4
+basedir=thebasedir
+[mysqld_1]
+[mysqld_2]
+[mysqld.9]
+[client]
+socket =\tasocketpath
+EOF
+;
+close OUT;
+
+my $config= My::Config->new($test_cnf);
+isa_ok( $config, "My::Config" );
+
+print $config;
+
+ok ( $config->group("mysqld_2"), "group mysqld_2 exists");
+ok ( $config->group("mysqld_1"), "group mysqld_1 exists");
+ok ( $config->group("mysqld.9"), "group mysqld.9 exists");
+ok ( $config->group("mysqld.9")->suffix() eq ".9", "group mysqld.9 has suffix .9");
+
+ok ( $config->group("mysqld"), "group mysqld exists");
+ok ( $config->group("client"), "group client exists");
+ok ( !$config->group("mysqld_3"), "group mysqld_3 does not exist");
+
+ok ( $config->options_in_group("mysqld") == 4, "options in [mysqld] is 4");
+ok ( $config->options_in_group("nonexist") == 0, "options in [nonexist] is 0");
+
+{
+ my @groups= $config->groups();
+ ok(@groups == 5, "5 groups");
+ my $idx= 0;
+ foreach my $name ('mysqld', 'mysqld_1', 'mysqld_2', 'mysqld.9', 'client') {
+ is($groups[$idx++]->name(), $name, "checking groups $idx");
+ }
+}
+
+{
+ my @groups= $config->like("mysqld");
+ ok(@groups == 4, "4 groups like mysqld");
+ my $idx= 0;
+ foreach my $name ('mysqld', 'mysqld_1', 'mysqld_2', 'mysqld.9') {
+ is($groups[$idx++]->name(), $name, "checking like(\"mysqld\") $idx");
+ }
+}
+
+{
+ my @groups= $config->like("not");
+ ok(@groups == 0, "checking like(\"not\")");
+}
+
+is($config->first_like("mysqld_")->name(), "mysqld_1", "first_like");
+
+is( $config->value('mysqld', 'option4'), undef,
+ "mysqld_option4 exists, does not have a value");
+
+ok( $config->exists('mysqld', 'option4'),
+ "mysqld_option4 exists");
+ok( $config->exists('mysqld', 'option2'),
+ "mysqld_option2 exists");
+ok( !$config->exists('mysqld', 'option5'),
+ "mysqld_option5 does not exists");
+
+# Save the config to file
+my $test2_cnf= "$dir/test2.cnf";
+$config->save($test2_cnf);
+
+# read it back and check it's the same
+my $config2= My::Config->new($test2_cnf);
+isa_ok( $config2, "My::Config" );
+is_deeply( \$config, \$config2, "test.cnf is equal to test2.cnf");
+
+
+my $test_include_cnf= "$dir/test_include.cnf";
+# Write test config file that includes test.cnf
+open(OUT, ">", $test_include_cnf) or die;
+
+print OUT <<EOF
+[mysqld]
+!include test.cnf
+# Comment
+option1=values3
+basedir=anotherbasedir
+EOF
+;
+close OUT;
+
+# Read the config file
+my $config3= My::Config->new($test_include_cnf);
+isa_ok( $config3, "My::Config" );
+print $config3;
+is( $config3->value('mysqld', 'basedir'), 'anotherbasedir',
+ "mysqld_basedir has been overriden by value in test_include.cnf");
+
+is( $config3->value('mysqld', 'option1'), 'values3',
+ "mysqld_option1 has been overriden by value in test_include.cnf");
+
+is( $config3->value('mysqld', 'option2'), 'value4',
+ "mysqld_option2 is from included file");
+
+is( $config3->value('client', 'socket'), 'asocketpath',
+ "client.socket is from included file");
+
+is( $config3->value('mysqld', 'option4'), undef,
+ "mysqld_option4 exists, does not have a value");
+
+print "$config3\n";
+
diff --git a/mysql-test/lib/t/testMyConfigFactory.t b/mysql-test/lib/t/testMyConfigFactory.t
new file mode 100755
index 00000000000..16fdd9db539
--- /dev/null
+++ b/mysql-test/lib/t/testMyConfigFactory.t
@@ -0,0 +1,98 @@
+#!/usr/bin/perl
+# -*- cperl -*-
+
+use strict;
+use warnings;
+
+use File::Temp qw / tempdir /;
+my $dir = tempdir( CLEANUP => 1 );
+
+use Test::More qw(no_plan);
+
+BEGIN { use_ok ( "My::ConfigFactory" ) };
+
+my $gen1_cnf= "$dir/gen1.cnf";
+open(OUT, ">", $gen1_cnf) or die;
+
+print OUT <<EOF
+[mysqld.master]
+# Comment
+option1=value1
+basedir=abasedir
+
+[mysqld.1]
+# Comment
+option1=value1
+option2=value2
+
+[ENV]
+MASTER_MY_PORT=\@mysqld.master.port
+
+EOF
+;
+close OUT;
+
+my $basedir= "../..";
+
+my $config= My::ConfigFactory->new_config
+(
+ {
+ basedir => $basedir,
+ template_path => $gen1_cnf,
+ vardir => "/path/to/var",
+ baseport => 10987,
+ #hosts => [ 'host1', 'host2' ],
+ }
+);
+
+print $config;
+
+ok ( $config->group("mysqld.master"), "group mysqld.master exists");
+ok ( $config->group("mysqld.1"), "group mysqld.1 exists");
+ok ( $config->group("client"), "group client exists");
+ok ( !$config->group("mysqld.3"), "group mysqld.3 does not exist");
+
+ok ( $config->first_like("mysqld"), "group like 'mysqld' exists");
+
+is( $config->value('mysqld.1', '#host'), 'localhost',
+ "mysqld.1.#host has been generated");
+
+is( $config->value('client', 'host'), 'localhost',
+ "client.host has been generated");
+
+is( $config->value('client', 'host'),
+ $config->value('mysqld.master', '#host'),
+ "client.host is same as mysqld.master.host");
+
+ok ( $config->value("mysqld.1", 'character-sets-dir') =~ /$basedir.*charsets$/,
+ "'character-sets-dir' generated");
+
+ok ( $config->value("mysqld.1", 'language') =~ /$basedir.*english$/,
+ "'language' generated");
+
+ok ( $config->value("ENV", 'MASTER_MY_PORT') =~ /\d/,
+ "'language' generated");
+
+my $gen2_cnf= "$dir/gen2.cnf";
+open(OUT, ">", $gen2_cnf) or die;
+
+print OUT <<EOF
+[mysqld.master]
+EOF
+;
+close OUT;
+
+my $config2= My::ConfigFactory->new_config
+(
+ {
+ basedir => $basedir,
+ template_path => $gen2_cnf,
+ vardir => "/path/to/var",
+ baseport => 10987,
+ #hosts => [ 'host1', 'host2' ],
+ }
+);
+
+print $config2;
+
+ok ( $config2->first_like("mysqld"), "group like 'mysqld' exists");
diff --git a/mysql-test/lib/t/test_child.pl b/mysql-test/lib/t/test_child.pl
new file mode 100755
index 00000000000..99f4e68003d
--- /dev/null
+++ b/mysql-test/lib/t/test_child.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/perl
+# -*- cperl -*-
+
+use strict;
+use Getopt::Long;
+
+my $opt_exit_code= 0;
+
+GetOptions
+ (
+ # Exit with the specified exit code
+ 'exit-code=i' => \$opt_exit_code
+ );
+
+
+print "Hello stdout\n";
+print STDERR "Hello stderr\n";
+
+exit ($opt_exit_code);
+
+