diff --git a/_lastrevision b/_lastrevision index 8c71fd2..fda2b82 100644 --- a/_lastrevision +++ b/_lastrevision @@ -1 +1 @@ -0f35901e836e26f224b8fe278679334f6ea6281d \ No newline at end of file +6c4669ed512eb32e435e4bfd2cebdcba315841bb \ No newline at end of file diff --git a/salt.changes b/salt.changes index 9c14dd8..de30994 100644 --- a/salt.changes +++ b/salt.changes @@ -1,3 +1,11 @@ +------------------------------------------------------------------- +Tue Mar 17 10:35:25 UTC 2020 - Pablo Suárez Hernández + +- virt._get_domain: don't raise an exception if there is no VM + +- Added: + * virt._get_domain-don-t-raise-an-exception-if-there-i.patch + ------------------------------------------------------------------- Mon Mar 16 13:40:30 UTC 2020 - Jochen Breuer diff --git a/salt.spec b/salt.spec index 8058e3b..62d1c80 100644 --- a/salt.spec +++ b/salt.spec @@ -308,6 +308,8 @@ Patch112: use-full-option-name-instead-of-undocumented-abbrevi.patch Patch113: loader-invalidate-the-import-cachefor-extra-modules.patch # PATCH-FIX_UPSTREAM: https://github.com/saltstack/salt/pull/55814 Patch114: open-suse-2019.2.3-virt-defined-states-219.patch +# PATCH-FIX_UPSTREAM: https://github.com/saltstack/salt/pull/56392 +Patch115: virt._get_domain-don-t-raise-an-exception-if-there-i.patch BuildRoot: %{_tmppath}/%{name}-%{version}-build @@ -949,6 +951,7 @@ cp %{S:5} ./.travis.yml %patch112 -p1 %patch113 -p1 %patch114 -p1 +%patch115 -p1 %build %if 0%{?build_py2} diff --git a/virt._get_domain-don-t-raise-an-exception-if-there-i.patch b/virt._get_domain-don-t-raise-an-exception-if-there-i.patch new file mode 100644 index 0000000..46b552e --- /dev/null +++ b/virt._get_domain-don-t-raise-an-exception-if-there-i.patch @@ -0,0 +1,79 @@ +From 9496d01c4ff7f20bfbff4902ae5c33c147a0343e Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?C=C3=A9dric=20Bosdonnat?= +Date: Tue, 17 Mar 2020 11:01:48 +0100 +Subject: [PATCH] virt._get_domain: don't raise an exception if there + is no VM + +Raising an exception if there is no VM in _get_domain makes sense if +looking for some VMs, but not when listing all VMs. +--- + salt/modules/virt.py | 2 +- + tests/unit/modules/test_virt.py | 41 +++++++++++++++++++++++++++++++++ + 2 files changed, 42 insertions(+), 1 deletion(-) + +diff --git a/salt/modules/virt.py b/salt/modules/virt.py +index b44d1a65bf97a363c2fe7a871e090cb9ca957e03..46a349ef98aa78483291dcafb38b4541bf0ac247 100644 +--- a/salt/modules/virt.py ++++ b/salt/modules/virt.py +@@ -268,7 +268,7 @@ def _get_domain(conn, *vms, **kwargs): + for id_ in conn.listDefinedDomains(): + all_vms.append(id_) + +- if not all_vms: ++ if vms and not all_vms: + raise CommandExecutionError('No virtual machines found.') + + if vms: +diff --git a/tests/unit/modules/test_virt.py b/tests/unit/modules/test_virt.py +index 2d3417ce91506a1819dbe03045a89887c9d19721..2696ed9d1bb892ee539b385e32393f8f98d23a30 100644 +--- a/tests/unit/modules/test_virt.py ++++ b/tests/unit/modules/test_virt.py +@@ -3634,3 +3634,44 @@ class VirtTestCase(TestCase, LoaderModuleMockMixin): + } + }, + [backend for backend in backends if backend['name'] == 'netfs'][0]['options']) ++ ++ def test_get_domain(self): ++ ''' ++ Test the virt._get_domain function ++ ''' ++ # Tests with no VM ++ self.mock_conn.listDomainsID.return_value = [] ++ self.mock_conn.listDefinedDomains.return_value = [] ++ self.assertEqual([], virt._get_domain(self.mock_conn)) ++ self.assertRaisesRegex(CommandExecutionError, 'No virtual machines found.', ++ virt._get_domain, self.mock_conn, 'vm2') ++ ++ # Test with active and inactive VMs ++ self.mock_conn.listDomainsID.return_value = [1] ++ ++ def create_mock_vm(idx): ++ mock_vm = MagicMock() ++ mock_vm.name.return_value = 'vm{0}'.format(idx) ++ return mock_vm ++ ++ mock_vms = [create_mock_vm(idx) for idx in range(3)] ++ self.mock_conn.lookupByID.return_value = mock_vms[0] ++ self.mock_conn.listDefinedDomains.return_value = ['vm1', 'vm2'] ++ ++ self.mock_conn.lookupByName.side_effect = mock_vms ++ self.assertEqual(mock_vms, virt._get_domain(self.mock_conn)) ++ ++ self.mock_conn.lookupByName.side_effect = None ++ self.mock_conn.lookupByName.return_value = mock_vms[0] ++ self.assertEqual(mock_vms[0], virt._get_domain(self.mock_conn, inactive=False)) ++ ++ self.mock_conn.lookupByName.return_value = None ++ self.mock_conn.lookupByName.side_effect = [mock_vms[1], mock_vms[2]] ++ self.assertEqual([mock_vms[1], mock_vms[2]], virt._get_domain(self.mock_conn, active=False)) ++ ++ self.mock_conn.reset_mock() ++ self.mock_conn.lookupByName.return_value = None ++ self.mock_conn.lookupByName.side_effect = [mock_vms[1], mock_vms[2]] ++ self.assertEqual([mock_vms[1], mock_vms[2]], virt._get_domain(self.mock_conn, 'vm1', 'vm2')) ++ self.assertRaisesRegex(CommandExecutionError, 'The VM "vm2" is not present', ++ virt._get_domain, self.mock_conn, 'vm2', inactive=False) +-- +2.23.0 + +