From 7715c476af20db8b463eb55a0e06dab7bf8807c5 Mon Sep 17 00:00:00 2001 From: Mikhail Konyakhin Date: Tue, 2 Aug 2016 16:20:37 +0300 Subject: [PATCH 1/8] Corrected travis-ci testing on ruby 1.9.3. --- Gemfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Gemfile b/Gemfile index b3097be..88ee2b3 100644 --- a/Gemfile +++ b/Gemfile @@ -19,6 +19,8 @@ group :test do gem 'metadata-json-lint', :require => false gem 'simplecov', :require => false gem 'json', '1.8.3', :require => false + gem 'json_pure', '1.8.3', :require => false + gem 'rubocop', '0.41.2', :require => false gem "puppet-blacksmith", :require => false gem 'pry', '<= 0.9.8', :require => false gem 'puppet-lint', :require => false From 492e2b838b10004e6122634f171f0ea365511f78 Mon Sep 17 00:00:00 2001 From: Mikhail Konyakhin Date: Wed, 3 Aug 2016 14:58:39 +0300 Subject: [PATCH 2/8] Update version rspec-core and rspec-puppet. --- Gemfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index 88ee2b3..e4b0517 100644 --- a/Gemfile +++ b/Gemfile @@ -11,8 +11,8 @@ def location_for(place, fake_version = nil) end group :test do - gem "rspec-core", '3.1.7', :require => false - gem "rspec-puppet", '2.3.2', :require => false + gem "rspec-core", '3.5.1', :require => false + gem "rspec-puppet", '2.4.0', :require => false gem 'puppetlabs_spec_helper', :git => 'https://github.com/puppetlabs/puppetlabs_spec_helper', :require => false gem 'rspec-puppet-facts', '1.3.0', :require => false gem "puppet-syntax", :require => false From 0900994de929bbfd47e6b92c4c1584c2b98440d7 Mon Sep 17 00:00:00 2001 From: Mikhail Konyakhin Date: Wed, 3 Aug 2016 15:00:41 +0300 Subject: [PATCH 3/8] Add feature custom factor in any format (not only strings). --- manifests/fact.pp | 3 ++ spec/classes/puppet_facts_spec.rb | 46 +++++++++++++++++-- spec/defines/fact_spec.rb | 75 ++++++++++++++++++++++++++++--- templates/fact.yaml.erb | 8 +++- templates/local_facts.yaml.erb | 10 ++--- 5 files changed, 125 insertions(+), 17 deletions(-) diff --git a/manifests/fact.pp b/manifests/fact.pp index 209a220..5478fd3 100644 --- a/manifests/fact.pp +++ b/manifests/fact.pp @@ -10,6 +10,9 @@ include ::puppet::defaults $facterbasepath = $::puppet::defaults::facterbasepath + validate_string($title) + $facter_data = { "${title}" => $value } + file { "${facterbasepath}/facts.d/${title}.yaml": ensure => $ensure, owner => 'root', diff --git a/spec/classes/puppet_facts_spec.rb b/spec/classes/puppet_facts_spec.rb index 581cbac..137ce93 100644 --- a/spec/classes/puppet_facts_spec.rb +++ b/spec/classes/puppet_facts_spec.rb @@ -110,16 +110,54 @@ end end end#no params - context 'when the custom_facts parameter is properly set' 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 iterate through the hash and properly populate the local_facts.yaml file' do should contain_file("#{facterbasepath}/facts.d/local.yaml").with_content( - /key1: \"val1\"/ + /---/ ).with_content( - /key2: \"val2\"/ + /key1: val1/ + ).with_content( + /key2: val2/ + ) + 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( + /---/ + ).with_content( + /key1:/ + ).with_content( + /- val11/ + ).with_content( + /- val12/ + ).with_content( + /key2:/ + ).with_content( + /- val21/ + ).with_content( + /- val22/ + ) + 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( + /---/ + ).with_content( + /key1:/ + ).with_content( + /key11: val11/ + ).with_content( + /key2:/ + ).with_content( + /key21: val21/ ) 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 e441a1e..455ac72 100644 --- a/spec/defines/fact_spec.rb +++ b/spec/defines/fact_spec.rb @@ -2,7 +2,7 @@ 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| @@ -50,7 +50,18 @@ # end # end#strings - end#input validation + end#input validation with type value is string + + 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 + + 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 + on_supported_os({ :hardwaremodels => ['x86_64'], :supported_os => [ @@ -82,19 +93,71 @@ else facterbasepath = '/etc/facter' 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", + :group=>"puppet", + :mode=>"0640" + }).with_content( + /# custom fact my_fact/ + ).with_content( + /---/ + ).with_content( + /my_fact: my_val/ + ) + end + 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=>"puppet", + :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=>"puppet", :mode=>"0640" - }).with_content("# custom fact my_fact\n---\nmy_fact: \"my_val\"\n") + }).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#no params + end end end diff --git a/templates/fact.yaml.erb b/templates/fact.yaml.erb index 9477236..2441593 100644 --- a/templates/fact.yaml.erb +++ b/templates/fact.yaml.erb @@ -1,3 +1,7 @@ # custom fact <%= @title %> ---- -<%= @title %>: "<%= @value %>" +<%- require 'yaml' -%> +<%- if RUBY_VERSION =~ /^1\.8\./ -%> +<%= YAML::dump(@facter_data) %> +<%- else -%> +<%= @facter_data.to_yaml(indentation:2, canonical:false, line_width:-1) %> +<%- end -%> diff --git a/templates/local_facts.yaml.erb b/templates/local_facts.yaml.erb index 6af6174..9d2a6c7 100644 --- a/templates/local_facts.yaml.erb +++ b/templates/local_facts.yaml.erb @@ -1,9 +1,9 @@ # custom facts for <%= @clientcert %> # FQDN <%= @fqdn %> # Environment <%= @environment %> ---- -<%- if @custom_facts -%> -<%- @custom_facts.keys.sort.each do |key| -%> -<%= key %>: "<%= @custom_facts[key] %>" -<%- end -%> +<%- require 'yaml' -%> +<%- if RUBY_VERSION =~ /^1\.8\./ -%> +<%= YAML::dump(@custom_facts) %> +<%- else -%> +<%= @custom_facts.to_yaml(indentation:2, canonical:false, line_width:-1) %> <%- end -%> From 612d5cd4244ff0cceae1efefff7957b5c70ef9e3 Mon Sep 17 00:00:00 2001 From: Mikhail Konyakhin Date: Wed, 10 Aug 2016 11:16:13 +0300 Subject: [PATCH 4/8] Add validation yaml format of facter files. --- manifests/fact.pp | 11 ++++++----- manifests/facts.pp | 11 ++++++----- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/manifests/fact.pp b/manifests/fact.pp index 5478fd3..4e39e8a 100644 --- a/manifests/fact.pp +++ b/manifests/fact.pp @@ -14,11 +14,12 @@ $facter_data = { "${title}" => $value } file { "${facterbasepath}/facts.d/${title}.yaml": - ensure => $ensure, - owner => 'root', - group => 'puppet', - mode => '0640', - content => template('puppet/fact.yaml.erb'), + ensure => $ensure, + owner => 'root', + group => 'puppet', + mode => '0640', + validate_cmd => "/usr/bin/env ruby -ryaml -e \"YAML.load_file ''\"", + content => template('puppet/fact.yaml.erb'), } } diff --git a/manifests/facts.pp b/manifests/facts.pp index fb75659..e653545 100644 --- a/manifests/facts.pp +++ b/manifests/facts.pp @@ -17,11 +17,12 @@ } file { "${facterbasepath}/facts.d/local.yaml": - ensure => file, - owner => 'root', - group => 'puppet', - mode => '0640', - content => template('puppet/local_facts.yaml.erb'), + ensure => file, + owner => 'root', + group => 'puppet', + mode => '0640', + validate_cmd => "/usr/bin/env ruby -ryaml -e \"YAML.load_file ''\"", + content => template('puppet/local_facts.yaml.erb'), } } From a8f1a483bf41de6f25473ec35452d62217672d2b Mon Sep 17 00:00:00 2001 From: Mikhail Konyakhin Date: Thu, 11 Aug 2016 14:16:56 +0300 Subject: [PATCH 5/8] Fix puppetlabs_spec_helper to version 1.1.1. --- Gemfile | 2 +- Gemfile.lock | 131 +++++++++++++++++++++++++++------------------------ 2 files changed, 71 insertions(+), 62 deletions(-) diff --git a/Gemfile b/Gemfile index e4b0517..3c280ac 100644 --- a/Gemfile +++ b/Gemfile @@ -13,7 +13,7 @@ end group :test do gem "rspec-core", '3.5.1', :require => false gem "rspec-puppet", '2.4.0', :require => false - gem 'puppetlabs_spec_helper', :git => 'https://github.com/puppetlabs/puppetlabs_spec_helper', :require => false + gem 'puppetlabs_spec_helper', '1.1.1', :require => false gem 'rspec-puppet-facts', '1.3.0', :require => false gem "puppet-syntax", :require => false gem 'metadata-json-lint', :require => false diff --git a/Gemfile.lock b/Gemfile.lock index 5d90863..21441a1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,38 +1,27 @@ -GIT - remote: https://github.com/puppetlabs/puppetlabs_spec_helper - revision: ad39768d69f005c414da1ee9f12a0d350f8b57a3 - specs: - puppetlabs_spec_helper (1.0.1) - mocha - puppet-lint - puppet-syntax - rake - rspec-puppet - GEM remote: https://rubygems.org/ specs: CFPropertyList (2.2.8) addressable (2.4.0) - backports (3.6.7) + ast (2.3.0) + backports (3.6.8) coderay (1.0.9) diff-lcs (1.2.5) docile (1.1.5) - domain_name (0.5.25) + domain_name (0.5.20160615) unf (>= 0.0.5, < 1.0.0) - ethon (0.8.1) + ethon (0.9.0) ffi (>= 1.3.0) facter (2.4.6) CFPropertyList (~> 2.2.6) - facterdb (0.3.1) + facterdb (0.3.6) facter jgrep - json faraday (0.9.2) multipart-post (>= 1.2, < 3) faraday_middleware (0.10.0) faraday (>= 0.7.4, < 0.10) - ffi (1.9.10) + ffi (1.9.14) gh (0.14.0) addressable backports @@ -40,12 +29,11 @@ GEM multi_json (~> 1.0) net-http-persistent (>= 2.7) net-http-pipeline - hiera (3.0.6) - json_pure + hiera (3.2.1) highline (1.7.8) http-cookie (1.0.2) domain_name (~> 0.5) - jgrep (1.4.0) + jgrep (1.4.1) json json (1.8.3) json_pure (1.8.3) @@ -56,79 +44,97 @@ GEM json spdx-licenses (~> 1.0) method_source (0.8.2) - mime-types (2.99) + mime-types (2.99.2) mocha (1.1.0) metaclass (~> 0.0.1) - multi_json (1.11.2) + multi_json (1.12.1) multipart-post (2.0.0) net-http-persistent (2.9.4) net-http-pipeline (1.0.1) netrc (0.11.0) + parser (2.3.1.2) + ast (~> 2.2) + powerpack (0.1.1) pry (0.9.8) coderay (~> 1.0.5) method_source (~> 0.7) slop (>= 2.4.3, < 3) puppet (4.3.2) + CFPropertyList (~> 2.2.6) facter (> 2.0, < 4) hiera (>= 2.0, < 4) - json_pure - puppet-blacksmith (3.3.1) + json_pure (~> 1.8) + puppet-blacksmith (3.4.0) puppet (>= 2.7.16) - rest-client + rest-client (~> 1.8.0) puppet-lint (1.1.0) - puppet-lint-absolute_classname-check (0.1.3) - puppet-lint (~> 1.0) - puppet-lint-empty_string-check (0.2.1) - puppet-lint (~> 1.0) - puppet-lint-leading_zero-check (0.1.0) - puppet-lint (~> 1.0) + puppet-lint-absolute_classname-check (0.2.4) + puppet-lint (>= 1.0, < 3.0) + puppet-lint-empty_string-check (0.2.2) + puppet-lint (>= 1.0, < 3.0) + puppet-lint-leading_zero-check (0.1.1) + puppet-lint (>= 1.0, < 3.0) puppet-lint-roles_and_profiles-check (0.1.0) puppet-lint (~> 1.0) - puppet-lint-spaceship_operator_without_tag-check (0.1.0) - puppet-lint (~> 1.0) - puppet-lint-undef_in_function-check (0.1.0) - puppet-lint (~> 1.0) - puppet-lint-unquoted_string-check (0.2.5) - puppet-lint (~> 1.0) - puppet-lint-variable_contains_upcase (1.0.0) - puppet-lint (~> 1.0) + puppet-lint-spaceship_operator_without_tag-check (0.1.1) + puppet-lint (>= 1.0, < 3.0) + puppet-lint-undef_in_function-check (0.2.1) + puppet-lint (>= 1.0, < 3.0) + puppet-lint-unquoted_string-check (0.3.0) + puppet-lint (>= 1.0, < 3.0) + puppet-lint-variable_contains_upcase (1.0.1) + puppet-lint (>= 1.0, < 3.0) puppet-syntax (2.1.0) rake + puppetlabs_spec_helper (1.1.1) + mocha + puppet-lint + puppet-syntax + rake + rspec-puppet pusher-client (0.6.2) json websocket (~> 1.0) - rake (10.5.0) + rainbow (2.1.0) + rake (11.2.2) rest-client (1.8.0) http-cookie (>= 1.0.2, < 2.0) mime-types (>= 1.16, < 3.0) netrc (~> 0.7) - rspec (3.1.0) - rspec-core (~> 3.1.0) - rspec-expectations (~> 3.1.0) - rspec-mocks (~> 3.1.0) - rspec-core (3.1.7) - rspec-support (~> 3.1.0) - rspec-expectations (3.1.2) + rspec (3.5.0) + rspec-core (~> 3.5.0) + rspec-expectations (~> 3.5.0) + rspec-mocks (~> 3.5.0) + rspec-core (3.5.1) + rspec-support (~> 3.5.0) + rspec-expectations (3.5.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.1.0) - rspec-mocks (3.1.3) - rspec-support (~> 3.1.0) - rspec-puppet (2.3.2) + rspec-support (~> 3.5.0) + rspec-mocks (3.5.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.5.0) + rspec-puppet (2.4.0) rspec rspec-puppet-facts (1.3.0) facter facterdb (>= 0.3.0) json puppet - rspec-support (3.1.2) - simplecov (0.11.1) + rspec-support (3.5.0) + rubocop (0.41.2) + parser (>= 2.3.1.1, < 3.0) + powerpack (~> 0.1) + rainbow (>= 1.99.1, < 3.0) + ruby-progressbar (~> 1.7) + unicode-display_width (~> 1.0, >= 1.0.1) + ruby-progressbar (1.8.1) + simplecov (0.12.0) docile (~> 1.1.0) - json (~> 1.8) + json (>= 1.8, < 3) simplecov-html (~> 0.10.0) simplecov-html (0.10.0) slop (2.4.4) - spdx-licenses (1.0.0) - json + spdx-licenses (1.1.0) travis (1.8.2) backports faraday (~> 0.9) @@ -144,8 +150,9 @@ GEM ethon (>= 0.8.0) unf (0.1.4) unf_ext - unf_ext (0.0.7.1) - websocket (1.2.2) + unf_ext (0.0.7.2) + unicode-display_width (1.1.0) + websocket (1.2.3) PLATFORMS ruby @@ -154,6 +161,7 @@ DEPENDENCIES facter hiera json (= 1.8.3) + json_pure (= 1.8.3) metadata-json-lint pry (<= 0.9.8) puppet @@ -168,10 +176,11 @@ DEPENDENCIES puppet-lint-unquoted_string-check puppet-lint-variable_contains_upcase puppet-syntax - puppetlabs_spec_helper! - rspec-core (= 3.1.7) - rspec-puppet (= 2.3.2) + puppetlabs_spec_helper (= 1.1.1) + rspec-core (= 3.5.1) + rspec-puppet (= 2.4.0) rspec-puppet-facts (= 1.3.0) + rubocop (= 0.41.2) simplecov travis travis-lint From bcca8dd9afebe7acbdce498a08ff3838b6e5175f Mon Sep 17 00:00:00 2001 From: Mikhail Konyakhin Date: Thu, 11 Aug 2016 18:15:59 +0300 Subject: [PATCH 6/8] fix validate_cmd for yaml, update tests and add validate file name. --- manifests/fact.pp | 4 ++-- manifests/facts.pp | 2 +- spec/classes/puppet_facts_spec.rb | 10 ++++++++++ spec/defines/fact_spec.rb | 6 ++++++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/manifests/fact.pp b/manifests/fact.pp index 4e39e8a..ce5d3fd 100644 --- a/manifests/fact.pp +++ b/manifests/fact.pp @@ -10,7 +10,7 @@ include ::puppet::defaults $facterbasepath = $::puppet::defaults::facterbasepath - validate_string($title) + 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": @@ -18,7 +18,7 @@ owner => 'root', group => 'puppet', mode => '0640', - validate_cmd => "/usr/bin/env ruby -ryaml -e \"YAML.load_file ''\"", + validate_cmd => "/usr/bin/env ruby -ryaml -e \"YAML.load_file '%'\"", content => template('puppet/fact.yaml.erb'), } diff --git a/manifests/facts.pp b/manifests/facts.pp index e653545..97b8d33 100644 --- a/manifests/facts.pp +++ b/manifests/facts.pp @@ -21,7 +21,7 @@ owner => 'root', group => 'puppet', mode => '0640', - validate_cmd => "/usr/bin/env ruby -ryaml -e \"YAML.load_file ''\"", + validate_cmd => "/usr/bin/env ruby -ryaml -e \"YAML.load_file '%'\"", content => template('puppet/local_facts.yaml.erb'), } diff --git a/spec/classes/puppet_facts_spec.rb b/spec/classes/puppet_facts_spec.rb index 137ce93..3b6b781 100644 --- a/spec/classes/puppet_facts_spec.rb +++ b/spec/classes/puppet_facts_spec.rb @@ -94,6 +94,8 @@ /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/ #) @@ -106,6 +108,8 @@ /FQDN my.fq.hostname/ ).with_content( /Environment production/ + ).with_validate_cmd( + "/usr/bin/env ruby -ryaml -e \"YAML.load_file '%'\"" ) end end @@ -119,6 +123,8 @@ /key1: val1/ ).with_content( /key2: val2/ + ).with_validate_cmd( + "/usr/bin/env ruby -ryaml -e \"YAML.load_file '%'\"" ) end end#custom_facts set key values is string @@ -139,6 +145,8 @@ /- val21/ ).with_content( /- val22/ + ).with_validate_cmd( + "/usr/bin/env ruby -ryaml -e \"YAML.load_file '%'\"" ) end end#custom_facts set key values is array @@ -155,6 +163,8 @@ /key2:/ ).with_content( /key21: val21/ + ).with_validate_cmd( + "/usr/bin/env ruby -ryaml -e \"YAML.load_file '%'\"" ) end end#custom_facts set key values is hash diff --git a/spec/defines/fact_spec.rb b/spec/defines/fact_spec.rb index 455ac72..33a01ef 100644 --- a/spec/defines/fact_spec.rb +++ b/spec/defines/fact_spec.rb @@ -109,6 +109,8 @@ /---/ ).with_content( /my_fact: my_val/ + ).with_validate_cmd( + "/usr/bin/env ruby -ryaml -e \"YAML.load_file '%'\"" ) end end @@ -132,6 +134,8 @@ /- my_val0/ ).with_content( /- my_val1/ + ).with_validate_cmd( + "/usr/bin/env ruby -ryaml -e \"YAML.load_file '%'\"" ) end end @@ -155,6 +159,8 @@ /my_key0: my_val0/ ).with_content( /my_key1: my_val1/ + ).with_validate_cmd( + "/usr/bin/env ruby -ryaml -e \"YAML.load_file '%'\"" ) end end From 773b03742f30e4944d613684381bc471c819ca21 Mon Sep 17 00:00:00 2001 From: Mikhail Konyakhin Date: Tue, 4 Apr 2017 17:09:55 +0300 Subject: [PATCH 7/8] Fix tests. --- spec/classes/puppet_facts_spec.rb | 105 +++++++++++++++++++---------- spec/defines/fact_spec.rb | 107 +++++++++--------------------- 2 files changed, 101 insertions(+), 111 deletions(-) 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 From 5d4054eda33c3e9bb059e95773f1ac43de4e17a8 Mon Sep 17 00:00:00 2001 From: Mikhail Konyakhin Date: Fri, 19 May 2017 11:32:02 +0300 Subject: [PATCH 8/8] Remove validate command. Not needed. --- manifests/fact.pp | 11 +++++------ manifests/facts.pp | 11 +++++------ spec/classes/puppet_facts_spec.rb | 8 -------- spec/defines/fact_spec.rb | 6 ------ 4 files changed, 10 insertions(+), 26 deletions(-) diff --git a/manifests/fact.pp b/manifests/fact.pp index 00edd32..8b678ca 100644 --- a/manifests/fact.pp +++ b/manifests/fact.pp @@ -14,12 +14,11 @@ $facter_data = { "${title}" => $value } file { "${facterbasepath}/facts.d/${title}.yaml": - ensure => $ensure, - owner => 'root', - group => $::puppet::defaults::puppet_group, - mode => '0640', - validate_cmd => "/usr/bin/env ruby -ryaml -e \"YAML.load_file '%'\"", - content => template('puppet/fact.yaml.erb'), + ensure => $ensure, + owner => 'root', + group => $::puppet::defaults::puppet_group, + mode => '0640', + content => template('puppet/fact.yaml.erb'), } } diff --git a/manifests/facts.pp b/manifests/facts.pp index a4cd953..feaf89c 100644 --- a/manifests/facts.pp +++ b/manifests/facts.pp @@ -16,12 +16,11 @@ } file { "${facterbasepath}/facts.d/local.yaml": - ensure => file, - owner => 'root', - group => $::puppet::defaults::puppet_group, - mode => '0640', - validate_cmd => "/usr/bin/env ruby -ryaml -e \"YAML.load_file '%'\"", - content => template('puppet/local_facts.yaml.erb'), + ensure => file, + owner => 'root', + group => $::puppet::defaults::puppet_group, + mode => '0640', + content => template('puppet/local_facts.yaml.erb'), } } diff --git a/spec/classes/puppet_facts_spec.rb b/spec/classes/puppet_facts_spec.rb index acafc7b..d78134d 100644 --- a/spec/classes/puppet_facts_spec.rb +++ b/spec/classes/puppet_facts_spec.rb @@ -102,8 +102,6 @@ /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/ @@ -129,8 +127,6 @@ /key1: val1/ ).with_content( /key2: val2/ - ).with_validate_cmd( - "/usr/bin/env ruby -ryaml -e \"YAML.load_file '%'\"" ) should contain_file("#{facterbasepath}/facts.d/local.yaml").with_content( /Environment production/ @@ -164,8 +160,6 @@ /- val21/ ).with_content( /- val22/ - ).with_validate_cmd( - "/usr/bin/env ruby -ryaml -e \"YAML.load_file '%'\"" ) should contain_file("#{facterbasepath}/facts.d/local.yaml").with_content( /Environment production/ @@ -195,8 +189,6 @@ /key2:/ ).with_content( /key21: val21/ - ).with_validate_cmd( - "/usr/bin/env ruby -ryaml -e \"YAML.load_file '%'\"" ) should contain_file("#{facterbasepath}/facts.d/local.yaml").with_content( /Environment production/ diff --git a/spec/defines/fact_spec.rb b/spec/defines/fact_spec.rb index db95fac..bb753bb 100644 --- a/spec/defines/fact_spec.rb +++ b/spec/defines/fact_spec.rb @@ -66,8 +66,6 @@ /---/ ).with_content( /my_fact: my_val/ - ).with_validate_cmd( - "/usr/bin/env ruby -ryaml -e \"YAML.load_file '%'\"" ) end end @@ -91,8 +89,6 @@ /- my_val0/ ).with_content( /- my_val1/ - ).with_validate_cmd( - "/usr/bin/env ruby -ryaml -e \"YAML.load_file '%'\"" ) end end @@ -116,8 +112,6 @@ /my_key0: my_val0/ ).with_content( /my_key1: my_val1/ - ).with_validate_cmd( - "/usr/bin/env ruby -ryaml -e \"YAML.load_file '%'\"" ) end end