mirror of
https://github.com/percona/percona-toolkit.git
synced 2025-09-27 16:12:04 +00:00
Rewrite t/pt-archiver/standard_options.t to make it reliable.
This commit is contained in:
@@ -126,12 +126,13 @@ sub create_dbs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sub get_dbh_for {
|
sub get_dbh_for {
|
||||||
my ( $self, $server, $cxn_ops ) = @_;
|
my ( $self, $server, $cxn_ops, $user ) = @_;
|
||||||
_check_server($server);
|
_check_server($server);
|
||||||
$cxn_ops ||= { AutoCommit => 1 };
|
$cxn_ops ||= { AutoCommit => 1 };
|
||||||
|
$user ||= 'msandbox';
|
||||||
PTDEBUG && _d('dbh for', $server, 'on port', $port_for{$server});
|
PTDEBUG && _d('dbh for', $server, 'on port', $port_for{$server});
|
||||||
my $dp = $self->{DSNParser};
|
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;
|
my $dbh;
|
||||||
# This is primarily for the benefit of CompareResults, but it's
|
# This is primarily for the benefit of CompareResults, but it's
|
||||||
# also quite convenient when using an affected OS
|
# also quite convenient when using an affected OS
|
||||||
@@ -542,6 +543,23 @@ sub config_file_for {
|
|||||||
return "/tmp/$port/my.sandbox.cnf"
|
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 {
|
sub _d {
|
||||||
my ($package, undef, $line) = caller 0;
|
my ($package, undef, $line) = caller 0;
|
||||||
@_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; }
|
@_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; }
|
||||||
|
@@ -23,101 +23,127 @@ my $dbh2 = $sb->get_dbh_for('slave1');
|
|||||||
if ( !$dbh ) {
|
if ( !$dbh ) {
|
||||||
plan skip_all => 'Cannot connect to sandbox master';
|
plan skip_all => 'Cannot connect to sandbox master';
|
||||||
}
|
}
|
||||||
else {
|
elsif ( !$dbh2 ) {
|
||||||
plan tests => 7;
|
plan skip_all => 'Cannot connect to sandbox slave';
|
||||||
}
|
}
|
||||||
|
|
||||||
my $output;
|
my $output;
|
||||||
my $cnf = "/tmp/12345/my.sandbox.cnf";
|
my $cnf = "/tmp/12345/my.sandbox.cnf";
|
||||||
my $cmd = "$trunk/bin/pt-archiver";
|
my $pid_file = "/tmp/pt-archiver-test.pid.$PID";
|
||||||
|
my $sentinel = "/tmp/pt-archiver-test.sentinel.$PID";
|
||||||
|
|
||||||
$sb->create_dbs($dbh, [qw(test)]);
|
$sb->create_dbs($dbh, [qw(test)]);
|
||||||
|
|
||||||
SKIP: {
|
ok(
|
||||||
skip 'Sandbox master does not have the sakila database', 1
|
no_diff(
|
||||||
unless @{$dbh->selectcol_arrayref("SHOW DATABASES LIKE 'sakila'")};
|
sub {
|
||||||
|
pt_archiver::main('--source', "F=$cnf,h=127.1,D=sakila,t=film",
|
||||||
$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 -`;
|
qw(--no-check-charset --purge --dry-run --port 12345),
|
||||||
is(
|
"--where", "film_id < 100")
|
||||||
$output,
|
},
|
||||||
'',
|
"t/pt-archiver/samples/issue-248.txt",
|
||||||
|
),
|
||||||
'DSNs inherit from standard connection options (issue 248)'
|
'DSNs inherit from standard connection options (issue 248)'
|
||||||
);
|
);
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
# Test with a sentinel file
|
# Test with a sentinel file
|
||||||
$sb->load_file('master', 't/pt-archiver/samples/table1.sql');
|
$sb->load_file('master', 't/pt-archiver/samples/table1.sql');
|
||||||
diag(`touch sentinel`);
|
diag(`touch $sentinel`);
|
||||||
|
|
||||||
$output = output(
|
$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"`;
|
$output = `/tmp/12345/use -N -e "select count(*) from test.table_1"`;
|
||||||
is($output + 0, 4, 'No rows were deleted');
|
is(
|
||||||
`rm sentinel`;
|
$output + 0,
|
||||||
|
4,
|
||||||
|
'No rows were deleted'
|
||||||
|
) or diag($output);
|
||||||
|
|
||||||
|
diag(`rm -f $sentinel`);
|
||||||
|
|
||||||
# Test --stop, which sets the sentinel
|
# Test --stop, which sets the sentinel
|
||||||
$output = output(
|
$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
|
# Issue 391: Add --pid option to mk-table-sync
|
||||||
# #############################################################################
|
# #############################################################################
|
||||||
`touch /tmp/mk-archiver.pid`;
|
diag(`touch $pid_file`);
|
||||||
$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`;
|
|
||||||
|
$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(
|
like(
|
||||||
$output,
|
$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)'
|
'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
|
# 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.
|
# 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');
|
$sb->load_file('master', 't/pt-archiver/samples/tables1-4.sql');
|
||||||
|
|
||||||
# Change passwords so defaults files won't work.
|
# Change passwords so defaults files won't work.
|
||||||
$dbh->do("SET PASSWORD FOR msandbox = PASSWORD('foo')");
|
$sb->do_as_root(
|
||||||
$dbh2->do("SET PASSWORD FOR msandbox = PASSWORD('foo')");
|
'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');
|
||||||
my $r = $dbh2->selectall_arrayref('SELECT * FROM test.table_2');
|
is(
|
||||||
is(
|
|
||||||
scalar @$r,
|
scalar @$r,
|
||||||
4,
|
4,
|
||||||
'--dest inherited from --source'
|
'--dest inherited from --source'
|
||||||
);
|
);
|
||||||
|
|
||||||
# Set the passwords back. If this fails we should bail out because
|
$sb->do_as_root('master', q/DROP USER 'bob'@'%'/);
|
||||||
# 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);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
# #############################################################################
|
# #############################################################################
|
||||||
# Done.
|
# Done.
|
||||||
# #############################################################################
|
# #############################################################################
|
||||||
$sb->wipe_clean($dbh);
|
$sb->wipe_clean($dbh);
|
||||||
$sb->wipe_clean($dbh2) if $dbh2;
|
|
||||||
ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
|
ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
|
||||||
exit;
|
done_testing;
|
||||||
|
Reference in New Issue
Block a user