Add missing virt states unit tests

So far only the virt.running state is unit tested. Add tests for the
other states. Deprecated virt states have been purposedly left untested.

(cherry picked from commit fc75872fb6)
This commit is contained in:
Cédric Bosdonnat 2018-06-28 09:42:07 +02:00
parent 0681d865f6
commit 13d7819c7f
No known key found for this signature in database
GPG Key ID: 743CCED3EDD1578D

View File

@ -23,6 +23,9 @@ import salt.states.virt as virt
import salt.utils.files
from salt.exceptions import CommandExecutionError
# Import 3rd-party libs
from salt.ext import six
class LibvirtMock(MagicMock): # pylint: disable=too-many-ancestors
'''
@ -34,6 +37,12 @@ class LibvirtMock(MagicMock): # pylint: disable=too-many-ancestors
libvirt error mockup
'''
def get_error_message(self):
'''
Fake function return error message
'''
return six.text_type(self)
@skipIf(NO_MOCK, NO_MOCK_REASON)
class LibvirtTestCase(TestCase, LoaderModuleMockMixin):
@ -314,3 +323,335 @@ class LibvirtTestCase(TestCase, LoaderModuleMockMixin):
}):
ret.update({'changes': {}, 'result': False, 'comment': 'libvirt error msg'})
self.assertDictEqual(virt.running('myvm'), ret)
def test_stopped(self):
'''
stopped state test cases.
'''
ret = {'name': 'myvm',
'changes': {},
'result': True}
shutdown_mock = MagicMock(return_value=True)
with patch.dict(virt.__salt__, { # pylint: disable=no-member
'virt.list_domains': MagicMock(return_value=['myvm', 'vm1']),
'virt.shutdown': shutdown_mock
}):
ret.update({'changes': {
'stopped': [{'domain': 'myvm', 'shutdown': True}]
},
'comment': 'Machine has been shut down'})
self.assertDictEqual(virt.stopped('myvm'), ret)
shutdown_mock.assert_called_with('myvm', connection=None, username=None, password=None)
with patch.dict(virt.__salt__, { # pylint: disable=no-member
'virt.list_domains': MagicMock(return_value=['myvm', 'vm1']),
'virt.shutdown': shutdown_mock,
}):
self.assertDictEqual(virt.stopped('myvm',
connection='myconnection',
username='user',
password='secret'), ret)
shutdown_mock.assert_called_with('myvm', connection='myconnection', username='user', password='secret')
with patch.dict(virt.__salt__, { # pylint: disable=no-member
'virt.list_domains': MagicMock(return_value=['myvm', 'vm1']),
'virt.shutdown': MagicMock(side_effect=self.mock_libvirt.libvirtError('Some error'))
}):
ret.update({'changes': {'ignored': [{'domain': 'myvm', 'issue': 'Some error'}]},
'result': False,
'comment': 'No changes had happened'})
self.assertDictEqual(virt.stopped('myvm'), ret)
with patch.dict(virt.__salt__, {'virt.list_domains': MagicMock(return_value=[])}): # pylint: disable=no-member
ret.update({'changes': {}, 'result': False, 'comment': 'No changes had happened'})
self.assertDictEqual(virt.stopped('myvm'), ret)
def test_powered_off(self):
'''
powered_off state test cases.
'''
ret = {'name': 'myvm',
'changes': {},
'result': True}
stop_mock = MagicMock(return_value=True)
with patch.dict(virt.__salt__, { # pylint: disable=no-member
'virt.list_domains': MagicMock(return_value=['myvm', 'vm1']),
'virt.stop': stop_mock
}):
ret.update({'changes': {
'unpowered': [{'domain': 'myvm', 'stop': True}]
},
'comment': 'Machine has been powered off'})
self.assertDictEqual(virt.powered_off('myvm'), ret)
stop_mock.assert_called_with('myvm', connection=None, username=None, password=None)
with patch.dict(virt.__salt__, { # pylint: disable=no-member
'virt.list_domains': MagicMock(return_value=['myvm', 'vm1']),
'virt.stop': stop_mock,
}):
self.assertDictEqual(virt.powered_off('myvm',
connection='myconnection',
username='user',
password='secret'), ret)
stop_mock.assert_called_with('myvm', connection='myconnection', username='user', password='secret')
with patch.dict(virt.__salt__, { # pylint: disable=no-member
'virt.list_domains': MagicMock(return_value=['myvm', 'vm1']),
'virt.stop': MagicMock(side_effect=self.mock_libvirt.libvirtError('Some error'))
}):
ret.update({'changes': {'ignored': [{'domain': 'myvm', 'issue': 'Some error'}]},
'result': False,
'comment': 'No changes had happened'})
self.assertDictEqual(virt.powered_off('myvm'), ret)
with patch.dict(virt.__salt__, {'virt.list_domains': MagicMock(return_value=[])}): # pylint: disable=no-member
ret.update({'changes': {}, 'result': False, 'comment': 'No changes had happened'})
self.assertDictEqual(virt.powered_off('myvm'), ret)
def test_snapshot(self):
'''
snapshot state test cases.
'''
ret = {'name': 'myvm',
'changes': {},
'result': True}
snapshot_mock = MagicMock(return_value=True)
with patch.dict(virt.__salt__, { # pylint: disable=no-member
'virt.list_domains': MagicMock(return_value=['myvm', 'vm1']),
'virt.snapshot': snapshot_mock
}):
ret.update({'changes': {
'saved': [{'domain': 'myvm', 'snapshot': True}]
},
'comment': 'Snapshot has been taken'})
self.assertDictEqual(virt.snapshot('myvm'), ret)
snapshot_mock.assert_called_with('myvm', suffix=None, connection=None, username=None, password=None)
with patch.dict(virt.__salt__, { # pylint: disable=no-member
'virt.list_domains': MagicMock(return_value=['myvm', 'vm1']),
'virt.snapshot': snapshot_mock,
}):
self.assertDictEqual(virt.snapshot('myvm',
suffix='snap',
connection='myconnection',
username='user',
password='secret'), ret)
snapshot_mock.assert_called_with('myvm',
suffix='snap',
connection='myconnection',
username='user',
password='secret')
with patch.dict(virt.__salt__, { # pylint: disable=no-member
'virt.list_domains': MagicMock(return_value=['myvm', 'vm1']),
'virt.snapshot': MagicMock(side_effect=self.mock_libvirt.libvirtError('Some error'))
}):
ret.update({'changes': {'ignored': [{'domain': 'myvm', 'issue': 'Some error'}]},
'result': False,
'comment': 'No changes had happened'})
self.assertDictEqual(virt.snapshot('myvm'), ret)
with patch.dict(virt.__salt__, {'virt.list_domains': MagicMock(return_value=[])}): # pylint: disable=no-member
ret.update({'changes': {}, 'result': False, 'comment': 'No changes had happened'})
self.assertDictEqual(virt.snapshot('myvm'), ret)
def test_rebooted(self):
'''
rebooted state test cases.
'''
ret = {'name': 'myvm',
'changes': {},
'result': True}
reboot_mock = MagicMock(return_value=True)
with patch.dict(virt.__salt__, { # pylint: disable=no-member
'virt.list_domains': MagicMock(return_value=['myvm', 'vm1']),
'virt.reboot': reboot_mock
}):
ret.update({'changes': {
'rebooted': [{'domain': 'myvm', 'reboot': True}]
},
'comment': 'Machine has been rebooted'})
self.assertDictEqual(virt.rebooted('myvm'), ret)
reboot_mock.assert_called_with('myvm', connection=None, username=None, password=None)
with patch.dict(virt.__salt__, { # pylint: disable=no-member
'virt.list_domains': MagicMock(return_value=['myvm', 'vm1']),
'virt.reboot': reboot_mock,
}):
self.assertDictEqual(virt.rebooted('myvm',
connection='myconnection',
username='user',
password='secret'), ret)
reboot_mock.assert_called_with('myvm',
connection='myconnection',
username='user',
password='secret')
with patch.dict(virt.__salt__, { # pylint: disable=no-member
'virt.list_domains': MagicMock(return_value=['myvm', 'vm1']),
'virt.reboot': MagicMock(side_effect=self.mock_libvirt.libvirtError('Some error'))
}):
ret.update({'changes': {'ignored': [{'domain': 'myvm', 'issue': 'Some error'}]},
'result': False,
'comment': 'No changes had happened'})
self.assertDictEqual(virt.rebooted('myvm'), ret)
with patch.dict(virt.__salt__, {'virt.list_domains': MagicMock(return_value=[])}): # pylint: disable=no-member
ret.update({'changes': {}, 'result': False, 'comment': 'No changes had happened'})
self.assertDictEqual(virt.rebooted('myvm'), ret)
def test_network_running(self):
'''
network_running state test cases.
'''
ret = {'name': 'mynet', 'changes': {}, 'result': True, 'comment': ''}
define_mock = MagicMock(return_value=True)
with patch.dict(virt.__salt__, { # pylint: disable=no-member
'virt.network_info': MagicMock(return_value={}),
'virt.network_define': define_mock
}):
ret.update({'changes': {'mynet': 'Network defined and started'},
'comment': 'Network mynet defined and started'})
self.assertDictEqual(virt.network_running('mynet',
'br2',
'bridge',
vport='openvswitch',
tag=180,
autostart=False,
connection='myconnection',
username='user',
password='secret'), ret)
define_mock.assert_called_with('mynet',
'br2',
'bridge',
'openvswitch',
tag=180,
autostart=False,
start=True,
connection='myconnection',
username='user',
password='secret')
with patch.dict(virt.__salt__, { # pylint: disable=no-member
'virt.network_info': MagicMock(return_value={'active': True}),
'virt.network_define': define_mock,
}):
ret.update({'changes': {}, 'comment': 'Network mynet exists and is running'})
self.assertDictEqual(virt.network_running('mynet', 'br2', 'bridge'), ret)
start_mock = MagicMock(return_value=True)
with patch.dict(virt.__salt__, { # pylint: disable=no-member
'virt.network_info': MagicMock(return_value={'active': False}),
'virt.network_start': start_mock,
'virt.network_define': define_mock,
}):
ret.update({'changes': {'mynet': 'Network started'}, 'comment': 'Network mynet started'})
self.assertDictEqual(virt.network_running('mynet',
'br2',
'bridge',
connection='myconnection',
username='user',
password='secret'), ret)
start_mock.assert_called_with('mynet', connection='myconnection', username='user', password='secret')
with patch.dict(virt.__salt__, { # pylint: disable=no-member
'virt.network_info': MagicMock(return_value={}),
'virt.network_define': MagicMock(side_effect=self.mock_libvirt.libvirtError('Some error'))
}):
ret.update({'changes': {}, 'comment': 'Some error', 'result': False})
self.assertDictEqual(virt.network_running('mynet', 'br2', 'bridge'), ret)
def test_pool_running(self):
'''
pool_running state test cases.
'''
ret = {'name': 'mypool', 'changes': {}, 'result': True, 'comment': ''}
mocks = {mock: MagicMock(return_value=True) for mock in ['define', 'autostart', 'build', 'start']}
with patch.dict(virt.__salt__, { # pylint: disable=no-member
'virt.pool_info': MagicMock(return_value={}),
'virt.pool_define': mocks['define'],
'virt.pool_build': mocks['build'],
'virt.pool_start': mocks['start'],
'virt.pool_set_autostart': mocks['autostart']
}):
ret.update({'changes': {'mypool': 'Pool defined and started'},
'comment': 'Pool mypool defined and started'})
self.assertDictEqual(virt.pool_running('mypool',
ptype='logical',
target='/dev/base',
permissions={'mode': '0770',
'owner': 1000,
'group': 100,
'label': 'seclabel'},
source={'devices': [{'path': '/dev/sda'}]},
transient=True,
autostart=True,
connection='myconnection',
username='user',
password='secret'), ret)
mocks['define'].assert_called_with('mypool',
ptype='logical',
target='/dev/base',
permissions={'mode': '0770',
'owner': 1000,
'group': 100,
'label': 'seclabel'},
source_devices=[{'path': '/dev/sda'}],
source_dir=None,
source_adapter=None,
source_hosts=None,
source_auth=None,
source_name=None,
source_format=None,
transient=True,
start=True,
connection='myconnection',
username='user',
password='secret')
mocks['autostart'].assert_called_with('mypool',
state='on',
connection='myconnection',
username='user',
password='secret')
mocks['build'].assert_called_with('mypool',
connection='myconnection',
username='user',
password='secret')
with patch.dict(virt.__salt__, { # pylint: disable=no-member
'virt.pool_info': MagicMock(return_value={'state': 'running'}),
}):
ret.update({'changes': {}, 'comment': 'Pool mypool exists and is running'})
self.assertDictEqual(virt.pool_running('mypool',
ptype='logical',
target='/dev/base',
source={'devices': [{'path': '/dev/sda'}]}), ret)
for mock in mocks:
mocks[mock].reset_mock()
with patch.dict(virt.__salt__, { # pylint: disable=no-member
'virt.pool_info': MagicMock(return_value={'state': 'stopped'}),
'virt.pool_build': mocks['build'],
'virt.pool_start': mocks['start']
}):
ret.update({'changes': {'mypool': 'Pool started'}, 'comment': 'Pool mypool started'})
self.assertDictEqual(virt.pool_running('mypool',
ptype='logical',
target='/dev/base',
source={'devices': [{'path': '/dev/sda'}]}), ret)
mocks['start'].assert_called_with('mypool', connection=None, username=None, password=None)
mocks['build'].assert_not_called()
with patch.dict(virt.__salt__, { # pylint: disable=no-member
'virt.pool_info': MagicMock(return_value={}),
'virt.pool_define': MagicMock(side_effect=self.mock_libvirt.libvirtError('Some error'))
}):
ret.update({'changes': {}, 'comment': 'Some error', 'result': False})
self.assertDictEqual(virt.pool_running('mypool',
ptype='logical',
target='/dev/base',
source={'devices': [{'path': '/dev/sda'}]}), ret)