Reputation: 834
I would like to add RSpec unit tests to some Puppet 4 modules. To get started and confirm basic functioning, I would first like to run unit tests that come with standard Puppet modules such as puppetlabs/apt.
If I try running a specific unit test like so
cd modules/apt
rspec spec/classes/apt_spec.rb
I get only failures, and some of the diagnostic output seems to indicate that part of the Puppet runtime environment (such as module stdlib
, which defines function merge
) was not picked up correctly:
Failures:
1) apt defaults should contain File[sources.list] that notifies Class[Apt::Update]
Failure/Error:
it { is_expected.to contain_file('sources.list').that_notifies('Class[Apt::Update]').only_with({
:ensure => 'file',
:path => '/etc/apt/sources.list',
:owner => 'root',
:group => 'root',
:mode => '0644',
:notify => 'Class[Apt::Update]',
})}
Puppet::PreformattedError:
Evaluation Error: Unknown function: 'merge'. at /home/MY_USER/modules/apt/spec/fixtures/modules/apt/manifests/init.pp:50:14 on node MY_HOST
If I try running all unit tests like so
rake spec
I get this error message:
rake aborted!
NameError: uninitialized constant Bundler
/home/MY_USER/modules/apt/Rakefile:3:in `<top (required)>'
(See full trace by running task with --trace)
Prior to these attempts I have taken those steps to prepare the runtime environment (on a Debian host):
mkdir modules
cd modules
puppet module --modulepath=. install puppetlabs-apt # installs also stdlib
sudo gem install rspec-puppet
sudo gem install puppet
sudo gem install puppetlabs_spec_helper
sudo apt-get install rake
My question is this: How can I correctly prepare the runtime environment and successfully run the unit tests that come with a Puppet module from Puppet Forge such as puppetlabs/apt
?
Upvotes: 1
Views: 1168
Reputation: 15472
To do this you need to firstly ensure you have Ruby Gems installed. Then ensure you have installed bundler, which usually just means typing:
$ gem install bundler
Next, I assume you already cloned the apt module, so cd into the directory you cloned it in and then install your bundle:
$ bundle install
Now, there's a problem with that specific module you've chosen. When I try to run bundle install I get:
Could not find gem 'puppet-module-posix-default-r2.0' in any of the gem sources listed in your Gemfile or available on this machine.
I am not sure what that means at the moment, and someone probably needs to raise a bug.
I expected to be able to work around it by using:
$ bundle install --without development system_tests
Even that is not working so I simply commented-out those groups.
Next, run the tests using:
$ bundle exec rake spec
That would normally run the tests. However, I think Puppet has broken the Rspec tests for people outside their company.
I am going to follow this up and come back and update the answer I have figured out what has happened.
I'll leave this answer here since it's still documenting the correct procedure.
See also my blog post on this subject, although possibly a little dated now. Let me know if there's anything I need to update.
Upvotes: 1