Skip to content

Commit 51cf806

Browse files
committed
(PUP-11621) Add filters to Hiera 3-specific tests
As Hiera 3 will no longer be a hard requirement for Puppet, this commit adds rspec filters to tests that depend on Hiera 3 being present.
1 parent be2e8ad commit 51cf806

File tree

4 files changed

+41
-55
lines changed

4 files changed

+41
-55
lines changed

spec/unit/functions/hiera_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def func(*args, &block)
161161
with_scope { |scope| the_func.call(scope, *args, &block) }
162162
end
163163

164-
context 'hiera' do
164+
context 'hiera', :if => Puppet.features.hiera? do
165165
it 'should require a key argument' do
166166
expect { func([]) }.to raise_error(ArgumentError)
167167
end
@@ -195,7 +195,7 @@ def func(*args, &block)
195195
expect(func('datasources', nil, 'the_override')).to eql(['the_override', 'first', 'second'])
196196
end
197197

198-
it 'a hiera v3 scope is used' do
198+
it 'a hiera v3 scope is used', :if => Puppet.features.hiera? do
199199
expect(eval_and_collect_notices(<<-PUPPET, node)).to eql(['-- testing --', '-- mod::foo --', '-- mod/foo --', '-- mod --', '-- mod --'])
200200
class testing () {
201201
notice(hiera('class_name'))
@@ -266,7 +266,7 @@ class testing () {
266266
end
267267
end
268268

269-
context 'hiera_array' do
269+
context 'hiera_array', :if => Puppet.features.hiera? do
270270
let(:the_func) { Puppet.lookup(:loaders).puppet_system_loader.load(:function, 'hiera_array') }
271271

272272
it 'should require a key argument' do
@@ -299,7 +299,7 @@ class testing () {
299299
end
300300
end
301301

302-
context 'hiera_hash' do
302+
context 'hiera_hash', :if => Puppet.features.hiera? do
303303
let(:the_func) { Puppet.lookup(:loaders).puppet_system_loader.load(:function, 'hiera_hash') }
304304

305305
it 'should require a key argument' do
@@ -336,7 +336,7 @@ class testing () {
336336
end
337337
end
338338

339-
context 'hiera_include' do
339+
context 'hiera_include', :if => Puppet.features.hiera? do
340340
let(:the_func) { Puppet.lookup(:loaders).puppet_system_loader.load(:function, 'hiera_include') }
341341

342342
it 'should require a key argument' do
@@ -365,7 +365,7 @@ class testing () {
365365
end
366366
end
367367

368-
context 'with custom backend and merge_behavior declared in hiera.yaml' do
368+
context 'with custom backend and merge_behavior declared in hiera.yaml', :if => Puppet.features.hiera? do
369369
let(:merge_behavior) { 'deeper' }
370370

371371
let(:hiera_yaml) do
@@ -468,7 +468,7 @@ class testing () {
468468
end
469469
end
470470

471-
context 'hiera_array' do
471+
context 'hiera_array', :if => Puppet.features.hiera? do
472472
let(:the_func) { Puppet.lookup(:loaders).puppet_system_loader.load(:function, 'hiera_array') }
473473

474474
it 'declared merge_behavior is ignored' do

spec/unit/functions/lookup_spec.rb

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
require 'puppet_spec/files'
44
require 'puppet/pops'
55
require 'deep_merge/core'
6-
require 'hiera'
76

87
describe "The lookup function" do
98
include PuppetSpec::Compiler
@@ -1652,7 +1651,7 @@ def uri_test_func(options, context)
16521651
end
16531652
end
16541653

1655-
context 'version 3' do
1654+
context 'version 3', :if => Puppet.features.hiera? do
16561655
it 'finds data in in global layer and reports deprecation warnings for hiera.yaml' do
16571656
expect(lookup('a')).to eql('value a (from global)')
16581657
expect(warnings).to include(/Use of 'hiera.yaml' version 3 is deprecated. It should be converted to version 5/)
@@ -1697,6 +1696,14 @@ def uri_test_func(options, context)
16971696
expect(lookup('datasources')).to eql(['common', 'example.com'])
16981697
end
16991698

1699+
it 'backend specific options are propagated to custom backend' do
1700+
expect(lookup('other_option')).to eql('value of other_option')
1701+
end
1702+
1703+
it 'dotted keys are passed down to custom backend' do
1704+
expect(lookup('dotted.key')).to eql('custom backend received request for dotted.key value')
1705+
end
1706+
17001707
it 'delegates configured hocon backend to hocon_data function' do
17011708
expect(explain('xs')).to match(/Hierarchy entry "hocon"\n.*\n.*\n.*"common"\n\s*Found key: "xs"/m)
17021709
end
@@ -1705,6 +1712,18 @@ def uri_test_func(options, context)
17051712
expect(lookup('xs.subkey')).to eql('value xs.subkey (from global hocon)')
17061713
end
17071714

1715+
it 'multiple hiera3_backend declarations can be used and are merged into the generated config' do
1716+
expect(lookup(['datasources', 'other_option'])).to eql([['common', 'example.com'], 'value of other_option'])
1717+
expect(Hiera::Config.instance_variable_get(:@config)).to eql(
1718+
{
1719+
:backends => ['custom', 'other'],
1720+
:hierarchy => ['common', '%{domain}'],
1721+
:custom => { :datadir => "#{code_dir}/hieradata" },
1722+
:other => { :other_option => 'value of other_option', :datadir=>"#{code_dir}/hieradata" },
1723+
:logger => 'puppet'
1724+
})
1725+
end
1726+
17081727
context 'with a module data provider' do
17091728
let(:module_files) do
17101729
{
@@ -2041,18 +2060,6 @@ def uri_test_func(options, context)
20412060
paths:
20422061
- common.conf
20432062
- "%{domain}.conf"
2044-
- name: Custom
2045-
hiera3_backend: custom
2046-
paths:
2047-
- common.custom
2048-
- "%{domain}.custom"
2049-
- name: Other
2050-
hiera3_backend: other
2051-
options:
2052-
other_option: value of other_option
2053-
paths:
2054-
- common.other
2055-
- "%{domain}.other"
20562063
YAML
20572064
end
20582065

@@ -2067,7 +2074,6 @@ def uri_test_func(options, context)
20672074
expect(explanation).to include('Hierarchy entry "Yaml"')
20682075
expect(explanation).to include('Hierarchy entry "Json"')
20692076
expect(explanation).to include('Hierarchy entry "Hocon"')
2070-
expect(explanation).to include('Hierarchy entry "Custom"')
20712077
expect(explanation).to include('Found key: "a" value: "value a (from global)"')
20722078
end
20732079

@@ -2079,35 +2085,10 @@ def uri_test_func(options, context)
20792085
'caa' => 'value hash_c.hash_ca.caa (from environment)',
20802086
'cab' => 'value hash_c.hash_ca.cab (from global)',
20812087
'cac' => 'value hash_c.hash_ca.cac (from global json)',
2082-
'cad' => 'value hash_c.hash_ca.cad (from global custom)'
20832088
}
20842089
})
20852090
end
20862091

2087-
it 'backend data sources are propagated to custom backend' do
2088-
expect(lookup('datasources')).to eql(['common', 'example.com'])
2089-
end
2090-
2091-
it 'backend specific options are propagated to custom backend' do
2092-
expect(lookup('other_option')).to eql('value of other_option')
2093-
end
2094-
2095-
it 'dotted keys are passed down to custom backend' do
2096-
expect(lookup('dotted.key')).to eql('custom backend received request for dotted.key value')
2097-
end
2098-
2099-
it 'multiple hiera3_backend declarations can be used and are merged into the generated config' do
2100-
expect(lookup(['datasources', 'other_option'])).to eql([['common', 'example.com'], 'value of other_option'])
2101-
expect(Hiera::Config.instance_variable_get(:@config)).to eql(
2102-
{
2103-
:backends => ['custom', 'other'],
2104-
:hierarchy => ['common', '%{domain}'],
2105-
:custom => { :datadir => "#{code_dir}/hieradata" },
2106-
:other => { :other_option => 'value of other_option', :datadir=>"#{code_dir}/hieradata" },
2107-
:logger => 'puppet'
2108-
})
2109-
end
2110-
21112092
it 'provides a sensible error message when the hocon library is not loaded' do
21122093
allow(Puppet.features).to receive(:hocon?).and_return(false)
21132094

@@ -2156,7 +2137,7 @@ def uri_test_func(options, context)
21562137
end
21572138
end
21582139

2159-
context 'and eyaml_lookup_key function' do
2140+
context 'and eyaml_lookup_key function', :if => Puppet.features.hiera_eyaml? do
21602141
let(:hiera_yaml) { <<-YAML.unindent }
21612142
version: 5
21622143
hierarchy:
@@ -2171,7 +2152,7 @@ def uri_test_func(options, context)
21712152
end
21722153
end
21732154

2174-
context 'with a hiera3_backend that has no paths' do
2155+
context 'with a hiera3_backend that has no paths', :if => Puppet.features.hiera? do
21752156
let(:hiera_yaml) do
21762157
<<-YAML.unindent
21772158
---
@@ -2215,7 +2196,7 @@ def uri_test_func(options, context)
22152196
expect { lookup('mod_a::b') }.to raise_error(Puppet::DataBinding::LookupError, /did not find a value for the name 'mod_a::b'/)
22162197
end
22172198

2218-
context 'with a Hiera v3 configuration' do
2199+
context 'with a Hiera v3 configuration', :if => Puppet.features.hiera? do
22192200
let(:mod_a_files) do
22202201
{
22212202
'mod_a' => {
@@ -2992,7 +2973,7 @@ def ruby_dig(segments, options, context)
29922973
end
29932974
end
29942975

2995-
context 'and an eyaml lookup_key function' do
2976+
context 'and an eyaml lookup_key function', :if => Puppet.features.hiera_eyaml? do
29962977
let(:private_key_name) { 'private_key.pkcs7.pem' }
29972978
let(:public_key_name) { 'public_key.pkcs7.pem' }
29982979

@@ -3294,7 +3275,7 @@ def ruby_dig(segments, options, context)
32943275
expect(lookup('array_a')).to eql(['array_a[0]', 'array_a[1]'])
32953276
end
32963277

3297-
context 'declared in global scope as a Hiera v3 backend' do
3278+
context 'declared in global scope as a Hiera v3 backend', :if => Puppet.features.hiera? do
32983279
let(:environment_files) { {} }
32993280
let(:data_file_content) { <<-YAML.unindent }
33003281
a: >

spec/unit/hiera_puppet_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require 'hiera_puppet'
33
require 'puppet_spec/scope'
44

5-
describe 'HieraPuppet' do
5+
describe 'HieraPuppet', :if => Puppet.features.hiera? do
66
include PuppetSpec::Scope
77

88
after(:all) do

spec/unit/indirector/hiera_spec.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
require 'spec_helper'
22
require 'puppet/data_binding'
33
require 'puppet/indirector/hiera'
4-
require 'hiera/backend'
54

6-
describe Puppet::Indirector::Hiera do
5+
begin
6+
require 'hiera/backend'
7+
rescue LoadError => e
8+
Puppet.warning(_("Unable to load Hiera 3 backend: %{message}") % {message: e.message})
9+
end
10+
11+
describe Puppet::Indirector::Hiera, :if => Puppet.features.hiera? do
712

813
module Testing
914
module DataBinding

0 commit comments

Comments
 (0)