When manipulating instance save end states instead of transitional
When starting an instance it should eventually enter running state. At least in the normal case. So we report pending but save running, this way when client requests state of instance a second time, we reply with running. Similar thing for stop/terminate/reboot.
This commit is contained in:
parent
c09110c905
commit
b5a454e0da
@ -29,24 +29,24 @@ class Instance(BotoInstance):
|
||||
super(Instance, self).__init__()
|
||||
self.id = random_instance_id()
|
||||
self.image_id = image_id
|
||||
self._state = InstanceState()
|
||||
self._state = InstanceState("running", 16)
|
||||
self.user_data = user_data
|
||||
|
||||
def start(self):
|
||||
self._state.name = "pending"
|
||||
self._state.code = 0
|
||||
self._state.name = "running"
|
||||
self._state.code = 16
|
||||
|
||||
def stop(self):
|
||||
self._state.name = "stopping"
|
||||
self._state.code = 64
|
||||
self._state.name = "stopped"
|
||||
self._state.code = 80
|
||||
|
||||
def terminate(self):
|
||||
self._state.name = "shutting-down"
|
||||
self._state.code = 32
|
||||
self._state.name = "terminated"
|
||||
self._state.code = 48
|
||||
|
||||
def reboot(self):
|
||||
self._state.name = "pending"
|
||||
self._state.code = 0
|
||||
self._state.name = "running"
|
||||
self._state.code = 16
|
||||
|
||||
def get_tags(self):
|
||||
tags = ec2_backend.describe_tags(self.id)
|
||||
|
@ -7,7 +7,10 @@ from moto.ec2.utils import instance_ids_from_querystring
|
||||
class AmisResponse(object):
|
||||
def create_image(self):
|
||||
name = self.querystring.get('Name')[0]
|
||||
description = self.querystring.get('Description')[0]
|
||||
if "Description" in self.querystring:
|
||||
description = self.querystring.get('Description')[0]
|
||||
else:
|
||||
description = ""
|
||||
instance_ids = instance_ids_from_querystring(self.querystring)
|
||||
instance_id = instance_ids[0]
|
||||
image = ec2_backend.create_image(instance_id, name, description)
|
||||
|
@ -95,8 +95,8 @@ EC2_RUN_INSTANCES = """<RunInstancesResponse xmlns="http://ec2.amazonaws.com/doc
|
||||
<instanceId>{{ instance.id }}</instanceId>
|
||||
<imageId>{{ instance.image_id }}</imageId>
|
||||
<instanceState>
|
||||
<code>{{ instance._state.code }}</code>
|
||||
<name>{{ instance._state.name }}</name>
|
||||
<code>0</code>
|
||||
<name>pending</name>
|
||||
</instanceState>
|
||||
<privateDnsName/>
|
||||
<dnsName/>
|
||||
@ -150,8 +150,8 @@ EC2_DESCRIBE_INSTANCES = """<DescribeInstancesResponse xmlns='http://ec2.amazona
|
||||
<code>{{ instance._state.code }}</code>
|
||||
<name>{{ instance._state.name }}</name>
|
||||
</instanceState>
|
||||
<privateDnsName/>
|
||||
<dnsName/>
|
||||
<privateDnsName>ip-10.0.0.12.ec2.internal</privateDnsName>
|
||||
<dnsName>ec2-46.51.219.63.compute-1.amazonaws.com</dnsName>
|
||||
<reason/>
|
||||
<keyName>gsg-keypair</keyName>
|
||||
<amiLaunchIndex>0</amiLaunchIndex>
|
||||
@ -216,8 +216,8 @@ EC2_TERMINATE_INSTANCES = """
|
||||
<name>running</name>
|
||||
</previousState>
|
||||
<currentState>
|
||||
<code>{{ instance._state.code }}</code>
|
||||
<name>{{ instance._state.name }}</name>
|
||||
<code>32</code>
|
||||
<name>shutting-down</name>
|
||||
</currentState>
|
||||
</item>
|
||||
{% endfor %}
|
||||
@ -236,8 +236,8 @@ EC2_STOP_INSTANCES = """
|
||||
<name>running</name>
|
||||
</previousState>
|
||||
<currentState>
|
||||
<code>{{ instance._state.code }}</code>
|
||||
<name>{{ instance._state.name }}</name>
|
||||
<code>64</code>
|
||||
<name>stopping</name>
|
||||
</currentState>
|
||||
</item>
|
||||
{% endfor %}
|
||||
@ -256,8 +256,8 @@ EC2_START_INSTANCES = """
|
||||
<name>running</name>
|
||||
</previousState>
|
||||
<currentState>
|
||||
<code>{{ instance._state.code }}</code>
|
||||
<name>{{ instance._state.name }}</name>
|
||||
<code>0</code>
|
||||
<name>pending</name>
|
||||
</currentState>
|
||||
</item>
|
||||
{% endfor %}
|
||||
|
@ -35,6 +35,7 @@ def test_instance_launch_and_terminate():
|
||||
reservation.should.be.a(Reservation)
|
||||
reservation.instances.should.have.length_of(1)
|
||||
instance = reservation.instances[0]
|
||||
instance.state.should.equal('pending')
|
||||
|
||||
reservations = conn.get_all_instances()
|
||||
reservations.should.have.length_of(1)
|
||||
@ -42,13 +43,13 @@ def test_instance_launch_and_terminate():
|
||||
instances = reservations[0].instances
|
||||
instances.should.have.length_of(1)
|
||||
instances[0].id.should.equal(instance.id)
|
||||
instances[0].state.should.equal('pending')
|
||||
instances[0].state.should.equal('running')
|
||||
|
||||
conn.terminate_instances([instances[0].id])
|
||||
|
||||
reservations = conn.get_all_instances()
|
||||
instance = reservations[0].instances[0]
|
||||
instance.state.should.equal('shutting-down')
|
||||
instance.state.should.equal('terminated')
|
||||
|
||||
|
||||
@mock_ec2
|
||||
@ -85,18 +86,18 @@ def test_get_instances_filtering_by_state():
|
||||
|
||||
conn.terminate_instances([instance1.id])
|
||||
|
||||
reservations = conn.get_all_instances(filters={'instance-state-name': 'pending'})
|
||||
reservations = conn.get_all_instances(filters={'instance-state-name': 'running'})
|
||||
reservations.should.have.length_of(1)
|
||||
# Since we terminated instance1, only instance2 and instance3 should be returned
|
||||
instance_ids = [instance.id for instance in reservations[0].instances]
|
||||
set(instance_ids).should.equal(set([instance2.id, instance3.id]))
|
||||
|
||||
reservations = conn.get_all_instances([instance2.id], filters={'instance-state-name': 'pending'})
|
||||
reservations = conn.get_all_instances([instance2.id], filters={'instance-state-name': 'running'})
|
||||
reservations.should.have.length_of(1)
|
||||
instance_ids = [instance.id for instance in reservations[0].instances]
|
||||
instance_ids.should.equal([instance2.id])
|
||||
|
||||
reservations = conn.get_all_instances([instance2.id], filters={'instance-state-name': 'terminating'})
|
||||
reservations = conn.get_all_instances([instance2.id], filters={'instance-state-name': 'terminated'})
|
||||
list(reservations).should.equal([])
|
||||
|
||||
# get_all_instances should still return all 3
|
||||
|
Loading…
x
Reference in New Issue
Block a user