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

Last change on this file was 52b7b9d, checked in by Florian Sesser <florian@…>, at 2025-08-28T15:56:36Z

Amend TestStreamingLogs? to work with Twisted 25.5.0

Fixes #4181(https://tahoe-lafs.org/trac/tahoe-lafs/ticket/4181)
Refs #4185(https://tahoe-lafs.org/trac/tahoe-lafs/ticket/4185)

  • 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
11from twisted.internet.testing import MemoryReactorClock
12
13from autobahn.twisted.testing import create_memory_agent, create_pumper
14
15from testtools.matchers import (
16    Equals,
17)
18from testtools.twistedsupport import (
19    succeeded,
20)
21
22from twisted.web.http import (
23    OK,
24)
25
26from treq.client import (
27    HTTPClient,
28)
29from treq.testing import (
30    RequestTraversalAgent,
31)
32
33from .matchers import (
34    has_response_code,
35)
36
37from ..common import (
38    SyncTestCase,
39    AsyncTestCase,
40)
41
42from ...web.logs import (
43    create_log_resources,
44    TokenAuthenticatedWebSocketServerProtocol,
45)
46
47from eliot import log_call
48
49class 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
69class 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"]))
Note: See TracBrowser for help on using the repository browser.