1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[openstack|compute] Add volume tests

This commit is contained in:
Ferran Rodenas 2013-04-04 12:00:41 +02:00
parent 8b84922ac7
commit 412a6cc3ae
10 changed files with 131 additions and 75 deletions

View file

@ -226,7 +226,8 @@ module Fog
'volumes' => 10,
'cores' => 20,
'ram' => 51200
}
},
:volumes => {}
}
end
end

View file

@ -21,16 +21,21 @@ module Fog
end
class Mock
def attach_volume(volume_id, server_id, device)
response = Excon::Response.new
response.status = 200
response.body ={ "volumeAttachment" => {
"id" => volume_id,
"volumeId" => volume_id
}
data = {
'id' => volume_id,
'volumeId' => volume_id,
'serverId' => server_id,
'device' => device
}
self.data[:volumes][volume_id]['attachments'] << data
response.body = { 'volumeAttachment' => data }
response
end
end
end

View file

@ -31,11 +31,10 @@ module Fog
def create_volume(name, description, size, options={})
response = Excon::Response.new
response.status = 202
response.body = {
'volume' => {
data = {
'id' => Fog::Mock.random_numbers(2),
'display_name' => name,
'display_description' => description,
'name' => name,
'description' => description,
'size' => size,
'status' => 'creating',
'snapshot_id' => '4',
@ -44,9 +43,11 @@ module Fog
'created_at' => Time.now,
'attachments' => []
}
}
self.data[:volumes][data['id']] = data
response.body = { 'volume' => data }
response
end
end
end

View file

@ -14,13 +14,20 @@ module Fog
end
class Mock
def delete_volume(volume_id)
response = Excon::Response.new
if list_volumes.body['volumes'].map { |v| v['id'] }.include? volume_id
self.data[:volumes].delete(volume_id)
response.status = 204
response
else
raise Fog::Compute::OpenStack::NotFound
end
end
end
end
end
end

View file

@ -14,10 +14,15 @@ module Fog
end
class Mock
def detach_volume(server_id, attachment_id)
response = Excon::Response.new
if self.data[:volumes][attachment_id]['attachments'].reject! { |attachment| attachment['serverId'] == server_id }
response.status = 202
response
else
raise Fog::Compute::OpenStack::NotFound
end
end
end

View file

@ -16,6 +16,16 @@ module Fog
class Mock
def get_server_volumes(server_id)
response = Excon::Response.new
response.status = 200
data = self.data[:volumes].values.find_all do |vol|
vol['attachments'].find { |attachment| attachment["serverId"] == server_id }
end
response.body = { 'volumeAttachments' => data.collect! { |vol| vol['attachments'] }.flatten(1) }
response
end
end
end

View file

@ -18,22 +18,13 @@ module Fog
def get_volume_details(volume_id)
response = Excon::Response.new
if data = self.data[:volumes][volume_id]
response.status = 200
response.body = {
'volume' => {
'id' => '1',
'display_name' => Fog::Mock.random_letters(rand(8) + 5),
'display_description' => Fog::Mock.random_letters(rand(12) + 10),
'size' => 3,
'volume_type' => nil,
'snapshot_id' => '4',
'status' => 'online',
'availability_zone' => 'nova',
'created_at' => Time.now,
'attachments' => []
}
}
response.body = { 'volume' => data }
response
else
raise Fog::Compute::OpenStack::NotFound
end
end
end

View file

@ -17,32 +17,10 @@ module Fog
class Mock
def list_volumes(detailed=true)
response = Excon::Response.new
response.status = 200
self.data[:volumes] ||= [
{ "status" => "available",
"display_description" => "",
"availability_zone" => "nova",
"display_name" => "test 1",
"attachments" => [{}],
"volume_type" => nil,
"snapshot_id" => nil,
"size" => 1,
"id" => Fog::Mock.random_hex(32),
"created_at" => Time.now },
{ "status" => "available",
"display_description" => "",
"availability_zone" => "nova",
"display_name" => "test 2",
"attachments" => [{}],
"volume_type" => nil,
"snapshot_id" => nil,
"size" => 1,
"id" => Fog::Mock.random_hex(32),
"created_at" => Time.now }
]
response.body = { 'volumes' => self.data[:volumes] }
response
Excon::Response.new(
:body => { 'volumes' => self.data[:volumes].values },
:status => 200
)
end
end

View file

@ -25,7 +25,7 @@ Shindo.tests("Fog::Compute[:openstack] | server", ['openstack']) do
group = found_groups.first
returns('my_group') { group.name }
returns(server.connection) { group.connection }
returns(server.service) { group.service }
ensure
unless Fog.mocking? then
server.destroy if server
@ -79,7 +79,63 @@ Shindo.tests("Fog::Compute[:openstack] | server", ['openstack']) do
end
tests('#volumes').succeeds do
fog = Fog::Compute[:openstack]
begin
volume = fog.volumes.new(:name => 'test volume',
:description => 'test volume',
:size => 1)
volume.save
volume.wait_for { volume.status == 'available' } unless Fog.mocking?
flavor = fog.flavors.first.id
image = fog.images.first.id
server = fog.servers.new(:name => 'test server',
:flavor_ref => flavor,
:image_ref => image)
server.save
server.wait_for { server.state == "ACTIVE" } unless Fog.mocking?
server.attach_volume(volume.id, '/dev/vdc')
volume.wait_for { volume.status == 'in-use' } unless Fog.mocking?
found_volumes = server.volumes
returns(1) { found_volumes.length }
volume = found_volumes.first
returns('test volume') { volume.name }
found_attachments = server.volume_attachments
returns(1) { found_attachments.length }
attachment = found_attachments.first
returns('/dev/vdc') { attachment['device'] }
server.detach_volume(volume.id)
volume.wait_for { volume.status == 'available' } unless Fog.mocking?
found_volumes = server.volumes
returns(0) { found_volumes.length }
found_attachments = server.volume_attachments
returns(0) { found_attachments.length }
ensure
unless Fog.mocking? then
server.destroy if server
volume.destroy if volume
begin
fog.servers.get(server.id).wait_for do false end
fog.volumes.get(volume.id).wait_for do false end
rescue Fog::Errors::Error
# ignore, server went away
end
end
end
end
end
end

View file

@ -4,9 +4,9 @@ Shindo.tests('Fog::Compute[:openstack] | volume requests', ['openstack']) do
@volume_format = {
'id' => String,
'display_name' => String,
'name' => String,
'size' => Integer,
'display_description' => String,
'description' => String,
'status' => String,
'snapshot_id' => Fog::Nullable::String,
'availability_zone' => String,
@ -16,23 +16,25 @@ Shindo.tests('Fog::Compute[:openstack] | volume requests', ['openstack']) do
}
tests('success') do
tests('#create_volume').formats({'volume' => @volume_format}) do
pending unless Fog.mocking?
Fog::Compute[:openstack].create_volume('loud', 'this is a loud volume', 3).body
end
tests('#list_volumes').formats({'volumes' => [@volume_format]}) do
Fog::Compute[:openstack].list_volumes.body
end
tests('#get_volume_detail').formats({'volume' => @volume_format}) do
pending unless Fog.mocking?
Fog::Compute[:openstack].get_volume_details(1).body
end
tests('#create_volume').formats({'volume' => @volume_format}) do
pending unless Fog.mocking?
Fog::Compute[:openstack].create_volume('loud', 'this is a loud volume', 3).body
volume_id = Fog::Compute[:openstack].volumes.all.first.id
Fog::Compute[:openstack].get_volume_details(volume_id).body
end
tests('#delete_volume').succeeds do
pending unless Fog.mocking?
Fog::Compute[:openstack].delete_volume(1)
volume_id = Fog::Compute[:openstack].volumes.all.first.id
Fog::Compute[:openstack].delete_volume(volume_id)
end
end
end