summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Falcon <TheRealFalcon@users.noreply.github.com>2020-11-30 10:20:15 -0600
committerGitHub <noreply@github.com>2020-11-30 11:20:15 -0500
commit2bd34bda9543c1b66f53eaf26706f0de887db187 (patch)
treef22548313ac2d9c81a38502ed9bf855cebfbc29c
parentde3183c1ff4660dda23f2624c1cc24bb76de5bf5 (diff)
downloadvyos-cloud-init-2bd34bda9543c1b66f53eaf26706f0de887db187.tar.gz
vyos-cloud-init-2bd34bda9543c1b66f53eaf26706f0de887db187.zip
Delete image snapshots created for integration tests (#682)
Integration tests have been leaving behind snapshot images, so now we clean them up. Also, in testing, found that in Azure, deleting a resource group will automatically delete the instance, so if KEEP_INSTANCE is True, we no longer delete the resource group. Co-authored-by: Daniel Watkins <oddbloke@ubuntu.com>
-rw-r--r--tests/integration_tests/clouds.py32
-rw-r--r--tests/integration_tests/conftest.py5
-rw-r--r--tests/integration_tests/instances.py6
3 files changed, 34 insertions, 9 deletions
diff --git a/tests/integration_tests/clouds.py b/tests/integration_tests/clouds.py
index 88ac4408..4d5c2c2a 100644
--- a/tests/integration_tests/clouds.py
+++ b/tests/integration_tests/clouds.py
@@ -32,7 +32,14 @@ class IntegrationCloud(ABC):
def __init__(self, settings=integration_settings):
self.settings = settings
self.cloud_instance = self._get_cloud_instance()
- self.image_id = self._get_initial_image()
+ self._released_image_id = self._get_initial_image()
+ self.snapshot_id = None
+
+ @property
+ def image_id(self):
+ if self.snapshot_id:
+ return self.snapshot_id
+ return self._released_image_id
def emit_settings_to_log(self) -> None:
log.info(
@@ -50,13 +57,13 @@ class IntegrationCloud(ABC):
raise NotImplementedError
def _get_initial_image(self):
- image_id = self.settings.OS_IMAGE
+ _released_image_id = self.settings.OS_IMAGE
try:
- image_id = self.cloud_instance.released_image(
+ _released_image_id = self.cloud_instance.released_image(
self.settings.OS_IMAGE)
except (ValueError, IndexError):
pass
- return image_id
+ return _released_image_id
def _perform_launch(self, launch_kwargs):
pycloudlib_instance = self.cloud_instance.launch(**launch_kwargs)
@@ -100,6 +107,14 @@ class IntegrationCloud(ABC):
def snapshot(self, instance):
return self.cloud_instance.snapshot(instance, clean=True)
+ def delete_snapshot(self):
+ if self.snapshot_id:
+ log.info(
+ 'Deleting snapshot image created for this testrun: %s',
+ self.snapshot_id
+ )
+ self.cloud_instance.delete_image(self.snapshot_id)
+
class Ec2Cloud(IntegrationCloud):
datasource = 'ec2'
@@ -130,7 +145,14 @@ class AzureCloud(IntegrationCloud):
return Azure(tag='azure-integration-test')
def destroy(self):
- self.cloud_instance.delete_resource_group()
+ if self.settings.KEEP_INSTANCE:
+ log.info(
+ 'NOT deleting resource group because KEEP_INSTANCE is true '
+ 'and deleting resource group would also delete instance. '
+ 'Instance and resource group must both be manually deleted.'
+ )
+ else:
+ self.cloud_instance.delete_resource_group()
class OciCloud(IntegrationCloud):
diff --git a/tests/integration_tests/conftest.py b/tests/integration_tests/conftest.py
index 6e1465be..d7e0fca2 100644
--- a/tests/integration_tests/conftest.py
+++ b/tests/integration_tests/conftest.py
@@ -81,7 +81,10 @@ def session_cloud():
cloud = platforms[integration_settings.PLATFORM]()
cloud.emit_settings_to_log()
yield cloud
- cloud.destroy()
+ try:
+ cloud.delete_snapshot()
+ finally:
+ cloud.destroy()
@pytest.fixture(scope='session', autouse=True)
diff --git a/tests/integration_tests/instances.py b/tests/integration_tests/instances.py
index 033847b8..8f6573cd 100644
--- a/tests/integration_tests/instances.py
+++ b/tests/integration_tests/instances.py
@@ -86,9 +86,9 @@ class IntegrationInstance:
version = self.execute('cloud-init -v').split()[-1]
log.info('Installed cloud-init version: %s', version)
self.instance.clean()
- image_id = self.snapshot()
- log.info('Created new image: %s', image_id)
- self.cloud.image_id = image_id
+ snapshot_id = self.snapshot()
+ log.info('Created new image: %s', snapshot_id)
+ self.cloud.snapshot_id = snapshot_id
def install_proposed_image(self):
log.info('Installing proposed image')