Ejemplo n.º 1
0
    def test_search_template_present(self):
        '''
        Test to manage a elasticsearch search template.
        '''
        name = 'foo'

        template = {"template": '{"test": "key"}'}

        ret = {'name': name,
               'result': True,
               'comment': 'Search template foo is already present',
               'changes': {}}

        mock_get = MagicMock(side_effect=[template, template, None, None, None, template, CommandExecutionError, None])
        mock_create = MagicMock(side_effect=[True, True, False, CommandExecutionError])

        with patch.dict(elasticsearch.__salt__, {'elasticsearch.search_template_get': mock_get,
                                             'elasticsearch.search_template_create': mock_create}):
            self.assertDictEqual(elasticsearch.search_template_present(name, {"test": "key"}), ret)

            ret.update({'comment': "Successfully replaced search template foo", 'changes': {'old': {"test": "key"}, 'new': {"test2": "key"}}})
            self.assertDictEqual(elasticsearch.search_template_present(name, {"test2": "key"}), ret)

            ret.update({'comment': "Successfully created search template foo", 'changes': {'new': {"test2": "key"}}})
            self.assertDictEqual(elasticsearch.search_template_present(name, {"test2": "key"}), ret)

            ret.update({'comment': 'Cannot create search template foo, False', 'result': False})
            self.assertDictEqual(elasticsearch.search_template_present(name, {"test2": "key"}), ret)

            with patch.dict(elasticsearch.__opts__, {'test': True}):
                ret.update({'comment': "Search template foo does not exist and will be created", 'result': None, 'changes': {'new': {"test2": "key"}}})
                self.assertDictEqual(elasticsearch.search_template_present(name, {"test2": "key"}), ret)

                ret.update({'comment': "Search template foo exists with wrong configuration and will be overriden", 'result': None, 'changes': {'old': {"test": "key"}, 'new': {"test2": "key"}}})
                self.assertDictEqual(elasticsearch.search_template_present(name, {"test2": "key"}), ret)

            ret.update({'comment': '', 'result': False, 'changes': {}})
            self.assertDictEqual(elasticsearch.search_template_present(name, {}), ret)

            ret.update({'comment': '', 'result': False, 'changes': {}})
            self.assertDictEqual(elasticsearch.search_template_present(name, {}), ret)
Ejemplo n.º 2
0
def test_search_template_present():
    """
    Test to manage a elasticsearch search template.
    """
    name = "foo"

    template = {"template": '{"test": "key"}'}

    ret = {
        "name": name,
        "result": True,
        "comment": "Search template foo is already present",
        "changes": {},
    }

    mock_get = MagicMock(side_effect=[
        template,
        template,
        None,
        None,
        None,
        template,
        CommandExecutionError,
        None,
    ])
    mock_create = MagicMock(
        side_effect=[True, True, False, CommandExecutionError])

    with patch.dict(
            elasticsearch.__salt__,
        {
            "elasticsearch.search_template_get": mock_get,
            "elasticsearch.search_template_create": mock_create,
        },
    ):
        assert elasticsearch.search_template_present(name,
                                                     {"test": "key"}) == ret

        ret.update({
            "comment": "Successfully replaced search template foo",
            "changes": {
                "old": {
                    "test": "key"
                },
                "new": {
                    "test2": "key"
                }
            },
        })
        assert elasticsearch.search_template_present(name,
                                                     {"test2": "key"}) == ret

        ret.update({
            "comment": "Successfully created search template foo",
            "changes": {
                "new": {
                    "test2": "key"
                }
            },
        })
        assert elasticsearch.search_template_present(name,
                                                     {"test2": "key"}) == ret

        ret.update({
            "comment": "Cannot create search template foo, False",
            "result": False
        })
        assert elasticsearch.search_template_present(name,
                                                     {"test2": "key"}) == ret

        with patch.dict(elasticsearch.__opts__, {"test": True}):
            ret.update({
                "comment":
                "Search template foo does not exist and will be created",
                "result": None,
                "changes": {
                    "new": {
                        "test2": "key"
                    }
                },
            })
            assert elasticsearch.search_template_present(
                name, {"test2": "key"}) == ret

            ret.update({
                "comment":
                ("Search template foo exists with wrong configuration and will"
                 " be overridden"),
                "result":
                None,
                "changes": {
                    "old": {
                        "test": "key"
                    },
                    "new": {
                        "test2": "key"
                    }
                },
            })
            assert elasticsearch.search_template_present(
                name, {"test2": "key"}) == ret

        ret.update({"comment": "", "result": False, "changes": {}})
        assert elasticsearch.search_template_present(name, {}) == ret

        ret.update({"comment": "", "result": False, "changes": {}})
        assert elasticsearch.search_template_present(name, {}) == ret