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:
Ilya Sukhanov 2013-08-28 10:19:12 -04:00
parent c09110c905
commit b5a454e0da
4 changed files with 29 additions and 25 deletions

View File

@ -29,24 +29,24 @@ class Instance(BotoInstance):
super(Instance, self).__init__() super(Instance, self).__init__()
self.id = random_instance_id() self.id = random_instance_id()
self.image_id = image_id self.image_id = image_id
self._state = InstanceState() self._state = InstanceState("running", 16)
self.user_data = user_data self.user_data = user_data
def start(self): def start(self):
self._state.name = "pending" self._state.name = "running"
self._state.code = 0 self._state.code = 16
def stop(self): def stop(self):
self._state.name = "stopping" self._state.name = "stopped"
self._state.code = 64 self._state.code = 80
def terminate(self): def terminate(self):
self._state.name = "shutting-down" self._state.name = "terminated"
self._state.code = 32 self._state.code = 48
def reboot(self): def reboot(self):
self._state.name = "pending" self._state.name = "running"
self._state.code = 0 self._state.code = 16
def get_tags(self): def get_tags(self):
tags = ec2_backend.describe_tags(self.id) tags = ec2_backend.describe_tags(self.id)

View File

@ -7,7 +7,10 @@ from moto.ec2.utils import instance_ids_from_querystring
class AmisResponse(object): class AmisResponse(object):
def create_image(self): def create_image(self):
name = self.querystring.get('Name')[0] 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_ids = instance_ids_from_querystring(self.querystring)
instance_id = instance_ids[0] instance_id = instance_ids[0]
image = ec2_backend.create_image(instance_id, name, description) image = ec2_backend.create_image(instance_id, name, description)

View File

@ -95,8 +95,8 @@ EC2_RUN_INSTANCES = """<RunInstancesResponse xmlns="http://ec2.amazonaws.com/doc
<instanceId>{{ instance.id }}</instanceId> <instanceId>{{ instance.id }}</instanceId>
<imageId>{{ instance.image_id }}</imageId> <imageId>{{ instance.image_id }}</imageId>
<instanceState> <instanceState>
<code>{{ instance._state.code }}</code> <code>0</code>
<name>{{ instance._state.name }}</name> <name>pending</name>
</instanceState> </instanceState>
<privateDnsName/> <privateDnsName/>
<dnsName/> <dnsName/>
@ -150,8 +150,8 @@ EC2_DESCRIBE_INSTANCES = """<DescribeInstancesResponse xmlns='http://ec2.amazona
<code>{{ instance._state.code }}</code> <code>{{ instance._state.code }}</code>
<name>{{ instance._state.name }}</name> <name>{{ instance._state.name }}</name>
</instanceState> </instanceState>
<privateDnsName/> <privateDnsName>ip-10.0.0.12.ec2.internal</privateDnsName>
<dnsName/> <dnsName>ec2-46.51.219.63.compute-1.amazonaws.com</dnsName>
<reason/> <reason/>
<keyName>gsg-keypair</keyName> <keyName>gsg-keypair</keyName>
<amiLaunchIndex>0</amiLaunchIndex> <amiLaunchIndex>0</amiLaunchIndex>
@ -216,8 +216,8 @@ EC2_TERMINATE_INSTANCES = """
<name>running</name> <name>running</name>
</previousState> </previousState>
<currentState> <currentState>
<code>{{ instance._state.code }}</code> <code>32</code>
<name>{{ instance._state.name }}</name> <name>shutting-down</name>
</currentState> </currentState>
</item> </item>
{% endfor %} {% endfor %}
@ -236,8 +236,8 @@ EC2_STOP_INSTANCES = """
<name>running</name> <name>running</name>
</previousState> </previousState>
<currentState> <currentState>
<code>{{ instance._state.code }}</code> <code>64</code>
<name>{{ instance._state.name }}</name> <name>stopping</name>
</currentState> </currentState>
</item> </item>
{% endfor %} {% endfor %}
@ -256,8 +256,8 @@ EC2_START_INSTANCES = """
<name>running</name> <name>running</name>
</previousState> </previousState>
<currentState> <currentState>
<code>{{ instance._state.code }}</code> <code>0</code>
<name>{{ instance._state.name }}</name> <name>pending</name>
</currentState> </currentState>
</item> </item>
{% endfor %} {% endfor %}

View File

@ -35,6 +35,7 @@ def test_instance_launch_and_terminate():
reservation.should.be.a(Reservation) reservation.should.be.a(Reservation)
reservation.instances.should.have.length_of(1) reservation.instances.should.have.length_of(1)
instance = reservation.instances[0] instance = reservation.instances[0]
instance.state.should.equal('pending')
reservations = conn.get_all_instances() reservations = conn.get_all_instances()
reservations.should.have.length_of(1) reservations.should.have.length_of(1)
@ -42,13 +43,13 @@ def test_instance_launch_and_terminate():
instances = reservations[0].instances instances = reservations[0].instances
instances.should.have.length_of(1) instances.should.have.length_of(1)
instances[0].id.should.equal(instance.id) 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]) conn.terminate_instances([instances[0].id])
reservations = conn.get_all_instances() reservations = conn.get_all_instances()
instance = reservations[0].instances[0] instance = reservations[0].instances[0]
instance.state.should.equal('shutting-down') instance.state.should.equal('terminated')
@mock_ec2 @mock_ec2
@ -85,18 +86,18 @@ def test_get_instances_filtering_by_state():
conn.terminate_instances([instance1.id]) 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) reservations.should.have.length_of(1)
# Since we terminated instance1, only instance2 and instance3 should be returned # Since we terminated instance1, only instance2 and instance3 should be returned
instance_ids = [instance.id for instance in reservations[0].instances] instance_ids = [instance.id for instance in reservations[0].instances]
set(instance_ids).should.equal(set([instance2.id, instance3.id])) 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) reservations.should.have.length_of(1)
instance_ids = [instance.id for instance in reservations[0].instances] instance_ids = [instance.id for instance in reservations[0].instances]
instance_ids.should.equal([instance2.id]) 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([]) list(reservations).should.equal([])
# get_all_instances should still return all 3 # get_all_instances should still return all 3