~percona-toolkit-dev/percona-toolkit/pt-mysql-summary-Blank-InnoDB-Section-for-5.6-1254233

« back to all changes in this revision

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

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

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env perl
 
2
 
 
3
BEGIN {
 
4
   die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
 
5
      unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
 
6
   unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
 
7
};
 
8
 
 
9
use strict;
 
10
use warnings FATAL => 'all';
 
11
use English qw(-no_match_vars);
 
12
use Test::More;
 
13
use JSON;
 
14
use File::Temp qw(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
      ),
 
131
   },
 
132
);
 
133
 
 
134
is(
 
135
   scalar @{$client->ua->{content}->{post}},
 
136
   1,
 
137
   "Only sent 1 resource"
 
138
) or diag(
 
139
   $output,
 
140
   Dumper($client->ua->{content}->{post}),
 
141
   `cat $tmpdir/logs/query-history.send`
 
142
);
 
143
 
 
144
is_deeply(
 
145
   $ua->{requests},
 
146
   [
 
147
      'GET /agents/123',
 
148
      'POST /query-history/data',
 
149
   ],
 
150
   "POST to Service.links.data"
 
151
);
 
152
 
 
153
ok(
 
154
   no_diff(
 
155
      $client->ua->{content}->{post}->[0] || '',
 
156
      "$sample/query-history/data001.send",
 
157
      cmd_output => 1,
 
158
   ),
 
159
   "Sent data file as multi-part resource (query-history/data001)"
 
160
) or diag(Dumper($client->ua->{content}->{post}));
 
161
 
 
162
ok(
 
163
   !-f "$tmpdir/query-history/1.data001.data",
 
164
   "Removed data file after sending successfully"
 
165
);
 
166
 
 
167
is(
 
168
   $ua->{request_objs}->[-1]->header('content-type'),
 
169
   'multipart/form-data; boundary=Ym91bmRhcnk',
 
170
   'Content-Type=multipart/form-data; boundary=Ym91bmRhcnk'
 
171
) or diag(Dumper($ua));
 
172
 
 
173
# #############################################################################
 
174
# Done.
 
175
# #############################################################################
 
176
done_testing;