diff --git a/examples/inheritance/controls/example.rb b/examples/inheritance/controls/example.rb index dc82d9f6e..9a1372f37 100644 --- a/examples/inheritance/controls/example.rb +++ b/examples/inheritance/controls/example.rb @@ -1,5 +1,5 @@ # encoding: utf-8 -# copyright: 2015, Chef Software, Inc. +# copyright: 2016, Chef Software, Inc. # license: All rights reserved include_controls 'profile' do diff --git a/examples/profile/README.md b/examples/profile/README.md index aec07dad4..563010d97 100644 --- a/examples/profile/README.md +++ b/examples/profile/README.md @@ -8,23 +8,41 @@ InSpec ships with built-in features to verify a profile structure. ```bash $ inspec check examples/profile -I, [2015-11-21T12:44:50.851137 #20661] INFO -- : Checking profile in examples/profile -I, [2015-11-21T12:44:50.851216 #20661] INFO -- : Metadata OK. -D, [2015-11-21T12:44:50.851239 #20661] DEBUG -- : Found 2 rules. -D, [2015-11-21T12:44:50.851251 #20661] DEBUG -- : Verify all rules in examples/profile/controls/example_spec.rb -D, [2015-11-21T12:44:50.851263 #20661] DEBUG -- : Verify all rules in examples/profile/controls/gordon_spec.rb -I, [2015-11-21T12:44:50.851317 #20661] INFO -- : Rule definitions OK. +Summary +------- +Location: examples/profile +Profile: profile +Controls: 3 +Timestamp: 2016-03-24T16:20:21+00:00 +Valid: true + +Errors +------ + +Warnings +-------- ``` ## Execute a profile -To run a profile on a local machine use `inspec exec /path/to/profile`. +To run all **supported** controls on a local machine use `inspec exec /path/to/profile`. ```bash $ inspec exec examples/profile .. Finished in 0.0025 seconds (files took 0.12449 seconds to load) -2 examples, 0 failures - +4 examples, 0 failures +``` + +## Execute a specific control from a profile + +To run one control from the profile use `inspec exec /path/to/profile --controls name`. + +```bash +$ inspec exec examples/profile --controls tmp-1.0 +. + +Finished in 0.0025 seconds (files took 0.12449 seconds to load) +1 examples, 0 failures ``` diff --git a/examples/profile/controls/gordon.rb b/examples/profile/controls/gordon.rb index 18afcc63b..e56370895 100644 --- a/examples/profile/controls/gordon.rb +++ b/examples/profile/controls/gordon.rb @@ -1,12 +1,13 @@ # encoding: utf-8 -# copyright: 2015, Chef Software, Inc. +# copyright: 2016, Chef Software, Inc. # license: All rights reserved title 'Gordon Config Checks' # To pass the test, create the following file # ```bash -# cat < /etc/gordon/config.yaml +# mkdir -p /tmp/gordon +# cat < /tmp/gordon/config.yaml # version: '1.0' # EOF # ``` @@ -16,5 +17,6 @@ control 'gordon-1.0' do desc 'An optional description...' describe gordon_config do its('version') { should eq('1.0') } + its('size') { should <= 20 } end end diff --git a/examples/profile/libraries/gordon_config.rb b/examples/profile/libraries/gordon_config.rb index 262a5b5e5..5f7cda9d1 100644 --- a/examples/profile/libraries/gordon_config.rb +++ b/examples/profile/libraries/gordon_config.rb @@ -1,16 +1,42 @@ require 'yaml' +# Custom resource based on the InSpec resource DSL class GordonConfig < Inspec.resource(1) name 'gordon_config' + desc " + Gordon's resource description ... + " + + example " + describe gordon_config do + its('version') { should eq('1.0') } + its('size') { should > 1 } + end + " + + # Load the configuration file on initialization def initialize - @path = '/etc/gordon/config.yaml' + @path = '/tmp/gordon/config.yaml' @file = inspec.file(@path) return skip_resource "Can't find file \"#{@path}\"" if !@file.file? - @params = YAML.load(@file.content) + # Protect from invalid YAML content + begin + @params = YAML.load(@file.content) + 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 + end + + # Expose all parameters def method_missing(name) @params[name.to_s] end