File: test.rb

package info (click to toggle)
ruby-mongo 2.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,020 kB
  • sloc: ruby: 110,810; makefile: 5
file content (379 lines) | stat: -rw-r--r-- 13,148 bytes parent folder | download
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
373
374
375
376
377
378
379
# frozen_string_literal: true
# rubocop:todo all

# Copyright (C) 2014-2020 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.

module Mongo
  module Transactions

    # Represents a single transaction test.
    #
    # @since 2.6.0
    class TransactionsTest < CRUD::CRUDTestBase
      include MongosMacros

      attr_reader :expected_results
      attr_reader :skip_reason

      attr_reader :results

      # @return [ Crud::Spec ] the top-level YAML specification object
      attr_reader :spec

      # Instantiate the new CRUDTest.
      #
      # @example Create the test.
      #   TransactionTest.new(data, test)
      #
      # @param [ Crud::Spec ] crud_spec The top level YAML specification object.
      # @param [ Array<Hash> ] data The documents the collection
      # must have before the test runs.
      # @param [ Hash ] test The test specification.
      # @param [ true | false | Proc ] expectations_bson_types Whether bson
      #   types should be expected. If a Proc is given, it is invoked with the
      #   test as its argument, and should return true or false.
      #
      # @since 2.6.0
      def initialize(crud_spec, data, test, expectations_bson_types: true)
        test = IceNine.deep_freeze(test)
        @spec = crud_spec
        @data = data || []
        @description = test['description']
        @client_options = {
          # Disable legacy read & write retries, so that when spec tests
          # disable modern retries we do not retry at all instead of using
          # legacy retries which is contrary to what the tests want.
          max_read_retries: 0,
          max_write_retries: 0,
          app_name: 'Tx spec - test client',
        }.update(::Utils.convert_client_options(test['clientOptions'] || {}))

        @fail_point_command = test['failPoint']

        @session_options = if opts = test['sessionOptions']
          Hash[opts.map do |session_name, options|
            [session_name.to_sym, ::Utils.convert_operation_options(options)]
          end]
        else
          {}
        end
        @skip_reason = test['skipReason']
        @multiple_mongoses = test['useMultipleMongoses']

        operations = test['operations']
        @operations = operations.map do |op|
          Operation.new(self, op)
        end

        if expectations_bson_types.respond_to?(:call)
          expectations_bson_types = expectations_bson_types[self]
        end

        mode = if expectations_bson_types then :bson else nil end
        @expectations = BSON::ExtJSON.parse_obj(test['expectations'], mode: mode)

        if test['outcome']
          @outcome = Mongo::CRUD::Outcome.new(BSON::ExtJSON.parse_obj(test['outcome'], mode: mode))
        end

        @expected_results = operations.map do |o|
          o = BSON::ExtJSON.parse_obj(o, mode: :bson)

          # We check both o.key('error') and o['error'] to provide a better
          # error message in case error: false is ever needed in the tests
          if o.key?('error')
            if o['error']
              {'error' => true}
            else
              raise "Unsupported error value #{o['error']}"
            end
          else
            result = o['result']
            next result unless result.class == Hash

            # Change maps of result ids to arrays of ids
            result.dup.tap do |r|
              r.each do |k, v|
                next unless ['insertedIds', 'upsertedIds'].include?(k)
                r[k] = v.to_a.sort_by(&:first).map(&:last)
              end
            end
          end
        end
      end

      attr_reader :outcome

      def multiple_mongoses?
        @multiple_mongoses
      end

      def support_client
        @support_client ||= ClientRegistry.instance.global_client('root_authorized').use(@spec.database_name)
      end

      def admin_support_client
        @admin_support_client ||= support_client.use('admin')
      end

      def test_client
        @test_client ||= begin
          sdam_proc = lambda do |test_client|
            test_client.subscribe(Mongo::Monitoring::COMMAND, command_subscriber)
            test_client.subscribe(Mongo::Monitoring::TOPOLOGY_OPENING, sdam_subscriber)
            test_client.subscribe(Mongo::Monitoring::SERVER_OPENING, sdam_subscriber)
            test_client.subscribe(Mongo::Monitoring::SERVER_DESCRIPTION_CHANGED, sdam_subscriber)
            test_client.subscribe(Mongo::Monitoring::TOPOLOGY_CHANGED, sdam_subscriber)
            test_client.subscribe(Mongo::Monitoring::SERVER_CLOSED, sdam_subscriber)
            test_client.subscribe(Mongo::Monitoring::TOPOLOGY_CLOSED, sdam_subscriber)
            test_client.subscribe(Mongo::Monitoring::CONNECTION_POOL, sdam_subscriber)
          end

          if kms_providers = @client_options.dig(:auto_encryption_options, :kms_providers)
            @client_options[:auto_encryption_options][:kms_providers] = kms_providers.map do |provider, opts|
              case provider
              when :aws_temporary
                [
                  :aws,
                  {
                    access_key_id: SpecConfig.instance.fle_aws_temp_key,
                    secret_access_key: SpecConfig.instance.fle_aws_temp_secret,
                    session_token: SpecConfig.instance.fle_aws_temp_session_token,
                  }
                ]
              when :aws_temporary_no_session_token
                [
                  :aws,
                  {
                    access_key_id: SpecConfig.instance.fle_aws_temp_key,
                    secret_access_key: SpecConfig.instance.fle_aws_temp_secret,
                  }
                ]
              else
                [provider, opts]
              end
            end.to_h
          end

          if @client_options[:auto_encryption_options] && SpecConfig.instance.crypt_shared_lib_path
            @client_options[:auto_encryption_options][:extra_options] ||= {}
            @client_options[:auto_encryption_options][:extra_options][:crypt_shared_lib_path] = SpecConfig.instance.crypt_shared_lib_path
          end

          ClientRegistry.instance.new_local_client(
            SpecConfig.instance.addresses,
            SpecConfig.instance.authorized_test_options.merge(
              database: @spec.database_name,
              auth_source: SpecConfig.instance.auth_options[:auth_source] || 'admin',
              sdam_proc: sdam_proc,
            ).merge(@client_options))
        end
      end

      def command_subscriber
        @command_subscriber ||= Mrss::EventSubscriber.new
      end

      def sdam_subscriber
        @sdam_subscriber ||= Mrss::EventSubscriber.new(name: 'sdam subscriber')
      end

      # Run the test.
      #
      # @example Run the test.
      #   test.run
      #
      # @return [ Result ] The result of running the test.
      #
      # @since 2.6.0
      def run
        @threads = {}

        results = @operations.map do |op|
          target = resolve_target(test_client, op)
          if op.needs_session?
            context = CRUD::Context.new(
              session0: session0,
              session1: session1,
              sdam_subscriber: sdam_subscriber,
              threads: @threads,
              primary_address: @primary_address,
            )
          else
            # Hack to support write concern operations tests, which are
            # defined to use transactions format but target pre-3.6 servers
            # that do not support sessions
            target ||= support_client
            context = CRUD::Context.new(
              sdam_subscriber: sdam_subscriber,
              threads: @threads,
              primary_address: @primary_address,
            )
          end

          op.execute(target, context).tap do
            @threads = context.threads
            @primary_address = context.primary_address
          end
        end

        session0_id = @session0&.session_id
        session1_id = @session1&.session_id

        @session0&.end_session
        @session1&.end_session

        actual_events = ::Utils.yamlify_command_events(command_subscriber.started_events)
        actual_events = actual_events.reject do |event|
          event['command_started_event']['command']['endSessions']
        end
        actual_events.each do |e|

          # Replace the session id placeholders with the actual session ids.
          payload = e['command_started_event']
          if @session0
            payload['command']['lsid'] = 'session0' if payload['command']['lsid'] == session0_id
          end
          if @session1
            payload['command']['lsid'] = 'session1' if payload['command']['lsid'] == session1_id
          end

        end

        @results = {
          results: results,
          contents: @result_collection.with(
            read: {mode: 'primary'},
            read_concern: { level: 'local' },
          ).find.sort(_id: 1).to_a,
          events: actual_events,
        }
      end

      def setup_test
        begin
          admin_support_client.command(killAllSessions: [])
        rescue Mongo::Error
        end

        if ClusterConfig.instance.fcv_ish >= '4.2'
          ::Utils.mongos_each_direct_client do |direct_client|
            direct_client.command(configureFailPoint: 'failCommand', mode: 'off')
          end
        end

        key_vault_coll = support_client
        .use(:keyvault)[:datakeys]
        .with(write: { w: :majority })

        key_vault_coll.drop
        # Insert data into the key vault collection if required to do so by
        # the tests.
        if @spec.key_vault_data && !@spec.key_vault_data.empty?
          key_vault_coll.insert_many(@spec.key_vault_data)
        end

        encrypted_fields = @spec.encrypted_fields if @spec.encrypted_fields
        coll = support_client[@spec.collection_name].with(write: { w: :majority })
        coll.drop(encrypted_fields: encrypted_fields)

        # Place a jsonSchema validator on the collection if required to do so
        # by the tests.
        collection_validator = if @spec.json_schema
          { '$jsonSchema' => @spec.json_schema }
        else
          {}
        end

        create_collection_spec = {
          create: @spec.collection_name,
          validator: collection_validator,
          writeConcern: { w: 'majority' }
        }

        create_collection_spec[:encryptedFields] = encrypted_fields if encrypted_fields
        support_client.command(create_collection_spec)

        coll.insert_many(@data) unless @data.empty?

        if description =~ /distinct/ || @operations.any? { |op| op.name == 'distinct' }
          run_mongos_distincts(@spec.database_name, 'test')
        end

        admin_support_client.command(@fail_point_command) if @fail_point_command

        @collection = test_client[@spec.collection_name]

        # Client-side encryption tests require the use of a separate client
        # without auto_encryption_options for querying results.
        result_collection_name = outcome&.collection_name || @spec.collection_name
        @result_collection = support_client.use(@spec.database_name)[result_collection_name]

        # DRIVERS-2816, adjusted for legacy spec runner
        @cluster_time = support_client.command(ping: 1).cluster_time
      end

      def teardown_test

        if @fail_point_command
          admin_support_client.command(configureFailPoint: 'failCommand', mode: 'off')
        end

        if $disable_fail_points
          $disable_fail_points.each do |(fail_point_command, address)|
            client = ClusterTools.instance.direct_client(address,
              database: 'admin')
            client.command(configureFailPoint: fail_point_command['configureFailPoint'],
              mode: 'off')
          end
          $disable_fail_points = nil
        end

        if @test_client
          @test_client.cluster.session_pool.end_sessions
        end
      end

      def resolve_target(client, operation)
        case operation.object
        when 'session0'
          session0
        when 'session1'
          session1
        when 'testRunner'
          # We don't actually use this target in any way.
          nil
        else
          super
        end
      end

      def new_session(options)
        test_client.start_session(options || {}).tap do |s|
          # DRIVERS-2816, adjusted for legacy spec runner
          s.advance_cluster_time(@cluster_time)
        end
      end

      def session0
        @session0 ||= new_session(@session_options[:session0])
      end

      def session1
        @session1 ||= new_session(@session_options[:session1])
      end
    end
  end
end