Skip to content

Commit

Permalink
Allow customizing module-layer Hiera configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
nabertrand committed Dec 28, 2018
1 parent 6e6ebcf commit fc9960f
Show file tree
Hide file tree
Showing 14 changed files with 271 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/rspec-puppet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def self.current_example
c.add_setting :platform, :default => Puppet::Util::Platform.actual_platform
c.add_setting :vendormoduledir, :default => Puppet::Util::Platform.actually_windows? ? 'c:/nul/' : '/dev/null'
c.add_setting :basemodulepath, :default => Puppet::Util::Platform.actually_windows? ? 'c:/nul/' : '/dev/null'
c.add_setting :disable_module_hiera, :default => false
c.add_setting :fixture_hiera_configs, :default => {}
c.add_setting :use_fixture_spec_hiera, :default => false
c.add_setting :fallback_to_default_hiera, :default => true

c.instance_eval do
def trusted_server_facts
Expand Down
34 changes: 34 additions & 0 deletions lib/rspec-puppet/monkey_patches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,40 @@ class Provider::Confine::Exists < Puppet::Provider::Confine
end
end
end

if Puppet::Util::Package.versioncmp(Puppet.version, '4.9.0') >= 0
class Module
old_hiera_conf_file = instance_method(:hiera_conf_file)
define_method(:hiera_conf_file) do
if RSpec::Puppet.rspec_puppet_example?
if RSpec.configuration.disable_module_hiera
return nil
elsif RSpec.configuration.fixture_hiera_configs.key?(name)
config = RSpec.configuration.fixture_hiera_configs[name]
config = File.absolute_path(config, path) unless config.nil?
return config
elsif RSpec.configuration.use_fixture_spec_hiera
config = RSpec::Puppet.current_example.fixture_spec_hiera_conf(self)
return config unless config.nil? && RSpec.configuration.fallback_to_default_hiera
end
end
old_hiera_conf_file.bind(self).call
end
end

class Pops::Lookup::ModuleDataProvider
old_configuration_path = instance_method(:configuration_path)
define_method(:configuration_path) do |lookup_invocation|
if RSpec::Puppet.rspec_puppet_example?
env = lookup_invocation.scope.environment
mod = env.module(module_name)
raise Puppet::DataBinding::LookupError, _("Environment '%{env}', cannot find module '%{module_name}'") % { :env => env.name, :module_name => module_name } unless mod
return Pathname.new(mod.hiera_conf_file)
end
old_configuration_path.bind(self).call(lookup_invocation)
end
end
end
end

class Pathname
Expand Down
28 changes: 27 additions & 1 deletion lib/rspec-puppet/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
module RSpec::Puppet
module Support
@@cache = RSpec::Puppet::Cache.new
@@fixture_hiera_configs = Hash.new { |h, k| h[k] = nil }

def subject
lambda { catalogue }
Expand Down Expand Up @@ -84,10 +85,18 @@ def load_catalogue(type, exported = false, manifest_opts = {})
hiera_config_value = self.respond_to?(:hiera_config) ? hiera_config : nil
hiera_data_value = self.respond_to?(:hiera_data) ? hiera_data : nil

rspec_config_values = [
:trusted_server_facts,
:disable_module_hiera,
:use_fixture_spec_hiera,
:fixture_hiera_configs,
:fallback_to_default_hiera,
].map { |setting| RSpec.configuration.send(setting) }

build_facts = facts_hash(node_name)
catalogue = build_catalog(node_name, build_facts, trusted_facts_hash(node_name), hiera_config_value,
build_code(type, manifest_opts), exported, node_params_hash, hiera_data_value,
RSpec.configuration.trusted_server_facts)
rspec_config_values)

test_module = type == :host ? nil : class_name.split('::').first
if type == :define
Expand Down Expand Up @@ -444,6 +453,23 @@ def rspec_compatibility
end
end

def fixture_spec_hiera_conf(mod)
return @@fixture_hiera_configs[mod.name] if @@fixture_hiera_configs.key?(mod.name)

path = Pathname.new(mod.path)
if path.join('spec').exist?
path.join('spec').find do |file|
Find.prune if %w[modules work-dir].any? do |dir|
file.relative_path_from(path).to_s.start_with?("spec/fixtures/#{dir}")
end
if file.basename.to_s.eql?(Puppet::Pops::Lookup::HieraConfig::CONFIG_FILE_NAME)
return @@fixture_hiera_configs[mod.name] = file.to_s
end
end
end
@@fixture_hiera_configs[mod.name]
end

# Helper to return a resource/node reference, so it gets translated in params to a raw string
# without quotes.
#
Expand Down
153 changes: 153 additions & 0 deletions spec/classes/hiera_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,156 @@
end
end

describe 'hiera_test', :if => Puppet::Util::Package.versioncmp(Puppet.version, '4.9.0') >= 0 do
before(:each) do
RSpec.configuration.disable_module_hiera = false
RSpec.configuration.use_fixture_spec_hiera = false
RSpec.configuration.fixture_hiera_configs = {}
RSpec.configuration.fallback_to_default_hiera = true
end

context 'without :hiera_config set' do
context 'with module eyaml hiera data enabled' do
it { should raise_error(Puppet::PreformattedError, %r{hiera_eyaml}) }
end

context 'with module eyaml hiera data disabled' do
before(:each) { RSpec.configuration.disable_module_hiera = true }

it { should raise_error(Puppet::ParseError) }
end

context 'with relative fixture hiera config path' do
before(:each) { RSpec.configuration.fixture_hiera_configs = {'hiera_test' => 'spec/module_hiera.yaml'} }

it { should contain_notify('module') }
end

context 'with absolute fixture hiera config path' do
before(:each) do
RSpec.configuration.fixture_hiera_configs = {
'hiera_test' => File.join(__FILE__, '../../fixtures/modules/hiera_test/spec/module_hiera.yaml')
}
end

it { should contain_notify('module') }
end

context 'with invalid fixture hiera config path' do
before(:each) { RSpec.configuration.fixture_hiera_configs = {'hiera_test' => 'non_existent.yaml'} }

it { should raise_error(Puppet::ParseError) }
end

context 'with :use_fixture_spec_hiera set' do
before(:each) { RSpec.configuration.use_fixture_spec_hiera = true }

it { should contain_notify('spec') }
end
end

context 'with :hiera_config set' do
let(:hiera_config) { 'spec/fixtures/hiera.yaml' }

context 'with module eyaml hiera data enabled' do
it { should raise_error(Puppet::PreformattedError, %r{hiera_eyaml}) }
end

context 'with module eyaml hiera data disabled' do
before(:each) { RSpec.configuration.disable_module_hiera = true }

it { should contain_notify('global') }
end

context 'with relative fixture hiera config path' do
before(:each) { RSpec.configuration.fixture_hiera_configs = {'hiera_test' => 'spec/module_hiera.yaml'} }

it { should contain_notify('global') }
end

context 'with absolute fixture hiera config path' do
before(:each) do
RSpec.configuration.fixture_hiera_configs = {
'hiera_test' => File.join(__FILE__, '../../fixtures/modules/hiera_test/spec/module_hiera.yaml')
}
end

it { should contain_notify('global') }
end

context 'with invalid fixture hiera config path' do
before(:each) { RSpec.configuration.fixture_hiera_configs = {'hiera_test' => 'non_existent.yaml'} }

it { should contain_notify('global') }
end

context 'with :use_fixture_spec_hiera set' do
before(:each) { RSpec.configuration.use_fixture_spec_hiera = true }

it { should contain_notify('global') }
end
end
end

describe 'hiera_test2', :if => Puppet::Util::Package.versioncmp(Puppet.version, '4.9.0') >= 0 do
before(:each) do
RSpec.configuration.disable_module_hiera = false
RSpec.configuration.use_fixture_spec_hiera = false
RSpec.configuration.fixture_hiera_configs = {}
RSpec.configuration.fallback_to_default_hiera = true
end

context 'without :hiera_config set' do
context 'with module-layer hiera enabled' do
it { should contain_notify('module') }
end

context 'with module-layer hiera disabled' do
before(:each) { RSpec.configuration.disable_module_hiera = true }

it { should raise_error(Puppet::ParseError) }
end

context 'with :use_fixture_spec_hiera set' do
before(:each) { RSpec.configuration.use_fixture_spec_hiera = true }

context 'with missing spec hiera.yaml and hiera fallback enabled' do
it { should contain_notify('module') }
end

context 'with missing spec hiera.yaml and hiera fallback disabled' do
before(:each) { RSpec.configuration.fallback_to_default_hiera = false }

it { should raise_error(Puppet::ParseError) }
end
end
end

context 'with :hiera_config set' do
let(:hiera_config) { 'spec/fixtures/hiera.yaml' }

context 'with module-layer hiera enabled' do
it { should contain_notify('global') }
end

context 'with module-layer hiera disabled' do
before(:each) { RSpec.configuration.disable_module_hiera = true }

it { should contain_notify('global') }
end

context 'with :use_fixture_spec_hiera set' do
before(:each) { RSpec.configuration.use_fixture_spec_hiera = true }

context 'with missing spec hiera.yaml and hiera fallback enabled' do
it { should contain_notify('global') }
end

context 'with missing spec hiera.yaml and hiera fallback disabled' do
before(:each) { RSpec.configuration.fallback_to_default_hiera = false }

it { should contain_notify('global') }
end
end
end
end
2 changes: 2 additions & 0 deletions spec/fixtures/hieradata/common.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
---
data: foo
hiera_test::test_param: global
hiera_test2::test_param: global
2 changes: 2 additions & 0 deletions spec/fixtures/modules/hiera_test/data/common.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
hiera_test::test_param: module
11 changes: 11 additions & 0 deletions spec/fixtures/modules/hiera_test/hiera.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
version: 5
defaults:
datadir: data
lookup_key: eyaml_lookup_key
hierarchy:
- name: 'Defaults'
glob: '**.yaml'
options:
pkcs7_private_key: /fakekey
pkcs7_public_key: /fakepubkey
5 changes: 5 additions & 0 deletions spec/fixtures/modules/hiera_test/manifests/init.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class hiera_test (
$test_param,
) {
notify { $test_param: }
}
2 changes: 2 additions & 0 deletions spec/fixtures/modules/hiera_test/spec/data/common.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
hiera_test::test_param: spec
8 changes: 8 additions & 0 deletions spec/fixtures/modules/hiera_test/spec/hiera.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
version: 5
defaults:
data_hash: yaml_data
datadir: data
hierarchy:
- name: Common
path: common.yaml
8 changes: 8 additions & 0 deletions spec/fixtures/modules/hiera_test/spec/module_hiera.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
version: 5
defaults:
data_hash: yaml_data
datadir: ../data
hierarchy:
- name: Common
path: common.yaml
2 changes: 2 additions & 0 deletions spec/fixtures/modules/hiera_test2/data/common.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
hiera_test2::test_param: module
8 changes: 8 additions & 0 deletions spec/fixtures/modules/hiera_test2/hiera.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
version: 5
defaults:
data_hash: yaml_data
datadir: data
hierarchy:
- name: 'Defaults'
glob: '**.yaml'
5 changes: 5 additions & 0 deletions spec/fixtures/modules/hiera_test2/manifests/init.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class hiera_test2 (
$test_param,
) {
notify { $test_param: }
}

0 comments on commit fc9960f

Please sign in to comment.