Merge lp:~percona-toolkit-dev/percona-toolkit/release-2.2.8 into lp:percona-toolkit/2.2
- release-2.2.8
- Merge into 2.2
Proposed by
Daniel Nichter
Status: | Merged |
---|---|
Approved by: | Daniel Nichter |
Approved revision: | 604 |
Merged at revision: | 605 |
Proposed branch: | lp:~percona-toolkit-dev/percona-toolkit/release-2.2.8 |
Merge into: | lp:percona-toolkit/2.2 |
Diff against target: |
5689 lines (+347/-4800) (has conflicts) 59 files modified
Changelog (+10/-0) bin/pt-archiver (+1/-1) bin/pt-config-diff (+1/-1) bin/pt-deadlock-logger (+17/-2) bin/pt-diskstats (+1/-1) bin/pt-duplicate-key-checker (+1/-1) bin/pt-find (+1/-1) bin/pt-fk-error-logger (+1/-1) bin/pt-heartbeat (+1/-1) bin/pt-index-usage (+1/-1) bin/pt-kill (+1/-1) bin/pt-online-schema-change (+25/-16) bin/pt-query-digest (+1/-1) bin/pt-slave-delay (+1/-1) bin/pt-slave-restart (+1/-1) bin/pt-table-checksum (+128/-17) bin/pt-table-sync (+1/-1) bin/pt-upgrade (+1/-1) bin/pt-variable-advisor (+1/-1) lib/Percona/Agent/Logger.pm (+0/-341) lib/Percona/Test/Mock/AgentLogger.pm (+0/-129) lib/Percona/Test/Mock/UserAgent.pm (+0/-71) lib/Percona/Toolkit.pm (+1/-1) lib/Percona/WebAPI/Client.pm (+0/-321) lib/Percona/WebAPI/Exception/Request.pm (+0/-69) lib/Percona/WebAPI/Exception/Resource.pm (+0/-66) lib/Percona/WebAPI/Representation.pm (+0/-86) lib/Percona/WebAPI/Resource/Agent.pm (+0/-77) lib/Percona/WebAPI/Resource/Config.pm (+0/-55) lib/Percona/WebAPI/Resource/LogEntry.pm (+0/-66) lib/Percona/WebAPI/Resource/Service.pm (+0/-94) lib/Percona/WebAPI/Resource/Task.pm (+0/-62) t/lib/Percona/WebAPI/Client.t (+0/-236) t/lib/Percona/WebAPI/Representation.t (+0/-51) t/pt-agent/basics.t (+0/-101) t/pt-agent/get_services.t (+0/-423) t/pt-agent/init_agent.t (+0/-333) t/pt-agent/make_new_crontab.t (+0/-151) t/pt-agent/replace_special_vars.t (+0/-73) t/pt-agent/run_agent.t (+0/-527) t/pt-agent/run_service.t (+0/-503) t/pt-agent/samples/crontab001.out (+0/-2) t/pt-agent/samples/crontab002.in (+0/-1) t/pt-agent/samples/crontab002.out (+0/-3) t/pt-agent/samples/crontab003.in (+0/-3) t/pt-agent/samples/crontab003.out (+0/-3) t/pt-agent/samples/crontab004.in (+0/-2) t/pt-agent/samples/crontab004.out (+0/-2) t/pt-agent/samples/query-history/data001.json (+0/-152) t/pt-agent/samples/query-history/data001.send (+0/-166) t/pt-agent/samples/service001 (+0/-19) t/pt-agent/samples/write_services001 (+0/-19) t/pt-agent/schedule_services.t (+0/-200) t/pt-agent/send_data.t (+0/-234) t/pt-agent/write_services.t (+0/-108) t/pt-online-schema-change/plugin.t (+1/-0) t/pt-online-schema-change/samples/plugins/all_hooks.pm (+7/-0) t/pt-table-checksum/plugin.t (+98/-0) t/pt-table-checksum/samples/plugins/all_hooks.pm (+45/-0) Contents conflict in bin/pt-agent |
To merge this branch: | bzr merge lp:~percona-toolkit-dev/percona-toolkit/release-2.2.8 |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Daniel Nichter | Approve | ||
Review via email:
|
Commit message
Description of the change
To post a comment you must log in.
Revision history for this message
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Daniel Nichter (daniel-nichter) : | # |
review:
Approve
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'Changelog' | |||
2 | --- Changelog 2014-02-20 08:10:16 +0000 | |||
3 | +++ Changelog 2014-05-30 01:02:37 +0000 | |||
4 | @@ -1,5 +1,15 @@ | |||
5 | 1 | Changelog for Percona Toolkit | 1 | Changelog for Percona Toolkit |
6 | 2 | 2 | ||
7 | 3 | * Removed pt-agent | ||
8 | 4 | * Added pt-slave-restart GTID support | ||
9 | 5 | * Added pt-table-checksum --plugin | ||
10 | 6 | * Fixed bug 1304062: --ignore-tables does not work correctly | ||
11 | 7 | * Fixed bug 1295667: pt-deadlock-logger logs incorrect ts | ||
12 | 8 | * Fixed bug 1254233: pt-mysql-summary blank InnoDB section for 5.6 | ||
13 | 9 | * Fixed bug 1286250: pt-online-schema-change requests password twice | ||
14 | 10 | * Fixed bug 965553: pt-query-digest dosn't fingerprint true/false literals correctly | ||
15 | 11 | * Fixed bug 290911: pt-show-grant --ask-pass prints "Enter password" to STDOUT | ||
16 | 12 | |||
17 | 3 | v2.2.7 released 2014-02-20 | 13 | v2.2.7 released 2014-02-20 |
18 | 4 | 14 | ||
19 | 5 | * Fixed bug 1279502: --version-check behaves like spyware | 15 | * Fixed bug 1279502: --version-check behaves like spyware |
20 | 6 | 16 | ||
21 | === renamed file 'bin/pt-agent' => 'bin/pt-agent.THIS' | |||
22 | === modified file 'bin/pt-archiver' | |||
23 | --- bin/pt-archiver 2014-05-24 21:36:33 +0000 | |||
24 | +++ bin/pt-archiver 2014-05-30 01:02:37 +0000 | |||
25 | @@ -43,7 +43,7 @@ | |||
26 | 43 | { | 43 | { |
27 | 44 | package Percona::Toolkit; | 44 | package Percona::Toolkit; |
28 | 45 | 45 | ||
30 | 46 | our $VERSION = '2.2.7'; | 46 | our $VERSION = '2.2.8'; |
31 | 47 | 47 | ||
32 | 48 | use strict; | 48 | use strict; |
33 | 49 | use warnings FATAL => 'all'; | 49 | use warnings FATAL => 'all'; |
34 | 50 | 50 | ||
35 | === modified file 'bin/pt-config-diff' | |||
36 | --- bin/pt-config-diff 2014-05-24 21:36:33 +0000 | |||
37 | +++ bin/pt-config-diff 2014-05-30 01:02:37 +0000 | |||
38 | @@ -43,7 +43,7 @@ | |||
39 | 43 | { | 43 | { |
40 | 44 | package Percona::Toolkit; | 44 | package Percona::Toolkit; |
41 | 45 | 45 | ||
43 | 46 | our $VERSION = '2.2.7'; | 46 | our $VERSION = '2.2.8'; |
44 | 47 | 47 | ||
45 | 48 | use strict; | 48 | use strict; |
46 | 49 | use warnings FATAL => 'all'; | 49 | use warnings FATAL => 'all'; |
47 | 50 | 50 | ||
48 | === modified file 'bin/pt-deadlock-logger' | |||
49 | --- bin/pt-deadlock-logger 2014-05-24 21:36:33 +0000 | |||
50 | +++ bin/pt-deadlock-logger 2014-05-30 01:02:37 +0000 | |||
51 | @@ -42,7 +42,7 @@ | |||
52 | 42 | { | 42 | { |
53 | 43 | package Percona::Toolkit; | 43 | package Percona::Toolkit; |
54 | 44 | 44 | ||
56 | 45 | our $VERSION = '2.2.7'; | 45 | our $VERSION = '2.2.8'; |
57 | 46 | 46 | ||
58 | 47 | use strict; | 47 | use strict; |
59 | 48 | use warnings FATAL => 'all'; | 48 | use warnings FATAL => 'all'; |
60 | @@ -4440,12 +4440,27 @@ | |||
61 | 4440 | 4440 | ||
62 | 4441 | my $dst; | 4441 | my $dst; |
63 | 4442 | if ( my $dst_dsn = $o->get('dest') ) { | 4442 | if ( my $dst_dsn = $o->get('dest') ) { |
64 | 4443 | |||
65 | 4444 | # set time_zone = SYSTEM , addresses https://bugs.launchpad.net/percona-toolkit/+bug/1295667 | ||
66 | 4445 | my $set_tz = sub { | ||
67 | 4446 | my ($dbh) = @_; | ||
68 | 4447 | my $sql = "SET time_zone=SYSTEM /* pt-deadlock-logger */"; | ||
69 | 4448 | eval { | ||
70 | 4449 | PTDEBUG && _d($dbh, $sql); | ||
71 | 4450 | $dbh->do($sql); | ||
72 | 4451 | }; | ||
73 | 4452 | if ( $EVAL_ERROR ) { | ||
74 | 4453 | die "Failed to $sql: $EVAL_ERROR\n"; | ||
75 | 4454 | } | ||
76 | 4455 | }; | ||
77 | 4456 | |||
78 | 4443 | $dst = Cxn->new( | 4457 | $dst = Cxn->new( |
79 | 4444 | dsn => $dst_dsn, | 4458 | dsn => $dst_dsn, |
80 | 4445 | prev_dsn => ($src ? $src->dsn : undef), | 4459 | prev_dsn => ($src ? $src->dsn : undef), |
81 | 4446 | parent => $o->get('daemonize'), | 4460 | parent => $o->get('daemonize'), |
82 | 4447 | DSNParser => $dp, | 4461 | DSNParser => $dp, |
83 | 4448 | OptionParser => $o, | 4462 | OptionParser => $o, |
84 | 4463 | set => $set_tz, | ||
85 | 4449 | ); | 4464 | ); |
86 | 4450 | } | 4465 | } |
87 | 4451 | 4466 | ||
88 | @@ -5199,7 +5214,7 @@ | |||
89 | 5199 | 5214 | ||
90 | 5200 | CREATE TABLE deadlocks ( | 5215 | CREATE TABLE deadlocks ( |
91 | 5201 | server char(20) NOT NULL, | 5216 | server char(20) NOT NULL, |
93 | 5202 | ts datetime NOT NULL, | 5217 | ts timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
94 | 5203 | thread int unsigned NOT NULL, | 5218 | thread int unsigned NOT NULL, |
95 | 5204 | txn_id bigint unsigned NOT NULL, | 5219 | txn_id bigint unsigned NOT NULL, |
96 | 5205 | txn_time smallint unsigned NOT NULL, | 5220 | txn_time smallint unsigned NOT NULL, |
97 | 5206 | 5221 | ||
98 | === modified file 'bin/pt-diskstats' | |||
99 | --- bin/pt-diskstats 2014-05-24 21:36:33 +0000 | |||
100 | +++ bin/pt-diskstats 2014-05-30 01:02:37 +0000 | |||
101 | @@ -38,7 +38,7 @@ | |||
102 | 38 | { | 38 | { |
103 | 39 | package Percona::Toolkit; | 39 | package Percona::Toolkit; |
104 | 40 | 40 | ||
106 | 41 | our $VERSION = '2.2.7'; | 41 | our $VERSION = '2.2.8'; |
107 | 42 | 42 | ||
108 | 43 | use strict; | 43 | use strict; |
109 | 44 | use warnings FATAL => 'all'; | 44 | use warnings FATAL => 'all'; |
110 | 45 | 45 | ||
111 | === modified file 'bin/pt-duplicate-key-checker' | |||
112 | --- bin/pt-duplicate-key-checker 2014-05-28 01:15:25 +0000 | |||
113 | +++ bin/pt-duplicate-key-checker 2014-05-30 01:02:37 +0000 | |||
114 | @@ -39,7 +39,7 @@ | |||
115 | 39 | { | 39 | { |
116 | 40 | package Percona::Toolkit; | 40 | package Percona::Toolkit; |
117 | 41 | 41 | ||
119 | 42 | our $VERSION = '2.2.7'; | 42 | our $VERSION = '2.2.8'; |
120 | 43 | 43 | ||
121 | 44 | use strict; | 44 | use strict; |
122 | 45 | use warnings FATAL => 'all'; | 45 | use warnings FATAL => 'all'; |
123 | 46 | 46 | ||
124 | === modified file 'bin/pt-find' | |||
125 | --- bin/pt-find 2014-05-24 21:36:33 +0000 | |||
126 | +++ bin/pt-find 2014-05-30 01:02:37 +0000 | |||
127 | @@ -35,7 +35,7 @@ | |||
128 | 35 | { | 35 | { |
129 | 36 | package Percona::Toolkit; | 36 | package Percona::Toolkit; |
130 | 37 | 37 | ||
132 | 38 | our $VERSION = '2.2.7'; | 38 | our $VERSION = '2.2.8'; |
133 | 39 | 39 | ||
134 | 40 | use strict; | 40 | use strict; |
135 | 41 | use warnings FATAL => 'all'; | 41 | use warnings FATAL => 'all'; |
136 | 42 | 42 | ||
137 | === modified file 'bin/pt-fk-error-logger' | |||
138 | --- bin/pt-fk-error-logger 2014-05-24 21:36:33 +0000 | |||
139 | +++ bin/pt-fk-error-logger 2014-05-30 01:02:37 +0000 | |||
140 | @@ -37,7 +37,7 @@ | |||
141 | 37 | { | 37 | { |
142 | 38 | package Percona::Toolkit; | 38 | package Percona::Toolkit; |
143 | 39 | 39 | ||
145 | 40 | our $VERSION = '2.2.7'; | 40 | our $VERSION = '2.2.8'; |
146 | 41 | 41 | ||
147 | 42 | use strict; | 42 | use strict; |
148 | 43 | use warnings FATAL => 'all'; | 43 | use warnings FATAL => 'all'; |
149 | 44 | 44 | ||
150 | === modified file 'bin/pt-heartbeat' | |||
151 | --- bin/pt-heartbeat 2014-05-24 21:36:33 +0000 | |||
152 | +++ bin/pt-heartbeat 2014-05-30 01:02:37 +0000 | |||
153 | @@ -38,7 +38,7 @@ | |||
154 | 38 | { | 38 | { |
155 | 39 | package Percona::Toolkit; | 39 | package Percona::Toolkit; |
156 | 40 | 40 | ||
158 | 41 | our $VERSION = '2.2.7'; | 41 | our $VERSION = '2.2.8'; |
159 | 42 | 42 | ||
160 | 43 | use strict; | 43 | use strict; |
161 | 44 | use warnings FATAL => 'all'; | 44 | use warnings FATAL => 'all'; |
162 | 45 | 45 | ||
163 | === modified file 'bin/pt-index-usage' | |||
164 | --- bin/pt-index-usage 2014-05-28 17:03:30 +0000 | |||
165 | +++ bin/pt-index-usage 2014-05-30 01:02:37 +0000 | |||
166 | @@ -45,7 +45,7 @@ | |||
167 | 45 | { | 45 | { |
168 | 46 | package Percona::Toolkit; | 46 | package Percona::Toolkit; |
169 | 47 | 47 | ||
171 | 48 | our $VERSION = '2.2.7'; | 48 | our $VERSION = '2.2.8'; |
172 | 49 | 49 | ||
173 | 50 | use strict; | 50 | use strict; |
174 | 51 | use warnings FATAL => 'all'; | 51 | use warnings FATAL => 'all'; |
175 | 52 | 52 | ||
176 | === modified file 'bin/pt-kill' | |||
177 | --- bin/pt-kill 2014-05-28 17:03:30 +0000 | |||
178 | +++ bin/pt-kill 2014-05-30 01:02:37 +0000 | |||
179 | @@ -47,7 +47,7 @@ | |||
180 | 47 | { | 47 | { |
181 | 48 | package Percona::Toolkit; | 48 | package Percona::Toolkit; |
182 | 49 | 49 | ||
184 | 50 | our $VERSION = '2.2.7'; | 50 | our $VERSION = '2.2.8'; |
185 | 51 | 51 | ||
186 | 52 | use strict; | 52 | use strict; |
187 | 53 | use warnings FATAL => 'all'; | 53 | use warnings FATAL => 'all'; |
188 | 54 | 54 | ||
189 | === modified file 'bin/pt-online-schema-change' | |||
190 | --- bin/pt-online-schema-change 2014-05-24 21:36:33 +0000 | |||
191 | +++ bin/pt-online-schema-change 2014-05-30 01:02:37 +0000 | |||
192 | @@ -54,7 +54,7 @@ | |||
193 | 54 | { | 54 | { |
194 | 55 | package Percona::Toolkit; | 55 | package Percona::Toolkit; |
195 | 56 | 56 | ||
197 | 57 | our $VERSION = '2.2.7'; | 57 | our $VERSION = '2.2.8'; |
198 | 58 | 58 | ||
199 | 59 | use strict; | 59 | use strict; |
200 | 60 | use warnings FATAL => 'all'; | 60 | use warnings FATAL => 'all'; |
201 | @@ -3771,7 +3771,7 @@ | |||
202 | 3771 | 3771 | ||
203 | 3772 | my $dbh = $self->{dbh}; | 3772 | my $dbh = $self->{dbh}; |
204 | 3773 | if ( !$dbh || !$dbh->ping() ) { | 3773 | if ( !$dbh || !$dbh->ping() ) { |
206 | 3774 | if ( $self->{ask_pass} && !$self->{asked_for_pass} ) { | 3774 | if ( $self->{ask_pass} && !$self->{asked_for_pass} && !defined $dsn->{p}) { |
207 | 3775 | $dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: "); | 3775 | $dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: "); |
208 | 3776 | $self->{asked_for_pass} = 1; | 3776 | $self->{asked_for_pass} = 1; |
209 | 3777 | } | 3777 | } |
210 | @@ -8105,21 +8105,29 @@ | |||
211 | 8105 | return; | 8105 | return; |
212 | 8106 | }; | 8106 | }; |
213 | 8107 | 8107 | ||
224 | 8108 | my $get_lag = sub { | 8108 | my $get_lag; |
225 | 8109 | my ($cxn) = @_; | 8109 | # The plugin is able to override the slavelag check so tools like |
226 | 8110 | my $dbh = $cxn->dbh(); | 8110 | # pt-heartbeat or other replicators (Tungsten...) can be used to |
227 | 8111 | if ( !$dbh || !$dbh->ping() ) { | 8111 | # measure replication lag |
228 | 8112 | eval { $dbh = $cxn->connect() }; # connect or die trying | 8112 | if ( $plugin && $plugin->can('get_slave_lag') ) { |
229 | 8113 | if ( $EVAL_ERROR ) { | 8113 | $get_lag = $plugin->get_slave_lag(oktorun => \$oktorun); |
230 | 8114 | $oktorun = 0; # flag for cleanup tasks | 8114 | } else { |
231 | 8115 | chomp $EVAL_ERROR; | 8115 | $get_lag = sub { |
232 | 8116 | die "Lost connection to replica " . $cxn->name() | 8116 | my ($cxn) = @_; |
233 | 8117 | . " while attempting to get its lag ($EVAL_ERROR)\n"; | 8117 | my $dbh = $cxn->dbh(); |
234 | 8118 | if ( !$dbh || !$dbh->ping() ) { | ||
235 | 8119 | eval { $dbh = $cxn->connect() }; # connect or die trying | ||
236 | 8120 | if ( $EVAL_ERROR ) { | ||
237 | 8121 | $oktorun = 0; # flag for cleanup tasks | ||
238 | 8122 | chomp $EVAL_ERROR; | ||
239 | 8123 | die "Lost connection to replica " . $cxn->name() | ||
240 | 8124 | . " while attempting to get its lag ($EVAL_ERROR)\n"; | ||
241 | 8125 | } | ||
242 | 8118 | } | 8126 | } |
247 | 8119 | } | 8127 | return $ms->get_slave_lag($dbh); |
248 | 8120 | return $ms->get_slave_lag($dbh); | 8128 | }; |
249 | 8121 | }; | 8129 | } |
250 | 8122 | 8130 | ||
251 | 8123 | $replica_lag = new ReplicaLagWaiter( | 8131 | $replica_lag = new ReplicaLagWaiter( |
252 | 8124 | slaves => $slave_lag_cxns, | 8132 | slaves => $slave_lag_cxns, |
253 | 8125 | max_lag => $o->get('max-lag'), | 8133 | max_lag => $o->get('max-lag'), |
254 | @@ -11345,6 +11353,7 @@ | |||
255 | 11345 | after_drop_old_table | 11353 | after_drop_old_table |
256 | 11346 | before_drop_triggers | 11354 | before_drop_triggers |
257 | 11347 | before_exit | 11355 | before_exit |
258 | 11356 | get_slave_lag | ||
259 | 11348 | 11357 | ||
260 | 11349 | Each hook is passed different arguments. To see which arguments are passed | 11358 | Each hook is passed different arguments. To see which arguments are passed |
261 | 11350 | to a hook, search for the hook's name in the tool's source code, like: | 11359 | to a hook, search for the hook's name in the tool's source code, like: |
262 | 11351 | 11360 | ||
263 | === modified file 'bin/pt-query-digest' | |||
264 | --- bin/pt-query-digest 2014-05-28 17:03:30 +0000 | |||
265 | +++ bin/pt-query-digest 2014-05-30 01:02:37 +0000 | |||
266 | @@ -64,7 +64,7 @@ | |||
267 | 64 | { | 64 | { |
268 | 65 | package Percona::Toolkit; | 65 | package Percona::Toolkit; |
269 | 66 | 66 | ||
271 | 67 | our $VERSION = '2.2.7'; | 67 | our $VERSION = '2.2.8'; |
272 | 68 | 68 | ||
273 | 69 | use strict; | 69 | use strict; |
274 | 70 | use warnings FATAL => 'all'; | 70 | use warnings FATAL => 'all'; |
275 | 71 | 71 | ||
276 | === modified file 'bin/pt-slave-delay' | |||
277 | --- bin/pt-slave-delay 2014-05-24 21:36:33 +0000 | |||
278 | +++ bin/pt-slave-delay 2014-05-30 01:02:37 +0000 | |||
279 | @@ -40,7 +40,7 @@ | |||
280 | 40 | { | 40 | { |
281 | 41 | package Percona::Toolkit; | 41 | package Percona::Toolkit; |
282 | 42 | 42 | ||
284 | 43 | our $VERSION = '2.2.7'; | 43 | our $VERSION = '2.2.8'; |
285 | 44 | 44 | ||
286 | 45 | use strict; | 45 | use strict; |
287 | 46 | use warnings FATAL => 'all'; | 46 | use warnings FATAL => 'all'; |
288 | 47 | 47 | ||
289 | === modified file 'bin/pt-slave-restart' | |||
290 | --- bin/pt-slave-restart 2014-05-28 22:25:08 +0000 | |||
291 | +++ bin/pt-slave-restart 2014-05-30 01:02:37 +0000 | |||
292 | @@ -41,7 +41,7 @@ | |||
293 | 41 | { | 41 | { |
294 | 42 | package Percona::Toolkit; | 42 | package Percona::Toolkit; |
295 | 43 | 43 | ||
297 | 44 | our $VERSION = '2.2.7'; | 44 | our $VERSION = '2.2.8'; |
298 | 45 | 45 | ||
299 | 46 | use strict; | 46 | use strict; |
300 | 47 | use warnings FATAL => 'all'; | 47 | use warnings FATAL => 'all'; |
301 | 48 | 48 | ||
302 | === modified file 'bin/pt-table-checksum' | |||
303 | --- bin/pt-table-checksum 2014-05-28 01:15:25 +0000 | |||
304 | +++ bin/pt-table-checksum 2014-05-30 01:02:37 +0000 | |||
305 | @@ -57,7 +57,7 @@ | |||
306 | 57 | { | 57 | { |
307 | 58 | package Percona::Toolkit; | 58 | package Percona::Toolkit; |
308 | 59 | 59 | ||
310 | 60 | our $VERSION = '2.2.7'; | 60 | our $VERSION = '2.2.8'; |
311 | 61 | 61 | ||
312 | 62 | use strict; | 62 | use strict; |
313 | 63 | use warnings FATAL => 'all'; | 63 | use warnings FATAL => 'all'; |
314 | @@ -9222,6 +9222,30 @@ | |||
315 | 9222 | my $slaves = []; # all slaves (that we can find) | 9222 | my $slaves = []; # all slaves (that we can find) |
316 | 9223 | my $slave_lag_cxns; # slaves whose lag we'll check | 9223 | my $slave_lag_cxns; # slaves whose lag we'll check |
317 | 9224 | 9224 | ||
318 | 9225 | # ######################################################################## | ||
319 | 9226 | # Create --plugin. | ||
320 | 9227 | # ######################################################################## | ||
321 | 9228 | my $plugin; | ||
322 | 9229 | if ( my $file = $o->get('plugin') ) { | ||
323 | 9230 | die "--plugin file $file does not exist\n" unless -f $file; | ||
324 | 9231 | eval { | ||
325 | 9232 | require $file; | ||
326 | 9233 | }; | ||
327 | 9234 | die "Error loading --plugin $file: $EVAL_ERROR" if $EVAL_ERROR; | ||
328 | 9235 | eval { | ||
329 | 9236 | $plugin = pt_table_checksum_plugin->new( | ||
330 | 9237 | master_cxn => $master_cxn, | ||
331 | 9238 | explain => $o->get('explain'), | ||
332 | 9239 | quiet => $o->get('quiet'), | ||
333 | 9240 | resume => $o->get('resume'), | ||
334 | 9241 | Quoter => $q, | ||
335 | 9242 | TableParser => $tp, | ||
336 | 9243 | ); | ||
337 | 9244 | }; | ||
338 | 9245 | die "Error creating --plugin: $EVAL_ERROR" if $EVAL_ERROR; | ||
339 | 9246 | print "Created plugin from $file.\n"; | ||
340 | 9247 | } | ||
341 | 9248 | |||
342 | 9225 | my $replica_lag; # ReplicaLagWaiter object | 9249 | my $replica_lag; # ReplicaLagWaiter object |
343 | 9226 | my $replica_lag_pr; # Progress for ReplicaLagWaiter | 9250 | my $replica_lag_pr; # Progress for ReplicaLagWaiter |
344 | 9227 | my $sys_load; # MySQLStatusWaiter object | 9251 | my $sys_load; # MySQLStatusWaiter object |
345 | @@ -9446,6 +9470,11 @@ | |||
346 | 9446 | # ##################################################################### | 9470 | # ##################################################################### |
347 | 9447 | if ( $o->get('replicate-check') && $o->get('replicate-check-only') ) { | 9471 | if ( $o->get('replicate-check') && $o->get('replicate-check-only') ) { |
348 | 9448 | PTDEBUG && _d('Will --replicate-check and exit'); | 9472 | PTDEBUG && _d('Will --replicate-check and exit'); |
349 | 9473 | |||
350 | 9474 | # --plugin hook | ||
351 | 9475 | if ( $plugin && $plugin->can('before_replicate_check') ) { | ||
352 | 9476 | $plugin->before_replicate_check(); | ||
353 | 9477 | } | ||
354 | 9449 | 9478 | ||
355 | 9450 | foreach my $slave ( @$slaves ) { | 9479 | foreach my $slave ( @$slaves ) { |
356 | 9451 | my $diffs = $rc->find_replication_differences( | 9480 | my $diffs = $rc->find_replication_differences( |
357 | @@ -9466,6 +9495,11 @@ | |||
358 | 9466 | } | 9495 | } |
359 | 9467 | } | 9496 | } |
360 | 9468 | 9497 | ||
361 | 9498 | # --plugin hook | ||
362 | 9499 | if ( $plugin && $plugin->can('after_replicate_check') ) { | ||
363 | 9500 | $plugin->after_replicate_check(); | ||
364 | 9501 | } | ||
365 | 9502 | |||
366 | 9469 | PTDEBUG && _d('Exit status', $exit_status, 'oktorun', $oktorun); | 9503 | PTDEBUG && _d('Exit status', $exit_status, 'oktorun', $oktorun); |
367 | 9470 | return $exit_status; | 9504 | return $exit_status; |
368 | 9471 | } | 9505 | } |
369 | @@ -9544,23 +9578,31 @@ | |||
370 | 9544 | return; | 9578 | return; |
371 | 9545 | }; | 9579 | }; |
372 | 9546 | 9580 | ||
385 | 9547 | my $get_lag = sub { | 9581 | my $get_lag; |
386 | 9548 | my ($cxn) = @_; | 9582 | # The plugin is able to override the slavelag check so tools like |
387 | 9549 | my $dbh = $cxn->dbh(); | 9583 | # pt-heartbeat or other replicators (Tungsten...) can be used to |
388 | 9550 | if ( !$dbh || !$dbh->ping() ) { | 9584 | # measure replication lag |
389 | 9551 | PTDEBUG && _d('Lost connection to slave', $cxn->name(), | 9585 | if ( $plugin && $plugin->can('get_slave_lag') ) { |
390 | 9552 | 'while waiting for slave lag'); | 9586 | $get_lag = $plugin->get_slave_lag(oktorun => \$oktorun); |
391 | 9553 | eval { $dbh = $cxn->connect() }; # connect or die trying | 9587 | } else { |
392 | 9554 | if ( $EVAL_ERROR ) { | 9588 | $get_lag = sub { |
393 | 9555 | $oktorun = 0; # Fatal error | 9589 | my ($cxn) = @_; |
394 | 9556 | chomp $EVAL_ERROR; | 9590 | my $dbh = $cxn->dbh(); |
395 | 9557 | die "Lost connection to replica " . $cxn->name() | 9591 | if ( !$dbh || !$dbh->ping() ) { |
396 | 9558 | . " while attempting to get its lag ($EVAL_ERROR)"; | 9592 | PTDEBUG && _d('Lost connection to slave', $cxn->name(), |
397 | 9593 | 'while waiting for slave lag'); | ||
398 | 9594 | eval { $dbh = $cxn->connect() }; # connect or die trying | ||
399 | 9595 | if ( $EVAL_ERROR ) { | ||
400 | 9596 | $oktorun = 0; # Fatal error | ||
401 | 9597 | chomp $EVAL_ERROR; | ||
402 | 9598 | die "Lost connection to replica " . $cxn->name() | ||
403 | 9599 | . " while attempting to get its lag ($EVAL_ERROR)"; | ||
404 | 9600 | } | ||
405 | 9559 | } | 9601 | } |
410 | 9560 | } | 9602 | return $ms->get_slave_lag($dbh); |
411 | 9561 | return $ms->get_slave_lag($dbh); | 9603 | }; |
412 | 9562 | }; | 9604 | } |
413 | 9563 | 9605 | ||
414 | 9564 | $replica_lag = new ReplicaLagWaiter( | 9606 | $replica_lag = new ReplicaLagWaiter( |
415 | 9565 | slaves => $slave_lag_cxns, | 9607 | slaves => $slave_lag_cxns, |
416 | 9566 | max_lag => $o->get('max-lag'), | 9608 | max_lag => $o->get('max-lag'), |
417 | @@ -10168,6 +10210,19 @@ | |||
418 | 10168 | }; | 10210 | }; |
419 | 10169 | 10211 | ||
420 | 10170 | # ######################################################################## | 10212 | # ######################################################################## |
421 | 10213 | # Init the --plugin. | ||
422 | 10214 | # ######################################################################## | ||
423 | 10215 | |||
424 | 10216 | # --plugin hook | ||
425 | 10217 | if ( $plugin && $plugin->can('init') ) { | ||
426 | 10218 | $plugin->init( | ||
427 | 10219 | slaves => $slaves, | ||
428 | 10220 | slave_lag_cxns => $slave_lag_cxns, | ||
429 | 10221 | repl_table => $repl_table, | ||
430 | 10222 | ); | ||
431 | 10223 | } | ||
432 | 10224 | |||
433 | 10225 | # ######################################################################## | ||
434 | 10171 | # Checksum each table. | 10226 | # Checksum each table. |
435 | 10172 | # ######################################################################## | 10227 | # ######################################################################## |
436 | 10173 | 10228 | ||
437 | @@ -10271,6 +10326,12 @@ | |||
438 | 10271 | @$all_cols; | 10326 | @$all_cols; |
439 | 10272 | $tbl->{checksum_cols} = \@cols; | 10327 | $tbl->{checksum_cols} = \@cols; |
440 | 10273 | 10328 | ||
441 | 10329 | # --plugin hook | ||
442 | 10330 | if ( $plugin && $plugin->can('before_checksum_table') ) { | ||
443 | 10331 | $plugin->before_checksum_table( | ||
444 | 10332 | tbl => $tbl); | ||
445 | 10333 | } | ||
446 | 10334 | |||
447 | 10274 | # Finally, checksum the table. | 10335 | # Finally, checksum the table. |
448 | 10275 | # The "1 while" loop is necessary because we're executing REPLACE | 10336 | # The "1 while" loop is necessary because we're executing REPLACE |
449 | 10276 | # statements which don't return rows and NibbleIterator only | 10337 | # statements which don't return rows and NibbleIterator only |
450 | @@ -10279,6 +10340,11 @@ | |||
451 | 10279 | # from the done callback, uses this start time. | 10340 | # from the done callback, uses this start time. |
452 | 10280 | $tbl->{checksum_results}->{start_time} = time; | 10341 | $tbl->{checksum_results}->{start_time} = time; |
453 | 10281 | 1 while $nibble_iter->next(); | 10342 | 1 while $nibble_iter->next(); |
454 | 10343 | |||
455 | 10344 | # --plugin hook | ||
456 | 10345 | if ( $plugin && $plugin->can('after_checksum_table') ) { | ||
457 | 10346 | $plugin->after_checksum_table(); | ||
458 | 10347 | } | ||
459 | 10282 | } | 10348 | } |
460 | 10283 | }; | 10349 | }; |
461 | 10284 | if ( $EVAL_ERROR ) { | 10350 | if ( $EVAL_ERROR ) { |
462 | @@ -12053,6 +12119,18 @@ | |||
463 | 12053 | tool will overwrite the PID file with the current PID. The PID file is | 12119 | tool will overwrite the PID file with the current PID. The PID file is |
464 | 12054 | removed automatically when the tool exits. | 12120 | removed automatically when the tool exits. |
465 | 12055 | 12121 | ||
466 | 12122 | =item --plugin | ||
467 | 12123 | |||
468 | 12124 | type: string | ||
469 | 12125 | |||
470 | 12126 | Perl module file that defines a C<pt_table_checksum_plugin> class. | ||
471 | 12127 | A plugin allows you to write a Perl module that can hook into many parts | ||
472 | 12128 | of pt-table-checksum. This requires a good knowledge of Perl and | ||
473 | 12129 | Percona Toolkit conventions, which are beyond this scope of this | ||
474 | 12130 | documentation. Please contact Percona if you have questions or need help. | ||
475 | 12131 | |||
476 | 12132 | See L<"PLUGIN"> for more information. | ||
477 | 12133 | |||
478 | 12056 | =item --port | 12134 | =item --port |
479 | 12057 | 12135 | ||
480 | 12058 | short form: -P; type: int; group: Connection | 12136 | short form: -P; type: int; group: Connection |
481 | @@ -12401,6 +12479,39 @@ | |||
482 | 12401 | 12479 | ||
483 | 12402 | =back | 12480 | =back |
484 | 12403 | 12481 | ||
485 | 12482 | =head1 PLUGIN | ||
486 | 12483 | |||
487 | 12484 | The file specified by L<"--plugin"> must define a class (i.e. a package) | ||
488 | 12485 | called C<pt_table_checksum_plugin> with a C<new()> subroutine. | ||
489 | 12486 | The tool will create an instance of this class and call any hooks that | ||
490 | 12487 | it defines. No hooks are required, but a plugin isn't very useful without | ||
491 | 12488 | them. | ||
492 | 12489 | |||
493 | 12490 | These hooks, in this order, are called if defined: | ||
494 | 12491 | |||
495 | 12492 | init | ||
496 | 12493 | before_replicate_check | ||
497 | 12494 | after_replicate_check | ||
498 | 12495 | get_slave_lag | ||
499 | 12496 | before_checksum_table | ||
500 | 12497 | after_checksum_table | ||
501 | 12498 | |||
502 | 12499 | Each hook is passed different arguments. To see which arguments are passed | ||
503 | 12500 | to a hook, search for the hook's name in the tool's source code, like: | ||
504 | 12501 | |||
505 | 12502 | # --plugin hook | ||
506 | 12503 | if ( $plugin && $plugin->can('init') ) { | ||
507 | 12504 | $plugin->init( | ||
508 | 12505 | slaves => $slaves, | ||
509 | 12506 | slave_lag_cxns => $slave_lag_cxns, | ||
510 | 12507 | repl_table => $repl_table, | ||
511 | 12508 | ); | ||
512 | 12509 | } | ||
513 | 12510 | |||
514 | 12511 | The comment C<# --plugin hook> precedes every hook call. | ||
515 | 12512 | |||
516 | 12513 | Please contact Percona if you have questions or need help. | ||
517 | 12514 | |||
518 | 12404 | =head1 DSN OPTIONS | 12515 | =head1 DSN OPTIONS |
519 | 12405 | 12516 | ||
520 | 12406 | These DSN options are used to create a DSN. Each option is given like | 12517 | These DSN options are used to create a DSN. Each option is given like |
521 | 12407 | 12518 | ||
522 | === modified file 'bin/pt-table-sync' | |||
523 | --- bin/pt-table-sync 2014-05-28 01:15:25 +0000 | |||
524 | +++ bin/pt-table-sync 2014-05-30 01:02:37 +0000 | |||
525 | @@ -55,7 +55,7 @@ | |||
526 | 55 | { | 55 | { |
527 | 56 | package Percona::Toolkit; | 56 | package Percona::Toolkit; |
528 | 57 | 57 | ||
530 | 58 | our $VERSION = '2.2.7'; | 58 | our $VERSION = '2.2.8'; |
531 | 59 | 59 | ||
532 | 60 | use strict; | 60 | use strict; |
533 | 61 | use warnings FATAL => 'all'; | 61 | use warnings FATAL => 'all'; |
534 | 62 | 62 | ||
535 | === modified file 'bin/pt-upgrade' | |||
536 | --- bin/pt-upgrade 2014-05-28 17:03:30 +0000 | |||
537 | +++ bin/pt-upgrade 2014-05-30 01:02:37 +0000 | |||
538 | @@ -61,7 +61,7 @@ | |||
539 | 61 | { | 61 | { |
540 | 62 | package Percona::Toolkit; | 62 | package Percona::Toolkit; |
541 | 63 | 63 | ||
543 | 64 | our $VERSION = '2.2.7'; | 64 | our $VERSION = '2.2.8'; |
544 | 65 | 65 | ||
545 | 66 | use strict; | 66 | use strict; |
546 | 67 | use warnings FATAL => 'all'; | 67 | use warnings FATAL => 'all'; |
547 | 68 | 68 | ||
548 | === modified file 'bin/pt-variable-advisor' | |||
549 | --- bin/pt-variable-advisor 2014-05-24 21:36:33 +0000 | |||
550 | +++ bin/pt-variable-advisor 2014-05-30 01:02:37 +0000 | |||
551 | @@ -44,7 +44,7 @@ | |||
552 | 44 | { | 44 | { |
553 | 45 | package Percona::Toolkit; | 45 | package Percona::Toolkit; |
554 | 46 | 46 | ||
556 | 47 | our $VERSION = '2.2.7'; | 47 | our $VERSION = '2.2.8'; |
557 | 48 | 48 | ||
558 | 49 | use strict; | 49 | use strict; |
559 | 50 | use warnings FATAL => 'all'; | 50 | use warnings FATAL => 'all'; |
560 | 51 | 51 | ||
561 | === removed directory 'lib/Percona/Agent' | |||
562 | === removed file 'lib/Percona/Agent/Logger.pm' | |||
563 | --- lib/Percona/Agent/Logger.pm 2013-12-11 03:07:36 +0000 | |||
564 | +++ lib/Percona/Agent/Logger.pm 1970-01-01 00:00:00 +0000 | |||
565 | @@ -1,341 +0,0 @@ | |||
566 | 1 | # This program is copyright 2013 Percona Ireland Ltd. | ||
567 | 2 | # Feedback and improvements are welcome. | ||
568 | 3 | # | ||
569 | 4 | # THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | ||
570 | 5 | # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | ||
571 | 6 | # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
572 | 7 | # | ||
573 | 8 | # This program is free software; you can redistribute it and/or modify it under | ||
574 | 9 | # the terms of the GNU General Public License as published by the Free Software | ||
575 | 10 | # Foundation, version 2; OR the Perl Artistic License. On UNIX and similar | ||
576 | 11 | # systems, you can issue `man perlgpl' or `man perlartistic' to read these | ||
577 | 12 | # licenses. | ||
578 | 13 | # | ||
579 | 14 | # You should have received a copy of the GNU General Public License along with | ||
580 | 15 | # this program; if not, write to the Free Software Foundation, Inc., 59 Temple | ||
581 | 16 | # Place, Suite 330, Boston, MA 02111-1307 USA. | ||
582 | 17 | # ########################################################################### | ||
583 | 18 | # Percona::Agent::Logger package | ||
584 | 19 | # ########################################################################### | ||
585 | 20 | package Percona::Agent::Logger; | ||
586 | 21 | |||
587 | 22 | use strict; | ||
588 | 23 | use warnings FATAL => 'all'; | ||
589 | 24 | use English qw(-no_match_vars); | ||
590 | 25 | |||
591 | 26 | use constant PTDEBUG => $ENV{PTDEBUG} || 0; | ||
592 | 27 | |||
593 | 28 | use POSIX qw(SIGALRM); | ||
594 | 29 | |||
595 | 30 | use Lmo; | ||
596 | 31 | use Transformers; | ||
597 | 32 | use Percona::WebAPI::Resource::LogEntry; | ||
598 | 33 | |||
599 | 34 | Transformers->import(qw(ts)); | ||
600 | 35 | |||
601 | 36 | has 'exit_status' => ( | ||
602 | 37 | is => 'rw', | ||
603 | 38 | isa => 'ScalarRef', | ||
604 | 39 | required => 1, | ||
605 | 40 | ); | ||
606 | 41 | |||
607 | 42 | has 'pid' => ( | ||
608 | 43 | is => 'ro', | ||
609 | 44 | isa => 'Int', | ||
610 | 45 | required => 1, | ||
611 | 46 | ); | ||
612 | 47 | |||
613 | 48 | has 'service' => ( | ||
614 | 49 | is => 'rw', | ||
615 | 50 | isa => 'Maybe[Str]', | ||
616 | 51 | required => 0, | ||
617 | 52 | default => sub { return; }, | ||
618 | 53 | ); | ||
619 | 54 | |||
620 | 55 | has 'data_ts' => ( | ||
621 | 56 | is => 'rw', | ||
622 | 57 | isa => 'Maybe[Int]', | ||
623 | 58 | required => 0, | ||
624 | 59 | default => sub { return; }, | ||
625 | 60 | ); | ||
626 | 61 | |||
627 | 62 | has 'online_logging' => ( | ||
628 | 63 | is => 'ro', | ||
629 | 64 | isa => 'Bool', | ||
630 | 65 | required => 0, | ||
631 | 66 | default => sub { return 1 }, | ||
632 | 67 | ); | ||
633 | 68 | |||
634 | 69 | has 'online_logging_enabled' => ( | ||
635 | 70 | is => 'rw', | ||
636 | 71 | isa => 'Bool', | ||
637 | 72 | required => 0, | ||
638 | 73 | default => sub { return 0 }, | ||
639 | 74 | ); | ||
640 | 75 | |||
641 | 76 | has 'quiet' => ( | ||
642 | 77 | is => 'rw', | ||
643 | 78 | isa => 'Int', | ||
644 | 79 | required => 0, | ||
645 | 80 | default => sub { return 0 }, | ||
646 | 81 | ); | ||
647 | 82 | |||
648 | 83 | has '_buffer' => ( | ||
649 | 84 | is => 'rw', | ||
650 | 85 | isa => 'ArrayRef', | ||
651 | 86 | required => 0, | ||
652 | 87 | default => sub { return []; }, | ||
653 | 88 | ); | ||
654 | 89 | |||
655 | 90 | has '_pipe_write' => ( | ||
656 | 91 | is => 'rw', | ||
657 | 92 | isa => 'Maybe[FileHandle]', | ||
658 | 93 | required => 0, | ||
659 | 94 | ); | ||
660 | 95 | |||
661 | 96 | sub read_stdin { | ||
662 | 97 | my ( $t ) = @_; | ||
663 | 98 | |||
664 | 99 | # Set the SIGALRM handler. | ||
665 | 100 | POSIX::sigaction( | ||
666 | 101 | SIGALRM, | ||
667 | 102 | POSIX::SigAction->new(sub { die 'read timeout'; }), | ||
668 | 103 | ) or die "Error setting SIGALRM handler: $OS_ERROR"; | ||
669 | 104 | |||
670 | 105 | my $timeout = 0; | ||
671 | 106 | my @lines; | ||
672 | 107 | eval { | ||
673 | 108 | alarm $t; | ||
674 | 109 | while(defined(my $line = <STDIN>)) { | ||
675 | 110 | push @lines, $line; | ||
676 | 111 | } | ||
677 | 112 | alarm 0; | ||
678 | 113 | }; | ||
679 | 114 | if ( $EVAL_ERROR ) { | ||
680 | 115 | PTDEBUG && _d('Read error:', $EVAL_ERROR); | ||
681 | 116 | die $EVAL_ERROR unless $EVAL_ERROR =~ m/read timeout/; | ||
682 | 117 | $timeout = 1; | ||
683 | 118 | } | ||
684 | 119 | return unless scalar @lines || $timeout; | ||
685 | 120 | return \@lines; | ||
686 | 121 | } | ||
687 | 122 | |||
688 | 123 | sub start_online_logging { | ||
689 | 124 | my ($self, %args) = @_; | ||
690 | 125 | my $client = $args{client}; | ||
691 | 126 | my $log_link = $args{log_link}; | ||
692 | 127 | my $read_timeout = $args{read_timeout} || 3; | ||
693 | 128 | |||
694 | 129 | return unless $self->online_logging; | ||
695 | 130 | |||
696 | 131 | my $pid = open(my $pipe_write, "|-"); | ||
697 | 132 | |||
698 | 133 | if ($pid) { | ||
699 | 134 | # parent | ||
700 | 135 | select $pipe_write; | ||
701 | 136 | $OUTPUT_AUTOFLUSH = 1; | ||
702 | 137 | $self->_pipe_write($pipe_write); | ||
703 | 138 | $self->online_logging_enabled(1); | ||
704 | 139 | } | ||
705 | 140 | else { | ||
706 | 141 | # child | ||
707 | 142 | my @log_entries; | ||
708 | 143 | my $n_errors = 0; | ||
709 | 144 | my $oktorun = 1; | ||
710 | 145 | QUEUE: | ||
711 | 146 | while ($oktorun) { | ||
712 | 147 | my $lines = read_stdin($read_timeout); | ||
713 | 148 | last QUEUE unless $lines; | ||
714 | 149 | LINE: | ||
715 | 150 | while ( defined(my $line = shift @$lines) ) { | ||
716 | 151 | # $line = ts,level,n_lines,message | ||
717 | 152 | my ($ts, $level, $n_lines, $msg) = $line =~ m/^([^,]+),([^,]+),([^,]+),(.+)/s; | ||
718 | 153 | if ( !$ts || !$level || !$n_lines || !$msg ) { | ||
719 | 154 | warn "$line\n"; | ||
720 | 155 | next LINE; | ||
721 | 156 | } | ||
722 | 157 | if ( $n_lines > 1 ) { | ||
723 | 158 | $n_lines--; # first line | ||
724 | 159 | for ( 1..$n_lines ) { | ||
725 | 160 | $msg .= shift @$lines; | ||
726 | 161 | } | ||
727 | 162 | } | ||
728 | 163 | |||
729 | 164 | push @log_entries, Percona::WebAPI::Resource::LogEntry->new( | ||
730 | 165 | pid => $self->pid, | ||
731 | 166 | entry_ts => $ts, | ||
732 | 167 | log_level => $level, | ||
733 | 168 | message => $msg, | ||
734 | 169 | ($self->service ? (service => $self->service) : ()), | ||
735 | 170 | ($self->data_ts ? (data_ts => $self->data_ts) : ()), | ||
736 | 171 | ); | ||
737 | 172 | } # LINE | ||
738 | 173 | |||
739 | 174 | if ( scalar @log_entries ) { | ||
740 | 175 | eval { | ||
741 | 176 | $client->post( | ||
742 | 177 | link => $log_link, | ||
743 | 178 | resources => \@log_entries, | ||
744 | 179 | ); | ||
745 | 180 | }; | ||
746 | 181 | if ( my $e = $EVAL_ERROR ) { | ||
747 | 182 | # Safegaurd: don't spam the agent log file with errors. | ||
748 | 183 | if ( ++$n_errors <= 10 ) { | ||
749 | 184 | warn "Error sending log entry to API: $e"; | ||
750 | 185 | if ( $n_errors == 10 ) { | ||
751 | 186 | my $ts = ts(time, 1); # 1=UTC | ||
752 | 187 | warn "$ts WARNING $n_errors consecutive errors, no more " | ||
753 | 188 | . "error messages will be printed until log entries " | ||
754 | 189 | . "are sent successfully again.\n"; | ||
755 | 190 | } | ||
756 | 191 | } | ||
757 | 192 | } | ||
758 | 193 | else { | ||
759 | 194 | @log_entries = (); | ||
760 | 195 | $n_errors = 0; | ||
761 | 196 | } | ||
762 | 197 | } # have log entries | ||
763 | 198 | |||
764 | 199 | # Safeguard: don't use too much memory if we lose connection | ||
765 | 200 | # to the API for a long time. | ||
766 | 201 | my $n_log_entries = scalar @log_entries; | ||
767 | 202 | if ( $n_log_entries > 1_000 ) { | ||
768 | 203 | warn "$n_log_entries log entries in send buffer, " | ||
769 | 204 | . "removing first 100 to avoid excessive usage.\n"; | ||
770 | 205 | @log_entries = @log_entries[100..($n_log_entries-1)]; | ||
771 | 206 | } | ||
772 | 207 | } # QUEUE | ||
773 | 208 | |||
774 | 209 | if ( scalar @log_entries ) { | ||
775 | 210 | my $ts = ts(time, 1); # 1=UTC | ||
776 | 211 | warn "$ts WARNING Failed to send these log entries " | ||
777 | 212 | . "(timestamps are UTC):\n"; | ||
778 | 213 | foreach my $log ( @log_entries ) { | ||
779 | 214 | warn sprintf("%s %s %s\n", | ||
780 | 215 | $log->entry_ts, | ||
781 | 216 | level_name($log->log_level), | ||
782 | 217 | $log->message, | ||
783 | 218 | ); | ||
784 | 219 | } | ||
785 | 220 | } | ||
786 | 221 | |||
787 | 222 | exit 0; | ||
788 | 223 | } # child | ||
789 | 224 | |||
790 | 225 | return; | ||
791 | 226 | } | ||
792 | 227 | |||
793 | 228 | sub level_number { | ||
794 | 229 | my $name = shift; | ||
795 | 230 | die "No log level name given" unless $name; | ||
796 | 231 | my $number = $name eq 'DEBUG' ? 1 | ||
797 | 232 | : $name eq 'INFO' ? 2 | ||
798 | 233 | : $name eq 'WARNING' ? 3 | ||
799 | 234 | : $name eq 'ERROR' ? 4 | ||
800 | 235 | : $name eq 'FATAL' ? 5 | ||
801 | 236 | : die "Invalid log level name: $name"; | ||
802 | 237 | } | ||
803 | 238 | |||
804 | 239 | sub level_name { | ||
805 | 240 | my $number = shift; | ||
806 | 241 | die "No log level name given" unless $number; | ||
807 | 242 | my $name = $number == 1 ? 'DEBUG' | ||
808 | 243 | : $number == 2 ? 'INFO' | ||
809 | 244 | : $number == 3 ? 'WARNING' | ||
810 | 245 | : $number == 4 ? 'ERROR' | ||
811 | 246 | : $number == 5 ? 'FATAL' | ||
812 | 247 | : die "Invalid log level number: $number"; | ||
813 | 248 | } | ||
814 | 249 | |||
815 | 250 | sub debug { | ||
816 | 251 | my $self = shift; | ||
817 | 252 | return if $self->online_logging; | ||
818 | 253 | return $self->_log(0, 'DEBUG', @_); | ||
819 | 254 | } | ||
820 | 255 | |||
821 | 256 | sub info { | ||
822 | 257 | my $self = shift; | ||
823 | 258 | return $self->_log(1, 'INFO', @_); | ||
824 | 259 | } | ||
825 | 260 | |||
826 | 261 | sub warning { | ||
827 | 262 | my $self = shift; | ||
828 | 263 | $self->_set_exit_status(); | ||
829 | 264 | return $self->_log(1, 'WARNING', @_); | ||
830 | 265 | } | ||
831 | 266 | |||
832 | 267 | sub error { | ||
833 | 268 | my $self = shift; | ||
834 | 269 | $self->_set_exit_status(); | ||
835 | 270 | return $self->_log(1, 'ERROR', @_); | ||
836 | 271 | } | ||
837 | 272 | |||
838 | 273 | sub fatal { | ||
839 | 274 | my $self = shift; | ||
840 | 275 | $self->_set_exit_status(); | ||
841 | 276 | $self->_log(1, 'FATAL', @_); | ||
842 | 277 | exit $self->exit_status; | ||
843 | 278 | } | ||
844 | 279 | |||
845 | 280 | sub _set_exit_status { | ||
846 | 281 | my $self = shift; | ||
847 | 282 | # exit_status is a scalar ref | ||
848 | 283 | my $exit_status = $self->exit_status; # get ref | ||
849 | 284 | $$exit_status |= 1; # deref to set | ||
850 | 285 | $self->exit_status($exit_status); # save back ref | ||
851 | 286 | return; | ||
852 | 287 | } | ||
853 | 288 | |||
854 | 289 | sub _log { | ||
855 | 290 | my ($self, $online, $level, $msg) = @_; | ||
856 | 291 | |||
857 | 292 | my $ts = ts(time, 1); # 1=UTC | ||
858 | 293 | my $level_number = level_number($level); | ||
859 | 294 | |||
860 | 295 | return if $self->quiet && $level_number < $self->quiet; | ||
861 | 296 | |||
862 | 297 | chomp($msg); | ||
863 | 298 | my $n_lines = 1; | ||
864 | 299 | $n_lines++ while $msg =~ m/\n/g; | ||
865 | 300 | |||
866 | 301 | if ( $online && $self->online_logging_enabled ) { | ||
867 | 302 | while ( defined(my $log_entry = shift @{$self->_buffer}) ) { | ||
868 | 303 | $self->_queue_log_entry(@$log_entry); | ||
869 | 304 | } | ||
870 | 305 | $self->_queue_log_entry($ts, $level_number, $n_lines, $msg); | ||
871 | 306 | } | ||
872 | 307 | else { | ||
873 | 308 | if ( $online && $self->online_logging ) { | ||
874 | 309 | push @{$self->_buffer}, [$ts, $level_number, $n_lines, $msg]; | ||
875 | 310 | } | ||
876 | 311 | |||
877 | 312 | if ( $level_number >= 3 ) { # warning | ||
878 | 313 | print STDERR "$ts $level $msg\n"; | ||
879 | 314 | } | ||
880 | 315 | else { | ||
881 | 316 | print STDOUT "$ts $level $msg\n"; | ||
882 | 317 | } | ||
883 | 318 | } | ||
884 | 319 | |||
885 | 320 | return; | ||
886 | 321 | } | ||
887 | 322 | |||
888 | 323 | sub _queue_log_entry { | ||
889 | 324 | my ($self, $ts, $log_level, $n_lines, $msg) = @_; | ||
890 | 325 | print "$ts,$log_level,$n_lines,$msg\n"; | ||
891 | 326 | return; | ||
892 | 327 | } | ||
893 | 328 | |||
894 | 329 | sub _d { | ||
895 | 330 | my ($package, undef, $line) = caller 0; | ||
896 | 331 | @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } | ||
897 | 332 | map { defined $_ ? $_ : 'undef' } | ||
898 | 333 | @_; | ||
899 | 334 | print STDERR "# $package:$line $PID ", join(' ', @_), "\n"; | ||
900 | 335 | } | ||
901 | 336 | |||
902 | 337 | no Lmo; | ||
903 | 338 | 1; | ||
904 | 339 | # ########################################################################### | ||
905 | 340 | # End Percona::Agent::Logger package | ||
906 | 341 | # ########################################################################### | ||
907 | 342 | 0 | ||
908 | === removed directory 'lib/Percona/Test' | |||
909 | === removed directory 'lib/Percona/Test/Mock' | |||
910 | === removed file 'lib/Percona/Test/Mock/AgentLogger.pm' | |||
911 | --- lib/Percona/Test/Mock/AgentLogger.pm 2013-06-17 00:28:18 +0000 | |||
912 | +++ lib/Percona/Test/Mock/AgentLogger.pm 1970-01-01 00:00:00 +0000 | |||
913 | @@ -1,129 +0,0 @@ | |||
914 | 1 | # This program is copyright 2013 Percona Ireland Ltd. | ||
915 | 2 | # Feedback and improvements are welcome. | ||
916 | 3 | # | ||
917 | 4 | # THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | ||
918 | 5 | # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | ||
919 | 6 | # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
920 | 7 | # | ||
921 | 8 | # This program is free software; you can redistribute it and/or modify it under | ||
922 | 9 | # the terms of the GNU General Public License as published by the Free Software | ||
923 | 10 | # Foundation, version 2; OR the Perl Artistic License. On UNIX and similar | ||
924 | 11 | # systems, you can issue `man perlgpl' or `man perlartistic' to read these | ||
925 | 12 | # licenses. | ||
926 | 13 | # | ||
927 | 14 | # You should have received a copy of the GNU General Public License along with | ||
928 | 15 | # this program; if not, write to the Free Software Foundation, Inc., 59 Temple | ||
929 | 16 | # Place, Suite 330, Boston, MA 02111-1307 USA. | ||
930 | 17 | # ########################################################################### | ||
931 | 18 | # Percona::Agent::Logger package | ||
932 | 19 | # ########################################################################### | ||
933 | 20 | package Percona::Test::Mock::AgentLogger; | ||
934 | 21 | |||
935 | 22 | use strict; | ||
936 | 23 | use warnings FATAL => 'all'; | ||
937 | 24 | use English qw(-no_match_vars); | ||
938 | 25 | |||
939 | 26 | use constant PTDEBUG => $ENV{PTDEBUG} || 0; | ||
940 | 27 | |||
941 | 28 | sub new { | ||
942 | 29 | my ($class, %args) = @_; | ||
943 | 30 | my $self = { | ||
944 | 31 | log => $args{log}, | ||
945 | 32 | |||
946 | 33 | exit_status => $args{exit_status}, | ||
947 | 34 | pid => $args{pid}, | ||
948 | 35 | online_logging => $args{online_logging}, | ||
949 | 36 | |||
950 | 37 | service => undef, | ||
951 | 38 | data_ts => undef, | ||
952 | 39 | quiet => 0, | ||
953 | 40 | |||
954 | 41 | }; | ||
955 | 42 | return bless $self, $class; | ||
956 | 43 | } | ||
957 | 44 | |||
958 | 45 | sub service { | ||
959 | 46 | my $self = shift; | ||
960 | 47 | my $_service = shift; | ||
961 | 48 | $self->{service} = $_service if $_service; | ||
962 | 49 | return $self->{service}; | ||
963 | 50 | } | ||
964 | 51 | |||
965 | 52 | sub data_ts { | ||
966 | 53 | my $self = shift; | ||
967 | 54 | my $_data_ts = shift; | ||
968 | 55 | $self->{data_ts} = $_data_ts if $_data_ts; | ||
969 | 56 | return $self->{data_ts}; | ||
970 | 57 | } | ||
971 | 58 | |||
972 | 59 | sub quiet { | ||
973 | 60 | my $self = shift; | ||
974 | 61 | my $_quiet = shift; | ||
975 | 62 | $self->{quiet} = $_quiet if $_quiet; | ||
976 | 63 | return $self->{quiet}; | ||
977 | 64 | } | ||
978 | 65 | |||
979 | 66 | sub start_online_logging { | ||
980 | 67 | my ($self, %args) = @_; | ||
981 | 68 | $self->_log('-', 'Called start_online_logging()'); | ||
982 | 69 | return; | ||
983 | 70 | } | ||
984 | 71 | |||
985 | 72 | sub level_number { | ||
986 | 73 | my $name = shift; | ||
987 | 74 | die "No log level name given" unless $name; | ||
988 | 75 | my $number = $name eq 'DEBUG' ? 1 | ||
989 | 76 | : $name eq 'INFO' ? 2 | ||
990 | 77 | : $name eq 'WARNING' ? 3 | ||
991 | 78 | : $name eq 'ERROR' ? 4 | ||
992 | 79 | : $name eq 'FATAL' ? 5 | ||
993 | 80 | : die "Invalid log level name: $name"; | ||
994 | 81 | } | ||
995 | 82 | |||
996 | 83 | sub level_name { | ||
997 | 84 | my $number = shift; | ||
998 | 85 | die "No log level name given" unless $number; | ||
999 | 86 | my $name = $number == 1 ? 'DEBUG' | ||
1000 | 87 | : $number == 2 ? 'INFO' | ||
1001 | 88 | : $number == 3 ? 'WARNING' | ||
1002 | 89 | : $number == 4 ? 'ERROR' | ||
1003 | 90 | : $number == 5 ? 'FATAL' | ||
1004 | 91 | : die "Invalid log level number: $number"; | ||
1005 | 92 | } | ||
1006 | 93 | |||
1007 | 94 | sub debug { | ||
1008 | 95 | my $self = shift; | ||
1009 | 96 | return $self->_log('DEBUG', @_); | ||
1010 | 97 | } | ||
1011 | 98 | |||
1012 | 99 | sub info { | ||
1013 | 100 | my $self = shift; | ||
1014 | 101 | return $self->_log('INFO', @_); | ||
1015 | 102 | } | ||
1016 | 103 | |||
1017 | 104 | sub warning { | ||
1018 | 105 | my $self = shift; | ||
1019 | 106 | return $self->_log('WARNING', @_); | ||
1020 | 107 | } | ||
1021 | 108 | |||
1022 | 109 | sub error { | ||
1023 | 110 | my $self = shift; | ||
1024 | 111 | return $self->_log('ERROR', @_); | ||
1025 | 112 | } | ||
1026 | 113 | |||
1027 | 114 | sub fatal { | ||
1028 | 115 | my $self = shift; | ||
1029 | 116 | $self->_log('FATAL', @_); | ||
1030 | 117 | return 255; | ||
1031 | 118 | } | ||
1032 | 119 | |||
1033 | 120 | sub _log { | ||
1034 | 121 | my ($self, $level, $msg) = @_; | ||
1035 | 122 | push @{$self->{log}}, "$level $msg"; | ||
1036 | 123 | return; | ||
1037 | 124 | } | ||
1038 | 125 | |||
1039 | 126 | 1; | ||
1040 | 127 | # ########################################################################### | ||
1041 | 128 | # End Percona::Test::Mock::AgentLogger package | ||
1042 | 129 | # ########################################################################### | ||
1043 | 130 | 0 | ||
1044 | === removed file 'lib/Percona/Test/Mock/UserAgent.pm' | |||
1045 | --- lib/Percona/Test/Mock/UserAgent.pm 2013-03-21 19:50:49 +0000 | |||
1046 | +++ lib/Percona/Test/Mock/UserAgent.pm 1970-01-01 00:00:00 +0000 | |||
1047 | @@ -1,71 +0,0 @@ | |||
1048 | 1 | # This program is copyright 2012-2013 Percona Inc. | ||
1049 | 2 | # Feedback and improvements are welcome. | ||
1050 | 3 | # | ||
1051 | 4 | # THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | ||
1052 | 5 | # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | ||
1053 | 6 | # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
1054 | 7 | # | ||
1055 | 8 | # This program is free software; you can redistribute it and/or modify it under | ||
1056 | 9 | # the terms of the GNU General Public License as published by the Free Software | ||
1057 | 10 | # Foundation, version 2; OR the Perl Artistic License. On UNIX and similar | ||
1058 | 11 | # systems, you can issue `man perlgpl' or `man perlartistic' to read these | ||
1059 | 12 | # licenses. | ||
1060 | 13 | # | ||
1061 | 14 | # You should have received a copy of the GNU General Public License along with | ||
1062 | 15 | # this program; if not, write to the Free Software Foundation, Inc., 59 Temple | ||
1063 | 16 | # Place, Suite 330, Boston, MA 02111-1307 USA. | ||
1064 | 17 | # ########################################################################### | ||
1065 | 18 | # Percona::Test::Mock::UserAgent package | ||
1066 | 19 | # ########################################################################### | ||
1067 | 20 | { | ||
1068 | 21 | package Percona::Test::Mock::UserAgent; | ||
1069 | 22 | |||
1070 | 23 | sub new { | ||
1071 | 24 | my ($class, %args) = @_; | ||
1072 | 25 | my $self = { | ||
1073 | 26 | encode => $args{encode} || sub { return $_[0] }, | ||
1074 | 27 | decode => $args{decode} || sub { return $_[0] }, | ||
1075 | 28 | requests => [], | ||
1076 | 29 | request_objs => [], | ||
1077 | 30 | responses => { | ||
1078 | 31 | get => [], | ||
1079 | 32 | post => [], | ||
1080 | 33 | put => [], | ||
1081 | 34 | }, | ||
1082 | 35 | content => { | ||
1083 | 36 | post => [], | ||
1084 | 37 | put => [], | ||
1085 | 38 | }, | ||
1086 | 39 | }; | ||
1087 | 40 | return bless $self, $class; | ||
1088 | 41 | } | ||
1089 | 42 | |||
1090 | 43 | sub request { | ||
1091 | 44 | my ($self, $req) = @_; | ||
1092 | 45 | if ( scalar @{$self->{request_objs}} > 10 ) { | ||
1093 | 46 | $self->{request_objs} = []; | ||
1094 | 47 | } | ||
1095 | 48 | push @{$self->{request_objs}}, $req; | ||
1096 | 49 | my $type = lc($req->method); | ||
1097 | 50 | push @{$self->{requests}}, uc($type) . ' ' . $req->uri; | ||
1098 | 51 | if ( $type eq 'post' || $type eq 'put' ) { | ||
1099 | 52 | push @{$self->{content}->{$type}}, $req->content; | ||
1100 | 53 | } | ||
1101 | 54 | my $r = shift @{$self->{responses}->{$type}}; | ||
1102 | 55 | my $c = $r->{content} ? $self->{encode}->($r->{content}) : ''; | ||
1103 | 56 | my $h = HTTP::Headers->new; | ||
1104 | 57 | $h->header(%{$r->{headers}}) if exists $r->{headers}; | ||
1105 | 58 | my $res = HTTP::Response->new( | ||
1106 | 59 | $r->{code} || 200, | ||
1107 | 60 | '', | ||
1108 | 61 | $h, | ||
1109 | 62 | $c, | ||
1110 | 63 | ); | ||
1111 | 64 | return $res; | ||
1112 | 65 | } | ||
1113 | 66 | |||
1114 | 67 | 1; | ||
1115 | 68 | } | ||
1116 | 69 | # ########################################################################### | ||
1117 | 70 | # End Percona::Test::Mock::UserAgent package | ||
1118 | 71 | # ########################################################################### | ||
1119 | 72 | 0 | ||
1120 | === modified file 'lib/Percona/Toolkit.pm' | |||
1121 | --- lib/Percona/Toolkit.pm 2014-02-20 03:47:43 +0000 | |||
1122 | +++ lib/Percona/Toolkit.pm 2014-05-30 01:02:37 +0000 | |||
1123 | @@ -18,7 +18,7 @@ | |||
1124 | 18 | # ########################################################################### | 18 | # ########################################################################### |
1125 | 19 | package Percona::Toolkit; | 19 | package Percona::Toolkit; |
1126 | 20 | 20 | ||
1128 | 21 | our $VERSION = '2.2.7'; | 21 | our $VERSION = '2.2.8'; |
1129 | 22 | 22 | ||
1130 | 23 | use strict; | 23 | use strict; |
1131 | 24 | use warnings FATAL => 'all'; | 24 | use warnings FATAL => 'all'; |
1132 | 25 | 25 | ||
1133 | === removed directory 'lib/Percona/WebAPI' | |||
1134 | === removed file 'lib/Percona/WebAPI/Client.pm' | |||
1135 | --- lib/Percona/WebAPI/Client.pm 2013-09-19 04:05:48 +0000 | |||
1136 | +++ lib/Percona/WebAPI/Client.pm 1970-01-01 00:00:00 +0000 | |||
1137 | @@ -1,321 +0,0 @@ | |||
1138 | 1 | # This program is copyright 2012 codenode LLC, 2012-2013 Percona Ireland Ltd. | ||
1139 | 2 | # | ||
1140 | 3 | # THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | ||
1141 | 4 | # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | ||
1142 | 5 | # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
1143 | 6 | # | ||
1144 | 7 | # This program is free software; you can redistribute it and/or modify it under | ||
1145 | 8 | # the terms of the GNU General Public License as published by the Free Software | ||
1146 | 9 | # Foundation, version 2; OR the Perl Artistic License. On UNIX and similar | ||
1147 | 10 | # systems, you can issue `man perlgpl' or `man perlartistic' to read these | ||
1148 | 11 | # licenses. | ||
1149 | 12 | # | ||
1150 | 13 | # You should have received a copy of the GNU General Public License along with | ||
1151 | 14 | # this program; if not, write to the Free Software Foundation, Inc., 59 Temple | ||
1152 | 15 | # Place, Suite 330, Boston, MA 02111-1307 USA. | ||
1153 | 16 | # ########################################################################### | ||
1154 | 17 | # Percona::WebAPI::Client package | ||
1155 | 18 | # ########################################################################### | ||
1156 | 19 | { | ||
1157 | 20 | package Percona::WebAPI::Client; | ||
1158 | 21 | |||
1159 | 22 | our $VERSION = '0.01'; | ||
1160 | 23 | |||
1161 | 24 | use strict; | ||
1162 | 25 | use warnings FATAL => 'all'; | ||
1163 | 26 | use English qw(-no_match_vars); | ||
1164 | 27 | use constant PTDEBUG => $ENV{PTDEBUG} || 0; | ||
1165 | 28 | |||
1166 | 29 | eval { | ||
1167 | 30 | require LWP; | ||
1168 | 31 | require JSON; | ||
1169 | 32 | }; | ||
1170 | 33 | |||
1171 | 34 | use Scalar::Util qw(blessed); | ||
1172 | 35 | |||
1173 | 36 | use Lmo; | ||
1174 | 37 | use Percona::Toolkit; | ||
1175 | 38 | use Percona::WebAPI::Representation; | ||
1176 | 39 | use Percona::WebAPI::Exception::Request; | ||
1177 | 40 | use Percona::WebAPI::Exception::Resource; | ||
1178 | 41 | |||
1179 | 42 | Percona::WebAPI::Representation->import(qw(as_json)); | ||
1180 | 43 | Percona::Toolkit->import(qw(_d Dumper have_required_args)); | ||
1181 | 44 | |||
1182 | 45 | has 'api_key' => ( | ||
1183 | 46 | is => 'ro', | ||
1184 | 47 | isa => 'Str', | ||
1185 | 48 | required => 1, | ||
1186 | 49 | ); | ||
1187 | 50 | |||
1188 | 51 | has 'entry_link' => ( | ||
1189 | 52 | is => 'rw', | ||
1190 | 53 | isa => 'Str', | ||
1191 | 54 | required => 0, | ||
1192 | 55 | default => sub { return 'https://cloud-api.percona.com' }, | ||
1193 | 56 | ); | ||
1194 | 57 | |||
1195 | 58 | has 'ua' => ( | ||
1196 | 59 | is => 'rw', | ||
1197 | 60 | isa => 'Object', | ||
1198 | 61 | lazy => 1, | ||
1199 | 62 | required => 0, | ||
1200 | 63 | builder => '_build_ua', | ||
1201 | 64 | ); | ||
1202 | 65 | |||
1203 | 66 | has 'response' => ( | ||
1204 | 67 | is => 'rw', | ||
1205 | 68 | isa => 'Object', | ||
1206 | 69 | required => 0, | ||
1207 | 70 | default => undef, | ||
1208 | 71 | ); | ||
1209 | 72 | |||
1210 | 73 | sub _build_ua { | ||
1211 | 74 | my $self = shift; | ||
1212 | 75 | my $ua = LWP::UserAgent->new; | ||
1213 | 76 | $ua->agent("Percona::WebAPI::Client/$Percona::WebAPI::Client::VERSION"); | ||
1214 | 77 | $ua->default_header('Content-Type', 'application/json'); | ||
1215 | 78 | $ua->default_header('X-Percona-API-Key', $self->api_key); | ||
1216 | 79 | return $ua; | ||
1217 | 80 | } | ||
1218 | 81 | |||
1219 | 82 | sub get { | ||
1220 | 83 | my ($self, %args) = @_; | ||
1221 | 84 | |||
1222 | 85 | have_required_args(\%args, qw( | ||
1223 | 86 | link | ||
1224 | 87 | )) or die; | ||
1225 | 88 | my ($link) = $args{link}; | ||
1226 | 89 | |||
1227 | 90 | # Get the resources at the link. | ||
1228 | 91 | eval { | ||
1229 | 92 | $self->_request( | ||
1230 | 93 | method => 'GET', | ||
1231 | 94 | link => $link, | ||
1232 | 95 | ); | ||
1233 | 96 | }; | ||
1234 | 97 | if ( my $e = $EVAL_ERROR ) { | ||
1235 | 98 | if (blessed($e) && $e->isa('Percona::WebAPI::Exception::Request')) { | ||
1236 | 99 | die $e; | ||
1237 | 100 | } | ||
1238 | 101 | else { | ||
1239 | 102 | die "Unknown error: $e"; | ||
1240 | 103 | } | ||
1241 | 104 | } | ||
1242 | 105 | |||
1243 | 106 | # The resource should be represented as JSON, decode it. | ||
1244 | 107 | my $resource = eval { | ||
1245 | 108 | JSON::decode_json($self->response->content); | ||
1246 | 109 | }; | ||
1247 | 110 | if ( $EVAL_ERROR ) { | ||
1248 | 111 | warn sprintf "Error decoding resource: %s: %s", | ||
1249 | 112 | $self->response->content, | ||
1250 | 113 | $EVAL_ERROR; | ||
1251 | 114 | return; | ||
1252 | 115 | } | ||
1253 | 116 | |||
1254 | 117 | # If the server tells us the resource's type, create a new object | ||
1255 | 118 | # of that type. Else, if there's no type, there's no resource, so | ||
1256 | 119 | # we should have received links. This usually only happens for the | ||
1257 | 120 | # entry link. The returned resource objects ref may be scalar or | ||
1258 | 121 | # an arrayref; the caller should know. | ||
1259 | 122 | my $resource_objects; | ||
1260 | 123 | if ( my $type = $self->response->headers->{'x-percona-resource-type'} ) { | ||
1261 | 124 | eval { | ||
1262 | 125 | $type = "Percona::WebAPI::Resource::$type"; | ||
1263 | 126 | if ( ref $resource eq 'ARRAY' ) { | ||
1264 | 127 | PTDEBUG && _d('Got a list of', $type, 'resources'); | ||
1265 | 128 | $resource_objects = []; | ||
1266 | 129 | foreach my $attribs ( @$resource ) { | ||
1267 | 130 | my $obj = $type->new(%$attribs); | ||
1268 | 131 | push @$resource_objects, $obj; | ||
1269 | 132 | } | ||
1270 | 133 | } | ||
1271 | 134 | else { | ||
1272 | 135 | PTDEBUG && _d('Got a', $type, 'resource', Dumper($resource)); | ||
1273 | 136 | $resource_objects = $type->new(%$resource); | ||
1274 | 137 | } | ||
1275 | 138 | }; | ||
1276 | 139 | if ( my $e = $EVAL_ERROR ) { | ||
1277 | 140 | die Percona::WebAPI::Exception::Resource->new( | ||
1278 | 141 | type => $type, | ||
1279 | 142 | link => $link, | ||
1280 | 143 | data => (ref $resource eq 'ARRAY' ? $resource : [ $resource ]), | ||
1281 | 144 | error => $e, | ||
1282 | 145 | ); | ||
1283 | 146 | } | ||
1284 | 147 | } | ||
1285 | 148 | elsif ( exists $resource->{links} ) { | ||
1286 | 149 | # Lie to the caller: this isn't an object, but the caller can | ||
1287 | 150 | # treat it like one, e.g. my $links = $api->get(<entry links>); | ||
1288 | 151 | # then access $links->{self}. A Links object couldn't have | ||
1289 | 152 | # dynamic attribs anyway, so no use having a real Links obj. | ||
1290 | 153 | $resource_objects = $resource->{links}; | ||
1291 | 154 | } | ||
1292 | 155 | elsif ( exists $resource->{pong} ) { | ||
1293 | 156 | PTDEBUG && _d("Ping pong!"); | ||
1294 | 157 | } | ||
1295 | 158 | else { | ||
1296 | 159 | warn "Did not get X-Percona-Resource-Type or links from $link\n"; | ||
1297 | 160 | } | ||
1298 | 161 | |||
1299 | 162 | return $resource_objects; | ||
1300 | 163 | } | ||
1301 | 164 | |||
1302 | 165 | # For a successful POST, the server sets the Location header with | ||
1303 | 166 | # the URI of the newly created resource. | ||
1304 | 167 | sub post { | ||
1305 | 168 | my $self = shift; | ||
1306 | 169 | $self->_set( | ||
1307 | 170 | @_, | ||
1308 | 171 | method => 'POST', | ||
1309 | 172 | ); | ||
1310 | 173 | return $self->response->header('Location'); | ||
1311 | 174 | } | ||
1312 | 175 | |||
1313 | 176 | sub put { | ||
1314 | 177 | my $self = shift; | ||
1315 | 178 | $self->_set( | ||
1316 | 179 | @_, | ||
1317 | 180 | method => 'PUT', | ||
1318 | 181 | ); | ||
1319 | 182 | return $self->response->header('Location'); | ||
1320 | 183 | } | ||
1321 | 184 | |||
1322 | 185 | sub delete { | ||
1323 | 186 | my ($self, %args) = @_; | ||
1324 | 187 | have_required_args(\%args, qw( | ||
1325 | 188 | link | ||
1326 | 189 | )) or die; | ||
1327 | 190 | my ($link) = $args{link}; | ||
1328 | 191 | |||
1329 | 192 | eval { | ||
1330 | 193 | $self->_request( | ||
1331 | 194 | method => 'DELETE', | ||
1332 | 195 | link => $link, | ||
1333 | 196 | headers => { 'Content-Length' => 0 }, | ||
1334 | 197 | ); | ||
1335 | 198 | }; | ||
1336 | 199 | if ( my $e = $EVAL_ERROR ) { | ||
1337 | 200 | if (blessed($e) && $e->isa('Percona::WebAPI::Exception::Request')) { | ||
1338 | 201 | die $e; | ||
1339 | 202 | } | ||
1340 | 203 | else { | ||
1341 | 204 | die "Unknown error: $e"; | ||
1342 | 205 | } | ||
1343 | 206 | } | ||
1344 | 207 | |||
1345 | 208 | return; | ||
1346 | 209 | } | ||
1347 | 210 | |||
1348 | 211 | # Low-level POST and PUT handler. | ||
1349 | 212 | sub _set { | ||
1350 | 213 | my ($self, %args) = @_; | ||
1351 | 214 | have_required_args(\%args, qw( | ||
1352 | 215 | method | ||
1353 | 216 | resources | ||
1354 | 217 | link | ||
1355 | 218 | )) or die; | ||
1356 | 219 | my $method = $args{method}; | ||
1357 | 220 | my $res = $args{resources}; | ||
1358 | 221 | my $link = $args{link}; | ||
1359 | 222 | |||
1360 | 223 | # Optional args | ||
1361 | 224 | my $headers = $args{headers}; | ||
1362 | 225 | |||
1363 | 226 | my $content = ''; | ||
1364 | 227 | if ( ref($res) eq 'ARRAY' ) { | ||
1365 | 228 | PTDEBUG && _d('List of resources'); | ||
1366 | 229 | $content = '[' . join(",\n", map { as_json($_) } @$res) . ']'; | ||
1367 | 230 | } | ||
1368 | 231 | elsif ( ref($res) ) { | ||
1369 | 232 | PTDEBUG && _d('Resource object'); | ||
1370 | 233 | $content = as_json($res); | ||
1371 | 234 | } | ||
1372 | 235 | elsif ( $res !~ m/\n/ && -f $res ) { | ||
1373 | 236 | PTDEBUG && _d('List of resources in file', $res); | ||
1374 | 237 | $content = '['; | ||
1375 | 238 | my $data = do { | ||
1376 | 239 | local $INPUT_RECORD_SEPARATOR = undef; | ||
1377 | 240 | open my $fh, '<', $res | ||
1378 | 241 | or die "Error opening $res: $OS_ERROR"; | ||
1379 | 242 | <$fh>; | ||
1380 | 243 | }; | ||
1381 | 244 | $data =~ s/,?\s*$/]/; | ||
1382 | 245 | $content .= $data; | ||
1383 | 246 | } | ||
1384 | 247 | else { | ||
1385 | 248 | PTDEBUG && _d('Resource text'); | ||
1386 | 249 | $content = $res; | ||
1387 | 250 | } | ||
1388 | 251 | |||
1389 | 252 | eval { | ||
1390 | 253 | $self->_request( | ||
1391 | 254 | method => $method, | ||
1392 | 255 | link => $link, | ||
1393 | 256 | content => $content, | ||
1394 | 257 | headers => $headers, | ||
1395 | 258 | ); | ||
1396 | 259 | }; | ||
1397 | 260 | if ( my $e = $EVAL_ERROR ) { | ||
1398 | 261 | if (blessed($e) && $e->isa('Percona::WebAPI::Exception::Request')) { | ||
1399 | 262 | die $e; | ||
1400 | 263 | } | ||
1401 | 264 | else { | ||
1402 | 265 | die "Unknown error: $e"; | ||
1403 | 266 | } | ||
1404 | 267 | } | ||
1405 | 268 | |||
1406 | 269 | return; | ||
1407 | 270 | } | ||
1408 | 271 | |||
1409 | 272 | # Low-level HTTP request handler for all methods. Sets $self->response | ||
1410 | 273 | # from the request. Returns nothing on success (HTTP status 2xx-3xx), | ||
1411 | 274 | # else throws an Percona::WebAPI::Exception::Request. | ||
1412 | 275 | sub _request { | ||
1413 | 276 | my ($self, %args) = @_; | ||
1414 | 277 | |||
1415 | 278 | have_required_args(\%args, qw( | ||
1416 | 279 | method | ||
1417 | 280 | link | ||
1418 | 281 | )) or die; | ||
1419 | 282 | my $method = $args{method}; | ||
1420 | 283 | my $link = $args{link}; | ||
1421 | 284 | |||
1422 | 285 | # Optional args | ||
1423 | 286 | my $content = $args{content}; | ||
1424 | 287 | my $headers = $args{headers}; | ||
1425 | 288 | |||
1426 | 289 | my $req = HTTP::Request->new($method => $link); | ||
1427 | 290 | if ( $content ) { | ||
1428 | 291 | $req->content($content); | ||
1429 | 292 | } | ||
1430 | 293 | if ( $headers ) { | ||
1431 | 294 | map { $req->header($_ => $headers->{$_}) } keys %$headers; | ||
1432 | 295 | } | ||
1433 | 296 | PTDEBUG && _d('Request', $method, $link, Dumper($req)); | ||
1434 | 297 | |||
1435 | 298 | my $response = $self->ua->request($req); | ||
1436 | 299 | PTDEBUG && _d('Response', Dumper($response)); | ||
1437 | 300 | |||
1438 | 301 | $self->response($response); | ||
1439 | 302 | |||
1440 | 303 | if ( !($response->code >= 200 && $response->code < 400) ) { | ||
1441 | 304 | die Percona::WebAPI::Exception::Request->new( | ||
1442 | 305 | method => $method, | ||
1443 | 306 | url => $link, | ||
1444 | 307 | content => $content, | ||
1445 | 308 | status => $response->code, | ||
1446 | 309 | error => "Failed to $method $link", | ||
1447 | 310 | ); | ||
1448 | 311 | } | ||
1449 | 312 | |||
1450 | 313 | return; | ||
1451 | 314 | } | ||
1452 | 315 | |||
1453 | 316 | no Lmo; | ||
1454 | 317 | 1; | ||
1455 | 318 | } | ||
1456 | 319 | # ########################################################################### | ||
1457 | 320 | # End Percona::WebAPI::Client package | ||
1458 | 321 | # ########################################################################### | ||
1459 | 322 | 0 | ||
1460 | === removed directory 'lib/Percona/WebAPI/Exception' | |||
1461 | === removed file 'lib/Percona/WebAPI/Exception/Request.pm' | |||
1462 | --- lib/Percona/WebAPI/Exception/Request.pm 2012-12-26 20:00:46 +0000 | |||
1463 | +++ lib/Percona/WebAPI/Exception/Request.pm 1970-01-01 00:00:00 +0000 | |||
1464 | @@ -1,69 +0,0 @@ | |||
1465 | 1 | # This program is copyright 2012-2013 Percona Inc. | ||
1466 | 2 | # Feedback and improvements are welcome. | ||
1467 | 3 | # | ||
1468 | 4 | # THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | ||
1469 | 5 | # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | ||
1470 | 6 | # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
1471 | 7 | # | ||
1472 | 8 | # This program is free software; you can redistribute it and/or modify it under | ||
1473 | 9 | # the terms of the GNU General Public License as published by the Free Software | ||
1474 | 10 | # Foundation, version 2; OR the Perl Artistic License. On UNIX and similar | ||
1475 | 11 | # systems, you can issue `man perlgpl' or `man perlartistic' to read these | ||
1476 | 12 | # licenses. | ||
1477 | 13 | # | ||
1478 | 14 | # You should have received a copy of the GNU General Public License along with | ||
1479 | 15 | # this program; if not, write to the Free Software Foundation, Inc., 59 Temple | ||
1480 | 16 | # Place, Suite 330, Boston, MA 02111-1307 USA. | ||
1481 | 17 | # ########################################################################### | ||
1482 | 18 | # Percona::WebAPI::Exception::Request package | ||
1483 | 19 | # ########################################################################### | ||
1484 | 20 | { | ||
1485 | 21 | package Percona::WebAPI::Exception::Request; | ||
1486 | 22 | |||
1487 | 23 | use Lmo; | ||
1488 | 24 | use overload '""' => \&as_string; | ||
1489 | 25 | |||
1490 | 26 | has 'method' => ( | ||
1491 | 27 | is => 'ro', | ||
1492 | 28 | isa => 'Str', | ||
1493 | 29 | required => 1, | ||
1494 | 30 | ); | ||
1495 | 31 | |||
1496 | 32 | has 'url' => ( | ||
1497 | 33 | is => 'ro', | ||
1498 | 34 | isa => 'Str', | ||
1499 | 35 | required => 1, | ||
1500 | 36 | ); | ||
1501 | 37 | |||
1502 | 38 | has 'content' => ( | ||
1503 | 39 | is => 'ro', | ||
1504 | 40 | isa => 'Maybe[Str]', | ||
1505 | 41 | required => 0, | ||
1506 | 42 | ); | ||
1507 | 43 | |||
1508 | 44 | has 'status' => ( | ||
1509 | 45 | is => 'ro', | ||
1510 | 46 | isa => 'Int', | ||
1511 | 47 | required => 1, | ||
1512 | 48 | ); | ||
1513 | 49 | |||
1514 | 50 | has 'error' => ( | ||
1515 | 51 | is => 'ro', | ||
1516 | 52 | isa => 'Str', | ||
1517 | 53 | required => 1, | ||
1518 | 54 | ); | ||
1519 | 55 | |||
1520 | 56 | sub as_string { | ||
1521 | 57 | my $self = shift; | ||
1522 | 58 | chomp(my $error = $self->error); | ||
1523 | 59 | $error =~ s/\n/ /g; | ||
1524 | 60 | return sprintf "%s\nRequest: %s %s %s\nStatus: %d\n", | ||
1525 | 61 | $error, $self->method, $self->url, $self->content || '', $self->status; | ||
1526 | 62 | } | ||
1527 | 63 | |||
1528 | 64 | no Lmo; | ||
1529 | 65 | 1; | ||
1530 | 66 | } | ||
1531 | 67 | # ########################################################################### | ||
1532 | 68 | # End Percona::WebAPI::Exception::Request package | ||
1533 | 69 | # ########################################################################### | ||
1534 | 70 | 0 | ||
1535 | === removed file 'lib/Percona/WebAPI/Exception/Resource.pm' | |||
1536 | --- lib/Percona/WebAPI/Exception/Resource.pm 2013-03-01 16:47:49 +0000 | |||
1537 | +++ lib/Percona/WebAPI/Exception/Resource.pm 1970-01-01 00:00:00 +0000 | |||
1538 | @@ -1,66 +0,0 @@ | |||
1539 | 1 | # This program is copyright 2012-2013 Percona Inc. | ||
1540 | 2 | # Feedback and improvements are welcome. | ||
1541 | 3 | # | ||
1542 | 4 | # THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | ||
1543 | 5 | # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | ||
1544 | 6 | # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
1545 | 7 | # | ||
1546 | 8 | # This program is free software; you can redistribute it and/or modify it under | ||
1547 | 9 | # the terms of the GNU General Public License as published by the Free Software | ||
1548 | 10 | # Foundation, version 2; OR the Perl Artistic License. On UNIX and similar | ||
1549 | 11 | # systems, you can issue `man perlgpl' or `man perlartistic' to read these | ||
1550 | 12 | # licenses. | ||
1551 | 13 | # | ||
1552 | 14 | # You should have received a copy of the GNU General Public License along with | ||
1553 | 15 | # this program; if not, write to the Free Software Foundation, Inc., 59 Temple | ||
1554 | 16 | # Place, Suite 330, Boston, MA 02111-1307 USA. | ||
1555 | 17 | # ########################################################################### | ||
1556 | 18 | # Percona::WebAPI::Exception::Resource package | ||
1557 | 19 | # ########################################################################### | ||
1558 | 20 | { | ||
1559 | 21 | package Percona::WebAPI::Exception::Resource; | ||
1560 | 22 | |||
1561 | 23 | use Lmo; | ||
1562 | 24 | use overload '""' => \&as_string; | ||
1563 | 25 | use Data::Dumper; | ||
1564 | 26 | |||
1565 | 27 | has 'type' => ( | ||
1566 | 28 | is => 'ro', | ||
1567 | 29 | isa => 'Str', | ||
1568 | 30 | required => 1, | ||
1569 | 31 | ); | ||
1570 | 32 | |||
1571 | 33 | has 'link' => ( | ||
1572 | 34 | is => 'ro', | ||
1573 | 35 | isa => 'Str', | ||
1574 | 36 | required => 1, | ||
1575 | 37 | ); | ||
1576 | 38 | |||
1577 | 39 | has 'data' => ( | ||
1578 | 40 | is => 'ro', | ||
1579 | 41 | isa => 'ArrayRef', | ||
1580 | 42 | required => 1, | ||
1581 | 43 | ); | ||
1582 | 44 | |||
1583 | 45 | has 'error' => ( | ||
1584 | 46 | is => 'ro', | ||
1585 | 47 | isa => 'Str', | ||
1586 | 48 | required => 1, | ||
1587 | 49 | ); | ||
1588 | 50 | |||
1589 | 51 | sub as_string { | ||
1590 | 52 | my $self = shift; | ||
1591 | 53 | chomp(my $error = $self->error); | ||
1592 | 54 | local $Data::Dumper::Indent = 1; | ||
1593 | 55 | local $Data::Dumper::Sortkeys = 1; | ||
1594 | 56 | local $Data::Dumper::Quotekeys = 0; | ||
1595 | 57 | return sprintf "Invalid %s resource from %s:\n\n%s\nError: %s\n\n", | ||
1596 | 58 | $self->type, $self->link, Dumper($self->data), $error; | ||
1597 | 59 | } | ||
1598 | 60 | |||
1599 | 61 | no Lmo; | ||
1600 | 62 | 1; | ||
1601 | 63 | } | ||
1602 | 64 | # ########################################################################### | ||
1603 | 65 | # End Percona::WebAPI::Exception::Resource package | ||
1604 | 66 | # ########################################################################### | ||
1605 | 67 | 0 | ||
1606 | === removed file 'lib/Percona/WebAPI/Representation.pm' | |||
1607 | --- lib/Percona/WebAPI/Representation.pm 2013-06-15 19:31:38 +0000 | |||
1608 | +++ lib/Percona/WebAPI/Representation.pm 1970-01-01 00:00:00 +0000 | |||
1609 | @@ -1,86 +0,0 @@ | |||
1610 | 1 | # This program is copyright 2012-2013 Percona Inc. | ||
1611 | 2 | # Feedback and improvements are welcome. | ||
1612 | 3 | # | ||
1613 | 4 | # THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | ||
1614 | 5 | # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | ||
1615 | 6 | # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
1616 | 7 | # | ||
1617 | 8 | # This program is free software; you can redistribute it and/or modify it under | ||
1618 | 9 | # the terms of the GNU General Public License as published by the Free Software | ||
1619 | 10 | # Foundation, version 2; OR the Perl Artistic License. On UNIX and similar | ||
1620 | 11 | # systems, you can issue `man perlgpl' or `man perlartistic' to read these | ||
1621 | 12 | # licenses. | ||
1622 | 13 | # | ||
1623 | 14 | # You should have received a copy of the GNU General Public License along with | ||
1624 | 15 | # this program; if not, write to the Free Software Foundation, Inc., 59 Temple | ||
1625 | 16 | # Place, Suite 330, Boston, MA 02111-1307 USA. | ||
1626 | 17 | # ########################################################################### | ||
1627 | 18 | # Percona::WebAPI::Representation package | ||
1628 | 19 | # ########################################################################### | ||
1629 | 20 | { | ||
1630 | 21 | package Percona::WebAPI::Representation; | ||
1631 | 22 | |||
1632 | 23 | eval { | ||
1633 | 24 | require JSON; | ||
1634 | 25 | }; | ||
1635 | 26 | |||
1636 | 27 | require Exporter; | ||
1637 | 28 | our @ISA = qw(Exporter); | ||
1638 | 29 | our @EXPORT_OK = qw( | ||
1639 | 30 | as_hashref | ||
1640 | 31 | as_json | ||
1641 | 32 | as_config | ||
1642 | 33 | ); | ||
1643 | 34 | |||
1644 | 35 | sub as_hashref { | ||
1645 | 36 | my ($resource, %args) = @_; | ||
1646 | 37 | |||
1647 | 38 | # Copy the object into a new hashref. | ||
1648 | 39 | my $as_hashref = { %$resource }; | ||
1649 | 40 | |||
1650 | 41 | # Delete the links because they're just for client-side use | ||
1651 | 42 | # and the caller should be sending this object, not getting it. | ||
1652 | 43 | # But sometimes for testing we want to keep the links. | ||
1653 | 44 | if ( !defined $args{with_links} || !$args{with_links} ) { | ||
1654 | 45 | delete $as_hashref->{links}; | ||
1655 | 46 | } | ||
1656 | 47 | |||
1657 | 48 | return $as_hashref; | ||
1658 | 49 | } | ||
1659 | 50 | |||
1660 | 51 | sub as_json { | ||
1661 | 52 | my ($resource, %args) = @_; | ||
1662 | 53 | |||
1663 | 54 | my $json = $args{json} || JSON->new; | ||
1664 | 55 | $json->allow_blessed([]); | ||
1665 | 56 | $json->convert_blessed([]); | ||
1666 | 57 | |||
1667 | 58 | my $text = $json->encode( | ||
1668 | 59 | ref $resource eq 'ARRAY' ? $resource : as_hashref($resource, %args) | ||
1669 | 60 | ); | ||
1670 | 61 | if ( $args{json} && $text ) { # for testing | ||
1671 | 62 | chomp($text); | ||
1672 | 63 | $text .= "\n"; | ||
1673 | 64 | } | ||
1674 | 65 | return $text; | ||
1675 | 66 | } | ||
1676 | 67 | |||
1677 | 68 | sub as_config { | ||
1678 | 69 | my $resource = shift; | ||
1679 | 70 | if ( !$resource->isa('Percona::WebAPI::Resource::Config') ) { | ||
1680 | 71 | die "Only Config resources can be represented as config.\n"; | ||
1681 | 72 | } | ||
1682 | 73 | my $as_hashref = as_hashref($resource); | ||
1683 | 74 | my $options = $as_hashref->{options}; | ||
1684 | 75 | my $config = join("\n", | ||
1685 | 76 | map { defined $options->{$_} ? "$_=$options->{$_}" : "$_" } | ||
1686 | 77 | sort keys %$options | ||
1687 | 78 | ) . "\n"; | ||
1688 | 79 | return $config; | ||
1689 | 80 | } | ||
1690 | 81 | |||
1691 | 82 | 1; | ||
1692 | 83 | } | ||
1693 | 84 | # ########################################################################### | ||
1694 | 85 | # End Percona::WebAPI::Representation package | ||
1695 | 86 | # ########################################################################### | ||
1696 | 87 | 0 | ||
1697 | === removed directory 'lib/Percona/WebAPI/Resource' | |||
1698 | === removed file 'lib/Percona/WebAPI/Resource/Agent.pm' | |||
1699 | --- lib/Percona/WebAPI/Resource/Agent.pm 2013-06-15 02:59:14 +0000 | |||
1700 | +++ lib/Percona/WebAPI/Resource/Agent.pm 1970-01-01 00:00:00 +0000 | |||
1701 | @@ -1,77 +0,0 @@ | |||
1702 | 1 | # This program is copyright 2012-2013 Percona Inc. | ||
1703 | 2 | # Feedback and improvements are welcome. | ||
1704 | 3 | # | ||
1705 | 4 | # THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | ||
1706 | 5 | # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | ||
1707 | 6 | # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
1708 | 7 | # | ||
1709 | 8 | # This program is free software; you can redistribute it and/or modify it under | ||
1710 | 9 | # the terms of the GNU General Public License as published by the Free Software | ||
1711 | 10 | # Foundation, version 2; OR the Perl Artistic License. On UNIX and similar | ||
1712 | 11 | # systems, you can issue `man perlgpl' or `man perlartistic' to read these | ||
1713 | 12 | # licenses. | ||
1714 | 13 | # | ||
1715 | 14 | # You should have received a copy of the GNU General Public License along with | ||
1716 | 15 | # this program; if not, write to the Free Software Foundation, Inc., 59 Temple | ||
1717 | 16 | # Place, Suite 330, Boston, MA 02111-1307 USA. | ||
1718 | 17 | # ########################################################################### | ||
1719 | 18 | # Percona::WebAPI::Resource::Agent package | ||
1720 | 19 | # ########################################################################### | ||
1721 | 20 | { | ||
1722 | 21 | package Percona::WebAPI::Resource::Agent; | ||
1723 | 22 | |||
1724 | 23 | use Lmo; | ||
1725 | 24 | |||
1726 | 25 | has 'uuid' => ( | ||
1727 | 26 | is => 'ro', | ||
1728 | 27 | isa => 'Str', | ||
1729 | 28 | required => 0, | ||
1730 | 29 | ); | ||
1731 | 30 | |||
1732 | 31 | has 'username' => ( | ||
1733 | 32 | is => 'rw', | ||
1734 | 33 | isa => 'Str', | ||
1735 | 34 | required => 0, | ||
1736 | 35 | default => sub { return $ENV{USER} || $ENV{LOGNAME} }, | ||
1737 | 36 | ); | ||
1738 | 37 | |||
1739 | 38 | has 'hostname' => ( | ||
1740 | 39 | is => 'rw', | ||
1741 | 40 | isa => 'Str', | ||
1742 | 41 | required => 0, | ||
1743 | 42 | default => sub { | ||
1744 | 43 | chomp(my $hostname = `hostname`); | ||
1745 | 44 | return $hostname; | ||
1746 | 45 | }, | ||
1747 | 46 | ); | ||
1748 | 47 | |||
1749 | 48 | has 'alias' => ( | ||
1750 | 49 | is => 'rw', | ||
1751 | 50 | isa => 'Str', | ||
1752 | 51 | required => 0, | ||
1753 | 52 | ); | ||
1754 | 53 | |||
1755 | 54 | has 'versions' => ( | ||
1756 | 55 | is => 'rw', | ||
1757 | 56 | isa => 'Maybe[HashRef]', | ||
1758 | 57 | required => 0, | ||
1759 | 58 | ); | ||
1760 | 59 | |||
1761 | 60 | has 'links' => ( | ||
1762 | 61 | is => 'rw', | ||
1763 | 62 | isa => 'Maybe[HashRef]', | ||
1764 | 63 | required => 0, | ||
1765 | 64 | default => sub { return {} }, | ||
1766 | 65 | ); | ||
1767 | 66 | |||
1768 | 67 | sub name { | ||
1769 | 68 | my ($self) = @_; | ||
1770 | 69 | return $self->alias || $self->hostname || $self->uuid || 'Unknown'; | ||
1771 | 70 | } | ||
1772 | 71 | |||
1773 | 72 | no Lmo; | ||
1774 | 73 | 1; | ||
1775 | 74 | } | ||
1776 | 75 | # ########################################################################### | ||
1777 | 76 | # End Percona::WebAPI::Resource::Agent package | ||
1778 | 77 | # ########################################################################### | ||
1779 | 78 | 0 | ||
1780 | === removed file 'lib/Percona/WebAPI/Resource/Config.pm' | |||
1781 | --- lib/Percona/WebAPI/Resource/Config.pm 2013-03-01 16:47:49 +0000 | |||
1782 | +++ lib/Percona/WebAPI/Resource/Config.pm 1970-01-01 00:00:00 +0000 | |||
1783 | @@ -1,55 +0,0 @@ | |||
1784 | 1 | # This program is copyright 2012-2013 Percona Inc. | ||
1785 | 2 | # Feedback and improvements are welcome. | ||
1786 | 3 | # | ||
1787 | 4 | # THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | ||
1788 | 5 | # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | ||
1789 | 6 | # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
1790 | 7 | # | ||
1791 | 8 | # This program is free software; you can redistribute it and/or modify it under | ||
1792 | 9 | # the terms of the GNU General Public License as published by the Free Software | ||
1793 | 10 | # Foundation, version 2; OR the Perl Artistic License. On UNIX and similar | ||
1794 | 11 | # systems, you can issue `man perlgpl' or `man perlartistic' to read these | ||
1795 | 12 | # licenses. | ||
1796 | 13 | # | ||
1797 | 14 | # You should have received a copy of the GNU General Public License along with | ||
1798 | 15 | # this program; if not, write to the Free Software Foundation, Inc., 59 Temple | ||
1799 | 16 | # Place, Suite 330, Boston, MA 02111-1307 USA. | ||
1800 | 17 | # ########################################################################### | ||
1801 | 18 | # Percona::WebAPI::Resource::Config package | ||
1802 | 19 | # ########################################################################### | ||
1803 | 20 | { | ||
1804 | 21 | package Percona::WebAPI::Resource::Config; | ||
1805 | 22 | |||
1806 | 23 | use Lmo; | ||
1807 | 24 | |||
1808 | 25 | has 'ts' => ( | ||
1809 | 26 | is => 'ro', | ||
1810 | 27 | isa => 'Int', | ||
1811 | 28 | required => 1, | ||
1812 | 29 | ); | ||
1813 | 30 | |||
1814 | 31 | has 'name' => ( | ||
1815 | 32 | is => 'ro', | ||
1816 | 33 | isa => 'Str', | ||
1817 | 34 | required => 1, | ||
1818 | 35 | ); | ||
1819 | 36 | |||
1820 | 37 | has 'options' => ( | ||
1821 | 38 | is => 'ro', | ||
1822 | 39 | isa => 'HashRef', | ||
1823 | 40 | required => 1, | ||
1824 | 41 | ); | ||
1825 | 42 | |||
1826 | 43 | has 'links' => ( | ||
1827 | 44 | is => 'rw', | ||
1828 | 45 | isa => 'Maybe[HashRef]', | ||
1829 | 46 | required => 0, | ||
1830 | 47 | default => sub { return {} }, | ||
1831 | 48 | ); | ||
1832 | 49 | |||
1833 | 50 | no Lmo; | ||
1834 | 51 | 1; | ||
1835 | 52 | } | ||
1836 | 53 | # ########################################################################### | ||
1837 | 54 | # End Percona::WebAPI::Resource::Config package | ||
1838 | 55 | # ########################################################################### | ||
1839 | 56 | 0 | ||
1840 | === removed file 'lib/Percona/WebAPI/Resource/LogEntry.pm' | |||
1841 | --- lib/Percona/WebAPI/Resource/LogEntry.pm 2013-06-10 00:15:16 +0000 | |||
1842 | +++ lib/Percona/WebAPI/Resource/LogEntry.pm 1970-01-01 00:00:00 +0000 | |||
1843 | @@ -1,66 +0,0 @@ | |||
1844 | 1 | # This program is copyright 2013 Percona Inc. | ||
1845 | 2 | # Feedback and improvements are welcome. | ||
1846 | 3 | # | ||
1847 | 4 | # THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | ||
1848 | 5 | # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | ||
1849 | 6 | # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
1850 | 7 | # | ||
1851 | 8 | # This program is free software; you can redistribute it and/or modify it under | ||
1852 | 9 | # the terms of the GNU General Public License as published by the Free Software | ||
1853 | 10 | # Foundation, version 2; OR the Perl Artistic License. On UNIX and similar | ||
1854 | 11 | # systems, you can issue `man perlgpl' or `man perlartistic' to read these | ||
1855 | 12 | # licenses. | ||
1856 | 13 | # | ||
1857 | 14 | # You should have received a copy of the GNU General Public License along with | ||
1858 | 15 | # this program; if not, write to the Free Software Foundation, Inc., 59 Temple | ||
1859 | 16 | # Place, Suite 330, Boston, MA 02111-1307 USA. | ||
1860 | 17 | # ########################################################################### | ||
1861 | 18 | # Percona::WebAPI::Resource::LogEntry package | ||
1862 | 19 | # ########################################################################### | ||
1863 | 20 | { | ||
1864 | 21 | package Percona::WebAPI::Resource::LogEntry; | ||
1865 | 22 | |||
1866 | 23 | use Lmo; | ||
1867 | 24 | |||
1868 | 25 | has 'pid' => ( | ||
1869 | 26 | is => 'ro', | ||
1870 | 27 | isa => 'Int', | ||
1871 | 28 | required => 1, | ||
1872 | 29 | ); | ||
1873 | 30 | |||
1874 | 31 | has 'service' => ( | ||
1875 | 32 | is => 'ro', | ||
1876 | 33 | isa => 'Str', | ||
1877 | 34 | required => 0, | ||
1878 | 35 | ); | ||
1879 | 36 | |||
1880 | 37 | has 'data_ts' => ( | ||
1881 | 38 | is => 'ro', | ||
1882 | 39 | isa => 'Int', | ||
1883 | 40 | required => 0, | ||
1884 | 41 | ); | ||
1885 | 42 | |||
1886 | 43 | has 'entry_ts' => ( | ||
1887 | 44 | is => 'ro', | ||
1888 | 45 | isa => 'Str', | ||
1889 | 46 | required => 1, | ||
1890 | 47 | ); | ||
1891 | 48 | |||
1892 | 49 | has 'log_level' => ( | ||
1893 | 50 | is => 'ro', | ||
1894 | 51 | isa => 'Int', | ||
1895 | 52 | required => 1, | ||
1896 | 53 | ); | ||
1897 | 54 | |||
1898 | 55 | has 'message' => ( | ||
1899 | 56 | is => 'ro', | ||
1900 | 57 | isa => 'Str', | ||
1901 | 58 | required => 1, | ||
1902 | 59 | ); | ||
1903 | 60 | |||
1904 | 61 | no Lmo; | ||
1905 | 62 | 1; | ||
1906 | 63 | } | ||
1907 | 64 | # ########################################################################### | ||
1908 | 65 | # End Percona::WebAPI::Resource::LogEntry package | ||
1909 | 66 | # ########################################################################### | ||
1910 | 67 | 0 | ||
1911 | === removed file 'lib/Percona/WebAPI/Resource/Service.pm' | |||
1912 | --- lib/Percona/WebAPI/Resource/Service.pm 2013-04-19 20:49:01 +0000 | |||
1913 | +++ lib/Percona/WebAPI/Resource/Service.pm 1970-01-01 00:00:00 +0000 | |||
1914 | @@ -1,94 +0,0 @@ | |||
1915 | 1 | # This program is copyright 2012-2013 Percona Inc. | ||
1916 | 2 | # Feedback and improvements are welcome. | ||
1917 | 3 | # | ||
1918 | 4 | # THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | ||
1919 | 5 | # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | ||
1920 | 6 | # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
1921 | 7 | # | ||
1922 | 8 | # This program is free software; you can redistribute it and/or modify it under | ||
1923 | 9 | # the terms of the GNU General Public License as published by the Free Software | ||
1924 | 10 | # Foundation, version 2; OR the Perl Artistic License. On UNIX and similar | ||
1925 | 11 | # systems, you can issue `man perlgpl' or `man perlartistic' to read these | ||
1926 | 12 | # licenses. | ||
1927 | 13 | # | ||
1928 | 14 | # You should have received a copy of the GNU General Public License along with | ||
1929 | 15 | # this program; if not, write to the Free Software Foundation, Inc., 59 Temple | ||
1930 | 16 | # Place, Suite 330, Boston, MA 02111-1307 USA. | ||
1931 | 17 | # ########################################################################### | ||
1932 | 18 | # Percona::WebAPI::Resource::Service package | ||
1933 | 19 | # ########################################################################### | ||
1934 | 20 | { | ||
1935 | 21 | package Percona::WebAPI::Resource::Service; | ||
1936 | 22 | |||
1937 | 23 | use Lmo; | ||
1938 | 24 | |||
1939 | 25 | has 'ts' => ( | ||
1940 | 26 | is => 'ro', | ||
1941 | 27 | isa => 'Int', | ||
1942 | 28 | required => 1, | ||
1943 | 29 | ); | ||
1944 | 30 | |||
1945 | 31 | has 'name' => ( | ||
1946 | 32 | is => 'ro', | ||
1947 | 33 | isa => 'Str', | ||
1948 | 34 | required => 1, | ||
1949 | 35 | ); | ||
1950 | 36 | |||
1951 | 37 | has 'tasks' => ( | ||
1952 | 38 | is => 'ro', | ||
1953 | 39 | isa => 'ArrayRef[Percona::WebAPI::Resource::Task]', | ||
1954 | 40 | required => 1, | ||
1955 | 41 | ); | ||
1956 | 42 | |||
1957 | 43 | has 'run_schedule' => ( | ||
1958 | 44 | is => 'ro', | ||
1959 | 45 | isa => 'Str', | ||
1960 | 46 | required => 0, | ||
1961 | 47 | ); | ||
1962 | 48 | |||
1963 | 49 | has 'spool_schedule' => ( | ||
1964 | 50 | is => 'ro', | ||
1965 | 51 | isa => 'Str', | ||
1966 | 52 | required => 0, | ||
1967 | 53 | ); | ||
1968 | 54 | |||
1969 | 55 | has 'meta' => ( | ||
1970 | 56 | is => 'ro', | ||
1971 | 57 | isa => 'Bool', | ||
1972 | 58 | required => 0, | ||
1973 | 59 | default => sub { return 0 }, | ||
1974 | 60 | ); | ||
1975 | 61 | |||
1976 | 62 | has 'run_once' => ( | ||
1977 | 63 | is => 'ro', | ||
1978 | 64 | isa => 'Bool', | ||
1979 | 65 | required => 0, | ||
1980 | 66 | default => sub { return 0 }, | ||
1981 | 67 | ); | ||
1982 | 68 | |||
1983 | 69 | has 'links' => ( | ||
1984 | 70 | is => 'rw', | ||
1985 | 71 | isa => 'Maybe[HashRef]', | ||
1986 | 72 | required => 0, | ||
1987 | 73 | default => sub { return {} }, | ||
1988 | 74 | ); | ||
1989 | 75 | |||
1990 | 76 | sub BUILDARGS { | ||
1991 | 77 | my ($class, %args) = @_; | ||
1992 | 78 | if ( ref $args{tasks} eq 'ARRAY' ) { | ||
1993 | 79 | my @tasks; | ||
1994 | 80 | foreach my $run_hashref ( @{$args{tasks}} ) { | ||
1995 | 81 | my $task = Percona::WebAPI::Resource::Task->new(%$run_hashref); | ||
1996 | 82 | push @tasks, $task; | ||
1997 | 83 | } | ||
1998 | 84 | $args{tasks} = \@tasks; | ||
1999 | 85 | } | ||
2000 | 86 | return $class->SUPER::BUILDARGS(%args); | ||
2001 | 87 | } | ||
2002 | 88 | |||
2003 | 89 | no Lmo; | ||
2004 | 90 | 1; | ||
2005 | 91 | } | ||
2006 | 92 | # ########################################################################### | ||
2007 | 93 | # End Percona::WebAPI::Resource::Service package | ||
2008 | 94 | # ########################################################################### | ||
2009 | 95 | 0 | ||
2010 | === removed file 'lib/Percona/WebAPI/Resource/Task.pm' | |||
2011 | --- lib/Percona/WebAPI/Resource/Task.pm 2013-04-19 20:49:01 +0000 | |||
2012 | +++ lib/Percona/WebAPI/Resource/Task.pm 1970-01-01 00:00:00 +0000 | |||
2013 | @@ -1,62 +0,0 @@ | |||
2014 | 1 | # This program is copyright 2012-2013 Percona Inc. | ||
2015 | 2 | # Feedback and improvements are welcome. | ||
2016 | 3 | # | ||
2017 | 4 | # THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | ||
2018 | 5 | # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | ||
2019 | 6 | # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
2020 | 7 | # | ||
2021 | 8 | # This program is free software; you can redistribute it and/or modify it under | ||
2022 | 9 | # the terms of the GNU General Public License as published by the Free Software | ||
2023 | 10 | # Foundation, version 2; OR the Perl Artistic License. On UNIX and similar | ||
2024 | 11 | # systems, you can issue `man perlgpl' or `man perlartistic' to read these | ||
2025 | 12 | # licenses. | ||
2026 | 13 | # | ||
2027 | 14 | # You should have received a copy of the GNU General Public License along with | ||
2028 | 15 | # this program; if not, write to the Free Software Foundation, Inc., 59 Temple | ||
2029 | 16 | # Place, Suite 330, Boston, MA 02111-1307 USA. | ||
2030 | 17 | # ########################################################################### | ||
2031 | 18 | # Percona::WebAPI::Resource::Task package | ||
2032 | 19 | # ########################################################################### | ||
2033 | 20 | { | ||
2034 | 21 | package Percona::WebAPI::Resource::Task; | ||
2035 | 22 | |||
2036 | 23 | use Lmo; | ||
2037 | 24 | |||
2038 | 25 | has 'name' => ( | ||
2039 | 26 | is => 'ro', | ||
2040 | 27 | isa => 'Str', | ||
2041 | 28 | required => 1, | ||
2042 | 29 | ); | ||
2043 | 30 | |||
2044 | 31 | has 'number' => ( | ||
2045 | 32 | is => 'ro', | ||
2046 | 33 | isa => 'Int', | ||
2047 | 34 | required => 1, | ||
2048 | 35 | ); | ||
2049 | 36 | |||
2050 | 37 | has 'program' => ( | ||
2051 | 38 | is => 'ro', | ||
2052 | 39 | isa => 'Maybe[Str]', | ||
2053 | 40 | required => 0, | ||
2054 | 41 | ); | ||
2055 | 42 | |||
2056 | 43 | has 'query' => ( | ||
2057 | 44 | is => 'ro', | ||
2058 | 45 | isa => 'Maybe[Str]', | ||
2059 | 46 | required => 0, | ||
2060 | 47 | ); | ||
2061 | 48 | |||
2062 | 49 | has 'output' => ( | ||
2063 | 50 | is => 'ro', | ||
2064 | 51 | isa => 'Maybe[Str]', | ||
2065 | 52 | required => 0, | ||
2066 | 53 | ); | ||
2067 | 54 | |||
2068 | 55 | sub TO_JSON { return { %{ shift() } }; } | ||
2069 | 56 | |||
2070 | 57 | no Lmo; | ||
2071 | 58 | 1; | ||
2072 | 59 | } | ||
2073 | 60 | # ########################################################################### | ||
2074 | 61 | # End Percona::WebAPI::Resource::Task package | ||
2075 | 62 | # ########################################################################### | ||
2076 | 63 | 0 | ||
2077 | === removed directory 't/lib/Percona/WebAPI' | |||
2078 | === removed file 't/lib/Percona/WebAPI/Client.t' | |||
2079 | --- t/lib/Percona/WebAPI/Client.t 2013-08-08 19:34:29 +0000 | |||
2080 | +++ t/lib/Percona/WebAPI/Client.t 1970-01-01 00:00:00 +0000 | |||
2081 | @@ -1,236 +0,0 @@ | |||
2082 | 1 | #!/usr/bin/env perl | ||
2083 | 2 | |||
2084 | 3 | BEGIN { | ||
2085 | 4 | die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n" | ||
2086 | 5 | unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH}; | ||
2087 | 6 | unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib"; | ||
2088 | 7 | }; | ||
2089 | 8 | |||
2090 | 9 | use strict; | ||
2091 | 10 | use warnings FATAL => 'all'; | ||
2092 | 11 | use English qw(-no_match_vars); | ||
2093 | 12 | use Test::More; | ||
2094 | 13 | use JSON; | ||
2095 | 14 | use File::Temp qw(tempdir); | ||
2096 | 15 | |||
2097 | 16 | use Percona::Test; | ||
2098 | 17 | use Percona::Test::Mock::UserAgent; | ||
2099 | 18 | use Percona::WebAPI::Client; | ||
2100 | 19 | use Percona::WebAPI::Resource::Agent; | ||
2101 | 20 | use Percona::WebAPI::Resource::Config; | ||
2102 | 21 | use Percona::WebAPI::Resource::Service; | ||
2103 | 22 | use Percona::WebAPI::Resource::Task; | ||
2104 | 23 | |||
2105 | 24 | Percona::Toolkit->import(qw(Dumper have_required_args)); | ||
2106 | 25 | Percona::WebAPI::Representation->import(qw(as_json as_hashref)); | ||
2107 | 26 | |||
2108 | 27 | # ############################################################################# | ||
2109 | 28 | # Create a client with a mock user-agent. | ||
2110 | 29 | # ############################################################################# | ||
2111 | 30 | |||
2112 | 31 | my $json = JSON->new; | ||
2113 | 32 | $json->allow_blessed([]); | ||
2114 | 33 | $json->convert_blessed([]); | ||
2115 | 34 | |||
2116 | 35 | my $ua = Percona::Test::Mock::UserAgent->new( | ||
2117 | 36 | encode => sub { my $c = shift; return $json->encode($c || {}) }, | ||
2118 | 37 | ); | ||
2119 | 38 | |||
2120 | 39 | my $client = eval { | ||
2121 | 40 | Percona::WebAPI::Client->new( | ||
2122 | 41 | api_key => '123', | ||
2123 | 42 | ua => $ua, | ||
2124 | 43 | ); | ||
2125 | 44 | }; | ||
2126 | 45 | |||
2127 | 46 | is( | ||
2128 | 47 | $EVAL_ERROR, | ||
2129 | 48 | '', | ||
2130 | 49 | 'Create client' | ||
2131 | 50 | ) or die; | ||
2132 | 51 | |||
2133 | 52 | # ############################################################################# | ||
2134 | 53 | # First thing a client should do is get the entry links. | ||
2135 | 54 | # ############################################################################# | ||
2136 | 55 | |||
2137 | 56 | my $return_links = { # what the server returns | ||
2138 | 57 | agents => '/agents', | ||
2139 | 58 | }; | ||
2140 | 59 | |||
2141 | 60 | $ua->{responses}->{get} = [ | ||
2142 | 61 | { | ||
2143 | 62 | content => { | ||
2144 | 63 | links => $return_links, | ||
2145 | 64 | } | ||
2146 | 65 | }, | ||
2147 | 66 | ]; | ||
2148 | 67 | |||
2149 | 68 | my $links = $client->get(link => $client->entry_link); | ||
2150 | 69 | |||
2151 | 70 | is_deeply( | ||
2152 | 71 | $links, | ||
2153 | 72 | $return_links, | ||
2154 | 73 | "Get entry links" | ||
2155 | 74 | ) or diag(Dumper($links)); | ||
2156 | 75 | |||
2157 | 76 | is_deeply( | ||
2158 | 77 | $ua->{requests}, | ||
2159 | 78 | [ | ||
2160 | 79 | 'GET https://cloud-api.percona.com', | ||
2161 | 80 | ], | ||
2162 | 81 | "1 request, 1 GET" | ||
2163 | 82 | ) or diag(Dumper($ua->{requests})); | ||
2164 | 83 | |||
2165 | 84 | |||
2166 | 85 | # ############################################################################# | ||
2167 | 86 | # Second, a new client will POST an Agent for itself. The entry links | ||
2168 | 87 | # should have an "agents" link. The server response is empty but the | ||
2169 | 88 | # URI for the new Agent resource is given by the Location header. | ||
2170 | 89 | # ############################################################################# | ||
2171 | 90 | |||
2172 | 91 | my $agent = Percona::WebAPI::Resource::Agent->new( | ||
2173 | 92 | id => '123', | ||
2174 | 93 | hostname => 'host', | ||
2175 | 94 | ); | ||
2176 | 95 | |||
2177 | 96 | $ua->{responses}->{post} = [ | ||
2178 | 97 | { | ||
2179 | 98 | headers => { 'Location' => 'agents/5' }, | ||
2180 | 99 | content => '', | ||
2181 | 100 | }, | ||
2182 | 101 | ]; | ||
2183 | 102 | |||
2184 | 103 | my $uri = $client->post(resources => $agent, link => $links->{agents}); | ||
2185 | 104 | |||
2186 | 105 | is( | ||
2187 | 106 | $uri, | ||
2188 | 107 | "agents/5", | ||
2189 | 108 | "POST Agent, got Location URI" | ||
2190 | 109 | ); | ||
2191 | 110 | |||
2192 | 111 | # ############################################################################# | ||
2193 | 112 | # After successfully creating the new Agent, the client should fetch | ||
2194 | 113 | # the new Agent resoruce which will have links to the next step: the | ||
2195 | 114 | # agent's config. | ||
2196 | 115 | # ############################################################################# | ||
2197 | 116 | |||
2198 | 117 | $return_links = { | ||
2199 | 118 | self => 'agents/5', | ||
2200 | 119 | config => 'agents/5/config', | ||
2201 | 120 | }; | ||
2202 | 121 | |||
2203 | 122 | my $content = { | ||
2204 | 123 | %{ as_hashref($agent) }, | ||
2205 | 124 | links => $return_links, | ||
2206 | 125 | }; | ||
2207 | 126 | |||
2208 | 127 | $ua->{responses}->{get} = [ | ||
2209 | 128 | { | ||
2210 | 129 | headers => { 'X-Percona-Resource-Type' => 'Agent' }, | ||
2211 | 130 | content => $content, | ||
2212 | 131 | }, | ||
2213 | 132 | ]; | ||
2214 | 133 | |||
2215 | 134 | # Re-using $agent, i.e. updating it with the actual, newly created | ||
2216 | 135 | # Agent resource as returned by the server with links. | ||
2217 | 136 | $agent = $client->get(link => $uri); | ||
2218 | 137 | |||
2219 | 138 | # Need to use with_links=>1 here because by as_hashref() removes | ||
2220 | 139 | # links by default because it's usually used to encode and send | ||
2221 | 140 | # resources, and clients never send links; but here we're using | ||
2222 | 141 | # it for testing. | ||
2223 | 142 | is_deeply( | ||
2224 | 143 | as_hashref($agent, with_links => 1), | ||
2225 | 144 | $content, | ||
2226 | 145 | "GET Agent with links" | ||
2227 | 146 | ) or diag(Dumper(as_hashref($agent, with_links => 1))); | ||
2228 | 147 | |||
2229 | 148 | # ############################################################################# | ||
2230 | 149 | # Now the agent can get its Config. | ||
2231 | 150 | # ############################################################################# | ||
2232 | 151 | |||
2233 | 152 | $return_links = { | ||
2234 | 153 | self => 'agents/5/config', | ||
2235 | 154 | services => 'agents/5/services', | ||
2236 | 155 | }; | ||
2237 | 156 | |||
2238 | 157 | my $return_config = Percona::WebAPI::Resource::Config->new( | ||
2239 | 158 | ts => '100', | ||
2240 | 159 | name => 'Default', | ||
2241 | 160 | options => {}, | ||
2242 | 161 | links => $return_links, | ||
2243 | 162 | ); | ||
2244 | 163 | |||
2245 | 164 | $ua->{responses}->{get} = [ | ||
2246 | 165 | { | ||
2247 | 166 | headers => { 'X-Percona-Resource-Type' => 'Config' }, | ||
2248 | 167 | content => as_hashref($return_config, with_links => 1), | ||
2249 | 168 | }, | ||
2250 | 169 | ]; | ||
2251 | 170 | |||
2252 | 171 | my $config = $client->get(link => $agent->links->{config}); | ||
2253 | 172 | |||
2254 | 173 | is_deeply( | ||
2255 | 174 | as_hashref($config, with_links => 1), | ||
2256 | 175 | as_hashref($return_config, with_links => 1), | ||
2257 | 176 | "GET Config" | ||
2258 | 177 | ) or diag(Dumper(as_hashref($config, with_links => 1))); | ||
2259 | 178 | |||
2260 | 179 | # ############################################################################# | ||
2261 | 180 | # Once an agent is configured, i.e. successfully gets a Config resource, | ||
2262 | 181 | # its Config should have a services link which returns a list of Service | ||
2263 | 182 | # resources, each with their own links. | ||
2264 | 183 | # ############################################################################# | ||
2265 | 184 | |||
2266 | 185 | $return_links = { | ||
2267 | 186 | 'send_data' => '/query-monitor', | ||
2268 | 187 | }; | ||
2269 | 188 | |||
2270 | 189 | my $run0 = Percona::WebAPI::Resource::Task->new( | ||
2271 | 190 | name => 'run-pqd', | ||
2272 | 191 | number => '0', | ||
2273 | 192 | program => 'pt-query-digest', | ||
2274 | 193 | options => '--output json', | ||
2275 | 194 | output => 'spool', | ||
2276 | 195 | ); | ||
2277 | 196 | |||
2278 | 197 | my $svc0 = Percona::WebAPI::Resource::Service->new( | ||
2279 | 198 | ts => '123', | ||
2280 | 199 | name => 'query-monitor', | ||
2281 | 200 | run_schedule => '1 * * * *', | ||
2282 | 201 | spool_schedule => '2 * * * *', | ||
2283 | 202 | tasks => [ $run0 ], | ||
2284 | 203 | links => $return_links, | ||
2285 | 204 | ); | ||
2286 | 205 | |||
2287 | 206 | $ua->{responses}->{get} = [ | ||
2288 | 207 | { | ||
2289 | 208 | headers => { 'X-Percona-Resource-Type' => 'Service' }, | ||
2290 | 209 | content => [ as_hashref($svc0, with_links => 1) ], | ||
2291 | 210 | }, | ||
2292 | 211 | ]; | ||
2293 | 212 | |||
2294 | 213 | my $services = $client->get(link => $config->links->{services}); | ||
2295 | 214 | |||
2296 | 215 | is( | ||
2297 | 216 | scalar @$services, | ||
2298 | 217 | 1, | ||
2299 | 218 | "Got 1 service" | ||
2300 | 219 | ); | ||
2301 | 220 | |||
2302 | 221 | is_deeply( | ||
2303 | 222 | as_hashref($services->[0], with_links => 1), | ||
2304 | 223 | as_hashref($svc0, with_links => 1), | ||
2305 | 224 | "GET Services" | ||
2306 | 225 | ) or diag(Dumper(as_hashref($services, with_links => 1))); | ||
2307 | 226 | |||
2308 | 227 | is( | ||
2309 | 228 | $services->[0]->links->{send_data}, | ||
2310 | 229 | "/query-monitor", | ||
2311 | 230 | "send_data link for Service" | ||
2312 | 231 | ); | ||
2313 | 232 | |||
2314 | 233 | # ############################################################################# | ||
2315 | 234 | # Done. | ||
2316 | 235 | # ############################################################################# | ||
2317 | 236 | done_testing; | ||
2318 | 237 | 0 | ||
2319 | === removed file 't/lib/Percona/WebAPI/Representation.t' | |||
2320 | --- t/lib/Percona/WebAPI/Representation.t 2013-03-01 16:47:49 +0000 | |||
2321 | +++ t/lib/Percona/WebAPI/Representation.t 1970-01-01 00:00:00 +0000 | |||
2322 | @@ -1,51 +0,0 @@ | |||
2323 | 1 | #!/usr/bin/perl | ||
2324 | 2 | |||
2325 | 3 | BEGIN { | ||
2326 | 4 | die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n" | ||
2327 | 5 | unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH}; | ||
2328 | 6 | unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib"; | ||
2329 | 7 | }; | ||
2330 | 8 | |||
2331 | 9 | use strict; | ||
2332 | 10 | use warnings FATAL => 'all'; | ||
2333 | 11 | use English qw(-no_match_vars); | ||
2334 | 12 | use Test::More; | ||
2335 | 13 | |||
2336 | 14 | use PerconaTest; | ||
2337 | 15 | use Percona::Toolkit; | ||
2338 | 16 | use Percona::WebAPI::Resource::Agent; | ||
2339 | 17 | use Percona::WebAPI::Resource::Config; | ||
2340 | 18 | use Percona::WebAPI::Representation; | ||
2341 | 19 | |||
2342 | 20 | my $agent = Percona::WebAPI::Resource::Agent->new( | ||
2343 | 21 | id => '123', | ||
2344 | 22 | hostname => 'pt', | ||
2345 | 23 | versions => { | ||
2346 | 24 | Perl => '5.10.1', | ||
2347 | 25 | }, | ||
2348 | 26 | ); | ||
2349 | 27 | |||
2350 | 28 | is( | ||
2351 | 29 | Percona::WebAPI::Representation::as_json($agent), | ||
2352 | 30 | q/{"versions":{"Perl":"5.10.1"},"id":"123","hostname":"pt"}/, | ||
2353 | 31 | "as_json" | ||
2354 | 32 | ); | ||
2355 | 33 | |||
2356 | 34 | my $config = Percona::WebAPI::Resource::Config->new( | ||
2357 | 35 | ts => '100', | ||
2358 | 36 | name => 'Default', | ||
2359 | 37 | options => { | ||
2360 | 38 | 'check-interval' => 60, | ||
2361 | 39 | }, | ||
2362 | 40 | ); | ||
2363 | 41 | |||
2364 | 42 | is( | ||
2365 | 43 | Percona::WebAPI::Representation::as_config($config), | ||
2366 | 44 | "check-interval=60\n", | ||
2367 | 45 | "as_config" | ||
2368 | 46 | ); | ||
2369 | 47 | |||
2370 | 48 | # ############################################################################# | ||
2371 | 49 | # Done. | ||
2372 | 50 | # ############################################################################# | ||
2373 | 51 | done_testing; | ||
2374 | 52 | 0 | ||
2375 | === removed directory 't/pt-agent' | |||
2376 | === removed file 't/pt-agent/basics.t' | |||
2377 | --- t/pt-agent/basics.t 2013-04-13 17:27:45 +0000 | |||
2378 | +++ t/pt-agent/basics.t 1970-01-01 00:00:00 +0000 | |||
2379 | @@ -1,101 +0,0 @@ | |||
2380 | 1 | #!/usr/bin/env perl | ||
2381 | 2 | |||
2382 | 3 | BEGIN { | ||
2383 | 4 | die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n" | ||
2384 | 5 | unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH}; | ||
2385 | 6 | unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib"; | ||
2386 | 7 | }; | ||
2387 | 8 | |||
2388 | 9 | use strict; | ||
2389 | 10 | use warnings FATAL => 'all'; | ||
2390 | 11 | use English qw(-no_match_vars); | ||
2391 | 12 | use Test::More; | ||
2392 | 13 | |||
2393 | 14 | use File::Temp qw(tempdir); | ||
2394 | 15 | |||
2395 | 16 | use Percona::Test; | ||
2396 | 17 | use Sandbox; | ||
2397 | 18 | use Percona::Test::Mock::UserAgent; | ||
2398 | 19 | require "$trunk/bin/pt-agent"; | ||
2399 | 20 | |||
2400 | 21 | my $dp = new DSNParser(opts=>$dsn_opts); | ||
2401 | 22 | my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp); | ||
2402 | 23 | my $dbh = $sb->get_dbh_for('master'); | ||
2403 | 24 | my $dsn = $sb->dsn_for('master'); | ||
2404 | 25 | my $o = new OptionParser(); | ||
2405 | 26 | $o->get_specs("$trunk/bin/pt-agent"); | ||
2406 | 27 | $o->get_opts(); | ||
2407 | 28 | my $cxn = Cxn->new( | ||
2408 | 29 | dsn_string => $dsn, | ||
2409 | 30 | OptionParser => $o, | ||
2410 | 31 | DSNParser => $dp, | ||
2411 | 32 | ); | ||
2412 | 33 | |||
2413 | 34 | Percona::Toolkit->import(qw(Dumper)); | ||
2414 | 35 | Percona::WebAPI::Representation->import(qw(as_hashref)); | ||
2415 | 36 | |||
2416 | 37 | # Running the agent is going to cause it to schedule the services, | ||
2417 | 38 | # i.e. write a real crontab. The test box/user shouldn't have a | ||
2418 | 39 | # crontab, so we'll warn and clobber it if there is one. | ||
2419 | 40 | my $crontab = `crontab -l 2>/dev/null`; | ||
2420 | 41 | if ( $crontab ) { | ||
2421 | 42 | warn "Removing crontab: $crontab\n"; | ||
2422 | 43 | `crontab -r`; | ||
2423 | 44 | } | ||
2424 | 45 | |||
2425 | 46 | my $tmp_lib = "/tmp/pt-agent"; | ||
2426 | 47 | my $tmp_log = "/tmp/pt-agent.log"; | ||
2427 | 48 | my $tmp_pid = "/tmp/pt-agent.pid"; | ||
2428 | 49 | |||
2429 | 50 | diag(`rm -rf $tmp_lib`) if -d $tmp_lib; | ||
2430 | 51 | unlink $tmp_log if -f $tmp_log; | ||
2431 | 52 | unlink $tmp_pid if -f $tmp_pid; | ||
2432 | 53 | |||
2433 | 54 | my $config_file = pt_agent::get_config_file(); | ||
2434 | 55 | unlink $config_file if -f $config_file; | ||
2435 | 56 | |||
2436 | 57 | my $output; | ||
2437 | 58 | |||
2438 | 59 | { | ||
2439 | 60 | no strict; | ||
2440 | 61 | no warnings; | ||
2441 | 62 | local *pt_agent::start_agent = sub { | ||
2442 | 63 | print "start_agent\n"; | ||
2443 | 64 | return { | ||
2444 | 65 | agent => 0, | ||
2445 | 66 | client => 0, | ||
2446 | 67 | daemon => 0, | ||
2447 | 68 | }; | ||
2448 | 69 | }; | ||
2449 | 70 | local *pt_agent::run_agent = sub { | ||
2450 | 71 | print "run_agent\n"; | ||
2451 | 72 | }; | ||
2452 | 73 | |||
2453 | 74 | $output = output( | ||
2454 | 75 | sub { | ||
2455 | 76 | pt_agent::main( | ||
2456 | 77 | qw(--api-key 123) | ||
2457 | 78 | ); | ||
2458 | 79 | }, | ||
2459 | 80 | stderr => 1, | ||
2460 | 81 | ); | ||
2461 | 82 | } | ||
2462 | 83 | |||
2463 | 84 | like( | ||
2464 | 85 | $output, | ||
2465 | 86 | qr/start_agent\nrun_agent\n/, | ||
2466 | 87 | "Starts and runs without a config file" | ||
2467 | 88 | ); | ||
2468 | 89 | |||
2469 | 90 | # ############################################################################# | ||
2470 | 91 | # Done. | ||
2471 | 92 | # ############################################################################# | ||
2472 | 93 | |||
2473 | 94 | `crontab -r 2>/dev/null`; | ||
2474 | 95 | |||
2475 | 96 | if ( -f $config_file ) { | ||
2476 | 97 | unlink $config_file | ||
2477 | 98 | or warn "Error removing $config_file: $OS_ERROR"; | ||
2478 | 99 | } | ||
2479 | 100 | |||
2480 | 101 | done_testing; | ||
2481 | 102 | 0 | ||
2482 | === removed file 't/pt-agent/get_services.t' | |||
2483 | --- t/pt-agent/get_services.t 2013-06-17 04:01:30 +0000 | |||
2484 | +++ t/pt-agent/get_services.t 1970-01-01 00:00:00 +0000 | |||
2485 | @@ -1,423 +0,0 @@ | |||
2486 | 1 | #!/usr/bin/env perl | ||
2487 | 2 | |||
2488 | 3 | BEGIN { | ||
2489 | 4 | die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n" | ||
2490 | 5 | unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH}; | ||
2491 | 6 | unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib"; | ||
2492 | 7 | }; | ||
2493 | 8 | |||
2494 | 9 | use strict; | ||
2495 | 10 | use warnings FATAL => 'all'; | ||
2496 | 11 | use English qw(-no_match_vars); | ||
2497 | 12 | use Test::More; | ||
2498 | 13 | |||
2499 | 14 | use JSON; | ||
2500 | 15 | use File::Temp qw(tempdir); | ||
2501 | 16 | |||
2502 | 17 | use Percona::Test; | ||
2503 | 18 | use Percona::Test::Mock::UserAgent; | ||
2504 | 19 | use Percona::Test::Mock::AgentLogger; | ||
2505 | 20 | require "$trunk/bin/pt-agent"; | ||
2506 | 21 | |||
2507 | 22 | Percona::Toolkit->import(qw(Dumper)); | ||
2508 | 23 | Percona::WebAPI::Representation->import(qw(as_hashref)); | ||
2509 | 24 | |||
2510 | 25 | my @log; | ||
2511 | 26 | my $logger = Percona::Test::Mock::AgentLogger->new(log => \@log); | ||
2512 | 27 | pt_agent::_logger($logger); | ||
2513 | 28 | |||
2514 | 29 | # Fake --lib and --spool dirs. | ||
2515 | 30 | my $tmpdir = tempdir("/tmp/pt-agent.$PID.XXXXXX", CLEANUP => 1); | ||
2516 | 31 | output( sub { | ||
2517 | 32 | pt_agent::init_lib_dir(lib_dir => $tmpdir); | ||
2518 | 33 | }); | ||
2519 | 34 | |||
2520 | 35 | # ############################################################################# | ||
2521 | 36 | # Create mock client and Agent | ||
2522 | 37 | # ############################################################################# | ||
2523 | 38 | |||
2524 | 39 | # These aren't the real tests yet: to run_agent, first we need | ||
2525 | 40 | # a client and Agent, so create mock ones. | ||
2526 | 41 | |||
2527 | 42 | my $output; | ||
2528 | 43 | my $json = JSON->new->canonical([1])->pretty; | ||
2529 | 44 | $json->allow_blessed([]); | ||
2530 | 45 | $json->convert_blessed([]); | ||
2531 | 46 | |||
2532 | 47 | my $ua = Percona::Test::Mock::UserAgent->new( | ||
2533 | 48 | encode => sub { my $c = shift; return $json->encode($c || {}) }, | ||
2534 | 49 | ); | ||
2535 | 50 | |||
2536 | 51 | my $client = eval { | ||
2537 | 52 | Percona::WebAPI::Client->new( | ||
2538 | 53 | api_key => '123', | ||
2539 | 54 | ua => $ua, | ||
2540 | 55 | ); | ||
2541 | 56 | }; | ||
2542 | 57 | |||
2543 | 58 | is( | ||
2544 | 59 | $EVAL_ERROR, | ||
2545 | 60 | '', | ||
2546 | 61 | 'Create mock client' | ||
2547 | 62 | ) or die; | ||
2548 | 63 | |||
2549 | 64 | my $agent = Percona::WebAPI::Resource::Agent->new( | ||
2550 | 65 | uuid => '123', | ||
2551 | 66 | hostname => 'host', | ||
2552 | 67 | username => 'user', | ||
2553 | 68 | links => { | ||
2554 | 69 | self => '/agents/123', | ||
2555 | 70 | config => '/agents/123/config', | ||
2556 | 71 | }, | ||
2557 | 72 | ); | ||
2558 | 73 | |||
2559 | 74 | my @cmds; | ||
2560 | 75 | my $exec_cmd = sub { | ||
2561 | 76 | my $cmd = shift; | ||
2562 | 77 | push @cmds, $cmd; | ||
2563 | 78 | return 0; | ||
2564 | 79 | }; | ||
2565 | 80 | |||
2566 | 81 | # ############################################################################# | ||
2567 | 82 | # Test get_services() | ||
2568 | 83 | # ############################################################################# | ||
2569 | 84 | |||
2570 | 85 | # query-history | ||
2571 | 86 | |||
2572 | 87 | my $run0 = Percona::WebAPI::Resource::Task->new( | ||
2573 | 88 | name => 'query-history', | ||
2574 | 89 | number => '0', | ||
2575 | 90 | program => 'pt-query-digest --output json', | ||
2576 | 91 | output => 'spool', | ||
2577 | 92 | ); | ||
2578 | 93 | |||
2579 | 94 | my $qh = Percona::WebAPI::Resource::Service->new( | ||
2580 | 95 | ts => '100', | ||
2581 | 96 | name => 'query-history', | ||
2582 | 97 | run_schedule => '1 * * * *', | ||
2583 | 98 | spool_schedule => '2 * * * *', | ||
2584 | 99 | tasks => [ $run0 ], | ||
2585 | 100 | links => { | ||
2586 | 101 | self => '/query-history', | ||
2587 | 102 | data => '/query-history/data', | ||
2588 | 103 | }, | ||
2589 | 104 | ); | ||
2590 | 105 | |||
2591 | 106 | my $run1 = Percona::WebAPI::Resource::Task->new( | ||
2592 | 107 | name => 'start-query-history', | ||
2593 | 108 | number => '0', | ||
2594 | 109 | program => 'echo "start-qh"', | ||
2595 | 110 | output => 'spool', | ||
2596 | 111 | ); | ||
2597 | 112 | |||
2598 | 113 | my $start_qh = Percona::WebAPI::Resource::Service->new( | ||
2599 | 114 | ts => '100', | ||
2600 | 115 | name => 'start-query-history', | ||
2601 | 116 | meta => 1, | ||
2602 | 117 | tasks => [ $run1 ], | ||
2603 | 118 | links => { | ||
2604 | 119 | self => '/query-history', | ||
2605 | 120 | data => '/query-history/data', | ||
2606 | 121 | }, | ||
2607 | 122 | ); | ||
2608 | 123 | |||
2609 | 124 | $ua->{responses}->{get} = [ | ||
2610 | 125 | { | ||
2611 | 126 | headers => { 'X-Percona-Resource-Type' => 'Service' }, | ||
2612 | 127 | content => [ | ||
2613 | 128 | as_hashref($qh, with_links => 1), | ||
2614 | 129 | as_hashref($start_qh, with_links => 1), | ||
2615 | 130 | ], | ||
2616 | 131 | }, | ||
2617 | 132 | ]; | ||
2618 | 133 | |||
2619 | 134 | my $services = {}; | ||
2620 | 135 | my $success = 0; | ||
2621 | 136 | |||
2622 | 137 | $output = output( | ||
2623 | 138 | sub { | ||
2624 | 139 | ($services, $success) = pt_agent::get_services( | ||
2625 | 140 | # Required args | ||
2626 | 141 | link => '/agents/123/services', | ||
2627 | 142 | agent => $agent, | ||
2628 | 143 | client => $client, | ||
2629 | 144 | lib_dir => $tmpdir, | ||
2630 | 145 | services => $services, | ||
2631 | 146 | # Optional args, for testing | ||
2632 | 147 | json => $json, | ||
2633 | 148 | bin_dir => "$trunk/bin/", | ||
2634 | 149 | exec_cmd => $exec_cmd, | ||
2635 | 150 | ); | ||
2636 | 151 | }, | ||
2637 | 152 | stderr => 1, | ||
2638 | 153 | ); | ||
2639 | 154 | |||
2640 | 155 | is( | ||
2641 | 156 | $success, | ||
2642 | 157 | 1, | ||
2643 | 158 | "Success" | ||
2644 | 159 | ); | ||
2645 | 160 | |||
2646 | 161 | is( | ||
2647 | 162 | ref $services, | ||
2648 | 163 | 'HASH', | ||
2649 | 164 | "Return services as hashref" | ||
2650 | 165 | ) or diag(Dumper($services)); | ||
2651 | 166 | |||
2652 | 167 | is( | ||
2653 | 168 | scalar keys %$services, | ||
2654 | 169 | 2, | ||
2655 | 170 | 'Only 2 services' | ||
2656 | 171 | ) or diag(Dumper($services)); | ||
2657 | 172 | |||
2658 | 173 | ok( | ||
2659 | 174 | exists $services->{'query-history'}, | ||
2660 | 175 | "services hashref keyed on service name" | ||
2661 | 176 | ) or diag(Dumper($services)); | ||
2662 | 177 | |||
2663 | 178 | isa_ok( | ||
2664 | 179 | ref $services->{'query-history'}, | ||
2665 | 180 | 'Percona::WebAPI::Resource::Service', | ||
2666 | 181 | 'services->{query-history}' | ||
2667 | 182 | ); | ||
2668 | 183 | |||
2669 | 184 | my $crontab = -f "$tmpdir/crontab" ? slurp_file("$tmpdir/crontab") : ''; | ||
2670 | 185 | is( | ||
2671 | 186 | $crontab, | ||
2672 | 187 | "1 * * * * $trunk/bin/pt-agent --run-service query-history | ||
2673 | 188 | 2 * * * * $trunk/bin/pt-agent --send-data query-history | ||
2674 | 189 | ", | ||
2675 | 190 | "crontab file" | ||
2676 | 191 | ) or diag($output, `ls -l $tmpdir/*`, Dumper(\@log)); | ||
2677 | 192 | |||
2678 | 193 | is_deeply( | ||
2679 | 194 | \@cmds, | ||
2680 | 195 | [ | ||
2681 | 196 | "$trunk/bin/pt-agent --run-service start-query-history >> $tmpdir/logs/start-stop.log 2>&1", | ||
2682 | 197 | "crontab $tmpdir/crontab > $tmpdir/crontab.err 2>&1", | ||
2683 | 198 | ], | ||
2684 | 199 | "Ran start-service and crontab" | ||
2685 | 200 | ) or diag(Dumper(\@cmds), Dumper(\@log)); | ||
2686 | 201 | |||
2687 | 202 | ok( | ||
2688 | 203 | -f "$tmpdir/services/query-history", | ||
2689 | 204 | "Wrote --lib/services/query-history" | ||
2690 | 205 | ); | ||
2691 | 206 | |||
2692 | 207 | # ############################################################################# | ||
2693 | 208 | # A more realistic transaction | ||
2694 | 209 | # ############################################################################# | ||
2695 | 210 | |||
2696 | 211 | # services/query-history should exist from the previous tests. For these | ||
2697 | 212 | # tests, get_services() should update the file, so we empty it and check | ||
2698 | 213 | # that it's re-created, i.e. updated. | ||
2699 | 214 | diag(`echo -n > $tmpdir/services/query-history`); | ||
2700 | 215 | is( | ||
2701 | 216 | -s "$tmpdir/services/query-history", | ||
2702 | 217 | 0, | ||
2703 | 218 | "Start: empty --lib/services/query-history" | ||
2704 | 219 | ); | ||
2705 | 220 | |||
2706 | 221 | # start-query-history | ||
2707 | 222 | |||
2708 | 223 | my $task1 = Percona::WebAPI::Resource::Task->new( | ||
2709 | 224 | name => 'disable-slow-query-log', | ||
2710 | 225 | number => '0', | ||
2711 | 226 | query => "SET GLOBAL slow_query_log=0", | ||
2712 | 227 | ); | ||
2713 | 228 | |||
2714 | 229 | my $task2 = Percona::WebAPI::Resource::Task->new( | ||
2715 | 230 | name => 'set-slow-query-log-file', | ||
2716 | 231 | number => '1', | ||
2717 | 232 | query => "SET GLOBAL slow_query_log_file='/tmp/slow.log'", | ||
2718 | 233 | ); | ||
2719 | 234 | |||
2720 | 235 | my $task3 = Percona::WebAPI::Resource::Task->new( | ||
2721 | 236 | name => 'set-long-query-time', | ||
2722 | 237 | number => '2', | ||
2723 | 238 | query => "SET GLOBAL long_query_time=0.01", | ||
2724 | 239 | ); | ||
2725 | 240 | |||
2726 | 241 | my $task4 = Percona::WebAPI::Resource::Task->new( | ||
2727 | 242 | name => 'enable-slow-query-log', | ||
2728 | 243 | number => '3', | ||
2729 | 244 | query => "SET GLOBAL slow_query_log=1", | ||
2730 | 245 | ); | ||
2731 | 246 | |||
2732 | 247 | $start_qh = Percona::WebAPI::Resource::Service->new( | ||
2733 | 248 | ts => '100', | ||
2734 | 249 | name => 'start-query-history', | ||
2735 | 250 | tasks => [ $task1, $task2, $task3, $task4 ], | ||
2736 | 251 | meta => 1, | ||
2737 | 252 | links => { | ||
2738 | 253 | self => '/query-history', | ||
2739 | 254 | data => '/query-history/data', | ||
2740 | 255 | }, | ||
2741 | 256 | ); | ||
2742 | 257 | |||
2743 | 258 | # stop-query-history | ||
2744 | 259 | |||
2745 | 260 | my $task5 = Percona::WebAPI::Resource::Task->new( | ||
2746 | 261 | name => 'disable-slow-query-log', | ||
2747 | 262 | number => '0', | ||
2748 | 263 | query => "SET GLOBAL slow_query_log=0", | ||
2749 | 264 | ); | ||
2750 | 265 | |||
2751 | 266 | my $stop_qh = Percona::WebAPI::Resource::Service->new( | ||
2752 | 267 | ts => '100', | ||
2753 | 268 | name => 'stop-query-history', | ||
2754 | 269 | tasks => [ $task5 ], | ||
2755 | 270 | meta => 1, | ||
2756 | 271 | links => { | ||
2757 | 272 | self => '/query-history', | ||
2758 | 273 | data => '/query-history/data', | ||
2759 | 274 | }, | ||
2760 | 275 | ); | ||
2761 | 276 | |||
2762 | 277 | # We'll use query-history from the previous tests. | ||
2763 | 278 | |||
2764 | 279 | $ua->{responses}->{get} = [ | ||
2765 | 280 | { | ||
2766 | 281 | headers => { 'X-Percona-Resource-Type' => 'Service' }, | ||
2767 | 282 | content => [ | ||
2768 | 283 | as_hashref($start_qh, with_links => 1), | ||
2769 | 284 | as_hashref($stop_qh, with_links => 1), | ||
2770 | 285 | as_hashref($qh, with_links => 1), # from previous tests | ||
2771 | 286 | ], | ||
2772 | 287 | }, | ||
2773 | 288 | ]; | ||
2774 | 289 | |||
2775 | 290 | @log = (); | ||
2776 | 291 | @cmds = (); | ||
2777 | 292 | $services = {}; | ||
2778 | 293 | $success = 0; | ||
2779 | 294 | |||
2780 | 295 | $output = output( | ||
2781 | 296 | sub { | ||
2782 | 297 | ($services, $success) = pt_agent::get_services( | ||
2783 | 298 | # Required args | ||
2784 | 299 | link => '/agents/123/services', | ||
2785 | 300 | agent => $agent, | ||
2786 | 301 | client => $client, | ||
2787 | 302 | lib_dir => $tmpdir, | ||
2788 | 303 | services => $services, | ||
2789 | 304 | # Optional args, for testing | ||
2790 | 305 | json => $json, | ||
2791 | 306 | bin_dir => "$trunk/bin/", | ||
2792 | 307 | exec_cmd => $exec_cmd, | ||
2793 | 308 | ); | ||
2794 | 309 | }, | ||
2795 | 310 | stderr => 1, | ||
2796 | 311 | ); | ||
2797 | 312 | |||
2798 | 313 | is_deeply( | ||
2799 | 314 | \@cmds, | ||
2800 | 315 | [ | ||
2801 | 316 | "$trunk/bin/pt-agent --run-service start-query-history >> $tmpdir/logs/start-stop.log 2>&1", | ||
2802 | 317 | "crontab $tmpdir/crontab > $tmpdir/crontab.err 2>&1", | ||
2803 | 318 | ], | ||
2804 | 319 | "Start: ran start-query-history" | ||
2805 | 320 | ) or diag(Dumper(\@cmds), $output); | ||
2806 | 321 | |||
2807 | 322 | ok( | ||
2808 | 323 | -f "$tmpdir/services/start-query-history", | ||
2809 | 324 | "Start: added --lib/services/start-query-history" | ||
2810 | 325 | ) or diag($output); | ||
2811 | 326 | |||
2812 | 327 | ok( | ||
2813 | 328 | -f "$tmpdir/services/stop-query-history", | ||
2814 | 329 | "Start: added --lib/services/stop-query-history" | ||
2815 | 330 | ) or diag($output); | ||
2816 | 331 | |||
2817 | 332 | my $contents = slurp_file("$tmpdir/services/query-history"); | ||
2818 | 333 | like( | ||
2819 | 334 | $contents, | ||
2820 | 335 | qr/query-history/, | ||
2821 | 336 | "Start: updated --lib/services/query-history" | ||
2822 | 337 | ) or diag($output); | ||
2823 | 338 | |||
2824 | 339 | $crontab = slurp_file("$tmpdir/crontab"); | ||
2825 | 340 | is( | ||
2826 | 341 | $crontab, | ||
2827 | 342 | "1 * * * * $trunk/bin/pt-agent --run-service query-history | ||
2828 | 343 | 2 * * * * $trunk/bin/pt-agent --send-data query-history | ||
2829 | 344 | ", | ||
2830 | 345 | "Start: only scheduled query-history" | ||
2831 | 346 | ) or diag($output); | ||
2832 | 347 | |||
2833 | 348 | # ############################################################################# | ||
2834 | 349 | # Update and restart a service | ||
2835 | 350 | # ############################################################################# | ||
2836 | 351 | |||
2837 | 352 | # pt-agent should remove a service's --lib/meta/ files when restarting, | ||
2838 | 353 | # so create one and check that it's removed. | ||
2839 | 354 | diag(`touch $tmpdir/meta/query-history.foo`); | ||
2840 | 355 | ok( | ||
2841 | 356 | -f "$tmpdir/meta/query-history.foo", | ||
2842 | 357 | "Restart: meta file exists" | ||
2843 | 358 | ); | ||
2844 | 359 | |||
2845 | 360 | $qh = Percona::WebAPI::Resource::Service->new( | ||
2846 | 361 | ts => '200', # was 100 | ||
2847 | 362 | name => 'query-history', | ||
2848 | 363 | run_schedule => '1 * * * *', | ||
2849 | 364 | spool_schedule => '2 * * * *', | ||
2850 | 365 | tasks => [ $run0 ], | ||
2851 | 366 | links => { | ||
2852 | 367 | self => '/query-history', | ||
2853 | 368 | data => '/query-history/data', | ||
2854 | 369 | }, | ||
2855 | 370 | ); | ||
2856 | 371 | |||
2857 | 372 | $ua->{responses}->{get} = [ | ||
2858 | 373 | { | ||
2859 | 374 | headers => { 'X-Percona-Resource-Type' => 'Service' }, | ||
2860 | 375 | content => [ | ||
2861 | 376 | as_hashref($start_qh, with_links => 1), # has not changed | ||
2862 | 377 | as_hashref($stop_qh, with_links => 1), # has not changed | ||
2863 | 378 | as_hashref($qh, with_links => 1), | ||
2864 | 379 | ], | ||
2865 | 380 | }, | ||
2866 | 381 | ]; | ||
2867 | 382 | |||
2868 | 383 | @log = (); | ||
2869 | 384 | @cmds = (); | ||
2870 | 385 | $success = 0; | ||
2871 | 386 | |||
2872 | 387 | $output = output( | ||
2873 | 388 | sub { | ||
2874 | 389 | ($services, $success) = pt_agent::get_services( | ||
2875 | 390 | # Required args | ||
2876 | 391 | link => '/agents/123/services', | ||
2877 | 392 | agent => $agent, | ||
2878 | 393 | client => $client, | ||
2879 | 394 | lib_dir => $tmpdir, | ||
2880 | 395 | services => $services, # retval from previous call | ||
2881 | 396 | # Optional args, for testing | ||
2882 | 397 | json => $json, | ||
2883 | 398 | bin_dir => "$trunk/bin/", | ||
2884 | 399 | exec_cmd => $exec_cmd, | ||
2885 | 400 | ); | ||
2886 | 401 | }, | ||
2887 | 402 | stderr => 1, | ||
2888 | 403 | ); | ||
2889 | 404 | |||
2890 | 405 | is_deeply( | ||
2891 | 406 | \@cmds, | ||
2892 | 407 | [ | ||
2893 | 408 | "$trunk/bin/pt-agent --run-service stop-query-history >> $tmpdir/logs/start-stop.log 2>&1", | ||
2894 | 409 | "$trunk/bin/pt-agent --run-service start-query-history >> $tmpdir/logs/start-stop.log 2>&1", | ||
2895 | 410 | "crontab $tmpdir/crontab > $tmpdir/crontab.err 2>&1", | ||
2896 | 411 | ], | ||
2897 | 412 | "Restart: ran stop-query-history then start-query-history" | ||
2898 | 413 | ) or diag(Dumper(\@cmds), $output); | ||
2899 | 414 | |||
2900 | 415 | ok( | ||
2901 | 416 | !-f "$tmpdir/meta/query-history.foo", | ||
2902 | 417 | "Restart: meta file removed" | ||
2903 | 418 | ) or diag($output); | ||
2904 | 419 | |||
2905 | 420 | # ############################################################################# | ||
2906 | 421 | # Done. | ||
2907 | 422 | # ############################################################################# | ||
2908 | 423 | done_testing; | ||
2909 | 424 | 0 | ||
2910 | === removed file 't/pt-agent/init_agent.t' | |||
2911 | --- t/pt-agent/init_agent.t 2013-09-23 19:07:34 +0000 | |||
2912 | +++ t/pt-agent/init_agent.t 1970-01-01 00:00:00 +0000 | |||
2913 | @@ -1,333 +0,0 @@ | |||
2914 | 1 | #!/usr/bin/env perl | ||
2915 | 2 | |||
2916 | 3 | BEGIN { | ||
2917 | 4 | die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n" | ||
2918 | 5 | unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH}; | ||
2919 | 6 | unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib"; | ||
2920 | 7 | }; | ||
2921 | 8 | |||
2922 | 9 | use strict; | ||
2923 | 10 | use warnings FATAL => 'all'; | ||
2924 | 11 | use English qw(-no_match_vars); | ||
2925 | 12 | use Test::More; | ||
2926 | 13 | use JSON; | ||
2927 | 14 | use File::Temp qw(tempdir); | ||
2928 | 15 | |||
2929 | 16 | use Percona::Test; | ||
2930 | 17 | use Percona::Test::Mock::UserAgent; | ||
2931 | 18 | use Percona::Test::Mock::AgentLogger; | ||
2932 | 19 | require "$trunk/bin/pt-agent"; | ||
2933 | 20 | |||
2934 | 21 | Percona::Toolkit->import(qw(Dumper)); | ||
2935 | 22 | Percona::WebAPI::Representation->import(qw(as_hashref)); | ||
2936 | 23 | |||
2937 | 24 | my $tmpdir = tempdir("/tmp/pt-agent.$PID.XXXXXX", CLEANUP => 1); | ||
2938 | 25 | |||
2939 | 26 | my $json = JSON->new->canonical([1])->pretty; | ||
2940 | 27 | $json->allow_blessed([]); | ||
2941 | 28 | $json->convert_blessed([]); | ||
2942 | 29 | |||
2943 | 30 | my @log; | ||
2944 | 31 | my $logger = Percona::Test::Mock::AgentLogger->new(log => \@log); | ||
2945 | 32 | pt_agent::_logger($logger); | ||
2946 | 33 | |||
2947 | 34 | my $ua = Percona::Test::Mock::UserAgent->new( | ||
2948 | 35 | encode => sub { my $c = shift; return $json->encode($c || {}) }, | ||
2949 | 36 | ); | ||
2950 | 37 | |||
2951 | 38 | my $client = eval { | ||
2952 | 39 | Percona::WebAPI::Client->new( | ||
2953 | 40 | api_key => '123', | ||
2954 | 41 | ua => $ua, | ||
2955 | 42 | ); | ||
2956 | 43 | }; | ||
2957 | 44 | |||
2958 | 45 | is( | ||
2959 | 46 | $EVAL_ERROR, | ||
2960 | 47 | '', | ||
2961 | 48 | 'Create Client with mock user agent' | ||
2962 | 49 | ) or die; | ||
2963 | 50 | |||
2964 | 51 | my @ok; | ||
2965 | 52 | my $oktorun = sub { | ||
2966 | 53 | return shift @ok; | ||
2967 | 54 | }; | ||
2968 | 55 | |||
2969 | 56 | my @wait; | ||
2970 | 57 | my $interval = sub { | ||
2971 | 58 | my $t = shift; | ||
2972 | 59 | push @wait, $t; | ||
2973 | 60 | }; | ||
2974 | 61 | |||
2975 | 62 | # ############################################################################# | ||
2976 | 63 | # Init a new agent, i.e. create it. | ||
2977 | 64 | # ############################################################################# | ||
2978 | 65 | |||
2979 | 66 | my $post_agent = Percona::WebAPI::Resource::Agent->new( | ||
2980 | 67 | uuid => '123', | ||
2981 | 68 | hostname => 'host1', | ||
2982 | 69 | username => 'name1', | ||
2983 | 70 | versions => { | ||
2984 | 71 | }, | ||
2985 | 72 | links => { | ||
2986 | 73 | self => '/agents/123', | ||
2987 | 74 | config => '/agents/123/config', | ||
2988 | 75 | }, | ||
2989 | 76 | ); | ||
2990 | 77 | |||
2991 | 78 | my $return_agent = Percona::WebAPI::Resource::Agent->new( | ||
2992 | 79 | uuid => '123', | ||
2993 | 80 | hostname => 'host2', | ||
2994 | 81 | username => 'name2', | ||
2995 | 82 | versions => { | ||
2996 | 83 | }, | ||
2997 | 84 | links => { | ||
2998 | 85 | self => '/agents/123', | ||
2999 | 86 | config => '/agents/123/config', | ||
3000 | 87 | }, | ||
3001 | 88 | ); | ||
3002 | 89 | |||
3003 | 90 | $ua->{responses}->{post} = [ | ||
3004 | 91 | { | ||
3005 | 92 | headers => { 'Location' => '/agents/123' }, | ||
3006 | 93 | }, | ||
3007 | 94 | ]; | ||
3008 | 95 | |||
3009 | 96 | $ua->{responses}->{get} = [ | ||
3010 | 97 | { | ||
3011 | 98 | headers => { 'X-Percona-Resource-Type' => 'Agent' }, | ||
3012 | 99 | content => as_hashref($return_agent, with_links =>1 ), | ||
3013 | 100 | }, | ||
3014 | 101 | ]; | ||
3015 | 102 | |||
3016 | 103 | my $got_agent; | ||
3017 | 104 | my $output = output( | ||
3018 | 105 | sub { | ||
3019 | 106 | ($got_agent) = pt_agent::init_agent( | ||
3020 | 107 | agent => $post_agent, | ||
3021 | 108 | action => 'post', | ||
3022 | 109 | link => "/agents", | ||
3023 | 110 | client => $client, | ||
3024 | 111 | interval => $interval, | ||
3025 | 112 | tries => 4, | ||
3026 | 113 | ); | ||
3027 | 114 | }, | ||
3028 | 115 | stderr => 1, | ||
3029 | 116 | ); | ||
3030 | 117 | |||
3031 | 118 | is( | ||
3032 | 119 | $got_agent->hostname, | ||
3033 | 120 | 'host2', | ||
3034 | 121 | 'Got and returned Agent' | ||
3035 | 122 | ) or diag($output, Dumper(as_hashref($got_agent, with_links => 1))); | ||
3036 | 123 | |||
3037 | 124 | is( | ||
3038 | 125 | scalar @wait, | ||
3039 | 126 | 0, | ||
3040 | 127 | "Client did not wait (new Agent)" | ||
3041 | 128 | ) or diag($output); | ||
3042 | 129 | |||
3043 | 130 | # ############################################################################# | ||
3044 | 131 | # Repeat this test but this time fake an error, so the tool isn't able | ||
3045 | 132 | # to create the Agent first time, so it should wait (call interval), | ||
3046 | 133 | # and try again. | ||
3047 | 134 | # ############################################################################# | ||
3048 | 135 | |||
3049 | 136 | $return_agent->{id} = '456'; | ||
3050 | 137 | $return_agent->{links} = { | ||
3051 | 138 | self => '/agents/456', | ||
3052 | 139 | config => '/agents/456/config', | ||
3053 | 140 | }; | ||
3054 | 141 | |||
3055 | 142 | $ua->{responses}->{post} = [ | ||
3056 | 143 | { # 1, the fake error | ||
3057 | 144 | code => 500, | ||
3058 | 145 | }, | ||
3059 | 146 | # 2, code should call interval | ||
3060 | 147 | { # 3, code should try again, then receive this | ||
3061 | 148 | code => 200, | ||
3062 | 149 | headers => { 'Location' => '/agents/456' }, | ||
3063 | 150 | }, | ||
3064 | 151 | ]; | ||
3065 | 152 | # 4, code will GET the new Agent | ||
3066 | 153 | $ua->{responses}->{get} = [ | ||
3067 | 154 | { | ||
3068 | 155 | headers => { 'X-Percona-Resource-Type' => 'Agent' }, | ||
3069 | 156 | content => as_hashref($return_agent, with_links =>1 ), | ||
3070 | 157 | }, | ||
3071 | 158 | ]; | ||
3072 | 159 | |||
3073 | 160 | @ok = qw(1 1 0); | ||
3074 | 161 | @wait = (); | ||
3075 | 162 | @log = (); | ||
3076 | 163 | $ua->{requests} = []; | ||
3077 | 164 | |||
3078 | 165 | $output = output( | ||
3079 | 166 | sub { | ||
3080 | 167 | ($got_agent) = pt_agent::init_agent( | ||
3081 | 168 | agent => $post_agent, | ||
3082 | 169 | action => 'post', | ||
3083 | 170 | link => "/agents", | ||
3084 | 171 | client => $client, | ||
3085 | 172 | interval => $interval, | ||
3086 | 173 | tries => 5, | ||
3087 | 174 | oktorun => $oktorun, | ||
3088 | 175 | ); | ||
3089 | 176 | }, | ||
3090 | 177 | stderr => 1, | ||
3091 | 178 | ); | ||
3092 | 179 | |||
3093 | 180 | is( | ||
3094 | 181 | ($got_agent ? $got_agent->hostname : ''), | ||
3095 | 182 | 'host2', | ||
3096 | 183 | 'Got and returned Agent after error' | ||
3097 | 184 | ) or diag($output, Dumper($got_agent)); | ||
3098 | 185 | |||
3099 | 186 | is( | ||
3100 | 187 | scalar @wait, | ||
3101 | 188 | 1, | ||
3102 | 189 | "Client waited after error" | ||
3103 | 190 | ); | ||
3104 | 191 | |||
3105 | 192 | is_deeply( | ||
3106 | 193 | $ua->{requests}, | ||
3107 | 194 | [ | ||
3108 | 195 | 'POST /agents', # first attempt, 500 error | ||
3109 | 196 | 'POST /agents', # second attemp, 200 OK | ||
3110 | 197 | 'GET /agents/456', # GET new Agent | ||
3111 | 198 | ], | ||
3112 | 199 | "POST POST GET new Agent after error" | ||
3113 | 200 | ) or diag(Dumper($ua->{requests})); | ||
3114 | 201 | |||
3115 | 202 | like( | ||
3116 | 203 | $log[1], | ||
3117 | 204 | qr{WARNING Failed to POST /agents}, | ||
3118 | 205 | "POST /agents failure logged after error" | ||
3119 | 206 | ) or diag(Dumper($ua->{requests}), Dumper(\@log)); | ||
3120 | 207 | |||
3121 | 208 | # ############################################################################# | ||
3122 | 209 | # Init an existing agent, i.e. update it. | ||
3123 | 210 | # ############################################################################# | ||
3124 | 211 | |||
3125 | 212 | my $put_agent = Percona::WebAPI::Resource::Agent->new( | ||
3126 | 213 | uuid => '123', | ||
3127 | 214 | hostname => 'host3', | ||
3128 | 215 | username => 'name3', | ||
3129 | 216 | versions => { | ||
3130 | 217 | }, | ||
3131 | 218 | links => { | ||
3132 | 219 | self => '/agents/123', | ||
3133 | 220 | config => '/agents/123/config', | ||
3134 | 221 | }, | ||
3135 | 222 | ); | ||
3136 | 223 | |||
3137 | 224 | $ua->{responses}->{put} = [ | ||
3138 | 225 | { | ||
3139 | 226 | code => 200, | ||
3140 | 227 | headers => { | ||
3141 | 228 | Location => '/agents/123', | ||
3142 | 229 | }, | ||
3143 | 230 | }, | ||
3144 | 231 | ]; | ||
3145 | 232 | $ua->{responses}->{get} = [ | ||
3146 | 233 | { | ||
3147 | 234 | code => 200, | ||
3148 | 235 | headers => { 'X-Percona-Resource-Type' => 'Agent' }, | ||
3149 | 236 | content => as_hashref($return_agent, with_links =>1 ), | ||
3150 | 237 | } | ||
3151 | 238 | ]; | ||
3152 | 239 | |||
3153 | 240 | @wait = (); | ||
3154 | 241 | $ua->{requests} = []; | ||
3155 | 242 | |||
3156 | 243 | $output = output( | ||
3157 | 244 | sub { | ||
3158 | 245 | ($got_agent) = pt_agent::init_agent( | ||
3159 | 246 | agent => $put_agent, | ||
3160 | 247 | action => 'put', | ||
3161 | 248 | link => "/agents/123", | ||
3162 | 249 | client => $client, | ||
3163 | 250 | interval => $interval, | ||
3164 | 251 | tries => 4, | ||
3165 | 252 | ); | ||
3166 | 253 | }, | ||
3167 | 254 | stderr => 1, | ||
3168 | 255 | ); | ||
3169 | 256 | |||
3170 | 257 | is( | ||
3171 | 258 | $got_agent->hostname, | ||
3172 | 259 | 'host2', | ||
3173 | 260 | 'PUT Agent' | ||
3174 | 261 | ) or diag($output, Dumper(as_hashref($got_agent, with_links => 1))); | ||
3175 | 262 | |||
3176 | 263 | is( | ||
3177 | 264 | scalar @wait, | ||
3178 | 265 | 0, | ||
3179 | 266 | "Client did not wait (saved Agent)" | ||
3180 | 267 | ); | ||
3181 | 268 | |||
3182 | 269 | is_deeply( | ||
3183 | 270 | $ua->{requests}, | ||
3184 | 271 | [ | ||
3185 | 272 | 'PUT /agents/123', | ||
3186 | 273 | 'GET /agents/123', | ||
3187 | 274 | ], | ||
3188 | 275 | "PUT then GET Agent" | ||
3189 | 276 | ) or diag(Dumper($ua->{requests})); | ||
3190 | 277 | |||
3191 | 278 | # ############################################################################# | ||
3192 | 279 | # Status 403 (too many agents) should abort further attempts. | ||
3193 | 280 | # ############################################################################# | ||
3194 | 281 | |||
3195 | 282 | $ua->{responses}->{post} = [ | ||
3196 | 283 | { # 1, the fake error | ||
3197 | 284 | code => 403, | ||
3198 | 285 | }, | ||
3199 | 286 | ]; | ||
3200 | 287 | |||
3201 | 288 | @ok = qw(1 1 0); | ||
3202 | 289 | @wait = (); | ||
3203 | 290 | @log = (); | ||
3204 | 291 | $ua->{requests} = []; | ||
3205 | 292 | |||
3206 | 293 | $output = output( | ||
3207 | 294 | sub { | ||
3208 | 295 | ($got_agent) = pt_agent::init_agent( | ||
3209 | 296 | agent => $post_agent, | ||
3210 | 297 | action => 'post', | ||
3211 | 298 | link => "/agents", | ||
3212 | 299 | client => $client, | ||
3213 | 300 | interval => $interval, | ||
3214 | 301 | tries => 3, | ||
3215 | 302 | oktorun => $oktorun, | ||
3216 | 303 | ); | ||
3217 | 304 | }, | ||
3218 | 305 | stderr => 1, | ||
3219 | 306 | ); | ||
3220 | 307 | |||
3221 | 308 | is( | ||
3222 | 309 | scalar @wait, | ||
3223 | 310 | 2, | ||
3224 | 311 | "Too many agents (403): waits" | ||
3225 | 312 | ); | ||
3226 | 313 | |||
3227 | 314 | is_deeply( | ||
3228 | 315 | $ua->{requests}, | ||
3229 | 316 | [ | ||
3230 | 317 | 'POST /agents', | ||
3231 | 318 | 'POST /agents', | ||
3232 | 319 | ], | ||
3233 | 320 | "Too many agents (403): tries" | ||
3234 | 321 | ) or diag(Dumper($ua->{requests})); | ||
3235 | 322 | |||
3236 | 323 | my $n = grep { $_ =~ m/too many agents/ } @log; | ||
3237 | 324 | is( | ||
3238 | 325 | $n, | ||
3239 | 326 | 1, | ||
3240 | 327 | "Too many agents (403): does not repeat warning" | ||
3241 | 328 | ) or diag(Dumper(\@log)); | ||
3242 | 329 | |||
3243 | 330 | # ############################################################################# | ||
3244 | 331 | # Done. | ||
3245 | 332 | # ############################################################################# | ||
3246 | 333 | done_testing; | ||
3247 | 334 | 0 | ||
3248 | === removed file 't/pt-agent/make_new_crontab.t' | |||
3249 | --- t/pt-agent/make_new_crontab.t 2013-04-19 20:49:01 +0000 | |||
3250 | +++ t/pt-agent/make_new_crontab.t 1970-01-01 00:00:00 +0000 | |||
3251 | @@ -1,151 +0,0 @@ | |||
3252 | 1 | #!/usr/bin/env perl | ||
3253 | 2 | |||
3254 | 3 | BEGIN { | ||
3255 | 4 | die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n" | ||
3256 | 5 | unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH}; | ||
3257 | 6 | unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib"; | ||
3258 | 7 | }; | ||
3259 | 8 | |||
3260 | 9 | use strict; | ||
3261 | 10 | use warnings FATAL => 'all'; | ||
3262 | 11 | use English qw(-no_match_vars); | ||
3263 | 12 | use Test::More; | ||
3264 | 13 | use JSON; | ||
3265 | 14 | use File::Temp qw(tempfile); | ||
3266 | 15 | |||
3267 | 16 | use Percona::Test; | ||
3268 | 17 | require "$trunk/bin/pt-agent"; | ||
3269 | 18 | |||
3270 | 19 | Percona::Toolkit->import(qw(have_required_args Dumper)); | ||
3271 | 20 | |||
3272 | 21 | my $sample = "t/pt-agent/samples"; | ||
3273 | 22 | |||
3274 | 23 | sub test_make_new_crontab { | ||
3275 | 24 | my (%args) = @_; | ||
3276 | 25 | have_required_args(\%args, qw( | ||
3277 | 26 | file | ||
3278 | 27 | services | ||
3279 | 28 | )) or die; | ||
3280 | 29 | my $file = $args{file}; | ||
3281 | 30 | my $services = $args{services}; | ||
3282 | 31 | |||
3283 | 32 | my $crontab_list = slurp_file("$trunk/$sample/$file.in"); | ||
3284 | 33 | |||
3285 | 34 | my $new_crontab = pt_agent::make_new_crontab( | ||
3286 | 35 | services => $services, | ||
3287 | 36 | crontab_list => $crontab_list, | ||
3288 | 37 | bin_dir => '', | ||
3289 | 38 | ); | ||
3290 | 39 | |||
3291 | 40 | ok( | ||
3292 | 41 | no_diff( | ||
3293 | 42 | $new_crontab, | ||
3294 | 43 | "$sample/$file.out", | ||
3295 | 44 | cmd_output => 1, | ||
3296 | 45 | ), | ||
3297 | 46 | $args{name} || $file, | ||
3298 | 47 | ) or diag($new_crontab); | ||
3299 | 48 | } | ||
3300 | 49 | |||
3301 | 50 | my $run0 = Percona::WebAPI::Resource::Task->new( | ||
3302 | 51 | name => 'query-history', | ||
3303 | 52 | number => '0', | ||
3304 | 53 | program => 'pt-query-digest', | ||
3305 | 54 | options => '--output json', | ||
3306 | 55 | output => 'spool', | ||
3307 | 56 | ); | ||
3308 | 57 | |||
3309 | 58 | my $svc0 = Percona::WebAPI::Resource::Service->new( | ||
3310 | 59 | ts => '100', | ||
3311 | 60 | name => 'query-history', | ||
3312 | 61 | run_schedule => '* 8 * * 1,2,3,4,5', | ||
3313 | 62 | spool_schedule => '* 9 * * 1,2,3,4,5', | ||
3314 | 63 | tasks => [ $run0 ], | ||
3315 | 64 | ); | ||
3316 | 65 | |||
3317 | 66 | # Empty crontab, add the service. | ||
3318 | 67 | test_make_new_crontab( | ||
3319 | 68 | file => "crontab001", | ||
3320 | 69 | services => [ $svc0 ], | ||
3321 | 70 | ); | ||
3322 | 71 | |||
3323 | 72 | # Crontab has another line, add the service to it. | ||
3324 | 73 | test_make_new_crontab( | ||
3325 | 74 | file => "crontab002", | ||
3326 | 75 | services => [ $svc0 ], | ||
3327 | 76 | ); | ||
3328 | 77 | |||
3329 | 78 | # Crontab has another line and an old service, remove the old service | ||
3330 | 79 | # and add the current service. | ||
3331 | 80 | test_make_new_crontab( | ||
3332 | 81 | file => "crontab003", | ||
3333 | 82 | services => [ $svc0 ], | ||
3334 | 83 | ); | ||
3335 | 84 | |||
3336 | 85 | # Crontab has old service, remove it and add only new service. | ||
3337 | 86 | test_make_new_crontab( | ||
3338 | 87 | file => "crontab004", | ||
3339 | 88 | services => [ $svc0 ], | ||
3340 | 89 | ); | ||
3341 | 90 | |||
3342 | 91 | # ############################################################################# | ||
3343 | 92 | # Use real crontab. | ||
3344 | 93 | # ############################################################################# | ||
3345 | 94 | |||
3346 | 95 | # The previous tests pass in a crontab file to make testing easier. | ||
3347 | 96 | # Now test that make_new_crontab() will run `crontab -l' if not given | ||
3348 | 97 | # input. To test this, we add a fake line to our crontab. If | ||
3349 | 98 | # make_new_crontab() really runs `crontab -l', then this fake line | ||
3350 | 99 | # will be in the new crontab it returns. | ||
3351 | 100 | |||
3352 | 101 | my $crontab = `crontab -l 2>/dev/null`; | ||
3353 | 102 | SKIP: { | ||
3354 | 103 | skip 'Crontab is not empty', 3 if $crontab; | ||
3355 | 104 | |||
3356 | 105 | # On most systems[1], crontab lines must end with a newline, | ||
3357 | 106 | # else an error like this happens: | ||
3358 | 107 | # "/tmp/new_crontab_file":1: premature EOF | ||
3359 | 108 | # errors in crontab file, can't install. | ||
3360 | 109 | # [1] Ubuntu 10 and Mac OS X work without the newline. | ||
3361 | 110 | my ($fh, $file) = tempfile(); | ||
3362 | 111 | print {$fh} "* 0 * * * date > /dev/null\n"; | ||
3363 | 112 | close $fh or warn "Cannot close $file: $OS_ERROR"; | ||
3364 | 113 | my $output = `crontab $file 2>&1`; | ||
3365 | 114 | |||
3366 | 115 | $crontab = `crontab -l 2>&1`; | ||
3367 | 116 | |||
3368 | 117 | is( | ||
3369 | 118 | $crontab, | ||
3370 | 119 | "* 0 * * * date > /dev/null\n", | ||
3371 | 120 | "Set other crontab line" | ||
3372 | 121 | ) or diag($output); | ||
3373 | 122 | |||
3374 | 123 | unlink $file or warn "Cannot remove $file: $OS_ERROR"; | ||
3375 | 124 | |||
3376 | 125 | my $new_crontab = pt_agent::make_new_crontab( | ||
3377 | 126 | services => [ $svc0 ], | ||
3378 | 127 | bin_dir => '', | ||
3379 | 128 | ); | ||
3380 | 129 | |||
3381 | 130 | is( | ||
3382 | 131 | $new_crontab, | ||
3383 | 132 | "* 0 * * * date > /dev/null | ||
3384 | 133 | * 8 * * 1,2,3,4,5 pt-agent --run-service query-history | ||
3385 | 134 | * 9 * * 1,2,3,4,5 pt-agent --send-data query-history | ||
3386 | 135 | ", | ||
3387 | 136 | "Runs crontab -l by default" | ||
3388 | 137 | ); | ||
3389 | 138 | |||
3390 | 139 | system("crontab -r 2>/dev/null"); | ||
3391 | 140 | $crontab = `crontab -l 2>/dev/null`; | ||
3392 | 141 | is( | ||
3393 | 142 | $crontab, | ||
3394 | 143 | "", | ||
3395 | 144 | "Removed crontab" | ||
3396 | 145 | ); | ||
3397 | 146 | }; | ||
3398 | 147 | |||
3399 | 148 | # ############################################################################# | ||
3400 | 149 | # Done. | ||
3401 | 150 | # ############################################################################# | ||
3402 | 151 | done_testing; | ||
3403 | 152 | 0 | ||
3404 | === removed file 't/pt-agent/replace_special_vars.t' | |||
3405 | --- t/pt-agent/replace_special_vars.t 2013-06-17 00:28:18 +0000 | |||
3406 | +++ t/pt-agent/replace_special_vars.t 1970-01-01 00:00:00 +0000 | |||
3407 | @@ -1,73 +0,0 @@ | |||
3408 | 1 | #!/usr/bin/env perl | ||
3409 | 2 | |||
3410 | 3 | BEGIN { | ||
3411 | 4 | die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n" | ||
3412 | 5 | unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH}; | ||
3413 | 6 | unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib"; | ||
3414 | 7 | }; | ||
3415 | 8 | |||
3416 | 9 | use strict; | ||
3417 | 10 | use warnings FATAL => 'all'; | ||
3418 | 11 | use English qw(-no_match_vars); | ||
3419 | 12 | use Test::More; | ||
3420 | 13 | use JSON; | ||
3421 | 14 | use File::Temp qw(tempfile); | ||
3422 | 15 | |||
3423 | 16 | use Percona::Test; | ||
3424 | 17 | use Percona::Test::Mock::AgentLogger; | ||
3425 | 18 | require "$trunk/bin/pt-agent"; | ||
3426 | 19 | |||
3427 | 20 | Percona::Toolkit->import(qw(have_required_args Dumper)); | ||
3428 | 21 | |||
3429 | 22 | my @log; | ||
3430 | 23 | my $logger = Percona::Test::Mock::AgentLogger->new(log => \@log); | ||
3431 | 24 | pt_agent::_logger($logger); | ||
3432 | 25 | |||
3433 | 26 | my @output_files = (); | ||
3434 | 27 | my $store = {}; | ||
3435 | 28 | |||
3436 | 29 | sub test_replace { | ||
3437 | 30 | my (%args) = @_; | ||
3438 | 31 | have_required_args(\%args, qw( | ||
3439 | 32 | cmd | ||
3440 | 33 | expect | ||
3441 | 34 | )) or die; | ||
3442 | 35 | my $cmd = $args{cmd}; | ||
3443 | 36 | my $expect = $args{expect}; | ||
3444 | 37 | |||
3445 | 38 | my $new_cmd = pt_agent::replace_special_vars( | ||
3446 | 39 | cmd => $cmd, | ||
3447 | 40 | output_files => \@output_files, | ||
3448 | 41 | service => 'service-name', | ||
3449 | 42 | lib_dir => '/var/lib/pt-agent', | ||
3450 | 43 | meta_dir => '/var/lib/pt-agent/meta', | ||
3451 | 44 | stage_dir => '/var/spool/.tmp', | ||
3452 | 45 | spool_dir => '/var/spool', | ||
3453 | 46 | bin_dir => $trunk, | ||
3454 | 47 | ts => '123', | ||
3455 | 48 | store => $store, | ||
3456 | 49 | ); | ||
3457 | 50 | |||
3458 | 51 | is( | ||
3459 | 52 | $new_cmd, | ||
3460 | 53 | $expect, | ||
3461 | 54 | $cmd, | ||
3462 | 55 | ); | ||
3463 | 56 | }; | ||
3464 | 57 | |||
3465 | 58 | @output_files = qw(zero one two); | ||
3466 | 59 | test_replace( | ||
3467 | 60 | cmd => "pt-query-digest __RUN_0_OUTPUT__", | ||
3468 | 61 | expect => "pt-query-digest zero", | ||
3469 | 62 | ); | ||
3470 | 63 | |||
3471 | 64 | $store->{slow_query_log_file} = 'slow.log'; | ||
3472 | 65 | test_replace( | ||
3473 | 66 | cmd => "echo '__STORE_slow_query_log_file__' > /var/spool/pt-agent/.tmp/1371269644.rotate-slow-query-log-all-5.1.slow_query_log_file", | ||
3474 | 67 | expect => "echo 'slow.log' > /var/spool/pt-agent/.tmp/1371269644.rotate-slow-query-log-all-5.1.slow_query_log_file", | ||
3475 | 68 | ); | ||
3476 | 69 | |||
3477 | 70 | # ############################################################################# | ||
3478 | 71 | # Done. | ||
3479 | 72 | # ############################################################################# | ||
3480 | 73 | done_testing; | ||
3481 | 74 | 0 | ||
3482 | === removed file 't/pt-agent/run_agent.t' | |||
3483 | --- t/pt-agent/run_agent.t 2013-06-17 04:01:30 +0000 | |||
3484 | +++ t/pt-agent/run_agent.t 1970-01-01 00:00:00 +0000 | |||
3485 | @@ -1,527 +0,0 @@ | |||
3486 | 1 | #!/usr/bin/env perl | ||
3487 | 2 | |||
3488 | 3 | BEGIN { | ||
3489 | 4 | die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n" | ||
3490 | 5 | unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH}; | ||
3491 | 6 | unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib"; | ||
3492 | 7 | }; | ||
3493 | 8 | |||
3494 | 9 | use strict; | ||
3495 | 10 | use warnings FATAL => 'all'; | ||
3496 | 11 | use English qw(-no_match_vars); | ||
3497 | 12 | use Test::More; | ||
3498 | 13 | |||
3499 | 14 | plan skip_all => "Need to make start-service testable"; | ||
3500 | 15 | |||
3501 | 16 | use JSON; | ||
3502 | 17 | use File::Temp qw(tempdir); | ||
3503 | 18 | |||
3504 | 19 | use Percona::Test; | ||
3505 | 20 | use Sandbox; | ||
3506 | 21 | use Percona::Test::Mock::UserAgent; | ||
3507 | 22 | use Percona::Test::Mock::AgentLogger; | ||
3508 | 23 | require "$trunk/bin/pt-agent"; | ||
3509 | 24 | |||
3510 | 25 | my $dp = new DSNParser(opts=>$dsn_opts); | ||
3511 | 26 | my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp); | ||
3512 | 27 | my $dbh = $sb->get_dbh_for('master'); | ||
3513 | 28 | my $dsn = $sb->dsn_for('master'); | ||
3514 | 29 | my $o = new OptionParser(); | ||
3515 | 30 | $o->get_specs("$trunk/bin/pt-agent"); | ||
3516 | 31 | $o->get_opts(); | ||
3517 | 32 | my $cxn = Cxn->new( | ||
3518 | 33 | dsn_string => $dsn, | ||
3519 | 34 | OptionParser => $o, | ||
3520 | 35 | DSNParser => $dp, | ||
3521 | 36 | ); | ||
3522 | 37 | |||
3523 | 38 | Percona::Toolkit->import(qw(Dumper)); | ||
3524 | 39 | Percona::WebAPI::Representation->import(qw(as_hashref)); | ||
3525 | 40 | |||
3526 | 41 | # Running the agent is going to cause it to schedule the services, | ||
3527 | 42 | # i.e. write a real crontab. The test box/user shouldn't have a | ||
3528 | 43 | # crontab, so we'll warn and clobber it if there is one. | ||
3529 | 44 | my $crontab = `crontab -l 2>/dev/null`; | ||
3530 | 45 | if ( $crontab ) { | ||
3531 | 46 | warn "Removing crontab: $crontab\n"; | ||
3532 | 47 | `crontab -r`; | ||
3533 | 48 | } | ||
3534 | 49 | |||
3535 | 50 | # Fake --lib and --spool dirs. | ||
3536 | 51 | my $tmpdir = tempdir("/tmp/pt-agent.$PID.XXXXXX"); #, CLEANUP => 1); | ||
3537 | 52 | mkdir "$tmpdir/spool" or die "Error making $tmpdir/spool: $OS_ERROR"; | ||
3538 | 53 | |||
3539 | 54 | my @log; | ||
3540 | 55 | my $logger = Percona::Test::Mock::AgentLogger->new(log => \@log); | ||
3541 | 56 | pt_agent::_logger($logger); | ||
3542 | 57 | |||
3543 | 58 | # ############################################################################# | ||
3544 | 59 | # Create mock client and Agent | ||
3545 | 60 | # ############################################################################# | ||
3546 | 61 | |||
3547 | 62 | # These aren't the real tests yet: to run_agent, first we need | ||
3548 | 63 | # a client and Agent, so create mock ones. | ||
3549 | 64 | |||
3550 | 65 | my $output; | ||
3551 | 66 | my $json = JSON->new->canonical([1])->pretty; | ||
3552 | 67 | $json->allow_blessed([]); | ||
3553 | 68 | $json->convert_blessed([]); | ||
3554 | 69 | |||
3555 | 70 | my $ua = Percona::Test::Mock::UserAgent->new( | ||
3556 | 71 | encode => sub { my $c = shift; return $json->encode($c || {}) }, | ||
3557 | 72 | ); | ||
3558 | 73 | |||
3559 | 74 | my $client = eval { | ||
3560 | 75 | Percona::WebAPI::Client->new( | ||
3561 | 76 | api_key => '123', | ||
3562 | 77 | ua => $ua, | ||
3563 | 78 | ); | ||
3564 | 79 | }; | ||
3565 | 80 | |||
3566 | 81 | is( | ||
3567 | 82 | $EVAL_ERROR, | ||
3568 | 83 | '', | ||
3569 | 84 | 'Create mock client' | ||
3570 | 85 | ) or die; | ||
3571 | 86 | |||
3572 | 87 | my $agent = Percona::WebAPI::Resource::Agent->new( | ||
3573 | 88 | uuid => '123', | ||
3574 | 89 | hostname => 'host', | ||
3575 | 90 | username => 'user', | ||
3576 | 91 | links => { | ||
3577 | 92 | self => '/agents/123', | ||
3578 | 93 | config => '/agents/123/config', | ||
3579 | 94 | }, | ||
3580 | 95 | ); | ||
3581 | 96 | |||
3582 | 97 | my $daemon = Daemon->new( | ||
3583 | 98 | daemonzie => 0, | ||
3584 | 99 | ); | ||
3585 | 100 | |||
3586 | 101 | my @wait; | ||
3587 | 102 | my $interval = sub { | ||
3588 | 103 | my $t = shift; | ||
3589 | 104 | push @wait, $t; | ||
3590 | 105 | print "interval=" . (defined $t ? $t : 'undef') . "\n"; | ||
3591 | 106 | }; | ||
3592 | 107 | |||
3593 | 108 | # ############################################################################# | ||
3594 | 109 | # Test run_agent | ||
3595 | 110 | # ############################################################################# | ||
3596 | 111 | |||
3597 | 112 | my $config = Percona::WebAPI::Resource::Config->new( | ||
3598 | 113 | ts => 1363720060, | ||
3599 | 114 | name => 'Default', | ||
3600 | 115 | options => { | ||
3601 | 116 | 'lib' => $tmpdir, # required | ||
3602 | 117 | 'spool' => "$tmpdir/spool", # required | ||
3603 | 118 | 'check-interval' => "11", | ||
3604 | 119 | }, | ||
3605 | 120 | links => { | ||
3606 | 121 | self => '/agents/123/config', | ||
3607 | 122 | services => '/agents/123/services', | ||
3608 | 123 | }, | ||
3609 | 124 | ); | ||
3610 | 125 | |||
3611 | 126 | my $run0 = Percona::WebAPI::Resource::Task->new( | ||
3612 | 127 | name => 'query-history', | ||
3613 | 128 | number => '0', | ||
3614 | 129 | program => 'pt-query-digest', | ||
3615 | 130 | options => '--output json', | ||
3616 | 131 | output => 'spool', | ||
3617 | 132 | ); | ||
3618 | 133 | |||
3619 | 134 | my $svc0 = Percona::WebAPI::Resource::Service->new( | ||
3620 | 135 | ts => 100, | ||
3621 | 136 | name => 'query-history', | ||
3622 | 137 | run_schedule => '1 * * * *', | ||
3623 | 138 | spool_schedule => '2 * * * *', | ||
3624 | 139 | tasks => [ $run0 ], | ||
3625 | 140 | links => { | ||
3626 | 141 | self => '/query-history', | ||
3627 | 142 | data => '/query-history/data', | ||
3628 | 143 | }, | ||
3629 | 144 | ); | ||
3630 | 145 | |||
3631 | 146 | my $run1 = Percona::WebAPI::Resource::Task->new( | ||
3632 | 147 | name => 'start-query-history', | ||
3633 | 148 | number => '0', | ||
3634 | 149 | program => 'echo "start-qh"', | ||
3635 | 150 | ); | ||
3636 | 151 | |||
3637 | 152 | my $start_qh = Percona::WebAPI::Resource::Service->new( | ||
3638 | 153 | ts => '100', | ||
3639 | 154 | name => 'start-query-history', | ||
3640 | 155 | meta => 1, | ||
3641 | 156 | tasks => [ $run1 ], | ||
3642 | 157 | links => { | ||
3643 | 158 | self => '/query-history', | ||
3644 | 159 | data => '/query-history/data', | ||
3645 | 160 | }, | ||
3646 | 161 | ); | ||
3647 | 162 | |||
3648 | 163 | $ua->{responses}->{get} = [ | ||
3649 | 164 | { | ||
3650 | 165 | headers => { 'X-Percona-Resource-Type' => 'Config' }, | ||
3651 | 166 | content => as_hashref($config, with_links => 1), | ||
3652 | 167 | }, | ||
3653 | 168 | { | ||
3654 | 169 | headers => { 'X-Percona-Resource-Type' => 'Service' }, | ||
3655 | 170 | content => [ | ||
3656 | 171 | as_hashref($start_qh, with_links => 1), | ||
3657 | 172 | as_hashref($svc0, with_links => 1), | ||
3658 | 173 | ], | ||
3659 | 174 | }, | ||
3660 | 175 | ]; | ||
3661 | 176 | |||
3662 | 177 | my $safeguards = Safeguards->new( | ||
3663 | 178 | disk_bytes_free => 1024, | ||
3664 | 179 | disk_pct_free => 1, | ||
3665 | 180 | ); | ||
3666 | 181 | |||
3667 | 182 | # The only thing pt-agent must have is the API key in the config file, | ||
3668 | 183 | # everything else relies on defaults until the first Config is gotten | ||
3669 | 184 | # from Percona. | ||
3670 | 185 | my $config_file = pt_agent::get_config_file(); | ||
3671 | 186 | unlink $config_file if -f $config_file; | ||
3672 | 187 | |||
3673 | 188 | like( | ||
3674 | 189 | $config_file, | ||
3675 | 190 | qr/$ENV{LOGNAME}\/\.pt-agent.conf$/, | ||
3676 | 191 | "Default config file is ~/.pt-agent.config" | ||
3677 | 192 | ); | ||
3678 | 193 | |||
3679 | 194 | pt_agent::write_config( | ||
3680 | 195 | config => $config | ||
3681 | 196 | ); | ||
3682 | 197 | |||
3683 | 198 | diag(`echo 'api-key=123' >> $config_file`); | ||
3684 | 199 | |||
3685 | 200 | is( | ||
3686 | 201 | `cat $config_file`, | ||
3687 | 202 | "check-interval=11\nlib=$tmpdir\nspool=$tmpdir/spool\napi-key=123\n", | ||
3688 | 203 | "Write Config to config file" | ||
3689 | 204 | ); | ||
3690 | 205 | |||
3691 | 206 | pt_agent::save_agent( | ||
3692 | 207 | agent => $agent, | ||
3693 | 208 | lib_dir => $tmpdir, | ||
3694 | 209 | ); | ||
3695 | 210 | |||
3696 | 211 | my @ok_code = (); # callbacks | ||
3697 | 212 | my @oktorun = ( | ||
3698 | 213 | 1, # 1st main loop check | ||
3699 | 214 | 0, # 2nd main loop check | ||
3700 | 215 | ); | ||
3701 | 216 | my $oktorun = sub { | ||
3702 | 217 | my $ok = shift @oktorun; | ||
3703 | 218 | print "oktorun=" . (defined $ok ? $ok : 'undef') . "\n"; | ||
3704 | 219 | my $code = shift @ok_code; | ||
3705 | 220 | $code->() if $code; | ||
3706 | 221 | return $ok | ||
3707 | 222 | }; | ||
3708 | 223 | |||
3709 | 224 | @wait = (); | ||
3710 | 225 | |||
3711 | 226 | $output = output( | ||
3712 | 227 | sub { | ||
3713 | 228 | pt_agent::run_agent( | ||
3714 | 229 | # Required args | ||
3715 | 230 | agent => $agent, | ||
3716 | 231 | client => $client, | ||
3717 | 232 | daemon => $daemon, | ||
3718 | 233 | interval => $interval, | ||
3719 | 234 | lib_dir => $tmpdir, | ||
3720 | 235 | safeguards => $safeguards, | ||
3721 | 236 | Cxn => $cxn, | ||
3722 | 237 | # Optional args, for testing | ||
3723 | 238 | oktorun => $oktorun, | ||
3724 | 239 | json => $json, | ||
3725 | 240 | bin_dir => "$trunk/bin", | ||
3726 | 241 | ); | ||
3727 | 242 | }, | ||
3728 | 243 | stderr => 1, | ||
3729 | 244 | ); | ||
3730 | 245 | |||
3731 | 246 | is( | ||
3732 | 247 | scalar @wait, | ||
3733 | 248 | 1, | ||
3734 | 249 | "Called interval once" | ||
3735 | 250 | ); | ||
3736 | 251 | |||
3737 | 252 | is( | ||
3738 | 253 | $wait[0], | ||
3739 | 254 | 11, | ||
3740 | 255 | "... used Config->options->check-interval" | ||
3741 | 256 | ); | ||
3742 | 257 | |||
3743 | 258 | ok( | ||
3744 | 259 | -f "$tmpdir/services/query-history", | ||
3745 | 260 | "Created services/query-history" | ||
3746 | 261 | ) or diag($output); | ||
3747 | 262 | |||
3748 | 263 | chomp(my $n_files = `ls -1 $tmpdir/services| wc -l | awk '{print \$1}'`); | ||
3749 | 264 | is( | ||
3750 | 265 | $n_files, | ||
3751 | 266 | 2, | ||
3752 | 267 | "... created services/query-history and services/start-query-history" | ||
3753 | 268 | ); | ||
3754 | 269 | |||
3755 | 270 | ok( | ||
3756 | 271 | no_diff( | ||
3757 | 272 | "cat $tmpdir/services/query-history", | ||
3758 | 273 | "t/pt-agent/samples/service001", | ||
3759 | 274 | ), | ||
3760 | 275 | "query-history service file" | ||
3761 | 276 | ); | ||
3762 | 277 | |||
3763 | 278 | $crontab = `crontab -l 2>/dev/null`; | ||
3764 | 279 | like( | ||
3765 | 280 | $crontab, | ||
3766 | 281 | qr/pt-agent --run-service query-history$/m, | ||
3767 | 282 | "Scheduled --run-service with crontab" | ||
3768 | 283 | ) or diag(Dumper(\@log)); | ||
3769 | 284 | |||
3770 | 285 | like( | ||
3771 | 286 | $crontab, | ||
3772 | 287 | qr/pt-agent --send-data query-history$/m, | ||
3773 | 288 | "Scheduled --send-data with crontab" | ||
3774 | 289 | ) or diag(Dumper(\@log)); | ||
3775 | 290 | exit; | ||
3776 | 291 | # ############################################################################# | ||
3777 | 292 | # Run run_agent again, like the agent had been stopped and restarted. | ||
3778 | 293 | # ############################################################################# | ||
3779 | 294 | |||
3780 | 295 | $ua->{responses}->{get} = [ | ||
3781 | 296 | # First check, fail | ||
3782 | 297 | { | ||
3783 | 298 | code => 500, | ||
3784 | 299 | }, | ||
3785 | 300 | # interval | ||
3786 | 301 | # 2nd check, init with latest Config and Services | ||
3787 | 302 | { | ||
3788 | 303 | headers => { 'X-Percona-Resource-Type' => 'Config' }, | ||
3789 | 304 | content => as_hashref($config, with_links => 1), | ||
3790 | 305 | }, | ||
3791 | 306 | { | ||
3792 | 307 | headers => { 'X-Percona-Resource-Type' => 'Service' }, | ||
3793 | 308 | content => [ as_hashref($svc0, with_links => 1) ], | ||
3794 | 309 | }, | ||
3795 | 310 | # interval | ||
3796 | 311 | # 3rd check, same Config and Services so nothing to do | ||
3797 | 312 | { | ||
3798 | 313 | headers => { 'X-Percona-Resource-Type' => 'Config' }, | ||
3799 | 314 | content => as_hashref($config, with_links => 1), | ||
3800 | 315 | }, | ||
3801 | 316 | { | ||
3802 | 317 | headers => { 'X-Percona-Resource-Type' => 'Service' }, | ||
3803 | 318 | content => [ as_hashref($svc0, with_links => 1) ], | ||
3804 | 319 | }, | ||
3805 | 320 | # interval, oktorun=0 | ||
3806 | 321 | ]; | ||
3807 | 322 | |||
3808 | 323 | @oktorun = ( | ||
3809 | 324 | 1, # 1st main loop check | ||
3810 | 325 | # First check, error 500 | ||
3811 | 326 | 1, # 2nd main loop check | ||
3812 | 327 | # Init with latest Config and Services | ||
3813 | 328 | 1, # 3rd main loop check | ||
3814 | 329 | # Same Config and services | ||
3815 | 330 | 0, # 4th main loop check | ||
3816 | 331 | ); | ||
3817 | 332 | |||
3818 | 333 | # Before the 3rd check, remove the config file (~/.pt-agent.conf) and | ||
3819 | 334 | # query-history service file. When the tool re-GETs these, they'll be | ||
3820 | 335 | # the same so it won't recreate them. A bug here will cause these files to | ||
3821 | 336 | # exist again after running. | ||
3822 | 337 | $ok_code[2] = sub { | ||
3823 | 338 | unlink "$config_file"; | ||
3824 | 339 | unlink "$tmpdir/services/query-history"; | ||
3825 | 340 | Percona::Test::wait_until(sub { ! -f "$config_file" }); | ||
3826 | 341 | Percona::Test::wait_until(sub { ! -f "$tmpdir/services/query-history" }); | ||
3827 | 342 | }; | ||
3828 | 343 | |||
3829 | 344 | @wait = (); | ||
3830 | 345 | |||
3831 | 346 | $output = output( | ||
3832 | 347 | sub { | ||
3833 | 348 | pt_agent::run_agent( | ||
3834 | 349 | # Required args | ||
3835 | 350 | agent => $agent, | ||
3836 | 351 | client => $client, | ||
3837 | 352 | daemon => $daemon, | ||
3838 | 353 | interval => $interval, | ||
3839 | 354 | lib_dir => $tmpdir, | ||
3840 | 355 | Cxn => $cxn, | ||
3841 | 356 | # Optional args, for testing | ||
3842 | 357 | oktorun => $oktorun, | ||
3843 | 358 | json => $json, | ||
3844 | 359 | ); | ||
3845 | 360 | }, | ||
3846 | 361 | stderr => 1, | ||
3847 | 362 | ); | ||
3848 | 363 | |||
3849 | 364 | is_deeply( | ||
3850 | 365 | \@wait, | ||
3851 | 366 | [ 60, 11, 11 ], | ||
3852 | 367 | "Got Config after error" | ||
3853 | 368 | ) or diag(Dumper(\@wait)); | ||
3854 | 369 | |||
3855 | 370 | ok( | ||
3856 | 371 | ! -f "$config_file", | ||
3857 | 372 | "No Config diff, no config file change" | ||
3858 | 373 | ); | ||
3859 | 374 | |||
3860 | 375 | ok( | ||
3861 | 376 | ! -f "$tmpdir/services/query-history", | ||
3862 | 377 | "No Service diff, no service file changes" | ||
3863 | 378 | ); | ||
3864 | 379 | |||
3865 | 380 | my $new_crontab = `crontab -l 2>/dev/null`; | ||
3866 | 381 | is( | ||
3867 | 382 | $new_crontab, | ||
3868 | 383 | $crontab, | ||
3869 | 384 | "Crontab is the same" | ||
3870 | 385 | ); | ||
3871 | 386 | |||
3872 | 387 | # ############################################################################# | ||
3873 | 388 | # Test a run_once_on_start service | ||
3874 | 389 | # ############################################################################# | ||
3875 | 390 | |||
3876 | 391 | diag(`rm -f $tmpdir/* >/dev/null 2>&1`); | ||
3877 | 392 | diag(`rm -rf $tmpdir/services/*`); | ||
3878 | 393 | diag(`rm -rf $tmpdir/spool/*`); | ||
3879 | 394 | |||
3880 | 395 | # When pt-agent manually runs --run-service test-run-at-start, it's going | ||
3881 | 396 | # to need an API key because it doesn't call its own run_service(), it runs | ||
3882 | 397 | # another instance of itself with system(). So put the fake API key in | ||
3883 | 398 | # the default config file. | ||
3884 | 399 | unlink $config_file if -f $config_file; | ||
3885 | 400 | diag(`echo "api-key=123" > $config_file`); | ||
3886 | 401 | |||
3887 | 402 | $config = Percona::WebAPI::Resource::Config->new( | ||
3888 | 403 | ts => 1363720060, | ||
3889 | 404 | name => 'Test run_once_on_start', | ||
3890 | 405 | options => { | ||
3891 | 406 | 'check-interval' => "15", | ||
3892 | 407 | 'lib' => $tmpdir, | ||
3893 | 408 | 'spool' => "$tmpdir/spool", | ||
3894 | 409 | 'pid' => "$tmpdir/pid", | ||
3895 | 410 | 'log' => "$tmpdir/log" | ||
3896 | 411 | }, | ||
3897 | 412 | links => { | ||
3898 | 413 | self => '/agents/123/config', | ||
3899 | 414 | services => '/agents/123/services', | ||
3900 | 415 | }, | ||
3901 | 416 | ); | ||
3902 | 417 | |||
3903 | 418 | $run0 = Percona::WebAPI::Resource::Task->new( | ||
3904 | 419 | name => 'run-at-start', | ||
3905 | 420 | number => '0', | ||
3906 | 421 | program => 'date', | ||
3907 | 422 | output => 'spool', | ||
3908 | 423 | ); | ||
3909 | 424 | |||
3910 | 425 | $svc0 = Percona::WebAPI::Resource::Service->new( | ||
3911 | 426 | ts => 100, | ||
3912 | 427 | name => 'test-run-at-start', | ||
3913 | 428 | run_schedule => '0 0 1 1 *', | ||
3914 | 429 | run_once => 1, # here's the magic | ||
3915 | 430 | tasks => [ $run0 ], | ||
3916 | 431 | links => { | ||
3917 | 432 | self => '/query-history', | ||
3918 | 433 | data => '/query-history/data', | ||
3919 | 434 | }, | ||
3920 | 435 | ); | ||
3921 | 436 | |||
3922 | 437 | $ua->{responses}->{get} = [ | ||
3923 | 438 | { | ||
3924 | 439 | headers => { 'X-Percona-Resource-Type' => 'Config' }, | ||
3925 | 440 | content => as_hashref($config, with_links => 1), | ||
3926 | 441 | }, | ||
3927 | 442 | { | ||
3928 | 443 | headers => { 'X-Percona-Resource-Type' => 'Service' }, | ||
3929 | 444 | content => [ as_hashref($svc0, with_links => 1) ], | ||
3930 | 445 | }, | ||
3931 | 446 | { | ||
3932 | 447 | headers => { 'X-Percona-Resource-Type' => 'Config' }, | ||
3933 | 448 | content => as_hashref($config, with_links => 1), | ||
3934 | 449 | }, | ||
3935 | 450 | { | ||
3936 | 451 | headers => { 'X-Percona-Resource-Type' => 'Service' }, | ||
3937 | 452 | content => [ as_hashref($svc0, with_links => 1) ], | ||
3938 | 453 | }, | ||
3939 | 454 | ]; | ||
3940 | 455 | |||
3941 | 456 | @wait = (); | ||
3942 | 457 | @ok_code = (); # callbacks | ||
3943 | 458 | @oktorun = ( | ||
3944 | 459 | 1, # 1st main loop check | ||
3945 | 460 | # Run once | ||
3946 | 461 | 1, # 2nd main loop check | ||
3947 | 462 | # Don't run it again | ||
3948 | 463 | 0, # 3d main loop check | ||
3949 | 464 | ); | ||
3950 | 465 | |||
3951 | 466 | $output = output( | ||
3952 | 467 | sub { | ||
3953 | 468 | pt_agent::run_agent( | ||
3954 | 469 | # Required args | ||
3955 | 470 | agent => $agent, | ||
3956 | 471 | client => $client, | ||
3957 | 472 | daemon => $daemon, | ||
3958 | 473 | interval => $interval, | ||
3959 | 474 | lib_dir => $tmpdir, | ||
3960 | 475 | Cxn => $cxn, | ||
3961 | 476 | # Optional args, for testing | ||
3962 | 477 | oktorun => $oktorun, | ||
3963 | 478 | json => $json, | ||
3964 | 479 | bin_dir => "$trunk/bin/", | ||
3965 | 480 | ); | ||
3966 | 481 | }, | ||
3967 | 482 | stderr => 1, | ||
3968 | 483 | ); | ||
3969 | 484 | |||
3970 | 485 | Percona::Test::wait_for_files("$tmpdir/spool/test-run-at-start/test-run-at-start"); | ||
3971 | 486 | |||
3972 | 487 | like( | ||
3973 | 488 | $output, | ||
3974 | 489 | qr/Starting test-run-at-start service/, | ||
3975 | 490 | "Ran service on start" | ||
3976 | 491 | ); | ||
3977 | 492 | |||
3978 | 493 | my @runs = $output =~ m/Starting test-run-at-start service/g; | ||
3979 | 494 | |||
3980 | 495 | is( | ||
3981 | 496 | scalar @runs, | ||
3982 | 497 | 1, | ||
3983 | 498 | "... only ran it once" | ||
3984 | 499 | ); | ||
3985 | 500 | |||
3986 | 501 | chomp($output = `cat $tmpdir/spool/test-run-at-start/test-run-at-start 2>/dev/null`); | ||
3987 | 502 | ok( | ||
3988 | 503 | $output, | ||
3989 | 504 | "... service ran at start" | ||
3990 | 505 | ) or diag($output); | ||
3991 | 506 | |||
3992 | 507 | chomp($output = `crontab -l`); | ||
3993 | 508 | unlike( | ||
3994 | 509 | $output, | ||
3995 | 510 | qr/--run-service test-run-at-start/, | ||
3996 | 511 | "... service was not scheduled" | ||
3997 | 512 | ); | ||
3998 | 513 | |||
3999 | 514 | # ############################################################################# | ||
4000 | 515 | # Done. | ||
4001 | 516 | # ############################################################################# | ||
4002 | 517 | |||
4003 | 518 | # This shouldn't cause an error, but if it does, let it show up | ||
4004 | 519 | # in the results as an error. | ||
4005 | 520 | `crontab -r`; | ||
4006 | 521 | |||
4007 | 522 | if ( -f $config_file ) { | ||
4008 | 523 | unlink $config_file | ||
4009 | 524 | or warn "Error removing $config_file: $OS_ERROR"; | ||
4010 | 525 | } | ||
4011 | 526 | |||
4012 | 527 | done_testing; | ||
4013 | 528 | 0 | ||
4014 | === removed file 't/pt-agent/run_service.t' | |||
4015 | --- t/pt-agent/run_service.t 2013-06-17 00:28:18 +0000 | |||
4016 | +++ t/pt-agent/run_service.t 1970-01-01 00:00:00 +0000 | |||
4017 | @@ -1,503 +0,0 @@ | |||
4018 | 1 | #!/usr/bin/env perl | ||
4019 | 2 | |||
4020 | 3 | BEGIN { | ||
4021 | 4 | die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n" | ||
4022 | 5 | unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH}; | ||
4023 | 6 | unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib"; | ||
4024 | 7 | }; | ||
4025 | 8 | |||
4026 | 9 | use strict; | ||
4027 | 10 | use warnings FATAL => 'all'; | ||
4028 | 11 | use English qw(-no_match_vars); | ||
4029 | 12 | use Test::More; | ||
4030 | 13 | use JSON; | ||
4031 | 14 | use File::Temp qw(tempdir); | ||
4032 | 15 | |||
4033 | 16 | $ENV{PTTEST_PRETTY_JSON} = 1; | ||
4034 | 17 | |||
4035 | 18 | use Percona::Test; | ||
4036 | 19 | use Sandbox; | ||
4037 | 20 | use Percona::Test::Mock::UserAgent; | ||
4038 | 21 | use Percona::Test::Mock::AgentLogger; | ||
4039 | 22 | require "$trunk/bin/pt-agent"; | ||
4040 | 23 | |||
4041 | 24 | my $dp = new DSNParser(opts=>$dsn_opts); | ||
4042 | 25 | my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp); | ||
4043 | 26 | my $dbh = $sb->get_dbh_for('master'); | ||
4044 | 27 | my $dsn = $sb->dsn_for('master'); | ||
4045 | 28 | my $o = new OptionParser(); | ||
4046 | 29 | $o->get_specs("$trunk/bin/pt-agent"); | ||
4047 | 30 | $o->get_opts(); | ||
4048 | 31 | |||
4049 | 32 | Percona::Toolkit->import(qw(Dumper have_required_args)); | ||
4050 | 33 | Percona::WebAPI::Representation->import(qw(as_hashref)); | ||
4051 | 34 | |||
4052 | 35 | my @log; | ||
4053 | 36 | my $logger = Percona::Test::Mock::AgentLogger->new(log => \@log); | ||
4054 | 37 | pt_agent::_logger($logger); | ||
4055 | 38 | |||
4056 | 39 | my $sample = "t/pt-agent/samples"; | ||
4057 | 40 | |||
4058 | 41 | # Create fake spool and lib dirs. Service-related subs in pt-agent | ||
4059 | 42 | # automatically add "/services" to the lib dir, but the spool dir is | ||
4060 | 43 | # used as-is. | ||
4061 | 44 | my $tmpdir = tempdir("/tmp/pt-agent.$PID.XXXXXX", CLEANUP => 1); | ||
4062 | 45 | output( | ||
4063 | 46 | sub { pt_agent::init_lib_dir(lib_dir => $tmpdir) } | ||
4064 | 47 | ); | ||
4065 | 48 | my $spool_dir = "$tmpdir/spool"; | ||
4066 | 49 | |||
4067 | 50 | sub write_svc_files { | ||
4068 | 51 | my (%args) = @_; | ||
4069 | 52 | have_required_args(\%args, qw( | ||
4070 | 53 | services | ||
4071 | 54 | )) or die; | ||
4072 | 55 | my $services = $args{services}; | ||
4073 | 56 | |||
4074 | 57 | my $output = output( | ||
4075 | 58 | sub { | ||
4076 | 59 | pt_agent::write_services( | ||
4077 | 60 | sorted_services => { added => $services }, | ||
4078 | 61 | lib_dir => $tmpdir, | ||
4079 | 62 | ); | ||
4080 | 63 | }, | ||
4081 | 64 | stderr => 1, | ||
4082 | 65 | die => 1, | ||
4083 | 66 | ); | ||
4084 | 67 | } | ||
4085 | 68 | |||
4086 | 69 | # ############################################################################# | ||
4087 | 70 | # Create mock client and Agent | ||
4088 | 71 | # ############################################################################# | ||
4089 | 72 | |||
4090 | 73 | my $json = JSON->new->canonical([1])->pretty; | ||
4091 | 74 | $json->allow_blessed([]); | ||
4092 | 75 | $json->convert_blessed([]); | ||
4093 | 76 | |||
4094 | 77 | my $ua = Percona::Test::Mock::UserAgent->new( | ||
4095 | 78 | encode => sub { my $c = shift; return $json->encode($c || {}) }, | ||
4096 | 79 | ); | ||
4097 | 80 | |||
4098 | 81 | # Create cilent, get entry links | ||
4099 | 82 | my $links = { | ||
4100 | 83 | agents => '/agents', | ||
4101 | 84 | config => '/agents/1/config', | ||
4102 | 85 | services => '/agents/1/services', | ||
4103 | 86 | 'query-history' => '/query-history', | ||
4104 | 87 | }; | ||
4105 | 88 | |||
4106 | 89 | $ua->{responses}->{get} = [ | ||
4107 | 90 | { | ||
4108 | 91 | content => $links, | ||
4109 | 92 | }, | ||
4110 | 93 | ]; | ||
4111 | 94 | |||
4112 | 95 | my $client = eval { | ||
4113 | 96 | Percona::WebAPI::Client->new( | ||
4114 | 97 | api_key => '123', | ||
4115 | 98 | ua => $ua, | ||
4116 | 99 | ); | ||
4117 | 100 | }; | ||
4118 | 101 | is( | ||
4119 | 102 | $EVAL_ERROR, | ||
4120 | 103 | '', | ||
4121 | 104 | 'Create mock client' | ||
4122 | 105 | ) or die; | ||
4123 | 106 | |||
4124 | 107 | my $agent = Percona::WebAPI::Resource::Agent->new( | ||
4125 | 108 | uuid => '123', | ||
4126 | 109 | hostname => 'prod1', | ||
4127 | 110 | links => $links, | ||
4128 | 111 | ); | ||
4129 | 112 | |||
4130 | 113 | is_deeply( | ||
4131 | 114 | as_hashref($agent), | ||
4132 | 115 | { | ||
4133 | 116 | uuid => '123', | ||
4134 | 117 | hostname => 'prod1', | ||
4135 | 118 | }, | ||
4136 | 119 | 'Create mock Agent' | ||
4137 | 120 | ) or die; | ||
4138 | 121 | |||
4139 | 122 | # ############################################################################# | ||
4140 | 123 | # Simple single task service using a program. | ||
4141 | 124 | # ############################################################################# | ||
4142 | 125 | |||
4143 | 126 | my $run0 = Percona::WebAPI::Resource::Task->new( | ||
4144 | 127 | name => 'query-history', | ||
4145 | 128 | number => '0', | ||
4146 | 129 | program => "__BIN_DIR__/pt-query-digest --output json $trunk/t/lib/samples/slowlogs/slow008.txt", | ||
4147 | 130 | output => 'spool', | ||
4148 | 131 | ); | ||
4149 | 132 | |||
4150 | 133 | my $svc0 = Percona::WebAPI::Resource::Service->new( | ||
4151 | 134 | ts => 100, | ||
4152 | 135 | name => 'query-history', | ||
4153 | 136 | run_schedule => '1 * * * *', | ||
4154 | 137 | spool_schedule => '2 * * * *', | ||
4155 | 138 | tasks => [ $run0 ], | ||
4156 | 139 | ); | ||
4157 | 140 | |||
4158 | 141 | write_svc_files( | ||
4159 | 142 | services => [ $svc0 ], | ||
4160 | 143 | ); | ||
4161 | 144 | |||
4162 | 145 | $ua->{responses}->{get} = [ | ||
4163 | 146 | { | ||
4164 | 147 | headers => { 'X-Percona-Resource-Type' => 'Agent' }, | ||
4165 | 148 | content => as_hashref($agent, with_links => 1), | ||
4166 | 149 | }, | ||
4167 | 150 | ]; | ||
4168 | 151 | |||
4169 | 152 | my $exit_status; | ||
4170 | 153 | my $output = output( | ||
4171 | 154 | sub { | ||
4172 | 155 | $exit_status = pt_agent::run_service( | ||
4173 | 156 | api_key => '123', | ||
4174 | 157 | service => 'query-history', | ||
4175 | 158 | lib_dir => $tmpdir, | ||
4176 | 159 | spool_dir => $spool_dir, | ||
4177 | 160 | Cxn => '', | ||
4178 | 161 | # for testing: | ||
4179 | 162 | client => $client, | ||
4180 | 163 | agent => $agent, | ||
4181 | 164 | entry_links => $links, | ||
4182 | 165 | prefix => '1', | ||
4183 | 166 | json => $json, | ||
4184 | 167 | bin_dir => "$trunk/bin", | ||
4185 | 168 | ); | ||
4186 | 169 | }, | ||
4187 | 170 | ); | ||
4188 | 171 | |||
4189 | 172 | ok( | ||
4190 | 173 | no_diff( | ||
4191 | 174 | "cat $tmpdir/spool/query-history/1.query-history.data", | ||
4192 | 175 | "$sample/query-history/data001.json", | ||
4193 | 176 | post_pipe => 'grep -v \'"name" :\'', | ||
4194 | 177 | ), | ||
4195 | 178 | "1 run: spool data (query-history/data001.json)" | ||
4196 | 179 | ) or diag( | ||
4197 | 180 | `ls -l $tmpdir/spool/query-history/`, | ||
4198 | 181 | `cat $tmpdir/logs/query-history.run`, | ||
4199 | 182 | Dumper(\@log) | ||
4200 | 183 | ); | ||
4201 | 184 | |||
4202 | 185 | chomp(my $n_files = `ls -1 $spool_dir/query-history/*.data | wc -l | awk '{print \$1}'`); | ||
4203 | 186 | is( | ||
4204 | 187 | $n_files, | ||
4205 | 188 | 1, | ||
4206 | 189 | "1 run: only wrote spool data" | ||
4207 | 190 | ) or diag(`ls -l $spool_dir`); | ||
4208 | 191 | |||
4209 | 192 | is( | ||
4210 | 193 | $exit_status, | ||
4211 | 194 | 0, | ||
4212 | 195 | "1 run: exit 0" | ||
4213 | 196 | ); | ||
4214 | 197 | |||
4215 | 198 | ok( | ||
4216 | 199 | -f "$tmpdir/spool/query-history/1.query-history.meta", | ||
4217 | 200 | "1 run: .meta file exists" | ||
4218 | 201 | ); | ||
4219 | 202 | |||
4220 | 203 | # ############################################################################# | ||
4221 | 204 | # Service with two task, both using a program. | ||
4222 | 205 | # ############################################################################# | ||
4223 | 206 | |||
4224 | 207 | diag(`rm -rf $tmpdir/spool/* $tmpdir/services/*`); | ||
4225 | 208 | @log = (); | ||
4226 | 209 | |||
4227 | 210 | # The result is the same as the previous single-run test, but instead of | ||
4228 | 211 | # having pqd read the slowlog directly, we have the first run cat the | ||
4229 | 212 | # log to a tmp file which pt-agent should auto-create. Then pqd in run1 | ||
4230 | 213 | # references this tmp file. | ||
4231 | 214 | |||
4232 | 215 | $run0 = Percona::WebAPI::Resource::Task->new( | ||
4233 | 216 | name => 'cat-slow-log', | ||
4234 | 217 | number => '0', | ||
4235 | 218 | program => "cat $trunk/t/lib/samples/slowlogs/slow008.txt", | ||
4236 | 219 | output => 'tmp', | ||
4237 | 220 | ); | ||
4238 | 221 | |||
4239 | 222 | my $run1 = Percona::WebAPI::Resource::Task->new( | ||
4240 | 223 | name => 'query-history', | ||
4241 | 224 | number => '1', | ||
4242 | 225 | program => "__BIN_DIR__/pt-query-digest --output json __RUN_0_OUTPUT__", | ||
4243 | 226 | output => 'spool', | ||
4244 | 227 | ); | ||
4245 | 228 | |||
4246 | 229 | $svc0 = Percona::WebAPI::Resource::Service->new( | ||
4247 | 230 | ts => 100, | ||
4248 | 231 | name => 'query-history', | ||
4249 | 232 | run_schedule => '3 * * * *', | ||
4250 | 233 | spool_schedule => '4 * * * *', | ||
4251 | 234 | tasks => [ $run0, $run1 ], | ||
4252 | 235 | ); | ||
4253 | 236 | |||
4254 | 237 | write_svc_files( | ||
4255 | 238 | services => [ $svc0 ], | ||
4256 | 239 | ); | ||
4257 | 240 | |||
4258 | 241 | $ua->{responses}->{get} = [ | ||
4259 | 242 | { | ||
4260 | 243 | headers => { 'X-Percona-Resource-Type' => 'Agent' }, | ||
4261 | 244 | content => as_hashref($agent, with_links => 1), | ||
4262 | 245 | }, | ||
4263 | 246 | ]; | ||
4264 | 247 | |||
4265 | 248 | $output = output( | ||
4266 | 249 | sub { | ||
4267 | 250 | $exit_status = pt_agent::run_service( | ||
4268 | 251 | api_key => '123', | ||
4269 | 252 | service => 'query-history', | ||
4270 | 253 | spool_dir => $spool_dir, | ||
4271 | 254 | lib_dir => $tmpdir, | ||
4272 | 255 | Cxn => '', | ||
4273 | 256 | # for testing: | ||
4274 | 257 | client => $client, | ||
4275 | 258 | agent => $agent, | ||
4276 | 259 | entry_links => $links, | ||
4277 | 260 | prefix => '2', | ||
4278 | 261 | json => $json, | ||
4279 | 262 | bin_dir => "$trunk/bin", | ||
4280 | 263 | ); | ||
4281 | 264 | }, | ||
4282 | 265 | ); | ||
4283 | 266 | |||
4284 | 267 | ok( | ||
4285 | 268 | no_diff( | ||
4286 | 269 | "cat $tmpdir/spool/query-history/2.query-history.data", | ||
4287 | 270 | "$sample/query-history/data001.json", | ||
4288 | 271 | post_pipe => 'grep -v \'"name" :\'', | ||
4289 | 272 | ), | ||
4290 | 273 | "2 runs: spool data (query-history/data001.json)" | ||
4291 | 274 | ) or diag( | ||
4292 | 275 | `ls -l $tmpdir/spool/query-history/`, | ||
4293 | 276 | `cat $tmpdir/logs/query-history.run`, | ||
4294 | 277 | Dumper(\@log) | ||
4295 | 278 | ); | ||
4296 | 279 | |||
4297 | 280 | chomp($n_files = `ls -1 $spool_dir/query-history/*.data | wc -l | awk '{print \$1}'`); | ||
4298 | 281 | is( | ||
4299 | 282 | $n_files, | ||
4300 | 283 | 1, | ||
4301 | 284 | "2 runs: only wrote spool data" | ||
4302 | 285 | ) or diag(`ls -l $spool_dir`); | ||
4303 | 286 | |||
4304 | 287 | is( | ||
4305 | 288 | $exit_status, | ||
4306 | 289 | 0, | ||
4307 | 290 | "2 runs: exit 0" | ||
4308 | 291 | ); | ||
4309 | 292 | |||
4310 | 293 | my @tmp_files = glob "$tmpdir/spool/.tmp/*"; | ||
4311 | 294 | is_deeply( | ||
4312 | 295 | \@tmp_files, | ||
4313 | 296 | [], | ||
4314 | 297 | "2 runs: temp file removed" | ||
4315 | 298 | ); | ||
4316 | 299 | |||
4317 | 300 | # ############################################################################# | ||
4318 | 301 | # More realistc: 3 services, multiple tasks, using programs and queries. | ||
4319 | 302 | # ############################################################################# | ||
4320 | 303 | |||
4321 | 304 | SKIP: { | ||
4322 | 305 | skip 'Cannot connect to sandbox master', 5 unless $dbh; | ||
4323 | 306 | skip 'No HOME environment variable', 5 unless $ENV{HOME}; | ||
4324 | 307 | |||
4325 | 308 | diag(`rm -rf $tmpdir/spool/* $tmpdir/services/*`); | ||
4326 | 309 | @log = (); | ||
4327 | 310 | |||
4328 | 311 | my (undef, $old_genlog) = $dbh->selectrow_array("SHOW VARIABLES LIKE 'general_log_file'"); | ||
4329 | 312 | |||
4330 | 313 | my $new_genlog = "$tmpdir/genlog"; | ||
4331 | 314 | |||
4332 | 315 | # First service: set up | ||
4333 | 316 | my $task00 = Percona::WebAPI::Resource::Task->new( | ||
4334 | 317 | name => 'disable-gen-log', | ||
4335 | 318 | number => '0', | ||
4336 | 319 | query => "SET GLOBAL general_log=OFF", | ||
4337 | 320 | ); | ||
4338 | 321 | my $task01 = Percona::WebAPI::Resource::Task->new( | ||
4339 | 322 | name => 'set-gen-log-file', | ||
4340 | 323 | number => '1', | ||
4341 | 324 | query => "SET GLOBAL general_log_file='$new_genlog'", | ||
4342 | 325 | ); | ||
4343 | 326 | my $task02 = Percona::WebAPI::Resource::Task->new( | ||
4344 | 327 | name => 'enable-gen-log', | ||
4345 | 328 | number => '2', | ||
4346 | 329 | query => "SET GLOBAL general_log=ON", | ||
4347 | 330 | ); | ||
4348 | 331 | my $svc0 = Percona::WebAPI::Resource::Service->new( | ||
4349 | 332 | ts => 100, | ||
4350 | 333 | name => 'enable-gen-log', | ||
4351 | 334 | run_schedule => '1 * * * *', | ||
4352 | 335 | spool_schedule => '2 * * * *', | ||
4353 | 336 | tasks => [ $task00, $task01, $task02 ], | ||
4354 | 337 | ); | ||
4355 | 338 | |||
4356 | 339 | # Second service: the actual service | ||
4357 | 340 | my $task10 = Percona::WebAPI::Resource::Task->new( | ||
4358 | 341 | name => 'query-history', | ||
4359 | 342 | number => '1', | ||
4360 | 343 | program => "$trunk/bin/pt-query-digest --output json --type genlog $new_genlog", | ||
4361 | 344 | output => 'spool', | ||
4362 | 345 | ); | ||
4363 | 346 | my $svc1 = Percona::WebAPI::Resource::Service->new( | ||
4364 | 347 | ts => 100, | ||
4365 | 348 | name => 'query-history', | ||
4366 | 349 | run_schedule => '3 * * * *', | ||
4367 | 350 | spool_schedule => '4 * * * *', | ||
4368 | 351 | tasks => [ $task10 ], | ||
4369 | 352 | ); | ||
4370 | 353 | |||
4371 | 354 | # Third service: tear down | ||
4372 | 355 | my $task20 = Percona::WebAPI::Resource::Task->new( | ||
4373 | 356 | name => 'disable-gen-log', | ||
4374 | 357 | number => '0', | ||
4375 | 358 | query => "SET GLOBAL general_log=OFF", | ||
4376 | 359 | ); | ||
4377 | 360 | my $task21 = Percona::WebAPI::Resource::Task->new( | ||
4378 | 361 | name => 'set-gen-log-file', | ||
4379 | 362 | number => '1', | ||
4380 | 363 | query => "SET GLOBAL general_log_file='$old_genlog'", | ||
4381 | 364 | ); | ||
4382 | 365 | my $task22 = Percona::WebAPI::Resource::Task->new( | ||
4383 | 366 | name => 'enable-gen-log', | ||
4384 | 367 | number => '2', | ||
4385 | 368 | query => "SET GLOBAL general_log=ON", | ||
4386 | 369 | ); | ||
4387 | 370 | my $svc2 = Percona::WebAPI::Resource::Service->new( | ||
4388 | 371 | ts => 100, | ||
4389 | 372 | name => 'disable-gen-log', | ||
4390 | 373 | run_schedule => '5 * * * *', | ||
4391 | 374 | spool_schedule => '6 * * * *', | ||
4392 | 375 | tasks => [ $task20, $task21, $task22 ], | ||
4393 | 376 | ); | ||
4394 | 377 | |||
4395 | 378 | write_svc_files( | ||
4396 | 379 | services => [ $svc0, $svc1, $svc2 ], | ||
4397 | 380 | ); | ||
4398 | 381 | |||
4399 | 382 | $ua->{responses}->{get} = [ | ||
4400 | 383 | { | ||
4401 | 384 | headers => { 'X-Percona-Resource-Type' => 'Agent' }, | ||
4402 | 385 | content => as_hashref($agent, with_links => 1), | ||
4403 | 386 | }, | ||
4404 | 387 | { | ||
4405 | 388 | headers => { 'X-Percona-Resource-Type' => 'Agent' }, | ||
4406 | 389 | content => as_hashref($agent, with_links => 1), | ||
4407 | 390 | }, | ||
4408 | 391 | { | ||
4409 | 392 | headers => { 'X-Percona-Resource-Type' => 'Agent' }, | ||
4410 | 393 | content => as_hashref($agent, with_links => 1), | ||
4411 | 394 | }, | ||
4412 | 395 | ]; | ||
4413 | 396 | |||
4414 | 397 | my $cxn = Cxn->new( | ||
4415 | 398 | dsn_string => $dsn, | ||
4416 | 399 | OptionParser => $o, | ||
4417 | 400 | DSNParser => $dp, | ||
4418 | 401 | ); | ||
4419 | 402 | |||
4420 | 403 | # Run the first service. | ||
4421 | 404 | $output = output( | ||
4422 | 405 | sub { | ||
4423 | 406 | $exit_status = pt_agent::run_service( | ||
4424 | 407 | api_key => '123', | ||
4425 | 408 | service => 'enable-gen-log', | ||
4426 | 409 | spool_dir => $spool_dir, | ||
4427 | 410 | lib_dir => $tmpdir, | ||
4428 | 411 | Cxn => $cxn, | ||
4429 | 412 | # for testing: | ||
4430 | 413 | client => $client, | ||
4431 | 414 | agent => $agent, | ||
4432 | 415 | entry_links => $links, | ||
4433 | 416 | prefix => '3', | ||
4434 | 417 | json => $json, | ||
4435 | 418 | bin_dir => "$trunk/bin", | ||
4436 | 419 | ); | ||
4437 | 420 | }, | ||
4438 | 421 | ); | ||
4439 | 422 | |||
4440 | 423 | my (undef, $genlog) = $dbh->selectrow_array( | ||
4441 | 424 | "SHOW VARIABLES LIKE 'general_log_file'"); | ||
4442 | 425 | is( | ||
4443 | 426 | $genlog, | ||
4444 | 427 | $new_genlog, | ||
4445 | 428 | "Task set MySQL var" | ||
4446 | 429 | ) or diag($output); | ||
4447 | 430 | |||
4448 | 431 | # Pretend some time passes... | ||
4449 | 432 | |||
4450 | 433 | # The next service doesn't need MySQL, so it shouldn't connect to it. | ||
4451 | 434 | # To check this, the genlog before running and after running should | ||
4452 | 435 | # be identical. | ||
4453 | 436 | `cp $new_genlog $tmpdir/genlog-before`; | ||
4454 | 437 | |||
4455 | 438 | # Run the second service. | ||
4456 | 439 | $output = output( | ||
4457 | 440 | sub { | ||
4458 | 441 | $exit_status = pt_agent::run_service( | ||
4459 | 442 | api_key => '123', | ||
4460 | 443 | service => 'query-history', | ||
4461 | 444 | spool_dir => $spool_dir, | ||
4462 | 445 | lib_dir => $tmpdir, | ||
4463 | 446 | Cxn => $cxn, | ||
4464 | 447 | # for testing: | ||
4465 | 448 | client => $client, | ||
4466 | 449 | agent => $agent, | ||
4467 | 450 | entry_links => $links, | ||
4468 | 451 | prefix => '4', | ||
4469 | 452 | json => $json, | ||
4470 | 453 | bin_dir => "$trunk/bin", | ||
4471 | 454 | ); | ||
4472 | 455 | }, | ||
4473 | 456 | ); | ||
4474 | 457 | |||
4475 | 458 | `cp $new_genlog $tmpdir/genlog-after`; | ||
4476 | 459 | my $diff = `diff $tmpdir/genlog-before $tmpdir/genlog-after`; | ||
4477 | 460 | is( | ||
4478 | 461 | $diff, | ||
4479 | 462 | '', | ||
4480 | 463 | "Tasks didn't need MySQL, didn't connect to MySQL" | ||
4481 | 464 | ) or diag($output); | ||
4482 | 465 | |||
4483 | 466 | # Pretend more time passes... | ||
4484 | 467 | |||
4485 | 468 | # Run the third service. | ||
4486 | 469 | $output = output( | ||
4487 | 470 | sub { | ||
4488 | 471 | $exit_status = pt_agent::run_service( | ||
4489 | 472 | api_key => '123', | ||
4490 | 473 | service => 'disable-gen-log', | ||
4491 | 474 | spool_dir => $spool_dir, | ||
4492 | 475 | lib_dir => $tmpdir, | ||
4493 | 476 | Cxn => $cxn, | ||
4494 | 477 | # for testing: | ||
4495 | 478 | client => $client, | ||
4496 | 479 | agent => $agent, | ||
4497 | 480 | entry_links => $links, | ||
4498 | 481 | prefix => '5', | ||
4499 | 482 | json => $json, | ||
4500 | 483 | bin_dir => "$trunk/bin", | ||
4501 | 484 | ); | ||
4502 | 485 | }, | ||
4503 | 486 | ); | ||
4504 | 487 | |||
4505 | 488 | (undef, $genlog) = $dbh->selectrow_array( | ||
4506 | 489 | "SHOW VARIABLES LIKE 'general_log_file'"); | ||
4507 | 490 | is( | ||
4508 | 491 | $genlog, | ||
4509 | 492 | $old_genlog, | ||
4510 | 493 | "Task restored MySQL var" | ||
4511 | 494 | ) or diag($output); | ||
4512 | 495 | |||
4513 | 496 | $dbh->do("SET GLOBAL general_log=ON"); | ||
4514 | 497 | $dbh->do("SET GLOBAL general_log_file='$old_genlog'"); | ||
4515 | 498 | } | ||
4516 | 499 | |||
4517 | 500 | # ############################################################################# | ||
4518 | 501 | # Done. | ||
4519 | 502 | # ############################################################################# | ||
4520 | 503 | done_testing; | ||
4521 | 504 | 0 | ||
4522 | === removed directory 't/pt-agent/samples' | |||
4523 | === removed file 't/pt-agent/samples/crontab001.in' | |||
4524 | === removed file 't/pt-agent/samples/crontab001.out' | |||
4525 | --- t/pt-agent/samples/crontab001.out 2013-03-19 22:35:37 +0000 | |||
4526 | +++ t/pt-agent/samples/crontab001.out 1970-01-01 00:00:00 +0000 | |||
4527 | @@ -1,2 +0,0 @@ | |||
4528 | 1 | * 8 * * 1,2,3,4,5 pt-agent --run-service query-history | ||
4529 | 2 | * 9 * * 1,2,3,4,5 pt-agent --send-data query-history | ||
4530 | 3 | 0 | ||
4531 | === removed file 't/pt-agent/samples/crontab002.in' | |||
4532 | --- t/pt-agent/samples/crontab002.in 2013-01-08 17:55:58 +0000 | |||
4533 | +++ t/pt-agent/samples/crontab002.in 1970-01-01 00:00:00 +0000 | |||
4534 | @@ -1,1 +0,0 @@ | |||
4535 | 1 | 17 3 * * 1 cmd | ||
4536 | 2 | 0 | ||
4537 | === removed file 't/pt-agent/samples/crontab002.out' | |||
4538 | --- t/pt-agent/samples/crontab002.out 2013-03-19 22:35:37 +0000 | |||
4539 | +++ t/pt-agent/samples/crontab002.out 1970-01-01 00:00:00 +0000 | |||
4540 | @@ -1,3 +0,0 @@ | |||
4541 | 1 | 17 3 * * 1 cmd | ||
4542 | 2 | * 8 * * 1,2,3,4,5 pt-agent --run-service query-history | ||
4543 | 3 | * 9 * * 1,2,3,4,5 pt-agent --send-data query-history | ||
4544 | 4 | 0 | ||
4545 | === removed file 't/pt-agent/samples/crontab003.in' | |||
4546 | --- t/pt-agent/samples/crontab003.in 2013-01-08 17:55:58 +0000 | |||
4547 | +++ t/pt-agent/samples/crontab003.in 1970-01-01 00:00:00 +0000 | |||
4548 | @@ -1,3 +0,0 @@ | |||
4549 | 1 | 17 3 * * 1 cmd | ||
4550 | 2 | * * * * 1 pt-agent --run-service old-service | ||
4551 | 3 | |||
4552 | 4 | 0 | ||
4553 | === removed file 't/pt-agent/samples/crontab003.out' | |||
4554 | --- t/pt-agent/samples/crontab003.out 2013-03-19 22:35:37 +0000 | |||
4555 | +++ t/pt-agent/samples/crontab003.out 1970-01-01 00:00:00 +0000 | |||
4556 | @@ -1,3 +0,0 @@ | |||
4557 | 1 | 17 3 * * 1 cmd | ||
4558 | 2 | * 8 * * 1,2,3,4,5 pt-agent --run-service query-history | ||
4559 | 3 | * 9 * * 1,2,3,4,5 pt-agent --send-data query-history | ||
4560 | 4 | 0 | ||
4561 | === removed file 't/pt-agent/samples/crontab004.in' | |||
4562 | --- t/pt-agent/samples/crontab004.in 2013-01-30 20:25:21 +0000 | |||
4563 | +++ t/pt-agent/samples/crontab004.in 1970-01-01 00:00:00 +0000 | |||
4564 | @@ -1,2 +0,0 @@ | |||
4565 | 1 | 1 * * * * pt-agent --run-service foo | ||
4566 | 2 | 2 * * * * pt-agent --send-data foo | ||
4567 | 3 | 0 | ||
4568 | === removed file 't/pt-agent/samples/crontab004.out' | |||
4569 | --- t/pt-agent/samples/crontab004.out 2013-03-19 22:35:37 +0000 | |||
4570 | +++ t/pt-agent/samples/crontab004.out 1970-01-01 00:00:00 +0000 | |||
4571 | @@ -1,2 +0,0 @@ | |||
4572 | 1 | * 8 * * 1,2,3,4,5 pt-agent --run-service query-history | ||
4573 | 2 | * 9 * * 1,2,3,4,5 pt-agent --send-data query-history | ||
4574 | 3 | 0 | ||
4575 | === removed directory 't/pt-agent/samples/query-history' | |||
4576 | === removed file 't/pt-agent/samples/query-history/data001.json' | |||
4577 | --- t/pt-agent/samples/query-history/data001.json 2013-11-08 03:03:01 +0000 | |||
4578 | +++ t/pt-agent/samples/query-history/data001.json 1970-01-01 00:00:00 +0000 | |||
4579 | @@ -1,152 +0,0 @@ | |||
4580 | 1 | |||
4581 | 2 | { | ||
4582 | 3 | "classes" : [ | ||
4583 | 4 | { | ||
4584 | 5 | "attribute" : "fingerprint", | ||
4585 | 6 | "checksum" : "C72BF45D68E35A6E", | ||
4586 | 7 | "distillate" : "SELECT tbl", | ||
4587 | 8 | "example" : { | ||
4588 | 9 | "Query_time" : "0.018799", | ||
4589 | 10 | "query" : "SELECT MIN(id),MAX(id) FROM tbl", | ||
4590 | 11 | "ts" : null | ||
4591 | 12 | }, | ||
4592 | 13 | "fingerprint" : "select min(id),max(id) from tbl", | ||
4593 | 14 | "histograms" : { | ||
4594 | 15 | "Query_time" : [ | ||
4595 | 16 | 0, | ||
4596 | 17 | 0, | ||
4597 | 18 | 0, | ||
4598 | 19 | 0, | ||
4599 | 20 | 1, | ||
4600 | 21 | 0, | ||
4601 | 22 | 0, | ||
4602 | 23 | 0 | ||
4603 | 24 | ] | ||
4604 | 25 | }, | ||
4605 | 26 | "metrics" : { | ||
4606 | 27 | "Lock_time" : { | ||
4607 | 28 | "avg" : "0.009453", | ||
4608 | 29 | "max" : "0.009453", | ||
4609 | 30 | "median" : "0.009453", | ||
4610 | 31 | "min" : "0.009453", | ||
4611 | 32 | "pct" : "0.333333", | ||
4612 | 33 | "pct_95" : "0.009453", | ||
4613 | 34 | "stddev" : "0.000000", | ||
4614 | 35 | "sum" : "0.009453" | ||
4615 | 36 | }, | ||
4616 | 37 | "Query_length" : { | ||
4617 | 38 | "avg" : "31", | ||
4618 | 39 | "max" : "31", | ||
4619 | 40 | "median" : "31", | ||
4620 | 41 | "min" : "31", | ||
4621 | 42 | "pct" : "0", | ||
4622 | 43 | "pct_95" : "31", | ||
4623 | 44 | "stddev" : "0", | ||
4624 | 45 | "sum" : "31" | ||
4625 | 46 | }, | ||
4626 | 47 | "Query_time" : { | ||
4627 | 48 | "avg" : "0.018799", | ||
4628 | 49 | "max" : "0.018799", | ||
4629 | 50 | "median" : "0.018799", | ||
4630 | 51 | "min" : "0.018799", | ||
4631 | 52 | "pct" : "0.333333", | ||
4632 | 53 | "pct_95" : "0.018799", | ||
4633 | 54 | "stddev" : "0.000000", | ||
4634 | 55 | "sum" : "0.018799" | ||
4635 | 56 | }, | ||
4636 | 57 | "Rows_examined" : { | ||
4637 | 58 | "avg" : "0", | ||
4638 | 59 | "max" : "0", | ||
4639 | 60 | "median" : "0", | ||
4640 | 61 | "min" : "0", | ||
4641 | 62 | "pct" : "0", | ||
4642 | 63 | "pct_95" : "0", | ||
4643 | 64 | "stddev" : "0", | ||
4644 | 65 | "sum" : "0" | ||
4645 | 66 | }, | ||
4646 | 67 | "Rows_sent" : { | ||
4647 | 68 | "avg" : "0", | ||
4648 | 69 | "max" : "0", | ||
4649 | 70 | "median" : "0", | ||
4650 | 71 | "min" : "0", | ||
4651 | 72 | "pct" : "0", | ||
4652 | 73 | "pct_95" : "0", | ||
4653 | 74 | "stddev" : "0", | ||
4654 | 75 | "sum" : "0" | ||
4655 | 76 | }, | ||
4656 | 77 | "db" : { | ||
4657 | 78 | "value" : "db2" | ||
4658 | 79 | }, | ||
4659 | 80 | "host" : { | ||
4660 | 81 | "value" : "" | ||
4661 | 82 | }, | ||
4662 | 83 | "user" : { | ||
4663 | 84 | "value" : "meow" | ||
4664 | 85 | } | ||
4665 | 86 | }, | ||
4666 | 87 | "query_count" : 1, | ||
4667 | 88 | "tables" : [ | ||
4668 | 89 | { | ||
4669 | 90 | "create" : "SHOW CREATE TABLE `db2`.`tbl`\\G", | ||
4670 | 91 | "status" : "SHOW TABLE STATUS FROM `db2` LIKE 'tbl'\\G" | ||
4671 | 92 | } | ||
4672 | 93 | ] | ||
4673 | 94 | } | ||
4674 | 95 | ], | ||
4675 | 96 | "global" : { | ||
4676 | 97 | "files" : [ | ||
4677 | 98 | { | ||
4678 | 99 | "size" : 656 | ||
4679 | 100 | } | ||
4680 | 101 | ], | ||
4681 | 102 | "metrics" : { | ||
4682 | 103 | "Lock_time" : { | ||
4683 | 104 | "avg" : "0.003151", | ||
4684 | 105 | "max" : "0.009453", | ||
4685 | 106 | "median" : "0.000000", | ||
4686 | 107 | "min" : "0.000000", | ||
4687 | 108 | "pct_95" : "0.009171", | ||
4688 | 109 | "stddev" : "0.004323", | ||
4689 | 110 | "sum" : "0.009453" | ||
4690 | 111 | }, | ||
4691 | 112 | "Query_length" : { | ||
4692 | 113 | "avg" : "24", | ||
4693 | 114 | "max" : "31", | ||
4694 | 115 | "median" : "26", | ||
4695 | 116 | "min" : "14", | ||
4696 | 117 | "pct_95" : "30", | ||
4697 | 118 | "stddev" : "6", | ||
4698 | 119 | "sum" : "72" | ||
4699 | 120 | }, | ||
4700 | 121 | "Query_time" : { | ||
4701 | 122 | "avg" : "0.006567", | ||
4702 | 123 | "max" : "0.018799", | ||
4703 | 124 | "median" : "0.000882", | ||
4704 | 125 | "min" : "0.000002", | ||
4705 | 126 | "pct_95" : "0.018157", | ||
4706 | 127 | "stddev" : "0.008359", | ||
4707 | 128 | "sum" : "0.019700" | ||
4708 | 129 | }, | ||
4709 | 130 | "Rows_examined" : { | ||
4710 | 131 | "avg" : "0", | ||
4711 | 132 | "max" : "0", | ||
4712 | 133 | "median" : "0", | ||
4713 | 134 | "min" : "0", | ||
4714 | 135 | "pct_95" : "0", | ||
4715 | 136 | "stddev" : "0", | ||
4716 | 137 | "sum" : "0" | ||
4717 | 138 | }, | ||
4718 | 139 | "Rows_sent" : { | ||
4719 | 140 | "avg" : "0", | ||
4720 | 141 | "max" : "0", | ||
4721 | 142 | "median" : "0", | ||
4722 | 143 | "min" : "0", | ||
4723 | 144 | "pct_95" : "0", | ||
4724 | 145 | "stddev" : "0", | ||
4725 | 146 | "sum" : "0" | ||
4726 | 147 | } | ||
4727 | 148 | }, | ||
4728 | 149 | "query_count" : 3, | ||
4729 | 150 | "unique_query_count" : 3 | ||
4730 | 151 | } | ||
4731 | 152 | } | ||
4732 | 153 | 0 | ||
4733 | === removed file 't/pt-agent/samples/query-history/data001.send' | |||
4734 | --- t/pt-agent/samples/query-history/data001.send 2013-11-08 03:03:01 +0000 | |||
4735 | +++ t/pt-agent/samples/query-history/data001.send 1970-01-01 00:00:00 +0000 | |||
4736 | @@ -1,166 +0,0 @@ | |||
4737 | 1 | --Ym91bmRhcnk | ||
4738 | 2 | Content-Disposition: form-data; name="agent" | ||
4739 | 3 | |||
4740 | 4 | { | ||
4741 | 5 | "hostname" : "prod1", | ||
4742 | 6 | "uuid" : "123" | ||
4743 | 7 | } | ||
4744 | 8 | --Ym91bmRhcnk | ||
4745 | 9 | Content-Disposition: form-data; name="meta" | ||
4746 | 10 | |||
4747 | 11 | |||
4748 | 12 | --Ym91bmRhcnk | ||
4749 | 13 | Content-Disposition: form-data; name="data" | ||
4750 | 14 | |||
4751 | 15 | { | ||
4752 | 16 | "classes" : [ | ||
4753 | 17 | { | ||
4754 | 18 | "attribute" : "fingerprint", | ||
4755 | 19 | "checksum" : "C72BF45D68E35A6E", | ||
4756 | 20 | "distillate" : "SELECT tbl", | ||
4757 | 21 | "example" : { | ||
4758 | 22 | "Query_time" : "0.018799", | ||
4759 | 23 | "query" : "SELECT MIN(id),MAX(id) FROM tbl", | ||
4760 | 24 | "ts" : null | ||
4761 | 25 | }, | ||
4762 | 26 | "fingerprint" : "select min(id),max(id) from tbl", | ||
4763 | 27 | "histograms" : { | ||
4764 | 28 | "Query_time" : [ | ||
4765 | 29 | 0, | ||
4766 | 30 | 0, | ||
4767 | 31 | 0, | ||
4768 | 32 | 0, | ||
4769 | 33 | 1, | ||
4770 | 34 | 0, | ||
4771 | 35 | 0, | ||
4772 | 36 | 0 | ||
4773 | 37 | ] | ||
4774 | 38 | }, | ||
4775 | 39 | "metrics" : { | ||
4776 | 40 | "Lock_time" : { | ||
4777 | 41 | "avg" : "0.009453", | ||
4778 | 42 | "max" : "0.009453", | ||
4779 | 43 | "median" : "0.009453", | ||
4780 | 44 | "min" : "0.009453", | ||
4781 | 45 | "pct" : "0.333333", | ||
4782 | 46 | "pct_95" : "0.009453", | ||
4783 | 47 | "stddev" : "0.000000", | ||
4784 | 48 | "sum" : "0.009453" | ||
4785 | 49 | }, | ||
4786 | 50 | "Query_length" : { | ||
4787 | 51 | "avg" : "31", | ||
4788 | 52 | "max" : "31", | ||
4789 | 53 | "median" : "31", | ||
4790 | 54 | "min" : "31", | ||
4791 | 55 | "pct" : "0", | ||
4792 | 56 | "pct_95" : "31", | ||
4793 | 57 | "stddev" : "0", | ||
4794 | 58 | "sum" : "31" | ||
4795 | 59 | }, | ||
4796 | 60 | "Query_time" : { | ||
4797 | 61 | "avg" : "0.018799", | ||
4798 | 62 | "max" : "0.018799", | ||
4799 | 63 | "median" : "0.018799", | ||
4800 | 64 | "min" : "0.018799", | ||
4801 | 65 | "pct" : "0.333333", | ||
4802 | 66 | "pct_95" : "0.018799", | ||
4803 | 67 | "stddev" : "0.000000", | ||
4804 | 68 | "sum" : "0.018799" | ||
4805 | 69 | }, | ||
4806 | 70 | "Rows_examined" : { | ||
4807 | 71 | "avg" : "0", | ||
4808 | 72 | "max" : "0", | ||
4809 | 73 | "median" : "0", | ||
4810 | 74 | "min" : "0", | ||
4811 | 75 | "pct" : "0", | ||
4812 | 76 | "pct_95" : "0", | ||
4813 | 77 | "stddev" : "0", | ||
4814 | 78 | "sum" : "0" | ||
4815 | 79 | }, | ||
4816 | 80 | "Rows_sent" : { | ||
4817 | 81 | "avg" : "0", | ||
4818 | 82 | "max" : "0", | ||
4819 | 83 | "median" : "0", | ||
4820 | 84 | "min" : "0", | ||
4821 | 85 | "pct" : "0", | ||
4822 | 86 | "pct_95" : "0", | ||
4823 | 87 | "stddev" : "0", | ||
4824 | 88 | "sum" : "0" | ||
4825 | 89 | }, | ||
4826 | 90 | "db" : { | ||
4827 | 91 | "value" : "db2" | ||
4828 | 92 | }, | ||
4829 | 93 | "host" : { | ||
4830 | 94 | "value" : "" | ||
4831 | 95 | }, | ||
4832 | 96 | "user" : { | ||
4833 | 97 | "value" : "meow" | ||
4834 | 98 | } | ||
4835 | 99 | }, | ||
4836 | 100 | "query_count" : 1, | ||
4837 | 101 | "tables" : [ | ||
4838 | 102 | { | ||
4839 | 103 | "create" : "SHOW CREATE TABLE `db2`.`tbl`\\G", | ||
4840 | 104 | "status" : "SHOW TABLE STATUS FROM `db2` LIKE 'tbl'\\G" | ||
4841 | 105 | } | ||
4842 | 106 | ] | ||
4843 | 107 | } | ||
4844 | 108 | ], | ||
4845 | 109 | "global" : { | ||
4846 | 110 | "files" : [ | ||
4847 | 111 | { | ||
4848 | 112 | "size" : 656 | ||
4849 | 113 | } | ||
4850 | 114 | ], | ||
4851 | 115 | "metrics" : { | ||
4852 | 116 | "Lock_time" : { | ||
4853 | 117 | "avg" : "0.003151", | ||
4854 | 118 | "max" : "0.009453", | ||
4855 | 119 | "median" : "0.000000", | ||
4856 | 120 | "min" : "0.000000", | ||
4857 | 121 | "pct_95" : "0.009171", | ||
4858 | 122 | "stddev" : "0.004323", | ||
4859 | 123 | "sum" : "0.009453" | ||
4860 | 124 | }, | ||
4861 | 125 | "Query_length" : { | ||
4862 | 126 | "avg" : "24", | ||
4863 | 127 | "max" : "31", | ||
4864 | 128 | "median" : "26", | ||
4865 | 129 | "min" : "14", | ||
4866 | 130 | "pct_95" : "30", | ||
4867 | 131 | "stddev" : "6", | ||
4868 | 132 | "sum" : "72" | ||
4869 | 133 | }, | ||
4870 | 134 | "Query_time" : { | ||
4871 | 135 | "avg" : "0.006567", | ||
4872 | 136 | "max" : "0.018799", | ||
4873 | 137 | "median" : "0.000882", | ||
4874 | 138 | "min" : "0.000002", | ||
4875 | 139 | "pct_95" : "0.018157", | ||
4876 | 140 | "stddev" : "0.008359", | ||
4877 | 141 | "sum" : "0.019700" | ||
4878 | 142 | }, | ||
4879 | 143 | "Rows_examined" : { | ||
4880 | 144 | "avg" : "0", | ||
4881 | 145 | "max" : "0", | ||
4882 | 146 | "median" : "0", | ||
4883 | 147 | "min" : "0", | ||
4884 | 148 | "pct_95" : "0", | ||
4885 | 149 | "stddev" : "0", | ||
4886 | 150 | "sum" : "0" | ||
4887 | 151 | }, | ||
4888 | 152 | "Rows_sent" : { | ||
4889 | 153 | "avg" : "0", | ||
4890 | 154 | "max" : "0", | ||
4891 | 155 | "median" : "0", | ||
4892 | 156 | "min" : "0", | ||
4893 | 157 | "pct_95" : "0", | ||
4894 | 158 | "stddev" : "0", | ||
4895 | 159 | "sum" : "0" | ||
4896 | 160 | } | ||
4897 | 161 | }, | ||
4898 | 162 | "query_count" : 3, | ||
4899 | 163 | "unique_query_count" : 3 | ||
4900 | 164 | } | ||
4901 | 165 | } | ||
4902 | 166 | --Ym91bmRhcnk | ||
4903 | 167 | 0 | ||
4904 | === removed file 't/pt-agent/samples/service001' | |||
4905 | --- t/pt-agent/samples/service001 2013-06-17 04:01:30 +0000 | |||
4906 | +++ t/pt-agent/samples/service001 1970-01-01 00:00:00 +0000 | |||
4907 | @@ -1,19 +0,0 @@ | |||
4908 | 1 | { | ||
4909 | 2 | "links" : { | ||
4910 | 3 | "data" : "/query-history/data", | ||
4911 | 4 | "self" : "/query-history" | ||
4912 | 5 | }, | ||
4913 | 6 | "name" : "query-history", | ||
4914 | 7 | "run_schedule" : "1 * * * *", | ||
4915 | 8 | "spool_schedule" : "2 * * * *", | ||
4916 | 9 | "tasks" : [ | ||
4917 | 10 | { | ||
4918 | 11 | "name" : "query-history", | ||
4919 | 12 | "number" : "0", | ||
4920 | 13 | "options" : "--output json", | ||
4921 | 14 | "output" : "spool", | ||
4922 | 15 | "program" : "pt-query-digest" | ||
4923 | 16 | } | ||
4924 | 17 | ], | ||
4925 | 18 | "ts" : 100 | ||
4926 | 19 | } | ||
4927 | 20 | 0 | ||
4928 | === removed file 't/pt-agent/samples/write_services001' | |||
4929 | --- t/pt-agent/samples/write_services001 2013-06-17 00:28:18 +0000 | |||
4930 | +++ t/pt-agent/samples/write_services001 1970-01-01 00:00:00 +0000 | |||
4931 | @@ -1,19 +0,0 @@ | |||
4932 | 1 | { | ||
4933 | 2 | "links" : { | ||
4934 | 3 | "data" : "/query-history/data", | ||
4935 | 4 | "self" : "/query-history" | ||
4936 | 5 | }, | ||
4937 | 6 | "name" : "query-history", | ||
4938 | 7 | "run_schedule" : "1 * * * *", | ||
4939 | 8 | "spool_schedule" : "2 * * * *", | ||
4940 | 9 | "tasks" : [ | ||
4941 | 10 | { | ||
4942 | 11 | "name" : "query-history", | ||
4943 | 12 | "number" : "0", | ||
4944 | 13 | "options" : "--report-format profile slow008.txt", | ||
4945 | 14 | "output" : "spool", | ||
4946 | 15 | "program" : "pt-query-digest" | ||
4947 | 16 | } | ||
4948 | 17 | ], | ||
4949 | 18 | "ts" : 100 | ||
4950 | 19 | } | ||
4951 | 20 | 0 | ||
4952 | === removed file 't/pt-agent/schedule_services.t' | |||
4953 | --- t/pt-agent/schedule_services.t 2013-06-17 00:28:18 +0000 | |||
4954 | +++ t/pt-agent/schedule_services.t 1970-01-01 00:00:00 +0000 | |||
4955 | @@ -1,200 +0,0 @@ | |||
4956 | 1 | #!/usr/bin/env perl | ||
4957 | 2 | |||
4958 | 3 | BEGIN { | ||
4959 | 4 | die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n" | ||
4960 | 5 | unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH}; | ||
4961 | 6 | unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib"; | ||
4962 | 7 | }; | ||
4963 | 8 | |||
4964 | 9 | use strict; | ||
4965 | 10 | use warnings FATAL => 'all'; | ||
4966 | 11 | use English qw(-no_match_vars); | ||
4967 | 12 | use Test::More; | ||
4968 | 13 | use JSON; | ||
4969 | 14 | use File::Temp qw(tempfile tempdir); | ||
4970 | 15 | |||
4971 | 16 | use Percona::Test; | ||
4972 | 17 | use Percona::Test::Mock::AgentLogger; | ||
4973 | 18 | require "$trunk/bin/pt-agent"; | ||
4974 | 19 | |||
4975 | 20 | my $crontab = `crontab -l 2>/dev/null`; | ||
4976 | 21 | if ( $crontab ) { | ||
4977 | 22 | plan skip_all => 'Crontab is not empty'; | ||
4978 | 23 | } | ||
4979 | 24 | |||
4980 | 25 | Percona::Toolkit->import(qw(have_required_args Dumper)); | ||
4981 | 26 | |||
4982 | 27 | my $sample = "t/pt-agent/samples"; | ||
4983 | 28 | my $tmpdir = tempdir("/tmp/pt-agent.$PID.XXXXXX", CLEANUP => 1); | ||
4984 | 29 | |||
4985 | 30 | my @log; | ||
4986 | 31 | my $logger = Percona::Test::Mock::AgentLogger->new(log => \@log); | ||
4987 | 32 | pt_agent::_logger($logger); | ||
4988 | 33 | |||
4989 | 34 | # ############################################################################# | ||
4990 | 35 | # Schedule a good crontab. | ||
4991 | 36 | # ############################################################################# | ||
4992 | 37 | |||
4993 | 38 | my $run0 = Percona::WebAPI::Resource::Task->new( | ||
4994 | 39 | name => 'query-history', | ||
4995 | 40 | number => '0', | ||
4996 | 41 | program => 'pt-query-digest', | ||
4997 | 42 | options => '--output json', | ||
4998 | 43 | output => 'spool', | ||
4999 | 44 | ); | ||
5000 | 45 |
The diff has been truncated for viewing.