diff --git a/qemu/tests/boot_order_check.py b/qemu/tests/boot_order_check.py index 66ee4b4d671e7b786af3920b03e6bddb68d61dfe..92fb86944cd6509f961df637c42e250e54565fe7 100644 --- a/qemu/tests/boot_order_check.py +++ b/qemu/tests/boot_order_check.py @@ -7,6 +7,13 @@ from avocado.utils import process from virttest import error_context +try: + cmp +except NameError: + def cmp(x, y): + return (x > y) - (x < y) + + @error_context.context_aware def run(test, params, env): """ diff --git a/qemu/tests/netperf_udp.py b/qemu/tests/netperf_udp.py index 2045ba06a4b6930a4232f215754ae175b00df52f..50b77b51e391ee5ac6b82a86fffa6555c02cf181 100644 --- a/qemu/tests/netperf_udp.py +++ b/qemu/tests/netperf_udp.py @@ -172,7 +172,8 @@ def run(test, params, env): finally: netperf_server.stop() - file(os.path.join(test.debugdir, "udp_results"), "w").write(msg) + with open(os.path.join(test.debugdir, "udp_results"), "w") as result_file: + result_file.write(msg) failratio = float(params.get("failratio", 0.3)) error_context.context("Compare UDP performance.", logging.info) for i in range(len(throughput) - 1): diff --git a/qemu/tests/qemu_nobody.py b/qemu/tests/qemu_nobody.py index 052e287d07516f4600357f621fdf247491f15cd9..0ba89d2b29ba2ee41c4b9146dcf78e78e5bbb980 100644 --- a/qemu/tests/qemu_nobody.py +++ b/qemu/tests/qemu_nobody.py @@ -7,6 +7,13 @@ from virttest import env_process from virttest import error_context +try: + cmp +except NameError: + def cmp(x, y): + return (x > y) - (x < y) + + @env_process.context_aware def run(test, params, env): """ diff --git a/qemu/tests/timedrift_monotonicity.py b/qemu/tests/timedrift_monotonicity.py index 46c347e07983785a5a6375ea47e1d7f197455a70..971895df14751e535525b75b2b773740fa6efb27 100644 --- a/qemu/tests/timedrift_monotonicity.py +++ b/qemu/tests/timedrift_monotonicity.py @@ -38,9 +38,8 @@ def run(test, params, env): if float(tv) < float(lasttv): p_tv = "time value = " + tv + "\n" p_lasttv = "last time value = " + lasttv + "\n" - time_log = file(host_path, 'a') - time_log.write("time went backwards:\n" + p_tv + p_lasttv) - time_log.close() + with open(host_path, 'a') as time_log: + time_log.write("time went backwards:\n" + p_tv + p_lasttv) lasttv = tv time.sleep(0.1) @@ -86,12 +85,11 @@ def run(test, params, env): log_dir = os.path.join(test.outputdir, "timedrift-monotonicity-result.txt") shutil.copyfile(host_path, log_dir) - myfile = file(host_path, 'r') - for line in myfile: - if "time went backwards" in line: - myfile.close() - test.fail("Failed Time Monotonicity testing, " - "Please check log %s" % host_path) + with open(host_path, 'r') as myfile: + for line in myfile: + if "time went backwards" in line: + test.fail("Failed Time Monotonicity testing, " + "Please check log %s" % host_path) finally: session1.close() # remove flags add for this test. diff --git a/qemu/tests/trace_cmd_boot.py b/qemu/tests/trace_cmd_boot.py index 61ea776550c3b179972aca653f134bdfe99818dc..48cc22a97d230a70e8e7cc7789abf7fd3030fff7 100644 --- a/qemu/tests/trace_cmd_boot.py +++ b/qemu/tests/trace_cmd_boot.py @@ -75,7 +75,8 @@ def run(test, params, env): logging.warn("trace-cmd could not finish after 120s.") trace_job = None process.system(trace_report_cmd, shell=True) - report_txt = file(report_file).read() + with open(report_file) as report_f: + report_txt = report_f.read() txt = "Check whether the trace.txt includes the error log." error_context.context(txt, logging.info) if re.findall(re_trace, report_txt, re.S): diff --git a/qemu/tests/win_virtio_serial_data_transfer_reboot.py b/qemu/tests/win_virtio_serial_data_transfer_reboot.py index 3407d6780678b9156e61102e811c13dfd2335776..938238c586759e5d216545476c58191f2374c666 100644 --- a/qemu/tests/win_virtio_serial_data_transfer_reboot.py +++ b/qemu/tests/win_virtio_serial_data_transfer_reboot.py @@ -39,7 +39,8 @@ def run(test, params, env): def receive_data(test, session, serial_receive_cmd, data_file): output = session.cmd_output(serial_receive_cmd, timeout=30) - ori_data = file(data_file, "r").read() + with open(data_file, "r") as data_file: + ori_data = data_file.read() if ori_data.strip() != output.strip(): err = "Data lost during transfer. Origin data is:\n%s" % ori_data err += "Guest receive data:\n%s" % output