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:
parent
f411163df4
commit
a13e9d62d9
4 changed files with 79 additions and 31 deletions
|
@ -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' }
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
unless Fog.mocking?
|
||||
|
||||
module Fog
|
||||
module AWS
|
||||
class EC2
|
||||
|
@ -22,3 +24,34 @@ 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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue