File: auto_encryption_mongocryptd_spawn_spec.rb

package info (click to toggle)
ruby-mongo 2.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,764 kB
  • sloc: ruby: 108,806; makefile: 5; sh: 2
file content (75 lines) | stat: -rw-r--r-- 2,433 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
# frozen_string_literal: true
# rubocop:todo all

require 'spec_helper'

describe 'Auto Encryption' do
  require_libmongocrypt
  min_server_fcv '4.2'
  require_enterprise

  include_context 'define shared FLE helpers'
  include_context 'with local kms_providers'

  context 'with an invalid mongocryptd spawn path' do
    let(:client) do
      new_local_client(
        SpecConfig.instance.addresses,
        SpecConfig.instance.test_options.merge(
          auto_encryption_options: {
            kms_providers: kms_providers,
            key_vault_namespace: key_vault_namespace,
            schema_map: { 'auto_encryption.users' => schema_map },
            extra_options: {
              mongocryptd_spawn_path: 'echo hello world',
              mongocryptd_spawn_args: []
            }
          },
          database: 'auto_encryption'
        ),
      )
    end

    let(:server_selector) { double("ServerSelector") }
    let(:cluster) { double("Cluster") }

    before do
      key_vault_collection.drop
      key_vault_collection.insert_one(data_key)

      allow(server_selector).to receive(:name)
      allow(server_selector).to receive(:server_selection_timeout)
      allow(server_selector).to receive(:local_threshold)
      allow(cluster).to receive(:summary)

      # Raise a server selection error on intent-to-encrypt commands to mock
      # what would happen if mongocryptd hadn't already been spawned. It is
      # necessary to mock this behavior because it is likely that another test
      # will have already spawned mongocryptd, causing this test to fail.
      allow_any_instance_of(Mongo::Database)
        .to receive(:command)
        .with(
          hash_including(
            'insert' => 'users',
            'ordered' => true,
            'lsid' => kind_of(Hash),
            'documents' => kind_of(Array),
            'jsonSchema' => kind_of(Hash),
            'isRemoteSchema' => false,
          ),
          { execution_options: { deserialize_as_bson: true }, timeout_ms: nil },

        )
        .and_raise(Mongo::Error::NoServerAvailable.new(server_selector, cluster))
    end

    it 'raises an exception when trying to perform auto encryption' do
      expect do
        client['users'].insert_one(ssn: ssn)
      end.to raise_error(
        Mongo::Error::MongocryptdSpawnError,
        /Failed to spawn mongocryptd at the path "echo hello world" with arguments/
      )
    end
  end
end