From 9e1c9862bdd8a43d35def30c8f3990de5574fcf6 Mon Sep 17 00:00:00 2001 From: Alex Pop Date: Mon, 4 Apr 2016 13:48:42 +0100 Subject: [PATCH 1/2] extended gordon_config with more examples --- examples/profile/README.md | 4 ++-- examples/profile/controls/gordon.rb | 14 +++++++++++- examples/profile/libraries/gordon_config.rb | 24 +++++++++++++++------ 3 files changed, 32 insertions(+), 10 deletions(-) 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 From 070c5bb0e932d4c562fee0ebab40f86c0b8b46b7 Mon Sep 17 00:00:00 2001 From: Alex Pop Date: Mon, 4 Apr 2016 14:19:13 +0100 Subject: [PATCH 2/2] update tests with 5 examples --- profile.tar.gz | Bin 0 -> 2612 bytes test/functional/inspec_exec_test.rb | 10 +++++----- 2 files changed, 5 insertions(+), 5 deletions(-) create mode 100644 profile.tar.gz diff --git a/profile.tar.gz b/profile.tar.gz new file mode 100644 index 0000000000000000000000000000000000000000..b9bc9a7eddbeb43e0792484c15039a9f47bd0f10 GIT binary patch literal 2612 zcmV-43d{8$iwFRfX#!UO1MFIDbK5o&&S(CLjoca9Ig=D+S#C8+Tc7PTzHzS|+wD!K z%_tBFSs0T91AunaO#b`rf*?itwRf%ExvODoBms-XVi$b&1xjRLl+5(A2WLgEB4>Uzi03$O8kr4e+_t%$W#<(W2nq5_}p8AM3xIB z&gNze1GL2^?9KTMPUOs7GQ~IHut-8A%S6Hp&Bw5tWnk$wD6YA>;Hl>sVKNSWWbzV9 zC1)bzzK63GvI3T-&>TFNbHzRP4SGl(#9_Y&sf1hF8tg(@Memw` z^D1XWfV3%_;xs!MnWS7N(AzCQmPW{eWwu8N`z;K^UWduP9hq{toG)sb@W0?ZMfIX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gEGnzSYgVFb&>ug&EOX0FT5Iurf6WQye)UctvtO@}Qw`L~oZ{hG?I z@d{iFXTx_pbHffRZYouPJ{RSr)n;#x6}~~~Fc-Gdf9b7#w39yl((BRf*K^C(a{J2j z>RT2{crn?5nZlDHiVGAyg`al6J!;B@mTt<=MB&u9+!B$687gVLX6$Bcc}iu${h zXi@a*bs-E5waVw`j!E7k)0&lT6nK@6_EXcjTHOkXs-_D&>L35`_n+YK1l}ARAHeB% z_nMj^&VNmMuru^mzGLhTnd|MIg)a5-s9cw?wt>4_ z<(<;DExT%}w%GcJaF?2HpsvT-1r1uWwNv+yI7w(cwB+mlrhi3>QsN#_IbbcuO?@0_ znRG!Pm&(Qlu!(>vSo!S4RI2VtH*;2QVd|82)t{QqFofAas2 z(v}asbL~yuK~;rC z#TPGJ7R%EYFP1-~p}Q@}a5EVKcrvGoHD$-|oHjV}gorRdGP2p>yPNc8DX-}QLoe?& z1}=JzJ`3%F*>r!f4YtaeYFM{MMkC2{9Eu0S;TP=~T8;vM4e#@o?I!f?Pix4sH*A~a z5Hpc1e}+RI@47lh&G4g-rYkGwAv$&@!Pf0o#}qEpA8gG?WsMr W|GV~Y00030{{sN3<${_3E&u>XaV`V^ literal 0 HcmV?d00001 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