mirror of
https://github.com/inspec/inspec
synced 2024-11-23 21:23:29 +00:00
Merge pull request #610 from chef/ap/super_gordon
Extended gordon_config with more examples
This commit is contained in:
commit
4ab190afa6
5 changed files with 37 additions and 15 deletions
|
@ -12,7 +12,7 @@ Summary
|
|||
-------
|
||||
Location: examples/profile
|
||||
Profile: profile
|
||||
Controls: 3
|
||||
Controls: 4
|
||||
Timestamp: 2016-03-24T16:20:21+00:00
|
||||
Valid: true
|
||||
|
||||
|
@ -32,7 +32,7 @@ $ inspec exec examples/profile
|
|||
..
|
||||
|
||||
Finished in 0.0025 seconds (files took 0.12449 seconds to load)
|
||||
4 examples, 0 failures
|
||||
8 examples, 0 failures
|
||||
```
|
||||
|
||||
## Execute a specific control from a profile
|
||||
|
|
|
@ -18,8 +18,20 @@ control 'gordon-1.0' do
|
|||
tag 'gordon'
|
||||
ref 'Gordon Requirements 1.0', uri: 'http://...'
|
||||
|
||||
# Test using the custom gordon_config Inspec resource
|
||||
# Find the resource content here: ../libraries/
|
||||
describe gordon_config do
|
||||
it { should exist }
|
||||
its('version') { should eq('1.0') }
|
||||
its('size') { should <= 20 }
|
||||
its('file_size') { should <= 20 }
|
||||
its('comma_count') { should eq 0 }
|
||||
end
|
||||
|
||||
# Test the version again to showcase variables
|
||||
g = gordon_config
|
||||
g_path = g.file_path
|
||||
g_version = g.version
|
||||
describe file(g_path) do
|
||||
its('content') { should match g_version }
|
||||
end
|
||||
end
|
||||
|
|
|
@ -11,12 +11,13 @@ class GordonConfig < Inspec.resource(1)
|
|||
example "
|
||||
describe gordon_config do
|
||||
its('version') { should eq('1.0') }
|
||||
its('size') { should > 1 }
|
||||
its('file_size') { should > 1 }
|
||||
end
|
||||
"
|
||||
|
||||
# Load the configuration file on initialization
|
||||
def initialize
|
||||
@params = {}
|
||||
@path = '/tmp/gordon/config.yaml'
|
||||
@file = inspec.file(@path)
|
||||
return skip_resource "Can't find file \"#{@path}\"" if !@file.file?
|
||||
|
@ -24,20 +25,29 @@ class GordonConfig < Inspec.resource(1)
|
|||
# Protect from invalid YAML content
|
||||
begin
|
||||
@params = YAML.load(@file.content)
|
||||
# Add two extra matchers
|
||||
@params['file_size'] = @file.size
|
||||
@params['file_path'] = @path
|
||||
@params['ruby'] = 'RUBY IS HERE TO HELP ME!'
|
||||
rescue Exception
|
||||
return skip_resource "#{@file}: #{$!}"
|
||||
end
|
||||
add_some_extra_params
|
||||
end
|
||||
|
||||
# Extra Ruby helper method
|
||||
def add_some_extra_params
|
||||
@params['size'] = @file.size
|
||||
@params['md5sum'] = @file.md5sum
|
||||
# Example method called by 'it { should exist }'
|
||||
# Returns true or false from the 'File.exists?' method
|
||||
def exists?
|
||||
return File.exists?(@path)
|
||||
end
|
||||
|
||||
# Example matcher for the number of commas in the file
|
||||
def comma_count
|
||||
text = @file.content
|
||||
return text.count(',')
|
||||
end
|
||||
|
||||
# Expose all parameters
|
||||
def method_missing(name)
|
||||
@params[name.to_s]
|
||||
return @params[name.to_s]
|
||||
end
|
||||
end
|
||||
|
|
BIN
profile.tar.gz
Normal file
BIN
profile.tar.gz
Normal file
Binary file not shown.
|
@ -12,7 +12,7 @@ describe 'inspec exec' do
|
|||
out.stderr.must_equal ''
|
||||
out.exit_status.must_equal 0
|
||||
out.stdout.must_match /^Pending: /
|
||||
out.stdout.must_include '4 examples, 0 failures, 1 pending'
|
||||
out.stdout.must_include '5 examples, 0 failures, 1 pending'
|
||||
end
|
||||
|
||||
it 'executes only specified controls' do
|
||||
|
@ -36,8 +36,8 @@ describe 'inspec exec' do
|
|||
let(:ex2) { examples.find{|x| x['id'] =~ /generated/} }
|
||||
let(:ex3) { examples.find{|x| x['id'] == 'gordon-1.0'} }
|
||||
|
||||
it 'must have 4 examples' do
|
||||
json['examples'].length.must_equal 4
|
||||
it 'must have 5 examples' do
|
||||
json['examples'].length.must_equal 5
|
||||
end
|
||||
|
||||
it 'id in json' do
|
||||
|
@ -82,8 +82,8 @@ describe 'inspec exec' do
|
|||
})
|
||||
end
|
||||
|
||||
it 'must have 4 examples' do
|
||||
json['examples'].length.must_equal 4
|
||||
it 'must have 5 examples' do
|
||||
json['examples'].length.must_equal 5
|
||||
end
|
||||
|
||||
it 'id in json' do
|
||||
|
|
Loading…
Reference in a new issue