puppet-ghostbuster 2.1.0 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 825f0bad6bda91f1b0c1ac0c423d436fb2593a4b77b2354815b4c2520bd74425
4
- data.tar.gz: 2b04bcddbb57796ebff9136ddab64626a48ecde7f20e273209e8cefc23e429c5
3
+ metadata.gz: a49a8e2b82cb4e340e41a2767d047eb168864c83a723ab49d4087f5c1228b0e1
4
+ data.tar.gz: f8d07b03c0c73d9e1966884f91eeba42e0e5be2ecc2cfbb615cf2230f3291dbe
5
5
  SHA512:
6
- metadata.gz: 7f0a8c6c0327eb6bb8873d24da61d266134d9ec7ea533d6dde5eff717ca720c8b917cb3c6a92557ab0c5d71d29a8e7134bd897778142481bbe53c22702e000ff
7
- data.tar.gz: e83248042353d7419766749fb28e0a45f768e64c924dd7386acda9b05ba7ed2838afe092dbb93a95ea6b1f09572aceba4e34ab54dfe31e1b8e482719c02e2886
6
+ metadata.gz: 561a9fc699dfe34e2ce6f25e7fb740df7121095bcca428bd00c4938a0a75c886e325f7eb0eca4c25a5b46a0577bf01f763e544bb657589e6d3bcdb30c8053da3
7
+ data.tar.gz: c9f876fa728ce126598afc6598088ec4919010cb0196b58a3aaf7ca2aef9f47b430c8aa6a70f41417f67dc2639e021a2127970ca005d26ab46ed2683bf1eed9f
@@ -0,0 +1,3 @@
1
+ ---
2
+ skip-changelog:
3
+ - head-branch: ['^release-*']
@@ -0,0 +1,40 @@
1
+ ---
2
+ # https://docs.github.com/en/repositories/releasing-projects-on-github/automatically-generated-release-notes
3
+
4
+ changelog:
5
+ exclude:
6
+ labels:
7
+ - duplicate
8
+ - invalid
9
+ - modulesync
10
+ - question
11
+ - skip-changelog
12
+ - wont-fix
13
+ - wontfix
14
+ - github_actions
15
+
16
+ categories:
17
+ - title: Breaking Changes 🛠
18
+ labels:
19
+ - backwards-incompatible
20
+
21
+ - title: New Features 🎉
22
+ labels:
23
+ - enhancement
24
+
25
+ - title: Bug Fixes 🐛
26
+ labels:
27
+ - bug
28
+
29
+ - title: Documentation Updates 📚
30
+ labels:
31
+ - documentation
32
+ - docs
33
+
34
+ - title: Dependency Updates ⬆️
35
+ labels:
36
+ - dependencies
37
+
38
+ - title: Other Changes
39
+ labels:
40
+ - "*"
@@ -0,0 +1,18 @@
1
+ ---
2
+ name: 🏷️ Pull Request Labeler
3
+
4
+ on:
5
+ - pull_request_target
6
+
7
+ permissions:
8
+ contents: read
9
+
10
+ jobs:
11
+ labeler:
12
+ name: Labeler
13
+ runs-on: ubuntu-latest
14
+ if: github.repository_owner == 'voxpupuli' || github.repository_owner == 'OpenVoxProject'
15
+ permissions:
16
+ pull-requests: write
17
+ steps:
18
+ - uses: actions/labeler@bf12e9b00b37c5c0ca2b87b79b2daf7891dbda13 # v7.0.0
@@ -15,7 +15,7 @@ jobs:
15
15
  name: Build the gem
16
16
  runs-on: ubuntu-24.04
17
17
  steps:
18
- - uses: actions/checkout@v6
18
+ - uses: actions/checkout@v7
19
19
  - name: Install Ruby
20
20
  uses: ruby/setup-ruby@v1
21
21
  with:
@@ -76,7 +76,7 @@ jobs:
76
76
  uses: actions/download-artifact@v8
77
77
  with:
78
78
  name: gem-artifact
79
- - uses: rubygems/configure-rubygems-credentials@v1.0.0
79
+ - uses: rubygems/configure-rubygems-credentials@v2.2.0
80
80
  - name: Publish gem to rubygems.org
81
81
  shell: bash
82
82
  run: gem push *.gem
@@ -10,11 +10,16 @@ on:
10
10
  env:
11
11
  BUNDLE_WITHOUT: release
12
12
 
13
+ permissions:
14
+ contents: read
15
+
13
16
  jobs:
14
- rubocop:
17
+ rubocop_and_matrix:
15
18
  runs-on: ubuntu-latest
19
+ outputs:
20
+ ruby: ${{ steps.ruby.outputs.versions }}
16
21
  steps:
17
- - uses: actions/checkout@v6
22
+ - uses: actions/checkout@v7
18
23
  - name: Setup ruby
19
24
  uses: ruby/setup-ruby@v1
20
25
  with:
@@ -22,21 +27,21 @@ jobs:
22
27
  bundler-cache: true
23
28
  - name: Run rake rubocop
24
29
  run: bundle exec rake rubocop
30
+ - id: ruby
31
+ uses: voxpupuli/ruby-version@be0467ce7366d206b2e87f5b7a032a030120b7cb # 2.0.0
25
32
  test:
26
33
  runs-on: ubuntu-latest
34
+ needs: rubocop_and_matrix
27
35
  strategy:
28
36
  fail-fast: false
29
37
  matrix:
30
- include:
31
- - ruby: "3.2"
32
- coverage: "yes"
33
- - ruby: "3.3"
34
- - ruby: "3.4"
35
- env:
36
- COVERAGE: ${{ matrix.coverage }}
38
+ ruby: ${{ fromJSON(needs.rubocop_and_matrix.outputs.ruby) }}
39
+
37
40
  name: Ruby ${{ matrix.ruby }}
41
+ env:
42
+ COVERAGE: ${{ (matrix.ruby == '3.2' || matrix.ruby == '4.0') && 'yes' || 'no' }}
38
43
  steps:
39
- - uses: actions/checkout@v6
44
+ - uses: actions/checkout@v7
40
45
  - name: Install Ruby ${{ matrix.ruby }}
41
46
  uses: ruby/setup-ruby@v1
42
47
  with:
@@ -47,10 +52,14 @@ jobs:
47
52
  - name: Verify gem builds
48
53
  run: gem build --strict --verbose *.gemspec
49
54
  tests:
55
+ if: always()
50
56
  needs:
51
- - rubocop
57
+ - rubocop_and_matrix
52
58
  - test
53
- runs-on: ubuntu-latest
59
+ runs-on: ubuntu-26.04
54
60
  name: Test suite
55
61
  steps:
56
- - run: echo Test suite completed
62
+ - name: Decide whether the needed jobs succeeded or failed
63
+ uses: re-actors/alls-green@release/v1
64
+ with:
65
+ jobs: ${{ toJSON(needs) }}
data/CHANGELOG.md CHANGED
@@ -2,9 +2,36 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
- ## [2.1.0](https://github.com/voxpupuli/puppet-ghostbuster/tree/2.1.0) (2026-02-27)
5
+ ## [2.3.0](https://github.com/voxpupuli/puppet-ghostbuster/tree/2.3.0) (2026-12-04)
6
6
 
7
- [Full Changelog](https://github.com/voxpupuli/puppet-ghostbuster/compare/v2.0.0...2.1.0)
7
+ [Full Changelog](https://github.com/voxpupuli/puppet-ghostbuster/compare/v2.2.0...2.3.0)
8
+
9
+ **Implemented enhancements:**
10
+
11
+ - support check\_ghostbuster\_files for directories with recurse = 'remote' [\#97](https://github.com/voxpupuli/puppet-ghostbuster/pull/97) ([batmac](https://github.com/batmac))
12
+
13
+ **Fixed bugs:**
14
+
15
+ - fix: detect unused Hiera files with puppet-lint 5.1 [\#133](https://github.com/voxpupuli/puppet-ghostbuster/pull/133) ([rwaffen](https://github.com/rwaffen))
16
+
17
+ ## [v2.2.0](https://github.com/voxpupuli/puppet-ghostbuster/tree/v2.2.0) (2026-10-08)
18
+
19
+ [Full Changelog](https://github.com/voxpupuli/puppet-ghostbuster/compare/v2.1.0...v2.2.0)
20
+
21
+ **Implemented enhancements:**
22
+
23
+ - feat: make it work with openvox9 [\#129](https://github.com/voxpupuli/puppet-ghostbuster/pull/129) ([rwaffen](https://github.com/rwaffen))
24
+
25
+ **Merged pull requests:**
26
+
27
+ - Update voxpupuli-rubocop requirement from ~\> 5.2.0 to ~\> 5.3.0 [\#126](https://github.com/voxpupuli/puppet-ghostbuster/pull/126) ([dependabot[bot]](https://github.com/apps/dependabot))
28
+ - Update json requirement from \>= 2.0, \< 3.0 to \>= 2.0, \< 4.0 [\#124](https://github.com/voxpupuli/puppet-ghostbuster/pull/124) ([dependabot[bot]](https://github.com/apps/dependabot))
29
+ - Update github\_changelog\_generator requirement from ~\> 1.16.4 to ~\> 1.18 [\#120](https://github.com/voxpupuli/puppet-ghostbuster/pull/120) ([dependabot[bot]](https://github.com/apps/dependabot))
30
+ - Update voxpupuli-rubocop requirement from ~\> 5.1.0 to ~\> 5.2.0 [\#119](https://github.com/voxpupuli/puppet-ghostbuster/pull/119) ([dependabot[bot]](https://github.com/apps/dependabot))
31
+
32
+ ## [v2.1.0](https://github.com/voxpupuli/puppet-ghostbuster/tree/v2.1.0) (2026-02-27)
33
+
34
+ [Full Changelog](https://github.com/voxpupuli/puppet-ghostbuster/compare/v2.0.0...v2.1.0)
8
35
 
9
36
  **Merged pull requests:**
10
37
 
data/Gemfile CHANGED
@@ -4,9 +4,11 @@ source ENV['GEM_SOURCE'] || 'https://rubygems.org'
4
4
 
5
5
  gemspec
6
6
 
7
- group :release do
7
+ gem 'openvox', ENV['OPENVOX_VERSION'] if ENV['OPENVOX_VERSION']
8
+
9
+ group :release, optional: true do
8
10
  gem 'faraday-retry', '~> 2.1', require: false
9
- gem 'github_changelog_generator', '~> 1.16.4', require: false
11
+ gem 'github_changelog_generator', '~> 1.18', require: false
10
12
  end
11
13
 
12
14
  group :coverage, optional: ENV['COVERAGE'] != 'yes' do
data/README.md CHANGED
@@ -16,6 +16,8 @@ This gems helps puppet users to find dead code by displaying unused classes, def
16
16
 
17
17
  This gems only support PuppetDB APi v4 (PuppetDB 3+)
18
18
 
19
+ Supports OpenVox 8 and 9 with Ruby 3.2 or newer.
20
+
19
21
  Usage
20
22
  -----
21
23
 
@@ -34,7 +36,10 @@ The location of the `hiera.yaml` file. Defaults to `./hiera.yaml` or `/etc/puppe
34
36
 
35
37
  ### PUPPETDB_URL
36
38
 
37
- The url or the PuppetDB. Defaults to `http://puppetdb:8080`
39
+ The URL of PuppetDB.
40
+ If unset, uses the first server URL from `puppetdb.conf` when the PuppetDB integration is available,
41
+ otherwise `https://<server>:8081` using the `server` setting in `puppet.conf`.
42
+ OpenVox 9 has no default `server`, so configure it or set `PUPPETDB_URL` explicitly.
38
43
 
39
44
  ### PUPPETDB_CACERT_FILE
40
45
 
@@ -5,17 +5,22 @@ class PuppetGhostbuster
5
5
  class PuppetDB
6
6
  Puppet.initialize_settings
7
7
 
8
- begin
8
+ def self.server_url
9
+ return ENV['PUPPETDB_URL'] if ENV['PUPPETDB_URL'] && !ENV['PUPPETDB_URL'].empty?
10
+
9
11
  require 'puppet/util/puppetdb'
10
- @@puppetdb = Puppet::Util::Puppetdb.config.server_urls[0]
12
+ Puppet::Util::Puppetdb.config.server_urls[0]
11
13
  rescue LoadError
12
- @@puppetdb = "https://#{Puppet[:server]}:8081"
14
+ server = Puppet[:server]
15
+ raise ArgumentError, 'Set PUPPETDB_URL or configure server in puppet.conf to connect to PuppetDB' if server.nil? || server.empty?
16
+
17
+ "https://#{server}:8081"
13
18
  end
14
19
 
15
20
  def self.client
16
21
  @@client ||= begin
17
22
  options = {
18
- server: ENV['PUPPETDB_URL'] || @@puppetdb,
23
+ server: server_url,
19
24
  }
20
25
 
21
26
  if ENV['PE_TOKEN']
@@ -1,3 +1,3 @@
1
1
  class PuppetGhostbuster
2
- VERSION = '2.1.0'.freeze
2
+ VERSION = '2.3.0'.freeze
3
3
  end
@@ -35,7 +35,7 @@ PuppetLint.new_check(:ghostbuster_files) do
35
35
  query = "resources[title] {
36
36
  (parameters.source = 'puppet:///modules/#{module_name}/#{dir_name}'
37
37
  or parameters.source = 'puppet:///modules/#{module_name}/#{dir_name}/')
38
- and parameters.recurse = true
38
+ and (parameters.recurse = true or parameters.recurse = 'remote')
39
39
  and nodes { deactivated is null } }"
40
40
  return if puppetdb.client.request('', query).data.size > 0
41
41
 
@@ -1,4 +1,11 @@
1
1
  class PuppetLint::Checks
2
+ # puppet-lint 5.1 filters YAML checks through a frozen allowlist.
3
+ if const_defined?(:YAML_COMPATIBLE_CHECKS, false)
4
+ yaml_checks = const_get(:YAML_COMPATIBLE_CHECKS)
5
+ remove_const(:YAML_COMPATIBLE_CHECKS)
6
+ const_set(:YAML_COMPATIBLE_CHECKS, (yaml_checks | [:ghostbuster_hiera_files]).freeze)
7
+ end
8
+
2
9
  def load_data(path, content)
3
10
  lexer = PuppetLint::Lexer.new
4
11
  PuppetLint::Data.path = path
@@ -28,67 +35,114 @@ PuppetLint.new_check(:ghostbuster_hiera_files) do
28
35
  hiera.dig('defaults', 'datadir') || 'data'
29
36
  end
30
37
 
38
+ def hiera_datadir(hierarchy)
39
+ (hierarchy['datadir'] || default_datadir).chomp('/')
40
+ end
41
+
42
+ def hiera_data_file?(path)
43
+ # Only consider actual hiera data files (keep noise like .gitignore out).
44
+ %w[.yaml .yml .eyaml].include?(File.extname(path))
45
+ end
46
+
31
47
  def path_in_datadirs?(path)
32
48
  return false unless hiera
33
49
 
34
- @data_dirs ||= hiera['hierarchy'].map { |h| (h['datadir'] || default_datadir).chomp('/') }.uniq
35
- path.match?(%(./#{Regexp.union(@data_dirs)}/))
50
+ @data_dirs ||= hiera['hierarchy'].map { |h| hiera_datadir(h) }.uniq
51
+ # Match a data directory at the path start or after '/'.
52
+ # Regexp.union combines directory alternatives and escapes special characters.
53
+ # Example: ['data', 'private.d'] becomes %r{data|private\.d}.
54
+ path.match?(%r{(^|/)#{Regexp.union(@data_dirs)}/})
36
55
  end
37
56
 
38
- def regexprs
39
- regs = {}
57
+ def hiera_patterns
58
+ patterns = []
59
+
40
60
  hiera['hierarchy'].each do |hierarchy|
41
- path_datadir = Regexp.escape((hierarchy['datadir'] || default_datadir).chomp('/'))
61
+ path_datadir = hiera_datadir(hierarchy)
42
62
  ([*hierarchy['path']] + [*hierarchy['paths']]).each do |level|
63
+ next if level.nil? || level.empty?
64
+
43
65
  level = File.join(path_datadir, level)
44
- regex = Regexp.new(level.gsub(/%\{(::)?(trusted|server_facts|facts)\.[^}]+\}/, '(.+)').gsub(/%\{[^}]+\}/, '.+'))
45
- facts = level.match(regex).captures.map { |f| f[/%{(::)?(trusted|server_facts|facts)\.(.+)}/, 3] }
46
- regs[regex] = facts
66
+
67
+ # Extract the names inside %{...} placeholders.
68
+ # Example: 'data/%{trusted.certname}/%{facts.os}.yaml' becomes ['certname', ['fact', 'os']].
69
+ fields = level.scan(/%\{([^}]+)\}/).flatten.map { |placeholder| puppetdb_field(placeholder) }
70
+
71
+ # Replace escaped placeholders with captures for non-empty path values.
72
+ # Example: 'data/%{trusted.certname}.yaml' becomes the regex source 'data/(.+)\.yaml'.
73
+ escaped_level = Regexp.escape(level).gsub(/%\\\{[^}]+\\\}/, '(.+)')
74
+
75
+ patterns << { regex: Regexp.new(escaped_level), fields: fields }
47
76
  end
48
77
  end
49
- regs
78
+
79
+ patterns
80
+ end
81
+
82
+ def puppetdb_field(placeholder)
83
+ # Remove a leading Puppet top-scope prefix.
84
+ name = placeholder.sub(/^::/, '')
85
+
86
+ case name
87
+ when 'certname', 'clientcert', 'fqdn', 'trusted.certname'
88
+ 'certname'
89
+ # Capture the complete fact name after 'facts.' or 'server_facts.'.
90
+ # Example: 'facts.os' becomes ['fact', 'os']; 'server_facts.environment' becomes ['fact', 'environment'].
91
+ when /\A(?:facts|server_facts)\.(.+)\z/
92
+ ['fact', Regexp.last_match(1)]
93
+ # Match names starting with 'trusted.'.
94
+ when /\Atrusted\./
95
+ # Other trusted fields cannot be queried reliably through PuppetDB.
96
+ nil
97
+ else
98
+ ['fact', name]
99
+ end
100
+ end
101
+
102
+ def coerce_fact_value(value)
103
+ case value
104
+ when 'true'
105
+ true
106
+ when 'false'
107
+ false
108
+ else
109
+ value
110
+ end
50
111
  end
51
112
 
52
113
  def check
53
- return unless path_in_datadirs? path
114
+ # Provides the path of the file being analysed as it was provided to puppet-lint to the check plugins.
115
+ # path() is a method
116
+ return unless path_in_datadirs?(path) && hiera_data_file?(path)
54
117
 
55
118
  puppetdb = PuppetGhostbuster::PuppetDB.new
119
+ matched_supported = false
120
+ matched_unsupported = false
56
121
 
57
- regexprs.each do |k, v|
58
- m = path.match(k)
59
- next if m.nil?
60
-
61
- return if m.captures.size == 0
62
-
63
- if m.captures.size > 1
64
- i = 0
65
- query = [:and]
66
- m.captures.map do |value|
67
- query << if v[i] == 'certname'
68
- [:'=', v[i], value]
69
- else
70
- [:'=', ['fact', v[i]], value]
71
- end
72
- i += 1
73
- end
74
- elsif v[0] == 'certname'
75
- query = [:'=', 'certname', m.captures[0]]
76
- else
77
- value = if m.captures[0] == 'true'
78
- true
79
- elsif m.captures[0] == 'false'
80
- false
81
- else
82
- m.captures[0]
83
- end
84
- query = [:'=', ['fact', v[0]], value]
85
- end
122
+ hiera_patterns.each do |pattern|
123
+ match = path.match(pattern[:regex])
124
+ next unless match
86
125
 
87
- query = [:and, query, [:'=', 'deactivated', nil]]
126
+ if pattern[:fields].include?(nil)
127
+ matched_unsupported = true
128
+ next
129
+ end
88
130
 
89
- return if puppetdb.client.request('nodes', query).data.size > 0
131
+ matched_supported = true
132
+ conditions = pattern[:fields].each_with_index.map do |field, index|
133
+ value = match.captures[index]
134
+ value = coerce_fact_value(value) unless field == 'certname'
135
+ [:'=', field, value]
136
+ end
137
+ # Fixed paths such as common.yaml only require an active node.
138
+ conditions << [:'=', 'deactivated', nil]
139
+ query = conditions.reduce { |left, right| [:and, left, right] }
140
+ return if puppetdb.client.request('nodes', query).data.any?
90
141
  end
91
142
 
143
+ # Keep files whose only matches use unsupported trusted fields.
144
+ return if matched_unsupported && !matched_supported
145
+
92
146
  notify :warning, {
93
147
  message: "Hiera File #{path} seems unused",
94
148
  line: 1,
@@ -25,10 +25,10 @@ Gem::Specification.new do |s|
25
25
  s.add_development_dependency 'rspec', '~> 3.0'
26
26
  s.add_development_dependency 'rspec-collection_matchers', '~> 1.0'
27
27
  s.add_development_dependency 'rspec-its', '~> 2.0'
28
- s.add_development_dependency 'voxpupuli-rubocop', '~> 5.1.0'
28
+ s.add_development_dependency 'voxpupuli-rubocop', '~> 5.3.0'
29
29
 
30
- s.add_dependency 'json', '>= 2.0', '< 3.0'
31
- s.add_dependency 'openvox', '>= 8.0', '< 9.0'
30
+ s.add_dependency 'json', '>= 2.0', '< 4.0'
31
+ s.add_dependency 'openvox', '>= 8.0', '< 10.0'
32
32
  s.add_dependency 'puppetdb-ruby', '~> 1.1', '>= 1.1.1'
33
33
  s.add_dependency 'syslog', '>= 0.3', '< 0.5'
34
34
  end
@@ -1,16 +1,21 @@
1
1
  [
2
2
  {
3
- "certname": "bar.example.com"
3
+ "certname": "bar.example.com",
4
+ "fact": {
5
+ "domain": "example.com"
6
+ }
4
7
  },
5
8
  {
6
9
  "certname": "virtual.example.com",
7
10
  "fact": {
11
+ "domain": "example.com",
8
12
  "is_virtual": true
9
13
  }
10
14
  },
11
15
  {
12
16
  "certname": "prod.example.com",
13
17
  "fact": {
18
+ "domain": "example.com",
14
19
  "environment": "production"
15
20
  }
16
21
  }
@@ -7,6 +7,92 @@ ENV['HIERA_YAML_PATH'] = './spec/fixtures/hiera.yaml'
7
7
  describe 'ghostbuster_hiera_files' do
8
8
  let(:code) { '' }
9
9
 
10
+ context 'when run through PuppetLint for a YAML file' do
11
+ let(:linter) { PuppetLint.new }
12
+ let(:code) { "---\ntest: true\n" }
13
+
14
+ before do
15
+ allow(ENV).to receive(:fetch).and_call_original
16
+ allow(ENV).to receive(:fetch).with('HIERA_YAML_PATH', false).and_return('./spec/fixtures/hiera.yaml')
17
+ allow(PuppetLint.configuration).to receive(:checks).and_return([:ghostbuster_hiera_files])
18
+ linter.path = path
19
+ linter.code = code
20
+ linter.run
21
+ end
22
+
23
+ context 'when the certname does not exist' do
24
+ let(:path) { './data/nodes/rwaffen.betadots.example.com.yaml' }
25
+
26
+ it 'warns about the unused file' do
27
+ expect(linter.problems).to contain_warning("Hiera File #{path} seems unused")
28
+ end
29
+ end
30
+
31
+ context 'when the certname exists' do
32
+ let(:path) { './data/nodes/bar.example.com.yaml' }
33
+
34
+ it 'does not report a problem' do
35
+ expect(linter.problems).to be_empty
36
+ end
37
+ end
38
+ end
39
+
40
+ context 'with a custom hierarchy' do
41
+ before do
42
+ allow(YAML).to receive(:load_file).and_return('hierarchy' => [{ 'paths' => hierarchy_paths }])
43
+ end
44
+
45
+ context 'when multiple placeholders match the same node' do
46
+ let(:hierarchy_paths) { ['nodes/%{::trusted.certname}-%{facts.is_virtual}.yaml'] }
47
+ let(:path) { './data/nodes/virtual.example.com-true.yaml' }
48
+
49
+ it 'does not report a problem' do
50
+ expect(problems).to be_empty
51
+ end
52
+ end
53
+
54
+ context 'when the fact value does not match the node' do
55
+ let(:hierarchy_paths) { ['nodes/%{::trusted.certname}-%{facts.is_virtual}.yaml'] }
56
+ let(:path) { './data/nodes/virtual.example.com-false.yaml' }
57
+
58
+ it 'warns about the unused file' do
59
+ expect(problems).to contain_warning("Hiera File #{path} seems unused")
60
+ end
61
+ end
62
+
63
+ context 'with a fixed path' do
64
+ let(:hierarchy_paths) { ['common.yaml'] }
65
+ let(:path) { './data/common.yaml' }
66
+
67
+ it 'does not report a problem when active nodes exist' do
68
+ expect(problems).to be_empty
69
+ end
70
+
71
+ it 'warns when no active nodes exist' do
72
+ allow(PuppetGhostbuster::PuppetDB.client).to receive(:request).and_return(PuppetDB::Response.new([], 0))
73
+ expect(problems).to contain_warning("Hiera File #{path} seems unused")
74
+ end
75
+ end
76
+
77
+ context 'with an unsupported trusted field' do
78
+ let(:hierarchy_paths) { ['nodes/%{trusted.extensions.role}.yaml'] }
79
+ let(:path) { './data/nodes/unknown.yaml' }
80
+
81
+ it 'keeps potentially used files' do
82
+ expect(problems).to be_empty
83
+ end
84
+ end
85
+
86
+ context 'when supported and unsupported hierarchy entries match' do
87
+ let(:hierarchy_paths) { ['nodes/%{trusted.extensions.role}.yaml', 'nodes/%{trusted.certname}.yaml'] }
88
+ let(:path) { './data/nodes/unknown.yaml' }
89
+
90
+ it 'warns when the supported query finds no node' do
91
+ expect(problems).to contain_warning("Hiera File #{path} seems unused")
92
+ end
93
+ end
94
+ end
95
+
10
96
  context 'with fix disabled' do
11
97
  context 'when a certname file is NOT used' do
12
98
  let(:path) { './data/nodes/foo.example.com.yaml' }
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe PuppetGhostbuster::PuppetDB do
6
+ describe '.server_url' do
7
+ around do |example|
8
+ original_url = ENV.fetch('PUPPETDB_URL', nil)
9
+ ENV.delete('PUPPETDB_URL')
10
+ example.run
11
+ ensure
12
+ if original_url
13
+ ENV['PUPPETDB_URL'] = original_url
14
+ else
15
+ ENV.delete('PUPPETDB_URL')
16
+ end
17
+ end
18
+
19
+ it 'uses the explicit URL without loading the PuppetDB integration' do
20
+ ENV['PUPPETDB_URL'] = 'https://database.example.com:8081'
21
+ allow(described_class).to receive(:require)
22
+ expect(described_class.server_url).to eq('https://database.example.com:8081')
23
+ expect(described_class).not_to have_received(:require)
24
+ end
25
+
26
+ it 'uses the first URL from the PuppetDB integration' do
27
+ config = Struct.new(:server_urls).new(['https://database.example.com:8081'])
28
+ integration = Struct.new(:config).new(config)
29
+ stub_const('Puppet::Util::Puppetdb', integration)
30
+ allow(described_class).to receive(:require).with('puppet/util/puppetdb')
31
+ expect(described_class.server_url).to eq('https://database.example.com:8081')
32
+ end
33
+
34
+ context 'without the PuppetDB integration' do
35
+ before do
36
+ allow(described_class).to receive(:require).with('puppet/util/puppetdb').and_raise(LoadError)
37
+ end
38
+
39
+ it 'uses the configured Puppet server' do
40
+ allow(Puppet).to receive(:[]).with(:server).and_return('compiler.example.com')
41
+ expect(described_class.server_url).to eq('https://compiler.example.com:8081')
42
+ end
43
+
44
+ it 'reports a missing server instead of constructing an invalid URL' do
45
+ allow(Puppet).to receive(:[]).with(:server).and_return('')
46
+ expect { described_class.server_url }.to raise_error(ArgumentError, /Set PUPPETDB_URL/)
47
+ end
48
+
49
+ it 'treats an empty explicit URL as unset' do
50
+ ENV['PUPPETDB_URL'] = ''
51
+ allow(Puppet).to receive(:[]).with(:server).and_return('compiler.example.com')
52
+ expect(described_class.server_url).to eq('https://compiler.example.com:8081')
53
+ end
54
+ end
55
+ end
56
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require 'puppet-lint'
2
2
  require 'jgrep'
3
3
 
4
+ ENV['PUPPETDB_URL'] ||= 'https://puppetdb.example.com:8081'
5
+
4
6
  PuppetLint::Plugins.load_spec_helper
5
7
 
6
8
  class PuppetDB::Client
@@ -14,7 +16,7 @@ class PuppetDB::Client
14
16
 
15
17
  def process_pql_kv(q)
16
18
  k, v = q.split(/\s*=\s*/)
17
- ".#{k}=#{v}"
19
+ ".#{k.strip}=#{v.strip}"
18
20
  end
19
21
 
20
22
  def pql_to_jgrep(query)
@@ -32,6 +34,7 @@ class PuppetDB::Client
32
34
 
33
35
  jgrep_query_and_parts = []
34
36
  query.split(/\s+and\s+/).each do |q|
37
+ q.strip!
35
38
  newq = ''
36
39
  if q.start_with?('(')
37
40
  newq << '('
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet-ghostbuster
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Camptocamp
@@ -138,14 +138,14 @@ dependencies:
138
138
  requirements:
139
139
  - - "~>"
140
140
  - !ruby/object:Gem::Version
141
- version: 5.1.0
141
+ version: 5.3.0
142
142
  type: :development
143
143
  prerelease: false
144
144
  version_requirements: !ruby/object:Gem::Requirement
145
145
  requirements:
146
146
  - - "~>"
147
147
  - !ruby/object:Gem::Version
148
- version: 5.1.0
148
+ version: 5.3.0
149
149
  - !ruby/object:Gem::Dependency
150
150
  name: json
151
151
  requirement: !ruby/object:Gem::Requirement
@@ -155,7 +155,7 @@ dependencies:
155
155
  version: '2.0'
156
156
  - - "<"
157
157
  - !ruby/object:Gem::Version
158
- version: '3.0'
158
+ version: '4.0'
159
159
  type: :runtime
160
160
  prerelease: false
161
161
  version_requirements: !ruby/object:Gem::Requirement
@@ -165,7 +165,7 @@ dependencies:
165
165
  version: '2.0'
166
166
  - - "<"
167
167
  - !ruby/object:Gem::Version
168
- version: '3.0'
168
+ version: '4.0'
169
169
  - !ruby/object:Gem::Dependency
170
170
  name: openvox
171
171
  requirement: !ruby/object:Gem::Requirement
@@ -175,7 +175,7 @@ dependencies:
175
175
  version: '8.0'
176
176
  - - "<"
177
177
  - !ruby/object:Gem::Version
178
- version: '9.0'
178
+ version: '10.0'
179
179
  type: :runtime
180
180
  prerelease: false
181
181
  version_requirements: !ruby/object:Gem::Requirement
@@ -185,7 +185,7 @@ dependencies:
185
185
  version: '8.0'
186
186
  - - "<"
187
187
  - !ruby/object:Gem::Version
188
- version: '9.0'
188
+ version: '10.0'
189
189
  - !ruby/object:Gem::Dependency
190
190
  name: puppetdb-ruby
191
191
  requirement: !ruby/object:Gem::Requirement
@@ -232,6 +232,9 @@ extensions: []
232
232
  extra_rdoc_files: []
233
233
  files:
234
234
  - ".github/dependabot.yml"
235
+ - ".github/labeler.yml"
236
+ - ".github/release.yml"
237
+ - ".github/workflows/labeler.yml"
235
238
  - ".github/workflows/release.yml"
236
239
  - ".github/workflows/test.yml"
237
240
  - ".gitignore"
@@ -279,6 +282,7 @@ files:
279
282
  - spec/puppet-lint/plugins/ghostbuster_hiera_files_spec.rb
280
283
  - spec/puppet-lint/plugins/ghostbuster_templates_spec.rb
281
284
  - spec/puppet-lint/plugins/ghostbuster_types_spec.rb
285
+ - spec/puppet_ghostbuster/puppet_db_spec.rb
282
286
  - spec/spec_helper.rb
283
287
  homepage: http://github.com/voxpupuli/puppet-ghostbuster
284
288
  licenses:
@@ -298,7 +302,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
298
302
  - !ruby/object:Gem::Version
299
303
  version: '0'
300
304
  requirements: []
301
- rubygems_version: 4.0.3
305
+ rubygems_version: 4.0.20
302
306
  specification_version: 4
303
307
  summary: Dead code detector for Puppet
304
308
  test_files: []