diff --git a/spec/classes/puppet_facts_spec.rb b/spec/classes/puppet_facts_spec.rb index 3b6b781..acafc7b 100644 --- a/spec/classes/puppet_facts_spec.rb +++ b/spec/classes/puppet_facts_spec.rb @@ -77,47 +77,53 @@ :clientcert => 'my.client.cert', :fqdn => 'my.fq.hostname', :environment => 'production', - :puppetversion => Puppet.version + :puppetversion => Puppet.version }) end it { is_expected.to compile.with_all_deps } if Puppet.version.to_f >= 4.0 facterbasepath = '/opt/puppetlabs/facter' + facterbasepath_group = 'root' else facterbasepath = '/etc/facter' + facterbasepath_group = 'puppet' end context 'when fed no parameters' do - if Puppet.version.to_f >= 4.0 - # setting environemnt doesn't work in puppet 4 - it "should lay down #{facterbasepath}/facts.d/local.yaml" do - should contain_file("#{facterbasepath}/facts.d/local.yaml").with_content( - /facts for my.client.cert/ - ).with_content( - /FQDN my.fq.hostname/ - ).with_validate_cmd( - "/usr/bin/env ruby -ryaml -e \"YAML.load_file '%'\"" - )#.with_content( - # /Environment production/ - #) - end - else - it "should lay down #{facterbasepath}/facts.d/local.yaml" do - should contain_file("#{facterbasepath}/facts.d/local.yaml").with_content( - /facts for my.client.cert/ - ).with_content( - /FQDN my.fq.hostname/ - ).with_content( - /Environment production/ - ).with_validate_cmd( - "/usr/bin/env ruby -ryaml -e \"YAML.load_file '%'\"" - ) - end + it "should lay down #{facterbasepath}/facts.d/local.yaml" do + should contain_file("#{facterbasepath}/facts.d/local.yaml").with({ + :path=>"#{facterbasepath}/facts.d/local.yaml", + :ensure=>'file', + :owner=>'root', + :group=>"#{facterbasepath_group}", + :mode=>'0640' + }).with_content( + /custom facts for my.client.cert/ + ).with_content( + /FQDN my.fq.hostname/ + ).with_content( + /---/ + ).with_validate_cmd( + "/usr/bin/env ruby -ryaml -e \"YAML.load_file '%'\"" + ) + should contain_file("#{facterbasepath}/facts.d/local.yaml").with_content( + /Environment production/ + ) if Puppet.version.to_f < 4.0 # setting environemnt doesn't work in puppet 4 end end#no params context 'when the custom_facts parameter is properly set key values is string' do - let(:params) {{'custom_facts' => {'key1' => 'val1', 'key2' => 'val2'}}} - it 'should iterate through the hash and properly populate the local_facts.yaml file' do - should contain_file("#{facterbasepath}/facts.d/local.yaml").with_content( + let(:params) { {'custom_facts' => {'key1' => 'val1', 'key2' => 'val2'}} } + it "should lay down #{facterbasepath}/facts.d/local.yaml" do + should contain_file("#{facterbasepath}/facts.d/local.yaml").with({ + :path=>"#{facterbasepath}/facts.d/local.yaml", + :ensure=>'file', + :owner=>'root', + :group=>"#{facterbasepath_group}", + :mode=>'0640' + }).with_content( + /custom facts for my.client.cert/ + ).with_content( + /FQDN my.fq.hostname/ + ).with_content( /---/ ).with_content( /key1: val1/ @@ -126,12 +132,25 @@ ).with_validate_cmd( "/usr/bin/env ruby -ryaml -e \"YAML.load_file '%'\"" ) + should contain_file("#{facterbasepath}/facts.d/local.yaml").with_content( + /Environment production/ + ) if Puppet.version.to_f < 4.0 # setting environemnt doesn't work in puppet 4 end end#custom_facts set key values is string context 'when the custom_facts parameter is properly set key values is array' do - let(:params) {{'custom_facts' => {'key1' => [ 'val11', 'val12' ], 'key2' => [ 'val21', 'val22']}}} - it 'should iterate through the hash and properly populate the local_facts.yaml file' do - should contain_file("#{facterbasepath}/facts.d/local.yaml").with_content( + let(:params) { {'custom_facts' => {'key1' => [ 'val11', 'val12' ], 'key2' => [ 'val21', 'val22']}} } + it "should lay down #{facterbasepath}/facts.d/local.yaml" do + should contain_file("#{facterbasepath}/facts.d/local.yaml").with({ + :path=>"#{facterbasepath}/facts.d/local.yaml", + :ensure=>'file', + :owner=>'root', + :group=>"#{facterbasepath_group}", + :mode=>'0640' + }).with_content( + /custom facts for my.client.cert/ + ).with_content( + /FQDN my.fq.hostname/ + ).with_content( /---/ ).with_content( /key1:/ @@ -148,12 +167,25 @@ ).with_validate_cmd( "/usr/bin/env ruby -ryaml -e \"YAML.load_file '%'\"" ) + should contain_file("#{facterbasepath}/facts.d/local.yaml").with_content( + /Environment production/ + ) if Puppet.version.to_f < 4.0 # setting environemnt doesn't work in puppet 4 end end#custom_facts set key values is array context 'when the custom_facts parameter is properly set key values is hash' do - let(:params) {{'custom_facts' => {'key1' => { 'key11' => 'val11' }, 'key2' => { 'key21' => 'val21'}}}} - it 'should iterate through the hash and properly populate the local_facts.yaml file' do - should contain_file("#{facterbasepath}/facts.d/local.yaml").with_content( + let(:params) { {'custom_facts' => {'key1' => { 'key11' => 'val11' }, 'key2' => { 'key21' => 'val21'}}} } + it "should lay down #{facterbasepath}/facts.d/local.yaml" do + should contain_file("#{facterbasepath}/facts.d/local.yaml").with({ + :path=>"#{facterbasepath}/facts.d/local.yaml", + :ensure=>'file', + :owner=>'root', + :group=>"#{facterbasepath_group}", + :mode=>'0640' + }).with_content( + /custom facts for my.client.cert/ + ).with_content( + /FQDN my.fq.hostname/ + ).with_content( /---/ ).with_content( /key1:/ @@ -166,6 +198,9 @@ ).with_validate_cmd( "/usr/bin/env ruby -ryaml -e \"YAML.load_file '%'\"" ) + should contain_file("#{facterbasepath}/facts.d/local.yaml").with_content( + /Environment production/ + ) if Puppet.version.to_f < 4.0 # setting environemnt doesn't work in puppet 4 end end#custom_facts set key values is hash end diff --git a/spec/defines/fact_spec.rb b/spec/defines/fact_spec.rb index 2788a3f..db95fac 100644 --- a/spec/defines/fact_spec.rb +++ b/spec/defines/fact_spec.rb @@ -5,51 +5,6 @@ context 'input validation with type value is string' do let (:title) { 'my_fact'} let (:params) {{ 'value' => 'my_val'}} -# ['path'].each do |paths| -# context "when the #{paths} parameter is not an absolute path" do -# let (:params) {{ paths => 'foo' }} -# it 'should fail' do -# expect { subject }.to raise_error(Puppet::Error, /"foo" is not an absolute path/) -# end -# end -# end#absolute path - -# ['array'].each do |arrays| -# context "when the #{arrays} parameter is not an array" do -# let (:params) {{ arrays => 'this is a string'}} -# it 'should fail' do -# expect { subject }.to raise_error(Puppet::Error, /is not an Array./) -# end -# end -# end#arrays - -# ['bool'].each do |bools| -# context "when the #{bools} parameter is not an boolean" do -# let (:params) {{bools => "BOGON"}} -# it 'should fail' do -# expect { subject }.to raise_error(Puppet::Error, /"BOGON" is not a boolean. It looks to be a String/) -# end -# end -# end#bools - -# ['hash'].each do |hashes| -# context "when the #{hashes} parameter is not an hash" do -# let (:params) {{ hashes => 'this is a string'}} -# it 'should fail' do -# expect { subject }.to raise_error(Puppet::Error, /is not a Hash./) -# end -# end -# end#hashes - -# ['string'].each do |strings| -# context "when the #{strings} parameter is not a string" do -# let (:params) {{strings => false }} -# it 'should fail' do -# expect { subject }.to raise_error(Puppet::Error, /false is not a string./) -# end -# end -# end#strings - end#input validation with type value is string context 'input validation with type value is array' do @@ -101,19 +56,19 @@ it 'should lay down our fact file as expected (value is string))' do should contain_file("#{facterbasepath}/facts.d/my_fact.yaml").with({ :path=>"#{facterbasepath}/facts.d/my_fact.yaml", - :ensure=>"present", - :owner=>"root", - :group=>"puppet", - :mode=>"0640" + :ensure=>'present', + :owner=>'root', + :group=>"#{facterbasepath_group}", + :mode=>'0640' }).with_content( - /# custom fact my_fact/ - ).with_content( - /---/ - ).with_content( - /my_fact: my_val/ - ).with_validate_cmd( - "/usr/bin/env ruby -ryaml -e \"YAML.load_file '%'\"" - ) + /# custom fact my_fact/ + ).with_content( + /---/ + ).with_content( + /my_fact: my_val/ + ).with_validate_cmd( + "/usr/bin/env ruby -ryaml -e \"YAML.load_file '%'\"" + ) end end context 'when fed no parameters (value is array)' do @@ -122,10 +77,10 @@ it 'should lay down our fact file as expected (value is array))' do should contain_file("#{facterbasepath}/facts.d/my_fact.yaml").with({ :path=>"#{facterbasepath}/facts.d/my_fact.yaml", - :ensure=>"present", - :owner=>"root", - :group=>"puppet", - :mode=>"0640" + :ensure=>'present', + :owner=>'root', + :group=>"#{facterbasepath_group}", + :mode=>'0640' }).with_content( /# custom fact my_fact/ ).with_content( @@ -147,23 +102,23 @@ it 'should lay down our fact file as expected (value is hash))' do should contain_file("#{facterbasepath}/facts.d/my_fact.yaml").with({ :path=>"#{facterbasepath}/facts.d/my_fact.yaml", - :ensure=>"present", - :owner=>"root", + :ensure=>'present', + :owner=>'root', :group=>"#{facterbasepath_group}", - :mode=>"0640" + :mode=>'0640' }).with_content( - /# custom fact my_fact/ - ).with_content( - /---/ - ).with_content( - /my_fact:/ - ).with_content( - /my_key0: my_val0/ - ).with_content( - /my_key1: my_val1/ - ).with_validate_cmd( - "/usr/bin/env ruby -ryaml -e \"YAML.load_file '%'\"" - ) + /# custom fact my_fact/ + ).with_content( + /---/ + ).with_content( + /my_fact:/ + ).with_content( + /my_key0: my_val0/ + ).with_content( + /my_key1: my_val1/ + ).with_validate_cmd( + "/usr/bin/env ruby -ryaml -e \"YAML.load_file '%'\"" + ) end end