mirror of
https://github.com/inspec/inspec
synced 2024-11-26 22:50:36 +00:00
CFINSPEC-73: Add unit test and relevant files for cgroup
Signed-off-by: Sonu Saha <sonu.saha@progress.com>
This commit is contained in:
parent
023f4275da
commit
4f151bd72c
6 changed files with 225 additions and 18 deletions
|
@ -17,12 +17,13 @@ module Inspec::Resources
|
|||
|
||||
# Resource initialization.
|
||||
def initialize(cgroup_name)
|
||||
raise Inspec::Exceptions::ResourceSkipped, "The `cgroup` resource is not supported on your OS yet." unless inspec.os.linux?
|
||||
|
||||
@cgroup_name = cgroup_name
|
||||
@valid_queries, @valid_queries_split = [], []
|
||||
find_valid_queries
|
||||
# Used to track the method calls in an "its" query
|
||||
@cgroup_info_query = []
|
||||
raise Inspec::Exceptions::ResourceSkipped, "The `cgroup` resource is not supported on your OS yet." unless inspec.os.linux?
|
||||
end
|
||||
|
||||
def resource_id
|
||||
|
@ -40,17 +41,18 @@ module Inspec::Resources
|
|||
|
||||
# The ith level param must match with atleast one row's ith column of @valid_queries_split
|
||||
# Else there is no way, we would find any valid query in further iteration, so raise exception.
|
||||
if @valid_queries_split.map{|e| e[@cgroup_info_query.length-1]}.include?(param.to_s)
|
||||
if @valid_queries_split.map { |e| e[@cgroup_info_query.length - 1] }.include?(param.to_s)
|
||||
# If the query form so far is part of @valid_queries, we are good to trigger find_cgroup_info
|
||||
# else go for next level of param
|
||||
if @valid_queries.include?(query)
|
||||
@cgroup_info_query = []
|
||||
find_cgroup_info(query)
|
||||
else
|
||||
self
|
||||
end
|
||||
self
|
||||
end
|
||||
else
|
||||
@cgroup_info_query = []
|
||||
|
||||
raise Inspec::Exceptions::ResourceFailed, "The query #{query} does not appear to be valid."
|
||||
end
|
||||
end
|
||||
|
@ -71,6 +73,7 @@ module Inspec::Resources
|
|||
bin = find_cgget_or_error
|
||||
cgget_cmd = "#{bin} -n -r #{query} #{@cgroup_name}"
|
||||
cmd = inspec.command(cgget_cmd)
|
||||
|
||||
raise Inspec::Exceptions::ResourceFailed, "Executing cgget failed: #{cmd.stderr}" if cmd.exit_status.to_i != 0
|
||||
|
||||
# For complex returns the user must use match /the_regex/
|
||||
|
@ -86,12 +89,13 @@ module Inspec::Resources
|
|||
bin = find_cgget_or_error
|
||||
cgget_all_cmd = "#{bin} -n -a #{@cgroup_name}"
|
||||
cmd = inspec.command(cgget_all_cmd)
|
||||
raise Inspec::Exceptions::ResourceFailed, "Executing cgget failed: #{cmd.stderr}" if cmd.exit_status.to_i != 0
|
||||
queries = cmd.stdout.to_s.gsub(/:.*/, "").gsub(/^\s+.*/, "").split("\n")
|
||||
|
||||
raise Inspec::Exceptions::ResourceFailed, "Executing cgget failed: #{cmd.stderr}" if cmd.exit_status.to_i != 0
|
||||
|
||||
queries = cmd.stdout.to_s.gsub(/:.*/, "").gsub(/^\s+.*/, "").split("\n")
|
||||
# store the relevant controller parameters in @valid_queries and the dot splitted paramters into @valid_queries_split
|
||||
@valid_queries = queries.map {|q| q if q.length > 0 }.compact
|
||||
@valid_queries_split = @valid_queries.map {|q| q.split(".") }.compact
|
||||
@valid_queries = queries.map { |q| q if q.length > 0 }.compact
|
||||
@valid_queries_split = @valid_queries.map { |q| q.split(".") }.compact
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
146
test/fixtures/cmd/cgget-n-a
vendored
Normal file
146
test/fixtures/cmd/cgget-n-a
vendored
Normal file
|
@ -0,0 +1,146 @@
|
|||
devices.list: a *:* rwm
|
||||
cpuset.memory_pressure: 0
|
||||
cpuset.memory_migrate: 0
|
||||
cpuset.mem_exclusive: 0
|
||||
cpuset.memory_spread_slab: 0
|
||||
cpuset.cpu_exclusive: 0
|
||||
cpuset.effective_mems: 0
|
||||
cpuset.effective_cpus: 0
|
||||
cpuset.sched_load_balance: 1
|
||||
cpuset.mems: 0
|
||||
cpuset.mem_hardwall: 0
|
||||
cpuset.sched_relax_domain_level: -1
|
||||
cpuset.cpus: 0
|
||||
cpuset.memory_spread_page: 0
|
||||
freezer.self_freezing: 0
|
||||
freezer.parent_freezing: 0
|
||||
freezer.state: THAWED
|
||||
hugetlb.2MB.limit_in_bytes: 9223372036852678656
|
||||
hugetlb.2MB.usage_in_bytes: 0
|
||||
hugetlb.2MB.rsvd.failcnt: 0
|
||||
hugetlb.2MB.max_usage_in_bytes: 0
|
||||
hugetlb.2MB.rsvd.limit_in_bytes: 9223372036852678656
|
||||
hugetlb.2MB.rsvd.usage_in_bytes: 0
|
||||
hugetlb.2MB.rsvd.max_usage_in_bytes: 0
|
||||
hugetlb.2MB.failcnt: 0
|
||||
net_cls.classid: 0
|
||||
net_prio.prioidx: 7
|
||||
net_prio.ifpriomap: lo 0
|
||||
eth0 0
|
||||
lxdbr0 0
|
||||
vethb7b9a0f5 0
|
||||
lxcbr0 0
|
||||
veth72d3ce6b 0
|
||||
blkio.throttle.read_iops_device:
|
||||
blkio.throttle.io_service_bytes: Total 0
|
||||
blkio.throttle.write_iops_device:
|
||||
blkio.throttle.read_bps_device:
|
||||
blkio.throttle.write_bps_device:
|
||||
blkio.throttle.io_serviced: Total 0
|
||||
blkio.throttle.io_service_bytes_recursive: Total 0
|
||||
blkio.throttle.io_serviced_recursive: Total 0
|
||||
rdma.current:
|
||||
rdma.max:
|
||||
cpu.cfs_period_us: 100000
|
||||
cpu.stat: nr_periods 0
|
||||
nr_throttled 0
|
||||
throttled_time 0
|
||||
cpu.shares: 1024
|
||||
cpu.cfs_quota_us: -1
|
||||
cpu.uclamp.min: 0.00
|
||||
cpu.uclamp.max: max
|
||||
cpuacct.usage_percpu_sys: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
|
||||
cpuacct.usage_percpu: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
|
||||
cpuacct.stat: user 0
|
||||
system 0
|
||||
cpuacct.usage: 0
|
||||
cpuacct.usage_sys: 0
|
||||
cpuacct.usage_all: cpu user system
|
||||
0 0 0
|
||||
1 0 0
|
||||
2 0 0
|
||||
3 0 0
|
||||
4 0 0
|
||||
5 0 0
|
||||
6 0 0
|
||||
7 0 0
|
||||
8 0 0
|
||||
9 0 0
|
||||
10 0 0
|
||||
11 0 0
|
||||
12 0 0
|
||||
13 0 0
|
||||
14 0 0
|
||||
cpuacct.usage_percpu_user: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
|
||||
cpuacct.usage_user: 0
|
||||
memory.use_hierarchy: 1
|
||||
memory.kmem.tcp.usage_in_bytes: 0
|
||||
memory.soft_limit_in_bytes: 9223372036854771712
|
||||
memory.move_charge_at_immigrate: 0
|
||||
memory.memsw.failcnt: 0
|
||||
memory.kmem.tcp.max_usage_in_bytes: 0
|
||||
memory.max_usage_in_bytes: 0
|
||||
memory.oom_control: oom_kill_disable 0
|
||||
under_oom 0
|
||||
oom_kill 0
|
||||
memory.stat: cache 0
|
||||
rss 0
|
||||
rss_huge 0
|
||||
shmem 0
|
||||
mapped_file 0
|
||||
dirty 0
|
||||
writeback 0
|
||||
swap 0
|
||||
pgpgin 0
|
||||
pgpgout 0
|
||||
pgfault 0
|
||||
pgmajfault 0
|
||||
inactive_anon 0
|
||||
active_anon 0
|
||||
inactive_file 0
|
||||
active_file 0
|
||||
unevictable 0
|
||||
hierarchical_memory_limit 9223372036854771712
|
||||
hierarchical_memsw_limit 9223372036854771712
|
||||
total_cache 0
|
||||
total_rss 0
|
||||
total_rss_huge 0
|
||||
total_shmem 0
|
||||
total_mapped_file 0
|
||||
total_dirty 0
|
||||
total_writeback 0
|
||||
total_swap 0
|
||||
total_pgpgin 0
|
||||
total_pgpgout 0
|
||||
total_pgfault 0
|
||||
total_pgmajfault 0
|
||||
total_inactive_anon 0
|
||||
total_active_anon 0
|
||||
total_inactive_file 0
|
||||
total_active_file 0
|
||||
total_unevictable 0
|
||||
memory.kmem.slabinfo:
|
||||
memory.limit_in_bytes: 9223372036854771712
|
||||
memory.swappiness: 60
|
||||
memory.memsw.max_usage_in_bytes: 0
|
||||
memory.numa_stat: total=0 N0=0
|
||||
file=0 N0=0
|
||||
anon=0 N0=0
|
||||
unevictable=0 N0=0
|
||||
hierarchical_total=0 N0=0
|
||||
hierarchical_file=0 N0=0
|
||||
hierarchical_anon=0 N0=0
|
||||
hierarchical_unevictable=0 N0=0
|
||||
memory.kmem.failcnt: 0
|
||||
memory.kmem.max_usage_in_bytes: 0
|
||||
memory.usage_in_bytes: 0
|
||||
memory.memsw.limit_in_bytes: 9223372036854771712
|
||||
memory.failcnt: 0
|
||||
memory.kmem.tcp.failcnt: 0
|
||||
memory.kmem.limit_in_bytes: 9223372036854771712
|
||||
memory.memsw.usage_in_bytes: 0
|
||||
memory.kmem.usage_in_bytes: 0
|
||||
memory.kmem.tcp.limit_in_bytes: 9223372036854771712
|
||||
pids.current: 0
|
||||
pids.events: max 0
|
||||
pids.max: max
|
1
test/fixtures/cmd/cgget-n-r
vendored
Normal file
1
test/fixtures/cmd/cgget-n-r
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
cpuset.cpus: 0
|
36
test/fixtures/cmd/cgget-n-r-stat
vendored
Normal file
36
test/fixtures/cmd/cgget-n-r-stat
vendored
Normal file
|
@ -0,0 +1,36 @@
|
|||
memory.stat: cache 0
|
||||
rss 0
|
||||
rss_huge 0
|
||||
shmem 0
|
||||
mapped_file 0
|
||||
dirty 0
|
||||
writeback 0
|
||||
swap 0
|
||||
pgpgin 0
|
||||
pgpgout 0
|
||||
pgfault 0
|
||||
pgmajfault 0
|
||||
inactive_anon 0
|
||||
active_anon 0
|
||||
inactive_file 0
|
||||
active_file 0
|
||||
unevictable 0
|
||||
hierarchical_memory_limit 9223372036854771712
|
||||
hierarchical_memsw_limit 9223372036854771712
|
||||
total_cache 0
|
||||
total_rss 0
|
||||
total_rss_huge 0
|
||||
total_shmem 0
|
||||
total_mapped_file 0
|
||||
total_dirty 0
|
||||
total_writeback 0
|
||||
total_swap 0
|
||||
total_pgpgin 0
|
||||
total_pgpgout 0
|
||||
total_pgfault 0
|
||||
total_pgmajfault 0
|
||||
total_inactive_anon 0
|
||||
total_active_anon 0
|
||||
total_inactive_file 0
|
||||
total_active_file 0
|
||||
total_unevictable 0
|
|
@ -378,6 +378,11 @@ class MockLoader
|
|||
# lxc
|
||||
"/usr/sbin/lxc info my-ubuntu-container | grep -i Status" => cmd.call("lxcinfo"),
|
||||
%{sh -c 'type "/usr/sbin/lxc"'} => empty.call,
|
||||
# cgroup
|
||||
"cgget -n -a carrotking" => cmd.call("cgget-n-a"),
|
||||
"cgget -n -r cpuset.cpus carrotking" => cmd.call("cgget-n-r"),
|
||||
"cgget -n -r memory.stat carrotking" => cmd.call("cgget-n-r-stat"),
|
||||
%{sh -c 'type "cgget"'} => empty.call,
|
||||
# apache_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"),
|
||||
|
|
|
@ -1,18 +1,33 @@
|
|||
# If we can load the InSpec globals definition file...
|
||||
require "inspec/globals"
|
||||
# ... we can find the core test unit helper file
|
||||
require "#{Inspec.src_root}/test/helper"
|
||||
|
||||
# Load (require) the resource library file
|
||||
require_relative "../../../lib/inspec/resources/cgroup"
|
||||
|
||||
describe Inspec::Resources::Cgroup do
|
||||
it "works correctly with the constructor on the platform" do
|
||||
# Add contructor arguments to load_resource if needed
|
||||
resource = MockLoader.new("linux".to_sym).load_resource("cgroup")
|
||||
# ubuntu
|
||||
it "check carrotking cgroup information on ubuntu" do
|
||||
resource = MockLoader.new("ubuntu".to_sym).load_resource("cgroup", "carrotking")
|
||||
_(resource.cpuset.cpus).must_equal 0
|
||||
_(resource.memory.stat).must_match(/hierarchical_memory_limit 9223372036854771712/)
|
||||
end
|
||||
|
||||
_(resource.has_bells?).must_equal true
|
||||
_(resource.shoe_size).must_equal 42
|
||||
_(resource.resource_id).must_equal "something special"
|
||||
# debian
|
||||
it "check carrotking cgroup information on debian" do
|
||||
resource = MockLoader.new("debian8".to_sym).load_resource("cgroup", "carrotking")
|
||||
_(resource.cpuset.cpus).must_equal 0
|
||||
_(resource.memory.stat).must_match(/hierarchical_memory_limit 9223372036854771712/)
|
||||
end
|
||||
|
||||
# windows
|
||||
it "check carrotking cgroup information on windows" do
|
||||
resource = MockLoader.new("windows".to_sym).load_resource("cgroup", "carrotking")
|
||||
_(resource.resource_skipped?).must_equal true
|
||||
_(resource.resource_exception_message).must_equal "The `cgroup` resource is not supported on your OS yet."
|
||||
end
|
||||
|
||||
# undefined
|
||||
it "check carrotking cgroup information on unsupported os" do
|
||||
resource = MockLoader.new("undefined".to_sym).load_resource("cgroup", "carrotking")
|
||||
_(resource.resource_skipped?).must_equal true
|
||||
_(resource.resource_exception_message).must_equal "The `cgroup` resource is not supported on your OS yet."
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue