4
die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
5
unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
6
unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
10
use warnings FATAL => 'all';
11
use English qw(-no_match_vars);
14
use File::Temp qw(tempfile);
17
require "$trunk/bin/pt-agent";
19
Percona::Toolkit->import(qw(have_required_args Dumper));
21
my $sample = "t/pt-agent/samples";
23
sub test_make_new_crontab {
25
have_required_args(\%args, qw(
29
my $file = $args{file};
30
my $services = $args{services};
32
my $crontab_list = slurp_file("$trunk/$sample/$file.in");
34
my $new_crontab = pt_agent::make_new_crontab(
35
services => $services,
36
crontab_list => $crontab_list,
47
) or diag($new_crontab);
50
my $run0 = Percona::WebAPI::Resource::Task->new(
51
name => 'query-history',
53
program => 'pt-query-digest',
54
options => '--output json',
58
my $svc0 = Percona::WebAPI::Resource::Service->new(
60
name => 'query-history',
61
run_schedule => '* 8 * * 1,2,3,4,5',
62
spool_schedule => '* 9 * * 1,2,3,4,5',
66
# Empty crontab, add the service.
67
test_make_new_crontab(
69
services => [ $svc0 ],
72
# Crontab has another line, add the service to it.
73
test_make_new_crontab(
75
services => [ $svc0 ],
78
# Crontab has another line and an old service, remove the old service
79
# and add the current service.
80
test_make_new_crontab(
82
services => [ $svc0 ],
85
# Crontab has old service, remove it and add only new service.
86
test_make_new_crontab(
88
services => [ $svc0 ],
91
# #############################################################################
93
# #############################################################################
95
# The previous tests pass in a crontab file to make testing easier.
96
# Now test that make_new_crontab() will run `crontab -l' if not given
97
# input. To test this, we add a fake line to our crontab. If
98
# make_new_crontab() really runs `crontab -l', then this fake line
99
# will be in the new crontab it returns.
101
my $crontab = `crontab -l 2>/dev/null`;
103
skip 'Crontab is not empty', 3 if $crontab;
105
# On most systems[1], crontab lines must end with a newline,
106
# else an error like this happens:
107
# "/tmp/new_crontab_file":1: premature EOF
108
# errors in crontab file, can't install.
109
# [1] Ubuntu 10 and Mac OS X work without the newline.
110
my ($fh, $file) = tempfile();
111
print {$fh} "* 0 * * * date > /dev/null\n";
112
close $fh or warn "Cannot close $file: $OS_ERROR";
113
my $output = `crontab $file 2>&1`;
115
$crontab = `crontab -l 2>&1`;
119
"* 0 * * * date > /dev/null\n",
120
"Set other crontab line"
123
unlink $file or warn "Cannot remove $file: $OS_ERROR";
125
my $new_crontab = pt_agent::make_new_crontab(
126
services => [ $svc0 ],
132
"* 0 * * * date > /dev/null
133
* 8 * * 1,2,3,4,5 pt-agent --run-service query-history
134
* 9 * * 1,2,3,4,5 pt-agent --send-data query-history
136
"Runs crontab -l by default"
139
system("crontab -r 2>/dev/null");
140
$crontab = `crontab -l 2>/dev/null`;
148
# #############################################################################
150
# #############################################################################