rspec-puppet: how to pass undef as parameter value?

1,849 views
Skip to first unread message

Andreas Haerter

unread,
Jul 19, 2013, 6:20:20 PM7/19/13
to puppet...@googlegroups.com
Hi all,

is there a way to pass the Puppets undef [1] value as parameter? I got a
module with a package_provider parameter, allowing the user two things:
1. specify a specific package provider to use; 2. pass "undef" to let
Puppet decide which provider to use instead of the module.

Something like:

> class example(
> $package_provider = $example::params::package_provider,
> ) inherits example::params {
>
> [...]
>
> package { 'foobar'
> ensure => 'present',
> provider => $package_provider,
> }
> }


Now I want to test if passing undef works:

> context 'with parameter(s): package_provider => undef' do
> let(:params) {{
> :package_provider => [WAY-TO-PASS-UNDEF-HERE],
> }}
>
> it 'contains a package with parameter(s): provider => unset' do
> should contain_package('blueprint').with({
> 'provider' => [WAY-TO-CHECK-FOR-UNDEF-HERE,
> })
> end
> end



But my test doesn't work. What I tried so far is:

1) undef (results in RSpec/Ruby parsing error)
> context 'with parameter(s): foo => undef' do
> let(:params) {{
> :foo => undef,
> }}
>
> [...]
> end

2) :undef (results in Puppet error: "Syntax error at':'; expected '}'" )
> context 'with parameter(s): foo => undef' do
> let(:params) {{
> :foo => :undef,
> }}
>
> [...]
> end

3) nil (does not result in an Puppet or Ruby error, but the
.with_foo(nil) or .without_foo matchers do not seem to work as the
string "nil" was passed tho puppet
> context 'with parameter(s): foo => undef' do
> let(:params) {{
> :foo => nil,
> }}
>
> [...]
> end

Any hints? Thanks in advance :-)


[1]
<http://docs.puppetlabs.com/puppet/latest/reference/lang_datatypes.html#undef>

--
Andreas <http://blog.andreas-haerter.com>

signature.asc

Remi Ferrand

unread,
Apr 18, 2014, 6:37:01 AM4/18/14
to puppet...@googlegroups.com, list+pup...@mail-node.com
I was also wondering how to do this, If anyone has the answer, I'll be glad to know it !

Thanks

Rémi

Johan De Wit

unread,
Apr 18, 2014, 7:39:20 AM4/18/14
to puppet...@googlegroups.com
it {
  should contain_package('foo').with ( {
    'provider' => nil,
  })
--
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/2e2b712e-cf4a-46be-941b-3303efa5d551%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:

Linux Training | https://www.open-future.be/linux-training-5-till-9th-may

Puppet Introduction Course | https://www.open-future.be/puppet-introduction-course-12th-may

Puppet Fundamentals Training | https://www.open-future.be/puppet-fundamentals-training-13-till-15th-may

Zabbix Certified Specialist | https://www.open-future.be/zabbix-certified-specialist-training-19-till-21th-may

Zabbix Certified Professional | https://www.open-future.be/zabbix-certified-professional-training-22-till-23rd-may

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


Remi Ferrand

unread,
Apr 18, 2014, 9:16:56 AM4/18/14
to puppet...@googlegroups.com
... I'm sorry but I don't understand your answer ...

I've created a very simple use case here at https://github.com/riton/puppetrspec

With what value should I replace "# WHAT SHOULD WE USE HERE TO PASS PUPPET undef VALUE ?#" with to make this unit test functional ?

Thanks in advance and sorry to ask you to answer again.

Cheers

Rémi


--
You received this message because you are subscribed to a topic in the Google Groups "Puppet Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/puppet-users/6nL2eROH8is/unsubscribe.
To unsubscribe from this group and all its topics, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/53510EE8.5080401%40open-future.be.

Johan De Wit

unread,
Apr 18, 2014, 9:52:33 AM4/18/14
to puppet...@googlegroups.com

oops, sorry about that.

An undef value in a manifest/class is mapped to 'nil' in the catalog.

But I would not allow to have an undef value for the ensure class parameter.   But that's another discussion.

I will have a look at the code later on.

For more options, visit https://groups.google.com/d/optout.

Felix Frank

unread,
Apr 22, 2014, 3:52:46 AM4/22/14
to puppet...@googlegroups.com
I *would* indeed try

let :params do
{ :ensure => nil }
end

I would also inline the braces {} as shown because they form a hash in
this instance, not a block. Aside from that, this spec is very well
structured. Consider my hat tipped.

Cheers,
Felix

Remi Ferrand

unread,
Apr 22, 2014, 4:08:26 AM4/22/14
to puppet...@googlegroups.com
Hi Felix,

Thank you for your answer, but with my tests it seems that passing Ruby *nil* value to puppet doesn't result in puppet *undef* value...
This is strange considering that puppet *undef* value results in ruby *nil* value in templates...

BTW, I've fixed the sample puppet module code (https://github.com/riton/puppetrspec/commit/febb49cfc949e0a459a96181422e9bd3ee250173) that has been done too quickly and couldn't work at all.

Cheers

Rémi


--
You received this message because you are subscribed to a topic in the Google Groups "Puppet Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/puppet-users/6nL2eROH8is/unsubscribe.
To unsubscribe from this group and all its topics, send an email to puppet-users...@googlegroups.com.

Nan Liu

unread,
Apr 22, 2014, 4:53:05 AM4/22/14
to puppet...@googlegroups.com
On Tue, Apr 22, 2014 at 3:08 AM, Remi Ferrand <remi.mathi...@gmail.com> wrote:
Thank you for your answer, but with my tests it seems that passing Ruby *nil* value to puppet doesn't result in puppet *undef* value...
This is strange considering that puppet *undef* value results in ruby *nil* value in templates...

Off the top of my head, I think it's a symbol:

let(:params) {{
  :package_provider => :undef,
}}

Nan

Remi Ferrand

unread,
Apr 22, 2014, 5:34:46 AM4/22/14
to puppet...@googlegroups.com
Hi Nan,

thanks for your answer.
With this modification:

diff --git a/spec/classes/puppetrspec_spec.rb b/spec/classes/puppetrspec_spec.rb
index 9d06d79..80fae58 100644
--- a/spec/classes/puppetrspec_spec.rb
+++ b/spec/classes/puppetrspec_spec.rb
@@ -22,7 +22,7 @@ describe 'puppetrspec', :type => :class do
     context 'with ensure undef' do
         
         let :params do {
-            :ensure     => '# WHAT SHOULD WE USE HERE TO PASS PUPPET undef VALUE ?#'
+            :ensure     =>  :undef
         } end
 
         it 'should have File with ensure absent' do


it fails with this error:

% rake spec                           
/usr/bin/ruby -S rspec spec/classes/puppetrspec_spec.rb --color
.F

Failures:

  1) puppetrspec with ensure undef should have File with ensure absent
     Failure/Error: should contain_file(filename).with_ensure('absent')
     Puppet::Error:
       Could not parse for environment production: Syntax error at ':'; expected '}' at line 4 on node ...
     # ./spec/classes/puppetrspec_spec.rb:30

Finished in 1.51 seconds
2 examples, 1 failure

Failed examples:

rspec ./spec/classes/puppetrspec_spec.rb:29 # puppetrspec with ensure undef should have File with ensure absent


Same error with this modification:

diff --git a/spec/classes/puppetrspec_spec.rb b/spec/classes/puppetrspec_spec.rb
index 9d06d79..63faa11 100644
--- a/spec/classes/puppetrspec_spec.rb
+++ b/spec/classes/puppetrspec_spec.rb
@@ -21,9 +21,9 @@ describe 'puppetrspec', :type => :class do
 
     context 'with ensure undef' do
         
-        let :params do {
-            :ensure     => '# WHAT SHOULD WE USE HERE TO PASS PUPPET undef VALUE ?#'
-        } end
+        let(:params) {{
+            :ensure     =>  :undef
+        }}


If you have any other idea, let me know :-)

Cheers

Rémi



--
You received this message because you are subscribed to a topic in the Google Groups "Puppet Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/puppet-users/6nL2eROH8is/unsubscribe.
To unsubscribe from this group and all its topics, send an email to puppet-users...@googlegroups.com.

Nan Liu

unread,
Apr 22, 2014, 6:57:18 AM4/22/14
to puppet...@googlegroups.com
So internally it appears to be :undef, and I thought that would work for rspec, but clearly not.

Trying to set it to Puppet::Parser::AST::Undef resulted in the wrong behavior with "if $var {...}". I may have mixed this up with a puppet function where I needed to return undef. I guess I shouldn't count on my memory at this hour of the day.

Nan

Remi Ferrand

unread,
Apr 27, 2014, 10:38:52 AM4/27/14
to puppet...@googlegroups.com
Hi everyone,

thanks all for your answers but i'm starting to think that there is way to pass puppet "undef" value from a rspec test...
If anybody has the solution to fix this test suite (https://github.com/riton/puppetrspec) please tell me; otherwise I'll have to rewrite my manifests and replace the puppet "undef" value with a simple "undef" string where it is possible...

It doesn't have the same meaning but it can help me moving on with my test suite.

Thaks.

Rémi


--
You received this message because you are subscribed to a topic in the Google Groups "Puppet Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/puppet-users/6nL2eROH8is/unsubscribe.
To unsubscribe from this group and all its topics, send an email to puppet-users...@googlegroups.com.

jcbollinger

unread,
Apr 28, 2014, 9:44:38 AM4/28/14
to puppet...@googlegroups.com


On Sunday, April 27, 2014 9:38:52 AM UTC-5, Remi Ferrand wrote:
Hi everyone,

thanks all for your answers but i'm starting to think that there is way to pass puppet "undef" value from a rspec test...
If anybody has the solution to fix this test suite (https://github.com/riton/puppetrspec) please tell me; otherwise I'll have to rewrite my manifests and replace the puppet "undef" value with a simple "undef" string where it is possible...

It doesn't have the same meaning but it can help me moving on with my test suite.



I've been watching this thread with interest because people were offering actual approaches to do something that I would not have thought was possible.  I'll be a bit disappointed if it indeed turns out that it really isn't possible.

To the best of my knowledge, however, Puppet never passes undef as the value of a class or resource parameter.  At least, if you assign undef to a class or resource parameter in the DSL, then that does not mean the parameter should take that value; rather, it is an affirmative expression of not assigning any value at all.  As a result, the parameter takes the default value defined for it, if there is any.  (That's most often useful in the form of an override.)

If you could use rspec to assign undef as a class or resource parameter, then what would that actually test?  Not any defined behavior of your class or type.  If it is Puppet's DSL evaluator that strips undef-valued parameters, then bypassing that via rspec -- if you can -- must yield undefined behavior.  On the other hand, if it is a general feature of Puppet's infrastructure for types that strips undef-valued parameters, then sending undef via rspec tests that feature, not anything your type can do.

I would have thought that the way to test what your type does when one of its parameter is declared undef would be simply to avoid specifying that parameter to it.


John

Dominic Cleal

unread,
Jul 7, 2014, 4:04:45 AM7/7/14
to puppet...@googlegroups.com
Sorry for bringing up an old thread, but I hit the same issue and found
a workaround.

rspec-puppet calls #inspect on the parameter values to see how they
should be represented in Puppet's DSL, so passing a string in causes it
to get quoted (as String#inspect returns a quoted string).

Instead, I created a small class that returns an unquoted value.

in spec_helper.rb:
class Undef
def inspect
'undef'
end
end

and then to use it:
let(:params) do
{ :example => Undef.new }
end

--
Dominic Cleal
Red Hat Engineering

On 27/04/14 15:38, Remi Ferrand wrote:
> Hi everyone,
>
> thanks all for your answers but i'm starting to think that there is way
> to pass puppet "undef" value from a rspec test...
> If anybody has the solution to fix this test suite
> (https://github.com/riton/puppetrspec) please tell me; otherwise I'll
> have to rewrite my manifests and replace the puppet "undef" value with a
> simple "undef" string where it is possible...
>
> It doesn't have the same meaning but it can help me moving on with my
> test suite.
>
> Thaks.
>
> Rémi
>
>
> On Tue, Apr 22, 2014 at 12:57 PM, Nan Liu <nan...@gmail.com
> <mailto:nan...@gmail.com>> wrote:
>
> On Tue, Apr 22, 2014 at 4:34 AM, Remi Ferrand
> <remi.mathi...@gmail.com
> <mailto:puppet-users...@googlegroups.com>.
> <https://groups.google.com/d/msgid/puppet-users/CACqVBqBGogT4Fig6fsH5drRjyRPyJcTmnkxBYh2gSOYyfutaUg%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> 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
> <mailto:puppet-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/CAMTZwM%2B%2BWXCWeijX%3DRJs%3DVoeRs7apQY4CypePLGp%2B1mJQZ9aoA%40mail.gmail.com
> <https://groups.google.com/d/msgid/puppet-users/CAMTZwM%2B%2BWXCWeijX%3DRJs%3DVoeRs7apQY4CypePLGp%2B1mJQZ9aoA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
Reply all
Reply to author
Forward
0 new messages