mirror of
https://github.com/inspec/inspec
synced 2024-11-23 05:03:07 +00:00
Merge pull request #5523 from kannanr/master
Support zfs_pool and zfs_dataset resources on Linux. Handled #5075
This commit is contained in:
commit
442c89d45e
9 changed files with 117 additions and 16 deletions
|
@ -11,7 +11,7 @@ platform = "linux"
|
|||
parent = "inspec/resources/os"
|
||||
+++
|
||||
|
||||
Use the `zfs_dataset` Chef InSpec audit resource to test the ZFS datasets on FreeBSD systems.
|
||||
Use the `zfs_dataset` Chef InSpec audit resource to test the ZFS datasets on FreeBSD & Linux (Check [OS Family Details](https://docs.chef.io/inspec/resources/os/#osfamily-helpers) for more details).
|
||||
|
||||
## Availability
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ platform = "linux"
|
|||
parent = "inspec/resources/os"
|
||||
+++
|
||||
|
||||
Use the `zfs_pool` Chef InSpec audit resource to test the ZFS pools on FreeBSD systems.
|
||||
Use the `zfs_pool` Chef InSpec audit resource to test the ZFS pools on FreeBSD & Linux (Centos, RHEL, Ubuntu, CloudLinux, Debian) systems.
|
||||
|
||||
## Availability
|
||||
|
||||
|
|
|
@ -16,16 +16,20 @@ module Inspec::Resources
|
|||
EXAMPLE
|
||||
|
||||
def initialize(zfs_dataset)
|
||||
return skip_resource "The `zfs_dataset` resource is not supported on your OS yet." unless inspec.os.bsd?
|
||||
return skip_resource "The `zfs_dataset` resource is not supported on your OS yet." unless inspec.os.bsd? || inspec.os.linux?
|
||||
|
||||
@zfs_dataset = zfs_dataset
|
||||
find_zfs = inspec.command("which zfs")
|
||||
@zfs_cmd = find_zfs.stdout.strip
|
||||
|
||||
return skip_resource "zfs is not installed" if find_zfs.exit_status != 0
|
||||
|
||||
@params = gather
|
||||
end
|
||||
|
||||
# method called by 'it { should exist }'
|
||||
def exists?
|
||||
inspec.command("/sbin/zfs get -Hp all #{@zfs_dataset}").exit_status == 0
|
||||
inspec.command("#{@zfs_cmd} get -Hp all #{@zfs_dataset}").exit_status == 0
|
||||
end
|
||||
|
||||
def mounted?
|
||||
|
@ -39,7 +43,7 @@ module Inspec::Resources
|
|||
end
|
||||
|
||||
def gather
|
||||
cmd = inspec.command("/sbin/zfs get -Hp all #{@zfs_dataset}")
|
||||
cmd = inspec.command("#{@zfs_cmd} get -Hp all #{@zfs_dataset}")
|
||||
return nil if cmd.exit_status.to_i != 0
|
||||
|
||||
# parse data
|
||||
|
|
|
@ -15,16 +15,20 @@ module Inspec::Resources
|
|||
EXAMPLE
|
||||
|
||||
def initialize(zfs_pool)
|
||||
return skip_resource "The `zfs_pool` resource is not supported on your OS yet." unless inspec.os.bsd?
|
||||
return skip_resource "The `zfs_pool` resource is not supported on your OS yet." unless inspec.os.bsd? || inspec.os.linux?
|
||||
|
||||
@zfs_pool = zfs_pool
|
||||
find_zpool = inspec.command("which zpool")
|
||||
@zpool_cmd = find_zpool.stdout.strip
|
||||
|
||||
return skip_resource "zfs is not installed" if find_zpool.exit_status != 0
|
||||
|
||||
@params = gather
|
||||
end
|
||||
|
||||
# method called by 'it { should exist }'
|
||||
def exists?
|
||||
inspec.command("/sbin/zpool get -Hp all #{@zfs_pool}").exit_status == 0
|
||||
inspec.command("#{@zpool_cmd} get -Hp all #{@zfs_pool}").exit_status == 0
|
||||
end
|
||||
|
||||
def to_s
|
||||
|
@ -32,7 +36,7 @@ module Inspec::Resources
|
|||
end
|
||||
|
||||
def gather
|
||||
cmd = inspec.command("/sbin/zpool get -Hp all #{@zfs_pool}")
|
||||
cmd = inspec.command("#{@zpool_cmd} get -Hp all #{@zfs_pool}")
|
||||
return nil if cmd.exit_status.to_i != 0
|
||||
|
||||
# parse data
|
||||
|
|
1
test/fixtures/cmd/zfs-which
vendored
Normal file
1
test/fixtures/cmd/zfs-which
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/sbin/zfs
|
1
test/fixtures/cmd/zpool-which
vendored
Normal file
1
test/fixtures/cmd/zpool-which
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/sbin/zpool
|
|
@ -427,6 +427,10 @@ class MockLoader
|
|||
"/sbin/zfs get -Hp all tank/tmp" => cmd.call("zfs-get-all-tank-tmp"),
|
||||
# zfs output for pool tank
|
||||
"/sbin/zpool get -Hp all tank" => cmd.call("zpool-get-all-tank"),
|
||||
# which zfs
|
||||
"which zfs" => cmd.call("zfs-which"),
|
||||
# which zpool
|
||||
"which zpool" => cmd.call("zpool-which"),
|
||||
# docker
|
||||
"4f8e24022ea8b7d3b117041ec32e55d9bf08f11f4065c700e7c1dc606c84fd17" => cmd.call("docker-ps-a"),
|
||||
"b40ed61c006b54f155b28a85dc944dc0352b30222087b47c6279568ec0e59d05" => cmd.call("df-PT"),
|
||||
|
@ -590,6 +594,24 @@ class MockLoader
|
|||
"netstat -tulpen" => cmd.call("netstat-tulpen")
|
||||
)
|
||||
end
|
||||
|
||||
# zfs dynamic commands
|
||||
if @platform && %w{centos debian ubuntu amazon}.include?(@platform[:name])
|
||||
mock_cmds.merge!(
|
||||
# zfs output for dataset tank/tmp
|
||||
%{`which zfs` get -Hp all tank/tmp} => cmd.call("zfs-get-all-tank-tmp"),
|
||||
# zfs output for pool tank
|
||||
%{`which zpool` get -Hp all tank} => cmd.call("zpool-get-all-tank")
|
||||
)
|
||||
end
|
||||
|
||||
if @platform && ! %w{centos cloudlinux coreos debian freebsd ubuntu amazon}.include?(@platform[:name])
|
||||
mock_cmds.delete("/sbin/zfs get -Hp all tank/tmp")
|
||||
mock_cmds.delete("/sbin/zpool get -Hp all tank")
|
||||
mock_cmds.delete("which zfs")
|
||||
mock_cmds.delete("which zpool")
|
||||
end
|
||||
|
||||
mock.commands = mock_cmds
|
||||
|
||||
@backend
|
||||
|
|
|
@ -7,9 +7,44 @@ describe Inspec::Resources::ZfsDataset do
|
|||
let(:tank_tmp_resource) { loader.send("load_resource", "zfs_dataset", "tank/tmp") }
|
||||
|
||||
it "parses the ZFS dataset data properly" do
|
||||
_(tank_tmp_resource.send(:mountpoint)).must_equal("/tmp")
|
||||
_(tank_tmp_resource.send(:type)).must_equal("filesystem")
|
||||
_(tank_tmp_resource.send(:exec)).must_equal("off")
|
||||
_(tank_tmp_resource.send(:setuid)).must_equal("off")
|
||||
if _(tank_tmp_resource)
|
||||
_(tank_tmp_resource.send(:mountpoint)).must_equal("/tmp")
|
||||
_(tank_tmp_resource.send(:type)).must_equal("filesystem")
|
||||
_(tank_tmp_resource.send(:exec)).must_equal("off")
|
||||
_(tank_tmp_resource.send(:setuid)).must_equal("off")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe Inspec::Resources::ZfsDataset do
|
||||
let(:loader) { MockLoader.new(:centos7) }
|
||||
let(:tank_tmp_resource) { loader.send("load_resource", "zfs_dataset", "tank/tmp") }
|
||||
|
||||
it "parses the ZFS dataset data properly" do
|
||||
if _(tank_tmp_resource)
|
||||
_(tank_tmp_resource.send(:mountpoint)).must_equal("/tmp")
|
||||
_(tank_tmp_resource.send(:type)).must_equal("filesystem")
|
||||
_(tank_tmp_resource.send(:exec)).must_equal("off")
|
||||
_(tank_tmp_resource.send(:setuid)).must_equal("off")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe Inspec::Resources::ZfsDataset do
|
||||
let(:loader) { MockLoader.new(:macos10_16) }
|
||||
let(:tank_resource) { loader.send("load_resource", "zfs_dataset", "tank") }
|
||||
|
||||
it "parses the ZFS pool data properly" do
|
||||
if _(tank_resource)
|
||||
_(tank_resource.resource_exception_message).must_equal("zfs is not installed")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe Inspec::Resources::ZfsDataset do
|
||||
it "parses the ZFS dataset properly" do
|
||||
resource = MockLoader.new(:macos10_16).load_resource("zfs_dataset", "tank")
|
||||
_(resource.resource_exception_message).must_equal "zfs is not installed"
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -7,9 +7,43 @@ describe Inspec::Resources::ZfsPool do
|
|||
let(:tank_resource) { loader.send("load_resource", "zfs_pool", "tank") }
|
||||
|
||||
it "parses the ZFS pool data properly" do
|
||||
_(tank_resource.send(:health)).must_equal("ONLINE")
|
||||
_(tank_resource.send(:guid)).must_equal("4711279777582057513")
|
||||
_(tank_resource.send(:failmode)).must_equal("continue")
|
||||
_(tank_resource.send(:'feature@lz4_compress')).must_equal("active")
|
||||
if _(tank_resource)
|
||||
_(tank_resource.send(:health)).must_equal("ONLINE")
|
||||
_(tank_resource.send(:guid)).must_equal("4711279777582057513")
|
||||
_(tank_resource.send(:failmode)).must_equal("continue")
|
||||
_(tank_resource.send(:'feature@lz4_compress')).must_equal("active")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe Inspec::Resources::ZfsPool do
|
||||
let(:loader) { MockLoader.new(:centos7) }
|
||||
let(:tank_resource) { loader.send("load_resource", "zfs_pool", "tank") }
|
||||
|
||||
it "parses the ZFS pool data properly" do
|
||||
if _(tank_resource)
|
||||
_(tank_resource.send(:health)).must_equal("ONLINE")
|
||||
_(tank_resource.send(:guid)).must_equal("4711279777582057513")
|
||||
_(tank_resource.send(:failmode)).must_equal("continue")
|
||||
_(tank_resource.send(:'feature@lz4_compress')).must_equal("active")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe Inspec::Resources::ZfsPool do
|
||||
let(:loader) { MockLoader.new(:macos10_16) }
|
||||
let(:tank_resource) { loader.send("load_resource", "zfs_pool", "tank") }
|
||||
|
||||
it "parses the ZFS pool data properly" do
|
||||
if _(tank_resource)
|
||||
_(tank_resource.resource_exception_message).must_equal("zfs is not installed")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe Inspec::Resources::ZfsPool do
|
||||
it "parses the ZFS pool data properly" do
|
||||
resource = MockLoader.new(:macos10_16).load_resource("zfs_pool", "tank")
|
||||
_(resource.resource_exception_message).must_equal "zfs is not installed"
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue