Merge "Use context manager for better file handling"
This commit is contained in:
commit
44e6e49052
@ -74,8 +74,8 @@ class TestPXEUtils(db_base.DbTestCase):
|
||||
'ROOT': '{{ ROOT }}',
|
||||
'DISK_IDENTIFIER': '{{ DISK_IDENTIFIER }}'})
|
||||
|
||||
expected_template = open(
|
||||
'ironic/tests/unit/drivers/pxe_config.template').read().rstrip()
|
||||
with open('ironic/tests/unit/drivers/pxe_config.template') as f:
|
||||
expected_template = f.read().rstrip()
|
||||
|
||||
self.assertEqual(six.text_type(expected_template), rendered_template)
|
||||
|
||||
@ -84,8 +84,8 @@ class TestPXEUtils(db_base.DbTestCase):
|
||||
CONF.pxe.ipxe_boot_script,
|
||||
{'ipxe_for_mac_uri': 'pxelinux.cfg/'})
|
||||
|
||||
expected_template = open(
|
||||
'ironic/tests/unit/drivers/boot.ipxe').read().rstrip()
|
||||
with open('ironic/tests/unit/drivers/boot.ipxe') as f:
|
||||
expected_template = f.read().rstrip()
|
||||
|
||||
self.assertEqual(six.text_type(expected_template), rendered_template)
|
||||
|
||||
@ -105,8 +105,9 @@ class TestPXEUtils(db_base.DbTestCase):
|
||||
'ROOT': '{{ ROOT }}',
|
||||
'DISK_IDENTIFIER': '{{ DISK_IDENTIFIER }}'})
|
||||
|
||||
expected_template = open(
|
||||
'ironic/tests/unit/drivers/ipxe_config.template').read().rstrip()
|
||||
templ_file = 'ironic/tests/unit/drivers/ipxe_config.template'
|
||||
with open(templ_file) as f:
|
||||
expected_template = f.read().rstrip()
|
||||
|
||||
self.assertEqual(six.text_type(expected_template), rendered_template)
|
||||
|
||||
@ -126,8 +127,9 @@ class TestPXEUtils(db_base.DbTestCase):
|
||||
'ROOT': '{{ ROOT }}',
|
||||
'DISK_IDENTIFIER': '{{ DISK_IDENTIFIER }}'})
|
||||
|
||||
tpl_file = 'ironic/tests/unit/drivers/ipxe_config_timeout.template'
|
||||
expected_template = open(tpl_file).read().rstrip()
|
||||
templ_file = 'ironic/tests/unit/drivers/ipxe_config_timeout.template'
|
||||
with open(templ_file) as f:
|
||||
expected_template = f.read().rstrip()
|
||||
|
||||
self.assertEqual(six.text_type(expected_template), rendered_template)
|
||||
|
||||
@ -145,9 +147,9 @@ class TestPXEUtils(db_base.DbTestCase):
|
||||
'ROOT': '{{ ROOT }}',
|
||||
'DISK_IDENTIFIER': '{{ DISK_IDENTIFIER }}'})
|
||||
|
||||
expected_template = open(
|
||||
'ironic/tests/unit/drivers/elilo_efi_pxe_config.template'
|
||||
).read().rstrip()
|
||||
templ_file = 'ironic/tests/unit/drivers/elilo_efi_pxe_config.template'
|
||||
with open(templ_file) as f:
|
||||
expected_template = f.read().rstrip()
|
||||
|
||||
self.assertEqual(six.text_type(expected_template), rendered_template)
|
||||
|
||||
@ -161,8 +163,9 @@ class TestPXEUtils(db_base.DbTestCase):
|
||||
'ROOT': '(( ROOT ))',
|
||||
'DISK_IDENTIFIER': '(( DISK_IDENTIFIER ))'})
|
||||
|
||||
template_file = 'ironic/tests/unit/drivers/pxe_grub_config.template'
|
||||
expected_template = open(template_file).read().rstrip()
|
||||
templ_file = 'ironic/tests/unit/drivers/pxe_grub_config.template'
|
||||
with open(templ_file) as f:
|
||||
expected_template = f.read().rstrip()
|
||||
|
||||
self.assertEqual(six.text_type(expected_template), rendered_template)
|
||||
|
||||
|
@ -56,7 +56,7 @@ class ExecuteTestCase(base.TestCase):
|
||||
fd, tmpfilename = tempfile.mkstemp()
|
||||
_, tmpfilename2 = tempfile.mkstemp()
|
||||
try:
|
||||
fp = os.fdopen(fd, 'w+')
|
||||
with os.fdopen(fd, 'w+') as fp:
|
||||
fp.write('''#!/bin/sh
|
||||
# If stdin fails to get passed during one of the runs, make a note.
|
||||
if ! grep -q foo
|
||||
@ -77,7 +77,6 @@ runs=$(($runs + 1))
|
||||
echo $runs > "$1"
|
||||
exit 1
|
||||
''')
|
||||
fp.close()
|
||||
os.chmod(tmpfilename, 0o755)
|
||||
try:
|
||||
self.assertRaises(processutils.ProcessExecutionError,
|
||||
@ -91,9 +90,8 @@ exit 1
|
||||
"Are you running with a noexec /tmp?")
|
||||
else:
|
||||
raise
|
||||
fp = open(tmpfilename2, 'r')
|
||||
with open(tmpfilename2, 'r') as fp:
|
||||
runs = fp.read()
|
||||
fp.close()
|
||||
self.assertNotEqual(runs.strip(), 'failure', 'stdin did not '
|
||||
'always get passed '
|
||||
'correctly')
|
||||
@ -120,7 +118,7 @@ exit 1
|
||||
fd, tmpfilename = tempfile.mkstemp()
|
||||
_, tmpfilename2 = tempfile.mkstemp()
|
||||
try:
|
||||
fp = os.fdopen(fd, 'w+')
|
||||
with os.fdopen(fd, 'w+') as fp:
|
||||
fp.write('''#!/bin/sh
|
||||
# If we've already run, bail out.
|
||||
grep -q foo "$1" && exit 1
|
||||
@ -129,7 +127,6 @@ echo foo > "$1"
|
||||
# Check that stdin gets passed correctly.
|
||||
grep foo
|
||||
''')
|
||||
fp.close()
|
||||
os.chmod(tmpfilename, 0o755)
|
||||
try:
|
||||
utils.execute(tmpfilename,
|
||||
|
@ -342,7 +342,8 @@ class TestImageCacheCleanUp(base.TestCase):
|
||||
files = [os.path.join(self.master_dir, str(i))
|
||||
for i in range(2)]
|
||||
for filename in files:
|
||||
open(filename, 'wb').write(b'X')
|
||||
with open(filename, 'wb') as f:
|
||||
f.write(b'X')
|
||||
new_current_time = time.time() + 900
|
||||
with mock.patch.object(time, 'time', lambda: new_current_time):
|
||||
self.cache.clean_up(amount=1)
|
||||
|
Loading…
x
Reference in New Issue
Block a user