From 31b55977c9faa901df5a6993e1dc019fefbdd0af Mon Sep 17 00:00:00 2001 From: Arthur Wang Date: Fri, 24 Oct 2014 19:44:29 +0000 Subject: [PATCH 1/3] EC2: Add support for the IncludeAllInstances option in DescribeInstanceStatus --- moto/ec2/models.py | 8 +++++ moto/ec2/responses/instances.py | 57 ++++++++++++++++++++------------- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/moto/ec2/models.py b/moto/ec2/models.py index 91b98cca6..63f0a3123 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -542,6 +542,14 @@ class InstanceBackend(object): instances.append(instance) return instances + def all_running_instances(self): + instances = [] + for reservation in self.all_reservations(): + for instance in reservation.instances: + if instance.state_code == 16: + instances.append(instance) + return instances + def get_multi_instances_by_id(self, instance_ids): """ :param instance_ids: A string list with instance ids diff --git a/moto/ec2/responses/instances.py b/moto/ec2/responses/instances.py index 49050f9d6..b1ccc7e12 100644 --- a/moto/ec2/responses/instances.py +++ b/moto/ec2/responses/instances.py @@ -4,7 +4,7 @@ from jinja2 import Template from moto.core.responses import BaseResponse from moto.core.utils import camelcase_to_underscores from moto.ec2.utils import instance_ids_from_querystring, filters_from_querystring, filter_reservations, \ - dict_from_querystring + dict_from_querystring, optional_from_querystring class InstanceResponse(BaseResponse): @@ -69,11 +69,15 @@ class InstanceResponse(BaseResponse): def describe_instance_status(self): instance_ids = instance_ids_from_querystring(self.querystring) + include_all_instances = optional_from_querystring('IncludeAllInstances', + self.querystring) == 'true' if instance_ids: instances = self.ec2_backend.get_multi_instances_by_id(instance_ids) - else: + elif include_all_instances: instances = self.ec2_backend.all_instances() + else: + instances = self.ec2_backend.all_running_instances() template = Template(EC2_INSTANCE_STATUS) return template.render(instances=instances) @@ -512,27 +516,36 @@ EC2_INSTANCE_STATUS = """ {{ instance.id }} us-east-1d - 16 - running + {{ instance.state_code }} + {{ instance.state }} - - ok -
- - reachability - passed - -
-
- - ok -
- - reachability - passed - -
-
+ {% if instance.state_code == 16 %} + + ok +
+ + reachability + passed + +
+
+ + ok +
+ + reachability + passed + +
+
+ {% else %} + + not-applicable + + + not-applicable + + {% endif %} {% endfor %} From d67c5b80a3a45ea50ff95e443058043658117b3d Mon Sep 17 00:00:00 2001 From: Arthur Wang Date: Fri, 24 Oct 2014 16:18:52 -0400 Subject: [PATCH 2/3] Add describe instance status test for new option --- tests/test_ec2/test_instances.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/test_ec2/test_instances.py b/tests/test_ec2/test_instances.py index 98740a8ae..9e83d5eff 100644 --- a/tests/test_ec2/test_instances.py +++ b/tests/test_ec2/test_instances.py @@ -516,3 +516,28 @@ def test_describe_instance_status_with_instance_filter(): cm.exception.code.should.equal('InvalidInstanceID.NotFound') cm.exception.status.should.equal(400) cm.exception.request_id.should_not.be.none + +@mock_ec2 +def test_describe_instance_status_with_non_running_instances(): + conn = boto.connect_ec2('the_key', 'the_secret') + reservation = conn.run_instances('ami-1234abcd', min_count=3) + instance1, instance2, instance3 = reservation.instances + instance1.stop() + instance2.terminate() + + all_running_status = conn.get_all_instance_status() + all_running_status.should.have.length_of(1) + all_running_status[0].id.should.equal(instance3.id) + all_running_status[0].state_name.should.equal('running') + + all_status = conn.get_all_instance_status(include_all_instances=True) + all_status.should.have.length_of(3) + + status1 = next((s for s in all_status if s.id == instance1.id), None) + status1.state_name.should.equal('stopped') + + status2 = next((s for s in all_status if s.id == instance2.id), None) + status2.state_name.should.equal('terminated') + + status3 = next((s for s in all_status if s.id == instance3.id), None) + status3.state_name.should.equal('running') From 3bafebee049c4626015d51230a615e4dcfef8cf3 Mon Sep 17 00:00:00 2001 From: Arthur Wang Date: Mon, 27 Oct 2014 14:48:17 +0000 Subject: [PATCH 3/3] Limit test for describe instance status filter by boto version --- tests/test_ec2/test_instances.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_ec2/test_instances.py b/tests/test_ec2/test_instances.py index 9e83d5eff..150544b49 100644 --- a/tests/test_ec2/test_instances.py +++ b/tests/test_ec2/test_instances.py @@ -517,6 +517,7 @@ def test_describe_instance_status_with_instance_filter(): cm.exception.status.should.equal(400) cm.exception.request_id.should_not.be.none +@requires_boto_gte("2.32.0") @mock_ec2 def test_describe_instance_status_with_non_running_instances(): conn = boto.connect_ec2('the_key', 'the_secret')