From 14f242f640cd4f94780793c540708ca9bf18b71c Mon Sep 17 00:00:00 2001 From: Dylan Egan Date: Wed, 13 Jul 2011 10:48:53 +1000 Subject: [PATCH] Begin work on mocking reserved instances. Provide describe_reserved_instances_offerings for mocking and real. REAL TALK --- .../describe_reserved_instances_offerings.rb | 4 +- .../describe_reserved_instances_offerings.rb | 23 ++++++++ lib/fog/providers/aws.rb | 4 ++ tests/compute/requests/aws/instance_tests.rb | 54 +++++++++++++++++++ 4 files changed, 83 insertions(+), 2 deletions(-) diff --git a/lib/fog/compute/parsers/aws/describe_reserved_instances_offerings.rb b/lib/fog/compute/parsers/aws/describe_reserved_instances_offerings.rb index 32b2132d6..8e7ea7023 100644 --- a/lib/fog/compute/parsers/aws/describe_reserved_instances_offerings.rb +++ b/lib/fog/compute/parsers/aws/describe_reserved_instances_offerings.rb @@ -12,7 +12,7 @@ module Fog def end_element(name) case name - when 'availabilityZone', 'instanceType', 'productDescription', 'reservedInstancesOfferingId' + when 'availabilityZone', 'currencyCode', 'instanceType', 'instanceTenancy', 'productDescription', 'reservedInstancesOfferingId' @reserved_instances_offering[name] = value when 'duration' @reserved_instances_offering[name] = value.to_i @@ -31,4 +31,4 @@ module Fog end end end -end \ No newline at end of file +end diff --git a/lib/fog/compute/requests/aws/describe_reserved_instances_offerings.rb b/lib/fog/compute/requests/aws/describe_reserved_instances_offerings.rb index d4cadeecf..387dfccfb 100644 --- a/lib/fog/compute/requests/aws/describe_reserved_instances_offerings.rb +++ b/lib/fog/compute/requests/aws/describe_reserved_instances_offerings.rb @@ -32,7 +32,30 @@ module Fog :parser => Fog::Parsers::Compute::AWS::DescribeReservedInstancesOfferings.new }.merge!(params)) end + end + class Mock + def describe_reserved_instances_offerings(filters = {}) + response = Excon::Response.new + response.status = 200 + + response.body = { + 'reservedInstancesOfferingsSet' => [{ + 'reservedInstancesOfferingId' => Fog::AWS::Mock.reserved_instances_offering_id, + 'instanceType' => 'm1.small', + 'availabilityZone' => 'us-east-1d', + 'duration' => 31536000, + 'fixedPrice' => 350.0, + 'usagePrice' => 0.03, + 'productDescription' => 'Linux/UNIX', + 'instanceTenancy' => 'default', + 'currencyCode' => 'USD' + }], + 'requestId' => Fog::AWS::Mock.request_id + } + + response + end end end end diff --git a/lib/fog/providers/aws.rb b/lib/fog/providers/aws.rb index 9138da2eb..bd62ab03d 100644 --- a/lib/fog/providers/aws.rb +++ b/lib/fog/providers/aws.rb @@ -178,6 +178,10 @@ module Fog "r-#{Fog::Mock.random_hex(8)}" end + def self.reserved_instances_offering_id + request_id + end + def self.snapshot_id "snap-#{Fog::Mock.random_hex(8)}" end diff --git a/tests/compute/requests/aws/instance_tests.rb b/tests/compute/requests/aws/instance_tests.rb index 8e0e5d2c1..5cd441d44 100644 --- a/tests/compute/requests/aws/instance_tests.rb +++ b/tests/compute/requests/aws/instance_tests.rb @@ -79,6 +79,43 @@ Shindo.tests('Fog::Compute[:aws] | instance requests', ['aws']) do 'requestId' => String } + @describe_reserved_instances_offerings_format = { + 'reservedInstancesOfferingsSet' => [{ + 'reservedInstancesOfferingId' => String, + 'instanceType' => String, + 'availabilityZone' => String, + 'duration' => Integer, + 'fixedPrice' => Float, + 'usagePrice' => Float, + 'productDescription' => String, + 'instanceTenancy' => String, + 'currencyCode' => String + }], + 'requestId' => String + } + + @describe_reserved_instances_format = { + 'reservedInstancesSet' => [{ + 'reservedInstancesId' => String, + 'instanceType' => String, + 'availabilityZone' => String, + 'start' => Time, + 'duration' => Integer, + 'fixedPrice' => Float, + 'usagePrice' => Float, + 'instanceCount' => Integer, + 'productDescription' => String, + 'state' => String, + 'tagSet' => [{ + 'key' => String, + 'value' => String + }], + 'instanceTenancy' => String, + 'currencyCode' => String + }], + 'requestId' => String + } + tests('success') do @instance_id = nil @@ -151,6 +188,23 @@ Shindo.tests('Fog::Compute[:aws] | instance requests', ['aws']) do Fog::Compute[:aws].terminate_instances(@instance_id).body end + tests("#describe_reserved_instances_offerings").formats(@describe_reserved_instances_offerings_format) do + @reserved_instances = Fog::Compute[:aws].describe_reserved_instances_offerings.body + @reserved_instances + end + + if Fog.mocking? + pending + + @reserved_instance_offering_id = @reserved_instances["reservedInstancesOfferingSet"].first["reservedInstancesOfferingId"] + tests("#purchase_reserved_instances_offering('#{@reserved_instance_offering_id}')").formats(@purchase_reserved_instances_offerings_format) do + Fog::Compute[:aws].purchase_reserved_instances_offering(@reserved_instance_offering_id, 1).body + end + + tests("#describe_reserved_instances").formats(@describe_reserved_instances_format) do + Fog::Compute[:aws].describe_reserved_instances.body + end + end end tests('failure') do