From 64c5ddf29a020e830303ad11bb317d0886195fc5 Mon Sep 17 00:00:00 2001 From: Marek Pastierik Date: Wed, 1 Nov 2023 10:04:06 +0100 Subject: [PATCH] Support hiera v5 --- .../plugins/check_ghostbuster_hiera_files.rb | 12 ++++++---- spec/fixtures/hiera.yaml | 23 +++++++++---------- .../plugins/ghostbuster_classes_spec.rb | 4 ++-- .../plugins/ghostbuster_defines_spec.rb | 4 ++-- .../plugins/ghostbuster_facts_spec.rb | 10 ++++---- .../plugins/ghostbuster_files_spec.rb | 12 +++++----- .../plugins/ghostbuster_functions_spec.rb | 8 +++---- .../plugins/ghostbuster_hiera_files_spec.rb | 14 +++++------ .../plugins/ghostbuster_templates_spec.rb | 8 +++---- .../plugins/ghostbuster_types_spec.rb | 4 ++-- 10 files changed, 50 insertions(+), 49 deletions(-) diff --git a/lib/puppet-lint/plugins/check_ghostbuster_hiera_files.rb b/lib/puppet-lint/plugins/check_ghostbuster_hiera_files.rb index 0f2fe09..7503851 100644 --- a/lib/puppet-lint/plugins/check_ghostbuster_hiera_files.rb +++ b/lib/puppet-lint/plugins/check_ghostbuster_hiera_files.rb @@ -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 @@ -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 diff --git a/spec/fixtures/hiera.yaml b/spec/fixtures/hiera.yaml index 53cb923..7d32a83 100644 --- a/spec/fixtures/hiera.yaml +++ b/spec/fixtures/hiera.yaml @@ -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" diff --git a/spec/puppet-lint/plugins/ghostbuster_classes_spec.rb b/spec/puppet-lint/plugins/ghostbuster_classes_spec.rb index 4952949..d1e6f52 100644 --- a/spec/puppet-lint/plugins/ghostbuster_classes_spec.rb +++ b/spec/puppet-lint/plugins/ghostbuster_classes_spec.rb @@ -10,7 +10,7 @@ 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 @@ -18,7 +18,7 @@ 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 diff --git a/spec/puppet-lint/plugins/ghostbuster_defines_spec.rb b/spec/puppet-lint/plugins/ghostbuster_defines_spec.rb index 68b7683..3c13cf3 100644 --- a/spec/puppet-lint/plugins/ghostbuster_defines_spec.rb +++ b/spec/puppet-lint/plugins/ghostbuster_defines_spec.rb @@ -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 @@ -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 diff --git a/spec/puppet-lint/plugins/ghostbuster_facts_spec.rb b/spec/puppet-lint/plugins/ghostbuster_facts_spec.rb index 0356fdc..bcb6307 100644 --- a/spec/puppet-lint/plugins/ghostbuster_facts_spec.rb +++ b/spec/puppet-lint/plugins/ghostbuster_facts_spec.rb @@ -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 @@ -26,7 +26,7 @@ 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 @@ -34,7 +34,7 @@ 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 @@ -42,7 +42,7 @@ 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 @@ -50,7 +50,7 @@ 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 diff --git a/spec/puppet-lint/plugins/ghostbuster_files_spec.rb b/spec/puppet-lint/plugins/ghostbuster_files_spec.rb index 280772d..685b790 100644 --- a/spec/puppet-lint/plugins/ghostbuster_files_spec.rb +++ b/spec/puppet-lint/plugins/ghostbuster_files_spec.rb @@ -10,7 +10,7 @@ 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 @@ -18,7 +18,7 @@ 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 @@ -26,7 +26,7 @@ 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 @@ -34,7 +34,7 @@ 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 @@ -42,7 +42,7 @@ 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 @@ -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 diff --git a/spec/puppet-lint/plugins/ghostbuster_functions_spec.rb b/spec/puppet-lint/plugins/ghostbuster_functions_spec.rb index 30786ad..b16f80b 100644 --- a/spec/puppet-lint/plugins/ghostbuster_functions_spec.rb +++ b/spec/puppet-lint/plugins/ghostbuster_functions_spec.rb @@ -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 @@ -22,7 +22,7 @@ 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 @@ -30,7 +30,7 @@ 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 @@ -38,7 +38,7 @@ 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 diff --git a/spec/puppet-lint/plugins/ghostbuster_hiera_files_spec.rb b/spec/puppet-lint/plugins/ghostbuster_hiera_files_spec.rb index 9b5031e..6d3a688 100644 --- a/spec/puppet-lint/plugins/ghostbuster_hiera_files_spec.rb +++ b/spec/puppet-lint/plugins/ghostbuster_hiera_files_spec.rb @@ -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 @@ -24,7 +24,7 @@ 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 @@ -32,7 +32,7 @@ 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 @@ -44,7 +44,7 @@ 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 @@ -52,7 +52,7 @@ 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 @@ -64,7 +64,7 @@ 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 @@ -72,7 +72,7 @@ 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 diff --git a/spec/puppet-lint/plugins/ghostbuster_templates_spec.rb b/spec/puppet-lint/plugins/ghostbuster_templates_spec.rb index 04871f5..df98144 100644 --- a/spec/puppet-lint/plugins/ghostbuster_templates_spec.rb +++ b/spec/puppet-lint/plugins/ghostbuster_templates_spec.rb @@ -10,7 +10,7 @@ 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 @@ -18,7 +18,7 @@ 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 @@ -26,7 +26,7 @@ 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 @@ -34,7 +34,7 @@ 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 diff --git a/spec/puppet-lint/plugins/ghostbuster_types_spec.rb b/spec/puppet-lint/plugins/ghostbuster_types_spec.rb index 6acbcdb..e04108d 100644 --- a/spec/puppet-lint/plugins/ghostbuster_types_spec.rb +++ b/spec/puppet-lint/plugins/ghostbuster_types_spec.rb @@ -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 @@ -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