2009-12-01 18:49:57 +00:00
|
|
|
require 'test/test_helper'
|
2009-09-11 16:12:24 +00:00
|
|
|
require 'logger'
|
|
|
|
require 'stringio'
|
2010-01-20 17:40:16 +00:00
|
|
|
require 'thread'
|
2009-01-16 14:52:31 +00:00
|
|
|
|
|
|
|
# NOTE: assumes Mongo is running
|
2009-08-20 22:48:09 +00:00
|
|
|
class TestConnection < Test::Unit::TestCase
|
2009-01-16 14:52:31 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
include Mongo
|
2010-04-05 15:07:01 +00:00
|
|
|
include BSON
|
2009-01-16 14:52:31 +00:00
|
|
|
|
|
|
|
def setup
|
|
|
|
@host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
2009-08-20 22:48:09 +00:00
|
|
|
@port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
|
|
|
@mongo = Connection.new(@host, @port)
|
2009-01-16 14:52:31 +00:00
|
|
|
end
|
|
|
|
|
2009-03-13 21:09:19 +00:00
|
|
|
def teardown
|
2010-04-05 19:48:35 +00:00
|
|
|
@mongo.db(MONGO_TEST_DB).error
|
2009-03-13 21:09:19 +00:00
|
|
|
end
|
|
|
|
|
2009-10-26 18:54:33 +00:00
|
|
|
def test_server_info
|
|
|
|
server_info = @mongo.server_info
|
2009-10-26 20:41:17 +00:00
|
|
|
assert server_info.keys.include?("version")
|
2009-10-26 18:54:33 +00:00
|
|
|
assert_equal 1.0, server_info["ok"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_server_version
|
|
|
|
assert_match /\d\.\d+(\.\d+)?/, @mongo.server_version.to_s
|
|
|
|
end
|
|
|
|
|
2009-08-13 20:26:51 +00:00
|
|
|
def test_invalid_database_names
|
|
|
|
assert_raise TypeError do @mongo.db(4) end
|
|
|
|
|
2010-04-05 15:07:01 +00:00
|
|
|
assert_raise Mongo::InvalidName do @mongo.db('') end
|
|
|
|
assert_raise Mongo::InvalidName do @mongo.db('te$t') end
|
|
|
|
assert_raise Mongo::InvalidName do @mongo.db('te.t') end
|
|
|
|
assert_raise Mongo::InvalidName do @mongo.db('te\\t') end
|
|
|
|
assert_raise Mongo::InvalidName do @mongo.db('te/t') end
|
|
|
|
assert_raise Mongo::InvalidName do @mongo.db('te st') end
|
2009-08-13 20:26:51 +00:00
|
|
|
end
|
|
|
|
|
2009-01-16 14:52:31 +00:00
|
|
|
def test_database_info
|
2010-04-05 19:48:35 +00:00
|
|
|
@mongo.drop_database(MONGO_TEST_DB)
|
|
|
|
@mongo.db(MONGO_TEST_DB).collection('info-test').insert('a' => 1)
|
2009-01-28 19:21:23 +00:00
|
|
|
|
2009-01-16 14:52:31 +00:00
|
|
|
info = @mongo.database_info
|
|
|
|
assert_not_nil info
|
|
|
|
assert_kind_of Hash, info
|
2010-04-05 19:48:35 +00:00
|
|
|
assert_not_nil info[MONGO_TEST_DB]
|
|
|
|
assert info[MONGO_TEST_DB] > 0
|
2009-01-28 19:21:23 +00:00
|
|
|
|
2010-04-05 19:48:35 +00:00
|
|
|
@mongo.drop_database(MONGO_TEST_DB)
|
2009-01-16 14:52:31 +00:00
|
|
|
end
|
|
|
|
|
2009-11-04 16:57:03 +00:00
|
|
|
def test_copy_database
|
|
|
|
@mongo.db('old').collection('copy-test').insert('a' => 1)
|
|
|
|
@mongo.copy_database('old', 'new')
|
2009-12-16 19:03:15 +00:00
|
|
|
old_object = @mongo.db('old').collection('copy-test').find.next_document
|
|
|
|
new_object = @mongo.db('new').collection('copy-test').find.next_document
|
2009-11-04 16:57:03 +00:00
|
|
|
assert_equal old_object, new_object
|
2010-03-16 17:56:30 +00:00
|
|
|
@mongo.drop_database('old')
|
|
|
|
@mongo.drop_database('new')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_copy_database_with_auth
|
|
|
|
@mongo.db('old').collection('copy-test').insert('a' => 1)
|
|
|
|
@mongo.db('old').add_user('bob', 'secret')
|
|
|
|
|
|
|
|
assert_raise Mongo::OperationFailure do
|
|
|
|
@mongo.copy_database('old', 'new', 'localhost', 'bob', 'badpassword')
|
|
|
|
end
|
|
|
|
|
|
|
|
result = @mongo.copy_database('old', 'new', 'localhost', 'bob', 'secret')
|
|
|
|
assert result['ok'].to_i == 1
|
|
|
|
|
|
|
|
@mongo.drop_database('old')
|
|
|
|
@mongo.drop_database('new')
|
2009-11-04 16:57:03 +00:00
|
|
|
end
|
|
|
|
|
2009-01-16 14:52:31 +00:00
|
|
|
def test_database_names
|
2010-04-05 19:48:35 +00:00
|
|
|
@mongo.drop_database(MONGO_TEST_DB)
|
|
|
|
@mongo.db(MONGO_TEST_DB).collection('info-test').insert('a' => 1)
|
2009-01-28 19:21:23 +00:00
|
|
|
|
2009-01-16 14:52:31 +00:00
|
|
|
names = @mongo.database_names
|
|
|
|
assert_not_nil names
|
|
|
|
assert_kind_of Array, names
|
|
|
|
assert names.length >= 1
|
2010-04-05 19:48:35 +00:00
|
|
|
assert names.include?(MONGO_TEST_DB)
|
2009-01-16 14:52:31 +00:00
|
|
|
end
|
|
|
|
|
2009-09-11 16:12:24 +00:00
|
|
|
def test_logging
|
|
|
|
output = StringIO.new
|
|
|
|
logger = Logger.new(output)
|
|
|
|
logger.level = Logger::DEBUG
|
2010-04-05 19:48:35 +00:00
|
|
|
db = Connection.new(@host, @port, :logger => logger).db(MONGO_TEST_DB)
|
2010-03-24 05:33:53 +00:00
|
|
|
assert output.string.include?("admin['$cmd'].find")
|
2009-09-11 16:12:24 +00:00
|
|
|
end
|
2010-02-25 19:58:32 +00:00
|
|
|
|
2009-10-07 23:39:36 +00:00
|
|
|
def test_connection_logger
|
|
|
|
output = StringIO.new
|
|
|
|
logger = Logger.new(output)
|
|
|
|
logger.level = Logger::DEBUG
|
|
|
|
connection = Connection.new(@host, @port, :logger => logger)
|
|
|
|
assert_equal logger, connection.logger
|
|
|
|
|
|
|
|
connection.logger.debug 'testing'
|
|
|
|
assert output.string.include?('testing')
|
|
|
|
end
|
2009-09-11 16:12:24 +00:00
|
|
|
|
2009-01-20 14:21:19 +00:00
|
|
|
def test_drop_database
|
2009-01-28 19:21:23 +00:00
|
|
|
db = @mongo.db('ruby-mongo-will-be-deleted')
|
2009-01-20 14:21:19 +00:00
|
|
|
coll = db.collection('temp')
|
2009-10-20 15:31:07 +00:00
|
|
|
coll.remove
|
2009-01-20 14:21:19 +00:00
|
|
|
coll.insert(:name => 'temp')
|
|
|
|
assert_equal 1, coll.count()
|
2009-01-28 19:21:23 +00:00
|
|
|
assert @mongo.database_names.include?('ruby-mongo-will-be-deleted')
|
2009-01-20 14:21:19 +00:00
|
|
|
|
2009-01-28 19:21:23 +00:00
|
|
|
@mongo.drop_database('ruby-mongo-will-be-deleted')
|
|
|
|
assert !@mongo.database_names.include?('ruby-mongo-will-be-deleted')
|
2009-01-20 14:21:19 +00:00
|
|
|
end
|
|
|
|
|
2009-11-23 20:20:05 +00:00
|
|
|
def test_nodes
|
|
|
|
db = Connection.new({:left => ['foo', 123]}, nil, :connect => false)
|
|
|
|
nodes = db.nodes
|
|
|
|
assert_equal 2, db.nodes.length
|
|
|
|
assert_equal ['foo', 123], nodes[0]
|
|
|
|
assert_equal ['localhost', Connection::DEFAULT_PORT], nodes[1]
|
|
|
|
|
|
|
|
db = Connection.new({:right => 'bar'}, nil, :connect => false)
|
|
|
|
nodes = db.nodes
|
|
|
|
assert_equal 2, nodes.length
|
|
|
|
assert_equal ['localhost', Connection::DEFAULT_PORT], nodes[0]
|
|
|
|
assert_equal ['bar', Connection::DEFAULT_PORT], nodes[1]
|
|
|
|
|
|
|
|
db = Connection.new({:right => ['foo', 123], :left => 'bar'}, nil, :connect => false)
|
|
|
|
nodes = db.nodes
|
|
|
|
assert_equal 2, nodes.length
|
|
|
|
assert_equal ['bar', Connection::DEFAULT_PORT], nodes[0]
|
|
|
|
assert_equal ['foo', 123], nodes[1]
|
2009-01-23 18:30:59 +00:00
|
|
|
end
|
2010-01-20 17:40:16 +00:00
|
|
|
|
2010-02-25 19:58:32 +00:00
|
|
|
context "Saved authentications" do
|
|
|
|
setup do
|
|
|
|
@conn = Mongo::Connection.new
|
|
|
|
@auth = {'db_name' => 'test', 'username' => 'bob', 'password' => 'secret'}
|
|
|
|
@conn.add_auth(@auth['db_name'], @auth['username'], @auth['password'])
|
|
|
|
end
|
|
|
|
|
|
|
|
should "save the authentication" do
|
|
|
|
assert_equal @auth, @conn.auths[0]
|
|
|
|
end
|
|
|
|
|
|
|
|
should "replace the auth if given a new auth for the same db" do
|
|
|
|
auth = {'db_name' => 'test', 'username' => 'mickey', 'password' => 'm0u53'}
|
|
|
|
@conn.add_auth(auth['db_name'], auth['username'], auth['password'])
|
|
|
|
assert_equal 1, @conn.auths.length
|
|
|
|
assert_equal auth, @conn.auths[0]
|
|
|
|
end
|
|
|
|
|
|
|
|
should "remove auths by database" do
|
|
|
|
@conn.remove_auth('non-existent database')
|
|
|
|
assert_equal 1, @conn.auths.length
|
|
|
|
|
|
|
|
@conn.remove_auth('test')
|
|
|
|
assert_equal 0, @conn.auths.length
|
|
|
|
end
|
|
|
|
|
|
|
|
should "remove all auths" do
|
|
|
|
@conn.clear_auths
|
|
|
|
assert_equal 0, @conn.auths.length
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-20 17:40:16 +00:00
|
|
|
context "Connection exceptions" do
|
|
|
|
setup do
|
|
|
|
@conn = Mongo::Connection.new('localhost', 27017, :pool_size => 10, :timeout => 10)
|
2010-04-05 19:48:35 +00:00
|
|
|
@coll = @conn[MONGO_TEST_DB]['test-connection-exceptions']
|
2010-01-20 17:40:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
should "release connection if an exception is raised on send_message" do
|
|
|
|
@conn.stubs(:send_message_on_socket).raises(ConnectionFailure)
|
|
|
|
assert_equal 0, @conn.checked_out.size
|
|
|
|
assert_raise ConnectionFailure do
|
|
|
|
@coll.insert({:test => "insert"})
|
|
|
|
end
|
|
|
|
assert_equal 0, @conn.checked_out.size
|
|
|
|
end
|
|
|
|
|
|
|
|
should "release connection if an exception is raised on send_with_safe_check" do
|
|
|
|
@conn.stubs(:receive).raises(ConnectionFailure)
|
|
|
|
assert_equal 0, @conn.checked_out.size
|
|
|
|
assert_raise ConnectionFailure do
|
|
|
|
@coll.insert({:test => "insert"}, :safe => true)
|
|
|
|
end
|
|
|
|
assert_equal 0, @conn.checked_out.size
|
|
|
|
end
|
|
|
|
|
|
|
|
should "release connection if an exception is raised on receive_message" do
|
|
|
|
@conn.stubs(:receive).raises(ConnectionFailure)
|
|
|
|
assert_equal 0, @conn.checked_out.size
|
|
|
|
assert_raise ConnectionFailure do
|
|
|
|
@coll.find.to_a
|
|
|
|
end
|
|
|
|
assert_equal 0, @conn.checked_out.size
|
|
|
|
end
|
|
|
|
end
|
2009-08-20 22:48:09 +00:00
|
|
|
end
|