mongo-ruby-driver/test/unit/connection_test.rb

182 lines
6.5 KiB
Ruby
Raw Normal View History

2010-09-09 19:58:51 +00:00
require './test/test_helper'
2010-02-20 00:17:38 +00:00
include Mongo
2010-02-22 20:49:04 +00:00
class ConnectionTest < Test::Unit::TestCase
context "Initialization: " do
2010-02-20 00:17:38 +00:00
setup do
def new_mock_socket(host='localhost', port=27017)
2010-02-22 20:49:04 +00:00
socket = Object.new
socket.stubs(:setsockopt).with(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
2010-08-24 16:06:23 +00:00
socket.stubs(:close)
2010-02-22 20:49:04 +00:00
socket
end
2010-02-22 20:49:04 +00:00
def new_mock_db
db = Object.new
end
2010-02-20 00:17:38 +00:00
end
2010-02-22 20:49:04 +00:00
context "given a single node" do
setup do
@conn = Connection.new('localhost', 27017, :connect => false)
2010-07-19 16:07:46 +00:00
TCPSocket.stubs(:new).returns(new_mock_socket)
2010-02-22 20:49:04 +00:00
admin_db = new_mock_db
admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
@conn.expects(:[]).with('admin').returns(admin_db)
2010-07-19 16:07:46 +00:00
@conn.connect
2010-02-22 20:49:04 +00:00
end
2010-02-22 20:49:04 +00:00
should "set localhost and port to master" do
assert_equal 'localhost', @conn.primary_pool.host
assert_equal 27017, @conn.primary_pool.port
2010-02-22 20:49:04 +00:00
end
2010-02-22 20:49:04 +00:00
should "set connection pool to 1" do
assert_equal 1, @conn.primary_pool.size
end
2010-02-22 20:49:04 +00:00
should "default slave_ok to false" do
assert !@conn.slave_ok?
end
end
2010-07-19 16:07:46 +00:00
context "connecting to a replica set" do
setup do
TCPSocket.stubs(:new).returns(new_mock_socket('localhost', 27017))
@conn = Connection.multi([['localhost', 27017]], :connect => false, :read_secondaries => true)
2010-07-19 16:07:46 +00:00
admin_db = new_mock_db
@hosts = ['localhost:27018', 'localhost:27019', 'localhost:27020']
admin_db.stubs(:command).returns({'ok' => 1, 'ismaster' => 1, 'hosts' => @hosts}).
then.returns({'ok' => 1, 'ismaster' => 0, 'hosts' => @hosts, 'secondary' => 1}).
then.returns({'ok' => 1, 'ismaster' => 0, 'hosts' => @hosts, 'secondary' => 1}).
then.returns({'ok' => 1, 'ismaster' => 0, 'arbiterOnly' => 1})
@conn.stubs(:[]).with('admin').returns(admin_db)
2010-07-19 16:07:46 +00:00
@conn.connect
end
should "store the hosts returned from the ismaster command" do
assert_equal 'localhost', @conn.primary_pool.host
assert_equal 27017, @conn.primary_pool.port
assert_equal 'localhost', @conn.secondary_pools[0].host
assert_equal 27018, @conn.secondary_pools[0].port
assert_equal 'localhost', @conn.secondary_pools[1].host
assert_equal 27019, @conn.secondary_pools[1].port
assert_equal 2, @conn.secondary_pools.length
2010-07-19 16:07:46 +00:00
end
end
context "connecting to a replica set and providing seed nodes" do
setup do
TCPSocket.stubs(:new).returns(new_mock_socket)
@conn = Connection.multi([['localhost', 27017], ['localhost', 27019]], :connect => false)
admin_db = new_mock_db
@hosts = ['localhost:27017', 'localhost:27018', 'localhost:27019']
2010-08-24 16:06:23 +00:00
admin_db.stubs(:command).returns({'ok' => 1, 'ismaster' => 1, 'hosts' => @hosts})
@conn.stubs(:[]).with('admin').returns(admin_db)
2010-07-19 16:07:46 +00:00
@conn.connect
end
should "not store any hosts redundantly" do
end
end
2010-02-22 20:49:04 +00:00
context "initializing a paired connection" do
should "require left and right nodes" do
assert_raise MongoArgumentError do
2010-07-19 16:07:46 +00:00
Connection.multi(['localhost', 27018], :connect => false)
2010-02-22 20:49:04 +00:00
end
2010-02-22 20:49:04 +00:00
assert_raise MongoArgumentError do
2010-07-19 16:07:46 +00:00
Connection.multi(['localhost', 27018], :connect => false)
2010-02-22 20:49:04 +00:00
end
end
2010-02-22 20:49:04 +00:00
should "store both nodes" do
2010-07-19 16:07:46 +00:00
@conn = Connection.multi([['localhost', 27017], ['localhost', 27018]], :connect => false)
2010-02-22 20:49:04 +00:00
assert_equal ['localhost', 27017], @conn.nodes[0]
assert_equal ['localhost', 27018], @conn.nodes[1]
end
2010-02-20 00:17:38 +00:00
end
2010-02-22 20:49:04 +00:00
context "initializing with a mongodb uri" do
should "parse a simple uri" do
@conn = Connection.from_uri("mongodb://localhost", :connect => false)
assert_equal ['localhost', 27017], @conn.nodes[0]
end
should "allow a complex host names" do
host_name = "foo.bar-12345.org"
@conn = Connection.from_uri("mongodb://#{host_name}", :connect => false)
assert_equal [host_name, 27017], @conn.nodes[0]
end
2010-02-22 20:49:04 +00:00
should "parse a uri specifying multiple nodes" do
@conn = Connection.from_uri("mongodb://localhost:27017,mydb.com:27018", :connect => false)
assert_equal ['localhost', 27017], @conn.nodes[0]
assert_equal ['mydb.com', 27018], @conn.nodes[1]
end
2010-02-22 20:49:04 +00:00
should "parse a uri specifying multiple nodes with auth" do
@conn = Connection.from_uri("mongodb://kyle:s3cr3t@localhost:27017/app,mickey:m0u5e@mydb.com:27018/dsny", :connect => false)
assert_equal ['localhost', 27017], @conn.nodes[0]
assert_equal ['mydb.com', 27018], @conn.nodes[1]
2010-02-25 19:58:32 +00:00
auth_hash = {'username' => 'kyle', 'password' => 's3cr3t', 'db_name' => 'app'}
assert_equal auth_hash, @conn.auths[0]
auth_hash = {'username' => 'mickey', 'password' => 'm0u5e', 'db_name' => 'dsny'}
assert_equal auth_hash, @conn.auths[1]
end
should "parse a uri with a hyphen & underscore in the username or password" do
@conn = Connection.from_uri("mongodb://hyphen-user_name:p-s_s@localhost:27017/db", :connect => false)
assert_equal ['localhost', 27017], @conn.nodes[0]
auth_hash = { 'db_name' => 'db', 'username' => 'hyphen-user_name', "password" => 'p-s_s' }
assert_equal auth_hash, @conn.auths[0]
end
2010-02-22 20:49:04 +00:00
should "attempt to connect" do
TCPSocket.stubs(:new).returns(new_mock_socket)
@conn = Connection.from_uri("mongodb://localhost", :connect => false)
admin_db = new_mock_db
admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
@conn.expects(:[]).with('admin').returns(admin_db)
2010-02-25 19:58:32 +00:00
@conn.expects(:apply_saved_authentication)
2010-07-19 16:07:46 +00:00
@conn.connect
end
2010-02-22 20:49:04 +00:00
should "raise an error on invalid uris" do
assert_raise MongoArgumentError do
Connection.from_uri("mongo://localhost", :connect => false)
end
assert_raise MongoArgumentError do
Connection.from_uri("mongodb://localhost:abc", :connect => false)
end
assert_raise MongoArgumentError do
Connection.from_uri("mongodb://localhost:27017, my.db.com:27018, ", :connect => false)
end
2010-02-20 00:17:38 +00:00
end
2010-02-22 20:49:04 +00:00
should "require all of username, password, and database if any one is specified" do
assert_raise MongoArgumentError do
Connection.from_uri("mongodb://localhost/db", :connect => false)
end
assert_raise MongoArgumentError do
Connection.from_uri("mongodb://kyle:password@localhost", :connect => false)
end
2010-02-20 00:17:38 +00:00
end
end
end
2009-11-25 15:25:28 +00:00
end