Deepak
Deepak

Reputation: 746

mocking of chef_environment apart from _default is throwing 404 apart

I am trying to mock chef_environment and I have succeeded if I try to mock _default chef environment. but as soon as I write context block with different environment variables I am getting 404 error

I have both stg and _default chef environment. But I have not use _default environment ever so its default_attributes list and other things are just empty. so can it be the reason of _default working while all other envs are giving the error?

below is my user_rspec.rb file:-

 require 'ChefSpec'
 require 'spec_helper'

 describe 'base::users' do
   let(:chef_run) do
     ChefSpec::ServerRunner.new(platform: 'amazon', version: '2017.09') do |node|
       env = Chef::Environment.new
       env.name environment # using a variable here so we can change it in other contexts
       allow(node).to receive(:chef_environment).and_return(env.name)
       allow(Chef::Environment).to receive(:load).and_return(env)
     end.converge(described_recipe)
   end

   context 'When all attributes are default, on an amazon 2017.09 with _default end' do
     let(:environment) { '_default' }

     it 'converges successfully' do
       expect{ chef_run }.to_not raise_error
     end

     it "create devops group" do
         expect(chef_run).to create_users_manage('devops').with(group_id: 3000)
     end

     it "assign sudo permission to devops-trainee on _default env" do
         expect(chef_run).to create_sudo('devops-trainee')
     end

   end

   context 'When all attributes are default, on an amazon 2017.09 with stg env' do
     let(:environment) { 'stg' }

     it "don't assign sudo permission to devops-trainee on stg env" do
         expect(chef_run).not_to create_sudo('devops-trainee')
     end

   end


 end

output

base::users
  When all attributes are default, on an amazon 2017.09 with _default end
    converges successfully
    create devops group
    assign sudo permission to devops-trainee on _default env
  When all attributes are default, on an amazon 2017.09 with stg env
    don't assign sudo permission to devops-trainee on stg env (FAILED - 1)

Failures:

  1) base::users When all attributes are default, on an amazon 2017.09 with stg env don't assign sudo permission to devops-trainee on stg env
     Failure/Error:
       ChefSpec::ServerRunner.new(platform: 'amazon', version: '2017.09') do |node|
         env = Chef::Environment.new
         env.name environment # using a variable here so we can change it in other contexts
         allow(node).to receive(:chef_environment).and_return(env.name)
         allow(Chef::Environment).to receive(:load).and_return(env)
       end.converge(described_recipe)

     Net::HTTPServerException:
       404 "Not Found "
     # ./cookbooks/base/spec/unit/recipes/users_spec.rb:16:in `block (2 levels) in <top (required)>'
     # ./cookbooks/base/spec/unit/recipes/users_spec.rb:40:in `block (3 levels) in <top (required)>'

Finished in 18.34 seconds (files took 1.36 seconds to load)
4 examples, 1 failure

Failed examples:

rspec ./cookbooks/base/spec/unit/recipes/users_spec.rb:39 # base::users When all attributes are default, on an amazon 2017.09 with stg env don't assign sudo permission to devops-trainee on stg env


ChefSpec Coverage report generated...

  Total Resources:   5
  Touched Resources: 1
  Touch Coverage:    20.0%

Upvotes: 0

Views: 261

Answers (1)

Deepak
Deepak

Reputation: 746

Well, I found the problem in the above example.

I am using ChefSpec::ServerRunner not the SoloRunner and above thing will only work in case of solo runner.

For ServerRunner you have to do the below thing.

  let(:chef_run) do
    ChefSpec::ServerRunner.new(platform: 'amazon', version: '2017.09') do |node, server|
      server.create_environment(environment)
      node.chef_environment = environment
    end.converge(described_recipe)
  end

Upvotes: 0

Related Questions