From a13e9d62d971974e7cfe73521c332d7adb862510 Mon Sep 17 00:00:00 2001 From: Wesley Beary Date: Sat, 12 Sep 2009 11:29:55 -0700 Subject: [PATCH] fixes for ec2 instance mocks --- .../aws/requests/ec2/describe_instances.rb | 2 + lib/fog/aws/requests/ec2/reboot_instances.rb | 71 ++++++++++++++----- .../aws/requests/ec2/terminate_instances.rb | 24 ++++--- .../aws/requests/ec2/reboot_instances_spec.rb | 13 +++- 4 files changed, 79 insertions(+), 31 deletions(-) diff --git a/lib/fog/aws/requests/ec2/describe_instances.rb b/lib/fog/aws/requests/ec2/describe_instances.rb index 99ffd656d..0feff8376 100644 --- a/lib/fog/aws/requests/ec2/describe_instances.rb +++ b/lib/fog/aws/requests/ec2/describe_instances.rb @@ -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' } diff --git a/lib/fog/aws/requests/ec2/reboot_instances.rb b/lib/fog/aws/requests/ec2/reboot_instances.rb index c962fd90c..39ae3cded 100644 --- a/lib/fog/aws/requests/ec2/reboot_instances.rb +++ b/lib/fog/aws/requests/ec2/reboot_instances.rb @@ -1,24 +1,57 @@ -module Fog - module AWS - class EC2 +unless Fog.mocking? + + 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 + +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 diff --git a/lib/fog/aws/requests/ec2/terminate_instances.rb b/lib/fog/aws/requests/ec2/terminate_instances.rb index 4411d6ef7..39d60ad96 100644 --- a/lib/fog/aws/requests/ec2/terminate_instances.rb +++ b/lib/fog/aws/requests/ec2/terminate_instances.rb @@ -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| - response.body = { - 'requestId' => Fog::AWS::Mock.request_id, - 'instancesSet' => [] - } - if instance = Fog::AWS::EC2.data[:instances][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' => [] + } + 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,16 +61,18 @@ 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} } - else - response.status = 400 - raise(Fog::Errors.status_error(200, 400, response)) end + else + response.status = 400 + raise(Fog::Errors.status_error(200, 400, response)) end response end diff --git a/spec/aws/requests/ec2/reboot_instances_spec.rb b/spec/aws/requests/ec2/reboot_instances_spec.rb index 530eba8f5..3e8bfb9d1 100644 --- a/spec/aws/requests/ec2/reboot_instances_spec.rb +++ b/spec/aws/requests/ec2/reboot_instances_spec.rb @@ -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