mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Let virt running state provide errors
As mentioned in issue 47972, applying the virt.running state doesn't
report any error if the libvirt create call actually failed.
This commit introduces proper handling of the libvirt errors to let
users see the libvirt error in case of failure.
Also add test cases for virt.running to prevent regression.
(cherry picked from commit 451e7da55b
)
This commit is contained in:
parent
6bae227f77
commit
2db7a988b2
1 changed files with 40 additions and 1 deletions
|
@ -23,13 +23,29 @@ import salt.states.virt as virt
|
|||
import salt.utils.files
|
||||
|
||||
|
||||
class LibvirtMock(MagicMock): # pylint: disable=too-many-ancestors
|
||||
'''
|
||||
libvirt library mockup
|
||||
'''
|
||||
|
||||
class libvirtError(Exception): # pylint: disable=invalid-name
|
||||
'''
|
||||
libvirt error mockup
|
||||
'''
|
||||
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
class LibvirtTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''
|
||||
Test cases for salt.states.libvirt
|
||||
'''
|
||||
def setup_loader_modules(self):
|
||||
return {virt: {}}
|
||||
self.mock_libvirt = LibvirtMock() # pylint: disable=attribute-defined-outside-init
|
||||
self.addCleanup(delattr, self, 'mock_libvirt')
|
||||
loader_globals = {
|
||||
'libvirt': self.mock_libvirt
|
||||
}
|
||||
return {virt: loader_globals}
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
|
@ -195,3 +211,26 @@ class LibvirtTestCase(TestCase, LoaderModuleMockMixin):
|
|||
locality='Los_Angeles',
|
||||
organization='SaltStack',
|
||||
expiration_days=700), ret)
|
||||
|
||||
def test_running(self):
|
||||
'''
|
||||
running state test cases.
|
||||
'''
|
||||
ret = {'name': 'myvm',
|
||||
'changes': {},
|
||||
'result': True,
|
||||
'comment': 'myvm is running'}
|
||||
with patch.dict(virt.__salt__, { # pylint: disable=no-member
|
||||
'virt.vm_state': MagicMock(return_value='stopped'),
|
||||
'virt.start': MagicMock(return_value=0)
|
||||
}):
|
||||
ret.update({'changes': {'myvm': 'Domain started'},
|
||||
'comment': 'Domain myvm started'})
|
||||
self.assertDictEqual(virt.running('myvm'), ret)
|
||||
|
||||
with patch.dict(virt.__salt__, { # pylint: disable=no-member
|
||||
'virt.vm_state': MagicMock(return_value='stopped'),
|
||||
'virt.start': MagicMock(side_effect=[self.mock_libvirt.libvirtError('libvirt error msg')])
|
||||
}):
|
||||
ret.update({'changes': {}, 'result': False, 'comment': 'libvirt error msg'})
|
||||
self.assertDictEqual(virt.running('myvm'), ret)
|
||||
|
|
Loading…
Add table
Reference in a new issue