mirror of
https://github.com/percona/percona-toolkit.git
synced 2025-09-15 16:00:23 +00:00
Implement and test --check-spool. Make Mock/UserAgent save put and post data in an array. Make Percona/WebAPI/Client accept ready-made text resources, for multi-part resources.
This commit is contained in:
129
t/pt-agent/check_spool.t
Normal file
129
t/pt-agent/check_spool.t
Normal file
@@ -0,0 +1,129 @@
|
||||
#!/usr/bin/env perl
|
||||
|
||||
BEGIN {
|
||||
die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
|
||||
unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
|
||||
unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
|
||||
};
|
||||
|
||||
use strict;
|
||||
use warnings FATAL => 'all';
|
||||
use English qw(-no_match_vars);
|
||||
use Test::More;
|
||||
use JSON;
|
||||
use File::Temp qw(tempdir);
|
||||
|
||||
use Percona::Test;
|
||||
use Percona::Test::Mock::UserAgent;
|
||||
require "$trunk/bin/pt-agent";
|
||||
|
||||
Percona::Toolkit->import(qw(Dumper have_required_args));
|
||||
Percona::WebAPI::Representation->import(qw(as_hashref));
|
||||
|
||||
my $sample = "t/pt-agent/samples";
|
||||
|
||||
# #############################################################################
|
||||
# Create mock client and Agent
|
||||
# #############################################################################
|
||||
|
||||
# These aren't the real tests yet: to run_agent(), first we need
|
||||
# a client and Agent, so create mock ones.
|
||||
|
||||
my $json = JSON->new;
|
||||
$json->allow_blessed([]);
|
||||
$json->convert_blessed([]);
|
||||
|
||||
my $ua = Percona::Test::Mock::UserAgent->new(
|
||||
encode => sub { my $c = shift; return $json->encode($c || {}) },
|
||||
);
|
||||
|
||||
# Create cilent, get entry links
|
||||
my $links = {
|
||||
agents => '/agents',
|
||||
config => '/agents/1/config',
|
||||
services => '/agents/1/services',
|
||||
'query-monitor' => '/query-monitor',
|
||||
};
|
||||
|
||||
$ua->{responses}->{get} = [
|
||||
{
|
||||
content => $links,
|
||||
},
|
||||
];
|
||||
|
||||
my $client = eval {
|
||||
Percona::WebAPI::Client->new(
|
||||
api_key => '123',
|
||||
ua => $ua,
|
||||
);
|
||||
};
|
||||
is(
|
||||
$EVAL_ERROR,
|
||||
'',
|
||||
'Create mock client'
|
||||
) or die;
|
||||
|
||||
my $agent = Percona::WebAPI::Resource::Agent->new(
|
||||
id => '123',
|
||||
hostname => 'prod1',
|
||||
);
|
||||
|
||||
is_deeply(
|
||||
as_hashref($agent),
|
||||
{
|
||||
id => '123',
|
||||
hostname => 'prod1',
|
||||
},
|
||||
'Create mock Agent'
|
||||
) or die;
|
||||
|
||||
# #############################################################################
|
||||
# Test check_spool()
|
||||
# #############################################################################
|
||||
|
||||
my $tmpdir = tempdir("/tmp/pt-agent.$PID.XXXXXX", CLEANUP => 1);
|
||||
mkdir "$tmpdir/query-monitor"
|
||||
or die "Cannot mkdir $tmpdir/query-monitor: $OS_ERROR";
|
||||
`cp $trunk/$sample/query-monitor/data001 $tmpdir/query-monitor`;
|
||||
|
||||
$ua->{responses}->{post} = [
|
||||
{
|
||||
content => $links,
|
||||
},
|
||||
];
|
||||
|
||||
my $output = output(
|
||||
sub {
|
||||
pt_agent::check_spool(
|
||||
client => $client,
|
||||
agent => $agent,
|
||||
spool_dir => $tmpdir,
|
||||
),
|
||||
},
|
||||
stderr => 1,
|
||||
);
|
||||
|
||||
is(
|
||||
scalar @{$client->ua->{content}->{post}},
|
||||
1,
|
||||
"Only sent 1 resource"
|
||||
) or diag(Dumper($client->ua->{content}->{post}));
|
||||
|
||||
ok(
|
||||
no_diff(
|
||||
$client->ua->{content}->{post}->[0] || '',
|
||||
"$sample/query-monitor/data001.send",
|
||||
cmd_output => 1,
|
||||
),
|
||||
"Sent data file as multi-part resource (query-monitor/data001)"
|
||||
) or diag(Dumper($client->ua->{content}->{post}));
|
||||
|
||||
ok(
|
||||
!-f "$tmpdir/query-monitor/data001",
|
||||
"Removed data file after sending successfully"
|
||||
);
|
||||
|
||||
# #############################################################################
|
||||
# Done.
|
||||
# #############################################################################
|
||||
done_testing;
|
7
t/pt-agent/samples/query-monitor/data001
Normal file
7
t/pt-agent/samples/query-monitor/data001
Normal file
@@ -0,0 +1,7 @@
|
||||
[
|
||||
{
|
||||
query_id: 1,
|
||||
arg: "select * from t where id = 1",
|
||||
Query_time: 0.123456,
|
||||
}
|
||||
]
|
9
t/pt-agent/samples/query-monitor/data001.send
Normal file
9
t/pt-agent/samples/query-monitor/data001.send
Normal file
@@ -0,0 +1,9 @@
|
||||
{"id":"123","hostname":"prod1"}
|
||||
--Ym91bmRhcnk=
|
||||
[
|
||||
{
|
||||
query_id: 1,
|
||||
arg: "select * from t where id = 1",
|
||||
Query_time: 0.123456,
|
||||
}
|
||||
]
|
Reference in New Issue
Block a user