我正在尝试模拟cook_environment,如果尝试模拟_default厨师环境,则成功。但是当我使用不同的环境变量编写上下文块时,就会收到404错误
我同时拥有stg和_default厨师环境。但是我从未使用过_default环境,因此它的default_attributes列表和其他内容都为空。那么这是_default在其他所有env都给出错误的情况下工作的原因吗?
下面是我的user_rspec.rb文件:-
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
输出
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%
答案 0 :(得分:0)
好吧,我在上面的示例中发现了问题。
我正在使用ChefSpec :: ServerRunner而不是SoloRunner,并且上述情况仅在单独运行时起作用。
对于ServerRunner,您必须执行以下操作。
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