Safely generate os-faults file to temporary files
Change-Id: If1031a6d600958409d7b505753abdb4feb11a15a
This commit is contained in:
parent
e9a0120417
commit
fc5ffcf87d
@ -57,6 +57,7 @@ load_object = loader_manager.load_object
|
||||
load_module = loader_manager.load_module
|
||||
|
||||
makedirs = _os.makedirs
|
||||
open_output_file = _os.open_output_file
|
||||
|
||||
discover_testcases = testcase_manager.discover_testcases
|
||||
|
||||
|
@ -93,12 +93,14 @@ class ExceptionInfo(collections.namedtuple('ExceptionInfo',
|
||||
|
||||
def __exit__(self, _type, _value, _traceback):
|
||||
if self.reraise_on_exit:
|
||||
LOG.exception("Exception occurred while handling %s(%s) "
|
||||
"exception.", _type, _value)
|
||||
if _type is not None:
|
||||
LOG.exception("Exception occurred while handling %s(%s) "
|
||||
"exception.", self.type, self.value)
|
||||
self.reraise()
|
||||
|
||||
def reraise(self):
|
||||
six.reraise(*self)
|
||||
if self.type is not None:
|
||||
six.reraise(*self)
|
||||
|
||||
|
||||
def exc_info(reraise=True):
|
||||
|
@ -13,11 +13,15 @@
|
||||
# under the License.
|
||||
from __future__ import absolute_import
|
||||
|
||||
import contextlib
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
from oslo_log import log
|
||||
|
||||
|
||||
from tobiko.common import _exception
|
||||
|
||||
LOG = log.getLogger(__name__)
|
||||
|
||||
|
||||
@ -33,3 +37,22 @@ def makedirs(name, mode=0o777, exist_ok=True):
|
||||
except Exception:
|
||||
if not exist_ok or not os.path.isdir(name):
|
||||
raise
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def open_output_file(filename, mode='w', temp_dir=None, text=False):
|
||||
basename = os.path.basename(filename)
|
||||
prefix, suffix = os.path.splitext(basename)
|
||||
prefix += '-'
|
||||
temp_fd, temp_filename = tempfile.mkstemp(prefix=prefix,
|
||||
suffix=suffix,
|
||||
dir=temp_dir,
|
||||
text=text)
|
||||
try:
|
||||
with os.fdopen(temp_fd, mode) as temp_stream:
|
||||
yield temp_stream
|
||||
os.rename(temp_filename, filename)
|
||||
finally:
|
||||
with _exception.exc_info():
|
||||
if os.path.isfile(temp_filename):
|
||||
os.remove(temp_filename)
|
||||
|
@ -137,7 +137,7 @@ class OsFaultsConfigFileFixture(tobiko.SharedFixture):
|
||||
services=self.list_services(),
|
||||
containers=self.list_containers(),
|
||||
proxy=None)
|
||||
with open(config_filename, "w") as f:
|
||||
with tobiko.open_output_file(config_filename) as f:
|
||||
f.write(config_content)
|
||||
return config_filename
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user