Skip to content

Commit 30ab47f

Browse files
authored
Merge pull request #75 from marek130/master
Support hiera v5
2 parents 1cc428a + 64c5ddf commit 30ab47f

10 files changed

+50
-49
lines changed

lib/puppet-lint/plugins/check_ghostbuster_hiera_files.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ def regexprs
1717
hiera_yaml_file = ENV['HIERA_YAML_PATH'] || '/etc/puppetlabs/code/hiera.yaml'
1818
hiera = YAML.load_file(hiera_yaml_file)
1919
regs = {}
20-
hiera[:hierarchy].each do |hierarchy|
21-
regex = hierarchy.gsub(/%\{(::)?(trusted|server_facts|facts)\.[^\}]+\}/, '(.+)').gsub(/%\{[^\}]+\}/, '.+')
22-
facts = hierarchy.match(regex).captures.map { |f| f[/%{(::)?(trusted|server_facts|facts)\.(.+)}/, 3] }
23-
regs[regex] = facts
20+
hiera['hierarchy'].each do |hierarchy|
21+
([*hierarchy['path']] + [*hierarchy['paths']]).each do |level|
22+
regex = level.gsub(/%\{(::)?(trusted|server_facts|facts)\.[^\}]+\}/, '(.+)').gsub(/%\{[^\}]+\}/, '.+')
23+
facts = level.match(regex).captures.map { |f| f[/%{(::)?(trusted|server_facts|facts)\.(.+)}/, 3] }
24+
regs[regex] = facts
25+
end
2426
end
2527
regs
2628
end
@@ -32,7 +34,7 @@ def check
3234
_path = path.gsub('./hieradata/', '')
3335

3436
regexprs.each do |k, v|
35-
m = _path.match(Regexp.new("#{k}.yaml"))
37+
m = _path.match(Regexp.new(k))
3638
next if m.nil?
3739
return if m.captures.size == 0
3840

spec/fixtures/hiera.yaml

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
---
2-
:backends: yaml
3-
:yaml:
4-
:datadir: "/etc/puppetlabs/code/environments/%{environment}/hieradata"
5-
:hierarchy:
6-
- "nodes/%{::trusted.certname}"
7-
- "environment/%{server_facts.environment}"
8-
- "virtual/%{facts.is_virtual}"
9-
- "domain/%{::domain}"
10-
- "common"
11-
:logger: console
12-
:merge_behavior: native
13-
:deep_merge_options: {}
2+
version: 5
3+
hierarchy:
4+
- name: "Per-node data (yaml version)"
5+
path: "nodes/%{trusted.certname}.yaml"
6+
7+
- name: "Other YAML hierarchy levels"
8+
paths:
9+
- "environment/%{server_facts.environment}.yaml"
10+
- "virtual/%{facts.is_virtual}.yaml"
11+
- "domain/%{domain}.yaml"
12+
- "common.yaml"

spec/puppet-lint/plugins/ghostbuster_classes_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
let(:code) { 'class foo {}' }
1111

1212
it 'does not detect any problem' do
13-
expect(problems).to have(0).problems
13+
expect(problems.size).to eq(0)
1414
end
1515
end
1616

1717
context 'when class is not used' do
1818
let(:code) { 'class bar {}' }
1919

2020
it 'detects one problem' do
21-
expect(problems).to have(1).problems
21+
expect(problems.size).to eq(1)
2222
end
2323

2424
it 'creates a warning' do

spec/puppet-lint/plugins/ghostbuster_defines_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
let(:path) { './modules/foo/manifests/foo.pp' }
1010

1111
it 'detects one problem' do
12-
expect(problems).to have(1).problems
12+
expect(problems.size).to eq(1)
1313
end
1414

1515
it 'creates a warning' do
@@ -22,7 +22,7 @@
2222
let(:path) { './modules/bar/manifests/foo.pp' }
2323

2424
it 'does not detect any problem' do
25-
expect(problems).to have(0).problems
25+
expect(problems.size).to eq(0)
2626
end
2727
end
2828
end

spec/puppet-lint/plugins/ghostbuster_facts_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
let(:path) { './spec/fixtures/modules/foo/lib/facter/multi.rb' }
1111

1212
it 'detects one problem' do
13-
expect(problems).to have(2).problems
13+
expect(problems.size).to eq(2)
1414
end
1515

1616
it 'creates a warning' do
@@ -26,31 +26,31 @@
2626
let(:path) { './spec/fixtures/modules/foo/lib/facter/foo.rb' }
2727

2828
it 'does not detect any problem' do
29-
expect(problems).to have(0).problems
29+
expect(problems.size).to eq(0)
3030
end
3131
end
3232

3333
context 'when fact is used in manifest' do
3434
let(:path) { './spec/fixtures/modules/foo/lib/facter/bar.rb' }
3535

3636
it 'does not detect any problem' do
37-
expect(problems).to have(0).problems
37+
expect(problems.size).to eq(0)
3838
end
3939
end
4040

4141
context 'when fact is used in a template' do
4242
let(:path) { './spec/fixtures/modules/foo/lib/facter/baz.rb' }
4343

4444
it 'does not detect any problem' do
45-
expect(problems).to have(0).problems
45+
expect(problems.size).to eq(0)
4646
end
4747
end
4848

4949
context 'when fact is used in an inline_template' do
5050
let(:path) { './spec/fixtures/modules/foo/lib/facter/quux.rb' }
5151

5252
it 'does not detect any problem' do
53-
expect(problems).to have(0).problems
53+
expect(problems.size).to eq(0)
5454
end
5555
end
5656
end

spec/puppet-lint/plugins/ghostbuster_files_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,39 @@
1010
let(:path) { './modules/foo/files/bar' }
1111

1212
it 'does not detect any problem' do
13-
expect(problems).to have(0).problems
13+
expect(problems.size).to eq(0)
1414
end
1515
end
1616

1717
context 'when parent directory with recurse => true usage is found in puppetdb' do
1818
let(:path) { './modules/foo/files/baz/baz' }
1919

2020
it 'does not detect any problem' do
21-
expect(problems).to have(0).problems
21+
expect(problems.size).to eq(0)
2222
end
2323
end
2424

2525
context 'when using full module name syntax' do
2626
let(:path) { './modules/foo/files/used_with_file' }
2727

2828
it 'does not detect any problem' do
29-
expect(problems).to have(0).problems
29+
expect(problems.size).to eq(0)
3030
end
3131
end
3232

3333
context 'when using $module_name syntax' do
3434
let(:path) { './modules/foo/files/used_with_file_and_module_name' }
3535

3636
it 'does not detect any problem' do
37-
expect(problems).to have(0).problems
37+
expect(problems.size).to eq(0)
3838
end
3939
end
4040

4141
context 'when file in ROOT is not used' do
4242
let(:path) { './modules/bar/files/foo' }
4343

4444
it 'detects one problem' do
45-
expect(problems).to have(1).problems
45+
expect(problems.size).to eq(1)
4646
end
4747

4848
it 'creates a warning' do
@@ -54,7 +54,7 @@
5454
let(:path) { './modules/bar/files/foo/bar' }
5555

5656
it 'detects one problem' do
57-
expect(problems).to have(1).problems
57+
expect(problems.size).to eq(1)
5858
end
5959

6060
it 'creates a warning' do

spec/puppet-lint/plugins/ghostbuster_functions_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
let(:path) { './spec/fixtures/modules/foo/lib/puppet/parser/functions/foo.rb' }
1111

1212
it 'detects one problem' do
13-
expect(problems).to have(1).problems
13+
expect(problems.size).to eq(1)
1414
end
1515

1616
it 'creates a warning' do
@@ -22,23 +22,23 @@
2222
let(:path) { './spec/fixtures/modules/foo/lib/puppet/parser/functions/bar.rb' }
2323

2424
it 'does not detect any problem' do
25-
expect(problems).to have(0).problems
25+
expect(problems.size).to eq(0)
2626
end
2727
end
2828

2929
context 'when function is used in a template using scope.function_baz()' do
3030
let(:path) { './spec/fixtures/modules/foo/lib/puppet/parser/functions/baz.rb' }
3131

3232
it 'does not detect any problem' do
33-
expect(problems).to have(0).problems
33+
expect(problems.size).to eq(0)
3434
end
3535
end
3636

3737
context 'when function is used in a template using Puppet::Parser::Functions.function(:quux)' do
3838
let(:path) { './spec/fixtures/modules/foo/lib/puppet/parser/functions/quux.rb' }
3939

4040
it 'does not detect any problem' do
41-
expect(problems).to have(0).problems
41+
expect(problems.size).to eq(0)
4242
end
4343
end
4444
end

spec/puppet-lint/plugins/ghostbuster_hiera_files_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
let(:path) { './hieradata/nodes/foo.example.com.yaml' }
1313

1414
it 'detects one problem' do
15-
expect(problems).to have(1).problems
15+
expect(problems.size).to eq(1)
1616
end
1717

1818
it 'creates a warning' do
@@ -24,15 +24,15 @@
2424
let(:path) { './hieradata/nodes/bar.example.com.yaml' }
2525

2626
it 'does not detect any problem' do
27-
expect(problems).to have(0).problems
27+
expect(problems.size).to eq(0)
2828
end
2929
end
3030

3131
context 'when an environment file is NOT used' do
3232
let(:path) { './hieradata/environment/foo.yaml' }
3333

3434
it 'detects one problem' do
35-
expect(problems).to have(1).problems
35+
expect(problems.size).to eq(1)
3636
end
3737

3838
it 'creates a warning' do
@@ -44,15 +44,15 @@
4444
let(:path) { './hieradata/environment/production.yaml' }
4545

4646
it 'does not detect any problem' do
47-
expect(problems).to have(0).problems
47+
expect(problems.size).to eq(0)
4848
end
4949
end
5050

5151
context 'when an fact is NOT used' do
5252
let(:path) { './hieradata/virtual/false.yaml' }
5353

5454
it 'detects one problem' do
55-
expect(problems).to have(1).problems
55+
expect(problems.size).to eq(1)
5656
end
5757

5858
it 'creates a warning' do
@@ -64,15 +64,15 @@
6464
let(:path) { './hieradata/virtual/true.yaml' }
6565

6666
it 'does not detect any problem' do
67-
expect(problems).to have(0).problems
67+
expect(problems.size).to eq(0)
6868
end
6969
end
7070

7171
context 'when using a variable in hierarchy' do
7272
let(:path) { './hieradata/domain/example.com.yaml' }
7373

7474
it 'does not detect any problem' do
75-
expect(problems).to have(0).problems
75+
expect(problems.size).to eq(0)
7676
end
7777
end
7878
end

spec/puppet-lint/plugins/ghostbuster_templates_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,31 @@
1010
let(:path) { './modules/foo/templates/used_with_template' }
1111

1212
it 'does not detect any problem' do
13-
expect(problems).to have(0).problems
13+
expect(problems.size).to eq(0)
1414
end
1515
end
1616

1717
context 'when using $module_name syntax' do
1818
let(:path) { './modules/foo/templates/used_with_template_and_module_name' }
1919

2020
it 'does not detect any problem' do
21-
expect(problems).to have(0).problems
21+
expect(problems.size).to eq(0)
2222
end
2323
end
2424

2525
context 'when using template in template' do
2626
let(:path) { './modules/foo/templates/used_in_template.erb' }
2727

2828
it 'does not detect any problem' do
29-
expect(problems).to have(0).problems
29+
expect(problems.size).to eq(0)
3030
end
3131
end
3232

3333
context 'when template usage is not found in manifests' do
3434
let(:path) { './modules/foo/templates/unused' }
3535

3636
it 'detects one problem' do
37-
expect(problems).to have(1).problems
37+
expect(problems.size).to eq(1)
3838
end
3939

4040
it 'creates a warning' do

spec/puppet-lint/plugins/ghostbuster_types_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
let(:path) { './spec/fixtures/modules/foo/lib/puppet/type/foo.rb' }
1111

1212
it 'detects one problem' do
13-
expect(problems).to have(1).problems
13+
expect(problems.size).to eq(1)
1414
end
1515

1616
it 'creates a warning' do
@@ -22,7 +22,7 @@
2222
let(:path) { './spec/fixtures/modules/foo/lib/puppet/type/bar.rb' }
2323

2424
it 'does not detect any problem' do
25-
expect(problems).to have(0).problems
25+
expect(problems.size).to eq(0)
2626
end
2727
end
2828
end

0 commit comments

Comments
 (0)