CFINSPEC-79: Add unit test and its related changes for lxc resource

Signed-off-by: Sonu Saha <sonu.saha@progress.com>
This commit is contained in:
Sonu Saha 2022-03-10 16:50:21 +05:30
parent 41ddceaa85
commit 36bf43f350
4 changed files with 33 additions and 14 deletions

View file

@ -14,10 +14,11 @@ module Inspec::Resources
# Resource initialization. # Resource initialization.
def initialize(container_name) def initialize(container_name)
skip_resource "The `lxc` resource is not yet available on your OS." unless inspec.os.linux?
@container_name = container_name @container_name = container_name
@lxc_info_cmd = lxc_info_cmd return if inspec.os.linux?
@container_info = [] @container_info = []
skip_resource "The `lxc` resource is not supported on your OS yet."
end end
def resource_id def resource_id
@ -29,12 +30,15 @@ module Inspec::Resources
end end
def exists? def exists?
@lxc_info_cmd.exit_status.to_i == 0 return false if defined?(@container_info)
lxc_info_cmd.exit_status.to_i == 0
end end
def running? def running?
exists? return false if defined?(@container_info)
@container_info = @lxc_info_cmd.stdout.split(":").map(&:strip)
@container_info = lxc_info_cmd.stdout.split(":").map(&:strip)
@container_info[0] == "Status" && @container_info[1] == "Running" @container_info[0] == "Status" && @container_info[1] == "Running"
end end
@ -46,7 +50,7 @@ module Inspec::Resources
return cmd if inspec.command(cmd).exist? return cmd if inspec.command(cmd).exist?
end end
raise Inspec::Exceptions::ResourceFailed, "Could not find `iptables`" raise Inspec::Exceptions::ResourceFailed, "Could not find `lxc`"
end end
def lxc_info_cmd def lxc_info_cmd

1
test/fixtures/cmd/lxcinfo vendored Normal file
View file

@ -0,0 +1 @@
Status: Running

View file

@ -375,6 +375,9 @@ class MockLoader
# ipfilter # ipfilter
"/usr/sbin/ipfstat -io" => cmd.call("ipfstat-io"), "/usr/sbin/ipfstat -io" => cmd.call("ipfstat-io"),
%{type "/usr/sbin/ipfstat"} => empty.call, %{type "/usr/sbin/ipfstat"} => empty.call,
# lxc
"/usr/sbin/lxc info my-ubuntu-container | grep -i Status" => cmd.call("lxcinfo"),
%{sh -c 'type "/usr/sbin/lxc"'} => empty.call,
# apache_conf # apache_conf
"sh -c 'find /etc/apache2/ports.conf -type f -maxdepth 1'" => cmd.call("find-apache2-ports-conf"), "sh -c 'find /etc/apache2/ports.conf -type f -maxdepth 1'" => cmd.call("find-apache2-ports-conf"),
"sh -c 'find /etc/httpd/conf.d/*.conf -type f -maxdepth 1'" => cmd.call("find-httpd-ssl-conf"), "sh -c 'find /etc/httpd/conf.d/*.conf -type f -maxdepth 1'" => cmd.call("find-httpd-ssl-conf"),

View file

@ -2,17 +2,28 @@
require "inspec/globals" require "inspec/globals"
# ... we can find the core test unit helper file # ... we can find the core test unit helper file
require "#{Inspec.src_root}/test/helper" require "#{Inspec.src_root}/test/helper"
require_relative "../../../lib/inspec/resource"
# Load (require) the resource library file # Load (require) the resource library file
require_relative "../../../lib/inspec/resources/lxc" require_relative "../../../lib/inspec/resources/lxc"
describe Inspec::Resources::Lxc do describe "Inspec::Resources::Lxc" do
it "works correctly with the constructor on the platform" do # ubuntu
# Add contructor arguments to load_resource if needed it "verify lxc resource on ubuntu" do
resource = MockLoader.new("linux".to_sym).load_resource("lxc") resource = MockLoader.new(:ubuntu).load_resource("lxc", "my-ubuntu-container")
_(resource.exists?).must_equal true
_(resource.running?).must_equal true
end
_(resource.has_bells?).must_equal true # windows
_(resource.shoe_size).must_equal 42 it "verify lxc resource on windows" do
_(resource.resource_id).must_equal "something special" resource = MockLoader.new(:windows).load_resource("lxc", "my-ubuntu-container")
_(resource.exists?).must_equal false
_(resource.running?).must_equal false
end
# undefined
it "verify lxc resource on unsupported os" do
resource = MockLoader.new(:undefined).load_resource("lxc", "my-ubuntu-container")
_(resource.exists?).must_equal false
end end
end end