Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions coriolis/tests/integration/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,8 @@ def _execute_transfer_and_deployment(self, deployment_kwargs=None):
skip_os_morphing=False,
**deployment_kwargs,
)
self.addCleanup(self._cleanup_deployment, deployment.id)
self.addCleanup(
self._cleanup_deployment, deployment.id, deployment.instances)
self.assertDeploymentCompleted(deployment.id)

def _cleanup_execution(self, transfer_id, execution_id):
Expand Down Expand Up @@ -470,7 +471,7 @@ def assertExecutionErrored(self, execution_id, timeout=600):
% (execution_id, execution.status),
)

def _cleanup_deployment(self, deployment_id):
def _cleanup_deployment(self, deployment_id, instances=None):
"""Cancel a running deployment if needed, then delete it.

Cancels the deployment if it has not yet reached a terminal state,
Expand All @@ -483,22 +484,29 @@ def _cleanup_deployment(self, deployment_id):

Calls ``_imp_provider.delete_deployed_instance`` for every deployment
instance, so that finalized VMs at the destination are destroyed.

*instances* is a fallback instance name list, used when the test has
already deleted the deployment (e.g. to exercise the delete API)
before this cleanup runs; callers should pass the instance list
captured right after deployment creation so destination VMs still
get cleaned up even though the deployment record is gone.
"""
ctxt = self._get_db_context()
deployment = db_api.get_deployment(ctxt, deployment_id)
if deployment is None:
LOG.info(
"Deployment '%s' not found. Skip cleanup.", deployment_id)
return

instances = list(deployment.instances or [])

if deployment.last_execution_status in (
constants.ACTIVE_EXECUTION_STATUSES):
self._client.deployments.cancel(deployment_id)
self.wait_for_deployment(deployment_id, timeout=60)

self._client.deployments.delete(deployment_id)
"Deployment '%s' not found; using instances captured at "
"registration time for VM cleanup.", deployment_id)
instances = list(instances or [])
else:
instances = list(deployment.instances or instances or [])

if deployment.last_execution_status in (
constants.ACTIVE_EXECUTION_STATUSES):
self._client.deployments.cancel(deployment_id)
self.wait_for_deployment(deployment_id, timeout=60)

self._client.deployments.delete(deployment_id)

for instance_name in instances:
try:
Expand Down
3 changes: 2 additions & 1 deletion coriolis/tests/integration/deployments/test_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def _make_deployment(self, **kwargs):

deployment = self._client.deployments.create_from_transfer(
self._transfer.id, **kwargs)
self.addCleanup(self._cleanup_deployment, deployment.id)
self.addCleanup(
self._cleanup_deployment, deployment.id, deployment.instances)

return deployment

Expand Down
Loading