Skip to content

(maint) Lazy-init host and token_file in PuppetlabsInfluxdb#129

Open
actowery wants to merge 2 commits into
mainfrom
maint/lazy-init-facter-influxdb
Open

(maint) Lazy-init host and token_file in PuppetlabsInfluxdb#129
actowery wants to merge 2 commits into
mainfrom
maint/lazy-init-facter-influxdb

Conversation

@actowery
Copy link
Copy Markdown

Summary

  • Defer Facter.value(:networking)['fqdn'] and Facter.value('identity')['user'] (which set the default host and token_file for the providers) from module-load time to first read.
  • Without this, requiring lib/puppet_x/puppetlabs/influxdb/influxdb.rb raises NoMethodError: undefined method '[]' for nil whenever Facter hasn't been populated yet — which is exactly the case under rspec. That blocked every spec under spec/unit/puppet/provider/** from loading at all.
  • Lazy class-level readers (self.host, self.token_file) compute the default on first access. By that point Facter is populated in both the agent runtime and test contexts, so production behavior is identical.

Test plan

  • bundle exec rspec spec/classes spec/defines spec/functions spec/hosts — 83 examples, 0 failures, 95% rspec-puppet resource coverage (unchanged from before).
  • bundle exec rspec spec/unit — 52 examples, 0 failures. Previously these specs would not even load due to the load-time Facter call.
  • CI green.

🤖 Generated with Claude Code

Defer the Facter.value(:networking) and Facter.value('identity') calls
that set default host and token_file until first read, rather than
running them at module-load time.

Loading lib/puppet_x/puppetlabs/influxdb/influxdb.rb previously raised
NoMethodError under rspec when Facter.value(:networking) returned nil,
which prevented spec/unit/puppet/provider/**_spec.rb from loading at
all. Lazy readers compute the default on first access, by which time
Facter is populated in both agent and test contexts. Behavior on a
real agent run is unchanged.

After this change: spec/unit provider specs load and pass (52 examples,
0 failures); manifest specs unchanged (83 examples, 0 failures, 95%
resource coverage).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@actowery actowery requested review from a team and bastelfreak as code owners May 22, 2026 13:09
Wrap the equality check in parens so the cop accepts the ternary, and
cache the user lookup in a local to avoid calling Facter twice.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR prevents PuppetX::Puppetlabs::PuppetlabsInfluxdb from querying Facter at file load time by lazily computing the default host and token_file on first access, which unblocks unit specs (and any other contexts where facts aren’t populated yet when the library is required).

Changes:

  • Replace class-level attr_accessor readers for host/token_file with lazy reader methods and keep writer methods for overrides.
  • Remove module-load-time initialization of self.host and self.token_file that previously triggered Facter too early.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/puppet_x/puppetlabs/influxdb/influxdb.rb
Comment thread lib/puppet_x/puppetlabs/influxdb/influxdb.rb
@actowery actowery closed this May 26, 2026
@actowery actowery reopened this May 26, 2026
@MartyEwings MartyEwings added the bug Something isn't working label May 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants