Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling merging structured facts #109

Open
gerases opened this issue Feb 21, 2024 · 4 comments
Open

Handling merging structured facts #109

gerases opened this issue Feb 21, 2024 · 4 comments

Comments

@gerases
Copy link

gerases commented Feb 21, 2024

Use Case

I’m curious if anyone has run into the following problem. If one has their custom puppet modules in one big git repo and then also rspec-puppet tests for each, then at some point, to avoid repetition, one might want to put certain default facts in some spec helper and include it in each test, something like this:

RSpec.configure do |c|
    # ...
    c.default_facts = {
      :architecture   => 'x86_64',
      :concat_basedir => '/tmp',
      :kernel         => 'Linux',
      :os             => {
        :distro  => {
          :description => 'Blah',
        },
        :family  => 'Blah',
        :name    => 'Blah',
        :release => {
          :major => 'x',
        },
      },
    }
    #  ...
  end

This works … until some rspec within the module does this:

 describe 'mymodule' do
    let :facts do
      {
        :os => {
          :family => 'something-custom',
        },
      }
    end
  end

What happens then is that the :os dictionary in default_facts becomes overwritten (no deep merge).

Is there a known, elegant solution to this?

Describe the Solution You Would Like

An elegant way for module level tests to merge their facts with default fact dictionaries.

Describe Alternatives You've Considered

I've written a deep_merge function that deep merges the module-level dictionary with a global dictionary with some defaults. Like so:

\ describe 'mymodule' do
    let :facts do
      DEFAULT_FACTS.deep_merge do
        {
          :os => {
            :family => 'something-custom',
          },
        }
      end
    end
  end
@jordanbreen28
Copy link

Thanks for raising this @gerases!
Looks like we should be able to update the merge! to deep_merge! in each of the instances of this method

def facts_hash(node)
. I know rspec-puppet-facts has a change awaiting release in v3.0.0 which will also allow you to merge facts using the add_custom_fact method (see here), so hopefully that should serve as a workaround until we get this implemented.

@gerases
Copy link
Author

gerases commented Feb 22, 2024

Thanks @jordanbreen28, for various reasons using rspec-puppet-facts is not an option for me. I will make the change manually on my side for now. Are you going to use a gem for deep_merge just like rspec-puppet-facts does?

@jordanbreen28
Copy link

jordanbreen28 commented Feb 22, 2024

@gerases No worries! yes i believe that is the approach we will take :) (yet to test it out so could change..)

@bastelfreak
Copy link
Collaborator

@gerases can you elaborate why you cannot use rspec-puppet-facts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants