~percona-toolkit-dev/percona-toolkit/pt-table-checksum-fails-on-BINARY-field-in-PK-1381280

« back to all changes in this revision

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

  • Committer: Daniel Nichter
  • Date: 2014-05-30 01:09:13 UTC
  • mfrom: (598.5.6 release-2.2.8)
  • Revision ID: daniel@percona.com-20140530010913-4wep0en37aa4vvok
Merge release-2.2.8.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env perl
2
 
 
3
 
BEGIN {
4
 
   die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
5
 
      unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
6
 
   unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
7
 
};
8
 
 
9
 
use strict;
10
 
use warnings FATAL => 'all';
11
 
use English qw(-no_match_vars);
12
 
use Test::More;
13
 
use JSON;
14
 
use File::Temp qw(tempdir);
15
 
 
16
 
use Percona::Test;
17
 
use Percona::Test::Mock::UserAgent;
18
 
use Percona::Test::Mock::AgentLogger;
19
 
require "$trunk/bin/pt-agent";
20
 
 
21
 
Percona::Toolkit->import(qw(Dumper have_required_args));
22
 
Percona::WebAPI::Representation->import(qw(as_hashref));
23
 
 
24
 
my @log;
25
 
my $logger = Percona::Test::Mock::AgentLogger->new(log => \@log);
26
 
pt_agent::_logger($logger);
27
 
 
28
 
my $sample = "t/pt-agent/samples";
29
 
 
30
 
# #############################################################################
31
 
# Create mock client and Agent
32
 
# #############################################################################
33
 
 
34
 
# These aren't the real tests yet: to run_agent(), first we need
35
 
# a client and Agent, so create mock ones.
36
 
 
37
 
my $json = JSON->new->canonical([1])->pretty;
38
 
$json->allow_blessed([]);
39
 
$json->convert_blessed([]);
40
 
 
41
 
my $ua = Percona::Test::Mock::UserAgent->new(
42
 
   encode => sub { my $c = shift; return $json->encode($c || {}) },
43
 
);
44
 
 
45
 
# Create cilent, get entry links
46
 
my $links = {
47
 
   agents          => '/agents',
48
 
   config          => '/agents/1/config',
49
 
   services        => '/agents/1/services',
50
 
   'query-history' => '/query-history',
51
 
};
52
 
 
53
 
$ua->{responses}->{get} = [
54
 
   {
55
 
      content => $links,
56
 
   },
57
 
];
58
 
 
59
 
my $client = eval {
60
 
   Percona::WebAPI::Client->new(
61
 
      api_key => '123',
62
 
      ua      => $ua,
63
 
   );
64
 
};
65
 
is(
66
 
   $EVAL_ERROR,
67
 
   '',
68
 
   'Create mock client'
69
 
) or die;
70
 
 
71
 
my $agent = Percona::WebAPI::Resource::Agent->new(
72
 
   uuid     => '123',
73
 
   hostname => 'prod1', 
74
 
   links    => $links,
75
 
);
76
 
 
77
 
is_deeply(
78
 
   as_hashref($agent),
79
 
   {
80
 
      uuid     => '123',
81
 
      hostname => 'prod1',
82
 
   },
83
 
   'Create mock Agent'
84
 
) or die;
85
 
 
86
 
# #############################################################################
87
 
# Test send_data
88
 
# #############################################################################
89
 
 
90
 
my $tmpdir = tempdir("/tmp/pt-agent.$PID.XXXXXX", CLEANUP => 1);
91
 
pt_agent::init_lib_dir(
92
 
   lib_dir => $tmpdir,
93
 
   quiet   => 1,
94
 
);
95
 
pt_agent::init_spool_dir(
96
 
   spool_dir => $tmpdir,
97
 
   service   => 'query-history',
98
 
   quiet     => 1,
99
 
); 
100
 
 
101
 
`cp $trunk/$sample/query-history/data001.json $tmpdir/query-history/1.data001.data`;
102
 
`cp $trunk/$sample/service001 $tmpdir/services/query-history`;
103
 
 
104
 
$ua->{responses}->{get} = [
105
 
   {
106
 
      headers => { 'X-Percona-Resource-Type' => 'Agent' },
107
 
      content => as_hashref($agent, with_links => 1),
108
 
   },
109
 
];
110
 
 
111
 
$ua->{responses}->{post} = [
112
 
   {
113
 
      content => $links,
114
 
   },
115
 
];
116
 
 
117
 
my $output = output(
118
 
   sub {
119
 
      pt_agent::send_data(
120
 
         api_key   => '123',
121
 
         service   => 'query-history',
122
 
         lib_dir   => $tmpdir,
123
 
         spool_dir => $tmpdir,
124
 
         # optional, for testing:
125
 
         client      => $client,
126
 
         entry_links => $links,
127
 
         agent       => $agent,
128
 
         log_file    => "$tmpdir/log",
129
 
         json        => $json,
130
 
         delay       => 0,
131
 
      ),
132
 
   },
133
 
);
134
 
 
135
 
is(
136
 
   scalar @{$client->ua->{content}->{post}},
137
 
   1,
138
 
   "Only sent 1 resource"
139
 
) or diag(
140
 
   $output,
141
 
   Dumper($client->ua->{content}->{post}),
142
 
   `cat $tmpdir/logs/query-history.send`
143
 
);
144
 
 
145
 
is_deeply(
146
 
   $ua->{requests},
147
 
   [
148
 
      'GET /agents/123',
149
 
      'POST /query-history/data',
150
 
   ],
151
 
   "POST to Service.links.data"
152
 
);
153
 
 
154
 
ok(
155
 
   no_diff(
156
 
      $client->ua->{content}->{post}->[0] || '',
157
 
      "$sample/query-history/data001.send",
158
 
      cmd_output => 1,
159
 
   ),
160
 
   "Sent data file as multi-part resource (query-history/data001)"
161
 
) or diag(Dumper($client->ua->{content}->{post}));
162
 
 
163
 
ok(
164
 
   !-f "$tmpdir/query-history/1.data001.data",
165
 
   "Removed data file after sending successfully"
166
 
);
167
 
 
168
 
is(
169
 
   $ua->{request_objs}->[-1]->header('content-type'),
170
 
   'multipart/form-data; boundary=Ym91bmRhcnk',
171
 
   'Content-Type=multipart/form-data; boundary=Ym91bmRhcnk'
172
 
) or diag(Dumper($ua));
173
 
 
174
 
# #############################################################################
175
 
# Error 400 on send
176
 
# #############################################################################
177
 
 
178
 
@log = ();
179
 
$client->ua->{content}->{post} = [];
180
 
$ua->{requests} = [];
181
 
 
182
 
`cp $trunk/$sample/query-history/data001.json $tmpdir/query-history/1.data001.data`;
183
 
 
184
 
$ua->{responses}->{get} = [
185
 
   {
186
 
      headers => { 'X-Percona-Resource-Type' => 'Agent' },
187
 
      content => as_hashref($agent, with_links => 1),
188
 
   },
189
 
];
190
 
 
191
 
$ua->{responses}->{post} = [
192
 
   {
193
 
      code    => 400,
194
 
      content => '',
195
 
   },
196
 
];
197
 
 
198
 
$output = output(
199
 
   sub {
200
 
      pt_agent::send_data(
201
 
         api_key   => '123',
202
 
         service   => 'query-history',
203
 
         lib_dir   => $tmpdir,
204
 
         spool_dir => $tmpdir,
205
 
         # optional, for testing:
206
 
         client      => $client,
207
 
         entry_links => $links,
208
 
         agent       => $agent,
209
 
         log_file    => "$tmpdir/log",
210
 
         json        => $json,
211
 
         delay       => 0,
212
 
      ),
213
 
   },
214
 
);
215
 
 
216
 
is(
217
 
   scalar @{$client->ua->{content}->{post}},
218
 
   1,
219
 
   "400: sent resource"
220
 
) or diag(
221
 
   $output,
222
 
   Dumper($client->ua->{content}->{post}),
223
 
   `cat $tmpdir/logs/query-history.send`
224
 
);
225
 
 
226
 
ok(
227
 
   -f "$tmpdir/query-history/1.data001.data",
228
 
   "400: file not removed"
229
 
) or diag($output);
230
 
 
231
 
# #############################################################################
232
 
# Done.
233
 
# #############################################################################
234
 
done_testing;