Checking response on buildinfo command.

This commit is contained in:
Kyle Banker 2009-11-02 13:22:46 -05:00
parent 9976e5c340
commit 44ff6c5918
5 changed files with 98 additions and 3 deletions

View File

@ -122,7 +122,7 @@ module Mongo
# Return the build information for the current connection.
def server_info
db("admin").db_command(:buildinfo => 1)
db("admin").command({:buildinfo => 1}, {:admin => true, :check_response => true})
end
# Returns the build version of the current server, using

View File

@ -501,6 +501,35 @@ module Mongo
cursor = Cursor.new(Collection.new(self, SYSTEM_COMMAND_COLLECTION), :admin => use_admin_db, :limit => -1, :selector => selector)
cursor.next_object
end
# Sends a command to the database.
#
# :selector (required) :: An OrderedHash, or a standard Hash with just one
# key, specifying the command to be performed.
#
# :admin (optional) :: If true, the command will be executed on the admin
# collection.
#
# :check_response (optional) :: If true, will raise an exception if the
# command fails.
#
# Note: DB commands must start with the "command" key. For this reason,
# any selector containing more than one key must be an OrderedHash.
def command(selector, admin=false, check_response=false)
raise MongoArgumentError, "command must be given a selector" unless selector.is_a?(Hash) && !selector.empty?
if selector.class.eql?(Hash) && selector.keys.length > 1
raise MongoArgumentError, "DB#command requires an OrderedHash when hash contains multiple keys"
end
result = Cursor.new(system_command_collection, :admin => admin,
:limit => -1, :selector => selector).next_object
if check_response && !ok?(result)
raise OperationFailure, "Database command '#{selector.keys.first}' failed."
else
result
end
end
def _synchronize &block
@semaphore.synchronize &block
@ -541,5 +570,9 @@ module Mongo
def hash_password(username, plaintext)
Digest::MD5.hexdigest("#{username}:mongo:#{plaintext}")
end
def system_command_collection
Collection.new(self, SYSTEM_COMMAND_COLLECTION)
end
end
end

View File

@ -18,6 +18,9 @@ module Mongo
# Generic Mongo Ruby Driver exception class.
class MongoRubyError < StandardError; end
# Raised when MongoDB itself has returned an error.
class MongoDBError < RuntimeError; end
# Raised when configuration options cause connections, queries, etc., to fail.
class ConfigurationError < MongoRubyError; end
@ -25,10 +28,10 @@ module Mongo
class MongoArgumentError < MongoRubyError; end
# Raised when a database operation fails.
class OperationFailure < RuntimeError; end
class OperationFailure < MongoDBError; end
# Raised when a client attempts to perform an invalid operation.
class InvalidOperation < RuntimeError; end
class InvalidOperation < MongoDBError; end
# Raised when an invalid name is used.
class InvalidName < RuntimeError; end

View File

@ -189,6 +189,13 @@ class DBTest < Test::Unit::TestCase
assert_nil @@db.previous_error
end
def test_check_command_response
command = {:forceerror => 1}
assert_raise OperationFailure do
@@db.command(command, false, true)
end
end
def test_last_status
@@db['test'].remove
@@db['test'].save("i" => 1)

52
test/unit/db_test.rb Normal file
View File

@ -0,0 +1,52 @@
require 'test/test_helper'
class DBTest < Test::Unit::TestCase
class MockDB < DB
def connect_to_master
true
end
end
context "DB commands" do
setup do
@db = MockDB.new("testing", ['localhost', 27017])
@collection = mock()
@db.stubs(:system_command_collection).returns(@collection)
end
should "raise an error if given a hash with more than one key" do
assert_raise MongoArgumentError do
@db.command(:buildinfo => 1, :somekey => 1)
end
end
should "raise an error if the selector is omitted" do
assert_raise MongoArgumentError do
@db.command({}, true)
end
end
should "create the proper cursor" do
@cursor = mock(:next_object => {"ok" => 1})
Cursor.expects(:new).with(@collection, :admin => true,
:limit => -1, :selector => {:buildinfo => 1}).returns(@cursor)
command = {:buildinfo => 1}
@db.command(command, true)
end
should "raise an error when the command fails" do
@cursor = mock(:next_object => {"ok" => 0})
Cursor.expects(:new).with(@collection, :admin => true,
:limit => -1, :selector => {:buildinfo => 1}).returns(@cursor)
assert_raise OperationFailure do
command = {:buildinfo => 1}
@db.command(command, true, true)
end
end
end
end