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
@@ -0,0 +1,160 @@
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
+ class MongoConfig < Test::Unit::TestCase
18
+
19
+ def startup
20
+ @sys_proc = nil
21
+ end
22
+
23
+ def shutdown
24
+ @sys_proc.stop if @sys_proc && @sys_proc.running?
25
+ end
26
+
27
+ test "config defaults" do
28
+ [ Mongo::Config::DEFAULT_BASE_OPTS,
29
+ Mongo::Config::DEFAULT_REPLICA_SET,
30
+ Mongo::Config::DEFAULT_SHARDED_SIMPLE,
31
+ Mongo::Config::DEFAULT_SHARDED_REPLICA
32
+ ].each do |params|
33
+ config = Mongo::Config.cluster(params)
34
+ assert(config.size > 0)
35
+ end
36
+ end
37
+
38
+ test "get available port" do
39
+ assert_not_nil(Mongo::Config.get_available_port)
40
+ end
41
+
42
+ test "SysProc start" do
43
+ cmd = "true"
44
+ @sys_proc = Mongo::Config::SysProc.new(cmd)
45
+ assert_equal(cmd, @sys_proc.cmd)
46
+ assert_nil(@sys_proc.pid)
47
+ start_and_assert_running?(@sys_proc)
48
+ end
49
+
50
+ test "SysProc wait" do
51
+ @sys_proc = Mongo::Config::SysProc.new("true")
52
+ start_and_assert_running?(@sys_proc)
53
+ assert(@sys_proc.running?)
54
+ @sys_proc.wait
55
+ assert(!@sys_proc.running?)
56
+ end
57
+
58
+ test "SysProc kill" do
59
+ @sys_proc = Mongo::Config::SysProc.new("true")
60
+ start_and_assert_running?(@sys_proc)
61
+ @sys_proc.kill
62
+ @sys_proc.wait
63
+ assert(!@sys_proc.running?)
64
+ end
65
+
66
+ test "SysProc stop" do
67
+ @sys_proc = Mongo::Config::SysProc.new("true")
68
+ start_and_assert_running?(@sys_proc)
69
+ @sys_proc.stop
70
+ assert(!@sys_proc.running?)
71
+ end
72
+
73
+ test "SysProc zombie respawn" do
74
+ @sys_proc = Mongo::Config::SysProc.new("true")
75
+ start_and_assert_running?(@sys_proc)
76
+ prev_pid = @sys_proc.pid
77
+ @sys_proc.kill
78
+ # don't wait, leaving a zombie
79
+ assert(@sys_proc.running?)
80
+ start_and_assert_running?(@sys_proc)
81
+ assert(prev_pid && @sys_proc.pid && prev_pid != @sys_proc.pid, 'SysProc#start should spawn a new process after a zombie')
82
+ @sys_proc.stop
83
+ assert(!@sys_proc.running?)
84
+ end
85
+
86
+ test "Server" do
87
+ server = Mongo::Config::Server.new('a cmd', 'host', 1234)
88
+ assert_equal('a cmd', server.cmd)
89
+ assert_equal('host', server.host)
90
+ assert_equal(1234, server.port)
91
+ end
92
+
93
+ test "DbServer" do
94
+ config = Mongo::Config::DEFAULT_BASE_OPTS
95
+ server = Mongo::Config::DbServer.new(config)
96
+ assert_equal(config, server.config)
97
+ assert_equal("mongod --dbpath data --logpath data/log", server.cmd)
98
+ assert_equal(config[:host], server.host)
99
+ assert_equal(config[:port], server.port)
100
+ end
101
+
102
+ def cluster_test(opts)
103
+ #debug 1, opts.inspect
104
+ config = Mongo::Config.cluster(opts)
105
+ #debug 1, config.inspect
106
+ manager = Mongo::Config::ClusterManager.new(config)
107
+ assert_equal(config, manager.config)
108
+ manager.start
109
+ yield manager
110
+ manager.stop
111
+ manager.servers.each{|s| assert(!s.running?)}
112
+ manager.clobber
113
+ end
114
+
115
+ test "cluster manager base" do
116
+ cluster_test(Mongo::Config::DEFAULT_BASE_OPTS) do |manager|
117
+
118
+ end
119
+ end
120
+
121
+ test "cluster manager replica set" do
122
+ cluster_test(Mongo::Config::DEFAULT_REPLICA_SET) do |manager|
123
+ servers = manager.servers
124
+ servers.each do |server|
125
+ assert_not_nil(Mongo::MongoClient.new(server.host, server.port))
126
+ assert_match(/oplogSize/, server.cmd, '--oplogSize option should be specified')
127
+ assert_match(/smallfiles/, server.cmd, '--smallfiles option should be specified')
128
+ assert_no_match(/nojournal/, server.cmd, '--nojournal option should not be specified')
129
+ assert_match(/noprealloc/, server.cmd, '--noprealloc option should be specified')
130
+ end
131
+ end
132
+ end
133
+
134
+ test "cluster manager sharded simple" do
135
+ cluster_test(Mongo::Config::DEFAULT_SHARDED_SIMPLE) do |manager|
136
+ servers = manager.shards + manager.configs
137
+ servers.each do |server|
138
+ assert_not_nil(Mongo::MongoClient.new(server.host, server.port))
139
+ assert_match(/oplogSize/, server.cmd, '--oplogSize option should be specified')
140
+ assert_match(/smallfiles/, server.cmd, '--smallfiles option should be specified')
141
+ assert_no_match(/nojournal/, server.cmd, '--nojournal option should not be specified')
142
+ assert_match(/noprealloc/, server.cmd, '--noprealloc option should be specified')
143
+ end
144
+ end
145
+ end
146
+
147
+ test "cluster manager sharded replica" do
148
+ #cluster_test(Mongo::Config::DEFAULT_SHARDED_REPLICA) # not yet supported by ClusterManager
149
+ end
150
+
151
+ private
152
+
153
+ def start_and_assert_running?(sys_proc)
154
+ assert_not_nil(sys_proc.start(0))
155
+ assert_not_nil(sys_proc.pid)
156
+ assert(sys_proc.running?)
157
+ end
158
+
159
+ end
160
+
@@ -0,0 +1,381 @@
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
+ include Mongo
17
+
18
+ class ClientUnitTest < Test::Unit::TestCase
19
+ context "Mongo::MongoClient initialization " do
20
+ context "given a single node" do
21
+ setup do
22
+ @client = MongoClient.new('localhost', 27017, :connect => false)
23
+ TCPSocket.stubs(:new).returns(new_mock_socket)
24
+
25
+ admin_db = new_mock_db
26
+ admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
27
+ @client.expects(:[]).with('admin').returns(admin_db)
28
+ @client.connect
29
+ end
30
+
31
+ should "gle writes by default" do
32
+ assert_equal 1, @client.write_concern[:w]
33
+ end
34
+
35
+ should "set localhost and port to master" do
36
+ assert_equal 'localhost', @client.primary_pool.host
37
+ assert_equal 27017, @client.primary_pool.port
38
+ end
39
+
40
+ should "set connection pool to 1" do
41
+ assert_equal 1, @client.primary_pool.size
42
+ end
43
+
44
+ should "set op timeout to default" do
45
+ assert_equal Mongo::MongoClient::DEFAULT_OP_TIMEOUT, @client.op_timeout
46
+ end
47
+
48
+ should "default slave_ok to false" do
49
+ assert !@client.slave_ok?
50
+ end
51
+
52
+ should "not raise error if no host or port is supplied" do
53
+ assert_nothing_raised do
54
+ MongoClient.new(:w => 1, :connect => false)
55
+ end
56
+ assert_nothing_raised do
57
+ MongoClient.new('localhost', :w => 1, :connect=> false)
58
+ end
59
+ end
60
+
61
+ should "warn if invalid options are specified" do
62
+ client = MongoClient.allocate
63
+ opts = {:connect => false}
64
+
65
+ MongoReplicaSetClient::REPL_SET_OPTS.each do |opt|
66
+ client.expects(:warn).with("#{opt} is not a valid option for #{client.class}")
67
+ opts[opt] = true
68
+ end
69
+
70
+ args = ['localhost', 27017, opts]
71
+ client.send(:initialize, *args)
72
+ end
73
+
74
+ context 'specifying nil op timeout explicitly' do
75
+ setup do
76
+ @client = MongoClient.new('localhost', 27017, :connect => false, :op_timeout => nil)
77
+ end
78
+
79
+ should 'set op timeout to nil' do
80
+ assert_equal nil, @client.op_timeout
81
+ end
82
+ end
83
+
84
+ context 'specifying a different op timeout than default' do
85
+ setup do
86
+ @client = MongoClient.new('localhost', 27017, :connect => false, :op_timeout => 50)
87
+ end
88
+
89
+ should 'set op timeout to the specified value' do
90
+ assert_equal 50, @client.op_timeout
91
+ end
92
+ end
93
+
94
+ context "given a replica set" do
95
+
96
+ should "warn if invalid options are specified" do
97
+ client = MongoReplicaSetClient.allocate
98
+ opts = {:connect => false}
99
+
100
+ MongoClient::CLIENT_ONLY_OPTS.each do |opt|
101
+ client.expects(:warn).with("#{opt} is not a valid option for #{client.class}")
102
+ opts[opt] = true
103
+ end
104
+
105
+ args = [['localhost:27017'], opts]
106
+ client.send(:initialize, *args)
107
+ end
108
+
109
+ should "throw error if superflous arguments are specified" do
110
+ assert_raise MongoArgumentError do
111
+ MongoReplicaSetClient.new(['localhost:27017'], ['localhost:27018'], {:connect => false})
112
+ end
113
+ end
114
+ end
115
+ end
116
+
117
+ context "initializing with a unix socket" do
118
+ setup do
119
+ @client = MongoClient.new('/tmp/mongod.sock', :connect => false)
120
+ UNIXSocket.stubs(:new).returns(new_mock_unix_socket)
121
+ end
122
+ should "parse a unix socket" do
123
+ assert_equal "/tmp/mongod.sock", @client.host_port.first
124
+ end
125
+ end
126
+
127
+ context "initializing with a unix socket in uri" do
128
+ setup do
129
+ @client = MongoClient.from_uri("mongodb:///tmp/mongod.sock", :connect => false)
130
+ UNIXSocket.stubs(:new).returns(new_mock_unix_socket)
131
+ end
132
+ should "parse a unix socket" do
133
+ assert_equal "/tmp/mongod.sock", @client.host_port.first
134
+ end
135
+ end
136
+
137
+ context "initializing with a mongodb uri" do
138
+ should "parse a simple uri" do
139
+ @client = MongoClient.from_uri("mongodb://localhost", :connect => false)
140
+ assert_equal ['localhost', 27017], @client.host_port
141
+ end
142
+
143
+ should "set auth source" do
144
+ @client = MongoClient.from_uri("mongodb://user:pass@localhost?authSource=foo", :connect => false)
145
+ assert_equal 'foo', @client.auths.first[:source]
146
+ end
147
+
148
+ should "set auth mechanism" do
149
+ @client = MongoClient.from_uri("mongodb://user@localhost?authMechanism=MONGODB-X509", :connect => false)
150
+ assert_equal 'MONGODB-X509', @client.auths.first[:mechanism]
151
+
152
+ assert_raise MongoArgumentError do
153
+ MongoClient.from_uri("mongodb://user@localhost?authMechanism=INVALID", :connect => false)
154
+ end
155
+ end
156
+
157
+ should "allow a complex host names" do
158
+ host_name = "foo.bar-12345.org"
159
+ @client = MongoClient.from_uri("mongodb://#{host_name}", :connect => false)
160
+ assert_equal [host_name, 27017], @client.host_port
161
+ end
162
+
163
+ should "allow db without username and password" do
164
+ host_name = "foo.bar-12345.org"
165
+ @client = MongoClient.from_uri("mongodb://#{host_name}/foo", :connect => false)
166
+ assert_equal [host_name, 27017], @client.host_port
167
+ end
168
+
169
+ should "set write concern options on connection" do
170
+ host_name = "localhost"
171
+ opts = "w=2&wtimeoutMS=1000&fsync=true&journal=true"
172
+ @client = MongoClient.from_uri("mongodb://#{host_name}/foo?#{opts}", :connect => false)
173
+ assert_equal({:w => 2, :wtimeout => 1000, :fsync => true, :j => true}, @client.write_concern)
174
+ end
175
+
176
+ should "set timeout options on connection" do
177
+ host_name = "localhost"
178
+ opts = "connectTimeoutMS=1000&socketTimeoutMS=5000"
179
+ @client = MongoClient.from_uri("mongodb://#{host_name}/foo?#{opts}", :connect => false)
180
+ assert_equal 1, @client.connect_timeout
181
+ assert_equal 5, @client.op_timeout
182
+ end
183
+
184
+ should "parse a uri with a hyphen & underscore in the username or password" do
185
+ @client = MongoClient.from_uri("mongodb://hyphen-user_name:p-s_s@localhost:27017/db", :connect => false)
186
+ assert_equal ['localhost', 27017], @client.host_port
187
+
188
+ auth_hash = {
189
+ :db_name => 'db',
190
+ :extra=>{},
191
+ :username => 'hyphen-user_name',
192
+ :password => 'p-s_s',
193
+ :source => 'db',
194
+ :mechanism => nil
195
+ }
196
+ assert_equal auth_hash, @client.auths.first
197
+ end
198
+
199
+ should "attempt to connect" do
200
+ TCPSocket.stubs(:new).returns(new_mock_socket)
201
+ @client = MongoClient.from_uri("mongodb://localhost", :connect => false)
202
+
203
+ admin_db = new_mock_db
204
+ admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
205
+ @client.expects(:[]).with('admin').returns(admin_db)
206
+ @client.connect
207
+ end
208
+
209
+ should "raise an error on invalid uris" do
210
+ assert_raise MongoArgumentError do
211
+ MongoClient.from_uri("mongo://localhost", :connect => false)
212
+ end
213
+
214
+ assert_raise MongoArgumentError do
215
+ MongoClient.from_uri("mongodb://localhost:abc", :connect => false)
216
+ end
217
+ end
218
+
219
+ should "require password if using legacy auth and username present" do
220
+ assert MongoClient.from_uri("mongodb://kyle:jones@localhost/db", :connect => false)
221
+
222
+ assert_raise MongoArgumentError do
223
+ MongoClient.from_uri("mongodb://kyle:@localhost", :connect => false)
224
+ end
225
+
226
+ assert_raise MongoArgumentError do
227
+ MongoClient.from_uri("mongodb://kyle@localhost", :connect => false)
228
+ end
229
+ end
230
+ end
231
+
232
+ context "initializing with ENV['MONGODB_URI']" do
233
+ should "parse a simple uri" do
234
+ uri = "mongodb://localhost?connect=false"
235
+ with_preserved_env_uri(uri) do
236
+ @client = MongoClient.new
237
+ assert_equal ['localhost', 27017], @client.host_port
238
+ end
239
+ end
240
+
241
+ should "set auth source" do
242
+ uri = "mongodb://user:pass@localhost?authSource=foo&connect=false"
243
+ with_preserved_env_uri(uri) do
244
+ @client = MongoClient.new
245
+ assert_equal 'foo', @client.auths.first[:source]
246
+ end
247
+ end
248
+
249
+ should "set auth mechanism" do
250
+ uri = "mongodb://user@localhost?authMechanism=MONGODB-X509&connect=false"
251
+ with_preserved_env_uri(uri) do
252
+ @client = MongoClient.new
253
+ assert_equal 'MONGODB-X509', @client.auths.first[:mechanism]
254
+
255
+ ENV['MONGODB_URI'] = "mongodb://user@localhost?authMechanism=INVALID&connect=false"
256
+ assert_raise MongoArgumentError do
257
+ MongoClient.new
258
+ end
259
+ end
260
+ end
261
+
262
+ should "allow a complex host names" do
263
+ host_name = "foo.bar-12345.org"
264
+ uri = "mongodb://#{host_name}?connect=false"
265
+ with_preserved_env_uri(uri) do
266
+ @client = MongoClient.new
267
+ assert_equal [host_name, 27017], @client.host_port
268
+ end
269
+ end
270
+
271
+ should "allow db without username and password" do
272
+ host_name = "foo.bar-12345.org"
273
+ uri = "mongodb://#{host_name}/foo?connect=false"
274
+ with_preserved_env_uri(uri) do
275
+ @client = MongoClient.new
276
+ assert_equal [host_name, 27017], @client.host_port
277
+ end
278
+ end
279
+
280
+ should "set write concern options on connection" do
281
+ host_name = "localhost"
282
+ opts = "w=2&wtimeoutMS=1000&fsync=true&journal=true&connect=false"
283
+ uri = "mongodb://#{host_name}/foo?#{opts}"
284
+ with_preserved_env_uri(uri) do
285
+ @client = MongoClient.new
286
+ assert_equal({:w => 2, :wtimeout => 1000, :fsync => true, :j => true}, @client.write_concern)
287
+ end
288
+ end
289
+
290
+ should "set timeout options on connection" do
291
+ host_name = "localhost"
292
+ opts = "connectTimeoutMS=1000&socketTimeoutMS=5000&connect=false"
293
+ uri = "mongodb://#{host_name}/foo?#{opts}"
294
+ with_preserved_env_uri(uri) do
295
+ @client = MongoClient.new
296
+ assert_equal 1, @client.connect_timeout
297
+ assert_equal 5, @client.op_timeout
298
+ end
299
+ end
300
+
301
+ should "parse a uri with a hyphen & underscore in the username or password" do
302
+ uri = "mongodb://hyphen-user_name:p-s_s@localhost:27017/db?connect=false"
303
+ with_preserved_env_uri(uri) do
304
+ @client = MongoClient.new
305
+ assert_equal ['localhost', 27017], @client.host_port
306
+
307
+ auth_hash = {
308
+ :db_name => 'db',
309
+ :extra=>{},
310
+ :username => 'hyphen-user_name',
311
+ :password => 'p-s_s',
312
+ :source => 'db',
313
+ :mechanism => nil
314
+ }
315
+ assert_equal auth_hash, @client.auths.first
316
+ end
317
+ end
318
+
319
+ should "attempt to connect" do
320
+ TCPSocket.stubs(:new).returns(new_mock_socket)
321
+ uri = "mongodb://localhost?connect=false"
322
+ with_preserved_env_uri(uri) do
323
+ @client = MongoClient.new
324
+
325
+ admin_db = new_mock_db
326
+ admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
327
+ @client.expects(:[]).with('admin').returns(admin_db)
328
+ @client.connect
329
+ end
330
+ end
331
+
332
+ should "raise an error on invalid uris" do
333
+ uri = "mongo://localhost"
334
+ with_preserved_env_uri(uri) do
335
+ assert_raise MongoArgumentError do
336
+ MongoClient.new
337
+ end
338
+
339
+ ENV['MONGODB_URI'] = "mongodb://localhost:abc?connect=false"
340
+ assert_raise MongoArgumentError do
341
+ MongoClient.new
342
+ end
343
+ end
344
+ end
345
+
346
+ should "require password if using legacy auth and username present" do
347
+ uri = "mongodb://kyle:jones@localhost?connect=false"
348
+ with_preserved_env_uri(uri) do
349
+ assert MongoClient.new
350
+
351
+ ENV['MONGODB_URI'] = "mongodb://kyle:@localhost?connect=false"
352
+ assert_raise MongoArgumentError do
353
+ MongoClient.new
354
+ end
355
+
356
+ ENV['MONGODB_URI'] = "mongodb://kyle@localhost?connect=false"
357
+ assert_raise MongoArgumentError do
358
+ MongoClient.new
359
+ end
360
+ end
361
+ end
362
+
363
+ should "require password if using PLAIN auth and username present" do
364
+ uri = "mongodb://kyle:jones@localhost?connect=false&authMechanism=PLAIN"
365
+ with_preserved_env_uri(uri) do
366
+ assert MongoClient.new
367
+
368
+ ENV['MONGODB_URI'] = "mongodb://kyle:@localhost?connect=false&authMechanism=PLAIN"
369
+ assert_raise MongoArgumentError do
370
+ MongoClient.new
371
+ end
372
+
373
+ ENV['MONGODB_URI'] = "mongodb://kyle@localhost?connect=false&authMechanism=PLAIN"
374
+ assert_raise MongoArgumentError do
375
+ MongoClient.new
376
+ end
377
+ end
378
+ end
379
+ end
380
+ end
381
+ end