Exemple #1
0
class TestHacheckDrainMethod:
    drain_method = drain_lib.HacheckDrainMethod(
        service="srv",
        instance="inst",
        registrations=["ns_one", "ns_two"],
        hacheck_port=12345,
    )

    async def _async_id(self, x):
        return x

    def test_spool_urls(self):
        fake_task = mock.Mock(host="fake_host", ports=[54321])
        actual = self.drain_method.spool_urls(fake_task)
        # Nerve hits /{mode}/{service}.{namespace}/{port}/status
        expected = [
            f"http://fake_host:12345/spool/{ns}/54321/status"
            for ns in self.drain_method.registrations
        ]
        assert actual == expected

    @pytest.mark.asyncio
    async def test_for_each_registration_with_no_ports(self):
        fake_task = mock.Mock(host="fake_host", ports=[])
        actual = await self.drain_method.for_each_registration(
            task=fake_task, func=self._async_id)
        assert actual is None

    @pytest.mark.asyncio
    async def test_for_each_registration(self):
        fake_task = mock.Mock(host="fake_host", ports=[54321])
        actual = await self.drain_method.for_each_registration(
            task=fake_task, func=self._async_id)
        assert actual == self.drain_method.spool_urls(fake_task)

    @pytest.mark.asyncio
    async def test_is_draining_yes(self):
        fake_response = mock.Mock(
            status=503,
            text=asynctest.CoroutineMock(
                return_value=
                "Service service in down state since 1435694078.778886 "
                "until 1435694178.780000: Drained by Paasta"),
        )
        fake_task = mock.Mock(host="fake_host", ports=[54321])
        with mock_ClientSession(get=mock.Mock(return_value=asynctest.MagicMock(
                __aenter__=asynctest.CoroutineMock(
                    return_value=fake_response)))):
            assert await self.drain_method.is_draining(fake_task) is True

    @pytest.mark.asyncio
    async def test_is_draining_no(self):
        fake_response = mock.Mock(
            status=200, text=asynctest.CoroutineMock(return_value=""))
        fake_task = mock.Mock(host="fake_host", ports=[54321])
        with mock_ClientSession(get=mock.Mock(return_value=asynctest.MagicMock(
                __aenter__=asynctest.CoroutineMock(
                    return_value=fake_response)))):
            assert await self.drain_method.is_draining(fake_task) is False
Exemple #2
0
def a_HacheckDrainMethod_object_with_delay(context, delay):
    context.drain_method = drain_lib.HacheckDrainMethod(
        service="service",
        instance="instance",
        nerve_ns="namespace",
        delay=delay,
        hacheck_port=context.hacheck_port,
    )
Exemple #3
0
def a_HacheckDrainMethod_object_with_delay(context, delay):
    context.drain_method = drain_lib.HacheckDrainMethod(
        service="service",
        instance="instance",
        registrations=["one", "two"],
        delay=delay,
        hacheck_port=context.hacheck_port,
    )
class TestHacheckDrainMethod(object):
    drain_method = drain_lib.HacheckDrainMethod("srv",
                                                "inst",
                                                "ns",
                                                hacheck_port=12345)

    def test_spool_url(self):
        fake_task = mock.Mock(host="fake_host", ports=[54321])
        actual = self.drain_method.spool_url(fake_task)
        # Nerve hits /{mode}/{service}.{namespace}/{port}/status
        expected = 'http://fake_host:12345/spool/srv.ns/54321/status'
        assert actual == expected

    def test_get_spool(self):
        fake_response = mock.Mock(
            status_code=503,
            text=
            "Service service in down state since 1435694078.778886 until 1435694178.780000: Drained by Paasta",
        )
        fake_task = mock.Mock(host="fake_host", ports=[54321])
        with mock.patch('requests.get',
                        return_value=fake_response,
                        autospec=True):
            actual = self.drain_method.get_spool(fake_task)

        expected = {
            'service': 'service',
            'state': 'down',
            'reason': 'Drained by Paasta',
            'since': 1435694078.778886,
            'until': 1435694178.780000,
        }
        assert actual == expected

    def test_is_draining_yes(self):
        fake_response = mock.Mock(
            status_code=503,
            text=
            "Service service in down state since 1435694078.778886 until 1435694178.780000: Drained by Paasta",
        )
        fake_task = mock.Mock(host="fake_host", ports=[54321])
        with mock.patch('requests.get',
                        return_value=fake_response,
                        autospec=True):
            assert self.drain_method.is_draining(fake_task) is True

    def test_is_draining_no(self):
        fake_response = mock.Mock(
            status_code=200,
            text="",
        )
        fake_task = mock.Mock(host="fake_host", ports=[54321])
        with mock.patch('requests.get',
                        return_value=fake_response,
                        autospec=True):
            assert self.drain_method.is_draining(fake_task) is False
Exemple #5
0
class TestHacheckDrainMethod(object):
    drain_method = drain_lib.HacheckDrainMethod("srv",
                                                "inst",
                                                "ns",
                                                hacheck_port=12345)

    def test_spool_url(self):
        fake_task = mock.Mock(host="fake_host", ports=[54321])
        actual = self.drain_method.spool_url(fake_task)
        # Nerve hits /{mode}/{service}.{namespace}/{port}/status
        expected = 'http://fake_host:12345/spool/srv.ns/54321/status'
        assert actual == expected

    def test_spool_url_handles_tasks_with_no_ports(self):
        fake_task = mock.Mock(host="fake_host", ports=[])
        actual = self.drain_method.spool_url(fake_task)
        assert actual is None

    @pytest.mark.asyncio
    async def test_get_spool(self):
        fake_response = mock.Mock(
            status=503,
            text=asynctest.CoroutineMock(
                return_value=
                "Service service in down state since 1435694078.778886 "
                "until 1435694178.780000: Drained by Paasta", ),
        )
        fake_task = mock.Mock(host="fake_host", ports=[54321])

        with mock_ClientSession(get=asynctest.CoroutineMock(
                return_value=fake_response)):
            actual = await self.drain_method.get_spool(fake_task)

        expected = {
            'service': 'service',
            'state': 'down',
            'reason': 'Drained by Paasta',
            'since': 1435694078.778886,
            'until': 1435694178.780000,
        }
        assert actual == expected

    @pytest.mark.asyncio
    async def test_get_spool_handles_no_ports(self):
        fake_task = mock.Mock(host="fake_host", ports=[])
        actual = await self.drain_method.get_spool(fake_task)
        assert actual is None

    @pytest.mark.asyncio
    async def test_is_draining_yes(self):
        fake_response = mock.Mock(
            status=503,
            text=asynctest.CoroutineMock(
                return_value=
                "Service service in down state since 1435694078.778886 "
                "until 1435694178.780000: Drained by Paasta", ),
        )
        fake_task = mock.Mock(host="fake_host", ports=[54321])
        with mock_ClientSession(get=asynctest.CoroutineMock(
                return_value=fake_response)):
            assert await self.drain_method.is_draining(fake_task) is True

    @pytest.mark.asyncio
    async def test_is_draining_no(self):
        fake_response = mock.Mock(
            status=200,
            text=asynctest.CoroutineMock(return_value="", ),
        )
        fake_task = mock.Mock(host="fake_host", ports=[54321])
        with mock_ClientSession(get=asynctest.CoroutineMock(
                return_value=fake_response)):
            assert await self.drain_method.is_draining(fake_task) is False