mongo 2.7.2 → 2.8.0.rc0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +1 -3
  4. data/lib/mongo/address.rb +17 -20
  5. data/lib/mongo/address/ipv4.rb +6 -3
  6. data/lib/mongo/address/ipv6.rb +6 -3
  7. data/lib/mongo/address/unix.rb +5 -2
  8. data/lib/mongo/auth.rb +15 -2
  9. data/lib/mongo/auth/cr/conversation.rb +4 -2
  10. data/lib/mongo/auth/ldap/conversation.rb +4 -2
  11. data/lib/mongo/auth/scram.rb +3 -7
  12. data/lib/mongo/auth/scram/conversation.rb +28 -19
  13. data/lib/mongo/auth/user.rb +45 -10
  14. data/lib/mongo/auth/x509/conversation.rb +4 -2
  15. data/lib/mongo/cluster.rb +9 -17
  16. data/lib/mongo/error.rb +2 -0
  17. data/lib/mongo/error/missing_password.rb +29 -0
  18. data/lib/mongo/error/operation_failure.rb +7 -3
  19. data/lib/mongo/error/parser.rb +2 -1
  20. data/lib/mongo/error/sdam_error_detection.rb +54 -0
  21. data/lib/mongo/operation/aggregate/command.rb +1 -16
  22. data/lib/mongo/operation/aggregate/op_msg.rb +1 -1
  23. data/lib/mongo/operation/collections_info.rb +2 -3
  24. data/lib/mongo/operation/delete/command.rb +2 -15
  25. data/lib/mongo/operation/delete/legacy.rb +1 -16
  26. data/lib/mongo/operation/explain/command.rb +1 -16
  27. data/lib/mongo/operation/explain/legacy.rb +1 -16
  28. data/lib/mongo/operation/find/command.rb +1 -16
  29. data/lib/mongo/operation/find/legacy.rb +1 -16
  30. data/lib/mongo/operation/get_more/command.rb +1 -16
  31. data/lib/mongo/operation/indexes/command.rb +1 -16
  32. data/lib/mongo/operation/indexes/legacy.rb +4 -16
  33. data/lib/mongo/operation/list_collections/command.rb +1 -16
  34. data/lib/mongo/operation/map_reduce/command.rb +1 -16
  35. data/lib/mongo/operation/parallel_scan/command.rb +1 -16
  36. data/lib/mongo/operation/result.rb +3 -0
  37. data/lib/mongo/operation/shared/executable.rb +4 -0
  38. data/lib/mongo/operation/shared/polymorphic_lookup.rb +1 -1
  39. data/lib/mongo/operation/shared/polymorphic_result.rb +8 -1
  40. data/lib/mongo/operation/shared/result/aggregatable.rb +0 -5
  41. data/lib/mongo/operation/update/command.rb +2 -15
  42. data/lib/mongo/operation/update/legacy.rb +1 -16
  43. data/lib/mongo/operation/users_info/command.rb +1 -16
  44. data/lib/mongo/retryable.rb +22 -10
  45. data/lib/mongo/server.rb +10 -1
  46. data/lib/mongo/server/app_metadata.rb +7 -2
  47. data/lib/mongo/server/connectable.rb +0 -6
  48. data/lib/mongo/server/connection.rb +86 -135
  49. data/lib/mongo/server/connection_base.rb +133 -0
  50. data/lib/mongo/server/connection_pool.rb +11 -24
  51. data/lib/mongo/server/connection_pool/queue.rb +41 -41
  52. data/lib/mongo/server/description.rb +1 -1
  53. data/lib/mongo/server/monitor.rb +4 -4
  54. data/lib/mongo/server/monitor/connection.rb +26 -7
  55. data/lib/mongo/server/pending_connection.rb +36 -0
  56. data/lib/mongo/server_selector/selectable.rb +9 -1
  57. data/lib/mongo/session.rb +0 -1
  58. data/lib/mongo/socket.rb +23 -6
  59. data/lib/mongo/socket/ssl.rb +11 -18
  60. data/lib/mongo/socket/tcp.rb +13 -14
  61. data/lib/mongo/socket/unix.rb +9 -27
  62. data/lib/mongo/uri.rb +1 -1
  63. data/lib/mongo/version.rb +1 -1
  64. data/spec/integration/auth_spec.rb +160 -0
  65. data/spec/integration/retryable_writes_spec.rb +55 -58
  66. data/spec/integration/sdam_error_handling_spec.rb +115 -0
  67. data/spec/mongo/address/ipv4_spec.rb +4 -0
  68. data/spec/mongo/address/ipv6_spec.rb +4 -0
  69. data/spec/mongo/auth/scram/conversation_spec.rb +6 -5
  70. data/spec/mongo/auth/scram/negotiation_spec.rb +25 -36
  71. data/spec/mongo/auth/scram_spec.rb +2 -2
  72. data/spec/mongo/auth/user_spec.rb +97 -0
  73. data/spec/mongo/client_construction_spec.rb +1 -1
  74. data/spec/mongo/error/operation_failure_spec.rb +125 -1
  75. data/spec/mongo/retryable_spec.rb +17 -8
  76. data/spec/mongo/server/connection_pool/queue_spec.rb +24 -10
  77. data/spec/mongo/server/connection_pool_spec.rb +30 -117
  78. data/spec/mongo/server/connection_spec.rb +147 -25
  79. data/spec/mongo/server/description_spec.rb +0 -14
  80. data/spec/mongo/server/monitor/connection_spec.rb +22 -0
  81. data/spec/mongo/server_selector_spec.rb +1 -0
  82. data/spec/mongo/server_spec.rb +6 -6
  83. data/spec/mongo/socket/ssl_spec.rb +48 -116
  84. data/spec/mongo/socket/tcp_spec.rb +22 -0
  85. data/spec/mongo/socket/unix_spec.rb +9 -9
  86. data/spec/mongo/socket_spec.rb +15 -3
  87. data/spec/spec_tests/server_selection_spec.rb +2 -0
  88. data/spec/support/client_registry.rb +8 -2
  89. data/spec/support/common_shortcuts.rb +20 -1
  90. data/spec/support/constraints.rb +10 -2
  91. data/spec/support/lite_constraints.rb +8 -0
  92. data/spec/support/spec_config.rb +9 -1
  93. metadata +14 -4
  94. metadata.gz.sig +0 -0
@@ -8,11 +8,7 @@ describe Mongo::Server::Connection, retry: 3 do
8
8
  ClientRegistry.instance.close_all_clients
9
9
  end
10
10
 
11
- after(:all) do
12
- ClientRegistry.instance.close_all_clients
13
- end
14
-
15
- let(:address) do
11
+ let!(:address) do
16
12
  default_address
17
13
  end
18
14
 
@@ -43,10 +39,9 @@ describe Mongo::Server::Connection, retry: 3 do
43
39
 
44
40
  declare_topology_double
45
41
 
42
+ let(:server_options) { SpecConfig.instance.test_options.merge(monitoring_io: false) }
46
43
  let(:server) do
47
- Mongo::Server.new(address, cluster, monitoring, listeners,
48
- SpecConfig.instance.test_options.merge(monitoring_io: false),
49
- )
44
+ Mongo::Server.new(address, cluster, monitoring, listeners, server_options)
50
45
  end
51
46
 
52
47
  let(:monitored_server) do
@@ -54,6 +49,7 @@ describe Mongo::Server::Connection, retry: 3 do
54
49
  SpecConfig.instance.test_options
55
50
  ).tap do |server|
56
51
  server.scan!
52
+ expect(server).not_to be_unknown
57
53
  end
58
54
  end
59
55
 
@@ -142,6 +138,23 @@ describe Mongo::Server::Connection, retry: 3 do
142
138
  end
143
139
  end
144
140
 
141
+ shared_examples_for 'logs a warning' do
142
+ let(:expected_message) do
143
+ "MONGODB | Failed to handshake with #{address}: #{error.class}: #{error}"
144
+ end
145
+
146
+ it 'logs a warning' do
147
+ messages = []
148
+ # Straightforward expectations are not working here for some reason
149
+ expect(Mongo::Logger.logger).to receive(:warn) do |msg|
150
+ messages << msg
151
+ end
152
+ expect(error).not_to be nil
153
+ expect(messages).to include(expected_message)
154
+ end
155
+
156
+ end
157
+
145
158
  context 'when #handshake! dependency raises a non-network exception' do
146
159
 
147
160
  let(:exception) do
@@ -181,15 +194,24 @@ describe Mongo::Server::Connection, retry: 3 do
181
194
 
182
195
  it_behaves_like 'failing connection'
183
196
  it_behaves_like 'marks server unknown'
197
+ it_behaves_like 'logs a warning'
184
198
  end
185
199
 
186
200
  context 'when #authenticate! raises an exception' do
201
+ require_auth
202
+
203
+ let(:server_options) do
204
+ SpecConfig.instance.test_options.merge(monitoring_io: false).
205
+ merge(SpecConfig.instance.auth_options)
206
+ end
207
+
187
208
  let(:exception) do
188
209
  Mongo::Error::OperationFailure.new
189
210
  end
190
211
 
191
212
  let(:error) do
192
- expect(connection).to receive(:authenticate!).and_raise(exception)
213
+ expect(Mongo::Auth).to receive(:get).and_raise(exception)
214
+ expect(connection.send(:socket)).to be nil
193
215
  begin
194
216
  connection.connect!
195
217
  rescue Exception => e
@@ -200,6 +222,7 @@ describe Mongo::Server::Connection, retry: 3 do
200
222
  end
201
223
 
202
224
  it_behaves_like 'failing connection'
225
+ it_behaves_like 'logs a warning'
203
226
  end
204
227
 
205
228
  context 'when a non-Mongo exception is raised' do
@@ -249,7 +272,7 @@ describe Mongo::Server::Connection, retry: 3 do
249
272
 
250
273
  shared_examples_for 'does not disconnect connection pool' do
251
274
  it 'does not disconnect non-monitoring sockets' do
252
- allow(cluster).to receive(:pool).with(server).and_return(pool)
275
+ allow(server).to receive(:pool).and_return(pool)
253
276
  expect(pool).not_to receive(:disconnect!)
254
277
  error
255
278
  end
@@ -257,7 +280,7 @@ describe Mongo::Server::Connection, retry: 3 do
257
280
 
258
281
  shared_examples_for 'disconnects connection pool' do
259
282
  it 'disconnects non-monitoring sockets' do
260
- expect(cluster).to receive(:pool).with(server).and_return(pool)
283
+ expect(server).to receive(:pool).and_return(pool)
261
284
  expect(pool).to receive(:disconnect!).and_return(true)
262
285
  error
263
286
  end
@@ -607,10 +630,6 @@ describe Mongo::Server::Connection, retry: 3 do
607
630
 
608
631
  context 'when the message is a command' do
609
632
 
610
- before do
611
- allow(connection).to receive(:max_bson_object_size).and_return(100)
612
- end
613
-
614
633
  let(:selector) do
615
634
  { :getlasterror => '1' }
616
635
  end
@@ -624,29 +643,92 @@ describe Mongo::Server::Connection, retry: 3 do
624
643
  end
625
644
 
626
645
  it 'checks the size against the max bson size' do
627
- expect {
646
+ expect_any_instance_of(Mongo::Server).to receive(:max_bson_object_size).at_least(:once).and_return(100)
647
+ expect do
628
648
  reply
629
- }.to raise_exception(Mongo::Error::MaxBSONSize)
649
+ end.to raise_exception(Mongo::Error::MaxBSONSize)
630
650
  end
631
651
  end
632
652
  end
633
653
 
634
- context 'when a network or socket error occurs' do
654
+ context 'when a network error occurs' do
655
+ let(:server) do
656
+ authorized_client.cluster.next_primary.tap do |server|
657
+ # to ensure the server stays in unknown state for the duration
658
+ # of the test, i.e. to avoid racing with the monitor thread
659
+ # which may put the server back into non-unknown state before
660
+ # we can verify that the server was marked unknown, kill off
661
+ # the monitor thread
662
+ server.monitor.instance_variable_get('@thread').kill
663
+ end
664
+ end
635
665
 
636
666
  let(:socket) do
637
667
  connection.connect!
638
668
  connection.instance_variable_get(:@socket)
639
669
  end
640
670
 
641
- before do
642
- expect(socket).to receive(:write).and_raise(Mongo::Error::SocketError)
671
+ context 'when a non-timeout socket error occurs' do
672
+
673
+ before do
674
+ expect(socket).to receive(:write).and_raise(Mongo::Error::SocketError)
675
+ end
676
+
677
+ let(:result) do
678
+ expect do
679
+ connection.dispatch([ insert ])
680
+ end.to raise_error(Mongo::Error::SocketError)
681
+ end
682
+
683
+ it 'disconnects and raises the exception' do
684
+ result
685
+ expect(connection).to_not be_connected
686
+ end
687
+
688
+ it 'disconnects connection pool' do
689
+ expect(server.pool).to receive(:disconnect!)
690
+ result
691
+ end
692
+
693
+ it 'does not request server scan' do
694
+ expect(server.monitor.scan_semaphore).not_to receive(:signal)
695
+ result
696
+ end
697
+
698
+ it 'marks server unknown' do
699
+ expect(server).not_to be_unknown
700
+ result
701
+ expect(server).to be_unknown
702
+ end
643
703
  end
644
704
 
645
- it 'disconnects and raises the exception' do
646
- expect {
647
- connection.dispatch([ insert ])
648
- }.to raise_error(Mongo::Error::SocketError)
649
- expect(connection).to_not be_connected
705
+ context 'when a socket timeout occurs' do
706
+
707
+ before do
708
+ expect(socket).to receive(:write).and_raise(Mongo::Error::SocketTimeoutError)
709
+ end
710
+
711
+ let(:result) do
712
+ expect do
713
+ connection.dispatch([ insert ])
714
+ end.to raise_error(Mongo::Error::SocketTimeoutError)
715
+ end
716
+
717
+ it 'disconnects the used connection' do
718
+ result
719
+ expect(connection).to_not be_connected
720
+ end
721
+
722
+ it 'does not disconnect connection pool' do
723
+ expect(server.pool).not_to receive(:disconnect!)
724
+ result
725
+ end
726
+
727
+ it 'does not mark server unknown' do
728
+ expect(server).not_to be_unknown
729
+ result
730
+ expect(server).not_to be_unknown
731
+ end
650
732
  end
651
733
  end
652
734
 
@@ -947,4 +1029,44 @@ describe Mongo::Server::Connection, retry: 3 do
947
1029
  end
948
1030
  end
949
1031
  end
1032
+
1033
+ describe '#app_metadata' do
1034
+ context 'when all options are identical to server' do
1035
+ let(:connection) do
1036
+ described_class.new(server, server.options)
1037
+ end
1038
+
1039
+ it 'is the same object as server app_metadata' do
1040
+ expect(connection.app_metadata).not_to be nil
1041
+ expect(connection.app_metadata).to be server.app_metadata
1042
+ end
1043
+ end
1044
+
1045
+ context 'when auth options are identical to server' do
1046
+ let(:connection) do
1047
+ described_class.new(server, server.options.merge(socket_timeout: 2))
1048
+ end
1049
+
1050
+ it 'is the same object as server app_metadata' do
1051
+ expect(connection.app_metadata).not_to be nil
1052
+ expect(connection.app_metadata).to be server.app_metadata
1053
+ end
1054
+ end
1055
+
1056
+ context 'when auth options differ from server' do
1057
+ let(:connection) do
1058
+ described_class.new(server, server.options.merge(user: 'foo'))
1059
+ end
1060
+
1061
+ it 'is different object from server app_metadata' do
1062
+ expect(connection.app_metadata).not_to be nil
1063
+ expect(connection.app_metadata).not_to be server.app_metadata
1064
+ end
1065
+
1066
+ it 'includes request auth mechanism' do
1067
+ document = connection.app_metadata.send(:document)
1068
+ expect(document[:saslSupportedMechs]).to eq('admin.foo')
1069
+ end
1070
+ end
1071
+ end
950
1072
  end
@@ -49,20 +49,6 @@ describe Mongo::Server::Description do
49
49
  end
50
50
  end
51
51
 
52
- describe '#initialize' do
53
- context 'when Time.now is mocked' do
54
- it 'does not freeze mocked time' do
55
- obj = Time.now
56
- expect(Time).to receive(:now).at_least(:once).and_return(obj)
57
- expect(obj.frozen?).to be false
58
-
59
- description = described_class.new(address)
60
- expect(description.last_update_time).to eq(obj)
61
- expect(obj.frozen?).to be false
62
- end
63
- end
64
- end
65
-
66
52
  describe '#arbiters' do
67
53
 
68
54
  context 'when the replica set has arbiters' do
@@ -158,4 +158,26 @@ describe Mongo::Server::Monitor::Connection do
158
158
  end
159
159
  end
160
160
  end
161
+
162
+ describe '#connect!' do
163
+ context 'network error' do
164
+ before do
165
+ address
166
+ server.monitor.instance_variable_get('@thread').kill
167
+ server.monitor.connection.disconnect!
168
+ expect_any_instance_of(Mongo::Socket).to receive(:write).and_raise(Mongo::Error::SocketError, 'test error')
169
+ end
170
+
171
+ let(:options) { SpecConfig.instance.test_options }
172
+
173
+ let(:expected_message) { "MONGODB | Failed to handshake with #{address}: Mongo::Error::SocketError: test error" }
174
+
175
+ it 'logs a warning' do
176
+ expect(Mongo::Logger.logger).to receive(:warn).with(expected_message).and_call_original
177
+ expect do
178
+ server.monitor.connection.connect!
179
+ end.to raise_error(Mongo::Error::SocketError, 'test error')
180
+ end
181
+ end
182
+ end
161
183
  end
@@ -218,6 +218,7 @@ describe Mongo::ServerSelector do
218
218
  allow(c).to receive(:unknown?).and_return(false)
219
219
  allow(c).to receive(:scan!).and_return(true)
220
220
  allow(c).to receive(:options).and_return(server_selection_timeout: 0.1)
221
+ allow(c).to receive(:server_selection_semaphore).and_return(nil)
221
222
  end
222
223
  end
223
224
 
@@ -25,7 +25,7 @@ describe Mongo::Server do
25
25
  end
26
26
 
27
27
  let(:pool) do
28
- Mongo::Server::ConnectionPool.get(server)
28
+ server.pool
29
29
  end
30
30
 
31
31
  describe '#==' do
@@ -35,7 +35,7 @@ describe Mongo::Server do
35
35
  end
36
36
 
37
37
  after do
38
- expect(cluster).to receive(:pool).with(server).and_return(pool)
38
+ expect(server).to receive(:pool).and_return(pool)
39
39
  server.disconnect!
40
40
  end
41
41
 
@@ -88,7 +88,7 @@ describe Mongo::Server do
88
88
 
89
89
  it 'stops the monitor instance' do
90
90
  expect(server.instance_variable_get(:@monitor)).to receive(:stop!).and_return(true)
91
- expect(cluster).to receive(:pool).with(server).and_return(pool)
91
+ expect(server).to receive(:pool).and_return(pool)
92
92
  server.disconnect!
93
93
  end
94
94
  end
@@ -106,7 +106,7 @@ describe Mongo::Server do
106
106
  end
107
107
 
108
108
  after do
109
- expect(cluster).to receive(:pool).with(server).and_return(pool)
109
+ expect(server).to receive(:pool).and_return(pool)
110
110
  server.disconnect!
111
111
  end
112
112
 
@@ -150,7 +150,7 @@ describe Mongo::Server do
150
150
  end
151
151
 
152
152
  after do
153
- expect(cluster).to receive(:pool).with(server).and_return(pool)
153
+ expect(server).to receive(:pool).and_return(pool)
154
154
  server.disconnect!
155
155
  end
156
156
 
@@ -170,7 +170,7 @@ describe Mongo::Server do
170
170
  end
171
171
 
172
172
  after do
173
- expect(cluster).to receive(:pool).with(server).and_return(pool)
173
+ expect(server).to receive(:pool).and_return(pool)
174
174
  server.disconnect!
175
175
  end
176
176
 
@@ -4,10 +4,12 @@ describe Mongo::Socket::SSL do
4
4
  require_ssl
5
5
 
6
6
  let(:address) do
7
- default_address
7
+ default_address.tap do
8
+ ClientRegistry.instance.close_all_clients
9
+ end
8
10
  end
9
11
 
10
- let(:resolver) do
12
+ let!(:resolver) do
11
13
  address.send(:create_resolver, {})
12
14
  end
13
15
 
@@ -47,16 +49,19 @@ describe Mongo::Socket::SSL do
47
49
  OpenSSL::PKey.read(key_string)
48
50
  end
49
51
 
52
+ describe '#address' do
53
+ it 'returns the address and tls indicator' do
54
+ addr = socket.instance_variable_get(:@tcp_socket).remote_address
55
+ expect(socket.send(:address)).to eq("#{addr.ip_address}:#{addr.ip_port} (#{default_address}, TLS)")
56
+ end
57
+ end
58
+
50
59
  describe '#connect!' do
51
60
 
52
61
  context 'when a certificate is provided' do
53
62
 
54
63
  context 'when connecting the tcp socket is successful' do
55
64
 
56
- before do
57
- socket.connect!
58
- end
59
-
60
65
  it 'connects to the server' do
61
66
  expect(socket).to be_alive
62
67
  end
@@ -64,21 +69,11 @@ describe Mongo::Socket::SSL do
64
69
 
65
70
  context 'when connecting the tcp socket raises an exception' do
66
71
 
67
- before do
68
- tcp_socket = socket.instance_variable_get(:@tcp_socket)
69
- allow(tcp_socket).to receive(:connect).and_raise(Mongo::Error::SocketTimeoutError)
70
- end
71
-
72
- let!(:result) do
73
- begin
74
- socket.connect!
75
- rescue => e
76
- e
77
- end
78
- end
79
-
80
72
  it 'raises an exception' do
81
- expect(result).to be_a(Mongo::Error::SocketTimeoutError)
73
+ expect_any_instance_of(::Socket).to receive(:connect).and_raise(Mongo::Error::SocketTimeoutError)
74
+ expect do
75
+ socket
76
+ end.to raise_error(Mongo::Error::SocketTimeoutError)
82
77
  end
83
78
  end
84
79
  end
@@ -94,10 +89,6 @@ describe Mongo::Socket::SSL do
94
89
  }
95
90
  end
96
91
 
97
- before do
98
- socket.connect!
99
- end
100
-
101
92
  it 'connects to the server' do
102
93
  expect(socket).to be_alive
103
94
  end
@@ -116,10 +107,6 @@ describe Mongo::Socket::SSL do
116
107
  }
117
108
  end
118
109
 
119
- before do
120
- socket.connect!
121
- end
122
-
123
110
  it 'connects to the server' do
124
111
  expect(socket).to be_alive
125
112
  end
@@ -136,10 +123,6 @@ describe Mongo::Socket::SSL do
136
123
  }
137
124
  end
138
125
 
139
- before do
140
- socket.connect!
141
- end
142
-
143
126
  it 'connects to the server' do
144
127
  expect(socket).to be_alive
145
128
  end
@@ -153,10 +136,6 @@ describe Mongo::Socket::SSL do
153
136
  )
154
137
  end
155
138
 
156
- before do
157
- socket.connect!
158
- end
159
-
160
139
  # since the lower priority option is clearly invalid we verify priority by checking that it connects
161
140
  it 'discards the value of :ssl_cert_string' do
162
141
  expect(socket).to be_alive
@@ -171,10 +150,6 @@ describe Mongo::Socket::SSL do
171
150
  )
172
151
  end
173
152
 
174
- before do
175
- socket.connect!
176
- end
177
-
178
153
  # since the lower priority option is clearly invalid we verify priority by checking that it connects
179
154
  it 'discards the value of :ssl_cert_object' do
180
155
  expect(socket).to be_alive
@@ -193,10 +168,6 @@ describe Mongo::Socket::SSL do
193
168
  }
194
169
  end
195
170
 
196
- before do
197
- socket.connect!
198
- end
199
-
200
171
  # since the lower priority option is clearly invalid we verify priority by checking that it connects
201
172
  it 'discards the value of :ssl_cert_object' do
202
173
  expect(socket).to be_alive
@@ -211,10 +182,6 @@ describe Mongo::Socket::SSL do
211
182
  )
212
183
  end
213
184
 
214
- before do
215
- socket.connect!
216
- end
217
-
218
185
  # since the lower priority option is clearly invalid we verify priority by checking that it connects
219
186
  it 'discards the value of :ssl_key_string' do
220
187
  expect(socket).to be_alive
@@ -229,10 +196,6 @@ describe Mongo::Socket::SSL do
229
196
  )
230
197
  end
231
198
 
232
- before do
233
- socket.connect!
234
- end
235
-
236
199
  # since the lower priority option is clearly invalid we verify priority by checking that it connects
237
200
  it 'discards the value of :ssl_key_object' do
238
201
  expect(socket).to be_alive
@@ -251,10 +214,6 @@ describe Mongo::Socket::SSL do
251
214
  }
252
215
  end
253
216
 
254
- before do
255
- socket.connect!
256
- end
257
-
258
217
  # since the lower priority option is clearly invalid we verify priority by checking that it connects
259
218
  it 'discards the value of :ssl_key_object' do
260
219
  expect(socket).to be_alive
@@ -274,9 +233,9 @@ describe Mongo::Socket::SSL do
274
233
  end
275
234
 
276
235
  it 'raises a TypeError' do
277
- expect{
278
- socket.connect!
279
- }.to raise_exception(TypeError)
236
+ expect do
237
+ socket
238
+ end.to raise_exception(TypeError)
280
239
  end
281
240
  end
282
241
 
@@ -290,13 +249,13 @@ describe Mongo::Socket::SSL do
290
249
  error = nil
291
250
  begin
292
251
  described_class.new(
293
- address.host,
294
- address.port,
252
+ resolver.host,
253
+ resolver.port,
295
254
  host_name,
296
255
  30,
297
256
  ::Socket::PF_INET,
298
257
  options.merge(ssl_verify_hostname: true)
299
- ).connect!
258
+ )
300
259
  rescue => e
301
260
  error = e
302
261
  end
@@ -310,13 +269,13 @@ describe Mongo::Socket::SSL do
310
269
  it 'raises an error' do
311
270
  expect {
312
271
  described_class.new(
313
- address.host,
314
- address.port,
272
+ resolver.host,
273
+ resolver.port,
315
274
  host_name,
316
275
  30,
317
276
  ::Socket::PF_INET,
318
277
  options.merge(ssl_verify_hostname: false)
319
- ).connect!
278
+ )
320
279
  }.not_to raise_error
321
280
  end
322
281
  end
@@ -338,9 +297,9 @@ describe Mongo::Socket::SSL do
338
297
  end
339
298
 
340
299
  it 'raises a TypeError' do
341
- expect{
342
- socket.connect!
343
- }.to raise_exception(TypeError)
300
+ expect do
301
+ socket
302
+ end.to raise_exception(TypeError)
344
303
  end
345
304
  end
346
305
  end
@@ -379,9 +338,9 @@ describe Mongo::Socket::SSL do
379
338
  end
380
339
 
381
340
  it 'raises a NoMethodError' do
382
- expect{
383
- socket.connect!
384
- }.to raise_exception(expected_exception)
341
+ expect do
342
+ socket
343
+ end.to raise_exception(expected_exception)
385
344
  end
386
345
  end
387
346
  end
@@ -412,9 +371,9 @@ describe Mongo::Socket::SSL do
412
371
  end
413
372
 
414
373
  it 'raises an exception' do
415
- expect {
416
- socket.connect!
417
- }.to raise_exception(expected_exception)
374
+ expect do
375
+ socket
376
+ end.to raise_exception(expected_exception)
418
377
  end
419
378
  end
420
379
 
@@ -430,10 +389,6 @@ describe Mongo::Socket::SSL do
430
389
  )
431
390
  end
432
391
 
433
- before do
434
- socket.connect!
435
- end
436
-
437
392
  it 'connects to the server' do
438
393
  expect(socket).to be_alive
439
394
  end
@@ -441,24 +396,20 @@ describe Mongo::Socket::SSL do
441
396
 
442
397
  context 'as a string containing the PEM-encoded certificate' do
443
398
 
444
- let (:options) do
399
+ let(:options) do
445
400
  super().merge(
446
401
  :ssl_ca_cert_string => ca_cert_string,
447
402
  :ssl_verify => true
448
403
  )
449
404
  end
450
405
 
451
- before do
452
- socket.connect!
453
- end
454
-
455
406
  it 'connects to the server' do
456
407
  expect(socket).to be_alive
457
408
  end
458
409
  end
459
410
 
460
411
  context 'as an array of Certificate objects' do
461
- let (:options) do
412
+ let(:options) do
462
413
  cert = [OpenSSL::X509::Certificate.new(ca_cert_string)]
463
414
  super().merge(
464
415
  :ssl_ca_cert_object => cert,
@@ -466,10 +417,6 @@ describe Mongo::Socket::SSL do
466
417
  )
467
418
  end
468
419
 
469
- before do
470
- socket.connect!
471
- end
472
-
473
420
  it 'connects to the server' do
474
421
  expect(socket).to be_alive
475
422
  end
@@ -485,10 +432,6 @@ describe Mongo::Socket::SSL do
485
432
  )
486
433
  end
487
434
 
488
- before do
489
- socket.connect!
490
- end
491
-
492
435
  # since the lower priority option is clearly invalid we verify priority by checking that it connects
493
436
  it 'discards the value of :ssl_ca_cert_string' do
494
437
  expect(socket).to be_alive
@@ -505,10 +448,6 @@ describe Mongo::Socket::SSL do
505
448
  )
506
449
  end
507
450
 
508
- before do
509
- socket.connect!
510
- end
511
-
512
451
  it 'discards the value of :ssl_ca_cert_object' do
513
452
  expect(socket).to be_alive
514
453
  end
@@ -525,10 +464,6 @@ describe Mongo::Socket::SSL do
525
464
  )
526
465
  end
527
466
 
528
- before do
529
- socket.connect!
530
- end
531
-
532
467
  it 'discards the value of :ssl_ca_cert_object' do
533
468
  expect(socket).to be_alive
534
469
  end
@@ -544,9 +479,14 @@ describe Mongo::Socket::SSL do
544
479
  )
545
480
  end
546
481
 
547
- before do
482
+ around do |example|
483
+ saved = ENV['SSL_CERT_FILE']
548
484
  ENV['SSL_CERT_FILE'] = CA_PEM
549
- socket.connect!
485
+ begin
486
+ example.run
487
+ ensure
488
+ ENV['SSL_CERT_FILE'] = saved
489
+ end
550
490
  end
551
491
 
552
492
  it 'uses the default cert store' do
@@ -563,10 +503,6 @@ describe Mongo::Socket::SSL do
563
503
  ).tap { |options| options.delete(:ssl_verify) }
564
504
  end
565
505
 
566
- before do
567
- socket.connect!
568
- end
569
-
570
506
  it 'verifies the server certificate' do
571
507
  expect(socket).to be_alive
572
508
  end
@@ -582,10 +518,6 @@ describe Mongo::Socket::SSL do
582
518
  )
583
519
  end
584
520
 
585
- before do
586
- socket.connect!
587
- end
588
-
589
521
  it 'verifies the server certificate' do
590
522
  expect(socket).to be_alive
591
523
  end
@@ -600,10 +532,6 @@ describe Mongo::Socket::SSL do
600
532
  )
601
533
  end
602
534
 
603
- before do
604
- socket.connect!
605
- end
606
-
607
535
  it 'does not verify the server certificate' do
608
536
  expect(socket).to be_alive
609
537
  end
@@ -642,9 +570,13 @@ describe Mongo::Socket::SSL do
642
570
 
643
571
  let(:socket_content) { "" }
644
572
 
573
+ let(:remote_address) { socket.instance_variable_get(:@tcp_socket).remote_address }
574
+ let(:address_str) { "#{remote_address.ip_address}:#{remote_address.ip_port} (#{default_address}, TLS)" }
575
+
645
576
  it 'should raise EOFError' do
646
- expect { socket.readbyte }
647
- .to raise_error(Mongo::Error::SocketError).with_message("EOFError: EOFError")
577
+ expect do
578
+ socket.readbyte
579
+ end.to raise_error(Mongo::Error::SocketError).with_message("EOFError: EOFError (for #{address_str})")
648
580
  end
649
581
  end
650
582
  end