Resource definition conflict for chef-provisioning-aws aws_vpc resource
I have hit an interesting use case with chef-provisioning/chef-provisioning-aws and ChefSpec with respect to the aws_vpc resource. The VPC resource creation is intentionally split into 3 steps due to dependencies in the underlying AWS EC2 API.
Chef-provisioning-aws VPC creation steps:
- Create the initial
aws_vpcresource - Create an
aws_route_tableresource, which has a dependency on the vpc object in step 1 - Update the original
aws_vpcresource to use the route table resource from step 2
Recipe:
require 'chef/provisioning/aws_driver'
with_driver 'aws:oregon:us-west-2'
aws_dhcp_options 'ref-dhcp-options' do
domain_name 'example.com'
domain_name_servers %w(8.8.8.8 8.8.4.4)
ntp_servers %w(8.8.8.8 8.8.4.4)
netbios_name_servers %w(8.8.8.8 8.8.4.4)
netbios_node_type 2
aws_tags :chef_type => "aws_dhcp_options"
end
aws_vpc 'ref-vpc' do
cidr_block '10.0.0.0/16'
internet_gateway true
instance_tenancy :default
main_routes '0.0.0.0/0' => :internet_gateway
dhcp_options 'ref-dhcp-options'
enable_dns_support true
enable_dns_hostnames true
aws_tags :chef_type => "aws_vpc"
end
aws_route_table 'ref-main-route-table' do
vpc 'ref-vpc'
routes '0.0.0.0/0' => :internet_gateway
aws_tags :chef_type => "aws_route_table"
end
aws_vpc 'ref-vpc' do
main_route_table 'ref-main-route-table'
end
When performing unit testing on the aws_vpc through ChefSpec, the separate create/update resource definitions conflict. The second definition overwrites the first and resource property definitions end up leading to nil property values instead of the expected value.
This recipe works without error when run against an AWS account using chef client -z <recipe> and chef-dk 0.9.
....F.
ChefSpec Coverage report generated...
Total Resources: 3
Touched Resources: 3
Touch Coverage: 100.0%
You are awesome and so is your test coverage! Have a fantastic day!
Failures:
1) chef-provisioning-resource-testing::default created vpc with correct cidr block
Failure/Error: expect(chef_run.aws_vpc('ref-vpc').cidr_block).to eq('10.0.0.0/16')
expected: "10.0.0.0/16"
got: nil
(compared using ==)
# ./spec/unit/recipes/default_spec.rb:36:in `block (2 levels) in <top (required)>'
Finished in 28.92 seconds (files took 5.1 seconds to load)
6 examples, 1 failure
Failed examples:
rspec ./spec/unit/recipes/default_spec.rb:35 # chef-provisioning-resource-testing::default created vpc with correct cidr block
You can see an example of this pattern from the chef-provisioning-aws repo here: https://github.com/chef/chef-provisioning-aws/blob/master/docs/examples/ref_full.rb
Testing recipe:
require 'spec_helper'
describe 'chef-provisioning-resource-testing::default' do
after(:all) do
ChefSpec::Coverage.report!
end
let(:chef_run) do
runner = ChefSpec::ServerRunner.new
runner.converge(described_recipe)
end
it 'converges successfully' do
expect { chef_run }.to_not raise_error
end
it 'created aws_dhcp_options' do
expect(chef_run).to create_aws_dhcp_options('ref-dhcp-options')
end
it 'created aws_vpc' do
expect(chef_run).to create_aws_vpc('ref-vpc')
end
it 'created aws_route_table' do
expect(chef_run).to create_aws_route_table('ref-main-route-table')
end
it 'created vpc with correct cidr block' do
expect(chef_run.aws_vpc('ref-vpc').cidr_block).to eq('10.0.0.0/16')
end
it 'created with correct route table' do
expect(chef_run.aws_vpc('ref-vpc').main_route_table).to eq('ref-main-route-table')
end
end
Matcher definitions:
if defined?(ChefSpec)
ChefSpec.define_matcher :aws_dhcp_options
def create_aws_dhcp_options(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:aws_dhcp_options, :create, resource_name)
end
def destroy_aws_dhcp_options(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:aws_dhcp_options, :destroy, resource_name)
end
ChefSpec.define_matcher :aws_vpc
def create_aws_vpc(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:aws_vpc, :create, resource_name)
end
def destroy_aws_vpc(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:aws_vpc, :destroy, resource_name)
end
ChefSpec.define_matcher :aws_route_table
def create_aws_route_table(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:aws_route_table, :create, resource_name)
end
def destroy_aws_route_table(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:aws_route_table, :destroy, resource_name)
end
end
It appears that you have two resources with the same name. It's probably validating both tests against the latter that does not have the cidr_block config?
Hi @nspottsie
You have two resources named aws_vcp with the same name. Resources in ChefSpec must be globally unique or else you are overwriting the previous definition.
Unfortunately this is the pattern required by the chef-provisioning-aws driver. We have abandoned chef for provisioning and are currently moving to terraform.
Thanks for your continued support of both tools.