mirror of
https://github.com/inspec/inspec
synced 2025-02-22 08:58:44 +00:00
Add unit test for mongodb_session resource
Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
This commit is contained in:
parent
a008514d57
commit
7f7cbf0ddb
2 changed files with 23 additions and 3 deletions
|
@ -38,14 +38,15 @@ module Inspec::Resources
|
|||
@auth_mech_properties = opts[:auth_mech_properties] || {}
|
||||
@client = nil
|
||||
|
||||
fail_resource "Can't run MongoDB checks without authentication" unless user && @password
|
||||
fail_resource "You must provide a database name for the session" unless database
|
||||
fail_resource "Can't run MongoDB checks without authentication." unless user && @password
|
||||
fail_resource "You must provide a database name for the session." unless database
|
||||
|
||||
create_session
|
||||
end
|
||||
|
||||
def query(command)
|
||||
raise Inspec::Exceptions::ResourceFailed, "#{resource_exception_message}" if resource_failed?
|
||||
|
||||
Lines.new(@client.command(command).documents.first, "MongoDB query: #{command}")
|
||||
rescue => e
|
||||
raise Inspec::Exceptions::ResourceFailed, "Can't run MongoDB command Error: #{e.message}"
|
||||
|
@ -54,6 +55,8 @@ module Inspec::Resources
|
|||
private
|
||||
|
||||
def create_session
|
||||
raise Inspec::Exceptions::ResourceFailed, "#{resource_exception_message}" if resource_failed?
|
||||
|
||||
options = { user: "#{user}",
|
||||
password: "#{@password}",
|
||||
database: "#{database}",
|
||||
|
@ -69,7 +72,7 @@ module Inspec::Resources
|
|||
@client = Mongo::Client.new([ "#{host}:#{port}" ], options)
|
||||
|
||||
rescue => e
|
||||
raise Inspec::Exceptions::ResourceFailed, "Can't run MongoDB command Error: #{e.message}"
|
||||
raise Inspec::Exceptions::ResourceFailed, "Can't run MongoDB command. Error: #{e.message}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
17
test/unit/resources/mongodb_session_test.rb
Normal file
17
test/unit/resources/mongodb_session_test.rb
Normal file
|
@ -0,0 +1,17 @@
|
|||
require "helper"
|
||||
require "inspec/resource"
|
||||
require "inspec/resources/mongodb_session"
|
||||
|
||||
describe "Inspec::Resources::MongodbSession" do
|
||||
it "fails when no user, password" do
|
||||
resource = load_resource("mongodb_session", host: "localhost", port: 27017, database: "test")
|
||||
_(resource.resource_failed?).must_equal true
|
||||
_(resource.resource_exception_message).must_equal "Can't run MongoDB command. Error: Can't run MongoDB checks without authentication."
|
||||
end
|
||||
|
||||
it "fails when no database name is provided" do
|
||||
resource = load_resource("mongodb_session", user: "foo", password: "bar", host: "localhost", port: 27017)
|
||||
_(resource.resource_failed?).must_equal true
|
||||
_(resource.resource_exception_message).must_equal "Can't run MongoDB command. Error: You must provide a database name for the session."
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue