Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature "expanded custom facts" #113

Open
wants to merge 10 commits into
base: production
Choose a base branch
from
7 changes: 5 additions & 2 deletions manifests/fact.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 1 addition & 2 deletions manifests/facts.pp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
#
# setup facts
#

class puppet::facts (
$custom_facts = undef
$custom_facts = undef,
) {
include ::puppet
include ::puppet::defaults
Expand Down
131 changes: 103 additions & 28 deletions spec/classes/puppet_facts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
120 changes: 69 additions & 51 deletions spec/defines/fact_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 => [
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions templates/fact.yaml.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# custom fact <%= @title %>
---
<%= @title %>: "<%= @value %>"
<%- require 'yaml' -%>
<%= YAML.dump(@facter_data) %>
8 changes: 2 additions & 6 deletions templates/local_facts.yaml.erb
Original file line number Diff line number Diff line change
@@ -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) %>