rspec testing a custom puppet provider that has a parent

163 views
Skip to first unread message

Bill Sirinek

unread,
Mar 21, 2018, 9:57:34 AM3/21/18
to Puppet Users
I am trying to write an rspec test for a custom type and provider I wrote and having issues.

The custom type has a provider that manages an ini file. Because I already have puppetlabs/inifile, I made this provider a child provider of ini_setting.  (I don't use the ini_setting type directly in my manifests, because some versions of the application use a command line tool talking to a DB for configuration, rather than a plaintext ini file, and my custom type supports both)   The type/provider code works in puppet, I'm writing tests for our modules after the fact. :\

lib/puppet/provider/ini_style_config.rb:

Puppet::Type.type(:myapp_config).provide(
             
:ini_style_config,
             
:parent => Puppet::Type.type(:ini_setting).provider(:ruby)
             
) do
  desc
"configuration items for version of the app that uses an .ini file"
 
 
< ... provider code ... >

spec/lib/puppet/provider/ini_style_config_spec.rb:

require 'spec_helper'


describe
Puppet::Type.type(:myapp_config).provider(:ini_style_config) do


  on_supported_os
.each do |os, facts|
    context
"on #{os}" do
      before
:each do
       
Facter.clear
        facts
.each do |k, v|
         
Facter.stubs(:fact).with(k).returns Facter.add(k) { setcode { v } }
       
end
     
end


      describe
'instances' do
        it
'should have an instance method' do
          expect
(described_class).to respond_to :instances
       
end
     
end


      describe
'prefetch' do
        it
'should have a prefetch method' do
          expect
(described_class).to respond_to :prefetch
       
end
     
end
   
end
 
end
end


When I run the tests I get an error because it can't find the ini_setting type from puppetlabs/inifile:



mymacbook
:myapp bsirinek$ pdk test unit
[✔] Preparing to run the unit tests.
[✖] Running unit tests.


An error occurred while loading ./spec/unit/puppet/provider/ini_style_config_spec.rb.
Failure/Error: :parent => Puppet::Type.type(:ini_setting).provider(:ruby)


Puppet::Error:
 
Could not autoload puppet/type/myapp_config: Could not autoload puppet/provider/myapp_config/ini_style_config: undefined method `provider' for nil:NilClass
# ./lib/puppet/provider/myapp_config/ini_style_config.rb:3:in `
<top (required)>'
# /opt/puppetlabs/pdk/share/cache/ruby/2.1.0/gems/puppet-5.3.3/lib/puppet/util/autoload.rb:68:in `load'

# /opt/puppetlabs/pdk/share/cache/ruby/2.1.0/gems/puppet-5.3.3/lib/puppet/util/autoload.rb:68:in `load_file'
# /opt/puppetlabs/pdk/share/cache/ruby/2.1.0/gems/puppet-5.3.3/lib/puppet/util/autoload.rb:83:in `block in loadall'
# /opt/puppetlabs/pdk/share/cache/ruby/2.1.0/gems/puppet-5.3.3/lib/puppet/util/autoload.rb:81:in `each'
# /opt/puppetlabs/pdk/share/cache/ruby/2.1.0/gems/puppet-5.3.3/lib/puppet/util/autoload.rb:81:in `loadall'
# /opt/puppetlabs/pdk/share/cache/ruby/2.1.0/gems/puppet-5.3.3/lib/puppet/util/autoload.rb:208:in `loadall'
# /opt/puppetlabs/pdk/share/cache/ruby/2.1.0/gems/puppet-5.3.3/lib/puppet/metatype/manager.rb:126:in `newtype'
# ./lib/puppet/type/myapp_config.rb:1:in `<top (required)>'
# /opt/puppetlabs/pdk/share/cache/ruby/2.1.0/gems/puppet-5.3.3/lib/puppet/util/autoload.rb:68:in `load'
# /opt/puppetlabs/pdk/share/cache/ruby/2.1.0/gems/puppet-5.3.3/lib/puppet/util/autoload.rb:68:in `load_file'
# /opt/puppetlabs/pdk/share/cache/ruby/2.1.0/gems/puppet-5.3.3/lib/puppet/util/autoload.rb:194:in `load'
# /opt/puppetlabs/pdk/share/cache/ruby/2.1.0/gems/puppet-5.3.3/lib/puppet/metatype/manager.rb:171:in `type'
# ./spec/unit/puppet/provider/ini_style_config_spec.rb:3:in `<top (required)>'
# ------------------
# --- Caused by: ---
# NoMethodError:
#   undefined method `provider' for nil:NilClass
#   ./lib/puppet/provider/myapp_config/ini_style_config.rb:3:in `<top (required)>'
 
Evaluated 0 tests in 0.00058 seconds: 0 failures, 0 pending.
[✔] Cleaning up after running unit tests.
mymacbook
:myapp bsirinek$


I have inifile listed in my .fixtures.yml.   

---
fixtures
:
  symlinks
:
    myapp
: "#{source_dir}"
  forge_modules
:
    stdlib
:
      repo
: "puppetlabs/stdlib"
     
ref: "4.20.0"
    inifile
:
      repo
: "puppetlabs/inifile"
     
ref: "2.0.0"


Am I missing something?

Thanks

Bill


David Schmitt

unread,
Mar 22, 2018, 5:50:12 AM3/22/18
to puppet...@googlegroups.com
Hi Bill,

On Wed, Mar 21, 2018 at 1:57 PM Bill Sirinek <bi...@sirinek.com> wrote:
I am trying to write an rspec test for a custom type and provider I wrote and having issues.

The custom type has a provider that manages an ini file. Because I already have puppetlabs/inifile, I made this provider a child provider of ini_setting.  (I don't use the ini_setting type directly in my manifests, because some versions of the application use a command line tool talking to a DB for configuration, rather than a plaintext ini file, and my custom type supports both)   The type/provider code works in puppet, I'm writing tests for our modules after the fact. :\

lib/puppet/provider/ini_style_config.rb:

Puppet::Type.type(:myapp_config).provide(

this should be `newtype`, not `type`.

 
              :ini_style_config,
             
:parent => Puppet::Type.type(:ini_setting).provider(:ruby)
             
) do
  desc
"configuration items for version of the app that uses an .ini file"
 
 
< ... provider code ... >

spec/lib/puppet/provider/ini_style_config_spec.rb:

require 'spec_helper'


describe
Puppet::Type.type(:myapp_config).provider(:ini_style_config) do


  on_supported_os
.each do |os, facts|
    context
"on #{os}" do
      before
:each do
       
Facter.clear
        facts
.each do |k, v|
         
Facter.stubs(:fact).with(k).returns Facter.add(k) { setcode { v } }
       
end
     
end

--
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/194dfe1f-ff97-4388-92f7-be755de24d7b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

Bill Sirinek

unread,
Mar 22, 2018, 9:38:32 AM3/22/18
to Puppet Users
My type file calls Puppet::Type.newtype() as you suggest, but the code snippet you were referring to was for my provider, so I think
Puppet::Type.type(:myapp_config).provide

is correct here. The type/provider itself actually works. I have verified that by actually using the module. I am in the process of writing tests for modules where we did not do so before.

There are two providers for my type. One of them, the one showing this error when running tests, manages an ini-style configuration file, so I defined Puppet::Type.type(:ini_setting).provider(:ruby) as a parent provider. That is part of the puppetlabs/inifile module. My test fails because it doesnt seem to be able to find this? From what I can see, Puppet::Type.type(:ini_setting).provider(:ruby) is nil when the test runs. I included the inifile module in my .fixtures.yml  but apparently that doesnt fix it.



As for the facts, the test code I for testing this type/provider started out as a copy and paste so I wont be able to deal with the facts until I get past this issue, but to answer your question, one big issue I ran into with PDK's default handling of facts is that it uses rspec-puppet-facts which does not seem to support Solaris 10 anymore, or at least I hadnt gotten it to work. So I have been having to keep Solaris 10 out of my metadata.json file, even though it probably should be there as Solaris 10 is supported by Puppet in my version of PE (2017.3.5) 

Bill

David Schmitt

unread,
Mar 23, 2018, 7:36:46 AM3/23/18
to puppet...@googlegroups.com
On Thu, Mar 22, 2018 at 1:38 PM Bill Sirinek <bi...@sirinek.com> wrote:
My type file calls Puppet::Type.newtype() as you suggest, but the code snippet you were referring to was for my provider, so I think
Puppet::Type.type(:myapp_config).provide

is correct here. The type/provider itself actually works. I have verified that by actually using the module. I am in the process of writing tests for modules where we did not do so before.

Oh yeah, sorry. You're right.
 

There are two providers for my type. One of them, the one showing this error when running tests, manages an ini-style configuration file, so I defined Puppet::Type.type(:ini_setting).provider(:ruby) as a parent provider. That is part of the puppetlabs/inifile module. My test fails because it doesnt seem to be able to find this? From what I can see, Puppet::Type.type(:ini_setting).provider(:ruby) is nil when the test runs. I included the inifile module in my .fixtures.yml  but apparently that doesnt fix it.

Hmm. can you provide the module's source, so we can have a look at it? If you can't publish it, please send it to pdk-mai...@puppet.com.

 

As for the facts, the test code I for testing this type/provider started out as a copy and paste so I wont be able to deal with the facts until I get past this issue, but to answer your question, one big issue I ran into with PDK's default handling of facts is that it uses rspec-puppet-facts which does not seem to support Solaris 10 anymore, or at least I hadnt gotten it to work. So I have been having to keep Solaris 10 out of my metadata.json file, even though it probably should be there as Solaris 10 is supported by Puppet in my version of PE (2017.3.5) 

I see you already found your way around to adding the facts to facterdb. Thanks a lot!

Cheers, David
 
--
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.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages