From b7daf27003873dea95fca836615182ea7d2429ac Mon Sep 17 00:00:00 2001 From: Eugene Howe Date: Mon, 14 Jan 2013 10:03:37 -0500 Subject: [PATCH] [rackspace] Moved methods to MockData module --- lib/fog/rackspace.rb | 28 ----------------- lib/fog/rackspace/mock_data.rb | 30 ++++++++++++++++++- .../requests/block_storage/create_snapshot.rb | 4 +-- .../requests/block_storage/create_volume.rb | 4 +-- .../requests/compute_v2/create_image.rb | 2 +- .../requests/compute_v2/create_server.rb | 14 ++++----- .../requests/compute_v2/get_server.rb | 2 +- .../requests/compute_v2/list_flavors.rb | 2 +- .../requests/compute_v2/list_servers.rb | 2 +- .../requests/compute_v2/update_server.rb | 2 +- .../models/compute_v2/server_tests.rb | 5 ++-- 11 files changed, 48 insertions(+), 47 deletions(-) diff --git a/lib/fog/rackspace.rb b/lib/fog/rackspace.rb index c9a889dea..f9a423a2b 100644 --- a/lib/fog/rackspace.rb +++ b/lib/fog/rackspace.rb @@ -5,34 +5,6 @@ module Fog module Rackspace extend Fog::Provider - def self.uuid - [8,4,4,4,12].map{|i| Fog::Mock.random_hex(i)}.join("-") - end - - def self.ipv4_address - 4.times.map{ Fog::Mock.random_numbers(3) }.join(".") - end - - def self.ipv6_address - 8.times.map { Fog::Mock.random_hex(4) }.join(":") - end - - def self.keep(hash, *keys) - {}.tap do |kept| - keys.each{|k| kept[k]= hash[k] if hash.key?(k)} - end - end - - def self.slice(hash, *keys) - hash.dup.tap do |sliced| - keys.each{|k| sliced.delete(k)} - end - end - - def self.zulu_time - Time.now.strftime("%Y-%m-%dT%H:%M:%SZ") - end - module Errors class ServiceError < Fog::Errors::Error attr_reader :response_data, :status_code diff --git a/lib/fog/rackspace/mock_data.rb b/lib/fog/rackspace/mock_data.rb index d07b7b2af..f20e01d28 100644 --- a/lib/fog/rackspace/mock_data.rb +++ b/lib/fog/rackspace/mock_data.rb @@ -7,7 +7,7 @@ module Fog #Compute V2 flavor_id = Fog.credentials[:rackspace_flavor_id].to_s ||= Fog::Mock.random_numbers(1) - image_id = Fog.credentials[:rackspace_image_id] ||= Fog::Rackspace.uuid + image_id = Fog.credentials[:rackspace_image_id] ||= Fog::Rackspace::MockData.uuid image_name = Fog::Mock.random_letters(6) flavor = { @@ -111,6 +111,34 @@ module Fog end end[@rackspace_api_key] end + + def self.uuid + [8,4,4,4,12].map{|i| Fog::Mock.random_hex(i)}.join("-") + end + + def self.ipv4_address + 4.times.map{ Fog::Mock.random_numbers(3) }.join(".") + end + + def self.ipv6_address + 8.times.map { Fog::Mock.random_hex(4) }.join(":") + end + + def self.keep(hash, *keys) + {}.tap do |kept| + keys.each{|k| kept[k]= hash[k] if hash.key?(k)} + end + end + + def self.slice(hash, *keys) + hash.dup.tap do |sliced| + keys.each{|k| sliced.delete(k)} + end + end + + def self.zulu_time + Time.now.strftime("%Y-%m-%dT%H:%M:%SZ") + end end end end diff --git a/lib/fog/rackspace/requests/block_storage/create_snapshot.rb b/lib/fog/rackspace/requests/block_storage/create_snapshot.rb index 7918056b2..7ad914922 100644 --- a/lib/fog/rackspace/requests/block_storage/create_snapshot.rb +++ b/lib/fog/rackspace/requests/block_storage/create_snapshot.rb @@ -28,7 +28,7 @@ module Fog if volume.nil? raise Fog::Rackspace::BlockStorage::NotFound else - snapshot_id = Fog::Rackspace.uuid + snapshot_id = Fog::Rackspace::MockData.uuid display_name = options[:display_name] || "test snapshot" display_description = options[:display_description] || "test snapshot description" @@ -39,7 +39,7 @@ module Fog "volume_id" => volume_id, "status" => "available", "size" => volume["size"], - "created_at" => Fog::Rackspace.zulu_time, + "created_at" => Fog::Rackspace::MockData.zulu_time, "availability_zone" => "nova", } diff --git a/lib/fog/rackspace/requests/block_storage/create_volume.rb b/lib/fog/rackspace/requests/block_storage/create_volume.rb index 0277f4d74..989b2232d 100644 --- a/lib/fog/rackspace/requests/block_storage/create_volume.rb +++ b/lib/fog/rackspace/requests/block_storage/create_volume.rb @@ -30,7 +30,7 @@ module Fog elsif size < 100 || size > 1024 raise Fog::Rackspace::BlockStorage::BadRequest.new("'size' parameter must be between 100 and 1024") else - volume_id = Fog::Rackspace.uuid + volume_id = Fog::Rackspace::MockData.uuid name = options[:display_name] || "test volume" description = options[:display_description] || "description goes here" volume_type = options[:volume_type] || "SATA" @@ -44,7 +44,7 @@ module Fog "volume_type" => volume_type, "snapshot_id" => nil, "attachments" => [], - "created_at" => Fog::Rackspace.zulu_time, + "created_at" => Fog::Rackspace::MockData.zulu_time, "availability_zone" => "nova", "metadata" => {}, } diff --git a/lib/fog/rackspace/requests/compute_v2/create_image.rb b/lib/fog/rackspace/requests/compute_v2/create_image.rb index b4dea6ee2..0c8aac109 100644 --- a/lib/fog/rackspace/requests/compute_v2/create_image.rb +++ b/lib/fog/rackspace/requests/compute_v2/create_image.rb @@ -28,7 +28,7 @@ module Fog class Mock def create_image(server_id, name, options = {}) - image_id = Fog::Rackspace.uuid + image_id = Fog::Rackspace::MockData.uuid image = { "OS-DCF:diskConfig" => "AUTO", "created" => "2012-02-28T19:38:57Z", diff --git a/lib/fog/rackspace/requests/compute_v2/create_server.rb b/lib/fog/rackspace/requests/compute_v2/create_server.rb index 168d5e767..7c9621dd8 100644 --- a/lib/fog/rackspace/requests/compute_v2/create_server.rb +++ b/lib/fog/rackspace/requests/compute_v2/create_server.rb @@ -29,11 +29,11 @@ module Fog class Mock def create_server(name, image_id, flavor_id, min_count, max_count, options={}) - server_id = Fog::Rackspace.uuid - public_ip4 = Fog::Rackspace.ipv4_address - public_ip6 = Fog::Rackspace.ipv6_address - private_ip4 = Fog::Rackspace.ipv4_address - private_ip6 = Fog::Rackspace.ipv6_address + server_id = Fog::Rackspace::MockData.uuid + public_ip4 = Fog::Rackspace::MockData.ipv4_address + public_ip6 = Fog::Rackspace::MockData.ipv6_address + private_ip4 = Fog::Rackspace::MockData.ipv4_address + private_ip6 = Fog::Rackspace::MockData.ipv6_address admin_pass = Fog::Mock.random_letters(12) flavor = self.data[:flavors][flavor_id] @@ -65,10 +65,10 @@ module Fog ] }, "created" => "2012-07-28T15:32:25Z", - "flavor" => Fog::Rackspace.keep(flavor, "id", "links"), + "flavor" => Fog::Rackspace::MockData.keep(flavor, "id", "links"), "hostId" => Fog::Mock.random_hex(56), "id" => server_id, - "image" => Fog::Rackspace.keep(image, "id", "links"), + "image" => Fog::Rackspace::MockData.keep(image, "id", "links"), "links" => [ { "href" => "https://dfw.servers.api.rackspacecloud.com/v2/010101/servers/#{server_id}", diff --git a/lib/fog/rackspace/requests/compute_v2/get_server.rb b/lib/fog/rackspace/requests/compute_v2/get_server.rb index a28d98856..bfefd6c63 100644 --- a/lib/fog/rackspace/requests/compute_v2/get_server.rb +++ b/lib/fog/rackspace/requests/compute_v2/get_server.rb @@ -17,7 +17,7 @@ module Fog if server.nil? raise Fog::Compute::RackspaceV2::NotFound else - server_response = Fog::Rackspace.keep(server, 'id', 'name', 'hostId', 'created', 'updated', 'status', 'progress', 'user_id', 'tenant_id', 'links', 'metadata', 'accessIPv4', 'accessIPv6', 'OS-DCF:diskConfig', 'rax-bandwidth:bandwidth', 'addresses', 'flavor', 'image') + server_response = Fog::Rackspace::MockData.keep(server, 'id', 'name', 'hostId', 'created', 'updated', 'status', 'progress', 'user_id', 'tenant_id', 'links', 'metadata', 'accessIPv4', 'accessIPv6', 'OS-DCF:diskConfig', 'rax-bandwidth:bandwidth', 'addresses', 'flavor', 'image') server_response['image']['links'].map! { |l| l.delete("type"); l } response(:body => {"server" => server_response}) end diff --git a/lib/fog/rackspace/requests/compute_v2/list_flavors.rb b/lib/fog/rackspace/requests/compute_v2/list_flavors.rb index b8d74ded8..fd0a4e346 100644 --- a/lib/fog/rackspace/requests/compute_v2/list_flavors.rb +++ b/lib/fog/rackspace/requests/compute_v2/list_flavors.rb @@ -13,7 +13,7 @@ module Fog class Mock def list_flavors - flavors = self.data[:flavors].values.map { |f| Fog::Rackspace.keep(f, 'id', 'name', 'links') } + flavors = self.data[:flavors].values.map { |f| Fog::Rackspace::MockData.keep(f, 'id', 'name', 'links') } response(:body => {"flavors" => flavors}) end end diff --git a/lib/fog/rackspace/requests/compute_v2/list_servers.rb b/lib/fog/rackspace/requests/compute_v2/list_servers.rb index 634781414..86434f019 100644 --- a/lib/fog/rackspace/requests/compute_v2/list_servers.rb +++ b/lib/fog/rackspace/requests/compute_v2/list_servers.rb @@ -13,7 +13,7 @@ module Fog class Mock def list_servers - servers = self.data[:servers].values.map { |s| Fog::Rackspace.keep(s, 'id', 'name', 'hostId', 'created', 'updated', 'status', 'progress', 'user_id', 'tenant_id', 'links', 'metadata') } + servers = self.data[:servers].values.map { |s| Fog::Rackspace::MockData.keep(s, 'id', 'name', 'hostId', 'created', 'updated', 'status', 'progress', 'user_id', 'tenant_id', 'links', 'metadata') } response(:body => {"servers" => servers}) end end diff --git a/lib/fog/rackspace/requests/compute_v2/update_server.rb b/lib/fog/rackspace/requests/compute_v2/update_server.rb index 0e0dda158..100ca0637 100644 --- a/lib/fog/rackspace/requests/compute_v2/update_server.rb +++ b/lib/fog/rackspace/requests/compute_v2/update_server.rb @@ -22,7 +22,7 @@ module Fog def update_server(server_id, name) server = self.data[:servers][server_id] server['name'] = name - server_response = Fog::Rackspace.keep(server, 'id', 'name', 'hostId', 'created', 'updated', 'status', 'progress', 'user_id', 'tenant_id', 'links', 'metadata', 'accessIPv4', 'accessIPv6', 'OS-DCF:diskConfig', 'rax-bandwidth:bandwidth', 'addresses', 'flavor', 'links', 'image') + server_response = Fog::Rackspace::MockData.keep(server, 'id', 'name', 'hostId', 'created', 'updated', 'status', 'progress', 'user_id', 'tenant_id', 'links', 'metadata', 'accessIPv4', 'accessIPv6', 'OS-DCF:diskConfig', 'rax-bandwidth:bandwidth', 'addresses', 'flavor', 'links', 'image') response( :status => 200, diff --git a/tests/rackspace/models/compute_v2/server_tests.rb b/tests/rackspace/models/compute_v2/server_tests.rb index 97b0c7056..d46709bf1 100644 --- a/tests/rackspace/models/compute_v2/server_tests.rb +++ b/tests/rackspace/models/compute_v2/server_tests.rb @@ -1,4 +1,5 @@ service = Fog::Compute::RackspaceV2.new +cbs_service = Fog::Rackspace::BlockStorage.new flavor_id = Fog.credentials[:rackspace_flavor_id] || service.flavors.first.id image_id = Fog.credentials[:rackspace_image_id] || service.images.first.id @@ -59,8 +60,8 @@ Shindo.tests('Fog::Compute::RackspaceV2 | server', ['rackspace']) do end tests('attachments') do - begin - @volume = cbs_service.volumes.create(:size => 100, :display_name => "fog-#{timestamp}") + begin + @volume = cbs_service.volumes.create(:size => 100, :display_name => "fog-#{Time.now.to_i.to_s}") @volume.wait_for(timeout=1500) { ready? } tests('#attach_volume').succeeds do @instance.attach_volume(@volume)