Skip packages resource for unsupported OS

Signed-off-by: Alex Pop <apop@chef.io>
This commit is contained in:
Alex Pop 2017-02-10 10:33:21 +00:00
parent 0153b571a3
commit ce927e657a
3 changed files with 20 additions and 20 deletions

View file

@ -23,10 +23,14 @@ module Inspec::Resources
"
def initialize(pattern)
@command = packages_command
return skip_resource "The packages resource is not yet supported on OS #{inspec.os.name}" unless @command
@pattern = pattern_regexp(pattern)
all_pkgs = package_list
all_pkgs = build_package_list(@command)
@list = all_pkgs.find_all do |hm|
hm[:name] =~ pattern_regexp(pattern)
hm[:name] =~ @pattern
end
end
@ -44,31 +48,29 @@ module Inspec::Resources
private
def packages_command
os = inspec.os
if os.debian?
command = "dpkg-query -W -f='${db:Status-Abbrev} ${Package} ${Version}\\n'"
end
command
end
def pattern_regexp(p)
if p.class == String
Regexp.new(Regexp.escape(p))
elsif p.class == Regexp
p
else
raise 'invalid name argument to packages resource, please use a "string" or /regexp/'
raise 'Invalid name argument to packages resource, please use a "string" or /regexp/'
end
end
def filtered_packages
warn "The packages resource is not yet supported on OS #{inspec.os.name}" unless @command
@list
end
def package_list
os = inspec.os
if os.debian?
command = "dpkg-query -W -f='${db:Status-Abbrev} ${Package} ${Version}\\n'"
else
raise "packages resource is not yet supported on #{os.name}"
end
build_package_list(command)
end
Package = Struct.new(:status, :name, :version)
def build_package_list(command)

View file

@ -57,7 +57,7 @@ module Inspec::Resources
name 'users'
desc 'Use the users InSpec audit resource to test local user profiles. Users can be filtered by groups to which they belong, the frequency of required password changes, the directory paths to home and shell.'
example "
describe users.where(uid: 0).entries do
describe users.where { uid == 0 }.entries do
it { should eq ['root'] }
its('uids') { should eq [1234] }
its('gids') { should eq [1234] }

View file

@ -43,11 +43,9 @@ describe 'Inspec::Resources::Packages' do
end
it 'fails on non debian platforms' do
proc {
resource = MockLoader.new(:centos6).load_resource('packages', 'bash')
resource.send(:entries, nil)
}.must_raise(RuntimeError)
it 'skips on non debian platforms' do
resource = MockLoader.new(:centos6).load_resource('packages', [:a, :b])
_(resource.resource_skipped).must_equal 'The packages resource is not yet supported on OS centos'
end
it 'fails if the packages name is not a string or regexp' do