puppetrspec-puppet

Puppet: variable value in test file


I'm writing some tests for puppet and in my init_spec.rb file I want to use a variable that is declared in the default_facts.yml file. How could I import the value of that variable without having to declare it in the init_spec.rb file.

Thanks in advance!


Solution

  • In general, you would be able to access that data inside the RSpec.configuration object.

    Supposing you had a default facts file like this:

    ▶ cat spec/default_facts.yml 
    # Use default_module_facts.yml for module specific facts.
    #
    # Facts specified here will override the values provided by rspec-puppet-facts.
    ---
    concat_basedir: "/tmp"
    ipaddress: "172.16.254.254"
    is_pe: false
    macaddress: "AA:AA:AA:AA:AA:AA"
    

    You could address that data in your tests like this:

    it 'ipaddress default fact' do
      expect(RSpec.configuration.default_facts['ipaddress']).to eq '172.16.254.254'
    end
    

    (I am assuming of course that your default facts file was set up correctly, e.g. by PDK.)

    If instead you just want a general way to access the data in any arbitrary YAML file, you can also do this:

    ▶ cat spec/fixtures/mydata.yml 
    ---
    foo: bar
    

    Then in your tests you can write:

    require 'yaml'
    mydata = YAML.load_file('spec/fixtures/mydata.yml')
    
    describe 'test' do
      it 'foo' do
        expect(mydata['foo']).to eq 'bar'
      end
    end