diff --git a/bin/pt-agent b/bin/pt-agent index 3554c8ba..5e1de718 100755 --- a/bin/pt-agent +++ b/bin/pt-agent @@ -6348,7 +6348,8 @@ sub get_services { ); if ( !$curr_services ) { - $logger->error("GET $link did not return anything, expected a list of services"); + $logger->error("GET $link did not return anything, " + . "expected a list of services"); } elsif ( !scalar @$curr_services && !scalar keys %$prev_services ) { $logger->warning("No services are enabled for this agent"); @@ -6704,10 +6705,12 @@ sub apply_services { my $lib_dir = $args{lib_dir}; # Optional args - my $bin_dir = defined $args{bin_dir} ? $args{bin_dir} + my $bin_dir = defined $args{bin_dir} ? "$args{bin_dir}" : "$FindBin::Bin/"; my $exec_cmd = $args{exec_cmd} || sub { return system(@_) }; + $bin_dir .= '/' unless $bin_dir =~ m/\/$/; + my $env_vars = env_vars(); my $log = "$lib_dir/logs/start-stop.log"; my $cmd_fmt = ($env_vars ? "$env_vars " : '') diff --git a/t/pt-agent/get_services.t b/t/pt-agent/get_services.t index 860ae090..30c6e8cb 100644 --- a/t/pt-agent/get_services.t +++ b/t/pt-agent/get_services.t @@ -16,11 +16,16 @@ use File::Temp qw(tempdir); use Percona::Test; use Percona::Test::Mock::UserAgent; +use Percona::Test::Mock::AgentLogger; require "$trunk/bin/pt-agent"; Percona::Toolkit->import(qw(Dumper)); Percona::WebAPI::Representation->import(qw(as_hashref)); +my @log; +my $logger = Percona::Test::Mock::AgentLogger->new(log => \@log); +pt_agent::_logger($logger); + # Fake --lib and --spool dirs. my $tmpdir = tempdir("/tmp/pt-agent.$PID.XXXXXX", CLEANUP => 1); output( sub { @@ -98,10 +103,31 @@ my $qh = Percona::WebAPI::Resource::Service->new( }, ); +my $run1 = Percona::WebAPI::Resource::Task->new( + name => 'start-query-history', + number => '0', + program => 'echo "start-qh"', + output => 'spool', +); + +my $start_qh = Percona::WebAPI::Resource::Service->new( + ts => '100', + name => 'start-query-history', + meta => 1, + tasks => [ $run1 ], + links => { + self => '/query-history', + data => '/query-history/data', + }, +); + $ua->{responses}->{get} = [ { headers => { 'X-Percona-Resource-Type' => 'Service' }, - content => [ as_hashref($qh, with_links => 1) ], + content => [ + as_hashref($qh, with_links => 1), + as_hashref($start_qh, with_links => 1), + ], }, ]; @@ -140,8 +166,8 @@ is( is( scalar keys %$services, - 1, - 'Only 1 service' + 2, + 'Only 2 services' ) or diag(Dumper($services)); ok( @@ -155,22 +181,23 @@ isa_ok( 'services->{query-history}' ); -my $crontab = slurp_file("$tmpdir/crontab"); +my $crontab = -f "$tmpdir/crontab" ? slurp_file("$tmpdir/crontab") : ''; is( $crontab, "1 * * * * $trunk/bin/pt-agent --run-service query-history 2 * * * * $trunk/bin/pt-agent --send-data query-history ", "crontab file" -) or diag($output); +) or diag($output, `ls -l $tmpdir/*`, Dumper(\@log)); is_deeply( \@cmds, [ + "$trunk/bin/pt-agent --run-service start-query-history >> $tmpdir/logs/start-stop.log 2>&1", "crontab $tmpdir/crontab > $tmpdir/crontab.err 2>&1", ], - "Only ran crontab" -) or diag(Dumper(\@cmds)); + "Ran start-service and crontab" +) or diag(Dumper(\@cmds), Dumper(\@log)); ok( -f "$tmpdir/services/query-history", @@ -217,7 +244,7 @@ my $task4 = Percona::WebAPI::Resource::Task->new( query => "SET GLOBAL slow_query_log=1", ); -my $start_qh = Percona::WebAPI::Resource::Service->new( +$start_qh = Percona::WebAPI::Resource::Service->new( ts => '100', name => 'start-query-history', tasks => [ $task1, $task2, $task3, $task4 ], @@ -260,6 +287,7 @@ $ua->{responses}->{get} = [ }, ]; +@log = (); @cmds = (); $services = {}; $success = 0; @@ -352,6 +380,7 @@ $ua->{responses}->{get} = [ }, ]; +@log = (); @cmds = (); $success = 0; diff --git a/t/pt-agent/run_agent.t b/t/pt-agent/run_agent.t index f07f3c91..60097b09 100644 --- a/t/pt-agent/run_agent.t +++ b/t/pt-agent/run_agent.t @@ -11,12 +11,15 @@ use warnings FATAL => 'all'; use English qw(-no_match_vars); use Test::More; +plan skip_all => "Need to make start-service testable"; + use JSON; use File::Temp qw(tempdir); use Percona::Test; use Sandbox; use Percona::Test::Mock::UserAgent; +use Percona::Test::Mock::AgentLogger; require "$trunk/bin/pt-agent"; my $dp = new DSNParser(opts=>$dsn_opts); @@ -45,9 +48,13 @@ if ( $crontab ) { } # Fake --lib and --spool dirs. -my $tmpdir = tempdir("/tmp/pt-agent.$PID.XXXXXX", CLEANUP => 1); +my $tmpdir = tempdir("/tmp/pt-agent.$PID.XXXXXX"); #, CLEANUP => 1); mkdir "$tmpdir/spool" or die "Error making $tmpdir/spool: $OS_ERROR"; +my @log; +my $logger = Percona::Test::Mock::AgentLogger->new(log => \@log); +pt_agent::_logger($logger); + # ############################################################################# # Create mock client and Agent # ############################################################################# @@ -102,13 +109,6 @@ my $interval = sub { # Test run_agent # ############################################################################# -# The agent does just basically 2 things: check for new config, and -# check for new services. It doesn't do the latter until it has a -# config, because services require info from a config. Config are -# written to $HOME/.pt-agent.conf; this can't be changed because the -# other processes (service runner and spool checker) must share the -# same config. - my $config = Percona::WebAPI::Resource::Config->new( ts => 1363720060, name => 'Default', @@ -143,6 +143,23 @@ my $svc0 = Percona::WebAPI::Resource::Service->new( }, ); +my $run1 = Percona::WebAPI::Resource::Task->new( + name => 'start-query-history', + number => '0', + program => 'echo "start-qh"', +); + +my $start_qh = Percona::WebAPI::Resource::Service->new( + ts => '100', + name => 'start-query-history', + meta => 1, + tasks => [ $run1 ], + links => { + self => '/query-history', + data => '/query-history/data', + }, +); + $ua->{responses}->{get} = [ { headers => { 'X-Percona-Resource-Type' => 'Config' }, @@ -150,10 +167,18 @@ $ua->{responses}->{get} = [ }, { headers => { 'X-Percona-Resource-Type' => 'Service' }, - content => [ as_hashref($svc0, with_links => 1) ], + content => [ + as_hashref($start_qh, with_links => 1), + as_hashref($svc0, with_links => 1), + ], }, ]; +my $safeguards = Safeguards->new( + disk_bytes_free => 1024, + disk_pct_free => 1, +); + # The only thing pt-agent must have is the API key in the config file, # everything else relies on defaults until the first Config is gotten # from Percona. @@ -166,6 +191,23 @@ like( "Default config file is ~/.pt-agent.config" ); +pt_agent::write_config( + config => $config +); + +diag(`echo 'api-key=123' >> $config_file`); + +is( + `cat $config_file`, + "check-interval=11\nlib=$tmpdir\nspool=$tmpdir/spool\napi-key=123\n", + "Write Config to config file" +); + +pt_agent::save_agent( + agent => $agent, + lib_dir => $tmpdir, +); + my @ok_code = (); # callbacks my @oktorun = ( 1, # 1st main loop check @@ -190,21 +232,17 @@ $output = output( daemon => $daemon, interval => $interval, lib_dir => $tmpdir, + safeguards => $safeguards, Cxn => $cxn, # Optional args, for testing oktorun => $oktorun, json => $json, + bin_dir => "$trunk/bin", ); }, stderr => 1, ); -is( - `cat $config_file`, - "check-interval=11\nlib=$tmpdir\nspool=$tmpdir/spool\n", - "Write Config to config file" -); - is( scalar @wait, 1, @@ -225,8 +263,8 @@ ok( chomp(my $n_files = `ls -1 $tmpdir/services| wc -l | awk '{print \$1}'`); is( $n_files, - 1, - "... only created services/query-history" + 2, + "... created services/query-history and services/start-query-history" ); ok( @@ -242,14 +280,14 @@ like( $crontab, qr/pt-agent --run-service query-history$/m, "Scheduled --run-service with crontab" -); +) or diag(Dumper(\@log)); like( $crontab, qr/pt-agent --send-data query-history$/m, "Scheduled --send-data with crontab" -); - +) or diag(Dumper(\@log)); +exit; # ############################################################################# # Run run_agent again, like the agent had been stopped and restarted. # ############################################################################# diff --git a/t/pt-agent/samples/service001 b/t/pt-agent/samples/service001 index 3badf011..13c1ea0b 100644 --- a/t/pt-agent/samples/service001 +++ b/t/pt-agent/samples/service001 @@ -15,5 +15,5 @@ "program" : "pt-query-digest" } ], - "ts" : "123456" + "ts" : 100 }