Example #1
0
    def test_url_error_handling(self, mocker):
        request = mocker.patch.object(client, "Request").return_value
        request.open.side_effect = URLError("bad error")
        c = client.Client("https://host", "u", "p", True, "ca")

        with pytest.raises(errors.UnitError, match="request failed"):
            c.request("get", ("config", "c"))
Example #2
0
    def test_ok(self, mocker):
        c = client.Client("https://host", "u", "p", True, "ca")
        request = mocker.patch.object(c, "request")
        request.return_value = client.Response(200, "")

        c.delete(("a", "b"))

        request.assert_called_once_with("DELETE", ("a", "b"))
Example #3
0
    def test_error(self, mocker):
        c = client.Client("https://host", "u", "p", True, "ca")
        request = mocker.patch.object(c, "request")
        request.return_value = client.Response(400, "")

        with pytest.raises(errors.UnitError, match="400"):
            c.delete(("a", "b"))

        request.assert_called_once_with("DELETE", ("a", "b"))
Example #4
0
    def test_failure(self, mocker):
        c = client.Client("https://host", "u", "p", True, "ca")
        request = mocker.patch.object(c, "request")
        request.return_value = client.Response(403, "")

        with pytest.raises(errors.UnitError, match="403"):
            c.get(("a", "b"))

        request.assert_called_once_with("GET", ("a", "b"))
Example #5
0
    def test_missing(self, mocker):
        c = client.Client("https://host", "u", "p", True, "ca")
        request = mocker.patch.object(c, "request")
        request.return_value = client.Response(404, "")

        obj = c.get(("a", "b"))

        request.assert_called_once_with("GET", ("a", "b"))
        assert obj == {}
Example #6
0
    def test_ok(self, mocker):
        c = client.Client("https://host", "u", "p", True, "ca")
        request = mocker.patch.object(c, "request")
        request.return_value = client.Response(200, '{"k":"v"}')

        obj = c.get(("a", "b"))

        request.assert_called_once_with("GET", ("a", "b"))
        assert obj == dict(k="v")
Example #7
0
    def test_http_error_handling(self, mocker):
        request = mocker.patch.object(client, "Request").return_value
        request.open.side_effect = HTTPError("url", 404, "missing", {}, None)
        c = client.Client("https://host", "u", "p", True, "ca")

        r = c.request("PUT", ("config", "a/b"))

        assert r.status == 404
        assert r.data == "missing"
Example #8
0
    def test_ok_on_first_try(self, mocker):
        c = client.Client("https://host", "u", "p", True, "ca")
        request = mocker.patch.object(c, "request")
        request.side_effect = (
            client.Response(200, '{"k":"v"}'),
            Exception("Should not reach this"),
        )

        c.put(("a", "b"), dict(my=5))

        request.assert_called_once_with("PUT", ("a", "b"), dict(my=5))
Example #9
0
    def test_ip_socket_parse(self, mocker, prefix):
        request = mocker.patch.object(client, "Request")

        client.Client(prefix + "://domain.name", "u", "p", True, "ca")

        request.assert_called_once_with(
            url_username="******",
            url_password="******",
            force_basic_auth=True,
            validate_certs=True,
            ca_path="ca",
        )
Example #10
0
    def test_error_during_retry(self, mocker):
        c = client.Client("https://host", "u", "p", True, "ca")
        request = mocker.patch.object(c, "request")
        request.side_effect = (
            client.Response(404, ""),
            client.Response(403, ""),
            Exception("Should not reach this"),
        )

        with pytest.raises(errors.UnitError, match="403"):
            c.put(("a", "b"), dict(my=5))

        assert request.call_count == 2
Example #11
0
    def test_empty_path(self, mocker):
        request = mocker.patch.object(client, "Request").return_value
        request.open.return_value.getcode.return_value = 200
        request.open.return_value.read.return_value = ""
        c = client.Client("https://host", "u", "p", True, "ca")

        c.request("PUT", ())

        request.open.assert_called_once_with(
            method="PUT",
            url="https://host",
            data=None,
        )
Example #12
0
    def test_ok_using_unix_socket_with_data(self, mocker):
        request = mocker.patch.object(client, "Request").return_value
        request.open.return_value.getcode.return_value = 404
        request.open.return_value.read.return_value = '{"k": "v"}'
        c = client.Client("unix:///path", "u", "p", True, "ca")

        r = c.request("PUT", ("config", "listeners"), dict(my="data"))

        request.open.assert_called_once_with(
            method="PUT",
            url="http://localhost/config/listeners",
            data='{"my":"data"}',
        )
        assert r.status == 404
        assert r.json == dict(k="v")
Example #13
0
    def test_ok_using_ip_socket_with_data(self, mocker):
        request = mocker.patch.object(client, "Request").return_value
        request.open.return_value.getcode.return_value = 404
        request.open.return_value.read.return_value = '{"k": "v"}'
        c = client.Client("https://host", "u", "p", True, "ca")

        r = c.request("PUT", ("config", "applications"), dict(number=4))

        request.open.assert_called_once_with(
            method="PUT",
            url="https://host/config/applications",
            data='{"number":4}',
        )
        assert r.status == 404
        assert r.json == dict(k="v")
Example #14
0
    def test_ok_using_unix_socket_no_data(self, mocker):
        request = mocker.patch.object(client, "Request").return_value
        request.open.return_value.getcode.return_value = 200
        request.open.return_value.read.return_value = '{"k": "v"}'
        c = client.Client("unix:///path", "u", "p", True, "ca")

        r = c.request("GET", ("config", "routes"))

        request.open.assert_called_once_with(
            method="GET",
            url="http://localhost/config/routes",
            data=None,
        )
        assert r.status == 200
        assert r.json == dict(k="v")
Example #15
0
    def test_ok_on_third_retry(self, mocker):
        c = client.Client("https://host", "u", "p", True, "ca")
        request = mocker.patch.object(c, "request")
        request.side_effect = (
            client.Response(404, ""),
            client.Response(404, ""),
            client.Response(200, '{"k":"v"}'),
            Exception("Should not reach this"),
        )

        c.put(("a", "b", "c"), dict(my=5))

        assert request.call_count == 3
        request.assert_has_calls((
            mocker.call("PUT", ("a", "b", "c"), dict(my=5)),
            mocker.call("PUT", ("a", "b"), dict(c=dict(my=5))),
            mocker.call("PUT", ("a", ), dict(b=dict(c=dict(my=5)))),
        ))
Example #16
0
    def test_invalid_prefix(self, mocker, prefix):
        mocker.patch.object(client, "Request")

        with pytest.raises(errors.UnitError, match="start with"):
            client.Client(prefix + "some/path", None, None, False, None)
Example #17
0
    def test_unix_socket_parse(self, mocker):
        request = mocker.patch.object(client, "Request")

        client.Client("unix:///var/run/sock", "u", "p", True, "ca")

        request.assert_called_once_with(unix_socket="/var/run/sock")