/
client_test.rb
252 lines (209 loc) · 8.88 KB
/
client_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
require 'test_helper'
include Mongo
class ClientTest < Test::Unit::TestCase
context "Mongo::MongoClient initialization " do
context "given a single node" do
setup do
@client = MongoClient.new('localhost', 27017, :connect => false)
TCPSocket.stubs(:new).returns(new_mock_socket)
admin_db = new_mock_db
admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
@client.expects(:[]).with('admin').returns(admin_db)
@client.connect
end
should "gle writes by default" do
assert_equal 1, @client.write_concern[:w]
end
should "set localhost and port to master" do
assert_equal 'localhost', @client.primary_pool.host
assert_equal 27017, @client.primary_pool.port
end
should "set connection pool to 1" do
assert_equal 1, @client.primary_pool.size
end
should "default slave_ok to false" do
assert !@client.slave_ok?
end
should "not raise error if no host or port is supplied" do
assert_nothing_raised do
MongoClient.new(:w => 1, :connect => false)
end
assert_nothing_raised do
MongoClient.new('localhost', :w => 1, :connect=> false)
end
end
should "warn if invalid options are specified" do
client = MongoClient.allocate
opts = {:connect => false}
MongoReplicaSetClient::REPL_SET_OPTS.each do |opt|
client.expects(:warn).with("#{opt} is not a valid option for #{client.class}")
opts[opt] = true
end
args = ['localhost', 27017, opts]
client.send(:initialize, *args)
end
context "given a replica set" do
should "warn if invalid options are specified" do
client = MongoReplicaSetClient.allocate
opts = {:connect => false}
MongoClient::CLIENT_ONLY_OPTS.each do |opt|
client.expects(:warn).with("#{opt} is not a valid option for #{client.class}")
opts[opt] = true
end
args = [['localhost:27017'], opts]
client.send(:initialize, *args)
end
should "throw error if superflous arguments are specified" do
assert_raise MongoArgumentError do
MongoReplicaSetClient.new(['localhost:27017'], ['localhost:27018'], {:connect => false})
end
end
end
end
context "initializing with a unix socket" do
setup do
@connection = Mongo::Connection.new('/tmp/mongod.sock', :safe => true, :connect => false)
UNIXSocket.stubs(:new).returns(new_mock_unix_socket)
end
should "parse a unix socket" do
assert_equal "/tmp/mongod.sock", @connection.host_port.first
end
end
context "initializing with a mongodb uri" do
should "parse a simple uri" do
@client = MongoClient.from_uri("mongodb://localhost", :connect => false)
assert_equal ['localhost', 27017], @client.host_port
end
#should "parse a unix socket" do
# socket_address = "/tmp/mongodb-27017.sock"
# @client = MongoClient.from_uri("mongodb://#{socket_address}")
# assert_equal socket_address, @client.host_port.first
#end
should "allow a complex host names" do
host_name = "foo.bar-12345.org"
@client = MongoClient.from_uri("mongodb://#{host_name}", :connect => false)
assert_equal [host_name, 27017], @client.host_port
end
should "allow db without username and password" do
host_name = "foo.bar-12345.org"
@client = MongoClient.from_uri("mongodb://#{host_name}/foo", :connect => false)
assert_equal [host_name, 27017], @client.host_port
end
should "set write concern options on connection" do
host_name = "localhost"
opts = "w=2&wtimeoutMS=1000&fsync=true&journal=true"
@client = MongoClient.from_uri("mongodb://#{host_name}/foo?#{opts}", :connect => false)
assert_equal({:w => 2, :wtimeout => 1000, :fsync => true, :j => true}, @client.write_concern)
end
should "set timeout options on connection" do
host_name = "localhost"
opts = "connectTimeoutMS=1000&socketTimeoutMS=5000"
@client = MongoClient.from_uri("mongodb://#{host_name}/foo?#{opts}", :connect => false)
assert_equal 1, @client.connect_timeout
assert_equal 5, @client.op_timeout
end
should "parse a uri with a hyphen & underscore in the username or password" do
@client = MongoClient.from_uri("mongodb://hyphen-user_name:p-s_s@localhost:27017/db", :connect => false)
assert_equal ['localhost', 27017], @client.host_port
auth_hash = { :db_name => 'db', :username => 'hyphen-user_name', :password => 'p-s_s' }
assert_equal auth_hash, @client.auths[0]
end
should "attempt to connect" do
TCPSocket.stubs(:new).returns(new_mock_socket)
@client = MongoClient.from_uri("mongodb://localhost", :connect => false)
admin_db = new_mock_db
admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
@client.expects(:[]).with('admin').returns(admin_db)
@client.connect
end
should "raise an error on invalid uris" do
assert_raise MongoArgumentError do
MongoClient.from_uri("mongo://localhost", :connect => false)
end
assert_raise MongoArgumentError do
MongoClient.from_uri("mongodb://localhost:abc", :connect => false)
end
end
should "require all of username, if password and db are specified" do
assert MongoClient.from_uri("mongodb://kyle:jones@localhost/db", :connect => false)
assert_raise MongoArgumentError do
MongoClient.from_uri("mongodb://kyle:password@localhost", :connect => false)
end
end
end
context "initializing with ENV['MONGODB_URI']" do
setup do
@old_mongodb_uri = ENV['MONGODB_URI']
end
teardown do
ENV['MONGODB_URI'] = @old_mongodb_uri
end
should "parse a simple uri" do
ENV['MONGODB_URI'] = "mongodb://localhost?connect=false"
@client = MongoClient.new
assert_equal ['localhost', 27017], @client.host_port
end
should "allow a complex host names" do
host_name = "foo.bar-12345.org"
ENV['MONGODB_URI'] = "mongodb://#{host_name}?connect=false"
@client = MongoClient.new
assert_equal [host_name, 27017], @client.host_port
end
should "allow db without username and password" do
host_name = "foo.bar-12345.org"
ENV['MONGODB_URI'] = "mongodb://#{host_name}/foo?connect=false"
@client = MongoClient.new
assert_equal [host_name, 27017], @client.host_port
end
should "set write concern options on connection" do
host_name = "localhost"
opts = "w=2&wtimeoutMS=1000&fsync=true&journal=true&connect=false"
ENV['MONGODB_URI'] = "mongodb://#{host_name}/foo?#{opts}"
@client = MongoClient.new
assert_equal({:w => 2, :wtimeout => 1000, :fsync => true, :j => true}, @client.write_concern)
end
should "set timeout options on connection" do
host_name = "localhost"
opts = "connectTimeoutMS=1000&socketTimeoutMS=5000&connect=false"
ENV['MONGODB_URI'] = "mongodb://#{host_name}/foo?#{opts}"
@client = MongoClient.new
assert_equal 1, @client.connect_timeout
assert_equal 5, @client.op_timeout
end
should "parse a uri with a hyphen & underscore in the username or password" do
ENV['MONGODB_URI'] = "mongodb://hyphen-user_name:p-s_s@localhost:27017/db?connect=false"
@client = MongoClient.new
assert_equal ['localhost', 27017], @client.host_port
auth_hash = { :db_name => 'db', :username => 'hyphen-user_name', :password => 'p-s_s' }
assert_equal auth_hash, @client.auths[0]
end
should "attempt to connect" do
TCPSocket.stubs(:new).returns(new_mock_socket)
ENV['MONGODB_URI'] = "mongodb://localhost?connect=false" # connect=false ??
@client = MongoClient.new
admin_db = new_mock_db
admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
@client.expects(:[]).with('admin').returns(admin_db)
@client.connect
end
should "raise an error on invalid uris" do
ENV['MONGODB_URI'] = "mongo://localhost"
assert_raise MongoArgumentError do
MongoClient.new
end
ENV['MONGODB_URI'] = "mongodb://localhost:abc"
assert_raise MongoArgumentError do
MongoClient.new
end
end
should "require all of username, if password and db are specified" do
ENV['MONGODB_URI'] = "mongodb://kyle:jones@localhost/db?connect=false"
assert MongoClient.new
ENV['MONGODB_URI'] = "mongodb://kyle:password@localhost"
assert_raise MongoArgumentError do
MongoClient.new
end
end
end
end
end