mongo 1.3.0 → 1.12.5

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 (185) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data/{LICENSE.txt → LICENSE} +1 -1
  4. data/README.md +122 -271
  5. data/Rakefile +25 -209
  6. data/VERSION +1 -0
  7. data/bin/mongo_console +31 -9
  8. data/lib/mongo/bulk_write_collection_view.rb +387 -0
  9. data/lib/mongo/collection.rb +576 -269
  10. data/lib/mongo/collection_writer.rb +364 -0
  11. data/lib/mongo/connection/node.rb +249 -0
  12. data/lib/mongo/connection/pool.rb +340 -0
  13. data/lib/mongo/connection/pool_manager.rb +320 -0
  14. data/lib/mongo/connection/sharding_pool_manager.rb +67 -0
  15. data/lib/mongo/connection/socket/socket_util.rb +37 -0
  16. data/lib/mongo/connection/socket/ssl_socket.rb +95 -0
  17. data/lib/mongo/connection/socket/tcp_socket.rb +87 -0
  18. data/lib/mongo/connection/socket/unix_socket.rb +39 -0
  19. data/lib/mongo/connection/socket.rb +18 -0
  20. data/lib/mongo/connection.rb +7 -875
  21. data/lib/mongo/cursor.rb +403 -117
  22. data/lib/mongo/db.rb +444 -243
  23. data/lib/mongo/exception.rb +145 -0
  24. data/lib/mongo/functional/authentication.rb +455 -0
  25. data/lib/mongo/functional/logging.rb +85 -0
  26. data/lib/mongo/functional/read_preference.rb +183 -0
  27. data/lib/mongo/functional/scram.rb +556 -0
  28. data/lib/mongo/functional/uri_parser.rb +409 -0
  29. data/lib/mongo/functional/write_concern.rb +66 -0
  30. data/lib/mongo/functional.rb +20 -0
  31. data/lib/mongo/gridfs/grid.rb +30 -24
  32. data/lib/mongo/gridfs/grid_ext.rb +6 -10
  33. data/lib/mongo/gridfs/grid_file_system.rb +38 -20
  34. data/lib/mongo/gridfs/grid_io.rb +84 -75
  35. data/lib/mongo/gridfs.rb +18 -0
  36. data/lib/mongo/legacy.rb +140 -0
  37. data/lib/mongo/mongo_client.rb +697 -0
  38. data/lib/mongo/mongo_replica_set_client.rb +535 -0
  39. data/lib/mongo/mongo_sharded_client.rb +159 -0
  40. data/lib/mongo/networking.rb +372 -0
  41. data/lib/mongo/{util → utils}/conversions.rb +29 -8
  42. data/lib/mongo/{util → utils}/core_ext.rb +28 -18
  43. data/lib/mongo/{util → utils}/server_version.rb +4 -6
  44. data/lib/mongo/{util → utils}/support.rb +29 -31
  45. data/lib/mongo/utils/thread_local_variable_manager.rb +25 -0
  46. data/lib/mongo/utils.rb +19 -0
  47. data/lib/mongo.rb +51 -50
  48. data/mongo.gemspec +29 -32
  49. data/test/functional/authentication_test.rb +39 -0
  50. data/test/functional/bulk_api_stress_test.rb +133 -0
  51. data/test/functional/bulk_write_collection_view_test.rb +1198 -0
  52. data/test/functional/client_test.rb +627 -0
  53. data/test/functional/collection_test.rb +2175 -0
  54. data/test/functional/collection_writer_test.rb +83 -0
  55. data/test/{conversions_test.rb → functional/conversions_test.rb} +47 -3
  56. data/test/functional/cursor_fail_test.rb +57 -0
  57. data/test/functional/cursor_message_test.rb +56 -0
  58. data/test/functional/cursor_test.rb +683 -0
  59. data/test/functional/db_api_test.rb +835 -0
  60. data/test/functional/db_connection_test.rb +25 -0
  61. data/test/functional/db_test.rb +348 -0
  62. data/test/functional/grid_file_system_test.rb +285 -0
  63. data/test/{grid_io_test.rb → functional/grid_io_test.rb} +72 -11
  64. data/test/{grid_test.rb → functional/grid_test.rb} +88 -15
  65. data/test/functional/pool_test.rb +136 -0
  66. data/test/functional/safe_test.rb +98 -0
  67. data/test/functional/ssl_test.rb +29 -0
  68. data/test/functional/support_test.rb +62 -0
  69. data/test/functional/timeout_test.rb +60 -0
  70. data/test/functional/uri_test.rb +446 -0
  71. data/test/functional/write_concern_test.rb +118 -0
  72. data/test/helpers/general.rb +50 -0
  73. data/test/helpers/test_unit.rb +476 -0
  74. data/test/replica_set/authentication_test.rb +37 -0
  75. data/test/replica_set/basic_test.rb +189 -0
  76. data/test/replica_set/client_test.rb +393 -0
  77. data/test/replica_set/connection_test.rb +138 -0
  78. data/test/replica_set/count_test.rb +66 -0
  79. data/test/replica_set/cursor_test.rb +220 -0
  80. data/test/replica_set/insert_test.rb +157 -0
  81. data/test/replica_set/max_values_test.rb +151 -0
  82. data/test/replica_set/pinning_test.rb +105 -0
  83. data/test/replica_set/query_test.rb +73 -0
  84. data/test/replica_set/read_preference_test.rb +219 -0
  85. data/test/replica_set/refresh_test.rb +211 -0
  86. data/test/replica_set/replication_ack_test.rb +95 -0
  87. data/test/replica_set/ssl_test.rb +32 -0
  88. data/test/sharded_cluster/basic_test.rb +203 -0
  89. data/test/shared/authentication/basic_auth_shared.rb +260 -0
  90. data/test/shared/authentication/bulk_api_auth_shared.rb +249 -0
  91. data/test/shared/authentication/gssapi_shared.rb +176 -0
  92. data/test/shared/authentication/sasl_plain_shared.rb +96 -0
  93. data/test/shared/authentication/scram_shared.rb +92 -0
  94. data/test/shared/ssl_shared.rb +235 -0
  95. data/test/test_helper.rb +53 -94
  96. data/test/threading/basic_test.rb +120 -0
  97. data/test/tools/mongo_config.rb +708 -0
  98. data/test/tools/mongo_config_test.rb +160 -0
  99. data/test/unit/client_test.rb +381 -0
  100. data/test/unit/collection_test.rb +89 -53
  101. data/test/unit/connection_test.rb +282 -32
  102. data/test/unit/cursor_test.rb +206 -8
  103. data/test/unit/db_test.rb +55 -13
  104. data/test/unit/grid_test.rb +43 -16
  105. data/test/unit/mongo_sharded_client_test.rb +48 -0
  106. data/test/unit/node_test.rb +93 -0
  107. data/test/unit/pool_manager_test.rb +111 -0
  108. data/test/unit/read_pref_test.rb +406 -0
  109. data/test/unit/read_test.rb +159 -0
  110. data/test/unit/safe_test.rb +69 -36
  111. data/test/unit/sharding_pool_manager_test.rb +84 -0
  112. data/test/unit/write_concern_test.rb +175 -0
  113. data.tar.gz.sig +3 -0
  114. metadata +227 -216
  115. metadata.gz.sig +0 -0
  116. data/docs/CREDITS.md +0 -123
  117. data/docs/FAQ.md +0 -116
  118. data/docs/GridFS.md +0 -158
  119. data/docs/HISTORY.md +0 -244
  120. data/docs/RELEASES.md +0 -33
  121. data/docs/REPLICA_SETS.md +0 -72
  122. data/docs/TUTORIAL.md +0 -247
  123. data/docs/WRITE_CONCERN.md +0 -28
  124. data/lib/mongo/exceptions.rb +0 -71
  125. data/lib/mongo/gridfs/grid_io_fix.rb +0 -38
  126. data/lib/mongo/repl_set_connection.rb +0 -342
  127. data/lib/mongo/test.rb +0 -20
  128. data/lib/mongo/util/pool.rb +0 -177
  129. data/lib/mongo/util/uri_parser.rb +0 -185
  130. data/test/async/collection_test.rb +0 -224
  131. data/test/async/connection_test.rb +0 -24
  132. data/test/async/cursor_test.rb +0 -162
  133. data/test/async/worker_pool_test.rb +0 -99
  134. data/test/auxillary/1.4_features.rb +0 -166
  135. data/test/auxillary/authentication_test.rb +0 -68
  136. data/test/auxillary/autoreconnect_test.rb +0 -41
  137. data/test/auxillary/fork_test.rb +0 -30
  138. data/test/auxillary/repl_set_auth_test.rb +0 -58
  139. data/test/auxillary/slave_connection_test.rb +0 -36
  140. data/test/auxillary/threaded_authentication_test.rb +0 -101
  141. data/test/bson/binary_test.rb +0 -15
  142. data/test/bson/bson_test.rb +0 -649
  143. data/test/bson/byte_buffer_test.rb +0 -208
  144. data/test/bson/hash_with_indifferent_access_test.rb +0 -38
  145. data/test/bson/json_test.rb +0 -17
  146. data/test/bson/object_id_test.rb +0 -154
  147. data/test/bson/ordered_hash_test.rb +0 -204
  148. data/test/bson/timestamp_test.rb +0 -24
  149. data/test/collection_test.rb +0 -910
  150. data/test/connection_test.rb +0 -309
  151. data/test/cursor_fail_test.rb +0 -75
  152. data/test/cursor_message_test.rb +0 -43
  153. data/test/cursor_test.rb +0 -483
  154. data/test/db_api_test.rb +0 -726
  155. data/test/db_connection_test.rb +0 -15
  156. data/test/db_test.rb +0 -287
  157. data/test/grid_file_system_test.rb +0 -243
  158. data/test/load/resque/load.rb +0 -21
  159. data/test/load/resque/processor.rb +0 -26
  160. data/test/load/thin/load.rb +0 -24
  161. data/test/load/unicorn/load.rb +0 -23
  162. data/test/load/unicorn/unicorn.rb +0 -29
  163. data/test/replica_sets/connect_test.rb +0 -94
  164. data/test/replica_sets/connection_string_test.rb +0 -32
  165. data/test/replica_sets/count_test.rb +0 -35
  166. data/test/replica_sets/insert_test.rb +0 -53
  167. data/test/replica_sets/pooled_insert_test.rb +0 -55
  168. data/test/replica_sets/query_secondaries.rb +0 -96
  169. data/test/replica_sets/query_test.rb +0 -51
  170. data/test/replica_sets/replication_ack_test.rb +0 -66
  171. data/test/replica_sets/rs_test_helper.rb +0 -27
  172. data/test/safe_test.rb +0 -68
  173. data/test/support/hash_with_indifferent_access.rb +0 -186
  174. data/test/support/keys.rb +0 -45
  175. data/test/support_test.rb +0 -18
  176. data/test/threading/threading_with_large_pool_test.rb +0 -90
  177. data/test/threading_test.rb +0 -87
  178. data/test/tools/auth_repl_set_manager.rb +0 -14
  179. data/test/tools/load.rb +0 -58
  180. data/test/tools/repl_set_manager.rb +0 -266
  181. data/test/tools/sharding_manager.rb +0 -202
  182. data/test/tools/test.rb +0 -4
  183. data/test/unit/pool_test.rb +0 -9
  184. data/test/unit/repl_set_connection_test.rb +0 -59
  185. data/test/uri_test.rb +0 -91
@@ -1,114 +1,125 @@
1
- require './test/test_helper'
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'test_helper'
16
+
17
+ module Mongo
18
+ class Collection
19
+ attr_reader :operation_writer,
20
+ :command_writer
21
+ end
22
+ end
2
23
 
3
- class CollectionTest < Test::Unit::TestCase
24
+ class CollectionUnitTest < Test::Unit::TestCase
4
25
 
5
26
  context "Basic operations: " do
6
27
  setup do
7
28
  @logger = mock()
29
+ @logger.stubs(:level => 0)
8
30
  @logger.expects(:debug)
31
+
32
+ @client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
33
+ @db = @client[TEST_DB]
34
+ @coll = @db.collection('collection-unit-test')
9
35
  end
10
36
 
11
37
  should "send update message" do
12
- @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
13
- @db = @conn['testing']
14
- @coll = @db.collection('books')
15
- @conn.expects(:send_message).with do |op, msg, log|
38
+ @client.expects(:send_message_with_gle).with do |op, msg, log|
16
39
  op == 2001
17
40
  end
18
- @conn.stubs(:log_operation)
41
+ @coll.operation_writer.stubs(:log_operation)
19
42
  @coll.update({}, {:title => 'Moby Dick'})
20
43
  end
21
44
 
22
45
  should "send insert message" do
23
- @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
24
- @db = @conn['testing']
25
- @coll = @db.collection('books')
26
- @conn.expects(:send_message).with do |op, msg, log|
46
+ @client.expects(:send_message_with_gle).with do |op, msg, log|
27
47
  op == 2002
28
48
  end
29
- @conn.expects(:log_operation).with do |name, payload|
30
- (name == :insert) && payload[:documents][0][:title].include?('Moby')
49
+ @coll.operation_writer.expects(:log_operation).with do |name, payload|
50
+ (name == :insert) && payload[:documents][:title].include?('Moby')
31
51
  end
32
52
  @coll.insert({:title => 'Moby Dick'})
33
53
  end
34
54
 
35
55
  should "send sort data" do
36
- @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
37
- @db = @conn['testing']
38
- @coll = @db.collection('books')
39
- @conn.expects(:receive_message).with do |op, msg, log, sock|
56
+ @client.expects(:checkout_reader).returns(new_mock_socket)
57
+ @client.expects(:receive_message).with do |op, msg, log, sock|
40
58
  op == 2004
41
59
  end.returns([[], 0, 0])
42
- @conn.expects(:log_operation).with do |name, payload|
43
- (name == :find) && payload[:selector][:title].include?('Moby')
44
- end
60
+ @logger.expects(:debug)
45
61
  @coll.find({:title => 'Moby Dick'}).sort([['title', 1], ['author', 1]]).next_document
46
62
  end
47
63
 
48
64
  should "not log binary data" do
49
- @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
50
- @db = @conn['testing']
51
- @coll = @db.collection('books')
52
65
  data = BSON::Binary.new(("BINARY " * 1000).unpack("c*"))
53
- @conn.expects(:send_message).with do |op, msg, log|
66
+ @client.expects(:send_message_with_gle).with do |op, msg, log|
54
67
  op == 2002
55
68
  end
56
- @conn.expects(:log_operation).with do |name, payload|
57
- (name == :insert) && payload[:documents][0][:data].inspect.include?('Binary')
69
+ @coll.operation_writer.expects(:log_operation).with do |name, payload|
70
+ (name == :insert) && payload[:documents][:data].inspect.include?('Binary')
58
71
  end
59
72
  @coll.insert({:data => data})
60
73
  end
61
74
 
62
75
  should "send safe update message" do
63
- @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
64
- @db = @conn['testing']
65
- @coll = @db.collection('books')
66
- @conn.expects(:send_message_with_safe_check).with do |op, msg, db_name, log|
76
+ @client.expects(:send_message_with_gle).with do |op, msg, db_name, log|
77
+ op == 2001
78
+ end
79
+ @coll.operation_writer.expects(:log_operation).with do |name, payload|
80
+ (name == :update) && payload[:documents][:title].include?('Moby')
81
+ end
82
+ @coll.update({}, {:title => 'Moby Dick'})
83
+ end
84
+
85
+ should "send safe update message with legacy" do
86
+ connection = Connection.new('localhost', 27017, :safe => true, :connect => false)
87
+ db = connection[TEST_DB]
88
+ coll = db.collection('collection-unit-test')
89
+ connection.expects(:send_message_with_gle).with do |op, msg, db_name, log|
67
90
  op == 2001
68
91
  end
69
- @conn.expects(:log_operation).with do |name, payload|
70
- (name == :update) && payload[:document][:title].include?('Moby')
92
+ coll.operation_writer.expects(:log_operation).with do |name, payload|
93
+ (name == :update) && payload[:documents][:title].include?('Moby')
71
94
  end
72
- @coll.update({}, {:title => 'Moby Dick'}, :safe => true)
95
+ coll.update({}, {:title => 'Moby Dick'})
73
96
  end
74
97
 
75
98
  should "send safe insert message" do
76
- @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
77
- @db = @conn['testing']
78
- @coll = @db.collection('books')
79
- @conn.expects(:send_message_with_safe_check).with do |op, msg, db_name, log|
99
+ @client.expects(:send_message_with_gle).with do |op, msg, db_name, log|
80
100
  op == 2001
81
101
  end
82
- @conn.stubs(:log_operation)
83
- @coll.update({}, {:title => 'Moby Dick'}, :safe => true)
102
+ @coll.operation_writer.stubs(:log_operation)
103
+ @coll.update({}, {:title => 'Moby Dick'})
84
104
  end
85
105
 
86
106
  should "not call insert for each ensure_index call" do
87
- @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
88
- @db = @conn['testing']
89
- @coll = @db.collection('books')
90
107
  @coll.expects(:generate_indexes).once
91
108
 
92
109
  @coll.ensure_index [["x", Mongo::DESCENDING]]
93
110
  @coll.ensure_index [["x", Mongo::DESCENDING]]
94
111
  end
95
112
 
96
- should "call generate_indexes for a new direction on the same field for ensure_index" do
97
- @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
98
- @db = @conn['testing']
99
- @coll = @db.collection('books')
113
+ should "call generate_indexes for a new type on the same field for ensure_index" do
100
114
  @coll.expects(:generate_indexes).twice
101
115
 
102
116
  @coll.ensure_index [["x", Mongo::DESCENDING]]
103
117
  @coll.ensure_index [["x", Mongo::ASCENDING]]
104
-
105
118
  end
106
119
 
107
120
  should "call generate_indexes twice because the cache time is 0 seconds" do
108
- @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
109
- @db = @conn['testing']
110
121
  @db.cache_time = 0
111
- @coll = @db.collection('books')
122
+ @coll = @db.collection('collection-unit-test')
112
123
  @coll.expects(:generate_indexes).twice
113
124
 
114
125
  @coll.ensure_index [["x", Mongo::DESCENDING]]
@@ -116,15 +127,40 @@ class CollectionTest < Test::Unit::TestCase
116
127
  end
117
128
 
118
129
  should "call generate_indexes for each key when calling ensure_indexes" do
119
- @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
120
- @db = @conn['testing']
121
130
  @db.cache_time = 300
122
- @coll = @db.collection('books')
131
+ @coll = @db.collection('collection-unit-test')
123
132
  @coll.expects(:generate_indexes).once.with do |a, b, c|
124
133
  a == {"x"=>-1, "y"=>-1}
125
134
  end
126
135
 
127
136
  @coll.ensure_index [["x", Mongo::DESCENDING], ["y", Mongo::DESCENDING]]
128
137
  end
138
+
139
+ should "call generate_indexes for each key when calling ensure_indexes with a hash" do
140
+ @db.cache_time = 300
141
+ @coll = @db.collection('collection-unit-test')
142
+ oh = BSON::OrderedHash.new
143
+ oh['x'] = Mongo::DESCENDING
144
+ oh['y'] = Mongo::DESCENDING
145
+ @coll.expects(:generate_indexes).once.with do |a, b, c|
146
+ a == oh
147
+ end
148
+
149
+ if RUBY_VERSION > '1.9'
150
+ @coll.ensure_index({"x" => Mongo::DESCENDING, "y" => Mongo::DESCENDING})
151
+ else
152
+ ordered_hash = BSON::OrderedHash.new
153
+ ordered_hash['x'] = Mongo::DESCENDING
154
+ ordered_hash['y'] = Mongo::DESCENDING
155
+ @coll.ensure_index(ordered_hash)
156
+ end
157
+ end
158
+
159
+ should "use the connection's logger" do
160
+ @logger.expects(:warn).with do |msg|
161
+ msg == "MONGODB [WARNING] test warning"
162
+ end
163
+ @coll.log(:warn, "test warning")
164
+ end
129
165
  end
130
166
  end
@@ -1,83 +1,333 @@
1
- require './test/test_helper'
2
- include Mongo
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
3
14
 
4
- class ConnectionTest < Test::Unit::TestCase
5
- context "Initialization: " do
15
+ require 'test_helper'
16
+
17
+ class ConnectionUnitTest < Test::Unit::TestCase
18
+ context "Mongo::MongoClient initialization " do
6
19
  context "given a single node" do
7
20
  setup do
8
- @conn = Connection.new('localhost', 27017, :connect => false)
21
+ @connection = Mongo::Connection.new('localhost', 27017, :safe => true, :connect => false)
9
22
  TCPSocket.stubs(:new).returns(new_mock_socket)
10
23
 
11
24
  admin_db = new_mock_db
12
- admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1}).twice
13
- @conn.expects(:[]).with('admin').returns(admin_db).twice
14
- @conn.connect
25
+ admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
26
+ @connection.expects(:[]).with('admin').returns(admin_db)
27
+ @connection.connect
28
+ end
29
+
30
+ should "set safe mode true" do
31
+ assert_equal true, @connection.safe
15
32
  end
16
33
 
17
34
  should "set localhost and port to master" do
18
- assert_equal 'localhost', @conn.primary_pool.host
19
- assert_equal 27017, @conn.primary_pool.port
35
+ assert_equal 'localhost', @connection.primary_pool.host
36
+ assert_equal 27017, @connection.primary_pool.port
20
37
  end
21
38
 
22
39
  should "set connection pool to 1" do
23
- assert_equal 1, @conn.primary_pool.size
40
+ assert_equal 1, @connection.primary_pool.size
24
41
  end
25
42
 
26
43
  should "default slave_ok to false" do
27
- assert !@conn.slave_ok?
44
+ assert !@connection.slave_ok?
45
+ end
46
+
47
+ should "not raise error if no host or port is supplied" do
48
+ assert_nothing_raised do
49
+ Mongo::Connection.new(:safe => true, :connect => false)
50
+ end
51
+ assert_nothing_raised do
52
+ Mongo::Connection.new('localhost', :safe => true, :connect => false)
53
+ end
54
+ end
55
+
56
+ should "warn if invalid options are specified" do
57
+ connection = Mongo::Connection.allocate
58
+ opts = {:connect => false}
59
+
60
+ Mongo::ReplSetConnection::REPL_SET_OPTS.each do |opt|
61
+ connection.expects(:warn).with("#{opt} is not a valid option for #{connection.class}")
62
+ opts[opt] = true
63
+ end
64
+
65
+ args = ['localhost', 27017, opts]
66
+ connection.send(:initialize, *args)
67
+ end
68
+
69
+ end
70
+
71
+ context "initializing with a unix socket" do
72
+ setup do
73
+ @connection = Mongo::Connection.new('/tmp/mongod.sock', :safe => true, :connect => false)
74
+ UNIXSocket.stubs(:new).returns(new_mock_unix_socket)
75
+ end
76
+ should "parse a unix socket" do
77
+ assert_equal "/tmp/mongod.sock", @connection.host_port.first
78
+ end
79
+ end
80
+
81
+ context "initializing with a unix socket in uri" do
82
+ setup do
83
+ @connection = Mongo::Connection.from_uri("mongodb:///tmp/mongod.sock", :connect => false)
84
+ UNIXSocket.stubs(:new).returns(new_mock_unix_socket)
85
+ end
86
+ should "parse a unix socket" do
87
+ assert_equal "/tmp/mongod.sock", @connection.host_port.first
28
88
  end
29
89
  end
30
90
 
31
91
  context "initializing with a mongodb uri" do
32
92
  should "parse a simple uri" do
33
- @conn = Connection.from_uri("mongodb://localhost", :connect => false)
34
- assert_equal ['localhost', 27017], @conn.host_to_try
93
+ @connection = Mongo::Connection.from_uri("mongodb://localhost", :connect => false)
94
+ assert_equal ['localhost', 27017], @connection.host_port
95
+ end
96
+
97
+ should "set auth source" do
98
+ @connection = Mongo::Connection.from_uri("mongodb://user:pass@localhost?authSource=foo", :connect => false)
99
+ assert_equal 'foo', @connection.auths.first[:source]
100
+ end
101
+
102
+ should "set auth mechanism" do
103
+ @connection = Mongo::Connection.from_uri("mongodb://user@localhost?authMechanism=MONGODB-X509", :connect => false)
104
+ assert_equal 'MONGODB-X509', @connection.auths.first[:mechanism]
105
+
106
+ assert_raise MongoArgumentError do
107
+ Mongo::Connection.from_uri("mongodb://localhost?authMechanism=INVALID", :connect => false)
108
+ end
35
109
  end
36
110
 
37
111
  should "allow a complex host names" do
38
112
  host_name = "foo.bar-12345.org"
39
- @conn = Connection.from_uri("mongodb://#{host_name}", :connect => false)
40
- assert_equal [host_name, 27017], @conn.host_to_try
113
+ @connection = Mongo::Connection.from_uri("mongodb://#{host_name}", :connect => false)
114
+ assert_equal [host_name, 27017], @connection.host_port
115
+ end
116
+
117
+ should "allow db without username and password" do
118
+ host_name = "foo.bar-12345.org"
119
+ @connection = Mongo::Connection.from_uri("mongodb://#{host_name}/foo", :connect => false)
120
+ assert_equal [host_name, 27017], @connection.host_port
121
+ end
122
+
123
+ should "set safe options on connection" do
124
+ host_name = "localhost"
125
+ opts = "safe=true&w=2&wtimeoutMS=1000&fsync=true&journal=true"
126
+ @connection = Mongo::Connection.from_uri("mongodb://#{host_name}/foo?#{opts}", :connect => false)
127
+ assert_equal({:w => 2, :wtimeout => 1000, :fsync => true, :j => true}, @connection.write_concern)
128
+ end
129
+
130
+ should "set timeout options on connection" do
131
+ host_name = "localhost"
132
+ opts = "connectTimeoutMS=1000&socketTimeoutMS=5000"
133
+ @connection = Mongo::Connection.from_uri("mongodb://#{host_name}/foo?#{opts}", :connect => false)
134
+ assert_equal 1, @connection.connect_timeout
135
+ assert_equal 5, @connection.op_timeout
41
136
  end
42
137
 
43
138
  should "parse a uri with a hyphen & underscore in the username or password" do
44
- @conn = Connection.from_uri("mongodb://hyphen-user_name:p-s_s@localhost:27017/db", :connect => false)
45
- assert_equal ['localhost', 27017], @conn.host_to_try
46
- auth_hash = { 'db_name' => 'db', 'username' => 'hyphen-user_name', "password" => 'p-s_s' }
47
- assert_equal auth_hash, @conn.auths[0]
139
+ @connection = Mongo::Connection.from_uri("mongodb://hyphen-user_name:p-s_s@localhost:27017/db", :connect => false)
140
+ assert_equal ['localhost', 27017], @connection.host_port
141
+
142
+ auth_hash = {
143
+ :db_name => 'db',
144
+ :extra=>{},
145
+ :username => 'hyphen-user_name',
146
+ :password => 'p-s_s',
147
+ :source => 'db',
148
+ :mechanism => nil
149
+ }
150
+ assert_equal auth_hash, @connection.auths.first
48
151
  end
49
152
 
50
153
  should "attempt to connect" do
51
154
  TCPSocket.stubs(:new).returns(new_mock_socket)
52
- @conn = Connection.from_uri("mongodb://localhost", :connect => false)
155
+ @connection = Mongo::Connection.from_uri("mongodb://localhost", :connect => false)
53
156
 
54
157
  admin_db = new_mock_db
55
- admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1}).twice
56
- @conn.expects(:[]).with('admin').returns(admin_db).twice
57
- @conn.connect
158
+ admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
159
+ @connection.expects(:[]).with('admin').returns(admin_db)
160
+ @connection.connect
58
161
  end
59
162
 
60
163
  should "raise an error on invalid uris" do
61
164
  assert_raise MongoArgumentError do
62
- Connection.from_uri("mongo://localhost", :connect => false)
165
+ Mongo::Connection.from_uri("mongo://localhost", :connect => false)
63
166
  end
64
167
 
65
168
  assert_raise MongoArgumentError do
66
- Connection.from_uri("mongodb://localhost:abc", :connect => false)
169
+ Mongo::Connection.from_uri("mongodb://localhost:abc", :connect => false)
67
170
  end
171
+ end
172
+
173
+ should "require password if using legacy auth and username present" do
174
+ assert Mongo::Connection.from_uri("mongodb://kyle:jones@localhost/db", :connect => false)
68
175
 
69
176
  assert_raise MongoArgumentError do
70
- Connection.from_uri("mongodb://localhost:27017, my.db.com:27018, ", :connect => false)
177
+ Mongo::Connection.from_uri("mongodb://kyle:@localhost", :connect => false)
71
178
  end
72
- end
73
179
 
74
- should "require all of username, password, and database if any one is specified" do
75
180
  assert_raise MongoArgumentError do
76
- Connection.from_uri("mongodb://localhost/db", :connect => false)
181
+ Mongo::Connection.from_uri("mongodb://kyle@localhost", :connect => false)
77
182
  end
183
+ end
184
+ end
78
185
 
79
- assert_raise MongoArgumentError do
80
- Connection.from_uri("mongodb://kyle:password@localhost", :connect => false)
186
+ context "initializing with ENV['MONGODB_URI']" do
187
+ should "parse a simple uri" do
188
+ uri = "mongodb://localhost?connect=false"
189
+ with_preserved_env_uri(uri) do
190
+ @connection = Mongo::Connection.new
191
+ assert_equal ['localhost', 27017], @connection.host_port
192
+ end
193
+ end
194
+
195
+ should "set auth source" do
196
+ uri = "mongodb://user:pass@localhost?authSource=foo&connect=false"
197
+ with_preserved_env_uri(uri) do
198
+ @connection = Mongo::Connection.new
199
+ assert_equal 'foo', @connection.auths.first[:source]
200
+ end
201
+ end
202
+
203
+ should "set auth mechanism" do
204
+ uri = "mongodb://user@localhost?authMechanism=MONGODB-X509&connect=false"
205
+ with_preserved_env_uri(uri) do
206
+ @connection = Mongo::Connection.new
207
+ assert_equal 'MONGODB-X509', @connection.auths.first[:mechanism]
208
+
209
+ ENV['MONGODB_URI'] = "mongodb://user@localhost?authMechanism=INVALID&connect=false"
210
+ assert_raise MongoArgumentError do
211
+ Mongo::Connection.new
212
+ end
213
+ end
214
+ end
215
+
216
+ should "allow a complex host names" do
217
+ host_name = "foo.bar-12345.org"
218
+ uri = "mongodb://#{host_name}?connect=false"
219
+ with_preserved_env_uri(uri) do
220
+ @connection = Mongo::Connection.new
221
+ assert_equal [host_name, 27017], @connection.host_port
222
+ end
223
+ end
224
+
225
+ should "allow db without username and password" do
226
+ host_name = "foo.bar-12345.org"
227
+ uri = "mongodb://#{host_name}/foo?connect=false"
228
+ with_preserved_env_uri(uri) do
229
+ @connection = Mongo::Connection.new
230
+ assert_equal [host_name, 27017], @connection.host_port
231
+ end
232
+ end
233
+
234
+ should "set safe options on connection" do
235
+ host_name = "localhost"
236
+ opts = "safe=true&w=2&wtimeoutMS=1000&fsync=true&journal=true&connect=false"
237
+ uri = "mongodb://#{host_name}/foo?#{opts}"
238
+ with_preserved_env_uri(uri) do
239
+ @connection = Mongo::Connection.new
240
+ assert_equal({:w => 2, :wtimeout => 1000, :fsync => true, :j => true}, @connection.safe)
241
+ end
242
+ end
243
+
244
+ should "set timeout options on connection" do
245
+ host_name = "localhost"
246
+ opts = "connectTimeoutMS=1000&socketTimeoutMS=5000&connect=false"
247
+ uri = "mongodb://#{host_name}/foo?#{opts}"
248
+ with_preserved_env_uri(uri) do
249
+ @connection = Mongo::Connection.new
250
+ assert_equal 1, @connection.connect_timeout
251
+ assert_equal 5, @connection.op_timeout
252
+ end
253
+ end
254
+
255
+ should "parse a uri with a hyphen & underscore in the username or password" do
256
+ uri = "mongodb://hyphen-user_name:p-s_s@localhost:27017/db?connect=false"
257
+ with_preserved_env_uri(uri) do
258
+ @connection = Mongo::Connection.new
259
+ assert_equal ['localhost', 27017], @connection.host_port
260
+
261
+ auth_hash = {
262
+ :db_name => 'db',
263
+ :extra=>{},
264
+ :username => 'hyphen-user_name',
265
+ :password => 'p-s_s',
266
+ :source => 'db',
267
+ :mechanism => nil
268
+ }
269
+ assert_equal auth_hash, @connection.auths.first
270
+ end
271
+ end
272
+
273
+ should "attempt to connect" do
274
+ TCPSocket.stubs(:new).returns(new_mock_socket)
275
+ uri = "mongodb://localhost?connect=false"
276
+ with_preserved_env_uri(uri) do
277
+ @connection = Mongo::Connection.new
278
+
279
+ admin_db = new_mock_db
280
+ admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
281
+ @connection.expects(:[]).with('admin').returns(admin_db)
282
+ @connection.connect
283
+ end
284
+ end
285
+
286
+ should "raise an error on invalid uris" do
287
+ uri = "mongo://localhost"
288
+ with_preserved_env_uri(uri) do
289
+ assert_raise MongoArgumentError do
290
+ Mongo::Connection.new
291
+ end
292
+
293
+ ENV['MONGODB_URI'] = "mongodb://localhost:abc"
294
+ assert_raise MongoArgumentError do
295
+ Mongo::Connection.new
296
+ end
297
+ end
298
+ end
299
+
300
+ should "require password if using legacy auth and username present" do
301
+ uri = "mongodb://kyle:jones@localhost/db?connect=false"
302
+ with_preserved_env_uri(uri) do
303
+ assert Mongo::Connection.new
304
+
305
+ ENV['MONGODB_URI'] = "mongodb://kyle:@localhost?connect=false"
306
+ assert_raise MongoArgumentError do
307
+ Mongo::Connection.new
308
+ end
309
+
310
+ ENV['MONGODB_URI'] = "mongodb://kyle@localhost?connect=false"
311
+ assert_raise MongoArgumentError do
312
+ Mongo::Connection.new
313
+ end
314
+ end
315
+ end
316
+
317
+ should "require password if using PLAIN auth and username present" do
318
+ uri = "mongodb://kyle:jones@localhost/db?connect=false&authMechanism=PLAIN"
319
+ with_preserved_env_uri(uri) do
320
+ assert Mongo::Connection.new
321
+
322
+ ENV['MONGODB_URI'] = "mongodb://kyle:@localhost?connect=false&authMechanism=PLAIN"
323
+ assert_raise MongoArgumentError do
324
+ Mongo::Connection.new
325
+ end
326
+
327
+ ENV['MONGODB_URI'] = "mongodb://kyle@localhost?connect=false&authMechanism=PLAIN"
328
+ assert_raise MongoArgumentError do
329
+ Mongo::Connection.new
330
+ end
81
331
  end
82
332
  end
83
333
  end