~percona-toolkit-dev/percona-toolkit/pt-stalk-sleep-collect-option

« back to all changes in this revision

Viewing changes to t/pt-agent/make_new_crontab.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);
 
15
 
 
16
use Percona::Test;
 
17
require "$trunk/bin/pt-agent";
 
18
 
 
19
Percona::Toolkit->import(qw(have_required_args Dumper));
 
20
 
 
21
my $sample = "t/pt-agent/samples";
 
22
 
 
23
sub test_make_new_crontab {
 
24
   my (%args) = @_;
 
25
   have_required_args(\%args, qw(
 
26
      file
 
27
      services
 
28
   )) or die;
 
29
   my $file     = $args{file};
 
30
   my $services = $args{services};
 
31
 
 
32
   my $crontab_list = slurp_file("$trunk/$sample/$file.in");
 
33
 
 
34
   my $new_crontab = pt_agent::make_new_crontab(
 
35
      services     => $services,
 
36
      crontab_list => $crontab_list,
 
37
      bin_dir      => '',
 
38
   );
 
39
 
 
40
   ok(
 
41
      no_diff(
 
42
         $new_crontab,
 
43
         "$sample/$file.out",
 
44
         cmd_output => 1,
 
45
      ),
 
46
      $args{name} || $file,
 
47
   ) or diag($new_crontab);
 
48
 
49
 
 
50
my $run0 = Percona::WebAPI::Resource::Task->new(
 
51
   name    => 'query-history',
 
52
   number  => '0',
 
53
   program => 'pt-query-digest',
 
54
   options => '--output json',
 
55
   output  => 'spool',
 
56
);
 
57
 
 
58
my $svc0 = Percona::WebAPI::Resource::Service->new(
 
59
   ts             => '100',
 
60
   name           => 'query-history',
 
61
   run_schedule   => '* 8 * * 1,2,3,4,5',
 
62
   spool_schedule => '* 9 * * 1,2,3,4,5',
 
63
   tasks          => [ $run0 ],
 
64
);
 
65
 
 
66
# Empty crontab, add the service.
 
67
test_make_new_crontab(
 
68
   file => "crontab001",
 
69
   services => [ $svc0 ],
 
70
);
 
71
 
 
72
# Crontab has another line, add the service to it.
 
73
test_make_new_crontab(
 
74
   file => "crontab002",
 
75
   services => [ $svc0 ],
 
76
);
 
77
 
 
78
# Crontab has another line and an old service, remove the old service
 
79
# and add the current service.
 
80
test_make_new_crontab(
 
81
   file => "crontab003",
 
82
   services => [ $svc0 ],
 
83
);
 
84
 
 
85
# Crontab has old service, remove it and add only new service.
 
86
test_make_new_crontab(
 
87
   file => "crontab004",
 
88
   services => [ $svc0 ],
 
89
);
 
90
 
 
91
# #############################################################################
 
92
# Use real crontab.
 
93
# #############################################################################
 
94
 
 
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.
 
100
 
 
101
my $crontab = `crontab -l 2>/dev/null`;
 
102
SKIP: {
 
103
   skip 'Crontab is not empty', 3 if $crontab;
 
104
 
 
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`;
 
114
 
 
115
   $crontab = `crontab -l 2>&1`;
 
116
 
 
117
   is(
 
118
      $crontab,
 
119
      "* 0  *  *  *  date > /dev/null\n",
 
120
      "Set other crontab line"
 
121
   ) or diag($output);
 
122
 
 
123
   unlink $file or warn "Cannot remove $file: $OS_ERROR";
 
124
 
 
125
   my $new_crontab = pt_agent::make_new_crontab(
 
126
      services => [ $svc0 ],
 
127
      bin_dir  => '',
 
128
   );
 
129
 
 
130
   is(
 
131
      $new_crontab,
 
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
 
135
",
 
136
      "Runs crontab -l by default"
 
137
   );
 
138
 
 
139
   system("crontab -r 2>/dev/null");
 
140
   $crontab = `crontab -l 2>/dev/null`;
 
141
   is(
 
142
      $crontab,
 
143
      "",
 
144
      "Removed crontab"
 
145
   );
 
146
};
 
147
 
 
148
# #############################################################################
 
149
# Done.
 
150
# #############################################################################
 
151
done_testing;