Ask Your Question
0

Can we use multiple hiera files in puppet-rspec?

asked 2015-12-29 02:53:01 -0500

karthikeayan gravatar image

updated 2015-12-29 13:49:02 -0500

GregLarkin gravatar image

hiera.yaml

---
:backends:
  - yaml
:yaml:   
  :datadir: spec/fixtures/hieradata
  :hierarchy:
    - common
    - linux01
    - linux02
    - linux03

linux01.yaml:

  hieravariable: lx01

linux02.yaml:

  hieravariable: lx02

linux03.yaml:

  hieravariable: lx03

rspec:

it do
  should contain_exec('Create something lx01')
end
it do
  should contain_exec('Create something lx02')
end
it do
  should contain_exec('Create something lx03')
end

manifest:

  exec { "Create something ${hieravariable}":
    command   => "some command",
    user      => 'user01',
  }

When I execute my rpsec, only 'Create something lx01' is getting passed. lx02 and lx03 are getting failed. How to use multiple hiera files?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2015-12-29 14:56:16 -0500

Marek Maksimczyk gravatar image

updated 2015-12-29 14:58:14 -0500

Your hiera's hierarchy is that hiera will always search files in this order:

  1. common .yaml
  2. linux01.yaml
  3. linux02.yaml
  4. linux03.yaml

beacause value is found in linux01.yaml (I think you used $hieravariable = hiera('hieravariable')) two other files are not checked anymore.

Try something like it: Hiera's hierarchy:

- %{operatingsystem}
- common

common.yaml should be checked as last one.

Three files Debian.yaml, Redhat.yaml, Ubuntu.yaml (I'm not sure if its correct value for operating systems but for tests is good enough) and use context:

    context 'for Redhat OS' do
      let(:facts) { {:operatingsystem => 'Redhat'} }

      it do
       should contain_exec('Create something lx01')
      end
     end

Code wasn't tested so it can be bugy.

edit flag offensive delete link more
0

answered 2017-01-12 14:51:57 -0500

DarylW gravatar image

updated 2017-01-12 14:52:34 -0500

This is a bit of a necro, but it highlights an interesting case.

What are you trying to accomplish?

If you want to evaluate different values for different nodes... You can move your linux01.yaml etc under hieradata/nodes/ and add nodes/%{::certname} to your hiera.yaml Then in your test you can set the 'node' you are on with let(:node){ 'linux01' }, let hiera 'pick' the node specific data from the hierarchy for the test.

Otherwise, you may be able to mock out different hiera configs and data using something like https://github.com/bobtfish/hiera-pup... .. I haven't used it, and I don't know if it is still maintained, or if there is a better gem available.

If you are trying to accomplish running different tests with different sets of hiera data, you can have a separate hiera.yaml (and/or separate hieradata directory referenced), and set the hieraconfig in the context for each of your tests.. I'm not sure how useful this scenario is for your directly but...

(also, common should be below any other values)

hiera01.yaml

---
:backends:
  - yaml
:yaml:   
  :datadir: spec/fixtures/hieradata1
  :hierarchy:
    - linux01
    - common

hiera02.yaml

---
:backends:
  - yaml
:yaml:   
  :datadir: spec/fixtures/hieradata1
  :hierarchy:
    - linux02
    - common

hiera03.yaml

---
:backends:
  - yaml
:yaml:   
  :datadir: spec/fixtures/hieradata1
  :hierarchy:
    - linux03
    - common

and in your tests you could do

describe 'using the hiera01.yaml' do
  let(:hiera_config) { 'spec/fixtures/hiera01.yaml' }
  it do
    should contain_exec('Create something lx01')
  end
end
describe 'using the hiera02.yaml' do
  let(:hiera_config) { 'spec/fixtures/hiera02.yaml' }
  it do
    should contain_exec('Create something lx02')
  end
end
describe 'using the hiera03.yaml' do
  let(:hiera_config) { 'spec/fixtures/hiera03.yaml' }
  it do
    should contain_exec('Create something lx03')
  end
end
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2015-12-29 02:53:01 -0500

Seen: 141 times

Last updated: Jan 12