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 %} diff --git a/tests/test_ec2/test_instances.py b/tests/test_ec2/test_instances.py index 98740a8ae..150544b49 100644 --- a/tests/test_ec2/test_instances.py +++ b/tests/test_ec2/test_instances.py @@ -516,3 +516,29 @@ 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 + +@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') + 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')