Checking response on buildinfo command.
This commit is contained in:
parent
9976e5c340
commit
44ff6c5918
@ -122,7 +122,7 @@ module Mongo
|
|||||||
|
|
||||||
# Return the build information for the current connection.
|
# Return the build information for the current connection.
|
||||||
def server_info
|
def server_info
|
||||||
db("admin").db_command(:buildinfo => 1)
|
db("admin").command({:buildinfo => 1}, {:admin => true, :check_response => true})
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns the build version of the current server, using
|
# Returns the build version of the current server, using
|
||||||
|
@ -502,6 +502,35 @@ module Mongo
|
|||||||
cursor.next_object
|
cursor.next_object
|
||||||
end
|
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
|
def _synchronize &block
|
||||||
@semaphore.synchronize &block
|
@semaphore.synchronize &block
|
||||||
end
|
end
|
||||||
@ -541,5 +570,9 @@ module Mongo
|
|||||||
def hash_password(username, plaintext)
|
def hash_password(username, plaintext)
|
||||||
Digest::MD5.hexdigest("#{username}:mongo:#{plaintext}")
|
Digest::MD5.hexdigest("#{username}:mongo:#{plaintext}")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def system_command_collection
|
||||||
|
Collection.new(self, SYSTEM_COMMAND_COLLECTION)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -18,6 +18,9 @@ module Mongo
|
|||||||
# Generic Mongo Ruby Driver exception class.
|
# Generic Mongo Ruby Driver exception class.
|
||||||
class MongoRubyError < StandardError; end
|
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.
|
# Raised when configuration options cause connections, queries, etc., to fail.
|
||||||
class ConfigurationError < MongoRubyError; end
|
class ConfigurationError < MongoRubyError; end
|
||||||
|
|
||||||
@ -25,10 +28,10 @@ module Mongo
|
|||||||
class MongoArgumentError < MongoRubyError; end
|
class MongoArgumentError < MongoRubyError; end
|
||||||
|
|
||||||
# Raised when a database operation fails.
|
# Raised when a database operation fails.
|
||||||
class OperationFailure < RuntimeError; end
|
class OperationFailure < MongoDBError; end
|
||||||
|
|
||||||
# Raised when a client attempts to perform an invalid operation.
|
# 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.
|
# Raised when an invalid name is used.
|
||||||
class InvalidName < RuntimeError; end
|
class InvalidName < RuntimeError; end
|
||||||
|
@ -189,6 +189,13 @@ class DBTest < Test::Unit::TestCase
|
|||||||
assert_nil @@db.previous_error
|
assert_nil @@db.previous_error
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_check_command_response
|
||||||
|
command = {:forceerror => 1}
|
||||||
|
assert_raise OperationFailure do
|
||||||
|
@@db.command(command, false, true)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_last_status
|
def test_last_status
|
||||||
@@db['test'].remove
|
@@db['test'].remove
|
||||||
@@db['test'].save("i" => 1)
|
@@db['test'].save("i" => 1)
|
||||||
|
52
test/unit/db_test.rb
Normal file
52
test/unit/db_test.rb
Normal 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
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user