Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance package resource error handling #2388

Merged
merged 4 commits into from
Dec 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions lib/resources/package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Package < Inspec.resource(1)
end
"

def initialize(package_name = nil, opts = {}) # rubocop:disable Metrics/AbcSize
def initialize(package_name, opts = {}) # rubocop:disable Metrics/AbcSize
@package_name = package_name
@name = @package_name
@cache = nil
Expand All @@ -45,36 +45,31 @@ def initialize(package_name = nil, opts = {}) # rubocop:disable Metrics/AbcSize
elsif ['hpux'].include?(os[:family])
@pkgman = HpuxPkg.new(inspec)
else
return skip_resource 'The `package` resource is not supported on your OS yet.'
raise Inspec::Exceptions::ResourceSkipped, 'The `package` resource is not supported on your OS yet.'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this - in the same vein as my prior comment, since we're adopting exceptions as the new normal, moving toward that is a good thing.

end

evaluate_missing_requirements
end

# returns true if the package is installed
def installed?(_provider = nil, _version = nil)
return false if info.nil?
info[:installed] == true
end

# returns true it the package is held (if the OS supports it)
def held?(_provider = nil, _version = nil)
return false if info.nil?
return false unless info.key?(:held)
info[:held] == true
end

# returns the package description
def info
return @cache if [email protected]?
return nil if @pkgman.nil?
@pkgman.info(@package_name)
end

# return the package version
def version
info = @pkgman.info(@package_name)
return nil if info.nil?
info[:version]
end

Expand All @@ -87,7 +82,7 @@ def to_s
def evaluate_missing_requirements
missing_requirements_string = @pkgman.missing_requirements.uniq.join(', ')
return if missing_requirements_string.empty?
skip_resource "The following requirements are not met for this resource: #{missing_requirements_string}"
raise Inspec::Exceptions::ResourceSkipped, "The following requirements are not met for this resource: #{missing_requirements_string}"
end
end

Expand All @@ -99,7 +94,7 @@ def initialize(inspec)

def missing_requirements
# Each provider can provide an Array of missing requirements that will be
# combined into a `skip_resource` message
# combined into a `ResourceSkipped` exception message.
[]
end
end
Expand All @@ -108,7 +103,7 @@ def missing_requirements
class Deb < PkgManagement
def info(package_name)
cmd = inspec.command("dpkg -s #{package_name}")
return nil if cmd.exit_status.to_i != 0
return {} if cmd.exit_status.to_i != 0

params = SimpleConfig.new(
cmd.stdout.chomp,
Expand Down Expand Up @@ -152,7 +147,7 @@ def info(package_name)
cmd = inspec.command(rpm_cmd)
# CentOS does not return an error code if the package is not installed,
# therefore we need to check for emptyness
return nil if cmd.exit_status.to_i != 0 || cmd.stdout.chomp.empty?
return {} if cmd.exit_status.to_i != 0 || cmd.stdout.chomp.empty?
params = SimpleConfig.new(
cmd.stdout.chomp,
assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
Expand Down Expand Up @@ -195,7 +190,7 @@ class Brew < PkgManagement
def info(package_name)
brew_path = inspec.command('brew').exist? ? 'brew' : '/usr/local/bin/brew'
cmd = inspec.command("#{brew_path} info --json=v1 #{package_name}")
return nil if cmd.exit_status.to_i != 0
return {} if cmd.exit_status.to_i != 0
# parse data
pkg = JSON.parse(cmd.stdout)[0]
{
Expand All @@ -204,16 +199,18 @@ def info(package_name)
version: pkg['installed'][0]['version'],
type: 'brew',
}
rescue JSON::ParserError => _e
return nil
rescue JSON::ParserError => e
raise Inspec::Exceptions::ResourceFailed,
'Failed to parse JSON from `brew` command. ' \
"Error: #{e}"
end
end

# Arch Linux
class Pacman < PkgManagement
def info(package_name)
cmd = inspec.command("pacman -Qi #{package_name}")
return nil if cmd.exit_status.to_i != 0
return {} if cmd.exit_status.to_i != 0

params = SimpleConfig.new(
cmd.stdout.chomp,
Expand All @@ -233,7 +230,7 @@ def info(package_name)
class HpuxPkg < PkgManagement
def info(package_name)
cmd = inspec.command("swlist -l product | grep #{package_name}")
return nil if cmd.exit_status.to_i != 0
return {} if cmd.exit_status.to_i != 0
pkg = cmd.stdout.strip.split(' ')
{
name: pkg[0],
Expand Down Expand Up @@ -268,8 +265,10 @@ def info(package_name)

begin
package = JSON.parse(cmd.stdout)
rescue JSON::ParserError => _e
return nil
rescue JSON::ParserError => e
raise Inspec::Exceptions::ResourceFailed,
'Failed to parse JSON from PowerShell. ' \
"Error: #{e}"
end

# What if we match multiple packages? just pick the first one for now.
Expand All @@ -288,7 +287,7 @@ def info(package_name)
class BffPkg < PkgManagement
def info(package_name)
cmd = inspec.command("lslpp -cL #{package_name}")
return nil if cmd.exit_status.to_i != 0
return {} if cmd.exit_status.to_i != 0

bff_pkg = cmd.stdout.split("\n").last.split(':')
{
Expand All @@ -313,7 +312,7 @@ def info(package_name)
# solaris 10
def solaris10_info(package_name)
cmd = inspec.command("pkginfo -l #{package_name}")
return nil if cmd.exit_status.to_i != 0
return {} if cmd.exit_status.to_i != 0

params = SimpleConfig.new(
cmd.stdout.chomp,
Expand All @@ -334,7 +333,7 @@ def solaris10_info(package_name)
# solaris 11
def solaris11_info(package_name)
cmd = inspec.command("pkg info #{package_name}")
return nil if cmd.exit_status.to_i != 0
return {} if cmd.exit_status.to_i != 0

params = SimpleConfig.new(
cmd.stdout.chomp,
Expand Down
4 changes: 2 additions & 2 deletions test/unit/resources/package_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
# undefined
it 'verify package handling on unsupported os' do
resource = MockLoader.new(:undefined).load_resource('package', 'curl')
_(resource.installed?).must_equal false
_(resource.info).must_be_nil
_(resource.resource_skipped?).must_equal true
_(resource.resource_exception_message).must_equal 'The `package` resource is not supported on your OS yet.'
end
end