2
0
Fork 0
mirror of https://github.com/inspec/inspec synced 2024-12-28 14:03:14 +00:00
inspec/examples/profile/libraries/gordon_config.rb

60 lines
1.3 KiB
Ruby
Raw Normal View History

2015-12-14 13:23:41 +00:00
require 'yaml'
# Custom resource based on the InSpec resource DSL
2015-12-14 13:23:41 +00:00
class GordonConfig < Inspec.resource(1)
name 'gordon_config'
supports platform: 'unix'
supports platform: 'windows'
desc "
Gordon's resource description ...
"
example "
describe gordon_config do
its('version') { should eq('1.0') }
its('file_size') { should > 1 }
end
"
# Load the configuration file on initialization
2015-12-14 13:23:41 +00:00
def initialize
@params = {}
@path = '/tmp/gordon/config.yaml'
2015-12-14 13:23:41 +00:00
@file = inspec.file(@path)
unless @file.file?
raise Inspec::Exceptions::ResourceSkipped, "Can't find file `#{@path}`"
end
2015-12-14 13:23:41 +00:00
# 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 StandardError => e
raise Inspec::Exceptions::ResourceSkipped, "#{@file}: #{e.message}"
end
end
# Example method called by 'it { should exist }'
# Returns true or false from the 'File.exist?' method
def exists?
File.exist?(@path)
end
# Example matcher for the number of commas in the file
def comma_count
text = @file.content
text.count(',')
2015-12-14 13:23:41 +00:00
end
# Expose all parameters
2015-12-14 13:23:41 +00:00
def method_missing(name)
@params[name.to_s]
2015-12-14 13:23:41 +00:00
end
end