diff --git a/lib/Sandbox.pm b/lib/Sandbox.pm index e1b8fc2d..e42ad3ce 100644 --- a/lib/Sandbox.pm +++ b/lib/Sandbox.pm @@ -126,12 +126,13 @@ sub create_dbs { } sub get_dbh_for { - my ( $self, $server, $cxn_ops ) = @_; + my ( $self, $server, $cxn_ops, $user ) = @_; _check_server($server); $cxn_ops ||= { AutoCommit => 1 }; + $user ||= 'msandbox'; PTDEBUG && _d('dbh for', $server, 'on port', $port_for{$server}); my $dp = $self->{DSNParser}; - my $dsn = $dp->parse('h=127.0.0.1,u=msandbox,p=msandbox,P=' . $port_for{$server}); + my $dsn = $dp->parse("h=127.0.0.1,u=$user,p=msandbox,P=" . $port_for{$server}); my $dbh; # This is primarily for the benefit of CompareResults, but it's # also quite convenient when using an affected OS @@ -542,6 +543,23 @@ sub config_file_for { return "/tmp/$port/my.sandbox.cnf" } +sub do_as_root { + my ($self, $server, @queries) = @_; + my $dbh = $self->get_dbh_for($server, undef, 'root'); + my $ok = 1; + eval { + foreach my $query ( @queries ) { + $dbh->do($query); + } + }; + if ( $EVAL_ERROR ) { + $ok = 0; + warn $EVAL_ERROR; + } + $dbh->disconnect; + return $ok; +} + sub _d { my ($package, undef, $line) = caller 0; @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } diff --git a/t/pt-archiver/standard_options.t b/t/pt-archiver/standard_options.t index d294865f..8f66b588 100644 --- a/t/pt-archiver/standard_options.t +++ b/t/pt-archiver/standard_options.t @@ -23,101 +23,127 @@ my $dbh2 = $sb->get_dbh_for('slave1'); if ( !$dbh ) { plan skip_all => 'Cannot connect to sandbox master'; } -else { - plan tests => 7; +elsif ( !$dbh2 ) { + plan skip_all => 'Cannot connect to sandbox slave'; } my $output; -my $cnf = "/tmp/12345/my.sandbox.cnf"; -my $cmd = "$trunk/bin/pt-archiver"; +my $cnf = "/tmp/12345/my.sandbox.cnf"; +my $pid_file = "/tmp/pt-archiver-test.pid.$PID"; +my $sentinel = "/tmp/pt-archiver-test.sentinel.$PID"; $sb->create_dbs($dbh, [qw(test)]); -SKIP: { - skip 'Sandbox master does not have the sakila database', 1 - unless @{$dbh->selectcol_arrayref("SHOW DATABASES LIKE 'sakila'")}; - - $output = `$cmd --source F=$cnf,h=127.1,D=sakila,t=film --no-check-charset --where "film_id < 100" --purge --dry-run --port 12345 | diff $trunk/t/pt-archiver/samples/issue-248.txt -`; - is( - $output, - '', - 'DSNs inherit from standard connection options (issue 248)' - ); -}; - +ok( + no_diff( + sub { + pt_archiver::main('--source', "F=$cnf,h=127.1,D=sakila,t=film", + qw(--no-check-charset --purge --dry-run --port 12345), + "--where", "film_id < 100") + }, + "t/pt-archiver/samples/issue-248.txt", + ), + 'DSNs inherit from standard connection options (issue 248)' +); # Test with a sentinel file $sb->load_file('master', 't/pt-archiver/samples/table1.sql'); -diag(`touch sentinel`); +diag(`touch $sentinel`); + $output = output( - sub { pt_archiver::main(qw(--where 1=1 --why-quit --sentinel sentinel), "--source", "D=test,t=table_1,F=$cnf", qw(--purge)) }, + sub { pt_archiver::main("--source", "D=test,t=table_1,F=$cnf", + qw(--where 1=1 --why-quit --purge), + "--sentinel", $sentinel) + }, + stderr => 1, ); -like($output, qr/because sentinel/, 'Exits because of sentinel'); + +like( + $output, + qr/because sentinel file $sentinel exists/, + 'Exits because of sentinel' +); + $output = `/tmp/12345/use -N -e "select count(*) from test.table_1"`; -is($output + 0, 4, 'No rows were deleted'); -`rm sentinel`; +is( + $output + 0, + 4, + 'No rows were deleted' +) or diag($output); + +diag(`rm -f $sentinel`); # Test --stop, which sets the sentinel $output = output( - sub { pt_archiver::main(qw(--sentinel sentinel --stop)) }, + sub { pt_archiver::main("--sentinel", $sentinel, "--stop") }, ); -like($output, qr/Successfully created file sentinel/, 'Created the sentinel OK'); -diag(`rm -f sentinel >/dev/null`); + +like( + $output, + qr/Successfully created file $sentinel/, + 'Created the sentinel OK' +); + +diag(`rm -f $sentinel`); # ############################################################################# # Issue 391: Add --pid option to mk-table-sync # ############################################################################# -`touch /tmp/mk-archiver.pid`; -$output = `$cmd --where 1=1 --source F=$cnf,D=test,t=issue_131_src --statistics --dest t=issue_131_dst --pid /tmp/mk-archiver.pid 2>&1`; +diag(`touch $pid_file`); + +$output = output( + sub { pt_archiver::main('--source', "F=$cnf,D=test,t=issue_131_src", + qw(--where 1=1 --statistics --dest t=issue_131_dst), + "--pid", $pid_file) + }, + stderr => 1, +); + like( $output, - qr{PID file /tmp/mk-archiver.pid already exists}, + qr{PID file $pid_file already exists}, 'Dies if PID file already exists (issue 391)' ); -`rm -rf /tmp/mk-archiver.pid`; +diag(`rm -f $pid_file`); # ############################################################################# # Issue 460: mk-archiver does not inherit DSN as documented # ############################################################################# -SKIP: { - skip 'Cannot connect to sandbox slave1', 1 unless $dbh2; - # This test will achive rows from dbh:test.table_1 to dbh2:test.table_2. - $sb->load_file('master', 't/pt-archiver/samples/tables1-4.sql'); +# This test will achive rows from dbh:test.table_1 to dbh2:test.table_2. +$sb->load_file('master', 't/pt-archiver/samples/tables1-4.sql'); - # Change passwords so defaults files won't work. - $dbh->do("SET PASSWORD FOR msandbox = PASSWORD('foo')"); - $dbh2->do("SET PASSWORD FOR msandbox = PASSWORD('foo')"); +# Change passwords so defaults files won't work. +$sb->do_as_root( + 'master', + q/CREATE USER 'bob'@'%' IDENTIFIED BY 'foo'/, + q/GRANT ALL ON *.* TO 'bob'@'%'/, +); +$dbh2->do('TRUNCATE TABLE test.table_2'); +$sb->wait_for_slaves; - $dbh2->do('TRUNCATE TABLE test.table_2'); +$output = output( + sub { pt_archiver::main( + '--source', 'h=127.1,P=12345,D=test,t=table_1,u=bob,p=foo', + '--dest', 'P=12346,t=table_2', + qw(--where 1=1)) + }, + stderr => 1, +); - $output = `$trunk/bin/pt-archiver --where 1=1 --source h=127.1,P=12345,D=test,t=table_1,u=msandbox,p=foo --dest P=12346,t=table_2 2>&1`; - my $r = $dbh2->selectall_arrayref('SELECT * FROM test.table_2'); - is( - scalar @$r, - 4, - '--dest inherited from --source' - ); +my $r = $dbh2->selectall_arrayref('SELECT * FROM test.table_2'); +is( + scalar @$r, + 4, + '--dest inherited from --source' +); - # Set the passwords back. If this fails we should bail out because - # nothing else is going to work. - eval { - $dbh->do("SET PASSWORD FOR msandbox = PASSWORD('msandbox')"); - $dbh2->do("SET PASSWORD FOR msandbox = PASSWORD('msandbox')"); - }; - if ( $EVAL_ERROR ) { - BAIL_OUT('Failed to reset the msandbox password on the master or slave ' - . 'sandbox. Check the Maatkit test environment with "test-env ' - . 'status" and restart with "test-env restart". The error was: ' - . $EVAL_ERROR); - } -}; +$sb->do_as_root('master', q/DROP USER 'bob'@'%'/); # ############################################################################# # Done. # ############################################################################# $sb->wipe_clean($dbh); -$sb->wipe_clean($dbh2) if $dbh2; ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox"); -exit; +done_testing;