Ejemplo n.º 1
0
def a_HttpDrainMethod_object(context):
    context.drain_method = drain_lib.HTTPDrainMethod(
        service="service",
        instance="instance",
        registrations=["one", "two"],
        drain={
            "url_format": "http://{host}:{port}/drain?nerve_ns={nerve_ns}",
            "method": "GET",
            "success_codes": 200,
        },
        stop_draining={
            "url_format":
            "http://{host}:{port}/drain/stop?nerve_ns={nerve_ns}",
            "method": "GET",
            "success_codes": 200,
        },
        is_draining={
            "url_format":
            "http://{host}:{port}/drain/status?nerve_ns={nerve_ns}",
            "method": "GET",
            "success_codes": 200,
        },
        is_safe_to_kill={
            "url_format":
            "http://{host}:{port}/drain/safe_to_kill?nerve_ns={nerve_ns}",
            "method": "GET",
            "success_codes": 200,
        },
    )
Ejemplo n.º 2
0
 def test_format_url(self):
     drain_method = drain_lib.HTTPDrainMethod(
         "fake_service", "fake_instance", ["fake_nerve_ns"], {}, {}, {}, {}
     )
     url_format = "foo_{host}"
     format_params = {"host": "fake_host"}
     assert drain_method.format_url(url_format, format_params) == "foo_fake_host"
Ejemplo n.º 3
0
    async def test_issue_request(self):
        drain_method = drain_lib.HTTPDrainMethod('fake_service', 'fake_instance', ['fake_nerve_ns'], {}, {}, {}, {})
        fake_task = mock.Mock(host='fake_host', ports=[54321])
        url_spec = {
            'url_format': 'http://localhost:654321/fake/{host}',
            'method': 'get',
            'success_codes': '1234',
        }

        fake_resp = mock.Mock(status=1234)
        mock_request = mock.Mock(
            return_value=asynctest.CoroutineMock(return_value=fake_resp)(),
        )
        with mock_ClientSession(request=mock_request):
            await drain_method.issue_request(
                url_spec=url_spec,
                task=fake_task,
            )

        mock_request.assert_called_once_with(
            method='GET',
            url='http://localhost:654321/fake/fake_host',
            headers=mock.ANY,
            timeout=15,
        )
Ejemplo n.º 4
0
    def test_check_response_code(self):
        drain_method = drain_lib.HTTPDrainMethod('fake_service', 'fake_instance', ['fake_nerve_ns'], {}, {}, {}, {})

        # Happy case
        drain_method.check_response_code(200, '200-299')

        # Sad case
        with raises(drain_lib.StatusCodeNotAcceptableError):
            drain_method.check_response_code(500, '200-299')
Ejemplo n.º 5
0
 def test_get_format_params(self):
     fake_task = mock.Mock(host="fake_host", ports=[54321])
     drain_method = drain_lib.HTTPDrainMethod('fake_service', 'fake_instance', ['fake_nerve_ns'], {}, {}, {}, {})
     assert drain_method.get_format_params(fake_task) == [{
         'host': 'fake_host',
         'port': 54321,
         'service': 'fake_service',
         'instance': 'fake_instance',
         'nerve_ns': 'fake_nerve_ns',
     }]
Ejemplo n.º 6
0
 def test_parse_success_codes(self):
     drain_method = drain_lib.HTTPDrainMethod('fake_service',
                                              'fake_instance',
                                              'fake_nerve_ns', {}, {}, {},
                                              {})
     assert drain_method.parse_success_codes('200') == set([200])
     assert drain_method.parse_success_codes('200-203') == set(
         [200, 201, 202, 203])
     assert drain_method.parse_success_codes('200-202,302,305-306') == set(
         [200, 201, 202, 302, 305, 305, 306])
     assert drain_method.parse_success_codes(200) == set([200])
Ejemplo n.º 7
0
    def test_check_response_code(self):
        drain_method = drain_lib.HTTPDrainMethod("fake_service",
                                                 "fake_instance",
                                                 ["fake_nerve_ns"], {}, {}, {},
                                                 {})

        # Happy case
        assert drain_method.check_response_code(200, "200-299") is True

        # Sad case
        assert drain_method.check_response_code(500, "200-299") is False
Ejemplo n.º 8
0
 def test_get_format_params(self):
     fake_task = mock.Mock(host="fake_host", ports=[54321])
     drain_method = drain_lib.HTTPDrainMethod(
         "fake_service", "fake_instance", ["fake_nerve_ns"], {}, {}, {}, {}
     )
     assert drain_method.get_format_params(fake_task) == [
         {
             "host": "fake_host",
             "port": 54321,
             "service": "fake_service",
             "instance": "fake_instance",
             "nerve_ns": "fake_nerve_ns",
         }
     ]
Ejemplo n.º 9
0
def make_http_drain_method(context):
    context.http_drain_method = drain_lib.HTTPDrainMethod(
        service='fake_service',
        instance='fake_instance',
        nerve_ns='fake_nerve_ns',
        drain={
            "url_format": "http://localhost:%d/drain" % context.fake_http_server.server.server_port,
            "success_codes": "200",
        },
        stop_draining={},
        is_draining={
            "url_format": "http://localhost:%d/is_draining" % context.fake_http_server.server.server_port,
            "success_codes": "200",
        },
        is_safe_to_kill={},
    )
Ejemplo n.º 10
0
 def test_parse_success_codes(self):
     drain_method = drain_lib.HTTPDrainMethod(
         "fake_service", "fake_instance", ["fake_nerve_ns"], {}, {}, {}, {}
     )
     assert drain_method.parse_success_codes("200") == {200}
     assert drain_method.parse_success_codes("200-203") == {200, 201, 202, 203}
     assert drain_method.parse_success_codes("200-202,302,305-306") == {
         200,
         201,
         202,
         302,
         305,
         305,
         306,
     }
     assert drain_method.parse_success_codes(200) == {200}
Ejemplo n.º 11
0
    def test_issue_request(self):
        drain_method = drain_lib.HTTPDrainMethod('fake_service', 'fake_instance', 'fake_nerve_ns', {}, {}, {}, {})
        fake_task = mock.Mock(host='fake_host', ports=[54321])
        url_spec = {
            'url_format': 'http://localhost:654321/fake/{host}',
            'method': 'get',
            'success_codes': '1234',
        }

        fake_resp = mock.Mock(status_code=1234)

        with mock.patch('paasta_tools.drain_lib.requests.get', autospec=True, return_value=fake_resp) as mock_get:
            drain_method.issue_request(
                url_spec=url_spec,
                task=fake_task,
            )

        mock_get.assert_called_once_with('http://localhost:654321/fake/fake_host', headers=mock.ANY, timeout=15)
Ejemplo n.º 12
0
    async def test_issue_request(self):
        drain_method = drain_lib.HTTPDrainMethod(
            "fake_service", "fake_instance", ["fake_nerve_ns"], {}, {}, {}, {}
        )
        fake_task = mock.Mock(host="fake_host", ports=[54321])
        url_spec = {
            "url_format": "http://localhost:654321/fake/{host}",
            "method": "get",
            "success_codes": "1234",
        }

        fake_resp = mock.Mock(status=1234)
        mock_request = mock.Mock(
            return_value=asynctest.CoroutineMock(return_value=fake_resp)()
        )
        with mock_ClientSession(request=mock_request):
            await drain_method.issue_request(url_spec=url_spec, task=fake_task)

        mock_request.assert_called_once_with(
            method="GET",
            url="http://localhost:654321/fake/fake_host",
            headers=mock.ANY,
            timeout=15,
        )