async def test_create_pack_fails_when_timeout(self, mock_get):
        mock_get.side_effect = CoroutineMock(side_effect=ClientTimeout())

        c = Client(url="http://test/unit")

        with self.assertRaises(FlyteClientError) as cm:
            await c.create_pack(Pack(name="tests", commands=[], links=[]))

        self.assertEqual("failed when calling http://test/unit/v1: __aexit__",
                         '{}'.format(cm.exception))
    async def test_take_action_fails_when__pack_has_not_been_registered_yet(
            self):
        c = Client()

        with self.assertRaises(FlyteClientError) as cm:
            await c.take_action()

        self.assertEqual(
            "hateoas links not found. You must register your pack first",
            '{}'.format(cm.exception))
    async def test_post_event_fails_when_pack_has_not_been_registered_yet(
            self):
        c = Client()

        with self.assertRaises(FlyteClientError) as cm:
            await c.post_event(Event(event="tests", payload="tests"))

        self.assertEqual(
            "hateoas links not found. You must register your pack first",
            '{}'.format(cm.exception))
    async def test_create_pack_fails_when_client_cant_fetch_api_links(
            self, mock_get):
        set_mock_response(mock_get, 500)

        c = Client()

        with self.assertRaises(FlyteClientError) as cm:
            await c.create_pack(Pack(name="tests", commands=[], links=[]))

        self.assertEqual("unable to fetch api links",
                         '{}'.format(cm.exception))
    async def test_create_pack_returns_flyte_client_error_when_flyte_server_fails(
            self, mock_get, mock_post):
        set_mock_response(mock_get, 200, content=self.get_hateoas_links())
        set_mock_response(mock_post, 500)

        c = Client()

        with self.assertRaises(FlyteClientError) as cm:
            await c.create_pack(Pack(name="tests", commands=[], links=[]))

        self.assertEqual("unable to register the pack",
                         '{}'.format(cm.exception))
    async def test_create_pack_returns_flyte_client_error_when_flyte_server_timesout(
            self, mock_get, mock_post):
        set_mock_response(mock_get, 200, content=self.get_hateoas_links())
        mock_post.side_effect = CoroutineMock(side_effect=ClientTimeout())

        c = Client(url="http://unittest")

        with self.assertRaises(FlyteClientError) as cm:
            await c.create_pack(Pack(name="tests", commands=[], links=[]))

        self.assertEqual(
            "failed when calling http://unitest/v1/packs: __aexit__",
            '{}'.format(cm.exception))
    async def test_create_pack_fails_when_client_cant_find_list_pack_link(
            self, mock_get):
        set_mock_response(mock_get, 500)
        set_mock_response(mock_get,
                          200,
                          content="""{
            "links": [
                {
                    "href": "http://unitest/v1/packs",
                    "rel": "something"
                }
            ]
        }""")

        c = Client()

        with self.assertRaises(ValueError) as _:
            await c.create_pack(Pack(name="tests", commands=[], links=[]))
Ejemplo n.º 8
0
if __name__ == "__main__":
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)

    logger.addHandler(logging.StreamHandler())

    loop = asyncio.get_event_loop()
    loop.add_signal_handler(signal.SIGTERM, functools.partial(shutdown, loop))
    loop.add_signal_handler(signal.SIGHUP, functools.partial(shutdown, loop))

    try:
        pack_def = PackDef(
            name="page-of-duty-pack",
            commands=[
                Command(name="Rota", handler=RotaCommandHandler(logger), output_events=[
                    EventDef(name="RotaRetrieved"),
                    EventDef(name="Error"),
                ]),
            ],
            labels={},
            event_defs=[],
            help_url="http://github.com/your-repo.git")

        pack = Pack(pack_def=pack_def, client=Client(url=os.environ['FLYTE_API']))

        loop.run_until_complete(pack.start())
    except KeyboardInterrupt:
        shutdown(loop)
        logger.info('Keyboard exception received. Exiting.')
        exit()
    async def register_pack(self):
        with patch('aiohttp.ClientSession.get') as mock_get:
            set_mock_response(mock_get, 200, content=self.get_hateoas_links())
            with patch('aiohttp.ClientSession.post') as mock_post:
                set_mock_response(mock_post,
                                  200,
                                  content="""{
                    "name": "FakeSlack",
                    "commands": [
                        {
                            "name": "SendMessage",
                            "events": [
                                "MessageReceived",
                                "SendMessageFailed",
                                "MessageSent"
                            ],
                            "links": [
                                {
                                    "href": "http://unitest/v1/packs/FakeSlack/actions/take?commandName=SendMessage",
                                    "rel": "http://unitest/swagger#!/action/takeAction"
                                }
                            ]
                        }
                    ],
                    "events": [
                        {
                            "name": "MessageSent"
                        },
                        {
                            "name": "SendMessageFailed"
                        },
                        {
                            "name": "MessageReceived"
                        }
                    ],
                    "links": [
                        {
                            "href": "http://unitest/v1/packs/FakeSlack",
                            "rel": "self"
                        },
                        {
                            "href": "http://unitest/v1/packs/FakeSlack/event",
                            "rel": "event"
                        },
                        {
                            "href": "http://unitest/v1/packs/FakeSlack/takeAction",
                            "rel": "takeAction"
                        }
                    ]
                }""")
                pack = Pack(
                    name="FakeSlack",
                    commands=[
                        Command(
                            name="SendMessage",
                            links=[
                                Link(href="http://unitest/command/help",
                                     rel="help")
                            ],
                            events=["MessageReceived", "SendMessageFailed"]),
                    ],
                    links=[Link(href="http://unitest/help", rel="help")])

                c = Client(url="http://unitest")
                pack_registered = await c.create_pack(pack)

                mock_get.assert_called_with(url="http://unitest/v1",
                                            timeout=self.timeout)
                mock_post.assert_called_with(url="http://unitest/v1/packs",
                                             timeout=self.timeout,
                                             data=pack.to_json())
        return pack, pack_registered, c