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

Fixing 2 regressions in 0.9.1 #128

Merged
merged 2 commits into from
May 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions lib/kitchen/driver/aws/instance_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# limitations under the License.

require "kitchen/logging"
require "base64"

module Kitchen

Expand Down Expand Up @@ -62,13 +63,21 @@ def ec2_instance_data # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
i[:network_interfaces] =
[{
:device_index => 0,
:associate_public_ip_address => config[:associate_public_ip]
:associate_public_ip_address => config[:associate_public_ip],
:delete_on_termination => true
}]
# If specifying `:network_interfaces` in the request, you must specify the
# subnet_id in the network_interfaces block and not at the top level
# If specifying `:network_interfaces` in the request, you must specify
# network specific configs in the network_interfaces block and not at
# the top level
if config[:subnet_id]
i[:network_interfaces][0][:subnet_id] = i.delete(:subnet_id)
end
if config[:private_ip_address]
i[:network_interfaces][0][:private_ip_address] = i.delete(:private_ip_address)
end
if config[:security_group_ids]
i[:network_interfaces][0][:groups] = i.delete(:security_group_ids)
end
end
i
end
Expand Down Expand Up @@ -134,14 +143,15 @@ def debug_if_root_device(bdms)

def prepared_user_data
# If user_data is a file reference, lets read it as such
unless @user_data
if config[:user_data] && File.file?(config[:user_data])
return nil if config[:user_data].nil?
@user_data ||= begin
if File.file?(config[:user_data])
@user_data = File.read(config[:user_data])
else
@user_data = config[:user_data]
end
@user_data = Base64.encode64(@user_data)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, that seems to be a low-level concern that we need to deal with as a user of the sdk. Ah well

end
@user_data
end

end
Expand Down
102 changes: 94 additions & 8 deletions spec/kitchen/driver/ec2/instance_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
require "kitchen/driver/aws/instance_generator"
require "kitchen/driver/aws/client"
require "tempfile"
require "base64"

describe Kitchen::Driver::Aws::InstanceGenerator do

Expand Down Expand Up @@ -70,14 +71,15 @@
end

it "reads the file contents" do
expect(generator.prepared_user_data).to eq("foo\nbar")
expect(Base64.decode64(generator.prepared_user_data)).to eq("foo\nbar")
end

it "memoizes the file contents" do
expect(generator.prepared_user_data).to eq("foo\nbar")
decoded = Base64.decode64(generator.prepared_user_data)
expect(decoded).to eq("foo\nbar")
tmp_file.write("other\nvalue")
tmp_file.rewind
expect(generator.prepared_user_data).to eq("foo\nbar")
expect(decoded).to eq("foo\nbar")
end
end
end
Expand Down Expand Up @@ -280,9 +282,92 @@
:key_name => nil,
:subnet_id => nil,
:private_ip_address => nil,
:network_interfaces => [{ :device_index => 0, :associate_public_ip_address => true }]
:network_interfaces => [{
:device_index => 0,
:associate_public_ip_address => true,
:delete_on_termination => true
}]
)
end

context "and subnet is provided" do
let(:config) do
{
:associate_public_ip => true,
:subnet_id => "s-456"
}
end

it "adds a network_interfaces block" do
expect(generator.ec2_instance_data).to eq(
:placement => { :availability_zone => nil },
:instance_type => nil,
:ebs_optimized => nil,
:image_id => nil,
:key_name => nil,
:private_ip_address => nil,
:network_interfaces => [{
:device_index => 0,
:associate_public_ip_address => true,
:delete_on_termination => true,
:subnet_id => "s-456"
}]
)
end
end

context "and security_group_ids is provided" do
let(:config) do
{
:associate_public_ip => true,
:security_group_ids => ["sg-789"]
}
end

it "adds a network_interfaces block" do
expect(generator.ec2_instance_data).to eq(
:placement => { :availability_zone => nil },
:instance_type => nil,
:ebs_optimized => nil,
:image_id => nil,
:key_name => nil,
:subnet_id => nil,
:private_ip_address => nil,
:network_interfaces => [{
:device_index => 0,
:associate_public_ip_address => true,
:delete_on_termination => true,
:groups => ["sg-789"]
}]
)
end
end

context "and private_ip_address is provided" do
let(:config) do
{
:associate_public_ip => true,
:private_ip_address => "0.0.0.0"
}
end

it "adds a network_interfaces block" do
expect(generator.ec2_instance_data).to eq(
:placement => { :availability_zone => nil },
:instance_type => nil,
:ebs_optimized => nil,
:image_id => nil,
:key_name => nil,
:subnet_id => nil,
:network_interfaces => [{
:device_index => 0,
:associate_public_ip_address => true,
:delete_on_termination => true,
:private_ip_address => "0.0.0.0"
}]
)
end
end
end

context "when provided the maximum config" do
Expand Down Expand Up @@ -319,7 +404,6 @@
:ebs_optimized => true,
:image_id => "ami-123",
:key_name => "key",
:private_ip_address => "0.0.0.0",
:block_device_mappings => [
{
:ebs => {
Expand All @@ -336,10 +420,12 @@
:network_interfaces => [{
:device_index => 0,
:associate_public_ip_address => true,
:subnet_id => "s-456"
:subnet_id => "s-456",
:delete_on_termination => true,
:groups => ["sg-789"],
:private_ip_address => "0.0.0.0"
}],
:security_group_ids => ["sg-789"],
:user_data => "foo"
:user_data => Base64.encode64("foo")
)
end
end
Expand Down