コード例 #1
0
    def test_present_exists(self):
        '''
        Test to ensure that named template is present and not changed
        '''
        name = 'A Testing Template'
        ret = {'name': name, 'result': False, 'comment': '', 'changes': {}}

        def side_effect_run_query(*args):
            '''
            Differentiate between __salt__ exec module function calls with different parameters.
            '''
            if args[0] == 'template.get':
                return EXISTING_OBJ
            elif args[0] == 'application.get':
                return ['non-empty']

        with patch.dict(zabbix_template.__opts__, {'test': False}):
            with patch.dict(
                    zabbix_template.__salt__, {
                        'zabbix.get_zabbix_id_mapper':
                        MagicMock(return_value={'template': 'templateid'}),
                        'zabbix.substitute_params':
                        MagicMock(side_effect=SUBSTITUTE_PARAMS_EXISTS),
                        'zabbix.run_query':
                        MagicMock(side_effect=side_effect_run_query),
                        'zabbix.compare_params':
                        MagicMock(return_value={
                            'new': {},
                            'old': {}
                        })
                    }):
                ret['result'] = True
                ret['comment'] = 'Zabbix Template "{0}" already exists and corresponds to a definition.'.format(
                    name)
                self.assertDictEqual(zabbix_template.present(name, {}), ret)
コード例 #2
0
    def test_present_create(self):
        '''
        Test to ensure that named template is created
        '''
        name = 'A Testing Template'
        ret = {'name': name, 'result': False, 'comment': '', 'changes': {}}

        def side_effect_run_query(*args):
            '''
            Differentiate between __salt__ exec module function calls with different parameters.
            '''
            if args[0] == 'template.get':
                return []
            elif args[0] == 'template.create':
                return {'templateids': ['10206']}
            elif args[0] == 'application.get':
                return []
            elif args[0] == 'application.create':
                return {"applicationids": ["701"]}

        with patch.dict(zabbix_template.__opts__, {'test': False}):
            with patch.dict(zabbix_template.__salt__,
                            {'zabbix.get_zabbix_id_mapper': MagicMock(return_value={'template': 'templateid'}),
                             'zabbix.substitute_params': MagicMock(side_effect=SUBSTITUTE_PARAMS_CREATE),
                             'zabbix.run_query': MagicMock(side_effect=side_effect_run_query),
                             'zabbix.compare_params': MagicMock(return_value={})}):
                ret['result'] = True
                ret['comment'] = 'Zabbix Template "{0}" created.'.format(name)
                ret['changes'] = {name: {'old': 'Zabbix Template "{0}" did not exist.'.format(name),
                                         'new': 'Zabbix Template "{0}" created according definition.'.format(name)}}
                self.assertDictEqual(zabbix_template.present(name, {}), ret)
コード例 #3
0
    def test_present_update(self):
        '''
        Test to ensure that named template is present but must be updated
        '''
        name = 'A Testing Template'
        ret = {'name': name, 'result': False, 'comment': '', 'changes': {}}

        def side_effect_run_query(*args):
            '''
            Differentiate between __salt__ exec module function calls with different parameters.
            '''
            if args[0] == 'template.get':
                return ['length of result is 1 = template exists']
            elif args[0] == 'template.update':
                return DIFF_PARAMS

        with patch.dict(zabbix_template.__opts__, {'test': False}):
            with patch.dict(zabbix_template.__salt__,
                            {'zabbix.get_zabbix_id_mapper': MagicMock(return_value={'template': 'templateid'}),
                             'zabbix.substitute_params': MagicMock(side_effect=SUBSTITUTE_PARAMS_UPDATE),
                             'zabbix.run_query': MagicMock(side_effect=side_effect_run_query),
                             'zabbix.compare_params': MagicMock(return_value=DIFF_PARAMS)}):
                ret['result'] = True
                ret['comment'] = 'Zabbix Template "{0}" updated.'.format(name)
                ret['changes'] = {name: {'old': 'Zabbix Template "{0}" differed.'.format(name),
                                         'new': 'Zabbix Template "{0}" updated according definition.'.format(name)}}
                self.assertDictEqual(zabbix_template.present(name, {}), ret)
コード例 #4
0
def test_present_create(substitute_params_create):
    """
    Test to ensure that named template is created
    """
    with patch("salt.states.zabbix_template.CHANGE_STACK", []):
        name = "A Testing Template"
        ret = {"name": name, "result": False, "comment": "", "changes": {}}

        def side_effect_run_query(*args):
            """
            Differentiate between __salt__ exec module function calls with different parameters.
            """
            if args[0] in ("template.get", "application.get"):
                return []
            elif args[0] == "template.create":
                return {"templateids": ["10206"]}
            elif args[0] == "application.create":
                return {"applicationids": ["701"]}

        with patch.dict(zabbix_template.__opts__, {"test": False}):
            with patch.dict(
                    zabbix_template.__salt__,
                {
                    "zabbix.get_zabbix_id_mapper":
                    MagicMock(return_value={"template": "templateid"}),
                    "zabbix.substitute_params":
                    MagicMock(side_effect=substitute_params_create),
                    "zabbix.run_query":
                    MagicMock(side_effect=side_effect_run_query),
                    "zabbix.compare_params":
                    MagicMock(return_value={}),
                },
            ):
                ret["result"] = True
                ret["comment"] = 'Zabbix Template "{}" created.'.format(name)
                ret["changes"] = {
                    name: {
                        "old":
                        'Zabbix Template "{}" did not exist.'.format(name),
                        "new":
                        ('Zabbix Template "{}" created according definition.'.
                         format(name)),
                    }
                }
                assert zabbix_template.present(name, {}) == ret
コード例 #5
0
def test_present_update(diff_params, substitute_params_update):
    """
    Test to ensure that named template is present but must be updated
    """
    with patch("salt.states.zabbix_template.CHANGE_STACK", []):
        name = "A Testing Template"
        ret = {"name": name, "result": False, "comment": "", "changes": {}}

        def side_effect_run_query(*args):
            """
            Differentiate between __salt__ exec module function calls with different parameters.
            """
            if args[0] == "template.get":
                return ["length of result is 1 = template exists"]
            elif args[0] == "template.update":
                return diff_params

        with patch.dict(zabbix_template.__opts__, {"test": False}):
            with patch.dict(
                    zabbix_template.__salt__,
                {
                    "zabbix.get_zabbix_id_mapper":
                    MagicMock(return_value={"template": "templateid"}),
                    "zabbix.substitute_params":
                    MagicMock(side_effect=substitute_params_update),
                    "zabbix.run_query":
                    MagicMock(side_effect=side_effect_run_query),
                    "zabbix.compare_params":
                    MagicMock(return_value=diff_params),
                },
            ):
                ret["result"] = True
                ret["comment"] = 'Zabbix Template "{}" updated.'.format(name)
                ret["changes"] = {
                    name: {
                        "old":
                        'Zabbix Template "{}" differed.'.format(name),
                        "new":
                        ('Zabbix Template "{}" updated according definition.'.
                         format(name)),
                    }
                }
                assert zabbix_template.present(name, {}) == ret
コード例 #6
0
def test_present_exists(existing_obj, substitute_params_exists):
    """
    Test to ensure that named template is present and not changed
    """
    with patch("salt.states.zabbix_template.CHANGE_STACK", []):
        name = "A Testing Template"
        ret = {"name": name, "result": False, "comment": "", "changes": {}}

        def side_effect_run_query(*args):
            """
            Differentiate between __salt__ exec module function calls with different parameters.
            """
            if args[0] == "template.get":
                return existing_obj
            elif args[0] == "application.get":
                return ["non-empty"]

        with patch.dict(zabbix_template.__opts__, {"test": False}):
            with patch.dict(
                    zabbix_template.__salt__,
                {
                    "zabbix.get_zabbix_id_mapper":
                    MagicMock(return_value={"template": "templateid"}),
                    "zabbix.substitute_params":
                    MagicMock(side_effect=substitute_params_exists),
                    "zabbix.run_query":
                    MagicMock(side_effect=side_effect_run_query),
                    "zabbix.compare_params":
                    MagicMock(return_value={
                        "new": {},
                        "old": {}
                    }),
                },
            ):
                ret["result"] = True
                ret["comment"] = (
                    'Zabbix Template "{}" already exists and corresponds to a'
                    " definition.".format(name))
                assert zabbix_template.present(name, {}) == ret