Acceptance tests: adds multi vm capability and basic migration test
PATCH v2 1/2 tests.acceptance: adds multi vm capability for acceptance tests
In reply to Caio CarraraThis change adds the possibility to write acceptance tests with multi virtual machine support. It's done keeping the virtual machines objects stored in a test attribute (dictionary). This dictionary shouldn't be accessed directly but through the new method added get_vm. This new method accept a list of args (that will be added as virtual machine arguments) and an optional name argument. The name is the key that identify a single virtual machine along the test machines available. If a name without a machine is informed a new machine will be instantiated.
The current usage of vm in tests will not be broken by this change since it keeps a property called vm in the base test class. This property only calls the new method get_vm with default parameters (no args and 'default' as machine name).
--- docs/devel/testing.rst | 40 ++++++++++++++++++++++- tests/acceptance/avocado_qemu/__init__.py | 25 +++++++++++--- 2 files changed, 60 insertions(+), 5 deletions(-)
docs/devel/testing.rst +39 -1
@@ -634,7 +634,45 @@ instance, available at ``self.vm``. Because many tests will tweak the QEMU command line, launching the QEMUMachine (by using ``self.vm.launch()``) is left to the test writer. -At test "tear down", ``avocado_qemu.Test`` handles the QEMUMachine +The base test class has also support for tests with more than one +QEMUMachine. The way to get machines is through the ``self.get_vm()`` +method which will return a QEMUMachine instance. The ``self.get_vm()`` +method also accepts an optional `name` attribute so you can identify a +specific machine and get it more than once through the tests methods. A +simple and hypothetical example follows: + +.. code:: + + from avocado_qemu import Test + + + class MultipleMachines(Test): + """ + :avocado: enable + """ + def test_multiple_machines(self): + first_machine = self.get_vm() + second_machine = self.get_vm() + self.get_vm(name='third_machine').launch() + + first_machine.launch() + second_machine.launch() + + first_res = first_machine.command( + 'human-monitor-command', + command_line='info version') + + second_res = second_machine.command( + 'human-monitor-command', + command_line='info version') + + third_res = self.get_vm(name='third_machine').command( + 'human-monitor-command', + command_line='info version') + + self.assertEquals(first_res, second_res, third_res) + +At test "tear down", ``avocado_qemu.Test`` handles all the QEMUMachines shutdown. QEMUMachine
tests/acceptance/avocado_qemu/__init__.py +21 -4
@@ -10,6 +10,7 @@ import os import sys +import uuid import avocado
@@ -42,13 +43,29 @@ def pick_default_qemu_bin(): class Test(avocado.Test): def setUp(self): - self.vm = None + self._vms = {} self.qemu_bin = self.params.get('qemu_bin', default=pick_default_qemu_bin()) if self.qemu_bin is None: self.cancel("No QEMU binary defined or found in the source tree") - self.vm = QEMUMachine(self.qemu_bin) + + def _new_vm(self, *args): + vm = QEMUMachine(self.qemu_bin) + if args: + vm.add_args(*args) + return vm + + @property + def vm(self): + return self.get_vm(name='default') + + def get_vm(self, *args, name=None): + if not name: + name = str(uuid.uuid4()) + if self._vms.get(name) is None: + self._vms[name] = self._new_vm(*args) + return self._vms[name] def tearDown(self): - if self.vm is not None: - self.vm.shutdown() + for vm in self._vms.values(): + vm.shutdown()
Signature · 1 line
2.20.1
v2 cover Acceptance tests: adds multi vm capability and basic migration test
This is the second attempt to add the multi vm capability to base class of acceptance tests. The difference from first version is that in this current version a simple migration test was added (done by Cleber) so the new code that is being added is properly used and we're not adding "new dead code".
There are more tests and test cases that can be added, but I'm assuming this patch is complete enough and other series can be sent after covering the other cases.
Caio Carrara (1):
tests.acceptance: adds multi vm capability for acceptance tests
Cleber Rosa (1):
Acceptance tests: add simple migration test
docs/devel/testing.rst | 40 +++++++++++++++++++- tests/acceptance/avocado_qemu/__init__.py | 25 +++++++++++-- tests/acceptance/migration.py | 45 +++++++++++++++++++++++ 3 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 tests/acceptance/migration.py
Signature · 1 line
2.20.1
PATCH v2 2/2 Acceptance tests: add simple migration test
From: Cleber Rosa <[email protected]>
This is the simplest possible migration test, exercising the multi VM capabilities of the test class.
--- tests/acceptance/migration.py | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 tests/acceptance/migration.py
tests/acceptance/migration.py new file +45 -0
@@ -0,0 +1,45 @@ +# Migration test +# +# Copyright (c) 2019 Red Hat, Inc. +# +# Author: +# Cleber Rosa <[email protected]> +# +# This work is licensed under the terms of the GNU GPL, version 2 or +# later. See the COPYING file in the top-level directory. + + +from avocado_qemu import Test + +from avocado.utils import network +from avocado.utils import wait + + +class Migration(Test): + """ + :avocado: enable + """ + + timeout = 10 + + @staticmethod + def migration_completed(vm): + cmd_result = vm.qmp('query-migrate') + if cmd_result is not None: + result = cmd_result.get('return') + if result is not None: + return result.get('status') == 'completed' + return False + + def test_tcp(self): + source = self.get_vm() + port = network.find_free_port() + if port is None: + self.cancel('Failed to find a free port') + dest_uri = 'tcp:localhost:%u' % port + dest = self.get_vm('-incoming', dest_uri) + dest.launch() + source.launch() + source.qmp('migrate', uri=dest_uri) + wait.wait_for(self.migration_completed, timeout=self.timeout, + step=0.1, args=(source,))
Signature · 1 line
2.20.1
Re: [Qemu-devel] [PATCH v2 1/2] tests.acceptance: adds multi vm capability for acceptance tests
In reply to Caio CarraraQuoted 9 lines · Caio Carrara
This change adds the possibility to write acceptance tests with multi virtual machine support. It's done keeping the virtual machines objects stored in a test attribute (dictionary). This dictionary shouldn't be accessed directly but through the new method added
get_vm. This new method accept a list of args (that will be added as virtual machine arguments) and an optional name argument. The name is the key that identify a single virtual machine along the test machines available. If a name without a machine is informed a new machine will be instantiated.
Quoted 4 lines
The current usage of vm in tests will not be broken by this change since it keeps a property called vm in the base test class. This property only calls the new method
get_vmwith default parameters (no args and 'default' as machine name).
I've checked that current tests does not break by this change. I also checked the example you provided on docs/devel/testing.rst works too.
So Tested-by: Wainer dos Santos Moschetta <[email protected]>
Quoted 12 lines
Signed-off-by Caio Carrara <[email protected]>--- docs/devel/testing.rst | 40 ++++++++++++++++++++++- tests/acceptance/avocado_qemu/__init__.py | 25 +++++++++++--- 2 files changed, 60 insertions(+), 5 deletions(-)docs/devel/testing.rst +0
-0
docs/devel/testing.rst instance, available at ``self.vm``. Because many tests will tweak the · line 642
Quoted 10 lines
docs/devel/testing.rst +6
-1@@ -634,7 +634,45 @@ instance, available at ``self.vm``. Because many tests will tweak the QEMU command line, launching the QEMUMachine (by using ``self.vm.launch()``) is left to the test writer. -At test "tear down", ``avocado_qemu.Test`` handles the QEMUMachine +The base test class has also support for tests with more than one +QEMUMachine. The way to get machines is through the ``self.get_vm()`` +method which will return a QEMUMachine instance. The ``self.get_vm()`` +method also accepts an optional `name` attribute so you can identify a +specific machine and get it more than once through the tests methods. A +simple and hypothetical example follows:
Since you explain the self.get_vm() optional name attribute, you also could mention it accepts arguments to be passed to the newly created VM.
Quoted 63 lines
docs/devel/testing.rst +33
-0+ +.. code:: + + from avocado_qemu import Test + + + class MultipleMachines(Test): + """ + :avocado: enable + """ + def test_multiple_machines(self): + first_machine = self.get_vm() + second_machine = self.get_vm() + self.get_vm(name='third_machine').launch() + + first_machine.launch() + second_machine.launch() + + first_res = first_machine.command( + 'human-monitor-command', + command_line='info version') + + second_res = second_machine.command( + 'human-monitor-command', + command_line='info version') + + third_res = self.get_vm(name='third_machine').command( + 'human-monitor-command', + command_line='info version') + + self.assertEquals(first_res, second_res, third_res) + +At test "tear down", ``avocado_qemu.Test`` handles all the QEMUMachines shutdown. QEMUMachinetests/acceptance/avocado_qemu/__init__.py +6
-2@@ -10,6 +10,7 @@ import os import sys +import uuid import avocado@@ -42,13 +43,29 @@ def pick_default_qemu_bin(): class Test(avocado.Test): def setUp(self): - self.vm = None + self._vms = {} self.qemu_bin = self.params.get('qemu_bin', default=pick_default_qemu_bin()) if self.qemu_bin is None: self.cancel("No QEMU binary defined or found in the source tree") - self.vm = QEMUMachine(self.qemu_bin) + + def _new_vm(self, *args): + vm = QEMUMachine(self.qemu_bin) + if args:
tests/acceptance/avocado_qemu/__init__.py def pick_default_qemu_bin(): · line 64
Quoted 10 lines
tests/acceptance/avocado_qemu/__init__.py +10
-0+ vm.add_args(*args) + return vm + + @property + def vm(self): + return self.get_vm(name='default') + + def get_vm(self, *args, name=None): + if not name: + name = str(uuid.uuid4())
Beware that if you don't give a name to the VM, the only way to access it later is to keep the reference returned by get_vm(). Do you think it is something we should care about? or assume the test writer handle this (unlikely?) case somehow?
- Wainer
Quoted 9 lines
tests/acceptance/avocado_qemu/__init__.py +5
-2+ if self._vms.get(name) is None: + self._vms[name] = self._new_vm(*args) + return self._vms[name] def tearDown(self): - if self.vm is not None: - self.vm.shutdown() + for vm in self._vms.values(): + vm.shutdown()
Re: [Qemu-devel] [PATCH v2 1/2] tests.acceptance: adds multi vm capability for acceptance tests
Hi, Wainer.
Quoted 111 lines · Wainer dos Santos Moschetta
Quoted 9 lines · Caio Carrara
This change adds the possibility to write acceptance tests with multi virtual machine support. It's done keeping the virtual machines objects stored in a test attribute (dictionary). This dictionary shouldn't be accessed directly but through the new method added
get_vm. This new method accept a list of args (that will be added as virtual machine arguments) and an optional name argument. The name is the key that identify a single virtual machine along the test machines available. If a name without a machine is informed a new machine will be instantiated.Quoted 4 lines
The current usage of vm in tests will not be broken by this change since it keeps a property called vm in the base test class. This property only calls the new method
get_vmwith default parameters (no args and 'default' as machine name).I've checked that current tests does not break by this change. I also checked the example you provided on
docs/devel/testing.rstworks too.So Tested-by: Wainer dos Santos Moschetta <[email protected]>
Quoted 12 lines
Signed-off-by Caio Carrara <[email protected]>--- docs/devel/testing.rst | 40 ++++++++++++++++++++++- tests/acceptance/avocado_qemu/__init__.py | 25 +++++++++++--- 2 files changed, 60 insertions(+), 5 deletions(-)docs/devel/testing.rst +0
-0docs/devel/testing.rst instance, available at ``self.vm``. Because many tests will tweak the · line 641
Quoted 9 lines
docs/devel/testing.rst +6
-1@@ -634,7 +634,45 @@ instance, available at ``self.vm``. Because many tests will tweak the QEMU command line, launching the QEMUMachine (by using ``self.vm.launch()``) is left to the test writer. -At test "tear down", ``avocado_qemu.Test`` handles the QEMUMachine +The base test class has also support for tests with more than one +QEMUMachine. The way to get machines is through the ``self.get_vm()`` +method which will return a QEMUMachine instance. The ``self.get_vm()`` +method also accepts an optional `name` attribute so you can identify a +specific machine and get it more than once through the tests methods. A +simple and hypothetical example follows:Since you explain the
self.get_vm()optional name attribute, you also could mention it accepts arguments to be passed to the newly created VM.Quoted 65 lines
docs/devel/testing.rst +33
-0+ +.. code:: + + from avocado_qemu import Test + + + class MultipleMachines(Test): + """ + :avocado: enable + """ + def test_multiple_machines(self): + first_machine = self.get_vm() + second_machine = self.get_vm() + self.get_vm(name='third_machine').launch() + + first_machine.launch() + second_machine.launch() + + first_res = first_machine.command( + 'human-monitor-command', + command_line='info version') + + second_res = second_machine.command( + 'human-monitor-command', + command_line='info version') + + third_res = self.get_vm(name='third_machine').command( + 'human-monitor-command', + command_line='info version') + + self.assertEquals(first_res, second_res, third_res) + +At test "tear down", ``avocado_qemu.Test`` handles all the QEMUMachines shutdown. QEMUMachinetests/acceptance/avocado_qemu/__init__.py +13
-2@@ -10,6 +10,7 @@ import os import sys +import uuid import avocado@@ -42,13 +43,29 @@ def pick_default_qemu_bin(): class Test(avocado.Test): def setUp(self): - self.vm = None + self._vms = {} self.qemu_bin = self.params.get('qemu_bin', default=pick_default_qemu_bin()) if self.qemu_bin is None: self.cancel("No QEMU binary defined or found in the source tree") - self.vm = QEMUMachine(self.qemu_bin) + + def _new_vm(self, *args): + vm = QEMUMachine(self.qemu_bin) + if args: + vm.add_args(*args) + return vm + + @property + def vm(self): + return self.get_vm(name='default') +
Quoted 8 lines
tests/acceptance/avocado_qemu/__init__.py def pick_default_qemu_bin(): · line 64
Quoted 3 lines
tests/acceptance/avocado_qemu/__init__.py +3
-0+ def get_vm(self, *args, name=None): + if not name: + name = str(uuid.uuid4())Beware that if you don't give a name to the VM, the only way to access it later is to keep the reference returned by
get_vm(). Do you think it is something we should care about? or assume the test writer handle this (unlikely?) case somehow?
I think it's something e should assume the test writer is going to handle.
Quoted 12 lines
- Wainer
Quoted 8 lines
tests/acceptance/avocado_qemu/__init__.py +5
-2+ if self._vms.get(name) is None: + self._vms[name] = self._new_vm(*args) + return self._vms[name] def tearDown(self): - if self.vm is not None: - self.vm.shutdown() + for vm in self._vms.values(): + vm.shutdown()
Thanks,
Signature · 3 lines
Caio Carrara Software Engineer, Virt Team - Red Hat [email protected]
Re: [Qemu-devel] [PATCH v2 1/2] tests.acceptance: adds multi vm capability for acceptance tests
Quoted 117 lines · Caio Carrara
Hi, Wainer.
Quoted 106 lines · Wainer dos Santos Moschetta
Quoted 9 lines · Caio Carrara
This change adds the possibility to write acceptance tests with multi virtual machine support. It's done keeping the virtual machines objects stored in a test attribute (dictionary). This dictionary shouldn't be accessed directly but through the new method added
get_vm. This new method accept a list of args (that will be added as virtual machine arguments) and an optional name argument. The name is the key that identify a single virtual machine along the test machines available. If a name without a machine is informed a new machine will be instantiated.Quoted 4 lines
The current usage of vm in tests will not be broken by this change since it keeps a property called vm in the base test class. This property only calls the new method
get_vmwith default parameters (no args and 'default' as machine name).I've checked that current tests does not break by this change. I also checked the example you provided on
docs/devel/testing.rstworks too.So Tested-by: Wainer dos Santos Moschetta <[email protected]>
Quoted 11 lines
Signed-off-by Caio Carrara <[email protected]>--- docs/devel/testing.rst | 40 ++++++++++++++++++++++- tests/acceptance/avocado_qemu/__init__.py | 25 +++++++++++--- 2 files changed, 60 insertions(+), 5 deletions(-)docs/devel/testing.rst +0
-0docs/devel/testing.rst instance, available at ``self.vm``. Because many tests will tweak the · line 641
Quoted 9 lines
docs/devel/testing.rst +6
-1@@ -634,7 +634,45 @@ instance, available at ``self.vm``. Because many tests will tweak the QEMU command line, launching the QEMUMachine (by using ``self.vm.launch()``) is left to the test writer. -At test "tear down", ``avocado_qemu.Test`` handles the QEMUMachine +The base test class has also support for tests with more than one +QEMUMachine. The way to get machines is through the ``self.get_vm()`` +method which will return a QEMUMachine instance. The ``self.get_vm()`` +method also accepts an optional `name` attribute so you can identify a +specific machine and get it more than once through the tests methods. A +simple and hypothetical example follows:Since you explain the
self.get_vm()optional name attribute, you also could mention it accepts arguments to be passed to the newly created VM.Quoted 64 lines
docs/devel/testing.rst +33
-0+ +.. code:: + + from avocado_qemu import Test + + + class MultipleMachines(Test): + """ + :avocado: enable + """ + def test_multiple_machines(self): + first_machine = self.get_vm() + second_machine = self.get_vm() + self.get_vm(name='third_machine').launch() + + first_machine.launch() + second_machine.launch() + + first_res = first_machine.command( + 'human-monitor-command', + command_line='info version') + + second_res = second_machine.command( + 'human-monitor-command', + command_line='info version') + + third_res = self.get_vm(name='third_machine').command( + 'human-monitor-command', + command_line='info version') + + self.assertEquals(first_res, second_res, third_res) + +At test "tear down", ``avocado_qemu.Test`` handles all the QEMUMachines shutdown. QEMUMachinetests/acceptance/avocado_qemu/__init__.py +12
-2@@ -10,6 +10,7 @@ import os import sys +import uuid import avocado@@ -42,13 +43,29 @@ def pick_default_qemu_bin(): class Test(avocado.Test): def setUp(self): - self.vm = None + self._vms = {} self.qemu_bin = self.params.get('qemu_bin', default=pick_default_qemu_bin()) if self.qemu_bin is None: self.cancel("No QEMU binary defined or found in the source tree") - self.vm = QEMUMachine(self.qemu_bin) + + def _new_vm(self, *args): + vm = QEMUMachine(self.qemu_bin) + if args: + vm.add_args(*args) + return vm + + @property + def vm(self): + return self.get_vm(name='default')Quoted 8 lines
tests/acceptance/avocado_qemu/__init__.py def pick_default_qemu_bin(): · line 64
Quoted 4 lines
tests/acceptance/avocado_qemu/__init__.py +4
-0+ + def get_vm(self, *args, name=None): + if not name: + name = str(uuid.uuid4())Beware that if you don't give a name to the VM, the only way to access it later is to keep the reference returned by
get_vm(). Do you think it is something we should care about? or assume the test writer handle this (unlikely?) case somehow?
Quoted 2 lines
I think it's something e should assume the test writer is going to handle.
Fair enough.
- Wainer
Quoted 12 lines
Quoted 10 lines
- Wainer
Quoted 8 lines
tests/acceptance/avocado_qemu/__init__.py +5
-2+ if self._vms.get(name) is None: + self._vms[name] = self._new_vm(*args) + return self._vms[name] def tearDown(self): - if self.vm is not None: - self.vm.shutdown() + for vm in self._vms.values(): + vm.shutdown()Thanks,
Re: [Qemu-devel] [PATCH v2 2/2] Acceptance tests: add simple migration test
In reply to Caio CarraraQuoted 38 lines · Caio Carrara
From: Cleber Rosa <[email protected]>
This is the simplest possible migration test, exercising the multi VM capabilities of the test class.
Signed-off-by Cleber Rosa <[email protected]>--- tests/acceptance/migration.py | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+)create mode 100644
tests/acceptance/migration.pytests/acceptance/migration.py new file +21
-0@@ -0,0 +1,45 @@ +# Migration test +# +# Copyright (c) 2019 Red Hat, Inc. +# +# Author: +# Cleber Rosa <[email protected]> +# +# This work is licensed under the terms of the GNU GPL, version 2 or +# later. See the COPYING file in the top-level directory. + + +from avocado_qemu import Test + +from avocado.utils import network +from avocado.utils import wait + + +class Migration(Test): + """ + :avocado: enable + """
tests/acceptance/migration.py line 31
Quoted 10 lines
tests/acceptance/migration.py new file +10
-0+ + timeout = 10 + + @staticmethod + def migration_completed(vm): + cmd_result = vm.qmp('query-migrate') + if cmd_result is not None: + result = cmd_result.get('return') + if result is not None: + return result.get('status') == 'completed'
You could verify other status that indicate failure on the migration to nicely fail the test, because the printed messaged on timeout does not help much to identify what went wrong.
It could be something like:
- @staticmethod
- def migration_completed(vm):
+ def migration_completed(self, vm):
cmd_result = vm.qmp('query-migrate')
if cmd_result is not None:
result = cmd_result.get('return')
if result is not None:
+ self.assertNotEqual(result.get('status'), 'failed') return result.get('status') == 'completed' return False
tests/acceptance/migration.py line 34
Quoted 3 lines
tests/acceptance/migration.py new file +3
-0+ return False + + def test_tcp(self):
What if you rename it to something like "test_with_tcp_on_localhost" to better represent the test scenario?
- Wainer
Quoted 11 lines
tests/acceptance/migration.py new file +11
-0+ source = self.get_vm() + port = network.find_free_port() + if port is None: + self.cancel('Failed to find a free port') + dest_uri = 'tcp:localhost:%u' % port + dest = self.get_vm('-incoming', dest_uri) + dest.launch() + source.launch() + source.qmp('migrate', uri=dest_uri) + wait.wait_for(self.migration_completed, timeout=self.timeout, + step=0.1, args=(source,))