Clean up expensive queries

This commit is contained in:
Tyler Johnson 2019-08-19 11:16:39 -06:00
parent b3d02df7d8
commit f764bc09f5
No known key found for this signature in database
GPG key ID: 691E31397E27D004

View file

@ -27,7 +27,7 @@ class CloudTest(ShellCase):
PROVIDER = ''
REQUIRED_PROVIDER_CONFIG_ITEMS = tuple()
__RE_RUN_DELAY = 15
__RE_TRIES = 12
__RE_TRIES = 3
def query_instances(self):
'''
@ -60,18 +60,11 @@ class CloudTest(ShellCase):
if not instance_name:
instance_name = self.instance_name
# Assert that the last query was successful
self.assertTrue(self._instance_exists(instance_name, query),
'Instance "{}" was not created successfully: |\n\t{}\n\t|`'.format(
instance_name, '\n\t'.join(creation_ret if creation_ret else query)))
log.debug('Instance exists and was created: "{}"'.format(instance_name))
# If it exists but doesn't show up in the creation_ret, there was probably an error during creation
if creation_ret:
self.assertIn(instance_name, [i.strip(': ') for i in creation_ret],
'An error occured during instance creation: |\n\t{}\n\t|'.format(
'\n\t'.join(creation_ret if creation_ret else query)
'\n\t'.join(creation_ret)
))
else:
# Verify that the instance exists via query
@ -85,49 +78,42 @@ class CloudTest(ShellCase):
sleep(self.__RE_RUN_DELAY)
query = self.query_instances()
# Assert that the last query was successful
self.assertTrue(self._instance_exists(instance_name, query),
'Instance "{}" was not created successfully: |\n\t{}\n\t|`'.format(
instance_name, '\n\t'.join(creation_ret if creation_ret else query)))
log.debug('Instance exists and was created: "{}"'.format(instance_name))
def _destroy_instance(self):
log.debug('Deleting instance "{}"'.format(self.instance_name))
delete = self.run_cloud('-d {0} --assume-yes'.format(self.instance_name), timeout=TIMEOUT)
# example response: ['gce-config:', '----------', ' gce:', '----------', 'cloud-test-dq4e6c:', 'True', '']
delete_str = ''.join(delete)
destroyed = False
log.debug('Deletion status: {}'.format(delete_str))
if any([x in delete_str for x in (
'True',
'was successfully deleted'
)]):
destroyed = True
log.debug('Instance "{}" was successfully deleted'.format(self.instance_name))
elif any([x in delete_str for x in (
'shutting-down',
'.delete',
)]):
destroyed = True
log.debug('Instance "{}" is cleaning up'.format(self.instance_name))
sleep(30)
else:
log.warning('Instance "{}" may not have been deleted properly'.format(self.instance_name))
return delete_str
return destroyed, delete_str
def assertDestroyInstance(self):
delete_str = self._destroy_instance()
# It might take a while to register that deletion has happened with `salt-cloud --query`
query = self.query_instances()
for tries in range(self.__RE_TRIES):
# If the instance doesn't exist, then deletion was a success. Move on
if not self._instance_exists(self.instance_name, query):
log.debug('Instance "{}" reported as deleted after {} seconds'.format(self.instance_name,
tries * self.__RE_RUN_DELAY))
break
else:
# Wait a bit and check again
sleep(self.__RE_RUN_DELAY)
query = self.query_instances()
# The instance should be reported as destroyed by the final query, otherwise fail
self.assertFalse(self._instance_exists(query), 'Could not destroy "{}". Delete_str: `{}`'
.format(self.instance_name, delete_str))
log.debug('Instance "{}" no longer exists'.format(self.instance_name))
success, delete_str = self._destroy_instance()
self.assertTrue(success, 'Instance "{}" was not deleted: {}'.format(self.instance_name, delete_str))
@property
def instance_name(self):
@ -202,9 +188,6 @@ class CloudTest(ShellCase):
.format(', '.join(missing_conf_item)) +
'\nCheck tests/integration/files/conf/cloud.providers.d/{0}.conf'.format(self.PROVIDER))
self.assertFalse(self._instance_exists(),
'The instance "{}" exists before it was created by the test'.format(self.instance_name))
def tearDown(self):
'''
Clean up after tests, If the instance still exists for any reason, delete it.
@ -213,16 +196,13 @@ class CloudTest(ShellCase):
if the tearDown is where an instance is destroyed.
'''
# Make sure that the instance for sure gets deleted, but fail the test if it happens in the tearDown
instance_deleted_before_teardown = True
for _ in range(12):
if self._instance_exists():
if self._instance_exists():
for _ in range(12):
sleep(30)
instance_deleted_before_teardown = False
self._destroy_instance()
success, result_str = self._destroy_instance()
if success:
self.fail('The instance "{}" was deleted during the tearDown, not the test.'.format(
self.instance_name))
self.assertFalse(self._instance_exists(), 'Instance exists after multiple attempts to delete: {}'
.format(self.instance_name))
# Destroying instances in the tearDown is a contingency, not the way things should work by default.
self.assertTrue(instance_deleted_before_teardown,
'The Instance "{}" was deleted during the tearDown, not the test.'.format(self.instance_name))
# Destroying instances in the tearDown is a contingency, not the way things should work by default.
self.fail('The Instance "{}" was not deleted after multiple attempts'.format(self.instance_name))