Skip to content

Commit

Permalink
Merge pull request #86 from nomadium/add-mock-for-request-spot-instances
Browse files Browse the repository at this point in the history
Add mock for EC2 request_spot_instances API request
  • Loading branch information
geemus committed Apr 14, 2015
2 parents 136b593 + 07acbcb commit e1b354c
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/fog/aws/mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ def self.rds_address(db_name,region)
"#{db_name}.#{Fog::Mock.random_letters(rand(12) + 4)}.#{region}.rds.amazonaws.com"
end

def self.spot_instance_request_id
"sir-#{Fog::Mock.random_letters_and_numbers(8)}"
end

def self.spot_product_descriptions
[
'Linux/UNIX',
Expand Down
80 changes: 80 additions & 0 deletions lib/fog/aws/requests/compute/request_spot_instances.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,86 @@ def request_spot_instances(image_id, instance_type, spot_price, options = {})
}.merge!(options))
end
end

class Mock
def request_spot_instances(image_id, instance_type, spot_price, options = {})
response = Excon::Response.new

if (image_id && instance_type && spot_price)
response.status = 200

all_instance_types = flavors.map { |f| f.id }
if !all_instance_types.include?(instance_type)
message = "InvalidParameterValue => Invalid value '#{instance_type}' for InstanceType."
raise Fog::Compute::AWS::Error.new(message)
end

spot_price = spot_price.to_f
if !(spot_price > 0)
message = "InvalidParameterValue => Value (#{spot_price}) for parameter price is invalid."
message << " \"#{spot_price}\" is an invalid spot instance price"
raise Fog::Compute::AWS::Error.new(message)
end

if !image_id.match(/^ami-[a-f0-9]{8}$/)
message = "The image id '[#{image_id}]' does not exist"
raise Fog::Compute::AWS::NotFound.new(message)
end

else
message = 'MissingParameter => '
message << 'The request must contain the parameter '
if !image_id
message << 'image_id'
elsif !instance_type
message << 'instance_type'
else
message << 'spot_price'
end
raise Fog::Compute::AWS::Error.new(message)
end

for key in %w(AvailabilityZoneGroup LaunchGroup)
if options.is_a?(Hash) && options.key?(key)
Fog::Logger.warning("#{key} filters are not yet mocked [light_black](#{caller.first})[/]")
Fog::Mock.not_implemented
end
end

launch_spec = {
'iamInstanceProfile' => {},
'blockDeviceMapping' => [],
'groupSet' => [Fog::AWS::Mock.security_group_id],
'imageId' => image_id,
'instanceType' => instance_type,
'monitoring' => options['MonitoringEnabled'] || false,
'subnetId' => nil,
'ebsOptimized' => false,
'keyName' => options['KeyName'] || nil
}

response.body = {
'spotInstanceRequestSet' => [
{
'launchSpecification' => launch_spec,
'spotInstanceRequestId' => Fog::AWS::Mock.spot_instance_request_id,
'spotPrice' => spot_price,
'type' => options['Type'] || 'one-time',
'state' => 'open',
'fault' => {
'code' => 'pending-evaluation',
'message' => 'Your Spot request has been submitted for review, and is pending evaluation.'
},
'createTime' => Time.now,
'productDescription' => 'Linux/UNIX'
}
],
'requestId' => Fog::AWS::Mock.request_id
}

response
end
end
end
end
end
4 changes: 2 additions & 2 deletions tests/requests/compute/spot_instance_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@

tests('success') do

pending if Fog.mocking?

tests("#request_spot_instances('ami-3202f25b', 't1.micro', '0.001')").formats(@spot_instance_requests_format) do
data = Fog::Compute[:aws].request_spot_instances('ami-3202f25b', 't1.micro', '0.001',{'LaunchSpecification.EbsOptimized' => false}).body
@spot_instance_request_id = data['spotInstanceRequestSet'].first['spotInstanceRequestId']
data
end

pending if Fog.mocking?

tests("#describe_spot_instance_requests").formats(@spot_instance_requests_format) do
Fog::Compute[:aws].describe_spot_instance_requests('spot-instance-request-id' => [@spot_instance_request_id]).body
end
Expand Down

0 comments on commit e1b354c

Please sign in to comment.