Beispiel #1
0
    def test_present_current_object_differ(self, mocker):
        client = mocker.Mock()
        client.get.side_effect = (
            http.Response(200, '{"spec": {"current": "data"}}'),
            http.Response(200, '{"spec": {"new": "data"}}'),
        )
        client.put.return_value = http.Response(201, "")

        changed, object = datastore.sync(
            "present",
            client,
            "/list",
            "/resource",
            {"spec": {
                "my": "data"
            }},
            False,
        )

        assert changed is True
        assert {"new": "data"} == object
        client.put.assert_called_once_with(
            "/resource",
            {"spec": {
                "my": "data"
            }},
        )
Beispiel #2
0
    def test_update_groups(self, mocker):
        client = mocker.Mock()
        client.put.side_effect = [
            http.Response(201, ''),
            http.Response(201, ''),
        ]
        client.delete.side_effect = [
            http.Response(204, ''),
            http.Response(204, ''),
        ]

        result = user.update_groups(
            client,
            '/path',
            ['a', 'b', 'c'],
            ['e', 'd', 'c'],
            False,
        )

        assert result is True
        client.put.assert_has_calls([
            mocker.call('/path/groups/d', None),
            mocker.call('/path/groups/e', None),
        ],
                                    any_order=True)
        client.delete.assert_has_calls([
            mocker.call('/path/groups/a'),
            mocker.call('/path/groups/b'),
        ],
                                       any_order=True)
Beispiel #3
0
    def test_disabled_current_object_present_check(self, mocker):
        client = mocker.Mock()
        client.get.return_value = http.Response(200, '{"current": "data"}')
        client.delete.return_value = http.Response(204, '')

        changed, object = user.sync({}, 'disabled', client, '/path', {}, True)
        assert changed is True
        assert object is not None
        client.delete.assert_not_called()
Beispiel #4
0
    def test_absent_current_object_present_check(self, mocker):
        client = mocker.Mock()
        client.get.return_value = http.Response(200, '{}')
        client.delete.return_value = http.Response(204, "")

        changed, object = utils.sync("absent", client, "/path", {}, True)

        assert changed is True
        assert object is None
        client.delete.assert_not_called()
Beispiel #5
0
    def test_no_current_object(self, mocker):
        client = mocker.Mock()
        client.get.return_value = http.Response(200, '{"new": "data"}')
        client.put.return_value = http.Response(201, '')

        changed, object = user.sync(None, 'disabled', client, '/path',
                                    {'my': 'data'}, False)

        assert changed is True
        assert {'new': 'data'} == object
        client.put.assert_called_once_with('/path', {'my': 'data'})
Beispiel #6
0
    def test_remote_and_desired_not_equal(self, mocker):
        client = mocker.Mock()
        client.get.side_effect = (
            http.Response(200, '{"opt_out": "false"}'),
            http.Response(200, '{"opt_out": "true"}'),
        )
        client.put.return_value = http.Response(200, "")
        changed, object = tessen.sync(client, "/path", {'opt_out': True}, False)

        assert changed is True
        assert object == {'opt_out': 'true'}
        client.put.assert_called_once_with("/path", {'opt_out': True})
Beispiel #7
0
    def test_present_no_current_object(self, mocker):
        client = mocker.Mock()
        client.get.side_effect = (
            http.Response(404, ""),
            http.Response(200, '{"new": "data"}'),
        )
        client.put.return_value = http.Response(201, "")

        changed, object = utils.sync(
            "present", client, "/path", {"my": "data"}, False,
        )

        assert changed is True
        assert {"new": "data"} == object
        client.put.assert_called_once_with("/path", {"my": "data"})
Beispiel #8
0
    def test_request_no_payload_token(self, mocker):
        request = mocker.patch.object(http, "request")
        request.side_effect = (
            http.Response(200, '{"access_token": "token"}'),
            http.Response(200, "data"),
        )

        client.Client("http://example.com/", "user", "pass", None).request(
            "PUT", "/path",
        )

        request.assert_called_with(
            "PUT", "http://example.com/path", payload=None,
            headers=dict(Authorization="Bearer token"),
        )
Beispiel #9
0
    def test_request_no_payload_api_key(self, mocker):
        request = mocker.patch.object(http, "request")
        request.side_effect = (
            http.Response(200, "{}"),
            http.Response(200, "data"),
        )

        client.Client("http://example.com/", "u", "p", "key").request(
            "PUT", "/path",
        )

        request.assert_called_with(
            "PUT", "http://example.com/path", payload=None,
            headers=dict(Authorization="Key key"),
        )
Beispiel #10
0
    def test_abort_on_invalid_status(self, mocker, status):
        client = mocker.Mock()
        client.delete.return_value = http.Response(status, "")

        with pytest.raises(errors.SyncError, match=str(status)):
            utils.delete(client, "/delete")
        client.delete.assert_called_once_with("/delete")
Beispiel #11
0
    def test_abort_on_invalid_json(self, mocker):
        client = mocker.Mock()
        client.get.return_value = http.Response(200, "")

        with pytest.raises(errors.SyncError, match="JSON"):
            utils.get(client, "/get")
        client.get.assert_called_once_with("/get")
Beispiel #12
0
    def test_get_check_404(self, mocker):
        client = mocker.Mock()
        client.get.return_value = http.Response(404, '')

        with pytest.raises(errors.SyncError,
                           match="Check with name 'check' does not exist on remote."):
            event.get_check(client, 'default', 'check')
Beispiel #13
0
    def test_remote_and_desired_equal_check(self, mocker):
        client = mocker.Mock()
        client.get.return_value = http.Response(200, '{}')
        changed, object = tessen.sync(client, "/path", {}, True)

        assert changed is False
        assert object == {}
Beispiel #14
0
    def test_abort_on_invalid_status(self, mocker, status):
        client = mocker.Mock()
        client.put.return_value = http.Response(status, "")

        with pytest.raises(errors.SyncError, match=str(status)):
            utils.put(client, "/put", {"payload": "data"})
        client.put.assert_called_once_with("/put", {"payload": "data"})
Beispiel #15
0
    def test_broken_backend(self, mocker):
        request = mocker.patch.object(http, "request")
        request.return_value = http.Response(500, None)
        c = client.Client("http://example.com/", "user", "pass", None)

        with pytest.raises(errors.SensuError, match="500"):
            c.validate_auth_data("check_user", "check_pass")
Beispiel #16
0
    def test_valid_delete(self, mocker):
        client = mocker.Mock()
        client.delete.return_value = http.Response(204, "{}")

        object = utils.delete(client, "/delete")

        assert object is None
        client.delete.assert_called_once_with("/delete")
Beispiel #17
0
    def test_valid_json(self, mocker):
        client = mocker.Mock()
        client.get.return_value = http.Response(200, '{"get": "data"}')

        object = utils.get(client, "/get")

        assert {"get": "data"} == object
        client.get.assert_called_once_with("/get")
Beispiel #18
0
    def test_login_failure_token_missing_token(self, mocker):
        request = mocker.patch.object(http, "request")
        request.return_value = http.Response(200, '{"access_bla": "token"}')

        with pytest.raises(errors.SensuError, match="token"):
            client.Client(
                "http://example.com/", "user", "pass", None,
            ).auth_header
Beispiel #19
0
    def test_valid_put(self, mocker, status):
        client = mocker.Mock()
        client.put.return_value = http.Response(status, '{"put": "data"}')

        object = utils.put(client, "/put", {"payload": "data"})

        assert object is None
        client.put.assert_called_once_with("/put", {"payload": "data"})
Beispiel #20
0
    def test_valid_version(self, mocker):
        c = client.Client("http://example.com/", "u", "p", None, True, None)
        mocker.patch.object(c, "get").return_value = http.Response(
            200,
            '{"sensu_backend":"5.21.0#sha-here"}',
        )

        assert c.version == "5.21.0"
Beispiel #21
0
    def test_absent_current_object_present(self, mocker):
        client = mocker.Mock()
        client.get.return_value = http.Response(200, '{}')
        client.delete.return_value = http.Response(204, "")

        changed, object = datastore.sync(
            "absent",
            client,
            "/list",
            "/resource",
            {},
            False,
        )

        assert changed is True
        assert object is None
        client.delete.assert_called_with("/resource")
Beispiel #22
0
    def test_login_failure_api_key_bad_status(self, mocker):
        request = mocker.patch.object(http, "request")
        request.return_value = http.Response(401, "{}")

        with pytest.raises(errors.SensuError, match="API key .+ invalid"):
            client.Client(
                "http://example.com/", "user", "pass", "12345-xxxx-abcde",
            ).auth_header
Beispiel #23
0
    def test_absent_no_current_object_check(self, mocker):
        client = mocker.Mock()
        client.get.return_value = http.Response(404, "")

        changed, object = utils.sync("absent", client, "/path", {}, True)

        assert changed is False
        assert object is None
Beispiel #24
0
    def test_invalid_version(self, mocker):
        c = client.Client("http://example.com/", "u", "p", None, True, None)
        mocker.patch.object(c, "get").return_value = http.Response(
            200,
            '{"sensu_backend":"devel"}',
        )

        assert c.version == c.BAD_VERSION
Beispiel #25
0
    def test_remote_and_desired_not_equal_check(self, mocker):
        client = mocker.Mock()
        client.get.return_value = http.Response(200, '{"opt_out": "false"}')
        changed, object = tessen.sync(client, "/path", {'opt_out': True}, True)

        assert changed is True
        assert object == {'opt_out': True}
        client.put.assert_not_called()
Beispiel #26
0
    def test_ignore_invalid_json_on_404(self, mocker):
        client = mocker.Mock()
        client.get.return_value = http.Response(404, "")

        object = utils.get(client, "/get")

        assert object is None
        client.get.assert_called_once_with("/get")
Beispiel #27
0
    def test_login_failure_token_bad_json(self, mocker):
        request = mocker.patch.object(http, "request")
        request.return_value = http.Response(200, "{ not a json }")

        with pytest.raises(errors.SensuError, match="JSON"):
            client.Client(
                "http://example.com/", "user", "pass", None,
            ).auth_header
Beispiel #28
0
    def test_non_200_response(self, mocker):
        c = client.Client("http://example.com/", "u", "p", None, True, None)
        mocker.patch.object(c, "get").return_value = http.Response(
            400,
            '{"sensu_backend":"5.21.0#sha-here"}',
        )

        with pytest.raises(errors.SensuError, match="400"):
            c.version
Beispiel #29
0
    def test_bad_json_response(self, mocker):
        c = client.Client("http://example.com/", "u", "p", None, True, None)
        mocker.patch.object(c, "get").return_value = http.Response(
            200,
            '"sensu_backend',
        )

        with pytest.raises(errors.SensuError, match="JSON"):
            c.version
Beispiel #30
0
    def test_cache_token(self, mocker):
        request = mocker.patch.object(http, "request")
        request.return_value = http.Response(200, '{"access_token": "token"}')

        c = client.Client("http://example.com/", "user", "pass")
        for i in range(5):
            c.token

        assert 1 == request.call_count