File: MongoDBTest.pm

package info (click to toggle)
libmongodb-perl 2.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 10,264 kB
  • sloc: perl: 14,419; python: 299; makefile: 20; sh: 11
file content (372 lines) | stat: -rw-r--r-- 10,403 bytes parent folder | download | duplicates (3)
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#  Copyright 2013 - 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.

package MongoDBTest;

use strict;
use warnings;

use Exporter 'import';
use MongoDB;
use BSON;
use Test::More;
use Path::Tiny;
use Scalar::Util qw/isvstring/;
use boolean;
use version;

our @EXPORT_OK = qw(
  build_client
  get_test_db
  server_version
  server_type
  clear_testdbs
  get_capped
  skip_if_mongod
  skip_unless_mongod
  skip_unless_failpoints_available
  set_failpoint
  clear_failpoint
  skip_unless_sessions
  skip_unless_transactions
  to_snake_case
  remap_hashref_to_snake_case
  uri_escape
  get_unique_collection
  get_features
  check_min_server_version
  uuid_to_string
  skip_unless_min_version
);

my @testdbs;

sub _check_local_rs {

}

# abstract building a connection
sub build_client {
    my %args = @_;
    my $host =
        exists $args{host}  ? delete $args{host}
      : exists $ENV{MONGOD} ? $ENV{MONGOD}
      :                       'localhost';

    my $ssl;
    if ( $ENV{EVG_ORCH_TEST} && $ENV{SSL} eq 'ssl' ) {
        $ssl = {
            SSL_cert_file => $ENV{EVG_TEST_SSL_PEM_FILE},
            SSL_ca_file   => $ENV{EVG_TEST_SSL_CA_FILE},
        };
    }

    my $codec;
    if ( $ENV{PERL_MONGO_TEST_CODEC_WRAPPED} ) {
        $codec = BSON->new(
            ordered      => 1,
            wrap_dbrefs  => 1,
            wrap_numbers => 1,
            wrap_strings => 1,
        );
    }

    # allow whole test suite to be run with compression enabled
    if ( my $comp = $ENV{PERL_MONGO_TEST_COMPRESSION} ) {
        $args{compressors} ||= [$comp];
    }

    # long query timeout may help spurious failures on heavily loaded CI machines
    return MongoDB->connect(
        $host,
        {
            ssl                         => $ssl || $ENV{MONGO_SSL},
            socket_timeout_ms           => 120000,
            server_selection_timeout_ms => $ENV{ATLAS_PROXY} ? 10000 : 2000,
            server_selection_try_once   => 0,
            wtimeout                    => 15000,
            compressors                 => ['zlib'],
            retry_writes                => 1,
            ( $codec ? ( bson_codec => $codec ) : () ),
            %args,
        }
    );
}

sub get_test_db {
    my $conn   = shift;
    my $prefix = shift || 'testdb';
    my $testdb = $prefix . int( rand( 2**31 ) );
    my $db     = $conn->get_database($testdb) or die "Can't get database\n";
    push( @testdbs, $db );
    return $db;
}

sub get_unique_collection {
    my ( $db, $prefix, $options ) = @_;
    return $db->get_collection(
        sprintf( '%s_%d_%d', $prefix, time(), int( rand(999999) ) ), $options, );
}

sub get_capped {
    my ( $db, $name, %args ) = @_;
    $name ||= 'capped' . int( rand( 2**31 ) );
    $args{size} ||= 500_000;
    $db->run_command( [ create => $name, capped => true, %args ] );
    return $db->get_collection($name);
}

sub skip_unless_mongod {
    my $min_version = shift;
    my $conn;
    eval {
        $conn = build_client( server_selection_timeout_ms => 10000 );
        my $topo = $conn->_topology;
        $topo->scan_all_servers;
        my $link;
        eval { $link = $topo->get_writable_link }
          or die "couldn't connect: $@";
        $conn->get_database("admin")->run_command( { serverStatus => 1 } )
          or die "Database has auth enabled\n";
        my $server = $link->server;
        if ( !$ENV{MONGOD} && $topo->type eq 'Single' && $server->type =~ /^RS/ ) {
            # direct connection to RS member on default, so add set name
            # via MONGOD environment variable for subsequent use
            $ENV{MONGOD} = "mongodb://localhost/?replicaSet=" . $server->set_name;
        }
##        $conn->_topology->_dump;
    };

    if ($@) {
        ( my $err = $@ ) =~ s/\n//g;
        if ( $ENV{EVG_ORCH_TEST} ) {
            BAIL_OUT($err);
        }
        if ( $err =~ /couldn't connect|connection refused/i ) {
            $err = "no mongod on " . ( $ENV{MONGOD} || "localhost:27017" );
            $err .= ' and $ENV{MONGOD} not set' unless $ENV{MONGOD};
        }
        plan skip_all => "$err";
    }

    if ($min_version) {
        plan skip_all => "Requires MongoDB $min_version"
            if check_min_server_version($conn, $min_version);
    }
}

sub skip_if_mongod {
    eval {
        my $conn = build_client( server_selection_timeout_ms => 1000 );
        my $topo = $conn->_topology;
        $topo->scan_all_servers;
        # will throw if no servers available
        $topo->get_readable_link( MongoDB::ReadPreference->new( { mode => 'nearest' } ) );
    };
    if ( !$@ ) {
        plan skip_all => "Test can't start with a running mongod";
    }
}

sub skip_unless_failpoints_available {
    my ($arg) = @_;

    # Setting failpoints will make the tested server unusable for ordinary
    # purposes. As this is risky, the test requires the user to opt-in
    unless ( $ENV{FAILPOINT_TESTING} ) {
        plan skip_all => "\$ENV{FAILPOINT_TESTING} is false";
    }

    # Test::Harness 3.31 supports the t/testrules.yml file to ensure that
    # this test file won't be run in parallel other tests, since turning on
    # a fail point will interfere with other tests.
    if ( version->parse( $ENV{HARNESS_VERSION} ) < version->parse(3.31) ) {
        plan skip_all => "not safe to run fail points before Test::Harness 3.31";
    }

    # If running from t/ check that the file is in the test rules file.
    if ( $0 =~ m{^t/.*\.t$} ) {
        my $rules = path("t/testrules.yml")->slurp_utf8;
        plan skip_all => "$0 not listed in t/testrules.yml"
          unless $rules =~ m{seq:\s+\Q$0\E};
    }

    my $conn        = build_client;
    my $server_type = server_type($conn);

    my $param = eval {
        $conn->get_database('admin')
          ->run_command( [ getParameter => 1, enableTestCommands => 1 ] );
    };

    plan skip_all => "enableTestCommands is off"
      unless $param && $param->{enableTestCommands};

    # For transaction tests under mongos
    plan skip_all => "fail points not supported via mongos"
      if $server_type eq 'Mongos'
        && ( $conn->_topology->wire_version_ceil < 8
          || $arg->{skip_mongos} );
}

sub set_failpoint {
    my ( $client, $failpoint ) = @_;

    return unless defined $failpoint;
    _send_failpoint_admin_command( $client, [
        configureFailPoint => $failpoint->{configureFailPoint},
        mode => $failpoint->{mode},
        defined $failpoint->{data}
          ? ( data => $failpoint->{data} )
          : (),
    ]);
}

sub clear_failpoint {
    my ( $client, $failpoint ) = @_;

    return unless defined $failpoint;
    _send_failpoint_admin_command( $client, [
        configureFailPoint => $failpoint->{configureFailPoint},
        mode => 'off',
    ]);
}

# Failpoint commands must be sent to all servers if sharded
sub _send_failpoint_admin_command {
    my ( $client, $command ) = @_;

    if ( $client->_topology->type eq 'Sharded' ) {
        for my $server ( $client->_topology->all_servers ) {
            $client->_send_direct_admin_command( $server->address, $command );
        }
    } else {
        $client->send_admin_command( $command );
    }
}

sub skip_unless_sessions {
    my $conn = build_client;

    plan skip_all => "Session support not available"
      unless $conn->_topology->_supports_sessions;
}

sub skip_unless_transactions {
    my $conn = build_client;

    plan skip_all => "Transaction support not available"
      unless $conn->_topology->_supports_transactions;
}

sub server_version {
    my $conn          = shift;
    return $conn->_primary_server_version;
}

sub check_min_server_version {
    my ( $conn, $min_version ) = @_;
    unless ( ref($min_version) eq 'version') {
        unless ( isvstring($min_version) ) {
            $min_version = "v$min_version" unless $min_version =~ /^v/;
            $min_version .= ".0" unless $min_version =~ /^v\d+\.\d+.\d+$/;
        }
        $min_version = version->new($min_version);
    }
    my $server_version = server_version($conn);
    if ( $min_version > $server_version ) {
        return 1;
    }
    return 0;
}

sub skip_unless_min_version {
    my ( $conn, $min_version ) = @_;
    plan skip_all => "Requires MongoDB $min_version"
        if check_min_server_version( $conn, $min_version );
}

sub server_type {

    my $conn = shift;
    my $server_type;

    # check database type
    my $ismaster = $conn->get_database('admin')->run_command( { ismaster => 1 } );
    if ( exists $ismaster->{msg} && $ismaster->{msg} eq 'isdbgrid' ) {
        $server_type = 'Mongos';
    }
    elsif ( $ismaster->{ismaster} && exists $ismaster->{setName} ) {
        $server_type = 'RSPrimary';
    }
    elsif ( !exists $ismaster->{setName} && !$ismaster->{isreplicaset} ) {
        $server_type = 'Standalone';
    }
    else {
        $server_type = 'Unknown';
    }
    return $server_type;
}

sub get_features {
    my $conn = shift;
    my $topo = $conn->_topology;
    $topo->scan_all_servers;
    my $link;
    eval { $link = $topo->get_writable_link };
    return $link // MongoDB::_Link->new( address => "0:0" );
}

# URI escaping adapted from HTTP::Tiny
my %escapes = map { chr($_) => sprintf( "%%%02X", $_ ) } 0 .. 255;
my $unsafe_char = qr/[^A-Za-z0-9\-\._~]/;

sub uri_escape {
    my ($str) = @_;
    utf8::encode($str);
    $str =~ s/($unsafe_char)/$escapes{$1}/ge;
    return $str;
}

sub to_snake_case {
    my $t = shift;
    $t =~ s{([A-Z])}{_\L$1}g;
    return $t;
}

sub remap_hashref_to_snake_case {
    my $hash = shift;
    return {
        map {
            my $k = to_snake_case($_);
            $k => $hash->{$_}
        } keys %$hash
    };
}

sub uuid_to_string {
    my $uuid = shift;
    return join "-", unpack( "H8H4H4H4H12", $uuid );
}

sub clear_testdbs { @testdbs = () }

# cleanup test dbs
END {
    for my $db (@testdbs) {
        $db->drop;
    }
}

1;