From 412a6cc3aedd5a1264f29dd5b6735b508543b9d8 Mon Sep 17 00:00:00 2001 From: Ferran Rodenas Date: Thu, 4 Apr 2013 12:00:41 +0200 Subject: [PATCH] [openstack|compute] Add volume tests --- lib/fog/openstack/compute.rb | 3 +- .../requests/compute/attach_volume.rb | 15 +++-- .../requests/compute/create_volume.rb | 27 ++++----- .../requests/compute/delete_volume.rb | 11 +++- .../requests/compute/detach_volume.rb | 9 ++- .../requests/compute/get_server_volumes.rb | 10 ++++ .../requests/compute/get_volume_details.rb | 23 +++----- .../requests/compute/list_volumes.rb | 30 ++-------- .../openstack/models/compute/server_tests.rb | 58 ++++++++++++++++++- .../requests/compute/volume_tests.rb | 20 ++++--- 10 files changed, 131 insertions(+), 75 deletions(-) diff --git a/lib/fog/openstack/compute.rb b/lib/fog/openstack/compute.rb index 1cd5a709d..6c0a997a8 100644 --- a/lib/fog/openstack/compute.rb +++ b/lib/fog/openstack/compute.rb @@ -226,7 +226,8 @@ module Fog 'volumes' => 10, 'cores' => 20, 'ram' => 51200 - } + }, + :volumes => {} } end end diff --git a/lib/fog/openstack/requests/compute/attach_volume.rb b/lib/fog/openstack/requests/compute/attach_volume.rb index dcfd7ad91..f29a45d9d 100644 --- a/lib/fog/openstack/requests/compute/attach_volume.rb +++ b/lib/fog/openstack/requests/compute/attach_volume.rb @@ -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 diff --git a/lib/fog/openstack/requests/compute/create_volume.rb b/lib/fog/openstack/requests/compute/create_volume.rb index 1a8a2fced..829e2d7c3 100644 --- a/lib/fog/openstack/requests/compute/create_volume.rb +++ b/lib/fog/openstack/requests/compute/create_volume.rb @@ -31,22 +31,23 @@ module Fog def create_volume(name, description, size, options={}) response = Excon::Response.new response.status = 202 - response.body = { - 'volume' => { - 'id' => Fog::Mock.random_numbers(2), - 'display_name' => name, - 'display_description' => description, - 'size' => size, - 'status' => 'creating', - 'snapshot_id' => '4', - 'volume_type' => nil, - 'availability_zone' => 'nova', - 'created_at' => Time.now, - 'attachments' => [] - } + data = { + 'id' => Fog::Mock.random_numbers(2), + 'name' => name, + 'description' => description, + 'size' => size, + 'status' => 'creating', + 'snapshot_id' => '4', + 'volume_type' => nil, + 'availability_zone' => 'nova', + 'created_at' => Time.now, + 'attachments' => [] } + self.data[:volumes][data['id']] = data + response.body = { 'volume' => data } response end + end end diff --git a/lib/fog/openstack/requests/compute/delete_volume.rb b/lib/fog/openstack/requests/compute/delete_volume.rb index 8009cc559..ed245d976 100644 --- a/lib/fog/openstack/requests/compute/delete_volume.rb +++ b/lib/fog/openstack/requests/compute/delete_volume.rb @@ -14,11 +14,18 @@ module Fog end class Mock + def delete_volume(volume_id) response = Excon::Response.new - response.status = 204 - response + 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 diff --git a/lib/fog/openstack/requests/compute/detach_volume.rb b/lib/fog/openstack/requests/compute/detach_volume.rb index 1b838ca40..887800b41 100644 --- a/lib/fog/openstack/requests/compute/detach_volume.rb +++ b/lib/fog/openstack/requests/compute/detach_volume.rb @@ -14,10 +14,15 @@ module Fog end class Mock + def detach_volume(server_id, attachment_id) response = Excon::Response.new - response.status = 202 - response + 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 diff --git a/lib/fog/openstack/requests/compute/get_server_volumes.rb b/lib/fog/openstack/requests/compute/get_server_volumes.rb index 64b26835e..5a60d1dd0 100644 --- a/lib/fog/openstack/requests/compute/get_server_volumes.rb +++ b/lib/fog/openstack/requests/compute/get_server_volumes.rb @@ -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 diff --git a/lib/fog/openstack/requests/compute/get_volume_details.rb b/lib/fog/openstack/requests/compute/get_volume_details.rb index bbeff4839..8fb2ccd51 100644 --- a/lib/fog/openstack/requests/compute/get_volume_details.rb +++ b/lib/fog/openstack/requests/compute/get_volume_details.rb @@ -18,22 +18,13 @@ module Fog def get_volume_details(volume_id) response = Excon::Response.new - 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 + if data = self.data[:volumes][volume_id] + response.status = 200 + response.body = { 'volume' => data } + response + else + raise Fog::Compute::OpenStack::NotFound + end end end diff --git a/lib/fog/openstack/requests/compute/list_volumes.rb b/lib/fog/openstack/requests/compute/list_volumes.rb index 9fa380d26..b8f811ee5 100644 --- a/lib/fog/openstack/requests/compute/list_volumes.rb +++ b/lib/fog/openstack/requests/compute/list_volumes.rb @@ -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 diff --git a/tests/openstack/models/compute/server_tests.rb b/tests/openstack/models/compute/server_tests.rb index 5450c4026..b5de06835 100644 --- a/tests/openstack/models/compute/server_tests.rb +++ b/tests/openstack/models/compute/server_tests.rb @@ -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 diff --git a/tests/openstack/requests/compute/volume_tests.rb b/tests/openstack/requests/compute/volume_tests.rb index 90d6830a8..e08755e23 100644 --- a/tests/openstack/requests/compute/volume_tests.rb +++ b/tests/openstack/requests/compute/volume_tests.rb @@ -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