Steps to build a Puppet 4 install in my home directory for building modules

34 views
Skip to first unread message

James Perry

unread,
Jul 18, 2017, 7:51:32 PM7/18/17
to Puppet Developers
Years ago there were a lot of docs about how to setup Puppet to allow someone to build modules outside having to have a master/client setup using puppet apply. 

I am trying to figure out the very cryptic world of spec/rspec, as it seems to not be documented very well anywhere for anyone other than someone that already knows it, In doing so I don't want to be able to develop in my home directory in a server versus having to develop modules and test with rspec against a full puppet server configuration. 

So far I have found little bits and pieces around, but nothing definitive or documented well for building something of this nature. The best I found so far were pre-built Ubuntu Docker containers or Vagrant builds. I don't have access to either presently or the time to build out a server to handle hosting either. 

Does anyone have a guide to setting up a stand-alone puppet client for development.  There used to be a rspec-puppet.com/setup page and that is what is linked from inside the documentation, but the page is gone. 

What I have so far is: 

1. .Install puppet-agent to the host as root. 
2. Setup paths to use the Ruby configuration from the puppet-agent so that the any gem add-ons are compatible with the version of the puppet-agent RPM. 
3. Install puppetlabs-stdlib, rspec-puppet and any dependencies they require. 

When I have done a puppet module generate <name> and a rspec-puppet-init, I create a basic test to ensure the module compiles, as noted on the rspec-puppet Github page https://github.com/rodjek/rspec-puppet, That fails saying it can find compile. 

describe 'mymodule' do 
   it { is_expected.to compile } 
end 

Any other tests that "should" work don't. either. 

So in modules/test/manifests/init.pp I have:
class test {
   package { 'somepackage': 
        ensure => present,
  }
}

And my modules/test/spec/class/init_spec.rb has 

require 'spec_helper' 

describe 'test' do
    it { is_expected.to contain('somepackage').with_ensure('present')
end

So since this seems to be non-functional in my puppet development environment, which is a copy of my prod, I want to set it up fresh. When I tried in my home directory all i got were errors. 

Rob Nelson

unread,
Jul 23, 2017, 5:40:33 PM7/23/17
to puppe...@googlegroups.com
James,

As you have discovered, the setup you want is not quite documented the way or in the locations you would expect, but it is documented in many places, such as https://puppet.com/blog/unit-testing-rspec-puppet-for-beginners and https://puppet.com/blog/use-onceover-start-testing-rspec-puppet.

IMO, what you really want to do is integrate tests with your version-controlled code, in-place, rather than replicating it somewhere else. You can do this by using ruby's bundler and a Gemfile to get started, then add a spec/spec_helper.rb and spec/classes/*_spec.rb files for unit tests and so on and so forth. But, you don't actually need puppet installed, as that can come from a gem in the bundler setup. There's a lot to do and many ways to do it, so there's no single solution to choose.

I've written a lot about my explorations with rspec on my blog (https://rnelson0.com/?s=rspec) but here's how I would start. Use puppet-module-skeleton (https://github.com/garethr/puppet-module-skeleton) which creates a new module with rspec tests. It is designed for a module rather than a controlrepo, so will take a little re-working to align it properly for that (https://rnelson0.com/2015/11/24/modern-rspec-puppet-practices/ and optionally https://rnelson0.com/2016/11/06/puppet-tech-debt-moving-rspec-tests/), but is pretty good for both. Once you get it all working, you commit the changes to your module/controlrepo and then it's bundled with your puppet code, and thus always available for testing. It does mean the tests are deployed on your master, but aside from a little extra disk space, it will have no impact on agents connecting and should not be a concern. It's also portable for when you get a vagrant or docker setup going.

With the bundler/rspec framework in place, your workflow becomes:
* Clone your controlrepo
* Checkout a feature branch for changes
* Run `bundle install` with appropriate args (I use `bundle install --path vendor --without system_tests development` most of the time)
* Run `bundle exec rake test` to run all the tests (the target `spec`, or the pair `spec_prep` and `spec_clean` will JUST run your unit tests), everything should pass
* Make all the edits you need
* Run `bundle exec rake test` and ensure the tests continue to pass. If not, repeat the previous step until tests do pass.
* Commit your code, push to upstream

There are plenty of examples of control repos out there but not all have a working test setup included. Check out these two that show the example:

I hope this helps!

--
You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-dev+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-dev/23805142-d49e-4612-9590-472ab581dade%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Schmitt

unread,
Jul 24, 2017, 3:28:07 AM7/24/17
to puppe...@googlegroups.com
On 19 July 2017 at 00:51, James Perry <jjpe...@gmail.com> wrote:
Years ago there were a lot of docs about how to setup Puppet to allow someone to build modules outside having to have a master/client setup using puppet apply. 

I am trying to figure out the very cryptic world of spec/rspec, as it seems to not be documented very well anywhere for anyone other than someone that already knows it, In doing so I don't want to be able to develop in my home directory in a server versus having to develop modules and test with rspec against a full puppet server configuration. 

The whole point of rspec is to not require a puppetserver for testing. You can read more on the newly updated rspec-puppet tutorial at http://rspec-puppet.com/tutorial/ .

If you need a development environment, we released a development kit preview last week (https://groups.google.com/d/msg/puppet-users/_G6issIdmVY/5oUfKhwkAQAJ ) that you might want to check out to get all the tools with a single installer.

Would love to hear how you got along with either!

Cheers, David

So far I have found little bits and pieces around, but nothing definitive or documented well for building something of this nature. The best I found so far were pre-built Ubuntu Docker containers or Vagrant builds. I don't have access to either presently or the time to build out a server to handle hosting either. 

Does anyone have a guide to setting up a stand-alone puppet client for development.  There used to be a rspec-puppet.com/setup page and that is what is linked from inside the documentation, but the page is gone. 

What I have so far is: 

1. .Install puppet-agent to the host as root. 
2. Setup paths to use the Ruby configuration from the puppet-agent so that the any gem add-ons are compatible with the version of the puppet-agent RPM. 
3. Install puppetlabs-stdlib, rspec-puppet and any dependencies they require. 

When I have done a puppet module generate <name> and a rspec-puppet-init, I create a basic test to ensure the module compiles, as noted on the rspec-puppet Github page https://github.com/rodjek/rspec-puppet, That fails saying it can find compile. 

describe 'mymodule' do 
   it { is_expected.to compile } 
end 

Any other tests that "should" work don't. either. 

So in modules/test/manifests/init.pp I have:
class test {
   package { 'somepackage': 
        ensure => present,
  }
}

And my modules/test/spec/class/init_spec.rb has 

require 'spec_helper' 

describe 'test' do
    it { is_expected.to contain('somepackage').with_ensure('present')
end

So since this seems to be non-functional in my puppet development environment, which is a copy of my prod, I want to set it up fresh. When I tried in my home directory all i got were errors. 

James Perry

unread,
Jul 24, 2017, 2:42:22 PM7/24/17
to Puppet Developers
Thanks. 

I had looked at these but was missing something along the way.  I now have what appears to be a working setup and a rake rspec is now properly mocking and testing the modules. 

Now I just have to figure out why it isn't picking up the custom fact. That will require more RTFM and Google.  

Rob Nelson

unread,
Jul 24, 2017, 3:04:14 PM7/24/17
to puppe...@googlegroups.com
If you're talking about testing it, it gets its own test like https://github.com/puppetlabs/puppetlabs-java/blob/master/spec/unit/facter/java_version_spec.rb. If you're talking about using the custom fact in an otherwise unrelated unit test. it depends on who sets the fact, and where; you may need to mock it up or just set it in a `let :facts` block for simplicity.

--
You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-dev+unsubscribe@googlegroups.com.

James Perry

unread,
Jul 24, 2017, 4:19:49 PM7/24/17
to Puppet Developers
Thanks. I think I will set the use a `let :facts` block for this testing. I want to have it test against specific mocked facts. 


On Monday, July 24, 2017 at 3:04:14 PM UTC-4, Rob Nelson wrote:
If you're talking about testing it, it gets its own test like https://github.com/puppetlabs/puppetlabs-java/blob/master/spec/unit/facter/java_version_spec.rb. If you're talking about using the custom fact in an otherwise unrelated unit test. it depends on who sets the fact, and where; you may need to mock it up or just set it in a `let :facts` block for simplicity.
On Mon, Jul 24, 2017 at 2:42 PM, James Perry <jjpe...@gmail.com> wrote:
Thanks. 

I had looked at these but was missing something along the way.  I now have what appears to be a working setup and a rake rspec is now properly mocking and testing the modules. 

Now I just have to figure out why it isn't picking up the custom fact. That will require more RTFM and Google.  

--
You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-dev+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages