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 if Time.now - instance['launchTime'] > 2
instance['instanceState'] = { :code => 16, :name => 'running' } instance['instanceState'] = { :code => 16, :name => 'running' }
end end
when 'rebooting'
instance['instanceState'] = { :code => 16, :name => 'running' }
when 'shutting-down' when 'shutting-down'
if Time.now - Fog::AWS::EC2.data[:deleted_at][instance['instanceId']] > 2 if Time.now - Fog::AWS::EC2.data[:deleted_at][instance['instanceId']] > 2
instance['instanceState'] = { :code => 16, :name => 'terminating' } instance['instanceState'] = { :code => 16, :name => 'terminating' }

View file

@ -1,24 +1,57 @@
module Fog unless Fog.mocking?
module AWS
class EC2 module Fog
module AWS
class EC2
# Reboot specified instances
#
# ==== Parameters
# * instance_id<~Array> - Ids of instances to reboot
#
# ==== Returns
# # * response<~Fog::AWS::Response>:
# * body<~Hash>:
# * 'requestId'<~String> - Id of request
# * 'return'<~Boolean> - success?
def reboot_instances(instance_id = [])
params = indexed_params('InstanceId', instance_id)
request({
'Action' => 'RebootInstances'
}.merge!(params), Fog::Parsers::AWS::EC2::Basic.new)
end
# Reboot specified instances
#
# ==== Parameters
# * instance_id<~Array> - Ids of instances to reboot
#
# ==== Returns
# # * response<~Fog::AWS::Response>:
# * body<~Hash>:
# * 'requestId'<~String> - Id of request
# * 'return'<~Boolean> - success?
def reboot_instances(instance_id = [])
params = indexed_params('InstanceId', instance_id)
request({
'Action' => 'RebootInstances'
}.merge!(params), Fog::Parsers::AWS::EC2::Basic.new)
end end
end 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 end

View file

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

View file

@ -8,14 +8,23 @@ describe 'EC2.reboot_instances' do
end end
after(:each) do after(:each) do
ec2.terminate_instances([@instance_id]) ec2.terminate_instances(@instance_id)
end end
it "should return proper attributes" do 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) actual.body['requestId'].should be_a(String)
[false, true].should include(actual.body['return']) [false, true].should include(actual.body['return'])
end end
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 end