Clean up azure instances properly

This commit is contained in:
Tyler Johnson 2019-08-23 10:48:20 -06:00
parent 993c3415a5
commit b45ae745ad
No known key found for this signature in database
GPG key ID: 691E31397E27D004

View file

@ -204,16 +204,18 @@ class CloudTest(ShellCase):
.format(', '.join(missing_conf_item)) +
'\nCheck tests/integration/files/conf/cloud.providers.d/{0}.conf'.format(self.PROVIDER))
def _alt_name(self):
def _alt_names(self):
'''
Check for an instance that was renamed to be longer
Check for an instances created alongside this test's instance that weren't cleaned up
'''
query = self.query_instances()
instances = set()
for q in query:
# Verify but this is a new name and not a shutting down ec2 instance
if q.startswith(self.instance_name) and not (q == self.instance_name) \
and not q.split('-')[-1].startswith('DEL'):
return q
if q.startswith(self.instance_name) and not q.split('-')[-1].startswith('DEL'):
instances.add(q)
log.debug('Adding "{}" to the set of instances that needs to be deleted'.format(q))
return instances
def _ensure_deletion(self, instance_name=None):
'''
@ -251,13 +253,17 @@ class CloudTest(ShellCase):
one time in a test for each instance created. This is a failSafe and something went wrong
if the tearDown is where an instance is destroyed.
'''
instance_destroyed, destroy_message = self._ensure_deletion()
alt_name = self._alt_name()
if alt_name:
alt_destroyed, alt_destroy_message = self._ensure_deletion(alt_name)
self.assertTrue(instance_destroyed and alt_destroyed, destroy_message + ' :: ' + alt_destroy_message)
else:
self.assertTrue(instance_destroyed, destroy_message)
success = True
fail_messages = []
alt_names = self._alt_names()
for instance in alt_names:
alt_destroyed, alt_destroy_message = self._ensure_deletion(instance)
if not alt_destroyed:
success = False
fail_messages.append(alt_destroy_message)
log.error('Failed to destroy instance "{}": {}'.format(instance, alt_destroy_message))
self.assertTrue(success, '\n'.join(fail_messages))
self.assertFalse(alt_names, 'Cleanup should happen in the test, not the TearDown')
@classmethod
def tearDownClass(cls):