File: seed_list_discovery_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 (127 lines) | stat: -rw-r--r-- 4,325 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
# frozen_string_literal: true
# rubocop:todo all

require 'lite_spec_helper'

require 'support/using_hash'
require 'runners/connection_string'
require 'mrss/lite_constraints'

SEED_LIST_DISCOVERY_TESTS = Dir.glob("#{CURRENT_PATH}/spec_tests/data/seed_list_discovery/**/*.yml").sort

describe 'DNS Seedlist Discovery' do
  require_external_connectivity

  include Mongo::ConnectionString

  SEED_LIST_DISCOVERY_TESTS.each do |test_path|

    spec = ::Utils.load_spec_yaml_file(test_path)

    test = Mongo::ConnectionString::Test.new(spec)

    context(File.basename(test_path)) do

      if test.raise_error?
        context 'the uri is invalid' do
          retry_test

          let(:valid_errors) do
            [
              Mongo::Error::InvalidTXTRecord,
              Mongo::Error::NoSRVRecords,
              Mongo::Error::InvalidURI,
              Mongo::Error::MismatchedDomain,
              # This is unfortunate. RUBY-2624
              ArgumentError,
            ]
          end

          let(:error) do
            begin
              test.client
            rescue => ex
            end
            ex
          end

          # In Evergreen sometimes this test fails intermittently.
          it 'raises an error' do
            expect(valid_errors).to include(error.class)
          end
        end

      else

        context 'the uri is valid' do
          retry_test
          # In Evergreen sometimes this test fails intermittently.
          it 'does not raise an exception' do
            expect(test.uri).to be_a(Mongo::URI::SRVProtocol)
          end

          if test.seeds
            # DNS seed list tests specify both seeds and hosts.
            # To get the hosts, the client must do SDAM (as required in the
            # spec tests' description), but this isn't testing DNS seed list -
            # it is testing SDAM. Plus, all of the hosts are always the same.
            # If seed list is given in the expectations, just test the seed
            # list and not the expanded hosts.
            it 'creates a client with the correct seeds' do
              expect(test.client).to have_hosts(test, test.seeds)
            end
          elsif test.num_seeds
            it 'has the right number of seeds' do
              num_servers = test.client.cluster.servers_list.length
              expect(num_servers).to eq(test.num_seeds)
            end
          else
            it 'creates a client with the correct hosts' do
              expect(test.client).to have_hosts(test, test.hosts)
            end
          end

          if test.expected_options
            it 'creates a client with the correct uri options' do
              mapped = Mongo::URI::OptionsMapper.new.ruby_to_smc(test.client.options)
              # Connection string spec tests do not use canonical URI option names
              actual = Utils.downcase_keys(mapped)
              expected = Utils.downcase_keys(test.expected_options)
              # SRV tests use ssl URI option instead of tls one
              if expected.key?('ssl') && !expected.key?('tls')
                expected['tls'] = expected.delete('ssl')
              end
              # The client object contains auth source in options which
              # isn't asserted in some tests.
              if actual.key?('authsource') && !expected.key?('authsource')
                actual.delete('authsource')
              end
              actual.should == expected
            end
          end

          if test.non_uri_options
            it 'creates a client with the correct non-uri options' do
              opts = UsingHash[test.non_uri_options]
              if user = opts.use('user')
                test.client.options[:user].should == user
              end
              if password = opts.use('password')
                test.client.options[:password].should == password
              end
              if db = opts.use('db')
                test.client.database.name.should == db
              end
              if auth_source = opts.use('auth_database')
                Mongo::Auth::User.new(test.client.options).auth_source == auth_source
              end
              unless opts.empty?
                raise "Unhandled keys: #{opts}"
              end
            end
          end
        end
      end
    end
  end
end