2019-06-11 22:24:35 +00:00
|
|
|
require "yaml"
|
2015-12-14 13:23:41 +00:00
|
|
|
|
2016-03-24 19:19:14 +00:00
|
|
|
# Custom resource based on the InSpec resource DSL
|
2019-10-09 07:08:28 +00:00
|
|
|
class ExampleConfig < Inspec.resource(1)
|
|
|
|
name "example_config"
|
2015-12-14 13:23:41 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
supports platform: "unix"
|
|
|
|
supports platform: "windows"
|
2018-04-05 12:50:49 +00:00
|
|
|
|
2016-03-24 19:19:14 +00:00
|
|
|
desc "
|
2019-10-09 07:08:28 +00:00
|
|
|
Example's resource description ...
|
2016-03-24 19:19:14 +00:00
|
|
|
"
|
|
|
|
|
|
|
|
example "
|
2019-10-09 07:08:28 +00:00
|
|
|
describe example_config do
|
2016-03-24 19:19:14 +00:00
|
|
|
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 = {}
|
2019-10-09 07:08:28 +00:00
|
|
|
@path = "/tmp/example/config.yaml"
|
2015-12-14 13:23:41 +00:00
|
|
|
@file = inspec.file(@path)
|
2018-04-05 12:50:49 +00:00
|
|
|
|
|
|
|
unless @file.file?
|
|
|
|
raise Inspec::Exceptions::ResourceSkipped, "Can't find file `#{@path}`"
|
|
|
|
end
|
2015-12-14 13:23:41 +00:00
|
|
|
|
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
|
2019-06-11 22:24:35 +00:00
|
|
|
@params["file_size"] = @file.size
|
|
|
|
@params["file_path"] = @path
|
|
|
|
@params["ruby"] = "RUBY IS HERE TO HELP ME!"
|
2018-04-05 12:50:49 +00:00
|
|
|
rescue StandardError => e
|
|
|
|
raise Inspec::Exceptions::ResourceSkipped, "#{@file}: #{e.message}"
|
2016-03-24 19:19:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-04 12:48:42 +00:00
|
|
|
# Example method called by 'it { should exist }'
|
2018-04-05 12:50:49 +00:00
|
|
|
# Returns true or false from the 'File.exist?' method
|
2016-04-04 12:48:42 +00:00
|
|
|
def exists?
|
2018-04-05 12:50:49 +00:00
|
|
|
File.exist?(@path)
|
2016-04-04 12:48:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Example matcher for the number of commas in the file
|
|
|
|
def comma_count
|
|
|
|
text = @file.content
|
2019-06-11 22:24:35 +00:00
|
|
|
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)
|
2018-04-05 12:50:49 +00:00
|
|
|
@params[name.to_s]
|
2015-12-14 13:23:41 +00:00
|
|
|
end
|
|
|
|
end
|