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