Correct way to start RSpec-puppet unit tests

二次信任 提交于 2019-12-12 19:41:22

问题


I have created a simple Puppet 4 class and a unit test to go along with it as follows (after executing touch metadata.json; rspec-puppet-init while in modules/test/):

# modules/test/manifests/hello_world1.pp
class test::hello_world1 {
  file { "/tmp/hello_world1":
    content => "Hello, world!\n"
  }
}

# modules/test/spec/classes/test__hello_world1_spec.rb
require 'spec_helper'
describe 'test::hello_world1' do
  it { is_expected.to compile }
  it { is_expected.to contain_file('/tmp/hello_world1')\
    .with_content(/^Hello, world!$/) }
end

I can successfully run the unit test by executing rspec spec/classes/test__hello_world1_spec.rb while in modules/test/.

I would now like to proceed to a slightly more advanced class that uses code from another module, namely concat (the module has arleady been installed in modules/concat):

# modules/test/manifests/hello_world2.pp
class test::hello_world2
{
  concat{ "/tmp/hello_world2":
    ensure => present,
  }
  concat::fragment{ "/tmp/hello_world2_01":
    target  => "/tmp/hello_world2",
    content => "Hello, world!\n",
    order   => '01',
  }
}

# modules/test/spec/classes/test__hello_world2_spec.rb
require 'spec_helper'
describe 'test::hello_world2' do
  it { is_expected.to compile }
  # ...
end

When I attempt running this unit test with rspec spec/classes/test__hello_world2_spec.rb while in modules/test I receive an error message that includes:

Failure/Error: it { is_expected.to compile } error during compilation: Evaluation Error: Error while evaluating a Resource Statement, Unknown resource type: 'concat'

I suspect the root cause is that rspec cannot find the other module(s), because it has not been told a "modulepath".

My question is this: How exactly am I supposed to start unit tests, especially ones that require access to other modules?


回答1:


Install the PDK for your platform from its download page. Re-create the module using pdk new module, and pdk new class, or by following the Guide.

Now, I come to what is probably the immediate problem in your code: your code depends on a Puppet Forge module, puppetlabs/concat but you haven't made it available. The PDK module template already has pre-configured puppetlabs_spec_helper to load fixtures for your module.

To tell puppetlabs_spec_helper to get it for you, you need a file .fixtures.yml with the following content:

fixtures:
  forge_modules:
    stdlib: puppetlabs/stdlib
    concat: puppetlabs/concat

Note that you also need puppetlabs/stdlib, because that is a dependency of puppetlabs/concat.

If you want to explore more fixture possibilities, please refer to puppetlabs_spec_helper's docs.

With all of this in place, and integrating the code samples and test content you posted into the initial code skeletons provided by the PDLK, your tests will all pass now when you run:

$ pdk test unit

Note that I have written all about the underlying technologies, in a blog post, showing how to set up Rspec-puppet and more from scratch (ref), and it still appears to be the most up-to-date reference on this subject.

To read more about rspec-puppet in general, please refer to the official rspec-puppet docs site.



来源:https://stackoverflow.com/questions/45791768/correct-way-to-start-rspec-puppet-unit-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!