source: trunk/src/allmydata/test/web/test_logs.py

Last change on this file was 1cfe843d, checked in by Alexandre Detiste <alexandre.detiste@…>, at 2024-02-22T23:40:25Z

more python2 removal

  • Property mode set to 100644
File size: 3.0 KB
Line 
1"""
2Tests for ``allmydata.web.logs``.
3
4Ported to Python 3.
5"""
6
7import json
8
9from twisted.internet.defer import inlineCallbacks
10
11
12from autobahn.twisted.testing import create_memory_agent, MemoryReactorClockResolver, create_pumper
13
14from testtools.matchers import (
15    Equals,
16)
17from testtools.twistedsupport import (
18    succeeded,
19)
20
21from twisted.web.http import (
22    OK,
23)
24
25from treq.client import (
26    HTTPClient,
27)
28from treq.testing import (
29    RequestTraversalAgent,
30)
31
32from .matchers import (
33    has_response_code,
34)
35
36from ..common import (
37    SyncTestCase,
38    AsyncTestCase,
39)
40
41from ...web.logs import (
42    create_log_resources,
43    TokenAuthenticatedWebSocketServerProtocol,
44)
45
46from eliot import log_call
47
48class 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
68class 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"]))
Note: See TracBrowser for help on using the repository browser.