mirror of
https://github.com/inspec/inspec
synced 2025-02-25 11:57:17 +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
|
Location: examples/profile
|
||||||
Profile: profile
|
Profile: profile
|
||||||
Controls: 3
|
Controls: 4
|
||||||
Timestamp: 2016-03-24T16:20:21+00:00
|
Timestamp: 2016-03-24T16:20:21+00:00
|
||||||
Valid: true
|
Valid: true
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ $ inspec exec examples/profile
|
||||||
..
|
..
|
||||||
|
|
||||||
Finished in 0.0025 seconds (files took 0.12449 seconds to load)
|
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
|
## Execute a specific control from a profile
|
||||||
|
|
|
@ -18,8 +18,20 @@ control 'gordon-1.0' do
|
||||||
tag 'gordon'
|
tag 'gordon'
|
||||||
ref 'Gordon Requirements 1.0', uri: 'http://...'
|
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
|
describe gordon_config do
|
||||||
|
it { should exist }
|
||||||
its('version') { should eq('1.0') }
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -11,12 +11,13 @@ class GordonConfig < Inspec.resource(1)
|
||||||
example "
|
example "
|
||||||
describe gordon_config do
|
describe gordon_config do
|
||||||
its('version') { should eq('1.0') }
|
its('version') { should eq('1.0') }
|
||||||
its('size') { should > 1 }
|
its('file_size') { should > 1 }
|
||||||
end
|
end
|
||||||
"
|
"
|
||||||
|
|
||||||
# Load the configuration file on initialization
|
# Load the configuration file on initialization
|
||||||
def initialize
|
def initialize
|
||||||
|
@params = {}
|
||||||
@path = '/tmp/gordon/config.yaml'
|
@path = '/tmp/gordon/config.yaml'
|
||||||
@file = inspec.file(@path)
|
@file = inspec.file(@path)
|
||||||
return skip_resource "Can't find file \"#{@path}\"" if !@file.file?
|
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
|
# Protect from invalid YAML content
|
||||||
begin
|
begin
|
||||||
@params = YAML.load(@file.content)
|
@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
|
rescue Exception
|
||||||
return skip_resource "#{@file}: #{$!}"
|
return skip_resource "#{@file}: #{$!}"
|
||||||
end
|
end
|
||||||
add_some_extra_params
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Extra Ruby helper method
|
# Example method called by 'it { should exist }'
|
||||||
def add_some_extra_params
|
# Returns true or false from the 'File.exists?' method
|
||||||
@params['size'] = @file.size
|
def exists?
|
||||||
@params['md5sum'] = @file.md5sum
|
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
|
end
|
||||||
|
|
||||||
# Expose all parameters
|
# Expose all parameters
|
||||||
def method_missing(name)
|
def method_missing(name)
|
||||||
@params[name.to_s]
|
return @params[name.to_s]
|
||||||
end
|
end
|
||||||
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.stderr.must_equal ''
|
||||||
out.exit_status.must_equal 0
|
out.exit_status.must_equal 0
|
||||||
out.stdout.must_match /^Pending: /
|
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
|
end
|
||||||
|
|
||||||
it 'executes only specified controls' do
|
it 'executes only specified controls' do
|
||||||
|
@ -36,8 +36,8 @@ describe 'inspec exec' do
|
||||||
let(:ex2) { examples.find{|x| x['id'] =~ /generated/} }
|
let(:ex2) { examples.find{|x| x['id'] =~ /generated/} }
|
||||||
let(:ex3) { examples.find{|x| x['id'] == 'gordon-1.0'} }
|
let(:ex3) { examples.find{|x| x['id'] == 'gordon-1.0'} }
|
||||||
|
|
||||||
it 'must have 4 examples' do
|
it 'must have 5 examples' do
|
||||||
json['examples'].length.must_equal 4
|
json['examples'].length.must_equal 5
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'id in json' do
|
it 'id in json' do
|
||||||
|
@ -82,8 +82,8 @@ describe 'inspec exec' do
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'must have 4 examples' do
|
it 'must have 5 examples' do
|
||||||
json['examples'].length.must_equal 4
|
json['examples'].length.must_equal 5
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'id in json' do
|
it 'id in json' do
|
||||||
|
|
Loading…
Add table
Reference in a new issue