def destroy_volume_wait(cls, volume): """Delete volume, tries to detach first. Use just for teardown! """ exc_num = 0 snaps = volume.snapshots() if len(snaps): LOG.critical("%s Volume has %s snapshot(s)", volume.id, map(snaps.id, snaps)) # NOTE(afazekas): detaching/attching not valid EC2 status def _volume_state(): volume.update(validate=True) try: if volume.status != "available": volume.detach(force=True) except BaseException: LOG.exception("Failed to detach volume %s" % volume) # exc_num += 1 "nonlocal" not in python2 return volume.status try: wait.re_search_wait(_volume_state, "available") # not validates status LOG.info(_volume_state()) volume.delete() except BaseException: LOG.exception("Failed to delete volume %s" % volume) exc_num += 1 if exc_num: raise exceptions.TearDownException(num=exc_num)
def destroy_reservation(cls, reservation): """Terminate instances in a reservation, just for teardown.""" exc_num = 0 def _instance_state(): try: instance.update(validate=True) except ValueError: return "_GONE" except exception.EC2ResponseError as exc: if cls.ec2_error_code.\ client.InvalidInstanceID.NotFound.match(exc) is None: return "_GONE" # NOTE(afazekas): incorrect code, # but the resource must be destoreyd if exc.error_code == "InstanceNotFound": return "_GONE" return instance.state for instance in reservation.instances: try: instance.terminate() wait.re_search_wait(_instance_state, "_GONE") except BaseException: LOG.exception("Failed to terminate instance %s " % instance) exc_num += 1 if exc_num: raise exceptions.TearDownException(num=exc_num)
def resource_cleanup(cls): """Calls the callables added by addResourceCleanUp, when you overwrite this function don't forget to call this too. """ fail_count = 0 trash_keys = sorted(cls._resource_trash_bin, reverse=True) for key in trash_keys: (function, pos_args, kw_args) = cls._resource_trash_bin[key] try: func_name = friendly_function_call_str(function, *pos_args, **kw_args) LOG.debug("Cleaning up: %s" % func_name) function(*pos_args, **kw_args) except BaseException: fail_count += 1 LOG.exception("Cleanup failed %s" % func_name) finally: del cls._resource_trash_bin[key] cls.clear_isolated_creds() super(BotoTestCase, cls).resource_cleanup() # NOTE(afazekas): let the super called even on exceptions # The real exceptions already logged, if the super throws another, # does not causes hidden issues if fail_count: raise exceptions.TearDownException(num=fail_count)
def destroy_volume_wait(cls, volume): """Delete volume, tries to detach first. Use just for teardown! """ exc_num = 0 snaps = volume.snapshots() if len(snaps): LOG.critical("%s Volume has %s snapshot(s)", volume.id, map(snaps.id, snaps)) # NOTE(afazekas): detaching/attching not valid EC2 status def _volume_state(): volume.update(validate=True) try: # NOTE(gmann): Make sure volume is attached. # Checking status as 'not "available"' is not enough to make # sure volume is attached as it can be in "error" state if volume.status == "in-use": volume.detach(force=True) except BaseException: LOG.exception("Failed to detach volume %s" % volume) # exc_num += 1 "nonlocal" not in python2 return volume.status try: wait.re_search_wait(_volume_state, "available") # not validates status LOG.info(_volume_state()) volume.delete() except BaseException: LOG.exception("Failed to delete volume %s" % volume) exc_num += 1 if exc_num: raise exceptions.TearDownException(num=exc_num)
def tearDownClass(cls): """Calls the callables added by addResourceCleanUp, when you overwire this function dont't forget to call this too. """ fail_count = 0 trash_keys = sorted(cls._resource_trash_bin, reverse=True) for key in trash_keys: (function, pos_args, kw_args) = cls._resource_trash_bin[key] try: LOG.debug( "Cleaning up: %s" % friendly_function_call_str(function, *pos_args, **kw_args)) function(*pos_args, **kw_args) except BaseException as exc: fail_count += 1 LOG.exception(exc) finally: del cls._resource_trash_bin[key] if fail_count: raise exceptions.TearDownException(num=fail_count)
def destroy_bucket(cls, connection_data, bucket): """Destroys the bucket and its content, just for teardown.""" exc_num = 0 try: with closing(boto.connect_s3(**connection_data)) as conn: if isinstance(bucket, basestring): bucket = conn.lookup(bucket) assert isinstance(bucket, s3.bucket.Bucket) for obj in bucket.list(): try: bucket.delete_key(obj.key) obj.close() except BaseException as exc: LOG.exception(exc) exc_num += 1 conn.delete_bucket(bucket) except BaseException as exc: LOG.exception(exc) exc_num += 1 if exc_num: raise exceptions.TearDownException(num=exc_num)