]> git-server-git.apps.pok.os.sepia.ceph.com Git - teuthology.git/commitdiff
python3: use open() function instead file() class 1313/head
authorKyr Shatskyy <kyrylo.shatskyy@suse.com>
Wed, 2 Oct 2019 19:08:18 +0000 (21:08 +0200)
committerKyr Shatskyy <kyrylo.shatskyy@suse.com>
Thu, 3 Oct 2019 10:03:36 +0000 (12:03 +0200)
Since python3 does not have file() we should stop using it.

Signed-off-by: Kyr Shatskyy <kyrylo.shatskyy@suse.com>
28 files changed:
teuthology/config.py
teuthology/coverage.py
teuthology/lock/cli.py
teuthology/ls.py
teuthology/misc.py
teuthology/nuke/__init__.py
teuthology/provision/cloud/test/test_cloud_util.py
teuthology/provision/cloud/util.py
teuthology/prune.py
teuthology/report.py
teuthology/run.py
teuthology/suite/run.py
teuthology/suite/test/test_run_.py
teuthology/task/ansible.py
teuthology/task/install/daemon-helper
teuthology/task/install/util.py
teuthology/task/internal/__init__.py
teuthology/task/kernel.py
teuthology/test/fake_archive.py
teuthology/test/task/test_ansible.py
teuthology/test/task/test_pcp.py
teuthology/test/test_ls.py
teuthology/test/test_misc.py
teuthology/test/test_report.py
teuthology/test/test_run.py
teuthology/test/test_timer.py
teuthology/timer.py
teuthology/util/flock.py

index 2144db0a949973c48b3f4943b99771723c00cd6e..e212c33669a1443a7adcf2271b36f792e05245e5 100644 (file)
@@ -37,7 +37,8 @@ class YamlConfig(collections.MutableMapping):
                 self._conf = yaml.safe_load(conf)
             return
         if os.path.exists(self.yaml_path):
-            self._conf = yaml.safe_load(file(self.yaml_path))
+            with open(self.yaml_path) as f:
+                self._conf = yaml.safe_load(f)
         else:
             log.debug("%s not found", self.yaml_path)
             self._conf = dict()
index 38296959697436b4353def1c87903d33529c1c03..46706360e187b3d3513b9248a29c1cb4e3f1fcea 100644 (file)
@@ -122,7 +122,7 @@ def analyze(test_dir, cov_tools_dir, lcov_output, html_output, skip_init):
     test_summaries = {}
     for test in tests:
         summary = {}
-        with file(os.path.join(test_dir, test, 'summary.yaml')) as f:
+        with open(os.path.join(test_dir, test, 'summary.yaml')) as f:
             g = yaml.safe_load_all(f)
             for new in g:
                 summary.update(new)
index 4848e502ee5b23a65b97e802e46587477d56c567..5df9ad0926ce7d5107ddd85d52eeb06e8a4fd44c 100644 (file)
@@ -37,7 +37,7 @@ def main(ctx):
 
     if ctx.targets:
         try:
-            with file(ctx.targets) as f:
+            with open(ctx.targets) as f:
                 g = yaml.safe_load_all(f)
                 for new in g:
                     if 'targets' in new:
@@ -278,7 +278,7 @@ def updatekeys(args):
                     for m in args['<machine>']]
     elif args['--targets']:
         targets = args['--targets']
-        with file(targets) as f:
+        with open(targets) as f:
             docs = yaml.safe_load_all(f)
             for doc in docs:
                 machines = [n for n in doc.get('targets', dict()).iterkeys()]
index ff4e6f8454dcc683897e8ab5985a333c37b9f41c..c6332bac705959099ab8f09e1d7ddd781d2ed1a3 100644 (file)
@@ -15,7 +15,7 @@ def ls(archive_dir, verbose):
         job_dir = os.path.join(archive_dir, j)
         summary = {}
         try:
-            with file(os.path.join(job_dir, 'summary.yaml')) as f:
+            with open(os.path.join(job_dir, 'summary.yaml')) as f:
                 g = yaml.safe_load_all(f)
                 for new in g:
                     summary.update(new)
index 52c304c5715810326ad1b0f1e7a0ffd7b62b3636..78d3d10c5f30634923c967a399e25ac11d2e60f1 100644 (file)
@@ -92,7 +92,7 @@ def config_file(string):
     """
     config_dict = {}
     try:
-        with file(string) as f:
+        with open(string) as f:
             g = yaml.safe_load_all(f)
             for new in g:
                 config_dict.update(new)
@@ -124,7 +124,7 @@ def merge_configs(config_paths):
         if not os.path.exists(conf_path):
             log.debug("The config path {0} does not exist, skipping.".format(conf_path))
             continue
-        with file(conf_path) as partial_file:
+        with open(conf_path) as partial_file:
             partial_dict = yaml.safe_load(partial_file)
         try:
             conf_dict = deep_merge(conf_dict, partial_dict)
index 260dc29e6ef516b67f332a8fc40567102380b15a..343e2ff13f9c0a4197846e7eccb6a6c80232092b 100644 (file)
@@ -184,7 +184,7 @@ def main(args):
         ctx.config = config_file(ctx.archive + '/config.yaml')
         ifn = os.path.join(ctx.archive, 'info.yaml')
         if os.path.exists(ifn):
-            with file(ifn, 'r') as fd:
+            with open(ifn, 'r') as fd:
                 info = yaml.load(fd.read())
         if not ctx.pid:
             ctx.pid = info.get('pid')
index 40e815745926056645b09b7b4df585a39ec59192..220833c0e630d28d622977c23228165351a29f08 100644 (file)
@@ -2,7 +2,7 @@ import datetime
 import dateutil
 import json
 
-from mock import patch, MagicMock
+from mock import patch, mock_open
 from pytest import mark
 
 from teuthology.provision.cloud import util
@@ -18,11 +18,10 @@ from teuthology.provision.cloud import util
 def test_get_user_ssh_pubkey(path, exists):
     with patch('os.path.exists') as m_exists:
         m_exists.return_value = exists
-        with patch('teuthology.provision.cloud.util.file') as m_file:
-            m_file.return_value = MagicMock(spec=file)
+        with patch('__builtin__.open', mock_open(), create=True) as m_open:
             util.get_user_ssh_pubkey(path)
             if exists:
-                assert m_file.called_once_with(path, 'rb')
+                m_open.assert_called_once_with(path, 'rb')
 
 
 @mark.parametrize(
@@ -103,7 +102,6 @@ class TestAuthToken(object):
         self.mocks = dict()
         for name, patcher in self.patchers.items():
             self.mocks[name] = patcher.start()
-        self.mocks['m_open'].return_value = MagicMock(spec=file)
 
     def teardown(self):
         for patcher in self.patchers.values():
index aaf255a0ff6c838ce2db954db860011f75be28fd..71cc34d49fc2f2921fc3aa22cfaec26034429d4b 100644 (file)
@@ -11,7 +11,7 @@ def get_user_ssh_pubkey(path='~/.ssh/id_rsa.pub'):
     full_path = os.path.expanduser(path)
     if not os.path.exists(full_path):
         return
-    with file(full_path, 'rb') as f:
+    with open(full_path, 'rb') as f:
         return f.read().strip()
 
 
index cccd1d4c915a93ae4d0ceaf6d7903886a0537049..cf7bda8671a7d91fb3b1a5a2e0c335422dc6f19c 100644 (file)
@@ -139,7 +139,7 @@ def maybe_remove_jobs(run_dir, pass_days, fail_days, dry_run=False):
         # Depending on whether it passed or failed, we have a different age
         # threshold
         summary_lines = [line.strip() for line in
-                         file(summary_path).readlines()]
+                         open(summary_path).readlines()]
         if 'success: true' in summary_lines:
             status = 'passed'
             days = pass_days
index a9fcc2220c65d2e562cb5cad5787dd035967594a..ab0430ee19fbfae9ad54c23d1a77edee438e0e47 100644 (file)
@@ -91,7 +91,7 @@ class ResultsSerializer(object):
             yaml_path = os.path.join(job_archive_dir, yaml_name)
             if not os.path.exists(yaml_path):
                 continue
-            with file(yaml_path) as yaml_file:
+            with open(yaml_path) as yaml_file:
                 partial_info = yaml.safe_load(yaml_file)
                 if partial_info is not None:
                     job_info.update(partial_info)
@@ -329,14 +329,14 @@ class ResultsReporter(object):
         if hasattr(self, '__last_run'):
             return self.__last_run
         elif os.path.exists(self.last_run_file):
-            with file(self.last_run_file) as f:
+            with open(self.last_run_file) as f:
                 self.__last_run = f.read().strip()
             return self.__last_run
 
     @last_run.setter
     def last_run(self, run_name):
         self.__last_run = run_name
-        with file(self.last_run_file, 'w') as f:
+        with open(self.last_run_file, 'w') as f:
             f.write(run_name)
 
     @last_run.deleter
@@ -395,7 +395,7 @@ class ResultsReporter(object):
         # parse the log file generated by teuthology.results.results()
         subset = None
         seed = None
-        with file(log_path) as results_log:
+        with open(log_path) as results_log:
             for line in results_log:
                 if ':' not in line:
                     # stop if this does not look line a log line
index 8286f7c2fb949e1c9bb97ac9a45224df5b65f0fe..ceaa9715870c9567cf0274085fc867fb51b09669 100644 (file)
@@ -35,13 +35,13 @@ def set_up_logging(verbose, archive):
 
 def write_initial_metadata(archive, config, name, description, owner):
     if archive is not None:
-        with file(os.path.join(archive, 'pid'), 'w') as f:
+        with open(os.path.join(archive, 'pid'), 'w') as f:
             f.write('%d' % os.getpid())
 
-        with file(os.path.join(archive, 'owner'), 'w') as f:
+        with open(os.path.join(archive, 'owner'), 'w') as f:
             f.write(owner + '\n')
 
-        with file(os.path.join(archive, 'orig.config.yaml'), 'w') as f:
+        with open(os.path.join(archive, 'orig.config.yaml'), 'w') as f:
             yaml.safe_dump(config, f, default_flow_style=False)
 
         info = {
@@ -53,7 +53,7 @@ def write_initial_metadata(archive, config, name, description, owner):
         if 'job_id' in config:
             info['job_id'] = config['job_id']
 
-        with file(os.path.join(archive, 'info.yaml'), 'w') as f:
+        with open(os.path.join(archive, 'info.yaml'), 'w') as f:
             yaml.safe_dump(info, f, default_flow_style=False)
 
 
@@ -255,7 +255,7 @@ def report_outcome(config, archive, summary, fake_ctx):
         nuke(fake_ctx, fake_ctx.lock)
 
     if archive is not None:
-        with file(os.path.join(archive, 'summary.yaml'), 'w') as f:
+        with open(os.path.join(archive, 'summary.yaml'), 'w') as f:
             yaml.safe_dump(summary, f, default_flow_style=False)
 
     with contextlib.closing(StringIO.StringIO()) as f:
index 921ea7af8474632bc8d420c3fc33867642158263..f5ca1c1984477572a61e78acdc12a08314e87811 100644 (file)
@@ -381,7 +381,7 @@ class Run(object):
                 if not is_collected:
                     continue
 
-            raw_yaml = '\n'.join([file(a, 'r').read() for a in fragment_paths])
+            raw_yaml = '\n'.join([open(a, 'r').read() for a in fragment_paths])
 
             parsed_yaml = yaml.safe_load(raw_yaml)
             os_type = parsed_yaml.get('os_type') or self.base_config.os_type
index ece9b7ce59b51b35af6a21a0aceeac700daf5ae6..0b843fb93ca2f86c7b5f5c623fe20d32d43506b1 100644 (file)
@@ -2,6 +2,7 @@ import os
 import pytest
 import requests
 import yaml
+import contextlib
 
 from datetime import datetime
 from mock import patch, call, ANY, DEFAULT
@@ -200,7 +201,7 @@ class TestScheduleSuite(object):
     @patch('teuthology.suite.util.has_packages_for_distro')
     @patch('teuthology.suite.util.get_package_versions')
     @patch('teuthology.suite.util.get_install_task_flavor')
-    @patch('__builtin__.file')
+    @patch('__builtin__.open')
     @patch('teuthology.suite.run.build_matrix')
     @patch('teuthology.suite.util.git_ls_remote')
     @patch('teuthology.suite.util.package_version_for_hash')
@@ -213,7 +214,7 @@ class TestScheduleSuite(object):
         m_package_version_for_hash,
         m_git_ls_remote,
         m_build_matrix,
-        m_file,
+        m_open,
         m_get_install_task_flavor,
         m_get_package_versions,
         m_has_packages_for_distro,
@@ -232,9 +233,10 @@ class TestScheduleSuite(object):
         m_build_matrix.return_value = build_matrix_output
         frag1_read_output = 'field1: val1'
         frag2_read_output = 'field2: val2'
-        m_file.side_effect = [
+        m_open.side_effect = [
             StringIO(frag1_read_output),
             StringIO(frag2_read_output),
+            contextlib.closing(StringIO())
         ]
         m_get_install_task_flavor.return_value = 'basic'
         m_get_package_versions.return_value = dict()
@@ -275,7 +277,7 @@ class TestScheduleSuite(object):
     @patch('teuthology.suite.util.has_packages_for_distro')
     @patch('teuthology.suite.util.get_package_versions')
     @patch('teuthology.suite.util.get_install_task_flavor')
-    @patch('__builtin__.file')
+    @patch('__builtin__.open', create=True)
     @patch('teuthology.suite.run.build_matrix')
     @patch('teuthology.suite.util.git_ls_remote')
     @patch('teuthology.suite.util.package_version_for_hash')
@@ -288,7 +290,7 @@ class TestScheduleSuite(object):
         m_package_version_for_hash,
         m_git_ls_remote,
         m_build_matrix,
-        m_file,
+        m_open,
         m_get_install_task_flavor,
         m_get_package_versions,
         m_has_packages_for_distro,
@@ -305,7 +307,7 @@ class TestScheduleSuite(object):
             (build_matrix_desc, build_matrix_frags),
         ]
         m_build_matrix.return_value = build_matrix_output
-        m_file.side_effect = [StringIO('field: val\n') for i in xrange(11)]
+        m_open.side_effect = [StringIO('field: val\n') for i in xrange(11)]
         m_get_install_task_flavor.return_value = 'basic'
         m_get_package_versions.return_value = dict()
         m_has_packages_for_distro.side_effect = [
@@ -330,7 +332,7 @@ class TestScheduleSuite(object):
     @patch('teuthology.suite.util.has_packages_for_distro')
     @patch('teuthology.suite.util.get_package_versions')
     @patch('teuthology.suite.util.get_install_task_flavor')
-    @patch('__builtin__.file')
+    @patch('__builtin__.open', create=True)
     @patch('teuthology.suite.run.build_matrix')
     @patch('teuthology.suite.util.git_ls_remote')
     @patch('teuthology.suite.util.package_version_for_hash')
@@ -343,7 +345,7 @@ class TestScheduleSuite(object):
         m_package_version_for_hash,
         m_git_ls_remote,
         m_build_matrix,
-        m_file,
+        m_open,
         m_get_install_task_flavor,
         m_get_package_versions,
         m_has_packages_for_distro,
@@ -364,9 +366,11 @@ class TestScheduleSuite(object):
             (build_matrix_desc, build_matrix_frags),
         ]
         m_build_matrix.return_value = build_matrix_output
-        m_file.side_effect = [
+        m_open.side_effect = [
             StringIO('field: val\n') for i in xrange(NUM_FAILS+1)
-        ]
+        ] + [
+            contextlib.closing(StringIO())
+        ] 
         m_get_install_task_flavor.return_value = 'basic'
         m_get_package_versions.return_value = dict()
         # NUM_FAILS, then success
index 30f799d66eb9708372b3e096d1279a4fd58248ed..0c94ff7832e627a5aa9bfcff3087d75fa443ac0a 100644 (file)
@@ -171,7 +171,7 @@ class Ansible(Task):
                     pb_in_repo = os.path.join(self.repo_path, playbook_path)
                     if os.path.exists(pb_in_repo):
                         playbook_path = pb_in_repo
-                self.playbook_file = file(playbook_path)
+                self.playbook_file = open(playbook_path)
                 playbook_yaml = yaml.safe_load(self.playbook_file)
                 self.playbook = playbook_yaml
             except Exception:
@@ -227,7 +227,7 @@ class Ansible(Task):
         if inv_suffix:
             inv_fn = '.'.join(inv_fn, inv_suffix)
         # Write out the inventory file
-        inv_file = file(inv_fn, 'w')
+        inv_file = open(inv_fn, 'w')
         inv_file.write(inventory)
         # Next, write the group_vars files
         all_group_vars = self.config.get('group_vars')
@@ -239,7 +239,7 @@ class Ansible(Task):
             for group_name in sorted(all_group_vars):
                 group_vars = all_group_vars[group_name]
                 path = os.path.join(group_vars_dir, group_name + '.yml')
-                gv_file = file(path, 'w')
+                gv_file = open(path, 'w')
                 yaml.safe_dump(group_vars, gv_file)
 
         return inventory_dir
index dbb3258d603476d644e3b6fb2a8f5f17ccad8e53..53455b928f87184a5e0788d8435452171e7228b0 100755 (executable)
@@ -59,7 +59,7 @@ if nostdin:
         args=args[skip_nostdin:],
         )
 else:
-    with file('/dev/null', 'rb') as devnull:
+    with open('/dev/null', 'rb') as devnull:
         proc = subprocess.Popen(
             args=args,
             stdin=devnull,
index 39692c9d36ec2f8ca27de34a3b5509a97bd2a63a..65d481e65f78a169a2078679ca9d286bc57472b2 100644 (file)
@@ -69,7 +69,7 @@ def ship_utilities(ctx, config):
     log.info('Shipping valgrind.supp...')
     assert 'suite_path' in ctx.config
     try:
-        with file(
+        with open(
             os.path.join(ctx.config['suite_path'], 'valgrind.supp'),
             'rb'
                 ) as f:
@@ -92,7 +92,7 @@ def ship_utilities(ctx, config):
         src = os.path.join(os.path.dirname(__file__), filename)
         dst = os.path.join(destdir, filename)
         filenames.append(dst)
-        with file(src, 'rb') as f:
+        with open(src, 'rb') as f:
             for rem in ctx.cluster.remotes.iterkeys():
                 teuthology.sudo_write_file(
                     remote=rem,
index 65ba9474a256a8391e0b2cf62df88df97f835f13..0c674e317e0f86c8c71876a14fd2bee38012706a 100644 (file)
@@ -58,7 +58,7 @@ def save_config(ctx, config):
     """
     log.info('Saving configuration')
     if ctx.archive is not None:
-        with file(os.path.join(ctx.archive, 'config.yaml'), 'w') as f:
+        with open(os.path.join(ctx.archive, 'config.yaml'), 'w') as f:
             yaml.safe_dump(ctx.config, f, default_flow_style=False)
 
 
@@ -242,7 +242,7 @@ def serialize_remote_roles(ctx, config):
     So that other software can be loosely coupled to teuthology
     """
     if ctx.archive is not None:
-        with file(os.path.join(ctx.archive, 'info.yaml'), 'r+') as info_file:
+        with open(os.path.join(ctx.archive, 'info.yaml'), 'r+') as info_file:
             info_yaml = yaml.safe_load(info_file)
             info_file.seek(0)
             info_yaml['cluster'] = dict([(rem.name, {'roles': roles}) for rem, roles in ctx.cluster.remotes.iteritems()])
index d453b31bed578a483a759f84b10272719abe1fe0..eba98a03cc3b1cf2bcc0ff1272a9dfa98e726cb0 100644 (file)
@@ -346,7 +346,7 @@ def download_kernel(ctx, config):
             proc = role_remote.run(
                 args=[
                     'python', '-c',
-                    'import shutil, sys; shutil.copyfileobj(sys.stdin, file(sys.argv[1], "wb"))',
+                    'import shutil, sys; shutil.copyfileobj(sys.stdin, open(sys.argv[1], "wb"))',
                     remote_pkg_path(role_remote),
                     ],
                 wait=False,
index 38d5864b506325533f8d0b63df5f5470f3ee4df1..76a944f46c968b5fd9fc5624d104de13a540fb62 100644 (file)
@@ -71,12 +71,12 @@ class FakeArchive(object):
             archive_dir = os.path.join(run_archive_dir, str(job['job_id']))
             os.mkdir(archive_dir)
 
-            with file(os.path.join(archive_dir, 'info.yaml'), 'w') as yfile:
+            with open(os.path.join(archive_dir, 'info.yaml'), 'w') as yfile:
                 yaml.safe_dump(job['info'], yfile)
 
             if 'summary' in job:
                 summary_path = os.path.join(archive_dir, 'summary.yaml')
-                with file(summary_path, 'w') as yfile:
+                with open(summary_path, 'w') as yfile:
                     yaml.safe_dump(job['summary'], yfile)
 
     def create_fake_run(self, run_name, job_count, yaml_path, num_hung=0):
index 43be9b70bde79438dbd4b6f95b8f84672f1e1145..bf2bbf9ebf1a49b8e5fbdb59191770d17c554b4e 100644 (file)
@@ -47,7 +47,7 @@ class TestAnsibleTask(TestTask):
             m_NTF,
         )
         self.patchers['file'] = patch(
-            'teuthology.task.ansible.file', create=True)
+            'teuthology.task.ansible.open', create=True)
         self.patchers['os_mkdir'] = patch(
             'teuthology.task.ansible.os.mkdir',
         )
index d49416903db1a9d3a08364a2ee17955a5a70fbd5..3e761e6160ca85670a591f873a5c387095dff87e 100644 (file)
@@ -2,7 +2,7 @@ import os
 import requests
 import urlparse
 
-from mock import patch, DEFAULT, Mock, MagicMock, call
+from mock import patch, DEFAULT, Mock, mock_open, call
 from pytest import raises
 
 from teuthology.config import config, FakeNamespace
@@ -170,8 +170,7 @@ class TestGraphiteGrapher(TestPCPGrapher):
             m_resp = Mock()
             m_resp.ok = True
             m_get.return_value = m_resp
-            with patch('teuthology.task.pcp.open', create=True) as m_open:
-                m_open.return_value = MagicMock(spec=file)
+            with patch('teuthology.task.pcp.open', mock_open(), create=True):
                 obj.download_graphs()
         expected_filenames = []
         for metric in obj.metrics:
@@ -199,8 +198,7 @@ class TestGraphiteGrapher(TestPCPGrapher):
             m_resp = Mock()
             m_resp.ok = True
             m_get.return_value = m_resp
-            with patch('teuthology.task.pcp.open', create=True) as m_open:
-                m_open.return_value = MagicMock(spec=file)
+            with patch('teuthology.task.pcp.open', mock_open(), create=True):
                 obj.download_graphs()
         html = obj.generate_html(mode='static')
         assert config.pcp_host not in html
index 98475ff0fb1853ea87a0a51e69e9edec50437bf5..6046c8619e2011e540fd7a5a3d55eb5cd8eadc3b 100644 (file)
@@ -17,18 +17,17 @@ class TestLs(object):
         assert results == ["1", "3"]
 
     @patch("yaml.safe_load_all")
-    @patch("__builtin__.file")
     @patch("teuthology.ls.get_jobs")
-    def test_ls(self, m_get_jobs, m_file, m_safe_load_all):
+    def test_ls(self, m_get_jobs,  m_safe_load_all):
         m_get_jobs.return_value = ["1", "2"]
         m_safe_load_all.return_value = [{"failure_reason": "reasons"}]
         ls.ls("some/archive/div", True)
 
-    @patch("__builtin__.file")
+    @patch("__builtin__.open")
     @patch("teuthology.ls.get_jobs")
-    def test_ls_ioerror(self, m_get_jobs, m_file):
+    def test_ls_ioerror(self, m_get_jobs, m_open):
         m_get_jobs.return_value = ["1", "2"]
-        m_file.side_effect = IOError()
+        m_open.side_effect = IOError()
         with pytest.raises(IOError):
             ls.ls("some/archive/dir", True)
 
index 87e330d73b3e68aadb30266cd8fc3fc727800c00..98fe251531ae948c7246d16af3cf4b4a600f9d4e 100644 (file)
@@ -301,8 +301,8 @@ class TestMergeConfigs(object):
 
     @patch("os.path.exists")
     @patch("yaml.safe_load")
-    @patch("__builtin__.file")
-    def test_merge_configs(self, m_file, m_safe_load, m_exists):
+    @patch("__builtin__.open")
+    def test_merge_configs(self, m_open, m_safe_load, m_exists):
         """ Only tests with one yaml file being passed, mainly just to test
             the loop logic.  The actual merge will be tested in subsequent
             tests.
@@ -312,7 +312,7 @@ class TestMergeConfigs(object):
         m_safe_load.return_value = expected
         result = misc.merge_configs(["path/to/config1"])
         assert result == expected
-        m_file.assert_called_once_with("path/to/config1")
+        m_open.assert_called_once_with("path/to/config1")
 
     def test_merge_configs_empty(self):
         assert misc.merge_configs([]) == {}
index c26c34e945d9c1823d19d0d2831f21c0ac8f7061..2d13b7d97ac2f4b96c9d85edd08b854f4b935461 100644 (file)
@@ -63,7 +63,7 @@ class TestSerializer(object):
         jobs = self.archive.create_fake_run(run_name, job_count, yaml_path)
         job = jobs[0]
 
-        with file(yaml_path) as yaml_file:
+        with open(yaml_path) as yaml_file:
             obj_from_yaml = yaml.safe_load(yaml_file)
         full_obj = obj_from_yaml.copy()
         full_obj.update(job['info'])
index eddcf8f08e6acc6bc7c8308e34193e142e92b8c6..77f8b25883527c77586450824a7eca062cc581ac 100644 (file)
@@ -45,8 +45,8 @@ class TestRun(object):
         with pytest.raises(AssertionError):
             run.setup_config(["some/config.yaml"])
 
-    @patch("__builtin__.file")
-    def test_write_initial_metadata(self, m_file):
+    @patch("__builtin__.open")
+    def test_write_initial_metadata(self, m_open):
         config = {"job_id": "123", "foo": "bar"}
         run.write_initial_metadata(
             "some/archive/dir",
@@ -61,7 +61,7 @@ class TestRun(object):
             call('some/archive/dir/orig.config.yaml', 'w'),
             call('some/archive/dir/info.yaml', 'w')
         ]
-        assert m_file.call_args_list == expected
+        assert m_open.call_args_list == expected
 
     def test_get_machine_type(self):
         result = run.get_machine_type(None, {"machine-type": "the_machine_type"})
@@ -121,9 +121,9 @@ class TestRun(object):
     @patch("yaml.safe_dump")
     @patch("teuthology.report.try_push_job_info")
     @patch("teuthology.run.email_results")
-    @patch("__builtin__.file")
+    @patch("__builtin__.open")
     @patch("sys.exit")
-    def test_report_outcome(self, m_sys_exit, m_file, m_email_results, m_try_push_job_info, m_safe_dump, m_nuke, m_get_status):
+    def test_report_outcome(self, m_sys_exit, m_open, m_email_results, m_try_push_job_info, m_safe_dump, m_nuke, m_get_status):
         config = {"nuke-on-error": True, "email-on-error": True}
         m_get_status.return_value = "fail"
         fake_ctx = Mock()
@@ -131,9 +131,9 @@ class TestRun(object):
         run.report_outcome(config, "the/archive/path", summary, fake_ctx)
         assert m_nuke.called
         m_try_push_job_info.assert_called_with(config, summary)
-        m_file.assert_called_with("the/archive/path/summary.yaml", "w")
+        m_open.assert_called_with("the/archive/path/summary.yaml", "w")
         assert m_email_results.called
-        assert m_file.called
+        assert m_open.called
         assert m_sys_exit.called
 
     @patch("teuthology.run.set_up_logging")
index 6034d8abe705c997c2bac8eb422ca4d87911dd6e..6a8d7d311db28e8ca20c763fe1d5906b70f2a6a2 100644 (file)
@@ -1,6 +1,6 @@
 from teuthology import timer
 
-from mock import MagicMock, patch
+from mock import MagicMock, patch, mock_open
 from time import time
 
 
@@ -53,15 +53,14 @@ class TestTimer(object):
         _path = '/path'
         _safe_dump = MagicMock(name='safe_dump')
         with patch('teuthology.timer.yaml.safe_dump', _safe_dump):
-            with patch('teuthology.timer.file') as _file:
-                _file.return_value = MagicMock(spec=file)
+            with patch('teuthology.timer.open', mock_open(), create=True) as _open:
                 self.timer = timer.Timer(path=_path)
                 assert self.timer.path == _path
                 self.timer.write()
-                _file.assert_called_once_with(_path, 'w')
+                _open.assert_called_once_with(_path, 'w')
                 _safe_dump.assert_called_once_with(
                     dict(),
-                    _file.return_value.__enter__.return_value,
+                    _open.return_value.__enter__.return_value,
                     default_flow_style=False,
                 )
 
@@ -69,15 +68,14 @@ class TestTimer(object):
         _path = '/path'
         _safe_dump = MagicMock(name='safe_dump')
         with patch('teuthology.timer.yaml.safe_dump', _safe_dump):
-            with patch('teuthology.timer.file') as _file:
-                _file.return_value = MagicMock(spec=file)
+            with patch('teuthology.timer.open', mock_open(), create=True) as _open:
                 self.timer = timer.Timer(path=_path, sync=True)
                 assert self.timer.path == _path
                 assert self.timer.sync is True
                 self.timer.mark()
-                _file.assert_called_once_with(_path, 'w')
+                _open.assert_called_once_with(_path, 'w')
                 _safe_dump.assert_called_once_with(
                     self.timer.data,
-                    _file.return_value.__enter__.return_value,
+                    _open.return_value.__enter__.return_value,
                     default_flow_style=False,
                 )
index 62148568269df7ce8bc1a04578f56dbb0b811061..0119b8b3a392069f1dc7668b280f7d82b5ad8899 100644 (file)
@@ -108,7 +108,7 @@ class Timer(object):
 
     def write(self):
         try:
-            with file(self.path, 'w') as f:
+            with open(self.path, 'w') as f:
                 yaml.safe_dump(self.data, f, default_flow_style=False)
         except Exception:
             log.exception("Failed to write timing.yaml !")
index 4264216d91f1b4fa6ce88dbf462d4f3a82c705cb..f381d8b51b4ec6cc089b3e0f96550b893f6bd411 100644 (file)
@@ -10,7 +10,7 @@ class FileLock(object):
     def __enter__(self):
         if not self.noop:
             assert self.file is None
-            self.file = file(self.filename, 'w')
+            self.file = open(self.filename, 'w')
             fcntl.lockf(self.file, fcntl.LOCK_EX)
         return self