mirror of
https://github.com/percona/percona-toolkit.git
synced 2025-09-20 19:04:59 +00:00
Finish writing and testing service scheduling code.
This commit is contained in:
182
t/pt-agent/schedule_services.t
Normal file
182
t/pt-agent/schedule_services.t
Normal file
@@ -0,0 +1,182 @@
|
||||
#!/usr/bin/env perl
|
||||
|
||||
BEGIN {
|
||||
die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
|
||||
unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
|
||||
unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
|
||||
};
|
||||
|
||||
use strict;
|
||||
use warnings FATAL => 'all';
|
||||
use English qw(-no_match_vars);
|
||||
use Test::More;
|
||||
use JSON;
|
||||
use File::Temp qw(tempfile tempdir);
|
||||
|
||||
use Percona::Test;
|
||||
require "$trunk/bin/pt-agent";
|
||||
|
||||
my $crontab = `crontab -l 2>/dev/null`;
|
||||
if ( $crontab ) {
|
||||
plan skip_all => 'Crontab is not empty';
|
||||
}
|
||||
|
||||
Percona::Toolkit->import(qw(have_required_args Dumper));
|
||||
|
||||
my $sample = "t/pt-agent/samples";
|
||||
my $tmpdir = tempdir("/tmp/pt-agent.$PID.XXXXXX", CLEANUP => 1);
|
||||
|
||||
# #############################################################################
|
||||
# Schedule a good crontab.
|
||||
# #############################################################################
|
||||
|
||||
my $run0 = Percona::WebAPI::Resource::Run->new(
|
||||
number => '0',
|
||||
program => 'pt-query-digest',
|
||||
options => '--output json',
|
||||
output => 'spool',
|
||||
);
|
||||
|
||||
my $svc0 = Percona::WebAPI::Resource::Service->new(
|
||||
name => 'query-monitor',
|
||||
alias => 'Query Monitor',
|
||||
schedule => '* 8 * * 1,2,3,4,5',
|
||||
runs => [ $run0 ],
|
||||
);
|
||||
|
||||
# First add a fake line so we can know that the real, existing
|
||||
# crontab is used and not clobbered.
|
||||
my ($fh, $file) = tempfile();
|
||||
print {$fh} "* 0 * * * date > /dev/null";
|
||||
close $fh or warn "Cannot close $file: $OS_ERROR";
|
||||
my $output = `crontab $file 2>&1`;
|
||||
|
||||
$crontab = `crontab -l 2>&1`;
|
||||
|
||||
is(
|
||||
$crontab,
|
||||
"* 0 * * * date > /dev/null",
|
||||
"Set other crontab line"
|
||||
) or diag($output);
|
||||
|
||||
unlink $file or warn "Cannot remove $file: $OS_ERROR";
|
||||
|
||||
eval {
|
||||
$output = output(
|
||||
sub {
|
||||
pt_agent::schedule_services(
|
||||
services => [ $svc0 ],
|
||||
lib_dir => $tmpdir,
|
||||
)
|
||||
},
|
||||
stderr => 1,
|
||||
);
|
||||
};
|
||||
|
||||
is(
|
||||
$EVAL_ERROR,
|
||||
"",
|
||||
"No error"
|
||||
) or diag($output);
|
||||
|
||||
$crontab = `crontab -l 2>/dev/null`;
|
||||
is(
|
||||
$crontab,
|
||||
"* 0 * * * date > /dev/null
|
||||
* 8 * * 1,2,3,4,5 pt-agent --run-service query-monitor
|
||||
",
|
||||
"schedule_services()"
|
||||
);
|
||||
|
||||
ok(
|
||||
-f "$tmpdir/crontab",
|
||||
"Wrote crontab to --lib/crontab"
|
||||
) or diag(`ls -l $tmpdir`);
|
||||
|
||||
ok(
|
||||
-f "$tmpdir/crontab.err",
|
||||
"Write --lib/crontab.err",
|
||||
) or diag(`ls -l $tmpdir`);
|
||||
|
||||
my $err = -f "$tmpdir/crontab.err" ? `cat $tmpdir/crontab.err` : '';
|
||||
is(
|
||||
$err,
|
||||
"",
|
||||
"No crontab error"
|
||||
);
|
||||
|
||||
system("crontab -r 2>/dev/null");
|
||||
$crontab = `crontab -l 2>/dev/null`;
|
||||
is(
|
||||
$crontab,
|
||||
"",
|
||||
"Removed crontab"
|
||||
);
|
||||
|
||||
# #############################################################################
|
||||
# Handle bad crontab lines.
|
||||
# #############################################################################
|
||||
|
||||
$svc0 = Percona::WebAPI::Resource::Service->new(
|
||||
name => 'query-monitor',
|
||||
alias => 'Query Monitor',
|
||||
schedule => '* * * * Foo', # "foo":0: bad day-of-week
|
||||
runs => [ $run0 ],
|
||||
);
|
||||
|
||||
eval {
|
||||
$output = output(
|
||||
sub {
|
||||
pt_agent::schedule_services(
|
||||
services => [ $svc0 ],
|
||||
lib_dir => $tmpdir,
|
||||
),
|
||||
},
|
||||
stderr => 1,
|
||||
die => 1,
|
||||
);
|
||||
};
|
||||
|
||||
like(
|
||||
$EVAL_ERROR,
|
||||
qr/Error setting new crontab/,
|
||||
"Throws errors"
|
||||
) or diag($output);
|
||||
|
||||
$crontab = `crontab -l 2>/dev/null`;
|
||||
is(
|
||||
$crontab,
|
||||
"",
|
||||
"Bad schedule_services()"
|
||||
);
|
||||
|
||||
ok(
|
||||
-f "$tmpdir/crontab",
|
||||
"Wrote crontab to --lib/crontab"
|
||||
) or diag(`ls -l $tmpdir`);
|
||||
|
||||
ok(
|
||||
-f "$tmpdir/crontab.err",
|
||||
"Write --lib/crontab.err",
|
||||
) or diag(`ls -l $tmpdir`);
|
||||
|
||||
$err = -f "$tmpdir/crontab.err" ? `cat $tmpdir/crontab.err` : '';
|
||||
like(
|
||||
$err,
|
||||
qr/bad/,
|
||||
"Crontab error"
|
||||
);
|
||||
|
||||
system("crontab -r 2>/dev/null");
|
||||
$crontab = `crontab -l 2>/dev/null`;
|
||||
is(
|
||||
$crontab,
|
||||
"",
|
||||
"Removed crontab"
|
||||
);
|
||||
|
||||
|
||||
# #############################################################################
|
||||
# Done.
|
||||
# #############################################################################
|
||||
done_testing;
|
Reference in New Issue
Block a user