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 tempdir);
17
use Percona::Test::Mock::AgentLogger;
18
require "$trunk/bin/pt-agent";
20
my $crontab = `crontab -l 2>/dev/null`;
22
plan skip_all => 'Crontab is not empty';
25
Percona::Toolkit->import(qw(have_required_args Dumper));
27
my $sample = "t/pt-agent/samples";
28
my $tmpdir = tempdir("/tmp/pt-agent.$PID.XXXXXX", CLEANUP => 1);
31
my $logger = Percona::Test::Mock::AgentLogger->new(log => \@log);
32
pt_agent::_logger($logger);
34
# #############################################################################
35
# Schedule a good crontab.
36
# #############################################################################
38
my $run0 = Percona::WebAPI::Resource::Task->new(
39
name => 'query-history',
41
program => 'pt-query-digest',
42
options => '--output json',
46
my $svc0 = Percona::WebAPI::Resource::Service->new(
48
name => 'query-history',
49
run_schedule => '* 8 * * 1,2,3,4,5',
50
spool_schedule => '* 9 * * 1,2,3,4,5',
54
# First add a fake line so we can know that the real, existing
55
# crontab is used and not clobbered.
56
my ($fh, $file) = tempfile();
57
print {$fh} "* 0 * * * date > /dev/null\n";
58
close $fh or warn "Cannot close $file: $OS_ERROR";
59
my $output = `crontab $file 2>&1`;
61
$crontab = `crontab -l 2>&1`;
65
"* 0 * * * date > /dev/null\n",
66
"Set other crontab line"
69
unlink $file or warn "Cannot remove $file: $OS_ERROR";
74
pt_agent::schedule_services(
75
services => [ $svc0 ],
89
$crontab = `crontab -l 2>/dev/null`;
91
# pt-agent uses $FindBin::Bin/pt-agent for the path to pt-agent,
92
# which in testing will be $trunk/t/pt-agent/ because that's where
93
# this file is located. However, if $FindBin::Bin resovles sym
94
# links where as $trunk does not, so to make things simple we just
95
# cut out the full path.
97
$crontab =~ s! /.+?/pt-agent --! pt-agent --!g;
101
"* 0 * * * date > /dev/null
102
* 8 * * 1,2,3,4,5 pt-agent --run-service query-history
103
* 9 * * 1,2,3,4,5 pt-agent --send-data query-history
105
"schedule_services()"
109
-f "$tmpdir/crontab",
110
"Wrote crontab to --lib/crontab"
111
) or diag(`ls -l $tmpdir`);
114
-f "$tmpdir/crontab.err",
115
"Write --lib/crontab.err",
116
) or diag(`ls -l $tmpdir`);
118
my $err = -f "$tmpdir/crontab.err" ? `cat $tmpdir/crontab.err` : '';
125
system("crontab -r 2>/dev/null");
126
$crontab = `crontab -l 2>/dev/null`;
133
# #############################################################################
134
# Handle bad crontab lines.
135
# #############################################################################
137
$svc0 = Percona::WebAPI::Resource::Service->new(
139
name => 'query-history',
140
run_schedule => '* * * * Foo', # "foo":0: bad day-of-week
141
spool_schedule => '* 8 * * Mon',
148
pt_agent::schedule_services(
149
services => [ $svc0 ],
160
qr/Error setting new crontab/,
164
$crontab = `crontab -l 2>/dev/null`;
168
"Bad schedule_services()"
172
-f "$tmpdir/crontab",
173
"Wrote crontab to --lib/crontab"
174
) or diag(`ls -l $tmpdir`);
177
-f "$tmpdir/crontab.err",
178
"Write --lib/crontab.err",
179
) or diag(`ls -l $tmpdir`);
181
$err = -f "$tmpdir/crontab.err" ? `cat $tmpdir/crontab.err` : '';
188
system("crontab -r 2>/dev/null");
189
$crontab = `crontab -l 2>/dev/null`;
197
# #############################################################################
199
# #############################################################################