Search code examples
chef-infrachefspec

how do i include non chef resources using chefspec


i want to write chefspec for one of the recipe which has the code to create jenkins user as shown below.

    jenkins_user 'test1' do
      full_name   'test user'
      email     'test1@test.com'
      password      'testp' 
    end

Here "jenkins_user" is not a chef resource. how do i write a chefspec to run a unit test on the recipe.

or if we have anyother way to run a unit test for the recipe kindly let me know.


Solution

  • When you are including resources that are defined in another cookbook, if the cookbook author has written a libraries/matchers.rb file then you can call those matchers in your ChefSpec unit tests.

    For example, if you are using the Jenkins Community Cookbook you will see there is already a matchers.rb file located here. So, you should be able to create unit tests like this:

    it 'should create the jenkins user' do
      expect(chef_run).to create_jenkins_user('test1')
    end
    

    If you are including resources from a cookbook that does not have matchers defined, then you will have to write your own matchers, and the details for doing this are in the documentation which Tensibai linked to.