simplify key symbolization in metadata

This commit is contained in:
Dominik Richter 2016-04-16 18:09:28 -04:00
parent 14995534cd
commit f54195408f
3 changed files with 34 additions and 37 deletions

View file

@ -43,7 +43,9 @@ module Inspec
end end
def is_supported?(os, entry) def is_supported?(os, entry)
name, family, release = support_fields(entry) name = entry[:'os-name'] || entry[:os]
family = entry[:'os-family']
release = entry[:release]
# return true if the backend matches the supported OS's # return true if the backend matches the supported OS's
# fields act as masks, i.e. any value configured for os-name, os-family, # fields act as masks, i.e. any value configured for os-name, os-family,
@ -68,31 +70,8 @@ module Inspec
name_ok && family_ok && release_ok name_ok && family_ok && release_ok
end end
def support_fields(entry)
if entry.is_a?(Hash)
try_support = self.class.symbolize_keys(entry)
name = try_support[:'os-name'] || try_support[:os]
family = try_support[:'os-family']
release = try_support[:release]
elsif entry.is_a?(String)
@logger.warn(
"Do not use deprecated `supports: #{entry}` syntax. Instead use "\
"`supports: {os-family: #{entry}}`.")
family = entry
end
[name, family, release]
end
def support_list
supp = params[:supports]
supp.is_a?(Hash) ? [supp] : Array(supp)
end
def inspec_requirement def inspec_requirement
supp = support_list inspec = params[:supports].find { |x| !x[:inspec].nil? } || {}
supp = supp.map { |x| self.class.symbolize_keys(x) }
inspec = supp.find { |x| !x[:inspec].nil? } || {}
Gem::Requirement.create(inspec[:inspec]) Gem::Requirement.create(inspec[:inspec])
end end
@ -105,10 +84,9 @@ module Inspec
# with no supports specified, always return true, as there are no # with no supports specified, always return true, as there are no
# constraints on the supported backend; it is equivalent to putting # constraints on the supported backend; it is equivalent to putting
# all fields into accept-all mode # all fields into accept-all mode
supp = support_list return true if params[:supports].empty?
return true if supp.empty?
found = supp.find do |entry| found = params[:supports].find do |entry|
is_supported?(backend.os, entry) is_supported?(backend.os, entry)
end end
@ -148,32 +126,51 @@ module Inspec
@missing_methods @missing_methods
end end
def self.symbolize_keys(hash) def self.symbolize_keys(obj)
hash.each_with_object({}) {|(k, v), h| return obj.map { |i| symbolize_keys(i) } if obj.is_a?(Array)
return obj unless obj.is_a?(Hash)
obj.each_with_object({}) {|(k, v), h|
v = symbolize_keys(v) if v.is_a?(Hash) v = symbolize_keys(v) if v.is_a?(Hash)
v = symbolize_keys(v) if v.is_a?(Array)
h[k.to_sym] = v h[k.to_sym] = v
} }
end end
def self.finalize(metadata, profile_id) def self.finalize(metadata, profile_id, logger = nil)
return nil if metadata.nil? return nil if metadata.nil?
param = metadata.params || {} param = metadata.params || {}
param['name'] = profile_id.to_s unless profile_id.to_s.empty? param['name'] = profile_id.to_s unless profile_id.to_s.empty?
param['version'] = param['version'].to_s unless param['version'].nil? param['version'] = param['version'].to_s unless param['version'].nil?
metadata.params = symbolize_keys(param) metadata.params = symbolize_keys(param)
# consolidate supports field with legacy mode
metadata.params[:supports] =
case x = metadata.params[:supports]
when Hash then [x]
when Array then x
when nil then []
else
logger ||= Logger.new(nil)
logger.warn(
"Do not use deprecated `supports: #{x}` syntax. Instead use "\
"`supports: {os-family: #{x}}`.")
[{ :'os-family' => x }]
end
metadata metadata
end end
def self.from_yaml(ref, contents, profile_id, logger = nil) def self.from_yaml(ref, contents, profile_id, logger = nil)
res = Metadata.new(ref, logger) res = Metadata.new(ref, logger)
res.params = YAML.load(contents) res.params = YAML.load(contents)
finalize(res, profile_id) finalize(res, profile_id, logger)
end end
def self.from_ruby(ref, contents, profile_id, logger = nil) def self.from_ruby(ref, contents, profile_id, logger = nil)
res = Metadata.new(ref, logger) res = Metadata.new(ref, logger)
res.instance_eval(contents, ref, 1) res.instance_eval(contents, ref, 1)
finalize(res, profile_id) finalize(res, profile_id, logger)
end end
def self.from_ref(ref, contents, profile_id, logger = nil) def self.from_ref(ref, contents, profile_id, logger = nil)

View file

@ -12,7 +12,7 @@ describe 'metadata with supported operating systems' do
res = Inspec::Metadata.from_yaml('mock', "---", nil, logger) res = Inspec::Metadata.from_yaml('mock', "---", nil, logger)
# manually inject supported parameters # manually inject supported parameters
res.params[:supports] = params res.params[:supports] = params
Inspec::Metadata.finalize(res, 'mock') Inspec::Metadata.finalize(res, 'mock', logger)
res res
end end
@ -40,7 +40,7 @@ describe 'metadata with supported operating systems' do
it 'loads the support field from metadata' do it 'loads the support field from metadata' do
res = Inspec::Metadata.from_yaml('mock', res = Inspec::Metadata.from_yaml('mock',
"---\nsupports:\n - os: ubuntu", nil) "---\nsupports:\n - os: ubuntu", nil)
res.params[:supports].must_equal([{ 'os' => 'ubuntu' }]) res.params[:supports].must_equal([{ os: 'ubuntu' }])
end end
it 'load a profile with empty supports clause' do it 'load a profile with empty supports clause' do
@ -50,10 +50,10 @@ describe 'metadata with supported operating systems' do
it 'supports legacy simple support style, but warns' do it 'supports legacy simple support style, but warns' do
# i.e. setting this to something that would fail: # i.e. setting this to something that would fail:
m = supports_meta('linux')
logger.expect :warn, nil, [ logger.expect :warn, nil, [
'Do not use deprecated `supports: linux` '\ 'Do not use deprecated `supports: linux` '\
'syntax. Instead use `supports: {os-family: linux}`.'] 'syntax. Instead use `supports: {os-family: linux}`.']
m = supports_meta('linux')
m.supports_transport?(backend).must_equal true m.supports_transport?(backend).must_equal true
logger.verify logger.verify
end end

View file

@ -172,7 +172,7 @@ describe Inspec::Profile do
end end
it 'doesnt have constraints on supported systems' do it 'doesnt have constraints on supported systems' do
profile.metadata.params.wont_include(:supports) profile.metadata.params[:supports].must_equal([])
end end
end end