~percona-core/percona-toolkit/release-2.2.8-v2

« back to all changes in this revision

Viewing changes to t/pt-agent/schedule_services.t

  • Committer: Daniel Nichter
  • Date: 2013-06-19 21:23:55 UTC
  • mfrom: (582.1.5 release-2.2.3)
  • Revision ID: daniel@percona.com-20130619212355-nf6bmx23j3b76afe
Tags: 2.2.3
Merge release-2.2.3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env perl
 
2
 
 
3
BEGIN {
 
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";
 
7
};
 
8
 
 
9
use strict;
 
10
use warnings FATAL => 'all';
 
11
use English qw(-no_match_vars);
 
12
use Test::More;
 
13
use JSON;
 
14
use File::Temp qw(tempfile tempdir);
 
15
 
 
16
use Percona::Test;
 
17
use Percona::Test::Mock::AgentLogger;
 
18
require "$trunk/bin/pt-agent";
 
19
 
 
20
my $crontab = `crontab -l 2>/dev/null`;
 
21
if ( $crontab ) {
 
22
   plan skip_all => 'Crontab is not empty';
 
23
}
 
24
 
 
25
Percona::Toolkit->import(qw(have_required_args Dumper));
 
26
 
 
27
my $sample = "t/pt-agent/samples";
 
28
my $tmpdir = tempdir("/tmp/pt-agent.$PID.XXXXXX", CLEANUP => 1);
 
29
 
 
30
my @log;
 
31
my $logger = Percona::Test::Mock::AgentLogger->new(log => \@log);
 
32
pt_agent::_logger($logger);
 
33
 
 
34
# #############################################################################
 
35
# Schedule a good crontab.
 
36
# #############################################################################
 
37
 
 
38
my $run0 = Percona::WebAPI::Resource::Task->new(
 
39
   name    => 'query-history',
 
40
   number  => '0',
 
41
   program => 'pt-query-digest',
 
42
   options => '--output json',
 
43
   output  => 'spool',
 
44
);
 
45
 
 
46
my $svc0 = Percona::WebAPI::Resource::Service->new(
 
47
   ts             => 100,
 
48
   name           => 'query-history',
 
49
   run_schedule   => '* 8 * * 1,2,3,4,5',
 
50
   spool_schedule => '* 9 * * 1,2,3,4,5',
 
51
   tasks          => [ $run0 ],
 
52
);
 
53
 
 
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`;
 
60
 
 
61
$crontab = `crontab -l 2>&1`;
 
62
 
 
63
is(
 
64
   $crontab,
 
65
   "* 0  *  *  *  date > /dev/null\n",
 
66
   "Set other crontab line"
 
67
) or diag($output);
 
68
 
 
69
unlink $file or warn "Cannot remove $file: $OS_ERROR";
 
70
 
 
71
eval {
 
72
   $output = output(
 
73
      sub {
 
74
         pt_agent::schedule_services(
 
75
            services => [ $svc0 ],
 
76
            lib_dir  => $tmpdir,
 
77
         )
 
78
      },
 
79
      stderr => 1,
 
80
   );
 
81
};
 
82
 
 
83
is(
 
84
   $EVAL_ERROR,
 
85
   "",
 
86
   "No error"
 
87
) or diag($output);
 
88
 
 
89
$crontab = `crontab -l 2>/dev/null`;
 
90
 
 
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. 
 
96
if ( $crontab ) {
 
97
   $crontab =~ s! /.+?/pt-agent --! pt-agent --!g;
 
98
}
 
99
is(
 
100
   $crontab,
 
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
 
104
",
 
105
   "schedule_services()"
 
106
);
 
107
 
 
108
ok(
 
109
   -f "$tmpdir/crontab",
 
110
   "Wrote crontab to --lib/crontab"
 
111
) or diag(`ls -l $tmpdir`);
 
112
 
 
113
ok(
 
114
   -f "$tmpdir/crontab.err",
 
115
   "Write --lib/crontab.err",
 
116
) or diag(`ls -l $tmpdir`);
 
117
 
 
118
my $err = -f "$tmpdir/crontab.err" ? `cat $tmpdir/crontab.err` : '';
 
119
is(
 
120
   $err,
 
121
   "",
 
122
   "No crontab error"
 
123
);
 
124
 
 
125
system("crontab -r 2>/dev/null");
 
126
$crontab = `crontab -l 2>/dev/null`;
 
127
is(
 
128
   $crontab,
 
129
   "",
 
130
   "Removed crontab"
 
131
);
 
132
 
 
133
# #############################################################################
 
134
# Handle bad crontab lines.
 
135
# #############################################################################
 
136
 
 
137
$svc0 = Percona::WebAPI::Resource::Service->new(
 
138
   ts             => 100,
 
139
   name           => 'query-history',
 
140
   run_schedule   => '* * * * Foo',  # "foo":0: bad day-of-week
 
141
   spool_schedule => '* 8 * * Mon',
 
142
   tasks          => [ $run0 ],
 
143
);
 
144
 
 
145
eval {
 
146
   $output = output(
 
147
      sub {
 
148
         pt_agent::schedule_services(
 
149
            services => [ $svc0 ],
 
150
            lib_dir  => $tmpdir,
 
151
         ),
 
152
      },
 
153
      stderr => 1,
 
154
      die    => 1,
 
155
   );
 
156
};
 
157
 
 
158
like(
 
159
   $EVAL_ERROR,
 
160
   qr/Error setting new crontab/,
 
161
   "Throws errors"
 
162
) or diag($output);
 
163
 
 
164
$crontab = `crontab -l 2>/dev/null`;
 
165
is(
 
166
   $crontab,
 
167
   "",
 
168
   "Bad schedule_services()"
 
169
);
 
170
 
 
171
ok(
 
172
   -f "$tmpdir/crontab",
 
173
   "Wrote crontab to --lib/crontab"
 
174
) or diag(`ls -l $tmpdir`);
 
175
 
 
176
ok(
 
177
   -f "$tmpdir/crontab.err",
 
178
   "Write --lib/crontab.err",
 
179
) or diag(`ls -l $tmpdir`);
 
180
 
 
181
$err = -f "$tmpdir/crontab.err" ? `cat $tmpdir/crontab.err` : '';
 
182
like(
 
183
   $err,
 
184
   qr/bad/,
 
185
   "Crontab error"
 
186
);
 
187
 
 
188
system("crontab -r 2>/dev/null");
 
189
$crontab = `crontab -l 2>/dev/null`;
 
190
is(
 
191
   $crontab,
 
192
   "",
 
193
   "Removed crontab"
 
194
);
 
195
 
 
196
 
 
197
# #############################################################################
 
198
# Done.
 
199
# #############################################################################
 
200
done_testing;