本文整理汇总了Python中salttesting.mock.MagicMock.return_value方法的典型用法代码示例。如果您正苦于以下问题:Python MagicMock.return_value方法的具体用法?Python MagicMock.return_value怎么用?Python MagicMock.return_value使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类salttesting.mock.MagicMock
的用法示例。
在下文中一共展示了MagicMock.return_value方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_add_volume_bricks
# 需要导入模块: from salttesting.mock import MagicMock [as 别名]
# 或者: from salttesting.mock.MagicMock import return_value [as 别名]
def test_add_volume_bricks(self):
'''
Test to add brick(s) to an existing volume
'''
name = 'salt'
bricks = ['host1:/drive1']
old_bricks = ['host1:/drive2']
ret = {'name': name,
'result': False,
'comment': '',
'changes': {}}
stopped_volinfo = {'salt': {'status': '0'}}
volinfo = {
'salt': {
'status': '1',
'bricks': {'brick1': {'path': old_bricks[0]}}
}
}
new_volinfo = {
'salt': {
'status': '1',
'bricks': {
'brick1': {'path': old_bricks[0]},
'brick2': {'path': bricks[0]}
}
}
}
mock_info = MagicMock(return_value={})
mock_add = MagicMock(side_effect=[False, True])
with patch.dict(glusterfs.__salt__,
{'glusterfs.info': mock_info,
'glusterfs.add_volume_bricks': mock_add}):
ret.update({'comment': 'Volume salt does not exist'})
self.assertDictEqual(glusterfs.add_volume_bricks(name, bricks), ret)
mock_info.return_value = stopped_volinfo
ret.update({'comment': 'Volume salt is not started'})
self.assertDictEqual(glusterfs.add_volume_bricks(name, bricks), ret)
mock_info.return_value = volinfo
ret.update({'comment': 'Adding bricks to volume salt failed'})
self.assertDictEqual(glusterfs.add_volume_bricks(name, bricks), ret)
ret.update({'result': True})
ret.update({'comment': 'Bricks already added in volume salt'})
self.assertDictEqual(glusterfs.add_volume_bricks(name, old_bricks),
ret)
mock_info.side_effect = [volinfo, new_volinfo]
ret.update({'comment': 'Bricks successfully added to volume salt',
'changes': {'new': bricks + old_bricks,
'old': old_bricks}})
self.assertDictEqual(glusterfs.add_volume_bricks(name, bricks), ret)
示例2: test_peer
# 需要导入模块: from salttesting.mock import MagicMock [as 别名]
# 或者: from salttesting.mock.MagicMock import return_value [as 别名]
def test_peer(self):
'''
Test if gluster peer call is successful.
'''
mock_run = MagicMock()
with patch.dict(glusterfs.__salt__, {'cmd.run': mock_run}):
mock_run.return_value = xml_peer_probe_already_member
self.assertTrue(glusterfs.peer('salt'))
mock_run.return_value = xml_peer_probe_localhost
self.assertTrue(glusterfs.peer('salt'))
mock_run.return_value = xml_peer_probe_fail_cant_connect
self.assertFalse(glusterfs.peer('salt'))
示例3: test_delete_volume
# 需要导入模块: from salttesting.mock import MagicMock [as 别名]
# 或者: from salttesting.mock.MagicMock import return_value [as 别名]
def test_delete_volume(self):
'''
Test if it deletes a gluster volume.
'''
mock_info = MagicMock(return_value={'Newvolume1': {'status': '1'}})
with patch.object(glusterfs, 'info', mock_info):
# volume doesn't exist
self.assertFalse(glusterfs.delete_volume('Newvolume3'))
mock_stop_volume = MagicMock(return_value=True)
mock_run = MagicMock(return_value=xml_command_success)
with patch.dict(glusterfs.__salt__, {'cmd.run': mock_run}):
with patch.object(glusterfs, 'stop_volume', mock_stop_volume):
# volume exists, should not be stopped, and is started
self.assertFalse(glusterfs.delete_volume('Newvolume1',
False))
self.assertFalse(mock_run.called)
self.assertFalse(mock_stop_volume.called)
# volume exists, should be stopped, and is started
self.assertTrue(glusterfs.delete_volume('Newvolume1'))
self.assertTrue(mock_run.called)
self.assertTrue(mock_stop_volume.called)
# volume exists and isn't started
mock_info = MagicMock(return_value={'Newvolume1': {'status': '2'}})
with patch.object(glusterfs, 'info', mock_info):
mock_run = MagicMock(return_value=xml_command_success)
with patch.dict(glusterfs.__salt__, {'cmd.run': mock_run}):
self.assertTrue(glusterfs.delete_volume('Newvolume1'))
mock_run.return_value = xml_command_fail
self.assertFalse(glusterfs.delete_volume('Newvolume1'))
示例4: test_create_volume
# 需要导入模块: from salttesting.mock import MagicMock [as 别名]
# 或者: from salttesting.mock.MagicMock import return_value [as 别名]
def test_create_volume(self):
'''
Test if it creates a glusterfs volume.
'''
mock_run = MagicMock(return_value=xml_command_success)
with patch.dict(glusterfs.__salt__, {'cmd.run': mock_run}):
self.assertRaises(
SaltInvocationError,
glusterfs.create_volume,
'newvolume',
'host1:brick')
self.assertRaises(
SaltInvocationError,
glusterfs.create_volume,
'newvolume',
'host1/brick')
self.assertFalse(mock_run.called)
mock_start_volume = MagicMock(return_value=True)
with patch.object(glusterfs, 'start_volume', mock_start_volume):
# Create, do not start
self.assertTrue(glusterfs.create_volume('newvolume',
'host1:/brick'))
self.assertFalse(mock_start_volume.called)
# Create and start
self.assertTrue(glusterfs.create_volume('newvolume',
'host1:/brick',
start=True))
self.assertTrue(mock_start_volume.called)
mock_start_volume.return_value = False
# Create and fail start
self.assertFalse(glusterfs.create_volume('newvolume',
'host1:/brick',
start=True))
mock_run.return_value = xml_command_fail
self.assertFalse(glusterfs.create_volume('newvolume', 'host1:/brick',
True, True, True, 'tcp', True))
示例5: test_started
# 需要导入模块: from salttesting.mock import MagicMock [as 别名]
# 或者: from salttesting.mock.MagicMock import return_value [as 别名]
def test_started(self):
'''
Test to check if volume has been started
'''
name = 'salt'
ret = {'name': name,
'result': False,
'comment': '',
'changes': {}}
started_info = {name: {'status': '1'}}
stopped_info = {name: {'status': '0'}}
mock_info = MagicMock(return_value={})
mock_start = MagicMock(return_value=True)
with patch.dict(glusterfs.__salt__,
{'glusterfs.info': mock_info,
'glusterfs.start_volume': mock_start}):
comt = ('Volume {0} does not exist'.format(name))
ret.update({'comment': comt})
self.assertDictEqual(glusterfs.started(name), ret)
mock_info.return_value = started_info
comt = ('Volume {0} is already started'.format(name))
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(glusterfs.started(name), ret)
with patch.dict(glusterfs.__opts__, {'test': True}):
mock_info.return_value = stopped_info
comt = ('Volume {0} will be started'.format(name))
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(glusterfs.started(name), ret)
with patch.dict(glusterfs.__opts__, {'test': False}):
comt = 'Volume {0} is started'.format(name)
ret.update({'comment': comt, 'result': True,
'change': {'new': 'started', 'old': 'stopped'}})
self.assertDictEqual(glusterfs.started(name), ret)
示例6: mock_open
# 需要导入模块: from salttesting.mock import MagicMock [as 别名]
# 或者: from salttesting.mock.MagicMock import return_value [as 别名]
def mock_open(data=None):
'''
Mock "open" function in a simple way.
:param data:
:return:
'''
data = StringIO(data)
mock = MagicMock(spec=file)
handle = MagicMock(spec=file)
handle.write.return_value = None
handle.__enter__.return_value = data or handle
mock.return_value = handle
return mock
示例7: test_peered
# 需要导入模块: from salttesting.mock import MagicMock [as 别名]
# 或者: from salttesting.mock.MagicMock import return_value [as 别名]
def test_peered(self):
'''
Test to verify if node is peered.
'''
name = 'server1'
ret = {'name': name,
'result': True,
'comment': '',
'changes': {}}
mock_ip = MagicMock(return_value=['1.2.3.4', '1.2.3.5'])
mock_hostbyname = MagicMock(return_value='1.2.3.5')
mock_peer = MagicMock(return_value=True)
mock_status = MagicMock(return_value={'uuid1': {'hostnames': [name]}})
with patch.dict(glusterfs.__salt__, {'glusterfs.peer_status': mock_status,
'glusterfs.peer': mock_peer,
'network.ip_addrs': mock_ip}):
with patch.object(socket, 'gethostbyname', mock_hostbyname):
comt = 'Peering with localhost is not needed'
ret.update({'comment': comt})
self.assertDictEqual(glusterfs.peered(name), ret)
mock_hostbyname.return_value = '1.2.3.42'
comt = ('Host {0} already peered'.format(name))
ret.update({'comment': comt})
self.assertDictEqual(glusterfs.peered(name), ret)
with patch.dict(glusterfs.__opts__, {'test': False}):
old = {'uuid1': {'hostnames': ['other1']}}
new = {'uuid1': {'hostnames': ['other1']},
'uuid2': {'hostnames': ['someAlias', name]}}
mock_status.side_effect = [old, new]
comt = 'Host {0} successfully peered'.format(name)
ret.update({'comment': comt,
'changes': {'old': old, 'new': new}})
self.assertDictEqual(glusterfs.peered(name), ret)
mock_status.side_effect = None
mock_status.return_value = {
'uuid1': {'hostnames': ['other']}
}
mock_peer.return_value = False
ret.update({'result': False})
comt = ('Failed to peer with {0},'
+ ' please check logs for errors').format(name)
ret.update({'comment': comt, 'changes': {}})
self.assertDictEqual(glusterfs.peered(name), ret)
comt = ('Invalid characters in peer name.')
ret.update({'comment': comt, 'name': ':/'})
self.assertDictEqual(glusterfs.peered(':/'), ret)
ret.update({'name': name})
with patch.dict(glusterfs.__opts__, {'test': True}):
comt = ('Peer {0} will be added.'.format(name))
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(glusterfs.peered(name), ret)
示例8: test_volume_present
# 需要导入模块: from salttesting.mock import MagicMock [as 别名]
# 或者: from salttesting.mock.MagicMock import return_value [as 别名]
def test_volume_present(self):
'''
Test to ensure that a volume exists
'''
name = 'salt'
bricks = ['host1:/brick1']
ret = {'name': name,
'result': True,
'comment': '',
'changes': {}}
started_info = {name: {'status': '1'}}
stopped_info = {name: {'status': '0'}}
mock_info = MagicMock()
mock_list = MagicMock()
mock_create = MagicMock()
mock_start = MagicMock(return_value=True)
with patch.dict(glusterfs.__salt__, {
'glusterfs.info': mock_info,
'glusterfs.list_volumes': mock_list,
'glusterfs.create_volume': mock_create,
'glusterfs.start_volume': mock_start}):
with patch.dict(glusterfs.__opts__, {'test': False}):
mock_list.return_value = [name]
mock_info.return_value = started_info
comt = ('Volume {0} already exists and is started'.format(name))
ret.update({'comment': comt})
self.assertDictEqual(glusterfs.volume_present(name, bricks,
start=True), ret)
mock_info.return_value = stopped_info
comt = ('Volume {0} already exists and is now started'.format(name))
ret.update({'comment': comt,
'changes': {'old': 'stopped', 'new': 'started'}})
self.assertDictEqual(glusterfs.volume_present(name, bricks,
start=True), ret)
comt = ('Volume {0} already exists'.format(name))
ret.update({'comment': comt, 'changes': {}})
self.assertDictEqual(glusterfs.volume_present(name, bricks,
start=False), ret)
with patch.dict(glusterfs.__opts__, {'test': True}):
comt = ('Volume {0} already exists'.format(name))
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(glusterfs.volume_present(name, bricks,
start=False), ret)
comt = ('Volume {0} already exists'
+ ' and will be started').format(name)
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(glusterfs.volume_present(name, bricks,
start=True), ret)
mock_list.return_value = []
comt = ('Volume {0} will be created'.format(name))
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(glusterfs.volume_present(name, bricks,
start=False), ret)
comt = ('Volume {0} will be created'
+ ' and started').format(name)
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(glusterfs.volume_present(name, bricks,
start=True), ret)
with patch.dict(glusterfs.__opts__, {'test': False}):
mock_list.side_effect = [[], [name]]
comt = ('Volume {0} is created'.format(name))
ret.update({'comment': comt,
'result': True,
'changes': {'old': [], 'new': [name]}})
self.assertDictEqual(glusterfs.volume_present(name, bricks,
start=False), ret)
mock_list.side_effect = [[], [name]]
comt = ('Volume {0} is created and is now started'.format(name))
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(glusterfs.volume_present(name, bricks,
start=True), ret)
mock_list.side_effect = None
mock_list.return_value = []
mock_create.return_value = False
comt = 'Creation of volume {0} failed'.format(name)
ret.update({'comment': comt, 'result': False, 'changes': {}})
self.assertDictEqual(glusterfs.volume_present(name, bricks),
ret)
with patch.object(salt.utils.cloud, 'check_name',
MagicMock(return_value=True)):
comt = ('Invalid characters in volume name.')
ret.update({'comment': comt, 'result': False})
self.assertDictEqual(glusterfs.volume_present(name, bricks),
ret)