mirror of
https://github.com/inspec/inspec
synced 2025-03-05 07:47:33 +00:00
use struct for processes list
we know all the fields + struct is fully compatible to the curren hash implementation
This commit is contained in:
parent
987c42ed99
commit
dde4433933
2 changed files with 27 additions and 43 deletions
|
@ -26,7 +26,6 @@ module Inspec::Resources
|
||||||
grep = '(/[^/]*)*'+grep if grep[0] != '/'
|
grep = '(/[^/]*)*'+grep if grep[0] != '/'
|
||||||
grep = Regexp.new('^' + grep + '(\s|$)')
|
grep = Regexp.new('^' + grep + '(\s|$)')
|
||||||
end
|
end
|
||||||
|
|
||||||
all_cmds = ps_aux
|
all_cmds = ps_aux
|
||||||
@list = all_cmds.find_all do |hm|
|
@list = all_cmds.find_all do |hm|
|
||||||
hm[:command] =~ grep
|
hm[:command] =~ grep
|
||||||
|
@ -57,7 +56,12 @@ module Inspec::Resources
|
||||||
build_process_list(command, regex, os)
|
build_process_list(command, regex, os)
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_process_list(command, regex, os) # rubocop:disable MethodLength, Metrics/AbcSize
|
Process = Struct.new(:label, :user, :pid,
|
||||||
|
:cpu, :mem, :vsz,
|
||||||
|
:rss, :tty, :stat,
|
||||||
|
:start, :time, :command)
|
||||||
|
|
||||||
|
def build_process_list(command, regex, os)
|
||||||
cmd = inspec.command(command)
|
cmd = inspec.command(command)
|
||||||
all = cmd.stdout.split("\n")[1..-1]
|
all = cmd.stdout.split("\n")[1..-1]
|
||||||
return [] if all.nil?
|
return [] if all.nil?
|
||||||
|
@ -66,40 +70,13 @@ module Inspec::Resources
|
||||||
line.match(regex)
|
line.match(regex)
|
||||||
end.compact
|
end.compact
|
||||||
|
|
||||||
if os.linux?
|
lines.map do |m|
|
||||||
lines.map do |m|
|
a = m.to_a[1..-1] # grab all matching groups
|
||||||
{
|
a.unshift(nil) unless os.linux?
|
||||||
label: m[1],
|
a[2] = a[2].to_i
|
||||||
user: m[2],
|
a[5] = a[5].to_i
|
||||||
pid: m[3].to_i,
|
a[6] = a[6].to_i
|
||||||
cpu: m[4],
|
Process.new(*a)
|
||||||
mem: m[5],
|
|
||||||
vsz: m[6].to_i,
|
|
||||||
rss: m[7].to_i,
|
|
||||||
tty: m[8],
|
|
||||||
stat: m[9],
|
|
||||||
start: m[10],
|
|
||||||
time: m[11],
|
|
||||||
command: m[12],
|
|
||||||
}
|
|
||||||
end
|
|
||||||
else
|
|
||||||
lines.map do |m|
|
|
||||||
{
|
|
||||||
label: nil,
|
|
||||||
user: m[1],
|
|
||||||
pid: m[2].to_i,
|
|
||||||
cpu: m[3],
|
|
||||||
mem: m[4],
|
|
||||||
vsz: m[5].to_i,
|
|
||||||
rss: m[6].to_i,
|
|
||||||
tty: m[7],
|
|
||||||
stat: m[8],
|
|
||||||
start: m[9],
|
|
||||||
time: m[10],
|
|
||||||
command: m[11],
|
|
||||||
}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,7 +13,8 @@ describe 'Inspec::Resources::Processes' do
|
||||||
|
|
||||||
it 'verify processes resource' do
|
it 'verify processes resource' do
|
||||||
resource = MockLoader.new(:freebsd10).load_resource('processes', '/bin/bash')
|
resource = MockLoader.new(:freebsd10).load_resource('processes', '/bin/bash')
|
||||||
_(resource.list).must_equal [{
|
_(resource.list.length).must_equal 1
|
||||||
|
_(resource.list[0].to_h).must_equal({
|
||||||
label: nil,
|
label: nil,
|
||||||
user: 'root',
|
user: 'root',
|
||||||
pid: 1,
|
pid: 1,
|
||||||
|
@ -26,14 +27,13 @@ describe 'Inspec::Resources::Processes' do
|
||||||
start: '14:15',
|
start: '14:15',
|
||||||
time: '0:00',
|
time: '0:00',
|
||||||
command: '/bin/bash',
|
command: '/bin/bash',
|
||||||
}]
|
})
|
||||||
|
|
||||||
_(resource.list.length).must_equal 1
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'verify processes resource on linux os' do
|
it 'verify processes resource on linux os' do
|
||||||
resource = MockLoader.new(:centos6).load_resource('processes', '/sbin/init')
|
resource = MockLoader.new(:centos6).load_resource('processes', '/sbin/init')
|
||||||
_(resource.list).must_equal [{
|
_(resource.list.length).must_equal 1
|
||||||
|
_(resource.list[0].to_h).must_equal({
|
||||||
label: 'system_u:system_r:kernel_t:s0',
|
label: 'system_u:system_r:kernel_t:s0',
|
||||||
user: 'root',
|
user: 'root',
|
||||||
pid: 1,
|
pid: 1,
|
||||||
|
@ -46,9 +46,16 @@ describe 'Inspec::Resources::Processes' do
|
||||||
start: 'May04',
|
start: 'May04',
|
||||||
time: '0:01',
|
time: '0:01',
|
||||||
command: '/sbin/init',
|
command: '/sbin/init',
|
||||||
}]
|
})
|
||||||
|
end
|
||||||
|
|
||||||
_(resource.list.length).must_equal 1
|
it 'access attributes of a process' do
|
||||||
|
resource = MockLoader.new(:centos6).load_resource('processes', '/sbin/init')
|
||||||
|
process = resource.list[0]
|
||||||
|
process.user.must_equal 'root'
|
||||||
|
process[:user].must_equal 'root'
|
||||||
|
process['user'].must_equal 'root'
|
||||||
|
process[1].must_equal 'root'
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'retrieves the users and states as arrays' do
|
it 'retrieves the users and states as arrays' do
|
||||||
|
|
Loading…
Add table
Reference in a new issue