mirror of
https://github.com/percona/percona-toolkit.git
synced 2025-09-14 23:40:58 +00:00
Finish writing and testing service scheduling code.
This commit is contained in:
@@ -11,7 +11,7 @@ use warnings FATAL => 'all';
|
||||
use English qw(-no_match_vars);
|
||||
use Test::More;
|
||||
use JSON;
|
||||
use File::Temp qw(tempdir);
|
||||
use File::Temp qw(tempfile);
|
||||
|
||||
use Percona::Test;
|
||||
require "$trunk/bin/pt-agent";
|
||||
@@ -24,14 +24,12 @@ sub test_make_new_crontab {
|
||||
my (%args) = @_;
|
||||
have_required_args(\%args, qw(
|
||||
file
|
||||
name
|
||||
services
|
||||
)) or die;
|
||||
my $file = $args{file};
|
||||
my $name = $args{name};
|
||||
my $services = $args{services};
|
||||
|
||||
my $crontab_list = slurp_file("$trunk/$sample/$file.in");
|
||||
my $crontab_list = slurp_file("$trunk/$sample/$file.in");
|
||||
|
||||
my $new_crontab = pt_agent::make_new_crontab(
|
||||
services => $services,
|
||||
@@ -44,14 +42,10 @@ sub test_make_new_crontab {
|
||||
"$sample/$file.out",
|
||||
cmd_output => 1,
|
||||
),
|
||||
"$name"
|
||||
$args{name} || $file,
|
||||
);
|
||||
}
|
||||
|
||||
# #############################################################################
|
||||
# Empty crontab, new service.
|
||||
# #############################################################################
|
||||
|
||||
my $run0 = Percona::WebAPI::Resource::Run->new(
|
||||
number => '0',
|
||||
program => 'pt-query-digest',
|
||||
@@ -66,12 +60,75 @@ my $svc0 = Percona::WebAPI::Resource::Service->new(
|
||||
runs => [ $run0 ],
|
||||
);
|
||||
|
||||
# Empty crontab, add the service.
|
||||
test_make_new_crontab(
|
||||
name => "crontab001",
|
||||
file => "crontab001",
|
||||
services => [ $svc0 ],
|
||||
);
|
||||
|
||||
# Crontab has another line, add the service to it.
|
||||
test_make_new_crontab(
|
||||
file => "crontab002",
|
||||
services => [ $svc0 ],
|
||||
);
|
||||
|
||||
# Crontab has another line and an old service, remove the old service
|
||||
# and add the current service.
|
||||
test_make_new_crontab(
|
||||
file => "crontab003",
|
||||
services => [ $svc0 ],
|
||||
);
|
||||
|
||||
# #############################################################################
|
||||
# Use real crontab.
|
||||
# #############################################################################
|
||||
|
||||
# The previous tests pass in a crontab file to make testing easier.
|
||||
# Now test that make_new_crontab() will run `crontab -l' if not given
|
||||
# input. To test this, we add a fake line to our crontab. If
|
||||
# make_new_crontab() really runs `crontab -l', then this fake line
|
||||
# will be in the new crontab it returns.
|
||||
|
||||
my $crontab = `crontab -l 2>/dev/null`;
|
||||
SKIP: {
|
||||
skip 'Crontab is not empty', 3 if $crontab;
|
||||
|
||||
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";
|
||||
|
||||
my $new_crontab = pt_agent::make_new_crontab(
|
||||
services => [ $svc0 ],
|
||||
);
|
||||
|
||||
is(
|
||||
$new_crontab,
|
||||
"* 0 * * * date > /dev/null
|
||||
* 8 * * 1,2,3,4,5 pt-agent --run-service query-monitor
|
||||
",
|
||||
"Runs crontab -l by default"
|
||||
);
|
||||
|
||||
system("crontab -r 2>/dev/null");
|
||||
$crontab = `crontab -l 2>/dev/null`;
|
||||
is(
|
||||
$crontab,
|
||||
"",
|
||||
"Removed crontab"
|
||||
);
|
||||
};
|
||||
|
||||
# #############################################################################
|
||||
# Done.
|
||||
# #############################################################################
|
||||
|
Reference in New Issue
Block a user