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

fixes for ec2 instance mocks

This commit is contained in:
Wesley Beary 2009-09-12 11:29:55 -07:00
parent f411163df4
commit a13e9d62d9
4 changed files with 79 additions and 31 deletions

View file

@ -70,6 +70,8 @@ else
if Time.now - instance['launchTime'] > 2
instance['instanceState'] = { :code => 16, :name => 'running' }
end
when 'rebooting'
instance['instanceState'] = { :code => 16, :name => 'running' }
when 'shutting-down'
if Time.now - Fog::AWS::EC2.data[:deleted_at][instance['instanceId']] > 2
instance['instanceState'] = { :code => 16, :name => 'terminating' }

View file

@ -1,4 +1,6 @@
module Fog
unless Fog.mocking?
module Fog
module AWS
class EC2
@ -21,4 +23,35 @@ module Fog
end
end
end
else
module Fog
module AWS
class EC2
def reboot_instances(instance_id = [])
response = Fog::Response.new
instance_id = [*instance_id]
if (Fog::AWS::EC2.data[:instances].keys & instance_id).length == instance_id.length
for instance_id in instance_id
Fog::AWS::EC2.data[:instances][instance_id]['status'] = 'rebooting'
end
response.status = 200
response.body = {
'requestId' => Fog::AWS::Mock.request_id,
'return' => true
}
else
response.status = 400
raise(Fog::Errors.status_error(200, 400, response))
end
response
end
end
end
end
end

View file

@ -39,17 +39,19 @@ else
class EC2
def terminate_instances(instance_id)
instance_id = [*instance_id]
response = Fog::Response.new
instance_id.each do |instance_id|
instance_id = [*instance_id]
if (Fog::AWS::EC2.data[:instances].keys & instance_id).length == instance_id.length
for instance_id in instance_id
response.body = {
'requestId' => Fog::AWS::Mock.request_id,
'instancesSet' => []
}
if instance = Fog::AWS::EC2.data[:instances][instance_id]
instance = Fog::AWS::EC2.data[:instances][instance_id]
Fog::AWS::EC2.data[:deleted_at][instance_id] = Time.now
instance['status'] = 'deleting'
response.status = 200
# TODO: the codes are mostly educated guessing, not certainty
code = case instance['state']
when 'pending'
0
@ -59,17 +61,19 @@ else
32
when 'terminated'
64
when 'rebooting'
128
end
response.body['instancesSet'] << {
'instanceId' => instance_id,
'previousState' => { 'name' => instance['state'], 'code' => code },
'shutdownState' => { 'name' => 'shutting-down', 'code' => 32}
}
end
else
response.status = 400
raise(Fog::Errors.status_error(200, 400, response))
end
end
response
end

View file

@ -8,14 +8,23 @@ describe 'EC2.reboot_instances' do
end
after(:each) do
ec2.terminate_instances([@instance_id])
ec2.terminate_instances(@instance_id)
end
it "should return proper attributes" do
actual = ec2.reboot_instances([@instance_id])
actual = ec2.reboot_instances(@instance_id)
actual.body['requestId'].should be_a(String)
[false, true].should include(actual.body['return'])
end
end
describe 'failure' do
it "should raise a BadRequest error if the instance does not exist" do
lambda {
ec2.reboot_instances('i-00000000')
}.should raise_error(Fog::Errors::BadRequest)
end
end
end