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

throw an error during inspec check if the version is not correct #1832

Merged
merged 1 commit into from
May 18, 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
1 change: 1 addition & 0 deletions inspec.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ Gem::Specification.new do |spec|
spec.add_dependency 'toml', '~> 0.1'
spec.add_dependency 'addressable', '~> 2.4'
spec.add_dependency 'parslet', '~> 1.5'
spec.add_dependency 'semverse'
end
14 changes: 14 additions & 0 deletions lib/inspec/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'logger'
require 'rubygems/version'
require 'rubygems/requirement'
require 'semverse'

module Inspec
# Extract metadata.rb information
Expand Down Expand Up @@ -109,6 +110,12 @@ def valid
next unless params[field.to_sym].nil?
errors.push("Missing profile #{field} in #{ref}")
end

# if version is set, ensure it is correct
if !params[:version].nil? && !valid_version?(params[:version])
errors.push('Version needs to be in SemVer format')
end

%w{ title summary maintainer copyright }.each do |field|
next unless params[field.to_sym].nil?
warnings.push("Missing profile #{field} in #{ref}")
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the inspec check output will provide the filename that generated the warning:

✖  /Users/aleff/profiles/test1/inspec.yml:0:0: Version need to be in SemVer format in inspec.yml

... I propose can just change this to "Version needs to be in SemVer format" to avoid the duplicate "inspec.yml" output

Expand All @@ -123,6 +130,13 @@ def valid?
errors.empty? && unsupported.empty?
end

def valid_version?(value)
Semverse::Version.new(value)
true
rescue Semverse::InvalidVersionFormat
false
end

def method_missing(sth, *args)
@logger.warn "#{ref} doesn't support: #{sth} #{args}"
@missing_methods.push(sth)
Expand Down
8 changes: 8 additions & 0 deletions test/unit/mock/profiles/invalid-version/inspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: invalid-version
title: InSpec Profile
maintainer: The Authors
copyright: The Authors
copyright_email: you@example.com
license: All Rights Reserved
summary: An InSpec Compliance Profile
version: 0.1.0.999
14 changes: 14 additions & 0 deletions test/unit/profiles/metadata_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ def supports_meta(params)
res.params[:name].must_equal('mock')
end

it 'reads the version from metadata' do
res = Inspec::Metadata.from_yaml('mock', "---\nversion: '1.1.0'", nil)
Inspec::Metadata.finalize(res, 'mock', empty_options)
res.params[:version].must_equal('1.1.0')
res.valid_version?(res.params[:version]).must_equal(true)
end

it 'does not accept invalid version from metadata' do
res = Inspec::Metadata.from_yaml('mock', "---\nversion: '1.1.0.1'", nil)
Inspec::Metadata.finalize(res, 'mock', empty_options)
res.params[:version].must_equal('1.1.0.1')
res.valid_version?(res.params[:version]).must_equal(false)
end

it 'finalizes a loaded metadata by turning strings into symbols' do
res = Inspec::Metadata.from_yaml('mock', "---\nauthor: world", nil)
Inspec::Metadata.finalize(res, 'mock', empty_options)
Expand Down
51 changes: 51 additions & 0 deletions test/unit/profiles/profile_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,5 +282,56 @@
result[:warnings].length.must_equal 0
end
end

describe 'a complete metadata profile with controls in zipfile' do
let(:profile_id) { 'complete-profile' }
let(:profile_path) { MockLoader.profile_zip(profile_id) }
let(:profile) { MockLoader.load_profile(profile_path, {logger: logger}) }

it 'prints ok messages and counts the controls' do
logger.expect :info, nil, ["Checking profile in #{home}/mock/profiles/#{profile_id}"]
logger.expect :info, nil, ['Metadata OK.']
logger.expect :info, nil, ['Found 1 controls.']
logger.expect :info, nil, ['Control definitions OK.']

result = MockLoader.load_profile(profile_id, {logger: logger}).check
# verify logger output
logger.verify

# verify hash result
result[:summary][:valid].must_equal true
result[:summary][:location].must_equal "#{home}/mock/profiles/#{profile_id}"
result[:summary][:profile].must_equal 'complete'
result[:summary][:controls].must_equal 1
result[:errors].length.must_equal 0
result[:warnings].length.must_equal 0
end
end

describe 'shows error if version is invalid' do
let(:profile_id) { 'invalid-version' }
let(:profile_path) { MockLoader.profile_zip(profile_id) }
let(:profile) { MockLoader.load_profile(profile_path, {logger: logger}) }

it 'prints ok messages and counts the controls' do
logger.expect :info, nil, ["Checking profile in #{home}/mock/profiles/#{profile_id}"]
logger.expect :warn, nil, ['No controls or tests were defined.']
logger.expect :error, nil, ['Version needs to be in SemVer format']

result = MockLoader.load_profile(profile_id, {logger: logger}).check

# verify logger output
logger.verify

# verify hash result
result[:summary][:valid].must_equal false
result[:summary][:location].must_equal "#{home}/mock/profiles/#{profile_id}"
result[:summary][:profile].must_equal 'invalid-version'

result[:summary][:controls].must_equal 0
result[:errors].length.must_equal 1
result[:warnings].length.must_equal 1
end
end
end
end