Skip to content

Commit

Permalink
Adding unit tests for new block mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
tyler-ball committed Feb 18, 2015
1 parent d883a9a commit 94a9396
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 16 deletions.
13 changes: 9 additions & 4 deletions lib/kitchen/driver/ec2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ class Ec2 < Kitchen::Driver::SSHBase
deprecated_configs.each do |d|
validations[d] = lambda do |attr, val, driver|
unless val.nil?
warn("WARN: The config key `#{attr}` is deprecated, please use `block_device_mappings`")
driver.warn "WARN: The config key `#{attr}` is deprecated," +
" please use `block_device_mappings`"
end
end
end
Expand Down Expand Up @@ -265,7 +266,7 @@ def hostname(server)
#
def self.hostname(server, interface_type=nil)
if interface_type
INTERFACE_TYPES.fetch(interface_type) do
interface_type = INTERFACE_TYPES.fetch(interface_type) do
raise Kitchen::UserError, "Invalid interface [#{interface_type}]"
end
server.send(interface_type)
Expand Down Expand Up @@ -321,11 +322,15 @@ def block_device_mappings
end

# This could be helpful for users debugging
image = connection.images.get(config[:image_id])
image_id = config[:image_id]
image = connection.images.get(image_id)
if image.nil?
raise "Could not find image [#{image_id}]"
end
root_device_name = image.root_device_name
bdms.find { |bdm|
if bdm[:ebs_device_name] == root_device_name
info("Overriding root device [#{root_device_name}] from image [#{config[:image_id]}]")
info("Overriding root device [#{root_device_name}] from image [#{image_id}]")
end
}

Expand Down
134 changes: 122 additions & 12 deletions spec/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,24 @@
end

let(:server) do
double(:id => "123",
:wait_for => nil,
:dns_name => "server.example.com",
:private_ip_address => '172.13.16.11',
:public_ip_address => '213.225.123.134')
double(
:id => "123",
:wait_for => nil,
:dns_name => "server.example.com",
:private_ip_address => '172.13.16.11',
:public_ip_address => '213.225.123.134'
)
end

let(:instance) do
Kitchen::Instance.new(:platform => double(:name => "centos-6.4"),
:suite => double(:name => "default"),
:driver => driver,
:provisioner => Kitchen::Provisioner::Dummy.new({}),
:busser => double("busser"),
:state_file => double("state_file"))
Kitchen::Instance.new(
:platform => double(:name => "centos-6.4"),
:suite => double(:name => "default"),
:driver => driver,
:provisioner => Kitchen::Provisioner::Dummy.new({}),
:busser => double("busser"),
:state_file => double("state_file")
)
end

let(:driver) do
Expand Down Expand Up @@ -65,7 +69,7 @@

it 'throws a nice exception if the config is bogus' do
config[:interface] = 'I am an idiot'
expect { driver.create(state) }.to raise_error(Kitchen::UserError, 'Invalid interface')
expect { driver.create(state) }.to raise_error(Kitchen::UserError, /^Invalid interface/)
end

end
Expand Down Expand Up @@ -124,4 +128,110 @@

end

describe '#block_device_mappings' do
let(:connection) { double(Fog::Compute) }
let(:image) { double('Image', :root_device_name => 'name') }
before do
expect(driver).to receive(:connection).and_return(connection)
end

context 'with bad config[:image_id]' do
let(:config) do
{
aws_ssh_key_id: 'larry',
aws_access_key_id: 'secret',
aws_secret_access_key: 'moarsecret',
image_id: 'foobar'
}
end

it 'raises an error' do
expect(connection).to receive_message_chain('images.get').with('foobar').and_return nil
expect { driver.send(:block_device_mappings) }.to raise_error(/Could not find image/)
end
end

context 'with good config[:image_id]' do
before do
expect(connection).to receive_message_chain('images.get')
.with('ami-bf5021d6').and_return image
end

context 'no config is set' do
it 'returns an empty default' do
expect(driver.send(:block_device_mappings)).to eq([{
"Ebs.VolumeType"=>"standard",
"Ebs.VolumeSize"=>nil,
"Ebs.DeleteOnTermination"=>nil,
"Ebs.SnapshotId"=>nil,
"DeviceName"=>nil,
"VirtualName"=>nil
}])
end
end

context 'deprecated configs are set' do
let(:config) do
{
aws_ssh_key_id: 'larry',
aws_access_key_id: 'secret',
aws_secret_access_key: 'moarsecret',
ebs_volume_size: 100,
ebs_delete_on_termination: false,
ebs_device_name: 'name'
}
end

it 'returns an empty default' do
expect(driver.send(:block_device_mappings)).to eq([{
"Ebs.VolumeType"=>"standard",
"Ebs.VolumeSize"=>100,
"Ebs.DeleteOnTermination"=>false,
"Ebs.SnapshotId"=>nil,
"DeviceName"=>'name',
"VirtualName"=>nil
}])
end
end

context 'deprecated configs are set and so are new configs' do
let(:block1) do
{
:ebs_volume_type => 'io1',
:ebs_volume_size => 22,
:ebs_delete_on_termination => true,
:ebs_snapshot_id => 'snap-12345',
:ebs_device_name => '/dev/sda1',
:ebs_virtual_name => 'main'
}
end

let(:config) do
{
aws_ssh_key_id: 'larry',
aws_access_key_id: 'secret',
aws_secret_access_key: 'moarsecret',
ebs_volume_size: 100,
ebs_delete_on_termination: false,
ebs_device_name: 'name',
block_device_mappings: [
block1
]
}
end

it 'returns the new configs' do
expect(driver.send(:block_device_mappings)).to eq([{
"Ebs.VolumeType"=>'io1',
"Ebs.VolumeSize"=>22,
"Ebs.DeleteOnTermination"=>true,
"Ebs.SnapshotId"=>'snap-12345',
"DeviceName"=>'/dev/sda1',
"VirtualName"=>'main'
}])
end
end
end
end

end

0 comments on commit 94a9396

Please sign in to comment.