2

I am trying to test a custom_type using rspec-puppet.

Puppet Code

class vim::ubuntu::config {
  custom_multiple_files { 'line_numbers':
    ensure     => 'present',
    parent_dir => '/home',
    file_name  => '.vimrc',
    line       => 'set number';
  }
}

Rspec-puppet code

require 'spec_helper'

describe "vim::ubuntu::config" do
  it do
    should contain_custom_multiple_files('line_numbers').with({
      'ensure'     => 'present',
      'parent_dir' => '/home',
      'file_name'  => '.vimrc',
      'line'       => 'set number',
    })
  end
end

Result

2) vim::ubuntu::config
Failure/Error: })
Puppet::Error:
Puppet::Parser::AST::Resource failed with error ArgumentError: Invalid resource
type custom_multiple_files at /etc/puppet/modules/vim/spec/fixtures/modules
/vim/manifests/ubuntu/config.pp:7 on node ...
# ./spec/classes/ubuntu_config_spec.rb:10

Puppet Version

puppet --version
3.4.3

Custom Module at Eclipse

enter image description here

Outcome

Rspec-puppet cannot find the custom type custom_multiple_files, while puppet is able to find and execute the custom type during a puppet run.

030
  • 10,842
  • 12
  • 78
  • 123

2 Answers2

4

You'll need to have a copy of your module in your spec/fixtures/module folder. You can also use a symlink to add a reference from spec/fixtures/module to your custom_type_module.

Reference: https://github.com/rodjek/rspec-puppet/issues/84

Dan Chan
  • 494
  • 4
  • 14
  • The `custom types` and `providers` were successfully found by executing `sudo ln -s /etc/puppet/modules/custom /etc/puppet/modules/vim/spec/fixtures/modules/custom` before issuing `rspec`. I will create a script to symlink the `custom` folder to all puppet modules. Thank you I am able to test custom puppet types and will continue testing. – 030 Apr 09 '14 at 22:05
  • I have upvoted your question because it does provide a solution to the OP's issue. Ideally though, you would simply [mock or stub](http://martinfowler.com/articles/mocksArentStubs.html) the external dependency (in this case the custom type), since you don't really need to test its logic, but only the fact that it was called with the right parameters. AFAIK, this is unfortunately non-trivial in Puppet if possible at all. – cassianoleal Apr 10 '14 at 07:07
  • Mocking the `custom_type` sounds interesting as this avoids creating a symlink to the `custom_module` in all puppet modules. Could you indicate how to mock the `custom_multiple_files` in rspec-puppet? I have checked the provided link regarding mocks, but did not find a Mock example for rspec-puppet. Could you please post an answer to this question as this would avoid the creation of many symlinks. – 030 Apr 11 '14 at 09:50
  • [Mock examples in rspec-puppet](https://github.com/TomPoulton/rspec-puppet-unit-testing) – 030 Apr 12 '14 at 15:10
  • I have tried to mock the `custom type` but I am unable to make it work. I will create a follow up question. – 030 Apr 14 '14 at 12:16
2

An example .fixtures.yml file inside the module's root directory:

---
fixtures:
  repositories:
    concat:
      repo: 'git://github.com/puppetlabs/puppetlabs-concat.git'
      ref:  '1.0.0'
    stdlib:
      repo: 'git://github.com/puppetlabs/puppetlabs-stdlib.git'
      ref:  '3.2.0'

  symlinks:
    keepalived: "#{source_dir}"

Be sure to include "require 'puppetlabs_spec_helper/rake_tasks'" in your Rakefile and adjust aforementioned paths/repos/module names.

Arioch
  • 35
  • 3