diff --git a/manifests/fact.pp b/manifests/fact.pp index 3298d24..8b678ca 100644 --- a/manifests/fact.pp +++ b/manifests/fact.pp @@ -4,12 +4,15 @@ # define puppet::fact ( - $value, $ensure = present, - ) { + $value = undef, +) { include ::puppet::defaults $facterbasepath = $::puppet::defaults::facterbasepath + validate_re($title, '^[0-9A-Za-z_\-]+$', 'The $title fact does not match ^[0-9A-Za-z_\-]+$') + $facter_data = { "${title}" => $value } + file { "${facterbasepath}/facts.d/${title}.yaml": ensure => $ensure, owner => 'root', diff --git a/manifests/facts.pp b/manifests/facts.pp index 9f1ce3d..feaf89c 100644 --- a/manifests/facts.pp +++ b/manifests/facts.pp @@ -4,9 +4,8 @@ # # setup facts # - class puppet::facts ( - $custom_facts = undef + $custom_facts = undef, ) { include ::puppet include ::puppet::defaults diff --git a/spec/classes/puppet_facts_spec.rb b/spec/classes/puppet_facts_spec.rb index 581cbac..d78134d 100644 --- a/spec/classes/puppet_facts_spec.rb +++ b/spec/classes/puppet_facts_spec.rb @@ -77,49 +77,124 @@ :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_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/ - ) - 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( + /---/ + ) + 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' do - let(:params) {{'custom_facts' => {'key1' => 'val1', 'key2' => 'val2'}}} - it 'should iterate through the hash and properly populate the local_facts.yaml file' do + context 'when the custom_facts parameter is properly set key values is string' do + 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/ + ).with_content( + /key2: val2/ + ) should contain_file("#{facterbasepath}/facts.d/local.yaml").with_content( - /key1: \"val1\"/ + /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 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:/ ).with_content( - /key2: \"val2\"/ + /- val11/ + ).with_content( + /- val12/ + ).with_content( + /key2:/ + ).with_content( + /- val21/ + ).with_content( + /- val22/ + ) + 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 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:/ + ).with_content( + /key11: val11/ + ).with_content( + /key2:/ + ).with_content( + /key21: val21/ ) + 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 + end#custom_facts set key values is hash end end end diff --git a/spec/defines/fact_spec.rb b/spec/defines/fact_spec.rb index d7831a3..bb753bb 100644 --- a/spec/defines/fact_spec.rb +++ b/spec/defines/fact_spec.rb @@ -2,55 +2,21 @@ require 'spec_helper' describe 'puppet::fact', :type => :define do - context 'input validation' do + 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 + end#input validation with type value is string -# ['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 + context 'input validation with type value is array' do + let (:title) { 'my_fact'} + let (:params) {{ 'value' => ['my_val0', 'my_val1']}} + end#input validation with type value is array -# ['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 + context 'input validation with type value is hash' do + let (:title) { 'my_fact'} + let (:params) {{ 'value' => {'my_key0' => 'my_val0', 'my_key1' => 'my_val1'}}} + end#input validation with type value is hash - end#input validation on_supported_os({ :hardwaremodels => ['x86_64'], :supported_os => [ @@ -84,19 +50,71 @@ facterbasepath = '/etc/facter' facterbasepath_group = 'puppet' end - context 'when fed no parameters' do + context 'when fed no parameters (value is string)' do let (:title) { 'my_fact'} let (:params) {{'value' => 'my_val'}} - it 'should lay down our fact file as expected' do + 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", + :ensure=>'present', + :owner=>'root', :group=>"#{facterbasepath_group}", - :mode=>"0640" - }).with_content("# custom fact my_fact\n---\nmy_fact: \"my_val\"\n") + :mode=>'0640' + }).with_content( + /# custom fact my_fact/ + ).with_content( + /---/ + ).with_content( + /my_fact: my_val/ + ) end - end#no params + end + context 'when fed no parameters (value is array)' do + let (:title) { 'my_fact'} + let (:params) {{'value' => ['my_val0', 'my_val1']}} + 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=>"#{facterbasepath_group}", + :mode=>'0640' + }).with_content( + /# custom fact my_fact/ + ).with_content( + /---/ + ).with_content( + /my_fact:/ + ).with_content( + /- my_val0/ + ).with_content( + /- my_val1/ + ) + end + end + context 'when fed no parameters (value is hash)' do + let (:title) { 'my_fact'} + let (:params) {{'value' => {'my_key0' => 'my_val0', 'my_key1' => 'my_val1'}}} + 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', + :group=>"#{facterbasepath_group}", + :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/ + ) + end + end end end diff --git a/templates/fact.yaml.erb b/templates/fact.yaml.erb index 9477236..4294a25 100644 --- a/templates/fact.yaml.erb +++ b/templates/fact.yaml.erb @@ -1,3 +1,3 @@ # custom fact <%= @title %> ---- -<%= @title %>: "<%= @value %>" +<%- require 'yaml' -%> +<%= YAML.dump(@facter_data) %> diff --git a/templates/local_facts.yaml.erb b/templates/local_facts.yaml.erb index 6af6174..331924b 100644 --- a/templates/local_facts.yaml.erb +++ b/templates/local_facts.yaml.erb @@ -1,9 +1,5 @@ # custom facts for <%= @clientcert %> # FQDN <%= @fqdn %> # Environment <%= @environment %> ---- -<%- if @custom_facts -%> -<%- @custom_facts.keys.sort.each do |key| -%> -<%= key %>: "<%= @custom_facts[key] %>" -<%- end -%> -<%- end -%> +<%- require 'yaml' -%> +<%= YAML.dump(@custom_facts) %>