rspec-puppet: let( :title ) isn't setting title

134 views
Skip to first unread message

Chris Galli

unread,
Jan 9, 2015, 5:08:19 PM1/9/15
to puppet...@googlegroups.com
I've created a simple module to play with rspec-puppet, and I'm having trouble getting my tests to pass because it appears I can't set the value of $title -- It's always the name of my module.

I think perhaps I'm not understanding something about $title in puppet or let( :title ) in rspec-puppet.


Here's what I'm testing and the results I'm getting.

Create module:
$ puppet module generate --skip-interview poc-filez



Module contents (poc-filez/manifests/init.pp):
class filez {

  file
{ $title:
   
# have also tried "${title}"
   
ensure => "present",
 
}
}




Spec contents (poc-filez/spec/classes/init_spec.rb)
require 'spec_helper'
describe
'filez' do
  expected_title
= '/home/me/foo'
  let
(:title) { expected_title }
 
# have tried variations, e.g. let (:title) { "#{expected_title" }


 
# this fails  
  it
{ should contain_file("#{expected_title}") }
 
 
# but when I use the module name, it passes, File[filez] is in the catalog
 
# it { should contain_file("filez") }
end



.fixtures.yml contents:
fixtures:

  repositories
:
   
#
  symlinks
:
    filez
: "#{source_dir}"




Test results:
$ rake spec


Failures:


 
1) filez should contain File[/home/me/foo]
     
Failure/Error: it { should contain_file("#{expected_title}") }
       expected that the catalogue would contain
File[/home/me/foo]



However, if I change the module to use $name, and pass $name as a parameter in the spec, then my tests pass, e.g.

manifests/init.pp
class filez {
  file
{ $name:
   
ensure => "present",

 
}
}


spec/classes/init_spec.rb
require 'spec_helper'
describe
'filez' do
  expected_title
= '/home/me/foo'
  let
(:params) {{ :name => expected_title }}


 
# this passes
  it
{ should contain_file("#{expected_title}") }

 
 
# this fails
 
# it { should contain_file("filez") }
end



I've examined other projects that use rspec-puppet and let( :title ) and on the surface they seems very similar.  But obviously, I'm missing something or not doing something correctly.  Any insight would be greatly appreciated.  I've got several legacy modules that use $title, so I'm hesitant to simply accept changing them to use name (unless of course that's the root of my problem).

Much appreciated,

Chris


ps
I'm using Ruby 2.1.5 on MAC OS X 10.9.5

Version Info (from Gemfile.lock):
GEM
  remote
: https://rubygems.org/
  specs
:
   
CFPropertyList (2.2.8)
    diff
-lcs (1.2.5)
    facter
(2.3.0)
     
CFPropertyList (~> 2.2.6)
    hiera
(1.3.4)
      json_pure
    json_pure
(1.8.1)
    metaclass
(0.0.4)
    mocha
(1.1.0)
      metaclass
(~> 0.0.1)
    puppet
(3.7.3)
      facter
(> 1.6, < 3)
      hiera
(~> 1.0)
      json_pure
    puppet
-lint (1.1.0)
    puppet
-syntax (1.3.0)
      rake
    puppetlabs_spec_helper
(0.8.2)
      mocha
      puppet
-lint
      puppet
-syntax
      rake
      rspec
      rspec
-puppet
    rake
(10.4.2)
    rspec
(3.1.0)
      rspec
-core (~> 3.1.0)
      rspec
-expectations (~> 3.1.0)
      rspec
-mocks (~> 3.1.0)
    rspec
-core (3.1.7)
      rspec
-support (~> 3.1.0)
    rspec
-expectations (3.1.2)
      diff
-lcs (>= 1.2.0, < 2.0)
      rspec
-support (~> 3.1.0)
    rspec
-mocks (3.1.3)
      rspec
-support (~> 3.1.0)
    rspec
-puppet (1.0.1)
      rspec
    rspec
-support (3.1.2)


PLATFORMS
  ruby


DEPENDENCIES
  facter
(>= 1.7.0)
  puppet
(>= 3.3)
  puppet
-lint (>= 0.3.2)
  puppetlabs_spec_helper
(>= 0.1.0)

Stephen Hoekstra

unread,
Jan 9, 2015, 5:46:24 PM1/9/15
to puppet...@googlegroups.com
Hi,

Change filez from a class to a define (https://docs.puppetlabs.com/learning/definedtypes.html).

Stephen

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/39cf1edd-aa4f-4370-8674-4b041e4cfa86%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gareth Rushgrove

unread,
Jan 10, 2015, 12:17:43 PM1/10/15
to puppet...@googlegroups.com
So, as mentioned, the let(:title) method is actually just for defined
types: http://rspec-puppet.com/tutorial/#specifying_the_title_of_the_defined_type

Given you have a class, title could be coming from a fact, in which
case you could use use:

let(:facts) { {:title => 'foo'} }

Or it should be a parameterised class, in which case use:

let(:params) { {:title => 'foo'} }

(given the special nature of title these may not work, but demonstrate
how to pass values to different constructs)

Or as mentioned it should be a defined type, in which case use let(:title).

I'd probably recommend trying to write a test for one of your
real-world puppet modules as I think the issue is with the Puppet code
more than the tests.

For a bit more context around the difference between name and title
see the docs too:

https://docs.puppetlabs.com/puppet/latest/reference/lang_defined_types.html#title-and-name

Gareth
> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to puppet-users...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/39cf1edd-aa4f-4370-8674-4b041e4cfa86%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Gareth Rushgrove
@garethr

devopsweekly.com
morethanseven.net
garethrushgrove.com

Chris Galli

unread,
Jan 12, 2015, 9:14:45 AM1/12/15
to puppet...@googlegroups.com
I'd probably recommend trying to write a test for one of your
real-world puppet modules as I think the issue is with the Puppet code
more than the tests.

 Thanks for the feedback and links Stephen and Gareth.

Gareth's comment sums things up nicely.  I'm dealing a large amount of Puppet 0.2x code, and my example was based one of them -- I gutted the logic but kept the usage of class and usage of $title in the resource.

I appreciate the help since I'm just getting started with Puppet.

Thanks!
Chris
Reply all
Reply to author
Forward
0 new messages