Ask Your Question
0

how to write puppet-rspec for dependent define?

asked 2015-12-28 04:32:03 -0500

karthikeayan gravatar image

I have a define like below,

define module::define1 (
  $path = undef,
  $someparam = undef,
  ...
) {

  unless defined(Define2[$someparam]) {
    fail("Please create define2[$someparam] first")
  }

  file { $path:
    ensure => directory,
  }
}

When I try to test the existence of file[$path] I am getting error "Please create define2[$someparam] first". But define2 is in another file. How can I include define2 inside my define1's rspec scope?

edit retag flag offensive close merge delete

Comments

Can you post what you have tried in rspec-puppet

Chutki gravatar imageChutki ( 2015-12-28 05:11:17 -0500 )edit

context 'when invoked' do let(:pre_condition) do 'Module::Define2 <| |>' end it { should contain_file('file_title') } end I tried this but not working!

karthikeayan gravatar imagekarthikeayan ( 2015-12-28 07:22:21 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-01-03 02:39:35 -0500

updated 2016-01-03 02:40:55 -0500

It's not clear what you are trying to do.

You could mean this:

define module::define1 (
  $path      = undef,
  $someparam = undef,
) {
  unless defined("module::define2") {
    fail("You have not defined a type 'module::define2'")
  }

  file { $path:
    ensure => directory,
  }
}

That is to say, perhaps you want a failure inside module::define1 if the programmer has forgotten to provide a definition for another type define2.

Or you could mean this:

define module::define1 (
  $path      = undef,
  $someparam = undef,
) {
  unless defined(Module::Define2[$someparam]) {
    fail("You have not declared a Module::Define2[$someparam]")
  }

  file { $path:
    ensure => directory,
  }
}

And then in spec/defines/define1_spec.rb:

require 'spec_helper'

describe 'module::define1', :type => :define do
  let :pre_condition do
    'module::define2 { "someval": }'
  end
  let(:title) { 'spec_m' }
  let(:params) {{
    :path      => '/tmp/somepath',
    :someparam => 'someval',
  }}
  it do
    is_expected.to contain_file('/tmp/somepath')
  end
end

That is to say, you want a failure inside module::define1 if the catalog doesn't already contain a resource Module::Define2[$someparam].

In either case, though, this seems like very confusing code, and there's undoubtedly a better way of doing whatever it is you're trying to do.

edit flag offensive delete link more

Comments

yeah, now I understood, when we say, let(:title) actually it will add 'spec_m' to the catalog

karthikeayan gravatar imagekarthikeayan ( 2016-01-11 08:00:56 -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: 2015-12-28 04:32:03 -0500

Seen: 99 times

Last updated: Jan 03 '16