EC2: Correct previous instance state (#5528)

This commit is contained in:
Laurie O 2022-10-04 20:36:39 +10:00 committed by GitHub
parent aefffd7eee
commit 8af786ed25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 18 deletions

View File

@ -334,6 +334,8 @@ class Instance(TaggedEC2Resource, BotoInstance, CloudFormationModel):
return self.id
def start(self):
previous_state = copy.copy(self._state)
for nic in self.nics.values():
nic.start()
@ -343,7 +345,11 @@ class Instance(TaggedEC2Resource, BotoInstance, CloudFormationModel):
self._reason = ""
self._state_reason = StateReason()
return previous_state
def stop(self):
previous_state = copy.copy(self._state)
for nic in self.nics.values():
nic.stop()
@ -358,6 +364,8 @@ class Instance(TaggedEC2Resource, BotoInstance, CloudFormationModel):
"Client.UserInitiatedShutdown",
)
return previous_state
def is_running(self):
return self._state.name == "running"
@ -365,6 +373,8 @@ class Instance(TaggedEC2Resource, BotoInstance, CloudFormationModel):
self.terminate()
def terminate(self):
previous_state = copy.copy(self._state)
for nic in self.nics.values():
nic.stop()
@ -413,6 +423,8 @@ class Instance(TaggedEC2Resource, BotoInstance, CloudFormationModel):
].id
)
return previous_state
def reboot(self):
self._state.name = "running"
self._state.code = 16
@ -709,16 +721,16 @@ class InstanceBackend:
def start_instances(self, instance_ids):
started_instances = []
for instance in self.get_multi_instances_by_id(instance_ids):
instance.start()
started_instances.append(instance)
previous_state = instance.start()
started_instances.append((instance, previous_state))
return started_instances
def stop_instances(self, instance_ids):
stopped_instances = []
for instance in self.get_multi_instances_by_id(instance_ids):
instance.stop()
stopped_instances.append(instance)
previous_state = instance.stop()
stopped_instances.append((instance, previous_state))
return stopped_instances
@ -731,8 +743,8 @@ class InstanceBackend:
for instance in self.get_multi_instances_by_id(instance_ids):
if instance.disable_api_termination == "true":
raise OperationNotPermitted4(instance.id)
instance.terminate()
terminated_instances.append(instance)
previous_state = instance.terminate()
terminated_instances.append((instance, previous_state))
return terminated_instances

View File

@ -721,12 +721,12 @@ EC2_TERMINATE_INSTANCES = """
<TerminateInstancesResponse xmlns="http://ec2.amazonaws.com/doc/2013-10-15/">
<requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
<instancesSet>
{% for instance in instances %}
{% for instance, previous_state in instances %}
<item>
<instanceId>{{ instance.id }}</instanceId>
<previousState>
<code>16</code>
<name>running</name>
<code>{{ previous_state.code }}</code>
<name>{{ previous_state.name }}</name>
</previousState>
<currentState>
<code>32</code>
@ -741,12 +741,12 @@ EC2_STOP_INSTANCES = """
<StopInstancesResponse xmlns="http://ec2.amazonaws.com/doc/2013-10-15/">
<requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
<instancesSet>
{% for instance in instances %}
{% for instance, previous_state in instances %}
<item>
<instanceId>{{ instance.id }}</instanceId>
<previousState>
<code>16</code>
<name>running</name>
<code>{{ previous_state.code }}</code>
<name>{{ previous_state.name }}</name>
</previousState>
<currentState>
<code>64</code>
@ -761,12 +761,12 @@ EC2_START_INSTANCES = """
<StartInstancesResponse xmlns="http://ec2.amazonaws.com/doc/2013-10-15/">
<requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
<instancesSet>
{% for instance in instances %}
{% for instance, previous_state in instances %}
<item>
<instanceId>{{ instance.id }}</instanceId>
<previousState>
<code>16</code>
<name>running</name>
<code>{{ previous_state.code }}</code>
<name>{{ previous_state.name }}</name>
</previousState>
<currentState>
<code>0</code>

View File

@ -88,7 +88,12 @@ def test_instance_launch_and_terminate():
"An error occurred (DryRunOperation) when calling the TerminateInstance operation: Request would have succeeded, but DryRun flag is set"
)
client.terminate_instances(InstanceIds=[instance_id])
response = client.terminate_instances(InstanceIds=[instance_id])
response["TerminatingInstances"].should.have.length_of(1)
instance = response["TerminatingInstances"][0]
instance["InstanceId"].should.equal(instance_id)
instance["PreviousState"].should.equal({"Code": 16, "Name": "running"})
instance["CurrentState"].should.equal({"Code": 32, "Name": "shutting-down"})
reservations = client.describe_instances(InstanceIds=[instance_id])["Reservations"]
instance = reservations[0]["Instances"][0]
@ -947,8 +952,7 @@ def test_instance_start_and_stop():
"StartingInstances"
]
started_instances[0]["CurrentState"].should.equal({"Code": 0, "Name": "pending"})
# TODO: The PreviousState is hardcoded to 'running' atm
# started_instances[0]["PreviousState"].should.equal({'Code': 80, 'Name': 'stopped'})
started_instances[0]["PreviousState"].should.equal({"Code": 80, "Name": "stopped"})
@mock_ec2