| 1 | """ |
|---|
| 2 | Tests for ``allmydata.web.logs``. |
|---|
| 3 | |
|---|
| 4 | Ported to Python 3. |
|---|
| 5 | """ |
|---|
| 6 | |
|---|
| 7 | import json |
|---|
| 8 | |
|---|
| 9 | from twisted.internet.defer import inlineCallbacks |
|---|
| 10 | |
|---|
| 11 | from twisted.internet.testing import MemoryReactorClock |
|---|
| 12 | |
|---|
| 13 | from autobahn.twisted.testing import create_memory_agent, create_pumper |
|---|
| 14 | |
|---|
| 15 | from testtools.matchers import ( |
|---|
| 16 | Equals, |
|---|
| 17 | ) |
|---|
| 18 | from testtools.twistedsupport import ( |
|---|
| 19 | succeeded, |
|---|
| 20 | ) |
|---|
| 21 | |
|---|
| 22 | from twisted.web.http import ( |
|---|
| 23 | OK, |
|---|
| 24 | ) |
|---|
| 25 | |
|---|
| 26 | from treq.client import ( |
|---|
| 27 | HTTPClient, |
|---|
| 28 | ) |
|---|
| 29 | from treq.testing import ( |
|---|
| 30 | RequestTraversalAgent, |
|---|
| 31 | ) |
|---|
| 32 | |
|---|
| 33 | from .matchers import ( |
|---|
| 34 | has_response_code, |
|---|
| 35 | ) |
|---|
| 36 | |
|---|
| 37 | from ..common import ( |
|---|
| 38 | SyncTestCase, |
|---|
| 39 | AsyncTestCase, |
|---|
| 40 | ) |
|---|
| 41 | |
|---|
| 42 | from ...web.logs import ( |
|---|
| 43 | create_log_resources, |
|---|
| 44 | TokenAuthenticatedWebSocketServerProtocol, |
|---|
| 45 | ) |
|---|
| 46 | |
|---|
| 47 | from eliot import log_call |
|---|
| 48 | |
|---|
| 49 | class StreamingEliotLogsTests(SyncTestCase): |
|---|
| 50 | """ |
|---|
| 51 | Tests for the log streaming resources created by ``create_log_resources``. |
|---|
| 52 | """ |
|---|
| 53 | def setUp(self): |
|---|
| 54 | self.resource = create_log_resources() |
|---|
| 55 | self.agent = RequestTraversalAgent(self.resource) |
|---|
| 56 | self.client = HTTPClient(self.agent) |
|---|
| 57 | return super(StreamingEliotLogsTests, self).setUp() |
|---|
| 58 | |
|---|
| 59 | def test_v1(self): |
|---|
| 60 | """ |
|---|
| 61 | There is a resource at *v1*. |
|---|
| 62 | """ |
|---|
| 63 | self.assertThat( |
|---|
| 64 | self.client.get(b"http:///v1"), |
|---|
| 65 | succeeded(has_response_code(Equals(OK))), |
|---|
| 66 | ) |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | class TestStreamingLogs(AsyncTestCase): |
|---|
| 70 | """ |
|---|
| 71 | Test websocket streaming of logs |
|---|
| 72 | """ |
|---|
| 73 | |
|---|
| 74 | def setUp(self): |
|---|
| 75 | super(TestStreamingLogs, self).setUp() |
|---|
| 76 | self.reactor = MemoryReactorClock() |
|---|
| 77 | self.pumper = create_pumper() |
|---|
| 78 | self.agent = create_memory_agent(self.reactor, self.pumper, TokenAuthenticatedWebSocketServerProtocol) |
|---|
| 79 | return self.pumper.start() |
|---|
| 80 | |
|---|
| 81 | def tearDown(self): |
|---|
| 82 | super(TestStreamingLogs, self).tearDown() |
|---|
| 83 | return self.pumper.stop() |
|---|
| 84 | |
|---|
| 85 | @inlineCallbacks |
|---|
| 86 | def test_one_log(self): |
|---|
| 87 | """ |
|---|
| 88 | Write a single Eliot log action and see it streamed via websocket. |
|---|
| 89 | """ |
|---|
| 90 | |
|---|
| 91 | proto = yield self.agent.open( |
|---|
| 92 | transport_config=u"ws://localhost:1234/ws", |
|---|
| 93 | options={}, |
|---|
| 94 | ) |
|---|
| 95 | |
|---|
| 96 | messages = [] |
|---|
| 97 | def got_message(msg, is_binary=False): |
|---|
| 98 | messages.append(json.loads(msg)) |
|---|
| 99 | proto.on("message", got_message) |
|---|
| 100 | |
|---|
| 101 | @log_call(action_type=u"test:cli:some-exciting-action") |
|---|
| 102 | def do_a_thing(arguments): |
|---|
| 103 | pass |
|---|
| 104 | |
|---|
| 105 | do_a_thing(arguments=[u"hello", b"good-\xff-day", 123, {"a": 35}, [None]]) |
|---|
| 106 | |
|---|
| 107 | proto.transport.loseConnection() |
|---|
| 108 | yield proto.is_closed |
|---|
| 109 | |
|---|
| 110 | self.assertThat(len(messages), Equals(3), messages) |
|---|
| 111 | self.assertThat(messages[0]["action_type"], Equals("test:cli:some-exciting-action")) |
|---|
| 112 | self.assertThat(messages[0]["arguments"], |
|---|
| 113 | Equals(["hello", "good-\\xff-day", 123, {"a": 35}, [None]])) |
|---|
| 114 | self.assertThat(messages[1]["action_type"], Equals("test:cli:some-exciting-action")) |
|---|
| 115 | self.assertThat("started", Equals(messages[0]["action_status"])) |
|---|
| 116 | self.assertThat("succeeded", Equals(messages[1]["action_status"])) |
|---|