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