Skip to contentSkip to conversation

iotests: More Python 3 fixes

PATCH In review Kevin Wolf 3 participants 8 messages Nov 20, 2018

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

cover iotests: More Python 3 fixes

Kevin Wolf (2):
iotests: Replace time.clock() with Timeout
iotests: Replace assertEquals() with assertEqual()

 tests/qemu-iotests/041        |  6 +++---
 tests/qemu-iotests/118        | 20 ++++++++------------
 tests/qemu-iotests/iotests.py |  2 +-
 3 files changed, 12 insertions(+), 16 deletions(-)
Signature · 1 line
2.19.1
author
#1 lore raw

PATCH 1/2 iotests: Replace time.clock() with Timeout

time.clock() is deprecated since Python 3.3. Current Python versions warn that the function will be removed in Python 3.8, and those warnings make the test case 118 fail.

Replace it with the Timeout mechanism that is compatible with both Python 2 and 3, and makes the code even a little nicer.

---
 tests/qemu-iotests/118 | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)
tests/qemu-iotests/118 +6 -10
index ff3b2ae3e7..c4f4c213ca 100755
@@ -53,21 +53,17 @@ class ChangeBaseClass(iotests.QMPTestCase):
         if not self.has_real_tray:
             return
 
-        timeout = time.clock() + 3
-        while not self.has_opened and time.clock() < timeout:
-            self.process_events()
-        if not self.has_opened:
-            self.fail('Timeout while waiting for the tray to open')
+        with iotests.Timeout(3, 'Timeout while waiting for the tray to open'):
+            while not self.has_opened:
+                self.process_events()
 
     def wait_for_close(self):
         if not self.has_real_tray:
             return
 
-        timeout = time.clock() + 3
-        while not self.has_closed and time.clock() < timeout:
-            self.process_events()
-        if not self.has_opened:
-            self.fail('Timeout while waiting for the tray to close')
+        with iotests.Timeout(3, 'Timeout while waiting for the tray to close'):
+            while not self.has_closed:
+                self.process_events()
 
 class GeneralChangeTestsBaseClass(ChangeBaseClass):
 
Signature · 1 line
2.19.1
author
#4 lore raw

PATCH 2/2 iotests: Replace assertEquals() with assertEqual()

In reply to Kevin Wolf

TestCase.assertEquals() is deprecated since Python 2.7. Recent Python versions print a warning when the function is called, which makes test cases fail.

Replace it with the preferred spelling assertEqual().

---
 tests/qemu-iotests/041        | 6 +++---
 tests/qemu-iotests/118        | 4 ++--
 tests/qemu-iotests/iotests.py | 2 +-
 3 files changed, 6 insertions(+), 6 deletions(-)
tests/qemu-iotests/041 +3 -3
index 3615011d98..26bf1701eb 100755
@@ -469,7 +469,7 @@ new_state = "1"
             self.assert_qmp(event, 'data/id', 'drive0')
             event = self.vm.get_qmp_event(wait=True)
 
-        self.assertEquals(event['event'], 'BLOCK_JOB_ERROR')
+        self.assertEqual(event['event'], 'BLOCK_JOB_ERROR')
         self.assert_qmp(event, 'data/device', 'drive0')
         self.assert_qmp(event, 'data/operation', 'read')
         result = self.vm.qmp('query-block-jobs')
@@ -494,7 +494,7 @@ new_state = "1"
             self.assert_qmp(event, 'data/id', 'drive0')
             event = self.vm.get_qmp_event(wait=True)
 
-        self.assertEquals(event['event'], 'BLOCK_JOB_ERROR')
+        self.assertEqual(event['event'], 'BLOCK_JOB_ERROR')
         self.assert_qmp(event, 'data/device', 'drive0')
         self.assert_qmp(event, 'data/operation', 'read')
         result = self.vm.qmp('query-block-jobs')
@@ -625,7 +625,7 @@ new_state = "1"
         self.assert_qmp(result, 'return', {})
 
         event = self.vm.event_wait(name='BLOCK_JOB_ERROR')
-        self.assertEquals(event['event'], 'BLOCK_JOB_ERROR')
+        self.assertEqual(event['event'], 'BLOCK_JOB_ERROR')
         self.assert_qmp(event, 'data/device', 'drive0')
         self.assert_qmp(event, 'data/operation', 'write')
         result = self.vm.qmp('query-block-jobs')
tests/qemu-iotests/118 +2 -2
index c4f4c213ca..603e10e8a2 100755
@@ -261,7 +261,7 @@ class GeneralChangeTestsBaseClass(ChangeBaseClass):
         result = self.vm.qmp('blockdev-close-tray', id=self.device_name)
         # Should be a no-op
         self.assert_qmp(result, 'return', {})
-        self.assertEquals(self.vm.get_qmp_events(wait=False), [])
+        self.assertEqual(self.vm.get_qmp_events(wait=False), [])
 
     def test_remove_on_closed(self):
         if not self.has_real_tray:
@@ -448,7 +448,7 @@ class TestChangeReadOnly(ChangeBaseClass):
                                                        read_only_mode='retain')
         self.assert_qmp(result, 'error/class', 'GenericError')
 
-        self.assertEquals(self.vm.get_qmp_events(wait=False), [])
+        self.assertEqual(self.vm.get_qmp_events(wait=False), [])
 
         result = self.vm.qmp('query-block')
         self.assert_qmp(result, 'return[0]/inserted/ro', False)
tests/qemu-iotests/iotests.py +1 -1
index 27bb2b600c..d537538ba0 100644
@@ -581,7 +581,7 @@ class QMPTestCase(unittest.TestCase):
     def wait_ready_and_cancel(self, drive='drive0'):
         self.wait_ready(drive=drive)
         event = self.cancel_and_wait(drive=drive)
-        self.assertEquals(event['event'], 'BLOCK_JOB_COMPLETED')
+        self.assertEqual(event['event'], 'BLOCK_JOB_COMPLETED')
         self.assert_qmp(event, 'data/type', 'mirror')
         self.assert_qmp(event, 'data/offset', event['data']['len'])
 
Signature · 1 line
2.19.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] iotests: Replace time.clock() with Timeout

In reply to Kevin Wolf
Quoted 8 lines · Kevin Wolf

time.clock() is deprecated since Python 3.3. Current Python versions warn that the function will be removed in Python 3.8, and those warnings make the test case 118 fail.

Replace it with the Timeout mechanism that is compatible with both Python 2 and 3, and makes the code even a little nicer.

Quoted 37 lines
---
  tests/qemu-iotests/118 | 16 ++++++----------
  1 file changed, 6 insertions(+), 10 deletions(-)
tests/qemu-iotests/118 +6 -10
index ff3b2ae3e7..c4f4c213ca 100755
@@ -53,21 +53,17 @@ class ChangeBaseClass(iotests.QMPTestCase):
          if not self.has_real_tray:
              return
 
-        timeout = time.clock() + 3
-        while not self.has_opened and time.clock() < timeout:
-            self.process_events()
-        if not self.has_opened:
-            self.fail('Timeout while waiting for the tray to open')
+        with iotests.Timeout(3, 'Timeout while waiting for the tray to open'):
+            while not self.has_opened:
+                self.process_events()
 
      def wait_for_close(self):
          if not self.has_real_tray:
              return
 
-        timeout = time.clock() + 3
-        while not self.has_closed and time.clock() < timeout:
-            self.process_events()
-        if not self.has_opened:
-            self.fail('Timeout while waiting for the tray to close')
+        with iotests.Timeout(3, 'Timeout while waiting for the tray to close'):
+            while not self.has_closed:
+                self.process_events()
 
  class GeneralChangeTestsBaseClass(ChangeBaseClass):
 
Reviewed-by on v1 2/2
#5 lore raw

Re: [Qemu-devel] [PATCH 2/2] iotests: Replace assertEquals() with assertEqual()

In reply to Kevin Wolf
Quoted 7 lines · Kevin Wolf

TestCase.assertEquals() is deprecated since Python 2.7. Recent Python versions print a warning when the function is called, which makes test cases fail.

Replace it with the preferred spelling assertEqual().

Quoted 73 lines
---
  tests/qemu-iotests/041        | 6 +++---
  tests/qemu-iotests/118        | 4 ++--
  tests/qemu-iotests/iotests.py | 2 +-
  3 files changed, 6 insertions(+), 6 deletions(-)
tests/qemu-iotests/041 +3 -3
index 3615011d98..26bf1701eb 100755
@@ -469,7 +469,7 @@ new_state = "1"
              self.assert_qmp(event, 'data/id', 'drive0')
              event = self.vm.get_qmp_event(wait=True)
 
-        self.assertEquals(event['event'], 'BLOCK_JOB_ERROR')
+        self.assertEqual(event['event'], 'BLOCK_JOB_ERROR')
          self.assert_qmp(event, 'data/device', 'drive0')
          self.assert_qmp(event, 'data/operation', 'read')
          result = self.vm.qmp('query-block-jobs')
@@ -494,7 +494,7 @@ new_state = "1"
              self.assert_qmp(event, 'data/id', 'drive0')
              event = self.vm.get_qmp_event(wait=True)
 
-        self.assertEquals(event['event'], 'BLOCK_JOB_ERROR')
+        self.assertEqual(event['event'], 'BLOCK_JOB_ERROR')
          self.assert_qmp(event, 'data/device', 'drive0')
          self.assert_qmp(event, 'data/operation', 'read')
          result = self.vm.qmp('query-block-jobs')
@@ -625,7 +625,7 @@ new_state = "1"
          self.assert_qmp(result, 'return', {})
 
          event = self.vm.event_wait(name='BLOCK_JOB_ERROR')
-        self.assertEquals(event['event'], 'BLOCK_JOB_ERROR')
+        self.assertEqual(event['event'], 'BLOCK_JOB_ERROR')
          self.assert_qmp(event, 'data/device', 'drive0')
          self.assert_qmp(event, 'data/operation', 'write')
          result = self.vm.qmp('query-block-jobs')
tests/qemu-iotests/118 +2 -2
index c4f4c213ca..603e10e8a2 100755
@@ -261,7 +261,7 @@ class GeneralChangeTestsBaseClass(ChangeBaseClass):
          result = self.vm.qmp('blockdev-close-tray', id=self.device_name)
          # Should be a no-op
          self.assert_qmp(result, 'return', {})
-        self.assertEquals(self.vm.get_qmp_events(wait=False), [])
+        self.assertEqual(self.vm.get_qmp_events(wait=False), [])
 
      def test_remove_on_closed(self):
          if not self.has_real_tray:
@@ -448,7 +448,7 @@ class TestChangeReadOnly(ChangeBaseClass):
                                                         read_only_mode='retain')
          self.assert_qmp(result, 'error/class', 'GenericError')
 
-        self.assertEquals(self.vm.get_qmp_events(wait=False), [])
+        self.assertEqual(self.vm.get_qmp_events(wait=False), [])
 
          result = self.vm.qmp('query-block')
          self.assert_qmp(result, 'return[0]/inserted/ro', False)
tests/qemu-iotests/iotests.py +1 -1
index 27bb2b600c..d537538ba0 100644
@@ -581,7 +581,7 @@ class QMPTestCase(unittest.TestCase):
      def wait_ready_and_cancel(self, drive='drive0'):
          self.wait_ready(drive=drive)
          event = self.cancel_and_wait(drive=drive)
-        self.assertEquals(event['event'], 'BLOCK_JOB_COMPLETED')
+        self.assertEqual(event['event'], 'BLOCK_JOB_COMPLETED')
          self.assert_qmp(event, 'data/type', 'mirror')
          self.assert_qmp(event, 'data/offset', event['data']['len'])
 
Reviewed-by on v1 1/2
#3 lore raw

Re: [Qemu-devel] [Qemu-block] [PATCH 1/2] iotests: Replace time.clock() with Timeout

In reply to Kevin Wolf
Quoted 35 lines · Kevin Wolf

time.clock() is deprecated since Python 3.3. Current Python versions warn that the function will be removed in Python 3.8, and those warnings make the test case 118 fail.

Replace it with the Timeout mechanism that is compatible with both Python 2 and 3, and makes the code even a little nicer.

---
 tests/qemu-iotests/118 | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)
tests/qemu-iotests/118 +3 -6
index ff3b2ae3e7..c4f4c213ca 100755
@@ -53,21 +53,17 @@ class ChangeBaseClass(iotests.QMPTestCase):
         if not self.has_real_tray:
             return
 
-        timeout = time.clock() + 3
-        while not self.has_opened and time.clock() < timeout:
-            self.process_events()
-        if not self.has_opened:
-            self.fail('Timeout while waiting for the tray to open')
+        with iotests.Timeout(3, 'Timeout while waiting for the tray to open'):
+            while not self.has_opened:
+                self.process_events()
 
     def wait_for_close(self):
         if not self.has_real_tray:
             return
 
-        timeout = time.clock() + 3

tests/qemu-iotests/118 class ChangeBaseClass(iotests.QMPTestCase): · line 69

Quoted 10 lines
tests/qemu-iotests/118 +3 -4
-        while not self.has_closed and time.clock() < timeout:
-            self.process_events()
-        if not self.has_opened:
-            self.fail('Timeout while waiting for the tray to close')
+        with iotests.Timeout(3, 'Timeout while waiting for the tray to close'):
+            while not self.has_closed:
+                self.process_events()
 
 class GeneralChangeTestsBaseClass(ChangeBaseClass):
 

I love the way that reads. Very cool!

Reviewed-by on v1 2/2
#6 lore raw

Re: [Qemu-devel] [Qemu-block] [PATCH 2/2] iotests: Replace assertEquals() with assertEqual()

In reply to Kevin Wolf
Quoted 7 lines · Kevin Wolf

TestCase.assertEquals() is deprecated since Python 2.7. Recent Python versions print a warning when the function is called, which makes test cases fail.

Replace it with the preferred spelling assertEqual().

author
#7 lore raw
In reply to Kevin Wolf

Am 20.11.2018 um 18:22 hat Kevin Wolf geschrieben:

Quoted 3 lines

Kevin Wolf (2):
iotests: Replace time.clock() with Timeout
iotests: Replace assertEquals() with assertEqual()

Thanks for the quick reviews, applied to the block branch.

Kevin