Correct way to start RSpec-puppet unit tests -
i have created simple puppet 4 class , unit test go along 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' { is_expected.to compile } { is_expected.to contain_file('/tmp/hello_world1')\ .with_content(/^hello, world!$/) } end i can run unit test executing rspec spec/classes/test__hello_world1_spec.rb while in modules/test/.
i proceed more advanced class uses code 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' { is_expected.to compile } # ... end when attempt running unit test rspec spec/classes/test__hello_world2_spec.rb while in modules/test receive error message includes:
failure/error: { is_expected.to compile } error during compilation: evaluation error: error while evaluating resource statement, unknown resource type: 'concat'
i suspect root cause rspec cannot find other module(s), because has not been told "modulepath".
my question this: how supposed start unit tests, ones require access other modules?
here* how set rspec-puppet scratch.
first, need ensure have installed ruby gems platform.
then, install bundler:
$ gem install bundler next, create minimal gemfile (this bundler's config file) following content:
source 'https://rubygems.org' group :test gem 'puppetlabs_spec_helper', :require => false end gem 'puppet' (note paths provide in answer relative modules/test directory mentioned in question. likewise, assumed current working directory.)
now, install "bundle" (set of gems) using:
$ bundle install if you're interested in gems installed try:
$ bundle show $ bundle show puppet etc.
now, procedure relies heavily on puppetlabs_spec_helper gem, recommending @ least skim read docs before proceeding (ref).
note 1 of dependencies of puppetlabs_spec_helper installed rake. rake tool use run tests. pre-configured puppetlabs_spec_helper, , can see available tasks running:
$ bundle exec rake -t now, come immediate problem in code: code depends on puppet forge module, puppetlabs/concat haven't made available.
to tell puppetlabs_spec_helper you, need file .fixtures.yml following content:
fixtures: forge_modules: stdlib: puppetlabs/stdlib concat: puppetlabs/concat symlinks: test: "#{source_dir}" note need puppetlabs/stdlib, because dependency of puppetlabs/concat.
also note need symlinks block, bit confusing @ first. it's bit hacky really, how puppetlabs_spec_helper makes module-under-test available in same directory other module dependencies. (read docs linked above more info here.)
finally, need spec/spec_helper.rb file, single line:
require 'puppetlabs_spec_helper/module_spec_helper' with of in place, , given code samples , test content posted, tests pass when run:
$ bundle exec rake spec note have written of in more detail in blog post, showing how set rspec-puppet , more scratch (ref), , still appears up-to-date reference on subject.
(at time of writing this, puppet has never maintained official docs rspec-puppet, because tool not written puppet.)
* should noted puppet has released puppet development kit, see here (ref). of above may or unnecessary.
Comments
Post a Comment