rspec test for augeas resource that also verifies the resulting content of the file?

230 views
Skip to first unread message

Bill Sirinek

unread,
Sep 25, 2014, 6:09:47 AM9/25/14
to puppet...@googlegroups.com
I have a simple class that sets the timezone on a Solaris system. I'm trying to write tests for it, and failing. It's telling me the file is nil, but there should be content as the file does exist. The file is not generated via a template or as a static puppet file in the module... the module just has an augeas resource to set a shell-style variable inside the file:  TZ=<timezone>
 
I have puppetlabs_spec_helper 0.8.1 and rspec-puppet 1.0.1 installed as gems into Puppet Enterprise's ruby (PE 3.2.3)
 
Here is the error and the offending rspec code:
 
bsirinek@myhost $ rake spec
/opt/puppet/bin/ruby -I/opt/puppet/lib/ruby/gems/1.9.1/gems/rspec-support-3.1.0/lib:/opt/puppet/lib/ruby/gems/1.9.1/gems/rspec-core-3.1.4/lib /opt/puppet/lib/ruby/gems/1.9.1/gems/rspec-core-3.1.4/exe/rspec --pattern spec/\{classes,defines,unit,functions,hosts,integration\}/\*\*/\*_spec.rb --color
...F
Failures:
  1) timezone when called with parameters on solaris should contain Augeas[set_timezone] with incl => "/etc/default/init", lens => "Shellvars.lns", changes => "set TZ US/Eastern" and content =~ /^TZ=US\/Eastern$/
     Failure/Error: }).with_content(/^TZ=US\/Eastern$/)
       expected that the catalogue would contain Augeas[set_timezone] with content set to /^TZ=US\/Eastern$/ but it is set to nil
     # ./spec/classes/timezone_spec.rb:47:in `block (3 levels) in <top (required)>'
(...annoying deprecation warnings removed...)
Finished in 0.60314 seconds (files took 1.4 seconds to load)
4 examples, 1 failure
Failed examples:
rspec ./spec/classes/timezone_spec.rb:43 # timezone when called with parameters on solaris should contain Augeas[set_timezone] with incl => "/etc/default/init", lens => "Shellvars.lns", changes => "set TZ US/Eastern" and content =~ /^TZ=US\/Eastern$/
/opt/puppet/bin/ruby -I/opt/puppet/lib/ruby/gems/1.9.1/gems/rspec-support-3.1.0/lib:/opt/puppet/lib/ruby/gems/1.9.1/gems/rspec-core-3.1.4/lib /opt/puppet/lib/ruby/gems/1.9.1/gems/rspec-core-3.1.4/exe/rspec --pattern spec/\{classes,defines,unit,functions,hosts,integration\}/\*\*/\*_spec.rb --color failed
 
 
rspec test:
 
  describe 'when called with parameter on solaris' do
    let(:facts) { { :osfamily => 'Solaris' } }
    let(:params) { { :time_zone => 'US/Eastern' } }
    it {
      should contain_augeas('set_timezone').with({
        :incl    => '/etc/default/init',
        :lens    => 'Shellvars.lns',
        :changes => "set TZ US/Eastern"
      }).with_content(/^TZ=US\/Eastern$/)
    }
  end
 
The test works fine if I leave off the with_content.
 
Any ideas?
 
Thanks
 
Bill

 

Johan De Wit

unread,
Sep 25, 2014, 9:02:27 AM9/25/14
to puppet...@googlegroups.com
Hi,

Augeas changes the file during the execution of the catalog on the node, so it cannot have any content in the catalo, and that is what puppet-rspec is testing.

the with_content test will always fail, because a content attribute is not available for the augeas resource

puppet describe -s  augeas

Parameters
----------
    changes, context, force, incl, lens, load_path, name, onlyif, returns,
    root, show_diff, type_check

For rspec unit testing, one must keep in mind that you are testing the content of the catalog.

hth

Grts

Johan
--
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/7007a8e5-df7b-492a-accb-eed09cb12871%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


-- 
Johan De Wit

Open Source Consultant

Red Hat Certified Engineer              (805008667232363)
Puppet Certified Professional 2013/2014 (PCP0000006)
_________________________________________________________
 
Open-Future                 Phone     +32 (0)2/255 70 70
Zavelstraat 72              Fax       +32 (0)2/255 70 71
3071 KORTENBERG             Mobile    +32 (0)474/42 40 73
BELGIUM                     http://www.open-future.be
_________________________________________________________
 

Upcoming Events:

Puppet Introduction Course | http://www.open-future.be/puppet-introduction-course-13th-october

Puppet Practitioner Training | http://www.open-future.be/puppet-practitioner-former-puppet-advanced-training-14-till-16th-october

Linux Training | http://www.open-future.be/linux-training-20-till-24th-october

Puppet Introduction Course | http://www.open-future.be/puppet-introduction-course-10th-november

Puppet Fundamentals Training | http://www.open-future.be/puppet-fundamentals-training-12-till-14th-november

Subscribe to our newsletter: http://eepurl.com/BUG8H


Wil Cooley

unread,
Sep 29, 2014, 12:41:52 PM9/29/14
to puppet-users group

On Sep 25, 2014 6:02 AM, "Johan De Wit" <Jo...@open-future.be> wrote:
>
> For rspec unit testing, one must keep in mind that you are testing the content of the catalog.
>

For *rspec-puppet* unit testing, that is.

There is also *rspec-puppet-augeas*, but unfortunately it requires a downrev of rspec-puppet until there is a new release of rspec-puppet, which re-enables the interface that -augeas needs. (Which is less painful w/RVM and gemsets.)

r-p-a also suffers from using augeas conditions to test expectations, which makes for a chicken-and-egg situation: if you are not confident with your augeas skills to make the change, you cannot be confident in your verification of the result. (I have not investigated the feasibility of getting the contents of the temp file and using regex or string matches, but it might be possible.)

Wil

Reply all
Reply to author
Forward
0 new messages