Ask Your Question
0

can I interpolate puppet variables on rspec?

asked 2016-12-20 11:08:42 -0500

Yeayu gravatar image

Hello all,

I am trying to test the file content from a template using rspec:

require 'spec_helper'
describe 'my_class' do
  context 'validate template' do
  let :params do
    {
      :my_puppet_variable => 'test'
    }
    it do
      is_expected.to contain_file('/etc/sysconfig/test').with_content(:my_puppet_variable) })
    end
  end
end

I tried wrapping the module variable using #{} to interpolate it , with no joy...

Any help would be much appreciated.

Regards

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-12-20 12:39:47 -0500

DarylW gravatar image

updated 2016-12-20 13:58:59 -0500

I'm not sure if you have a typo or if you are using it wrong. You seem to be using an inconsistent combination of do ... end and { }

do takes a block {} which should contain a hash {:key => 'value'}, looking like this

let(:params) {{ :my_puppet_variable => 'test' }}

this is also valid block syntax

let(:params) do 
  { :my_puppet_variable => 'test' }
end

Are you by chance passing the hash and the 'it' grouping as arguments to 'let'?

I would try the slight modification of your original example.

require 'spec_helper'
describe 'my_class' do
  context 'validate template' do
  let(:params) do #Added parens here, I think it was interpreting the block as an argument instead of a block
    {
      :my_puppet_variable => 'test'
    }
    end #Added this end block to match with the 'do' above
    it do
      is_expected.to contain_file('/etc/sysconfig/test').with_content(:my_puppet_variable) #There was an extra }) on this line
    end
  end
end

Also, you are passing the symbol :my_puppet_variable to the with_content method, you should probably be passing the string you wish to evaluate, 'test' This gets back to your initial question of 'can I interpolate variables'... YES, you can, but :my_puppet_variable is not a variable, it is a key to a hash. You would have two options, either declare your variable outside your hash declaration

require 'spec_helper'
describe 'my_class' do
  context 'validate template' do
    my_value = 'test'
    let(:params) {{
      :my_puppet_variable => my_value
    }}
    it  { is_expected.to contain_file('/etc/sysconfig/test').with_content(my_value) }
  end
end

or declare your hash somewhere else and pull the value out of it using your key.

require 'spec_helper'
describe 'my_class' do
  context 'validate template' do
    my_param_hash = { :my_puppet_variable => 'test' }
    let(:params) { my_param_hash }
    it  { is_expected.to contain_file('/etc/sysconfig/test').with_content(my_param_hash[:my_puppet_variable]) }
  end
end

If I was writing it, I would probably just repeat myself for ease of readability, and use the hard coded string in both places, violating 'dry' but keeping things simpler

require 'spec_helper'
describe 'my_class' do
  context 'validate template' do
    let(:params) {{
      :my_puppet_variable => 'test'
    }}
    it  { is_expected.to contain_file('/etc/sysconfig/test').with_content('test') }
  end
end
edit flag offensive delete link more

Comments

1

Great! Many thanks for the explanation :)

Yeayu gravatar imageYeayu ( 2016-12-21 04:35:10 -0500 )edit

getting started with testing in puppet isn't as smooth as it could be. There was some good discussion on the current state of testing in puppet - https://www.youtube.com/watch?v=hc-bwua_Eac&list=PLV86BgbREluVjwwt-9UL8u2Uy8xnzpIqa&index=21

DarylW gravatar imageDarylW ( 2016-12-21 07:12:34 -0500 )edit

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-12-20 11:08:42 -0500

Seen: 128 times

Last updated: Dec 20 '16