Giter Club home page Giter Club logo

Comments (4)

benhocker avatar benhocker commented on July 17, 2024 1

Got it working doing this. Thanks!

      describe "profiles class without any parameters" do
        case facts[:operatingsystemmajrelease]
          when '6'
            codename = 'Santiago'
          when '7'
            codename = 'Maipo'
        end
        let(:hiera_config) { 'spec/fixtures/hiera/hiera.yaml' }
        let(:params) {{ }}
        let(:facts) do
          facts.merge({
            :lsbdistcodename => codename,
            :concat_basedir => '/tmp',
          })
        end

from rspec-puppet-facts.

codylane avatar codylane commented on July 17, 2024

I'm seeing the same behavior when using facts.merge({ :my_custom_fact => 'value' }). That fact is being ignored. But... If I put the facts.merge({ :my_custom_fact => 'value' }) inside a it block it works fine.

I'm not sure what I'm doing wrong or if this is expected behavior. Here is an example.

describe 'profiles::base' do
  on_supported_os.each do |os, facts|
    context "on #{os}" do
      context 'with defaults for all parameters' do
        let(:facts) do
          facts.merge({
            :my_custom_fact => "value",
          })
        end

        it do
          expect(facts[:my_custom_fact]).to eq 'value'
        end
      end
    end
  end
end

Here's the failure

1) profiles::base on centos-6-x86_64 with defaults for all parameters should eq "value"
     Failure/Error: expect(facts[:my_custom_fact]).to eq 'value'

       expected: "value"
            got: nil

       (compared using ==)
     # ./spec/classes/base_spec.rb:20:in `block (5 levels) in <top (required)>

But if I do this instead, the test passes. Notice that I had to use facts.merge! instead of facts.merge.

describe 'profiles::base' do
  on_supported_os.each do |os, facts|
    context "on #{os}" do
      context 'with defaults for all parameters' do

        it do
          facts.merge!({
              :my_custom_fact => "value",
          })
          expect(facts[:my_custom_fact]).to eq 'value'
        end
      end
    end
  end
end

My ruby is not great, so I'm not sure I can identify the culprit or if I'm just mis-understanding something about rspec-puppet-facts. On a side note, removing the on_supported_os.each do |os, facts| block and going back to the old method, this works.

describe 'profiles::base' do context 'with defaults for all parameters' do let(:facts) do { :my_custom_fact => 'value' } end it do expect(facts[:my_custom_fact]).to eq 'value' end end end

from rspec-puppet-facts.

codylane avatar codylane commented on July 17, 2024

After thinking about this for a moment, I think I understand what is happening. When we define the block on_supported_os.each do |os, facts| this essentially overrides the let(:facts) when defined further down in a describe or context block. I ended up re-naming the facts loop variable to default_facts and I can indeed, merge custom facts in another describe or context block.

Here's a working example:

describe 'profiles::base' do
  on_supported_os.each do |os, default_facts|
    context "on #{os}" do

      context 'with defaults for all parameters' do
        let(:facts) do
          default_facts.merge({
            :my_custom_fact => 'value'
          })
        end
        it do
          expect(facts[:my_custom_fact]).to eq 'value'
        end

        it do
          expect(facts[:osfamily]).to eq 'RedHat'
        end
      end
    end
  end
end

from rspec-puppet-facts.

mcanevet avatar mcanevet commented on July 17, 2024

@phathocker lsb facts are not availble on RedHat untill you install the redhat-lsb-code package, which is not installed by default, so they are not included in the facts database (facterdb). However you can appends the missing facts to the facts hash using this https://github.com/mcanevet/rspec-puppet-facts/blob/master/README.md#append-some-facts.

from rspec-puppet-facts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.