extended gordon_config with more examples

This commit is contained in:
Alex Pop 2016-04-04 13:48:42 +01:00
parent 0cffb21b97
commit 9e1c9862bd
3 changed files with 32 additions and 10 deletions

View file

@ -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

View file

@ -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

View file

@ -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