diff --git a/examples/profile/README.md b/examples/profile/README.md index 563010d97..b1ffbc675 100644 --- a/examples/profile/README.md +++ b/examples/profile/README.md @@ -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 diff --git a/examples/profile/controls/gordon.rb b/examples/profile/controls/gordon.rb index cf68aa202..1167e6a12 100644 --- a/examples/profile/controls/gordon.rb +++ b/examples/profile/controls/gordon.rb @@ -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 diff --git a/examples/profile/libraries/gordon_config.rb b/examples/profile/libraries/gordon_config.rb index 57483a827..26b7ab49e 100644 --- a/examples/profile/libraries/gordon_config.rb +++ b/examples/profile/libraries/gordon_config.rb @@ -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 diff --git a/profile.tar.gz b/profile.tar.gz new file mode 100644 index 000000000..b9bc9a7ed Binary files /dev/null and b/profile.tar.gz differ diff --git a/test/functional/inspec_exec_test.rb b/test/functional/inspec_exec_test.rb index 850fd747c..ce01f934b 100644 --- a/test/functional/inspec_exec_test.rb +++ b/test/functional/inspec_exec_test.rb @@ -12,7 +12,7 @@ describe 'inspec exec' do out.stderr.must_equal '' out.exit_status.must_equal 0 out.stdout.must_match /^Pending: / - out.stdout.must_include '4 examples, 0 failures, 1 pending' + out.stdout.must_include '5 examples, 0 failures, 1 pending' end it 'executes only specified controls' do @@ -36,8 +36,8 @@ describe 'inspec exec' do let(:ex2) { examples.find{|x| x['id'] =~ /generated/} } let(:ex3) { examples.find{|x| x['id'] == 'gordon-1.0'} } - it 'must have 4 examples' do - json['examples'].length.must_equal 4 + it 'must have 5 examples' do + json['examples'].length.must_equal 5 end it 'id in json' do @@ -82,8 +82,8 @@ describe 'inspec exec' do }) end - it 'must have 4 examples' do - json['examples'].length.must_equal 4 + it 'must have 5 examples' do + json['examples'].length.must_equal 5 end it 'id in json' do