当前位置: 首页>>代码示例>>Python>>正文


Python MagicMock.configure_mock方法代码示例

本文整理汇总了Python中salttesting.mock.MagicMock.configure_mock方法的典型用法代码示例。如果您正苦于以下问题:Python MagicMock.configure_mock方法的具体用法?Python MagicMock.configure_mock怎么用?Python MagicMock.configure_mock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在salttesting.mock.MagicMock的用法示例。


在下文中一共展示了MagicMock.configure_mock方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: SnapperTestCase

# 需要导入模块: from salttesting.mock import MagicMock [as 别名]
# 或者: from salttesting.mock.MagicMock import configure_mock [as 别名]
class SnapperTestCase(TestCase):
    def setUp(self):
        self.dbus_mock = MagicMock()
        self.DBusExceptionMock = MagicMock()  # pylint: disable=invalid-name
        self.dbus_mock.configure_mock(DBusException=self.DBusExceptionMock)
        snapper.dbus = self.dbus_mock
        snapper.snapper = MagicMock()

    def test__snapshot_to_data(self):
        data = snapper._snapshot_to_data(DBUS_RET['ListSnapshots'][0])  # pylint: disable=protected-access
        self.assertEqual(data['id'], 42)
        self.assertNotIn('pre', data)
        self.assertEqual(data['type'], 'pre')
        self.assertEqual(data['user'], 'root')
        self.assertEqual(data['timestamp'], 1457006571)
        self.assertEqual(data['description'], 'Some description')
        self.assertEqual(data['cleanup'], '')
        self.assertEqual(data['userdata']['userdata1'], 'userval1')

    @patch('salt.modules.snapper.snapper.ListSnapshots', MagicMock(return_value=DBUS_RET['ListSnapshots']))
    def test_list_snapshots(self):
        self.assertEqual(snapper.list_snapshots(), MODULE_RET["SNAPSHOTS"])

    @patch('salt.modules.snapper.snapper.GetSnapshot', MagicMock(return_value=DBUS_RET['ListSnapshots'][0]))
    def test_get_snapshot(self):
        self.assertEqual(snapper.get_snapshot(), MODULE_RET["SNAPSHOTS"][0])
        self.assertEqual(snapper.get_snapshot(number=42), MODULE_RET["SNAPSHOTS"][0])
        self.assertNotEqual(snapper.get_snapshot(number=42), MODULE_RET["SNAPSHOTS"][1])

    @patch('salt.modules.snapper.snapper.ListConfigs', MagicMock(return_value=DBUS_RET['ListConfigs']))
    def test_list_configs(self):
        self.assertEqual(snapper.list_configs(), MODULE_RET["LISTCONFIGS"])

    @patch('salt.modules.snapper.snapper.GetConfig', MagicMock(return_value=DBUS_RET['ListConfigs'][0]))
    def test_get_config(self):
        self.assertEqual(snapper.get_config(), DBUS_RET["ListConfigs"][0])

    @patch('salt.modules.snapper.snapper.SetConfig', MagicMock())
    def test_set_config(self):
        opts = {'sync_acl': True, 'dummy': False, 'foobar': 1234}
        self.assertEqual(snapper.set_config(opts), True)

    def test_status_to_string(self):
        self.assertEqual(snapper.status_to_string(1), ["created"])
        self.assertEqual(snapper.status_to_string(2), ["deleted"])
        self.assertEqual(snapper.status_to_string(4), ["type changed"])
        self.assertEqual(snapper.status_to_string(8), ["modified"])
        self.assertEqual(snapper.status_to_string(16), ["permission changed"])
        self.assertListEqual(snapper.status_to_string(24), ["modified", "permission changed"])
        self.assertEqual(snapper.status_to_string(32), ["owner changed"])
        self.assertEqual(snapper.status_to_string(64), ["group changed"])
        self.assertListEqual(snapper.status_to_string(97), ["created", "owner changed", "group changed"])
        self.assertEqual(snapper.status_to_string(128), ["extended attributes changed"])
        self.assertEqual(snapper.status_to_string(256), ["ACL info changed"])

    @patch('salt.modules.snapper.snapper.CreateSingleSnapshot', MagicMock(return_value=1234))
    @patch('salt.modules.snapper.snapper.CreatePreSnapshot', MagicMock(return_value=1234))
    @patch('salt.modules.snapper.snapper.CreatePostSnapshot', MagicMock(return_value=1234))
    def test_create_snapshot(self):
        for snapshot_type in ['pre', 'post', 'single']:
            opts = {
                '__pub_jid': 20160607130930720112,
                'type': snapshot_type,
                'description': 'Test description',
                'cleanup_algorithm': 'number',
                'pre_number': 23,
            }
            self.assertEqual(snapper.create_snapshot(**opts), 1234)

    @patch('salt.modules.snapper._get_last_snapshot', MagicMock(return_value={'id': 42}))
    def test__get_num_interval(self):
        self.assertEqual(snapper._get_num_interval(config=None, num_pre=None, num_post=None), (42, 0))  # pylint: disable=protected-access
        self.assertEqual(snapper._get_num_interval(config=None, num_pre=None, num_post=50), (42, 50))  # pylint: disable=protected-access
        self.assertEqual(snapper._get_num_interval(config=None, num_pre=42, num_post=50), (42, 50))  # pylint: disable=protected-access

    def test_run(self):
        patch_dict = {
            'snapper.create_snapshot': MagicMock(return_value=43),
            'test.ping': MagicMock(return_value=True),
        }
        with patch.dict(snapper.__salt__, patch_dict):
            self.assertEqual(snapper.run("test.ping"), True)
            self.assertRaises(CommandExecutionError, snapper.run, "unknown.func")

    @patch('salt.modules.snapper._get_num_interval', MagicMock(return_value=(42, 43)))
    @patch('salt.modules.snapper.snapper.GetComparison', MagicMock())
    @patch('salt.modules.snapper.snapper.GetFiles', MagicMock(return_value=DBUS_RET['GetFiles']))
    def test_status(self):
        self.assertItemsEqual(snapper.status(), MODULE_RET['GETFILES'])
        self.assertItemsEqual(snapper.status(num_pre="42", num_post=43), MODULE_RET['GETFILES'])
        self.assertItemsEqual(snapper.status(num_pre=42), MODULE_RET['GETFILES'])
        self.assertItemsEqual(snapper.status(num_post=43), MODULE_RET['GETFILES'])

    @patch('salt.modules.snapper.status', MagicMock(return_value=MODULE_RET['GETFILES']))
    def test_changed_files(self):
        self.assertEqual(snapper.changed_files(), MODULE_RET['GETFILES'].keys())

    @patch('salt.modules.snapper._get_num_interval', MagicMock(return_value=(42, 43)))
    @patch('salt.modules.snapper.status', MagicMock(return_value=MODULE_RET['GETFILES']))
    def test_undo(self):
#.........这里部分代码省略.........
开发者ID:SUSE,项目名称:salt-snapper-module,代码行数:103,代码来源:snapper_test.py


注:本文中的salttesting.mock.MagicMock.configure_mock方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。