Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for tenancy parameter in placement config. #235

Merged
merged 13 commits into from
Feb 7, 2017
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ gem "test-kitchen"
gem "winrm-transport"
gem "winrm-fs"
gem "activesupport", "~> 4.0"
gem "faraday-http-cache", "~> 1.3"

group :test do
gem "rake"
Expand Down
23 changes: 23 additions & 0 deletions lib/kitchen/driver/aws/instance_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ def ec2_instance_data # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
end
i[:placement] = { :availability_zone => availability_zone.downcase }
end
tenancy = config[:tenancy]
if tenancy && %w[default dedicated].include?(tenancy)
if i.key?(:placement)
i[:placement][:tenancy] = tenancy
else
i[:placement] = { :tenancy => tenancy }
end
end
unless config[:block_device_mappings].nil? || config[:block_device_mappings].empty?
i[:block_device_mappings] = config[:block_device_mappings]
end
Expand Down Expand Up @@ -84,6 +92,21 @@ def ec2_instance_data # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
i[:network_interfaces][0][:groups] = i.delete(:security_group_ids)
end
end
availability_zone = config[:availability_zone]
if availability_zone
if availability_zone =~ /^[a-z]$/i
availability_zone = "#{config[:region]}#{availability_zone}"
end
i[:placement] = { :availability_zone => availability_zone.downcase }
end
tenancy = config[:tenancy]
if tenancy && %w[default dedicated].include?(tenancy)
if i.key?(:placement)
i[:placement][:tenancy] = tenancy
else
i[:placement] = { :tenancy => tenancy }
end
end
unless config[:instance_initiated_shutdown_behavior].nil? ||
config[:instance_initiated_shutdown_behavior].empty?
i[:instance_initiated_shutdown_behavior] = config[:instance_initiated_shutdown_behavior]
Expand Down
1 change: 1 addition & 0 deletions lib/kitchen/driver/ec2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class Ec2 < Kitchen::Driver::Base # rubocop:disable Metrics/ClassLength
default_config :interface, nil
default_config :http_proxy, ENV["HTTPS_PROXY"] || ENV["HTTP_PROXY"]
default_config :retry_limit, 3
default_config :tenancy, "default"
default_config :instance_initiated_shutdown_behavior, nil

def initialize(*args, &block)
Expand Down
122 changes: 122 additions & 0 deletions spec/kitchen/driver/ec2/instance_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,128 @@
end
end

context "when availability_zone and tenancy are provided" do
let(:config) do
{
:region => "eu-east-1",
:availability_zone => "c",
:tenancy => "dedicated"
}
end
it "adds the region to it in the instance data" do
expect(generator.ec2_instance_data).to eq(
:instance_type => nil,
:ebs_optimized => nil,
:image_id => nil,
:key_name => nil,
:subnet_id => nil,
:private_ip_address => nil,
:placement => { :availability_zone => "eu-east-1c",
:tenancy => "dedicated" }
)
end
end

context "when tenancy is provided but availability_zone isn't" do
let(:config) do
{
:region => "eu-east-1",
:tenancy => "default"
}
end
it "is not added to the instance data" do
expect(generator.ec2_instance_data).to eq(
:instance_type => nil,
:ebs_optimized => nil,
:image_id => nil,
:key_name => nil,
:subnet_id => nil,
:private_ip_address => nil,
:placement => { :tenancy => "default" }
)
end
end

context "when tenancy is not supported" do
let(:config) do
{
:region => "eu-east-1",
:tenancy => "ephemeral"
}
end
it "is not added to the instance data" do
expect(generator.ec2_instance_data).to eq(
:instance_type => nil,
:ebs_optimized => nil,
:image_id => nil,
:key_name => nil,
:subnet_id => nil,
:private_ip_address => nil
)
end
end

context "when availability_zone and tenancy are provided" do
let(:config) do
{
:region => "eu-east-1",
:availability_zone => "c",
:tenancy => "dedicated"
}
end
it "adds the region to it in the instance data" do
expect(generator.ec2_instance_data).to eq(
:instance_type => nil,
:ebs_optimized => nil,
:image_id => nil,
:key_name => nil,
:subnet_id => nil,
:private_ip_address => nil,
:placement => { :availability_zone => "eu-east-1c",
:tenancy => "dedicated" }
)
end
end

context "when tenancy is provided but availability_zone isn't" do
let(:config) do
{
:region => "eu-east-1",
:tenancy => "default"
}
end
it "is not added to the instance data" do
expect(generator.ec2_instance_data).to eq(
:instance_type => nil,
:ebs_optimized => nil,
:image_id => nil,
:key_name => nil,
:subnet_id => nil,
:private_ip_address => nil,
:placement => { :tenancy => "default" }
)
end
end

context "when tenancy is not supported" do
let(:config) do
{
:region => "eu-east-1",
:tenancy => "ephemeral"
}
end
it "is not added to the instance data" do
expect(generator.ec2_instance_data).to eq(
:instance_type => nil,
:ebs_optimized => nil,
:image_id => nil,
:key_name => nil,
:subnet_id => nil,
:private_ip_address => nil
)
end
end

context "when subnet_id is provided" do
let(:config) do
{
Expand Down