Skip to content

Commit

Permalink
Support hiera v5
Browse files Browse the repository at this point in the history
  • Loading branch information
marek130 committed Nov 1, 2023
1 parent 1cc428a commit 64c5ddf
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 49 deletions.
12 changes: 7 additions & 5 deletions lib/puppet-lint/plugins/check_ghostbuster_hiera_files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ def regexprs
hiera_yaml_file = ENV['HIERA_YAML_PATH'] || '/etc/puppetlabs/code/hiera.yaml'
hiera = YAML.load_file(hiera_yaml_file)
regs = {}
hiera[:hierarchy].each do |hierarchy|
regex = hierarchy.gsub(/%\{(::)?(trusted|server_facts|facts)\.[^\}]+\}/, '(.+)').gsub(/%\{[^\}]+\}/, '.+')
facts = hierarchy.match(regex).captures.map { |f| f[/%{(::)?(trusted|server_facts|facts)\.(.+)}/, 3] }
regs[regex] = facts
hiera['hierarchy'].each do |hierarchy|
([*hierarchy['path']] + [*hierarchy['paths']]).each do |level|
regex = level.gsub(/%\{(::)?(trusted|server_facts|facts)\.[^\}]+\}/, '(.+)').gsub(/%\{[^\}]+\}/, '.+')
facts = level.match(regex).captures.map { |f| f[/%{(::)?(trusted|server_facts|facts)\.(.+)}/, 3] }
regs[regex] = facts
end
end
regs
end
Expand All @@ -32,7 +34,7 @@ def check
_path = path.gsub('./hieradata/', '')

regexprs.each do |k, v|
m = _path.match(Regexp.new("#{k}.yaml"))
m = _path.match(Regexp.new(k))
next if m.nil?
return if m.captures.size == 0

Expand Down
23 changes: 11 additions & 12 deletions spec/fixtures/hiera.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
---
:backends: yaml
:yaml:
:datadir: "/etc/puppetlabs/code/environments/%{environment}/hieradata"
:hierarchy:
- "nodes/%{::trusted.certname}"
- "environment/%{server_facts.environment}"
- "virtual/%{facts.is_virtual}"
- "domain/%{::domain}"
- "common"
:logger: console
:merge_behavior: native
:deep_merge_options: {}
version: 5
hierarchy:
- name: "Per-node data (yaml version)"
path: "nodes/%{trusted.certname}.yaml"

- name: "Other YAML hierarchy levels"
paths:
- "environment/%{server_facts.environment}.yaml"
- "virtual/%{facts.is_virtual}.yaml"
- "domain/%{domain}.yaml"
- "common.yaml"
4 changes: 2 additions & 2 deletions spec/puppet-lint/plugins/ghostbuster_classes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
let(:code) { 'class foo {}' }

it 'does not detect any problem' do
expect(problems).to have(0).problems
expect(problems.size).to eq(0)
end
end

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

it 'detects one problem' do
expect(problems).to have(1).problems
expect(problems.size).to eq(1)
end

it 'creates a warning' do
Expand Down
4 changes: 2 additions & 2 deletions spec/puppet-lint/plugins/ghostbuster_defines_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
let(:path) { './modules/foo/manifests/foo.pp' }

it 'detects one problem' do
expect(problems).to have(1).problems
expect(problems.size).to eq(1)
end

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

it 'does not detect any problem' do
expect(problems).to have(0).problems
expect(problems.size).to eq(0)
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions spec/puppet-lint/plugins/ghostbuster_facts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
let(:path) { './spec/fixtures/modules/foo/lib/facter/multi.rb' }

it 'detects one problem' do
expect(problems).to have(2).problems
expect(problems.size).to eq(2)
end

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

it 'does not detect any problem' do
expect(problems).to have(0).problems
expect(problems.size).to eq(0)
end
end

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

it 'does not detect any problem' do
expect(problems).to have(0).problems
expect(problems.size).to eq(0)
end
end

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

it 'does not detect any problem' do
expect(problems).to have(0).problems
expect(problems.size).to eq(0)
end
end

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

it 'does not detect any problem' do
expect(problems).to have(0).problems
expect(problems.size).to eq(0)
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions spec/puppet-lint/plugins/ghostbuster_files_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,39 @@
let(:path) { './modules/foo/files/bar' }

it 'does not detect any problem' do
expect(problems).to have(0).problems
expect(problems.size).to eq(0)
end
end

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

it 'does not detect any problem' do
expect(problems).to have(0).problems
expect(problems.size).to eq(0)
end
end

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

it 'does not detect any problem' do
expect(problems).to have(0).problems
expect(problems.size).to eq(0)
end
end

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

it 'does not detect any problem' do
expect(problems).to have(0).problems
expect(problems.size).to eq(0)
end
end

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

it 'detects one problem' do
expect(problems).to have(1).problems
expect(problems.size).to eq(1)
end

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

it 'detects one problem' do
expect(problems).to have(1).problems
expect(problems.size).to eq(1)
end

it 'creates a warning' do
Expand Down
8 changes: 4 additions & 4 deletions spec/puppet-lint/plugins/ghostbuster_functions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
let(:path) { './spec/fixtures/modules/foo/lib/puppet/parser/functions/foo.rb' }

it 'detects one problem' do
expect(problems).to have(1).problems
expect(problems.size).to eq(1)
end

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

it 'does not detect any problem' do
expect(problems).to have(0).problems
expect(problems.size).to eq(0)
end
end

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

it 'does not detect any problem' do
expect(problems).to have(0).problems
expect(problems.size).to eq(0)
end
end

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

it 'does not detect any problem' do
expect(problems).to have(0).problems
expect(problems.size).to eq(0)
end
end
end
Expand Down
14 changes: 7 additions & 7 deletions spec/puppet-lint/plugins/ghostbuster_hiera_files_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
let(:path) { './hieradata/nodes/foo.example.com.yaml' }

it 'detects one problem' do
expect(problems).to have(1).problems
expect(problems.size).to eq(1)
end

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

it 'does not detect any problem' do
expect(problems).to have(0).problems
expect(problems.size).to eq(0)
end
end

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

it 'detects one problem' do
expect(problems).to have(1).problems
expect(problems.size).to eq(1)
end

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

it 'does not detect any problem' do
expect(problems).to have(0).problems
expect(problems.size).to eq(0)
end
end

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

it 'detects one problem' do
expect(problems).to have(1).problems
expect(problems.size).to eq(1)
end

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

it 'does not detect any problem' do
expect(problems).to have(0).problems
expect(problems.size).to eq(0)
end
end

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

it 'does not detect any problem' do
expect(problems).to have(0).problems
expect(problems.size).to eq(0)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/puppet-lint/plugins/ghostbuster_templates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@
let(:path) { './modules/foo/templates/used_with_template' }

it 'does not detect any problem' do
expect(problems).to have(0).problems
expect(problems.size).to eq(0)
end
end

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

it 'does not detect any problem' do
expect(problems).to have(0).problems
expect(problems.size).to eq(0)
end
end

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

it 'does not detect any problem' do
expect(problems).to have(0).problems
expect(problems.size).to eq(0)
end
end

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

it 'detects one problem' do
expect(problems).to have(1).problems
expect(problems.size).to eq(1)
end

it 'creates a warning' do
Expand Down
4 changes: 2 additions & 2 deletions spec/puppet-lint/plugins/ghostbuster_types_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
let(:path) { './spec/fixtures/modules/foo/lib/puppet/type/foo.rb' }

it 'detects one problem' do
expect(problems).to have(1).problems
expect(problems.size).to eq(1)
end

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

it 'does not detect any problem' do
expect(problems).to have(0).problems
expect(problems.size).to eq(0)
end
end
end
Expand Down

0 comments on commit 64c5ddf

Please sign in to comment.