Merge pull request #744 from chef/dr/processes-struct

use struct for processes list
This commit is contained in:
Alex Pop 2016-05-13 11:04:25 +01:00
commit 6f7be9713a
4 changed files with 41 additions and 43 deletions

13
lib/inspec/polyfill.rb Normal file
View file

@ -0,0 +1,13 @@
# encoding: utf-8
# copyright: 2016, Chef Software Inc.
# author: Dominik Richter
# author: Christoph Hartmann
# license: All rights reserved
class Struct
unless instance_methods.include? :to_h
def to_h
Hash[each_pair.to_a]
end
end
end

View file

@ -4,6 +4,7 @@
# author: Christoph Hartmann
require 'forwardable'
require 'inspec/polyfill'
require 'inspec/fetcher'
require 'inspec/source_reader'
require 'inspec/metadata'

View file

@ -26,7 +26,6 @@ module Inspec::Resources
grep = '(/[^/]*)*'+grep if grep[0] != '/'
grep = Regexp.new('^' + grep + '(\s|$)')
end
all_cmds = ps_aux
@list = all_cmds.find_all do |hm|
hm[:command] =~ grep
@ -57,7 +56,12 @@ module Inspec::Resources
build_process_list(command, regex, os)
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)
all = cmd.stdout.split("\n")[1..-1]
return [] if all.nil?
@ -66,40 +70,13 @@ module Inspec::Resources
line.match(regex)
end.compact
if os.linux?
lines.map do |m|
{
label: m[1],
user: m[2],
pid: m[3].to_i,
cpu: m[4],
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
lines.map do |m|
a = m.to_a[1..-1] # grab all matching groups
a.unshift(nil) unless os.linux?
a[2] = a[2].to_i
a[5] = a[5].to_i
a[6] = a[6].to_i
Process.new(*a)
end
end
end

View file

@ -13,7 +13,8 @@ describe 'Inspec::Resources::Processes' do
it 'verify processes resource' do
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,
user: 'root',
pid: 1,
@ -26,14 +27,13 @@ describe 'Inspec::Resources::Processes' do
start: '14:15',
time: '0:00',
command: '/bin/bash',
}]
_(resource.list.length).must_equal 1
})
end
it 'verify processes resource on linux os' do
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',
user: 'root',
pid: 1,
@ -46,9 +46,16 @@ describe 'Inspec::Resources::Processes' do
start: 'May04',
time: '0:01',
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
it 'retrieves the users and states as arrays' do