From df03800278984bc7b415425b4480ff913e48be4e Mon Sep 17 00:00:00 2001 From: Wojtek Porczyk Date: Mon, 29 May 2017 16:42:40 +0200 Subject: [PATCH] Fix miscellaneous warnings Among them: - explicitly close files (possibly using with: syntax) - use non-deprecated methods --- qubes/tests/__init__.py | 2 +- qubes/tests/init.py | 10 ++-- qubes/tests/integ/backup.py | 6 +-- qubes/tests/integ/basic.py | 80 +++++++++++++++--------------- qubes/tests/integ/dispvm.py | 4 +- qubes/tests/integ/vm_qrexec_gui.py | 21 ++++---- qubes/tests/storage_file.py | 18 +++---- qubes/tests/vm/qubesvm.py | 12 ++--- qubes/vm/adminvm.py | 7 +-- 9 files changed, 80 insertions(+), 80 deletions(-) diff --git a/qubes/tests/__init__.py b/qubes/tests/__init__.py index 6f8786a9..4b2b9857 100644 --- a/qubes/tests/__init__.py +++ b/qubes/tests/__init__.py @@ -675,7 +675,7 @@ class SystemTestsMixin(object): try: # XXX .is_running() may throw libvirtError if undefined if vm.is_running(): - vm.force_shutdown() + vm.kill() except: # pylint: disable=bare-except pass diff --git a/qubes/tests/init.py b/qubes/tests/init.py index 22c7cfa9..55405764 100644 --- a/qubes/tests/init.py +++ b/qubes/tests/init.py @@ -79,7 +79,7 @@ class TC_10_property(qubes.tests.QubesTestCase): hash(self.holder.__class__.testprop1) def test_002_eq(self): - self.assertEquals(qubes.property('testprop2'), + self.assertEqual(qubes.property('testprop2'), qubes.property('testprop2')) def test_010_set(self): @@ -116,7 +116,7 @@ class TC_10_property(qubes.tests.QubesTestCase): def setter(self2, prop, value): self.assertIs(self2, holder) self.assertIs(prop, MyTestHolder.testprop1) - self.assertEquals(value, 'testvalue') + self.assertEqual(value, 'testvalue') return 'settervalue' class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder): @@ -236,9 +236,9 @@ class TC_20_PropertyHolder(qubes.tests.QubesTestCase): self.assertEventFired(self.holder, 'property-loaded') self.assertEventNotFired(self.holder, 'property-set:testprop1') - self.assertEquals(self.holder.testprop1, 'testvalue1') - self.assertEquals(self.holder.testprop2, 'testref2') - self.assertEquals(self.holder.testprop3, 'testdefault') + self.assertEqual(self.holder.testprop1, 'testvalue1') + self.assertEqual(self.holder.testprop2, 'testref2') + self.assertEqual(self.holder.testprop3, 'testdefault') with self.assertRaises(AttributeError): self.holder.testprop4 diff --git a/qubes/tests/integ/backup.py b/qubes/tests/integ/backup.py index 90fcfc0d..7dec4add 100644 --- a/qubes/tests/integ/backup.py +++ b/qubes/tests/integ/backup.py @@ -257,7 +257,7 @@ class BackupTestsMixin(qubes.tests.SystemTestsMixin): 'internal'): if not hasattr(vm, prop): continue - self.assertEquals( + self.assertEqual( getattr(vm, prop), getattr(restored_vm, prop), "VM {} - property {} not properly restored".format( vm.name, prop)) @@ -267,11 +267,11 @@ class BackupTestsMixin(qubes.tests.SystemTestsMixin): orig_value = getattr(vm, prop) restored_value = getattr(restored_vm, prop) if orig_value and restored_value: - self.assertEquals(orig_value.name, restored_value.name, + self.assertEqual(orig_value.name, restored_value.name, "VM {} - property {} not properly restored".format( vm.name, prop)) else: - self.assertEquals(orig_value, restored_value, + self.assertEqual(orig_value, restored_value, "VM {} - property {} not properly restored".format( vm.name, prop)) for dev_class in vm.devices.keys(): diff --git a/qubes/tests/integ/basic.py b/qubes/tests/integ/basic.py index 2689c471..dbf7ecf4 100644 --- a/qubes/tests/integ/basic.py +++ b/qubes/tests/integ/basic.py @@ -126,7 +126,7 @@ class TC_01_Properties(qubes.tests.SystemTestsMixin, qubes.tests.QubesTestCase): os.path.join(os.getenv("HOME"), ".local/share/applications", self.vmname + "-firefox.desktop"))) self.vm.firewall.load() - self.assertEquals(pre_rename_firewall, self.vm.firewall.rules) + self.assertEqual(pre_rename_firewall, self.vm.firewall.rules) with self.assertNotRaises((qubes.exc.QubesException, OSError)): self.vm.firewall.save() self.assertTrue(self.vm.autostart) @@ -171,24 +171,24 @@ class TC_01_Properties(qubes.tests.SystemTestsMixin, qubes.tests.QubesTestCase): testvm1 = self.app.domains[testvm1.qid] testvm2 = self.app.domains[testvm2.qid] - self.assertEquals(testvm1.label, testvm2.label) - self.assertEquals(testvm1.netvm, testvm2.netvm) - self.assertEquals(testvm1.property_is_default('netvm'), + self.assertEqual(testvm1.label, testvm2.label) + self.assertEqual(testvm1.netvm, testvm2.netvm) + self.assertEqual(testvm1.property_is_default('netvm'), testvm2.property_is_default('netvm')) - self.assertEquals(testvm1.kernel, testvm2.kernel) - self.assertEquals(testvm1.kernelopts, testvm2.kernelopts) - self.assertEquals(testvm1.property_is_default('kernel'), + self.assertEqual(testvm1.kernel, testvm2.kernel) + self.assertEqual(testvm1.kernelopts, testvm2.kernelopts) + self.assertEqual(testvm1.property_is_default('kernel'), testvm2.property_is_default('kernel')) - self.assertEquals(testvm1.property_is_default('kernelopts'), + self.assertEqual(testvm1.property_is_default('kernelopts'), testvm2.property_is_default('kernelopts')) - self.assertEquals(testvm1.memory, testvm2.memory) - self.assertEquals(testvm1.maxmem, testvm2.maxmem) - self.assertEquals(testvm1.devices, testvm2.devices) - self.assertEquals(testvm1.include_in_backups, + self.assertEqual(testvm1.memory, testvm2.memory) + self.assertEqual(testvm1.maxmem, testvm2.maxmem) + self.assertEqual(testvm1.devices, testvm2.devices) + self.assertEqual(testvm1.include_in_backups, testvm2.include_in_backups) - self.assertEquals(testvm1.default_user, testvm2.default_user) - self.assertEquals(testvm1.features, testvm2.features) - self.assertEquals(testvm1.firewall.rules, + self.assertEqual(testvm1.default_user, testvm2.default_user) + self.assertEqual(testvm1.features, testvm2.features) + self.assertEqual(testvm1.firewall.rules, testvm2.firewall.rules) # now some non-default values @@ -214,24 +214,24 @@ class TC_01_Properties(qubes.tests.SystemTestsMixin, qubes.tests.QubesTestCase): testvm1 = self.app.domains[testvm1.qid] testvm3 = self.app.domains[testvm3.qid] - self.assertEquals(testvm1.label, testvm3.label) - self.assertEquals(testvm1.netvm, testvm3.netvm) - self.assertEquals(testvm1.property_is_default('netvm'), + self.assertEqual(testvm1.label, testvm3.label) + self.assertEqual(testvm1.netvm, testvm3.netvm) + self.assertEqual(testvm1.property_is_default('netvm'), testvm3.property_is_default('netvm')) - self.assertEquals(testvm1.kernel, testvm3.kernel) - self.assertEquals(testvm1.kernelopts, testvm3.kernelopts) - self.assertEquals(testvm1.property_is_default('kernel'), + self.assertEqual(testvm1.kernel, testvm3.kernel) + self.assertEqual(testvm1.kernelopts, testvm3.kernelopts) + self.assertEqual(testvm1.property_is_default('kernel'), testvm3.property_is_default('kernel')) - self.assertEquals(testvm1.property_is_default('kernelopts'), + self.assertEqual(testvm1.property_is_default('kernelopts'), testvm3.property_is_default('kernelopts')) - self.assertEquals(testvm1.memory, testvm3.memory) - self.assertEquals(testvm1.maxmem, testvm3.maxmem) - self.assertEquals(testvm1.devices, testvm3.devices) - self.assertEquals(testvm1.include_in_backups, + self.assertEqual(testvm1.memory, testvm3.memory) + self.assertEqual(testvm1.maxmem, testvm3.maxmem) + self.assertEqual(testvm1.devices, testvm3.devices) + self.assertEqual(testvm1.include_in_backups, testvm3.include_in_backups) - self.assertEquals(testvm1.default_user, testvm3.default_user) - self.assertEquals(testvm1.features, testvm3.features) - self.assertEquals(testvm1.firewall.rules, + self.assertEqual(testvm1.default_user, testvm3.default_user) + self.assertEqual(testvm1.features, testvm3.features) + self.assertEqual(testvm1.firewall.rules, testvm2.firewall.rules) def test_020_name_conflict_app(self): @@ -294,7 +294,7 @@ class TC_02_QvmPrefs(qubes.tests.SystemTestsMixin, qubes.tests.QubesTestCase): ) (stdout, stderr) = p.communicate() if valid: - self.assertEquals(p.returncode, 0, + self.assertEqual(p.returncode, 0, "qvm-prefs .. '{}' '{}' failed: {}{}".format( name, value, stdout, stderr )) @@ -307,7 +307,7 @@ class TC_02_QvmPrefs(qubes.tests.SystemTestsMixin, qubes.tests.QubesTestCase): p = subprocess.Popen(['qvm-prefs'] + self.sharedopts + ['--', self.testvm.name, name], stdout=subprocess.PIPE) (stdout, _) = p.communicate() - self.assertEquals(p.returncode, 0) + self.assertEqual(p.returncode, 0) return stdout.strip() bool_test_values = [ @@ -330,7 +330,7 @@ class TC_02_QvmPrefs(qubes.tests.SystemTestsMixin, qubes.tests.QubesTestCase): for (value, expected, valid) in values: self.pref_set(name, value, valid) if valid: - self.assertEquals(self.pref_get(name), expected) + self.assertEqual(self.pref_get(name), expected) @unittest.skip('test not converted to core3 API') def test_006_template(self): @@ -421,7 +421,7 @@ class TC_03_QvmRevertTemplateChanges(qubes.tests.SystemTestsMixin, subprocess.check_call(revert_cmd) checksum_after = self.get_rootimg_checksum() - self.assertEquals(checksum_before, checksum_after) + self.assertEqual(checksum_before, checksum_after) @unittest.expectedFailure def test_000_revert_pv(self): @@ -484,12 +484,12 @@ class TC_30_Gui_daemon(qubes.tests.SystemTestsMixin, qubes.tests.QubesTestCase): clipboard_content = \ open('/var/run/qubes/qubes-clipboard.bin', 'r').read().strip() - self.assertEquals(clipboard_content, test_string, + self.assertEqual(clipboard_content, test_string, "Clipboard copy operation failed - content") clipboard_source = \ open('/var/run/qubes/qubes-clipboard.bin.source', 'r').read().strip() - self.assertEquals(clipboard_source, testvm1.name, + self.assertEqual(clipboard_source, testvm1.name, "Clipboard copy operation failed - owner") # Then paste it to the other window @@ -505,16 +505,16 @@ class TC_30_Gui_daemon(qubes.tests.SystemTestsMixin, qubes.tests.QubesTestCase): # And compare the result (test_output, _) = self.loop.run_until_complete( testvm2.run_for_stdio('cat test.txt')) - self.assertEquals(test_string, test_output.strip().decode('ascii')) + self.assertEqual(test_string, test_output.strip().decode('ascii')) clipboard_content = \ open('/var/run/qubes/qubes-clipboard.bin', 'r').read().strip() - self.assertEquals(clipboard_content, "", + self.assertEqual(clipboard_content, "", "Clipboard not wiped after paste - content") clipboard_source = \ open('/var/run/qubes/qubes-clipboard.bin.source', 'r').\ read().strip() - self.assertEquals(clipboard_source, "", + self.assertEqual(clipboard_source, "", "Clipboard not wiped after paste - owner") class TC_05_StandaloneVM(qubes.tests.SystemTestsMixin, qubes.tests.QubesTestCase): @@ -530,7 +530,7 @@ class TC_05_StandaloneVM(qubes.tests.SystemTestsMixin, qubes.tests.QubesTestCase testvm1.clone_disk_files(self.app.default_template)) self.app.save() self.loop.run_until_complete(testvm1.start()) - self.assertEquals(testvm1.get_power_state(), "Running") + self.assertEqual(testvm1.get_power_state(), "Running") @unittest.expectedFailure def test_100_resize_root_img(self): @@ -541,7 +541,7 @@ class TC_05_StandaloneVM(qubes.tests.SystemTestsMixin, qubes.tests.QubesTestCase self.app.save() self.loop.run_until_complete( testvm1.storage.resize(testvm1.volumes['root'], 20 * 1024 ** 3)) - self.assertEquals(testvm1.volumes['root'].size, 20 * 1024 ** 3) + self.assertEqual(testvm1.volumes['root'].size, 20 * 1024 ** 3) self.loop.run_until_complete(testvm1.start()) # new_size in 1k-blocks (new_size, _) = self.loop.run_until_complete( diff --git a/qubes/tests/integ/dispvm.py b/qubes/tests/integ/dispvm.py index 365833fd..2d6da892 100644 --- a/qubes/tests/integ/dispvm.py +++ b/qubes/tests/integ/dispvm.py @@ -53,7 +53,7 @@ class TC_04_DispVM(qubes.tests.SystemTestsMixin, p = self.testvm.run("qvm-run --dispvm bash", passio_popen=True) (stdout, _) = p.communicate(input=b"echo test; qubesdb-read /name; " b"echo ERROR\n") - self.assertEquals(p.returncode, 0) + self.assertEqual(p.returncode, 0) lines = stdout.decode('ascii').splitlines() self.assertEqual(lines[0], "test") dispvm_name = lines[1] @@ -84,7 +84,7 @@ class TC_04_DispVM(qubes.tests.SystemTestsMixin, time.sleep(1) timeout -= 1 # includes check for None - timeout - self.assertEquals(p.returncode, 0) + self.assertEqual(p.returncode, 0) lines = p.stdout.read().splitlines() self.assertTrue(lines, 'No output received from DispVM') dispvm_name = lines[0] diff --git a/qubes/tests/integ/vm_qrexec_gui.py b/qubes/tests/integ/vm_qrexec_gui.py index b03e5fca..79ef2014 100644 --- a/qubes/tests/integ/vm_qrexec_gui.py +++ b/qubes/tests/integ/vm_qrexec_gui.py @@ -71,7 +71,7 @@ class TC_00_AppVMMixin(qubes.tests.SystemTestsMixin): def test_000_start_shutdown(self): self.loop.run_until_complete(self.testvm1.start()) - self.assertEquals(self.testvm1.get_power_state(), "Running") + self.assertEqual(self.testvm1.get_power_state(), "Running") self.loop.run_until_complete(self.testvm1.shutdown()) shutdown_counter = 0 @@ -81,13 +81,13 @@ class TC_00_AppVMMixin(qubes.tests.SystemTestsMixin): shutdown_counter += 1 time.sleep(1) time.sleep(1) - self.assertEquals(self.testvm1.get_power_state(), "Halted") + self.assertEqual(self.testvm1.get_power_state(), "Halted") @unittest.skipUnless(spawn.find_executable('xdotool'), "xdotool not installed") def test_010_run_xterm(self): self.loop.run_until_complete(self.testvm1.start()) - self.assertEquals(self.testvm1.get_power_state(), "Running") + self.assertEqual(self.testvm1.get_power_state(), "Running") p = self.loop.run_until_complete(self.testvm1.run('xterm')) try: @@ -97,8 +97,7 @@ class TC_00_AppVMMixin(qubes.tests.SystemTestsMixin): title = 'user@host' while subprocess.call( ['xdotool', 'search', '--name', title], - stdout=open(os.path.devnull, 'w'), - stderr=subprocess.STDOUT) > 0: + stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) > 0: wait_count += 1 if wait_count > 100: self.fail("Timeout while waiting for xterm window") @@ -128,7 +127,7 @@ class TC_00_AppVMMixin(qubes.tests.SystemTestsMixin): if "minimal" in self.template: self.skipTest("Minimal template doesn't have 'gnome-terminal'") self.loop.run_until_complete(self.testvm1.start()) - self.assertEquals(self.testvm1.get_power_state(), "Running") + self.assertEqual(self.testvm1.get_power_state(), "Running") p = self.loop.run_until_complete(self.testvm1.run('gnome-terminal')) try: title = 'user@{}'.format(self.testvm1.name) @@ -167,7 +166,7 @@ class TC_00_AppVMMixin(qubes.tests.SystemTestsMixin): @unittest.expectedFailure def test_012_qubes_desktop_run(self): self.loop.run_until_complete(self.testvm1.start()) - self.assertEquals(self.testvm1.get_power_state(), "Running") + self.assertEqual(self.testvm1.get_power_state(), "Running") xterm_desktop_path = "/usr/share/applications/xterm.desktop" # Debian has it different... xterm_desktop_path_debian = \ @@ -224,7 +223,7 @@ class TC_00_AppVMMixin(qubes.tests.SystemTestsMixin): self.fail( "Timeout, probably EOF wasn't transferred to the VM process") - self.assertEquals(stdout, TEST_DATA, + self.assertEqual(stdout, TEST_DATA, 'Received data differs from what was sent') self.assertFalse(stderr, 'Some data was printed to stderr') @@ -243,7 +242,7 @@ class TC_00_AppVMMixin(qubes.tests.SystemTestsMixin): p.stdin.write(TEST_DATA) yield from p.stdin.drain() p.stdin.close() - self.assertEquals(stdout.strip(), 'test', + self.assertEqual(stdout.strip(), 'test', 'Received data differs from what was expected') # this may hang in some buggy cases self.assertFalse((yield from p.stderr.read()), @@ -280,7 +279,7 @@ class TC_00_AppVMMixin(qubes.tests.SystemTestsMixin): except asyncio.TimeoutError: self.fail("Timeout, probably EOF wasn't transferred") - self.assertEquals(stdout, b'test', + self.assertEqual(stdout, b'test', 'Received data differs from what was expected') @unittest.expectedFailure @@ -304,7 +303,7 @@ class TC_00_AppVMMixin(qubes.tests.SystemTestsMixin): except asyncio.TimeoutError: self.fail("Timeout, probably EOF wasn't transferred") - self.assertEquals(stdout, b'test', + self.assertEqual(stdout, b'test', 'Received data differs from what was expected') def test_055_qrexec_dom0_service_abort(self): diff --git a/qubes/tests/storage_file.py b/qubes/tests/storage_file.py index ca22b603..5d97d34d 100644 --- a/qubes/tests/storage_file.py +++ b/qubes/tests/storage_file.py @@ -82,7 +82,7 @@ class TC_00_FilePool(qubes.tests.QubesTestCase): """ result = self.app.get_pool("default").dir_path expected = '/var/lib/qubes' - self.assertEquals(result, expected) + self.assertEqual(result, expected) def test001_default_storage_class(self): """ Check when using default pool the Storage is @@ -244,7 +244,7 @@ class TC_01_FileVolumes(qubes.tests.QubesTestCase): volumes = vm.volumes b_dev = volumes[dev_name].block_device() self.assertEqual(b_dev.rw, rw) - self.assertEquals(b_dev.path, expected) + self.assertEqual(b_dev.path, expected) class TC_03_FilePool(qubes.tests.QubesTestCase): @@ -319,12 +319,12 @@ class TC_03_FilePool(qubes.tests.QubesTestCase): expected_vmdir = os.path.join(self.APPVMS_DIR, vm.name) expected_private_path = os.path.join(expected_vmdir, 'private.img') - self.assertEquals(vm.volumes['private'].path, expected_private_path) + self.assertEqual(vm.volumes['private'].path, expected_private_path) expected_volatile_path = os.path.join(expected_vmdir, 'volatile.img') vm.storage.get_pool(vm.volumes['volatile'])\ .reset(vm.volumes['volatile']) - self.assertEqualsAndExists(vm.volumes['volatile'].path, + self.assertEqualAndExists(vm.volumes['volatile'].path, expected_volatile_path) def test_013_template_file_images(self): @@ -353,25 +353,25 @@ class TC_03_FilePool(qubes.tests.QubesTestCase): expected_root_cow_path = os.path.join(expected_vmdir, 'root-cow.img') expected_root_path = '%s:%s' % (expected_root_origin_path, expected_root_cow_path) - self.assertEquals(vm.volumes['root'].block_device().path, + self.assertEqual(vm.volumes['root'].block_device().path, expected_root_path) self.assertExist(vm.volumes['root'].path) expected_private_path = os.path.join(expected_vmdir, 'private.img') - self.assertEqualsAndExists(vm.volumes['private'].path, + self.assertEqualAndExists(vm.volumes['private'].path, expected_private_path) expected_rootcow_path = os.path.join(expected_vmdir, 'root-cow.img') - self.assertEqualsAndExists(vm.volumes['root'].path_cow, + self.assertEqualAndExists(vm.volumes['root'].path_cow, expected_rootcow_path) - def assertEqualsAndExists(self, result_path, expected_path): + def assertEqualAndExists(self, result_path, expected_path): """ Check if the ``result_path``, matches ``expected_path`` and exists. See also: :meth:``assertExist`` """ # :pylint: disable=invalid-name - self.assertEquals(result_path, expected_path) + self.assertEqual(result_path, expected_path) self.assertExist(result_path) def assertExist(self, path): diff --git a/qubes/tests/vm/qubesvm.py b/qubes/tests/vm/qubesvm.py index 9e99240e..f7a08160 100644 --- a/qubes/tests/vm/qubesvm.py +++ b/qubes/tests/vm/qubesvm.py @@ -159,13 +159,13 @@ class QubesVMTestsMixin(object): # single exception? with self.assertNotRaises((ValueError, TypeError, KeyError)): setattr(vm, prop_name, set_value) - self.assertEquals(getattr(vm, prop_name), expected_value) + self.assertEqual(getattr(vm, prop_name), expected_value) if expected_xml_content is not None: xml = vm.__xml__() prop_xml = xml.xpath( './properties/property[@name=\'{}\']'.format(prop_name)) - self.assertEquals(len(prop_xml), 1, "Property not found in XML") - self.assertEquals(prop_xml[0].text, expected_xml_content) + self.assertEqual(len(prop_xml), 1, "Property not found in XML") + self.assertEqual(prop_xml[0].text, expected_xml_content) def assertPropertyInvalidValue(self, vm, prop_name, set_value): orig_value_set = True @@ -179,7 +179,7 @@ class QubesVMTestsMixin(object): with self.assertRaises((ValueError, TypeError, KeyError)): setattr(vm, prop_name, set_value) if orig_value_set: - self.assertEquals(getattr(vm, prop_name), orig_value) + self.assertEqual(getattr(vm, prop_name), orig_value) else: with self.assertRaises(AttributeError): getattr(vm, prop_name) @@ -191,11 +191,11 @@ class QubesVMTestsMixin(object): getattr(vm, prop_name) else: with self.assertNotRaises(AttributeError): - self.assertEquals(getattr(vm, prop_name), expected_default) + self.assertEqual(getattr(vm, prop_name), expected_default) xml = vm.__xml__() prop_xml = xml.xpath( './properties/property[@name=\'{}\']'.format(prop_name)) - self.assertEquals(len(prop_xml), 0, "Property still found in XML") + self.assertEqual(len(prop_xml), 0, "Property still found in XML") def _test_generic_bool_property(self, vm, prop_name, default=False): self.assertPropertyDefaultValue(vm, prop_name, default) diff --git a/qubes/vm/adminvm.py b/qubes/vm/adminvm.py index 88710280..75eeb5a1 100644 --- a/qubes/vm/adminvm.py +++ b/qubes/vm/adminvm.py @@ -97,9 +97,10 @@ class AdminVM(qubes.vm.qubesvm.QubesVM): ''' # return psutil.virtual_memory().total/1024 - for line in open('/proc/meminfo'): - if line.startswith('MemTotal:'): - return int(line.split(':')[1].strip().split()[0]) + with open('/proc/meminfo') as file: + for line in file: + if line.startswith('MemTotal:'): + return int(line.split(':')[1].strip().split()[0]) raise NotImplementedError() def get_mem_static_max(self):