diff options
author | Jenkins <jenkins@review.openstack.org> | 2017-01-24 11:16:47 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2017-01-24 11:16:47 +0000 |
commit | 44e6e4905278aa14df52e34b0a030022a5622ca0 (patch) | |
tree | 5578d01887c5d0978a20e3068ed127e8566299a6 | |
parent | e952768a1bea262527891001475026aab51e6963 (diff) | |
parent | b6ccb4b5243ab6df2f9935cc8fbc2354ce3c9068 (diff) | |
download | ironic-44e6e4905278aa14df52e34b0a030022a5622ca0.tar.gz |
Merge "Use context manager for better file handling"
-rw-r--r-- | ironic/tests/unit/common/test_pxe_utils.py | 29 | ||||
-rw-r--r-- | ironic/tests/unit/common/test_utils.py | 15 | ||||
-rw-r--r-- | ironic/tests/unit/drivers/modules/test_image_cache.py | 3 |
3 files changed, 24 insertions, 23 deletions
diff --git a/ironic/tests/unit/common/test_pxe_utils.py b/ironic/tests/unit/common/test_pxe_utils.py index 037aaae7e..668cac7ad 100644 --- a/ironic/tests/unit/common/test_pxe_utils.py +++ b/ironic/tests/unit/common/test_pxe_utils.py @@ -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) diff --git a/ironic/tests/unit/common/test_utils.py b/ironic/tests/unit/common/test_utils.py index 58fd9bd93..48527e7de 100644 --- a/ironic/tests/unit/common/test_utils.py +++ b/ironic/tests/unit/common/test_utils.py @@ -56,8 +56,8 @@ class ExecuteTestCase(base.TestCase): fd, tmpfilename = tempfile.mkstemp() _, tmpfilename2 = tempfile.mkstemp() try: - fp = os.fdopen(fd, 'w+') - fp.write('''#!/bin/sh + 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 then @@ -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') - runs = fp.read() - fp.close() + with open(tmpfilename2, 'r') as fp: + runs = fp.read() self.assertNotEqual(runs.strip(), 'failure', 'stdin did not ' 'always get passed ' 'correctly') @@ -120,8 +118,8 @@ exit 1 fd, tmpfilename = tempfile.mkstemp() _, tmpfilename2 = tempfile.mkstemp() try: - fp = os.fdopen(fd, 'w+') - fp.write('''#!/bin/sh + with os.fdopen(fd, 'w+') as fp: + fp.write('''#!/bin/sh # If we've already run, bail out. grep -q foo "$1" && exit 1 # Mark that we've run before. @@ -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, diff --git a/ironic/tests/unit/drivers/modules/test_image_cache.py b/ironic/tests/unit/drivers/modules/test_image_cache.py index 1224f528f..b50d3fe40 100644 --- a/ironic/tests/unit/drivers/modules/test_image_cache.py +++ b/ironic/tests/unit/drivers/modules/test_image_cache.py @@ -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) |