2015-12-14 13:23:41 +00:00
|
|
|
require 'yaml'
|
|
|
|
|
2016-03-24 19:19:14 +00:00
|
|
|
# Custom resource based on the InSpec resource DSL
|
2015-12-14 13:23:41 +00:00
|
|
|
class GordonConfig < Inspec.resource(1)
|
|
|
|
name 'gordon_config'
|
|
|
|
|
2016-03-24 19:19:14 +00:00
|
|
|
desc "
|
|
|
|
Gordon's resource description ...
|
|
|
|
"
|
|
|
|
|
|
|
|
example "
|
|
|
|
describe gordon_config do
|
|
|
|
its('version') { should eq('1.0') }
|
2016-04-04 12:48:42 +00:00
|
|
|
its('file_size') { should > 1 }
|
2016-03-24 19:19:14 +00:00
|
|
|
end
|
|
|
|
"
|
|
|
|
|
|
|
|
# Load the configuration file on initialization
|
2015-12-14 13:23:41 +00:00
|
|
|
def initialize
|
2016-04-04 12:48:42 +00:00
|
|
|
@params = {}
|
2016-03-24 19:19:14 +00:00
|
|
|
@path = '/tmp/gordon/config.yaml'
|
2015-12-14 13:23:41 +00:00
|
|
|
@file = inspec.file(@path)
|
|
|
|
return skip_resource "Can't find file \"#{@path}\"" if !@file.file?
|
|
|
|
|
2016-03-24 19:19:14 +00:00
|
|
|
# Protect from invalid YAML content
|
|
|
|
begin
|
|
|
|
@params = YAML.load(@file.content)
|
2016-04-04 12:48:42 +00:00
|
|
|
# Add two extra matchers
|
|
|
|
@params['file_size'] = @file.size
|
|
|
|
@params['file_path'] = @path
|
|
|
|
@params['ruby'] = 'RUBY IS HERE TO HELP ME!'
|
2016-03-24 19:19:14 +00:00
|
|
|
rescue Exception
|
|
|
|
return skip_resource "#{@file}: #{$!}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-04 12:48:42 +00:00
|
|
|
# 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(',')
|
2015-12-14 13:23:41 +00:00
|
|
|
end
|
|
|
|
|
2016-03-24 19:19:14 +00:00
|
|
|
# Expose all parameters
|
2015-12-14 13:23:41 +00:00
|
|
|
def method_missing(name)
|
2016-04-04 12:48:42 +00:00
|
|
|
return @params[name.to_s]
|
2015-12-14 13:23:41 +00:00
|
|
|
end
|
|
|
|
end
|