Merge "Use context manager for better file handling"

This commit is contained in:
Jenkins 2017-01-24 11:16:47 +00:00 committed by Gerrit Code Review
commit 44e6e49052
3 changed files with 24 additions and 23 deletions

View File

@ -74,8 +74,8 @@ class TestPXEUtils(db_base.DbTestCase):
'ROOT': '{{ ROOT }}', 'ROOT': '{{ ROOT }}',
'DISK_IDENTIFIER': '{{ DISK_IDENTIFIER }}'}) 'DISK_IDENTIFIER': '{{ DISK_IDENTIFIER }}'})
expected_template = open( with open('ironic/tests/unit/drivers/pxe_config.template') as f:
'ironic/tests/unit/drivers/pxe_config.template').read().rstrip() expected_template = f.read().rstrip()
self.assertEqual(six.text_type(expected_template), rendered_template) self.assertEqual(six.text_type(expected_template), rendered_template)
@ -84,8 +84,8 @@ class TestPXEUtils(db_base.DbTestCase):
CONF.pxe.ipxe_boot_script, CONF.pxe.ipxe_boot_script,
{'ipxe_for_mac_uri': 'pxelinux.cfg/'}) {'ipxe_for_mac_uri': 'pxelinux.cfg/'})
expected_template = open( with open('ironic/tests/unit/drivers/boot.ipxe') as f:
'ironic/tests/unit/drivers/boot.ipxe').read().rstrip() expected_template = f.read().rstrip()
self.assertEqual(six.text_type(expected_template), rendered_template) self.assertEqual(six.text_type(expected_template), rendered_template)
@ -105,8 +105,9 @@ class TestPXEUtils(db_base.DbTestCase):
'ROOT': '{{ ROOT }}', 'ROOT': '{{ ROOT }}',
'DISK_IDENTIFIER': '{{ DISK_IDENTIFIER }}'}) 'DISK_IDENTIFIER': '{{ DISK_IDENTIFIER }}'})
expected_template = open( templ_file = 'ironic/tests/unit/drivers/ipxe_config.template'
'ironic/tests/unit/drivers/ipxe_config.template').read().rstrip() with open(templ_file) as f:
expected_template = f.read().rstrip()
self.assertEqual(six.text_type(expected_template), rendered_template) self.assertEqual(six.text_type(expected_template), rendered_template)
@ -126,8 +127,9 @@ class TestPXEUtils(db_base.DbTestCase):
'ROOT': '{{ ROOT }}', 'ROOT': '{{ ROOT }}',
'DISK_IDENTIFIER': '{{ DISK_IDENTIFIER }}'}) 'DISK_IDENTIFIER': '{{ DISK_IDENTIFIER }}'})
tpl_file = 'ironic/tests/unit/drivers/ipxe_config_timeout.template' templ_file = 'ironic/tests/unit/drivers/ipxe_config_timeout.template'
expected_template = open(tpl_file).read().rstrip() with open(templ_file) as f:
expected_template = f.read().rstrip()
self.assertEqual(six.text_type(expected_template), rendered_template) self.assertEqual(six.text_type(expected_template), rendered_template)
@ -145,9 +147,9 @@ class TestPXEUtils(db_base.DbTestCase):
'ROOT': '{{ ROOT }}', 'ROOT': '{{ ROOT }}',
'DISK_IDENTIFIER': '{{ DISK_IDENTIFIER }}'}) 'DISK_IDENTIFIER': '{{ DISK_IDENTIFIER }}'})
expected_template = open( templ_file = 'ironic/tests/unit/drivers/elilo_efi_pxe_config.template'
'ironic/tests/unit/drivers/elilo_efi_pxe_config.template' with open(templ_file) as f:
).read().rstrip() expected_template = f.read().rstrip()
self.assertEqual(six.text_type(expected_template), rendered_template) self.assertEqual(six.text_type(expected_template), rendered_template)
@ -161,8 +163,9 @@ class TestPXEUtils(db_base.DbTestCase):
'ROOT': '(( ROOT ))', 'ROOT': '(( ROOT ))',
'DISK_IDENTIFIER': '(( DISK_IDENTIFIER ))'}) 'DISK_IDENTIFIER': '(( DISK_IDENTIFIER ))'})
template_file = 'ironic/tests/unit/drivers/pxe_grub_config.template' templ_file = 'ironic/tests/unit/drivers/pxe_grub_config.template'
expected_template = open(template_file).read().rstrip() with open(templ_file) as f:
expected_template = f.read().rstrip()
self.assertEqual(six.text_type(expected_template), rendered_template) self.assertEqual(six.text_type(expected_template), rendered_template)

View File

@ -56,8 +56,8 @@ class ExecuteTestCase(base.TestCase):
fd, tmpfilename = tempfile.mkstemp() fd, tmpfilename = tempfile.mkstemp()
_, tmpfilename2 = tempfile.mkstemp() _, tmpfilename2 = tempfile.mkstemp()
try: try:
fp = os.fdopen(fd, 'w+') with os.fdopen(fd, 'w+') as fp:
fp.write('''#!/bin/sh fp.write('''#!/bin/sh
# If stdin fails to get passed during one of the runs, make a note. # If stdin fails to get passed during one of the runs, make a note.
if ! grep -q foo if ! grep -q foo
then then
@ -77,7 +77,6 @@ runs=$(($runs + 1))
echo $runs > "$1" echo $runs > "$1"
exit 1 exit 1
''') ''')
fp.close()
os.chmod(tmpfilename, 0o755) os.chmod(tmpfilename, 0o755)
try: try:
self.assertRaises(processutils.ProcessExecutionError, self.assertRaises(processutils.ProcessExecutionError,
@ -91,9 +90,8 @@ exit 1
"Are you running with a noexec /tmp?") "Are you running with a noexec /tmp?")
else: else:
raise raise
fp = open(tmpfilename2, 'r') with open(tmpfilename2, 'r') as fp:
runs = fp.read() runs = fp.read()
fp.close()
self.assertNotEqual(runs.strip(), 'failure', 'stdin did not ' self.assertNotEqual(runs.strip(), 'failure', 'stdin did not '
'always get passed ' 'always get passed '
'correctly') 'correctly')
@ -120,8 +118,8 @@ exit 1
fd, tmpfilename = tempfile.mkstemp() fd, tmpfilename = tempfile.mkstemp()
_, tmpfilename2 = tempfile.mkstemp() _, tmpfilename2 = tempfile.mkstemp()
try: try:
fp = os.fdopen(fd, 'w+') with os.fdopen(fd, 'w+') as fp:
fp.write('''#!/bin/sh fp.write('''#!/bin/sh
# If we've already run, bail out. # If we've already run, bail out.
grep -q foo "$1" && exit 1 grep -q foo "$1" && exit 1
# Mark that we've run before. # Mark that we've run before.
@ -129,7 +127,6 @@ echo foo > "$1"
# Check that stdin gets passed correctly. # Check that stdin gets passed correctly.
grep foo grep foo
''') ''')
fp.close()
os.chmod(tmpfilename, 0o755) os.chmod(tmpfilename, 0o755)
try: try:
utils.execute(tmpfilename, utils.execute(tmpfilename,

View File

@ -342,7 +342,8 @@ class TestImageCacheCleanUp(base.TestCase):
files = [os.path.join(self.master_dir, str(i)) files = [os.path.join(self.master_dir, str(i))
for i in range(2)] for i in range(2)]
for filename in files: 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 new_current_time = time.time() + 900
with mock.patch.object(time, 'time', lambda: new_current_time): with mock.patch.object(time, 'time', lambda: new_current_time):
self.cache.clean_up(amount=1) self.cache.clean_up(amount=1)