Support zfs_pool and zfs_dataset resources on Linux. Handled #5075. Signed-off-by: @kannanr <kannaa.ram@gmail.com>

This commit is contained in:
Kannan Ramakrishnan 2021-05-15 12:18:39 +05:30
parent 43c9caa7e9
commit 19c624cf38
9 changed files with 118 additions and 20 deletions

View file

@ -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 (Centos, RHEL, Ubuntu, CloudLinux, Debian) systems.
## Availability

View file

@ -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

View file

@ -16,16 +16,17 @@ 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? or 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_cli} get -Hp all #{@zfs_dataset}").exit_status == 0
end
def mounted?
@ -39,7 +40,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

View file

@ -15,16 +15,17 @@ 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? or 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 +33,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
View file

@ -0,0 +1 @@
/sbin/zfs

1
test/fixtures/cmd/zpool-which vendored Normal file
View file

@ -0,0 +1 @@
/sbin/zpool

View file

@ -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 && ["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 && !["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

View file

@ -7,9 +7,46 @@ 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

View file

@ -7,9 +7,44 @@ 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