1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
# Copyright 2018 - present MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
use strict;
use warnings;
use Test::More;
use Test::Fatal;
use MongoDB;
use MongoDB::Error;
use lib "t/lib";
use MongoDBTest
qw/skip_unless_mongod build_client get_test_db server_version server_type/;
skip_unless_mongod();
my $conn = build_client();
my $server_version = server_version($conn);
my $server_type = server_type($conn);
#--------------------------------------------------------------------------#
# Event callback for testing -- just closures over an array
#--------------------------------------------------------------------------#
my @events;
sub clear_events { @events = () }
sub event_count { scalar @events }
sub event_cb { push @events, $_[0] }
#--------------------------------------------------------------------------#
# Tests
#--------------------------------------------------------------------------#
subtest "Initialize client with monitoring callback" => sub {
clear_events();
my $mc = build_client( monitoring_callback => \&event_cb );
$mc->monitoring_callback->( { hello => "world" } );
is( event_count(), 1, "got an event" );
is( $events[0]->{hello}, "world", "correct event" );
};
subtest "run_command" => sub {
clear_events();
my $mc = build_client( monitoring_callback => \&event_cb, dt_type => undef );
$mc->send_admin_command( [ ismaster => 1 ] );
ok( event_count() >= 2, "got 2+ events" ) or return;
subtest "command_started" => sub {
my @started = grep { $_->{type} eq "command_started" } @events;
ok( scalar @started >= 1, "command_success count" ) or return;
# last command should be the one we ran
my $last_start = $started[-1];
my $ok = 1;
$ok &&= is( $last_start->{databaseName}, "admin", "databaseName" );
$ok &&= is( $last_start->{commandName}, "ismaster", "commandName" );
$ok &&= is( $last_start->{command}{ismaster}, 1, "command" );
$ok &&= ok( defined $last_start->{requestId}, "requestId" );
$ok &&= like( $last_start->{connectionId}, qr/^[^:]+:\d+$/, "connectionId" );
diag explain $last_start unless $ok;
};
subtest "command_succeeded" => sub {
my @success = grep { $_->{type} eq "command_succeeded" } @events;
ok( scalar @success >= 1, "command_succeeded count" ) or return;
# last command should be the one we ran
my $last_success = $success[-1];
my $ok = 1;
$ok &&= is( $last_success->{databaseName}, "admin", "databaseName" );
$ok &&= is( $last_success->{commandName}, "ismaster", "commandName" );
$ok &&= ok( defined $last_success->{requestId}, "requestId" );
$ok &&= ok( $last_success->{durationSecs} > 0, "duration" );
$ok &&= like( $last_success->{connectionId}, qr/^[^:]+:\d+$/, "connectionId" );
diag explain $last_success unless $ok;
};
subtest "command_failed" => sub {
clear_events();
eval { $mc->send_admin_command( [ notarealcommand => 1 ] ) };
ok( $@, "Got exception" );
ok( event_count() >= 2, "got 2+ events" ) or return;
my @failure = grep { $_->{type} eq "command_failed" } @events;
ok( scalar @failure >= 1, "command_failed count" ) or return;
# last command should be the one we ran
my $last_failure = $failure[-1];
my $ok = 1;
$ok &&= is( $last_failure->{databaseName}, "admin", "databaseName" );
$ok &&= is( $last_failure->{commandName}, "notarealcommand", "commandName" );
$ok &&= ok( defined $last_failure->{requestId}, "requestId" );
$ok &&= ok( $last_failure->{durationSecs} > 0, "duration" );
$ok &&= like( $last_failure->{connectionId}, qr/^[^:]+:\d+$/, "connectionId" );
$ok &&= like( $last_failure->{failure}, qr/no such (?:command|cmd)|command not found/i, "failure" );
$ok &&= isa_ok( $last_failure->{reply}, 'HASH', "reply");
diag explain $last_failure unless $ok;
};
};
subtest "write commands" => sub {
clear_events();
my $coll = _coll_with_monitor( "test_write_events" );
_test_writes($coll);
};
subtest "unack'd writes" => sub {
clear_events();
my $coll = _coll_with_monitor( "test_write_events", { write_concern => { w => 0 } } );
_test_writes($coll);
};
subtest "find and getMore" => sub {
clear_events();
my $coll = _coll_with_monitor("test_read_events");
$coll->insert_many( [ map { ; { x => $_ } } 1 .. 100 ] );
# Clear after insert so we're only looking for find/getmore
clear_events();
my @docs = $coll->find( { x => { '$gt' => 10 } }, { batchSize => 30 } )->all;
subtest "command_started" => sub {
my @started = grep { $_->{type} eq "command_started" } @events;
ok( scalar @started >= 2, "got events" );
my $ok = 1;
$ok &&= is( (scalar grep { $_->{commandName} eq 'find' } @started), 1, "find command" );
$ok &&= is( (scalar grep { $_->{commandName} eq 'getMore' } @started), 3, "getMore commands" );
diag explain \@started unless $ok;
};
subtest "command_succeeded" => sub {
my @succeeded = grep { $_->{type} eq "command_succeeded" } @events;
ok( scalar @succeeded >= 2, "got events" );
my $ok = 1;
$ok &&= is( (scalar grep { $_->{commandName} eq 'find' } @succeeded), 1, "find command" );
$ok &&= is( (scalar grep { $_->{commandName} eq 'getMore' } @succeeded), 3, "getMore commands" );
diag explain \@succeeded unless $ok;
};
subtest "command_failed" => sub {
clear_events();
eval { $coll->find( { x => { '$xxxx' => 10 } }, { batchSize => 30 } )->all };
ok( $@, "Got exception" );
my @failed = grep { $_->{type} eq "command_failed" } @events;
ok( scalar @failed >= 1, "got events" );
my $ok = 1;
$ok &&= is( (scalar grep { $_->{commandName} eq 'find' } @failed), 1, "find command" );
diag explain \@failed unless $ok;
};
};
subtest "exceptions are command_failed" => sub {
subtest 'insert' => sub {
no warnings 'redefine';
my $coll = _coll_with_monitor("test");
$coll->insert_one({}); # force topology discovery
my $err;
{
local *MongoDB::_Link::write = \&_throw_mock_network_error;
clear_events();
eval {$coll->insert_one({})};
$err = $@;
}
# force reset topology status
$coll->client->topology_status( refresh => 1 );
ok( $err, "got exception" );
my @failed = grep { $_->{type} eq "command_failed" } @events;
ok( scalar @failed >= 1, "got events" );
my $last_failure = $failed[-1];
my $ok = 1;
$ok &&= is( $last_failure->{commandName}, "insert", "commandName" );
$ok &&= ok( defined $last_failure->{requestId}, "requestId" );
$ok &&= ok( $last_failure->{durationSecs} > 0, "duration" );
$ok &&= like( $last_failure->{connectionId}, qr/^[^:]+:\d+$/, "connectionId" );
$ok &&= like( $last_failure->{failure}, qr/fake network error/, "failure msg" );
$ok &&= isa_ok( $last_failure->{eval_error}, "MongoDB::NetworkError", "eval_error" );
diag explain $last_failure unless $ok;
};
subtest "insert unack'd" => sub {
no warnings 'redefine';
my $coll = _coll_with_monitor("test", { write_concern => { w => 0 } });
$coll->insert_one({}); # force topology discovery
my $err;
{
local *MongoDB::_Link::write = \&_throw_mock_network_error;
clear_events();
eval {$coll->insert_one({})};
$err = $@;
}
# force reset topology status
$coll->client->topology_status( refresh => 1 );
ok( $err, "got exception" );
my @failed = grep { $_->{type} eq "command_failed" } @events;
ok( scalar @failed >= 1, "got events" );
my $last_failure = $failed[-1];
my $ok = 1;
$ok &&= is( $last_failure->{commandName}, "insert", "commandName" );
$ok &&= ok( defined $last_failure->{requestId}, "requestId" );
$ok &&= ok( $last_failure->{durationSecs} > 0, "duration" );
$ok &&= like( $last_failure->{connectionId}, qr/^[^:]+:\d+$/, "connectionId" );
$ok &&= like( $last_failure->{failure}, qr/fake network error/, "failure msg" );
$ok &&= isa_ok( $last_failure->{eval_error}, "MongoDB::NetworkError", "eval_error" );
diag explain $last_failure unless $ok;
};
subtest 'find' => sub {
no warnings 'redefine';
my $coll = _coll_with_monitor("test");
$coll->insert_one({}); # force topology discovery
my $err;
{
local *MongoDB::_Link::write = \&_throw_mock_network_error;
clear_events();
eval {$coll->find({})->all};
$err = $@;
}
# force reset topology status
$coll->client->topology_status( refresh => 1 );
ok( $err, "got exception" );
my @failed = grep { $_->{type} eq "command_failed" } @events;
ok( scalar @failed >= 1, "got events" );
my $last_failure = $failed[-1];
my $ok = 1;
$ok &&= is( $last_failure->{commandName}, "find", "commandName" );
$ok &&= ok( defined $last_failure->{requestId}, "requestId" );
$ok &&= ok( $last_failure->{durationSecs} > 0, "duration" );
$ok &&= like( $last_failure->{connectionId}, qr/^[^:]+:\d+$/, "connectionId" );
$ok &&= like( $last_failure->{failure}, qr/fake network error/, "failure msg" );
$ok &&= isa_ok( $last_failure->{eval_error}, "MongoDB::NetworkError", "eval_error" );
diag explain $last_failure unless $ok;
};
};
subtest 'redactions' => sub {
clear_events();
my $mc = build_client( monitoring_callback => \&event_cb );
my $testdb = get_test_db($mc);
$testdb->run_command([getnonce => 1]);
my ($started, $succeeded) =
grep { $_->{commandName} eq 'getnonce' }
@events;
is $started->{type}, 'command_started', 'start event';
is $succeeded->{type}, 'command_succeeded', 'success event';
ok defined($started->{command}), 'command not empty';
ok defined($succeeded->{reply}), 'reply not empty';
is scalar(keys %{ $started->{command} }), 0, 'no command fields';
is scalar(keys %{ $succeeded->{reply} }), 0, 'no reply fields';
};
sub _coll_with_monitor {
# retry writes messes with event counting and iterating over events from
# monitoring callback
my $mc = build_client( monitoring_callback => \&event_cb, retry_writes => 0 );
my $testdb = get_test_db($mc);
my $col = $testdb->coll(@_);
}
sub _throw_mock_network_error {
MongoDB::NetworkError->throw("fake network error");
}
sub _test_writes {
my ($coll) = shift;
local $Test::Builder::Level = $Test::Builder::Level + 1;
$coll->insert_one( { x => 1 } );
$coll->replace_one( { x => 1 }, { x => 0 } );
$coll->delete_one( { x => 0 } );
subtest "command_started" => sub {
my @started = grep { $_->{type} eq "command_started" } @events;
ok( scalar @started >= 3, "got events" ) or return;
my $ok = 1;
for my $cmd (qw/insert update delete/) {
$ok &&=
ok( ( scalar grep { $_->{commandName} eq $cmd } @started ), "saw $cmd command" );
}
diag explain \@started unless $ok;
};
subtest "command_succeeded" => sub {
my @succeeded = grep { $_->{type} eq "command_succeeded" } @events;
ok( scalar @succeeded >= 3, "got events" ) or return;
my $ok = 1;
for my $cmd (qw/insert update delete/) {
$ok &&=
ok( ( scalar grep { $_->{commandName} eq $cmd } @succeeded ), "saw $cmd command" );
}
diag explain \@succeeded unless $ok;
};
subtest "failed write is still command_succeeded" => sub {
plan skip_all => "w:0 won't error"
unless $coll->write_concern->is_acknowledged;
$coll->insert_one( { _id => 123 } );
clear_events();
eval { $coll->insert_one( { _id => 123 } ) };
ok( $@, "Got exception" );
my @succeeded = grep { $_->{type} eq "command_succeeded" } @events;
ok( scalar @succeeded >= 1, "got events" ) or return;
ok( ( scalar grep { $_->{commandName} eq 'insert' } @succeeded ), "saw insert command" )
or diag explain \@succeeded;
};
}
done_testing;
|