Skip to content

Commit

Permalink
t/fiotestlib: use 'with' for opening files
Browse files Browse the repository at this point in the history
Use with when opening files to relieve us of the need to explicitly
close the files.

Signed-off-by: Vincent Fu <[email protected]>
  • Loading branch information
vincentkfu committed Jun 8, 2023
1 parent 68b3a74 commit 1cf0ba9
Showing 1 changed file with 24 additions and 29 deletions.
53 changes: 24 additions & 29 deletions t/fiotestlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,32 +81,31 @@ def run(self):
"""Execute the binary or script described by this instance."""

command = [self.paths['exe']] + self.parameters
command_file = open(self.filenames['cmd'], "w+",
encoding=locale.getpreferredencoding())
command_file.write(f"{command}\n")
command_file.close()

stdout_file = open(self.filenames['stdout'], "w+",
encoding=locale.getpreferredencoding())
stderr_file = open(self.filenames['stderr'], "w+",
encoding=locale.getpreferredencoding())
exitcode_file = open(self.filenames['exitcode'], "w+",
encoding=locale.getpreferredencoding())
with open(self.filenames['cmd'], "w+",
encoding=locale.getpreferredencoding()) as command_file:
command_file.write(f"{command}\n")

try:
proc = None
# Avoid using subprocess.run() here because when a timeout occurs,
# fio will be stopped with SIGKILL. This does not give fio a
# chance to clean up and means that child processes may continue
# running and submitting IO.
proc = subprocess.Popen(command,
stdout=stdout_file,
stderr=stderr_file,
cwd=self.paths['test_dir'],
universal_newlines=True)
proc.communicate(timeout=self.success['timeout'])
exitcode_file.write(f'{proc.returncode}\n')
logging.debug("Test %d: return code: %d", self.testnum, proc.returncode)
self.output['proc'] = proc
with open(self.filenames['stdout'], "w+",
encoding=locale.getpreferredencoding()) as stdout_file, \
open(self.filenames['stderr'], "w+",
encoding=locale.getpreferredencoding()) as stderr_file, \
open(self.filenames['exitcode'], "w+",
encoding=locale.getpreferredencoding()) as exitcode_file:
proc = None
# Avoid using subprocess.run() here because when a timeout occurs,
# fio will be stopped with SIGKILL. This does not give fio a
# chance to clean up and means that child processes may continue
# running and submitting IO.
proc = subprocess.Popen(command,
stdout=stdout_file,
stderr=stderr_file,
cwd=self.paths['test_dir'],
universal_newlines=True)
proc.communicate(timeout=self.success['timeout'])
exitcode_file.write(f'{proc.returncode}\n')
logging.debug("Test %d: return code: %d", self.testnum, proc.returncode)
self.output['proc'] = proc
except subprocess.TimeoutExpired:
proc.terminate()
proc.communicate()
Expand All @@ -119,10 +118,6 @@ def run(self):
proc.communicate()
self.output['failure'] = 'exception'
self.output['exc_info'] = sys.exc_info()
finally:
stdout_file.close()
stderr_file.close()
exitcode_file.close()

def check_result(self):
"""Check results of test run."""
Expand Down

0 comments on commit 1cf0ba9

Please sign in to comment.