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

View file

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

View file

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