mongo 1.9.2 → 1.10.0.rc0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/LICENSE +1 -1
  5. data/README.md +94 -334
  6. data/Rakefile +6 -4
  7. data/VERSION +1 -1
  8. data/bin/mongo_console +13 -6
  9. data/lib/mongo.rb +22 -27
  10. data/lib/mongo/bulk_write_collection_view.rb +352 -0
  11. data/lib/mongo/collection.rb +128 -188
  12. data/lib/mongo/collection_writer.rb +348 -0
  13. data/lib/mongo/connection.rb +19 -0
  14. data/lib/mongo/{util → connection}/node.rb +15 -1
  15. data/lib/mongo/{util → connection}/pool.rb +34 -19
  16. data/lib/mongo/{util → connection}/pool_manager.rb +8 -2
  17. data/lib/mongo/{util → connection}/sharding_pool_manager.rb +1 -1
  18. data/lib/mongo/connection/socket.rb +18 -0
  19. data/lib/mongo/{util → connection/socket}/socket_util.rb +5 -2
  20. data/lib/mongo/{util → connection/socket}/ssl_socket.rb +3 -4
  21. data/lib/mongo/{util → connection/socket}/tcp_socket.rb +25 -15
  22. data/lib/mongo/{util → connection/socket}/unix_socket.rb +6 -4
  23. data/lib/mongo/cursor.rb +113 -47
  24. data/lib/mongo/db.rb +203 -131
  25. data/lib/mongo/{exceptions.rb → exception.rb} +7 -1
  26. data/lib/mongo/functional.rb +19 -0
  27. data/lib/mongo/functional/authentication.rb +303 -0
  28. data/lib/mongo/{util → functional}/logging.rb +1 -1
  29. data/lib/mongo/{util → functional}/read_preference.rb +49 -1
  30. data/lib/mongo/{util → functional}/uri_parser.rb +81 -69
  31. data/lib/mongo/{util → functional}/write_concern.rb +2 -1
  32. data/{test/unit/pool_test.rb → lib/mongo/gridfs.rb} +5 -10
  33. data/lib/mongo/gridfs/grid.rb +1 -3
  34. data/lib/mongo/gridfs/grid_ext.rb +1 -1
  35. data/lib/mongo/gridfs/grid_file_system.rb +1 -1
  36. data/lib/mongo/gridfs/grid_io.rb +1 -1
  37. data/lib/mongo/legacy.rb +63 -8
  38. data/lib/mongo/mongo_client.rb +128 -154
  39. data/lib/mongo/mongo_replica_set_client.rb +17 -11
  40. data/lib/mongo/mongo_sharded_client.rb +2 -1
  41. data/lib/mongo/networking.rb +19 -10
  42. data/lib/mongo/utils.rb +19 -0
  43. data/lib/mongo/{util → utils}/conversions.rb +1 -1
  44. data/lib/mongo/{util → utils}/core_ext.rb +1 -1
  45. data/lib/mongo/{util → utils}/server_version.rb +1 -1
  46. data/lib/mongo/{util → utils}/support.rb +10 -57
  47. data/lib/mongo/{util → utils}/thread_local_variable_manager.rb +1 -1
  48. data/test/functional/authentication_test.rb +8 -21
  49. data/test/functional/bulk_write_collection_view_test.rb +782 -0
  50. data/test/functional/{connection_test.rb → client_test.rb} +153 -78
  51. data/test/functional/collection_test.rb +343 -97
  52. data/test/functional/collection_writer_test.rb +83 -0
  53. data/test/functional/conversions_test.rb +1 -3
  54. data/test/functional/cursor_fail_test.rb +3 -3
  55. data/test/functional/cursor_message_test.rb +3 -3
  56. data/test/functional/cursor_test.rb +38 -3
  57. data/test/functional/db_api_test.rb +5 -5
  58. data/test/functional/db_connection_test.rb +2 -2
  59. data/test/functional/db_test.rb +35 -11
  60. data/test/functional/grid_file_system_test.rb +2 -2
  61. data/test/functional/grid_io_test.rb +2 -2
  62. data/test/functional/grid_test.rb +2 -2
  63. data/test/functional/pool_test.rb +2 -3
  64. data/test/functional/safe_test.rb +5 -5
  65. data/test/functional/ssl_test.rb +22 -102
  66. data/test/functional/support_test.rb +1 -1
  67. data/test/functional/timeout_test.rb +6 -22
  68. data/test/functional/uri_test.rb +113 -12
  69. data/test/functional/write_concern_test.rb +6 -6
  70. data/test/helpers/general.rb +50 -0
  71. data/test/helpers/test_unit.rb +309 -0
  72. data/test/replica_set/authentication_test.rb +8 -23
  73. data/test/replica_set/basic_test.rb +41 -14
  74. data/test/replica_set/client_test.rb +179 -117
  75. data/test/replica_set/complex_connect_test.rb +6 -7
  76. data/test/replica_set/connection_test.rb +46 -38
  77. data/test/replica_set/count_test.rb +2 -2
  78. data/test/replica_set/cursor_test.rb +8 -8
  79. data/test/replica_set/insert_test.rb +64 -2
  80. data/test/replica_set/max_values_test.rb +59 -10
  81. data/test/replica_set/pinning_test.rb +2 -2
  82. data/test/replica_set/query_test.rb +2 -2
  83. data/test/replica_set/read_preference_test.rb +6 -6
  84. data/test/replica_set/refresh_test.rb +7 -7
  85. data/test/replica_set/replication_ack_test.rb +5 -5
  86. data/test/replica_set/ssl_test.rb +24 -106
  87. data/test/sharded_cluster/basic_test.rb +43 -15
  88. data/test/shared/authentication/basic_auth_shared.rb +215 -0
  89. data/test/shared/authentication/sasl_plain_shared.rb +96 -0
  90. data/test/shared/ssl_shared.rb +173 -0
  91. data/test/test_helper.rb +31 -199
  92. data/test/threading/basic_test.rb +29 -3
  93. data/test/tools/mongo_config.rb +45 -20
  94. data/test/tools/mongo_config_test.rb +1 -1
  95. data/test/unit/client_test.rb +136 -57
  96. data/test/unit/collection_test.rb +31 -55
  97. data/test/unit/connection_test.rb +135 -72
  98. data/test/unit/cursor_test.rb +2 -2
  99. data/test/unit/db_test.rb +19 -15
  100. data/test/unit/grid_test.rb +2 -2
  101. data/test/unit/mongo_sharded_client_test.rb +17 -15
  102. data/test/unit/node_test.rb +2 -2
  103. data/test/unit/pool_manager_test.rb +7 -5
  104. data/test/unit/read_pref_test.rb +82 -2
  105. data/test/unit/read_test.rb +14 -14
  106. data/test/unit/safe_test.rb +9 -9
  107. data/test/unit/sharding_pool_manager_test.rb +11 -5
  108. data/test/unit/write_concern_test.rb +9 -9
  109. metadata +71 -56
  110. metadata.gz.sig +0 -0
  111. data/test/functional/threading_test.rb +0 -109
  112. data/test/shared/authentication.rb +0 -121
  113. data/test/unit/util_test.rb +0 -69
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -14,13 +14,13 @@
14
14
 
15
15
  require 'test_helper'
16
16
 
17
- class TestThreading < Test::Unit::TestCase
17
+ class ThreadingTest < Test::Unit::TestCase
18
18
 
19
19
  include Mongo
20
20
 
21
21
  def setup
22
22
  @client = standard_connection(:pool_size => 10, :pool_timeout => 30)
23
- @db = @client.db(MONGO_TEST_DB)
23
+ @db = @client.db(TEST_DB)
24
24
  @coll = @db.collection('thread-test-collection')
25
25
  @coll.drop
26
26
 
@@ -91,4 +91,30 @@ class TestThreading < Test::Unit::TestCase
91
91
  assert thread_values.all?{|v| v == 1000}
92
92
  assert_equal thread_values.size, n_threads
93
93
  end
94
+
95
+ def test_threading
96
+ @coll.drop
97
+ @coll = @db.collection('thread-test-collection')
98
+
99
+ docs = []
100
+ 1000.times {|i| docs << {:x => i}}
101
+ @coll.insert(docs)
102
+
103
+ threads = []
104
+
105
+ 10.times do |i|
106
+ threads[i] = Thread.new do
107
+ sum = 0
108
+ @coll.find().each do |document|
109
+ sum += document["x"]
110
+ end
111
+ assert_equal 499500, sum
112
+ end
113
+ end
114
+
115
+ 10.times do |i|
116
+ threads[i].join
117
+ end
118
+ end
119
+
94
120
  end
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- # Copyright (C) 2013 10gen Inc.
3
+ # Copyright (C) 2009-2013 MongoDB, Inc.
4
4
  #
5
5
  # Licensed under the Apache License, Version 2.0 (the "License");
6
6
  # you may not use this file except in compliance with the License.
@@ -41,7 +41,7 @@ module Mongo
41
41
  class Config
42
42
  DEFAULT_BASE_OPTS = { :host => 'localhost', :dbpath => 'data', :logpath => 'data/log' }
43
43
  DEFAULT_REPLICA_SET = DEFAULT_BASE_OPTS.merge( :replicas => 3, :arbiters => 0 )
44
- DEFAULT_SHARDED_SIMPLE = DEFAULT_BASE_OPTS.merge( :shards => 2, :configs => 1, :routers => 4 )
44
+ DEFAULT_SHARDED_SIMPLE = DEFAULT_BASE_OPTS.merge( :shards => 2, :configs => 1, :routers => 2 )
45
45
  DEFAULT_SHARDED_REPLICA = DEFAULT_SHARDED_SIMPLE.merge( :replicas => 3, :arbiters => 0)
46
46
 
47
47
  IGNORE_KEYS = [:host, :command, :_id]
@@ -50,7 +50,7 @@ module Mongo
50
50
  MONGODS_OPT_KEYS = [:mongods]
51
51
  CLUSTER_OPT_KEYS = SHARDING_OPT_KEYS + REPLICA_OPT_KEYS + MONGODS_OPT_KEYS
52
52
 
53
- FLAGS = [:noprealloc, :smallfiles, :logappend, :configsvr, :shardsvr, :quiet, :fastsync, :auth]
53
+ FLAGS = [:noprealloc, :smallfiles, :logappend, :configsvr, :shardsvr, :quiet, :fastsync, :auth, :ipv6]
54
54
 
55
55
  DEFAULT_VERIFIES = 60
56
56
  BASE_PORT = 3000
@@ -64,6 +64,7 @@ module Mongo
64
64
  raise "missing required option" if [:host, :dbpath].any?{|k| !opts[k]}
65
65
 
66
66
  config = opts.reject {|k,v| CLUSTER_OPT_KEYS.include?(k)}
67
+
67
68
  kinds = CLUSTER_OPT_KEYS.select{|key| opts.has_key?(key)} # order is significant
68
69
 
69
70
  replica_count = 0
@@ -117,6 +118,7 @@ module Mongo
117
118
  quiet = opts[:quiet] || true
118
119
  fast_sync = opts[:fastsync] || false
119
120
  auth = opts[:auth] || true
121
+ ipv6 = opts[:ipv6].nil? ? true : opts[:ipv6]
120
122
 
121
123
  params.merge(:command => mongod,
122
124
  :dbpath => path,
@@ -124,7 +126,8 @@ module Mongo
124
126
  :noprealloc => noprealloc,
125
127
  :quiet => quiet,
126
128
  :fastsync => fast_sync,
127
- :auth => auth)
129
+ :auth => auth,
130
+ :ipv6 => ipv6)
128
131
  end
129
132
 
130
133
  def self.make_replica(opts, id)
@@ -383,27 +386,48 @@ module Mongo
383
386
  end
384
387
 
385
388
  def repl_set_startup
386
- states = nil
389
+ states = nil
390
+ healthy = false
391
+
387
392
  60.times do
388
- states = repl_set_get_status.zip(repl_set_is_master)
393
+ # enter the thunderdome...
394
+ states = repl_set_get_status.zip(repl_set_is_master)
389
395
  healthy = states.all? do |status, is_master|
390
- members = status['members']
391
- if status['ok'] == 1.0 && members.collect{|m| m['state']}.all?{|state| [1,2,7].index(state)}
392
- members.any?{|m| m['state'] == 1} &&
393
- case status['myState']
394
- when 1
395
- is_master['ismaster'] == true && is_master['secondary'] == false
396
- when 2
397
- is_master['ismaster'] == false && is_master['secondary'] == true
398
- when 7
399
- is_master['ismaster'] == false && is_master['secondary'] == false
400
- end
396
+ # check replica set status for member list
397
+ next unless status['ok'] == 1.0 && (members = status['members'])
398
+
399
+ # ensure all replica set members are in a valid state
400
+ next unless members.all? { |m| [1,2,7].include?(m['state']) }
401
+
402
+ # check for primary replica set member
403
+ next unless (primary = members.find { |m| m['state'] == 1 })
404
+
405
+ # check replica set member optimes
406
+ primary_optime = primary['optime'].seconds
407
+ next unless primary_optime && members.all? do |m|
408
+ m['state'] == 7 || primary_optime - m['optime'].seconds < 5
409
+ end
410
+
411
+ # check replica set state
412
+ case status['myState']
413
+ when 1
414
+ is_master['ismaster'] == true &&
415
+ is_master['secondary'] == false
416
+ when 2
417
+ is_master['ismaster'] == false &&
418
+ is_master['secondary'] == true
419
+ when 7
420
+ is_master['ismaster'] == false &&
421
+ is_master['secondary'] == false
401
422
  end
402
423
  end
403
- return true if healthy
424
+
425
+ return healthy if healthy
404
426
  sleep(1)
405
427
  end
406
- raise Mongo::OperationFailure, "replSet startup failed - status: #{states.inspect}"
428
+
429
+ raise Mongo::OperationFailure,
430
+ "replSet startup failed - status: #{states.inspect}"
407
431
  end
408
432
 
409
433
  def repl_set_seeds
@@ -424,7 +448,8 @@ module Mongo
424
448
 
425
449
  def member_names_by_state(state)
426
450
  states = Array(state)
427
- status = repl_set_get_status.first
451
+ # Any status with a REMOVED node won't have the full cluster state
452
+ status = repl_set_get_status.find {|status| status['members'].find {|m| m['state'] == 'REMOVED'}.nil?}
428
453
  status['members'].find_all{|member| states.index(member['state']) }.collect{|member| member['name']}
429
454
  end
430
455
 
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2013 10gen Inc.
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -15,7 +15,7 @@
15
15
  require 'test_helper'
16
16
  include Mongo
17
17
 
18
- class ClientTest < Test::Unit::TestCase
18
+ class ClientUnitTest < Test::Unit::TestCase
19
19
  context "Mongo::MongoClient initialization " do
20
20
  context "given a single node" do
21
21
  setup do
@@ -92,7 +92,7 @@ class ClientTest < Test::Unit::TestCase
92
92
 
93
93
  context "initializing with a unix socket" do
94
94
  setup do
95
- @connection = Mongo::Connection.new('/tmp/mongod.sock', :safe => true, :connect => false)
95
+ @connection = Mongo::Connection.new('/tmp/mongod.sock', :connect => false)
96
96
  UNIXSocket.stubs(:new).returns(new_mock_unix_socket)
97
97
  end
98
98
  should "parse a unix socket" do
@@ -106,11 +106,19 @@ class ClientTest < Test::Unit::TestCase
106
106
  assert_equal ['localhost', 27017], @client.host_port
107
107
  end
108
108
 
109
- #should "parse a unix socket" do
110
- # socket_address = "/tmp/mongodb-27017.sock"
111
- # @client = MongoClient.from_uri("mongodb://#{socket_address}")
112
- # assert_equal socket_address, @client.host_port.first
113
- #end
109
+ should "set auth source" do
110
+ @client = MongoClient.from_uri("mongodb://user:pass@localhost?authSource=foo", :connect => false)
111
+ assert_equal 'foo', @client.auths.first[:source]
112
+ end
113
+
114
+ should "set auth mechanism" do
115
+ @client = MongoClient.from_uri("mongodb://user@localhost?authMechanism=MONGODB-X509", :connect => false)
116
+ assert_equal 'MONGODB-X509', @client.auths.first[:mechanism]
117
+
118
+ assert_raise MongoArgumentError do
119
+ MongoClient.from_uri("mongodb://user@localhost?authMechanism=INVALID", :connect => false)
120
+ end
121
+ end
114
122
 
115
123
  should "allow a complex host names" do
116
124
  host_name = "foo.bar-12345.org"
@@ -142,8 +150,15 @@ class ClientTest < Test::Unit::TestCase
142
150
  should "parse a uri with a hyphen & underscore in the username or password" do
143
151
  @client = MongoClient.from_uri("mongodb://hyphen-user_name:p-s_s@localhost:27017/db", :connect => false)
144
152
  assert_equal ['localhost', 27017], @client.host_port
145
- auth_hash = { :db_name => 'db', :username => 'hyphen-user_name', :password => 'p-s_s' }
146
- assert_equal auth_hash, @client.auths[0]
153
+
154
+ auth_hash = {
155
+ :db_name => 'db',
156
+ :username => 'hyphen-user_name',
157
+ :password => 'p-s_s',
158
+ :source => 'db',
159
+ :mechanism => Authentication::DEFAULT_MECHANISM
160
+ }
161
+ assert_equal auth_hash, @client.auths.first
147
162
  end
148
163
 
149
164
  should "attempt to connect" do
@@ -166,99 +181,163 @@ class ClientTest < Test::Unit::TestCase
166
181
  end
167
182
  end
168
183
 
169
- should "require all of username, if password and db are specified" do
184
+ should "require password if using legacy auth and username present" do
170
185
  assert MongoClient.from_uri("mongodb://kyle:jones@localhost/db", :connect => false)
171
186
 
172
187
  assert_raise MongoArgumentError do
173
- MongoClient.from_uri("mongodb://kyle:password@localhost", :connect => false)
188
+ MongoClient.from_uri("mongodb://kyle:@localhost", :connect => false)
189
+ end
190
+
191
+ assert_raise MongoArgumentError do
192
+ MongoClient.from_uri("mongodb://kyle@localhost", :connect => false)
174
193
  end
175
194
  end
176
195
  end
177
196
 
178
197
  context "initializing with ENV['MONGODB_URI']" do
179
- setup do
180
- @old_mongodb_uri = ENV['MONGODB_URI']
198
+ should "parse a simple uri" do
199
+ uri = "mongodb://localhost?connect=false"
200
+ with_preserved_env_uri(uri) do
201
+ @client = MongoClient.new
202
+ assert_equal ['localhost', 27017], @client.host_port
203
+ end
181
204
  end
182
205
 
183
- teardown do
184
- ENV['MONGODB_URI'] = @old_mongodb_uri
206
+ should "set auth source" do
207
+ uri = "mongodb://user:pass@localhost?authSource=foo&connect=false"
208
+ with_preserved_env_uri(uri) do
209
+ @client = MongoClient.new
210
+ assert_equal 'foo', @client.auths.first[:source]
211
+ end
185
212
  end
186
213
 
187
- should "parse a simple uri" do
188
- ENV['MONGODB_URI'] = "mongodb://localhost?connect=false"
189
- @client = MongoClient.new
190
- assert_equal ['localhost', 27017], @client.host_port
214
+ should "set auth mechanism" do
215
+ uri = "mongodb://user@localhost?authMechanism=MONGODB-X509&connect=false"
216
+ with_preserved_env_uri(uri) do
217
+ @client = MongoClient.new
218
+ assert_equal 'MONGODB-X509', @client.auths.first[:mechanism]
219
+
220
+ ENV['MONGODB_URI'] = "mongodb://user@localhost?authMechanism=INVALID&connect=false"
221
+ assert_raise MongoArgumentError do
222
+ MongoClient.new
223
+ end
224
+ end
191
225
  end
192
226
 
193
227
  should "allow a complex host names" do
194
228
  host_name = "foo.bar-12345.org"
195
- ENV['MONGODB_URI'] = "mongodb://#{host_name}?connect=false"
196
- @client = MongoClient.new
197
- assert_equal [host_name, 27017], @client.host_port
229
+ uri = "mongodb://#{host_name}?connect=false"
230
+ with_preserved_env_uri(uri) do
231
+ @client = MongoClient.new
232
+ assert_equal [host_name, 27017], @client.host_port
233
+ end
198
234
  end
199
235
 
200
236
  should "allow db without username and password" do
201
237
  host_name = "foo.bar-12345.org"
202
- ENV['MONGODB_URI'] = "mongodb://#{host_name}/foo?connect=false"
203
- @client = MongoClient.new
204
- assert_equal [host_name, 27017], @client.host_port
238
+ uri = "mongodb://#{host_name}/foo?connect=false"
239
+ with_preserved_env_uri(uri) do
240
+ @client = MongoClient.new
241
+ assert_equal [host_name, 27017], @client.host_port
242
+ end
205
243
  end
206
244
 
207
245
  should "set write concern options on connection" do
208
246
  host_name = "localhost"
209
247
  opts = "w=2&wtimeoutMS=1000&fsync=true&journal=true&connect=false"
210
- ENV['MONGODB_URI'] = "mongodb://#{host_name}/foo?#{opts}"
211
- @client = MongoClient.new
212
- assert_equal({:w => 2, :wtimeout => 1000, :fsync => true, :j => true}, @client.write_concern)
248
+ uri = "mongodb://#{host_name}/foo?#{opts}"
249
+ with_preserved_env_uri(uri) do
250
+ @client = MongoClient.new
251
+ assert_equal({:w => 2, :wtimeout => 1000, :fsync => true, :j => true}, @client.write_concern)
252
+ end
213
253
  end
214
254
 
215
255
  should "set timeout options on connection" do
216
256
  host_name = "localhost"
217
257
  opts = "connectTimeoutMS=1000&socketTimeoutMS=5000&connect=false"
218
- ENV['MONGODB_URI'] = "mongodb://#{host_name}/foo?#{opts}"
219
- @client = MongoClient.new
220
- assert_equal 1, @client.connect_timeout
221
- assert_equal 5, @client.op_timeout
258
+ uri = "mongodb://#{host_name}/foo?#{opts}"
259
+ with_preserved_env_uri(uri) do
260
+ @client = MongoClient.new
261
+ assert_equal 1, @client.connect_timeout
262
+ assert_equal 5, @client.op_timeout
263
+ end
222
264
  end
223
265
 
224
266
  should "parse a uri with a hyphen & underscore in the username or password" do
225
- ENV['MONGODB_URI'] = "mongodb://hyphen-user_name:p-s_s@localhost:27017/db?connect=false"
226
- @client = MongoClient.new
227
- assert_equal ['localhost', 27017], @client.host_port
228
- auth_hash = { :db_name => 'db', :username => 'hyphen-user_name', :password => 'p-s_s' }
229
- assert_equal auth_hash, @client.auths[0]
267
+ uri = "mongodb://hyphen-user_name:p-s_s@localhost:27017/db?connect=false"
268
+ with_preserved_env_uri(uri) do
269
+ @client = MongoClient.new
270
+ assert_equal ['localhost', 27017], @client.host_port
271
+
272
+ auth_hash = {
273
+ :db_name => 'db',
274
+ :username => 'hyphen-user_name',
275
+ :password => 'p-s_s',
276
+ :source => 'db',
277
+ :mechanism => Authentication::DEFAULT_MECHANISM
278
+ }
279
+ assert_equal auth_hash, @client.auths.first
280
+ end
230
281
  end
231
282
 
232
283
  should "attempt to connect" do
233
284
  TCPSocket.stubs(:new).returns(new_mock_socket)
234
- ENV['MONGODB_URI'] = "mongodb://localhost?connect=false" # connect=false ??
235
- @client = MongoClient.new
236
-
237
- admin_db = new_mock_db
238
- admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
239
- @client.expects(:[]).with('admin').returns(admin_db)
240
- @client.connect
285
+ uri = "mongodb://localhost?connect=false"
286
+ with_preserved_env_uri(uri) do
287
+ @client = MongoClient.new
288
+
289
+ admin_db = new_mock_db
290
+ admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
291
+ @client.expects(:[]).with('admin').returns(admin_db)
292
+ @client.connect
293
+ end
241
294
  end
242
295
 
243
296
  should "raise an error on invalid uris" do
244
- ENV['MONGODB_URI'] = "mongo://localhost"
245
- assert_raise MongoArgumentError do
246
- MongoClient.new
297
+ uri = "mongo://localhost"
298
+ with_preserved_env_uri(uri) do
299
+ assert_raise MongoArgumentError do
300
+ MongoClient.new
301
+ end
302
+
303
+ ENV['MONGODB_URI'] = "mongodb://localhost:abc?connect=false"
304
+ assert_raise MongoArgumentError do
305
+ MongoClient.new
306
+ end
247
307
  end
308
+ end
248
309
 
249
- ENV['MONGODB_URI'] = "mongodb://localhost:abc"
250
- assert_raise MongoArgumentError do
251
- MongoClient.new
310
+ should "require password if using legacy auth and username present" do
311
+ uri = "mongodb://kyle:jones@localhost?connect=false"
312
+ with_preserved_env_uri(uri) do
313
+ assert MongoClient.new
314
+
315
+ ENV['MONGODB_URI'] = "mongodb://kyle:@localhost?connect=false"
316
+ assert_raise MongoArgumentError do
317
+ MongoClient.new
318
+ end
319
+
320
+ ENV['MONGODB_URI'] = "mongodb://kyle@localhost?connect=false"
321
+ assert_raise MongoArgumentError do
322
+ MongoClient.new
323
+ end
252
324
  end
253
325
  end
254
326
 
255
- should "require all of username, if password and db are specified" do
256
- ENV['MONGODB_URI'] = "mongodb://kyle:jones@localhost/db?connect=false"
257
- assert MongoClient.new
327
+ should "require password if using PLAIN auth and username present" do
328
+ uri = "mongodb://kyle:jones@localhost?connect=false&authMechanism=PLAIN"
329
+ with_preserved_env_uri(uri) do
330
+ assert MongoClient.new
258
331
 
259
- ENV['MONGODB_URI'] = "mongodb://kyle:password@localhost"
260
- assert_raise MongoArgumentError do
261
- MongoClient.new
332
+ ENV['MONGODB_URI'] = "mongodb://kyle:@localhost?connect=false&authMechanism=PLAIN"
333
+ assert_raise MongoArgumentError do
334
+ MongoClient.new
335
+ end
336
+
337
+ ENV['MONGODB_URI'] = "mongodb://kyle@localhost?connect=false&authMechanism=PLAIN"
338
+ assert_raise MongoArgumentError do
339
+ MongoClient.new
340
+ end
262
341
  end
263
342
  end
264
343
  end