2019-04-23 23:25:17 +00:00
|
|
|
import json
|
|
|
|
|
|
|
|
from twisted.trial import unittest
|
2019-05-07 19:46:56 +00:00
|
|
|
from twisted.internet.defer import inlineCallbacks
|
2019-04-23 23:25:17 +00:00
|
|
|
|
|
|
|
from eliot import log_call
|
|
|
|
|
2019-04-24 21:27:10 +00:00
|
|
|
from autobahn.twisted.testing import create_memory_agent, MemoryReactorClockResolver, create_pumper
|
2019-04-23 23:25:17 +00:00
|
|
|
|
|
|
|
from allmydata.web.logs import TokenAuthenticatedWebSocketServerProtocol
|
2019-05-07 19:39:50 +00:00
|
|
|
|
2019-04-23 23:25:17 +00:00
|
|
|
|
|
|
|
class TestStreamingLogs(unittest.TestCase):
|
|
|
|
"""
|
|
|
|
Test websocket streaming of logs
|
|
|
|
"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.reactor = MemoryReactorClockResolver()
|
2019-04-24 21:27:10 +00:00
|
|
|
self.pumper = create_pumper()
|
|
|
|
self.agent = create_memory_agent(self.reactor, self.pumper, TokenAuthenticatedWebSocketServerProtocol)
|
|
|
|
return self.pumper.start()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
return self.pumper.stop()
|
2019-04-23 23:25:17 +00:00
|
|
|
|
|
|
|
@inlineCallbacks
|
|
|
|
def test_one_log(self):
|
2019-06-18 22:04:05 +00:00
|
|
|
"""
|
|
|
|
write a single Eliot log and see it streamed via websocket
|
|
|
|
"""
|
2019-04-23 23:25:17 +00:00
|
|
|
|
|
|
|
proto = yield self.agent.open(
|
|
|
|
transport_config=u"ws://localhost:1234/ws",
|
|
|
|
options={},
|
|
|
|
)
|
|
|
|
|
|
|
|
messages = []
|
|
|
|
def got_message(msg, is_binary=False):
|
|
|
|
messages.append(json.loads(msg))
|
|
|
|
proto.on("message", got_message)
|
|
|
|
|
2020-02-13 15:43:50 +00:00
|
|
|
@log_call(action_type=u"test:cli:some-exciting-action")
|
2019-04-23 23:25:17 +00:00
|
|
|
def do_a_thing():
|
|
|
|
pass
|
|
|
|
|
|
|
|
do_a_thing()
|
|
|
|
|
|
|
|
proto.transport.loseConnection()
|
|
|
|
yield proto.is_closed
|
|
|
|
|
|
|
|
self.assertEqual(len(messages), 2)
|
|
|
|
self.assertEqual("started", messages[0]["action_status"])
|
|
|
|
self.assertEqual("succeeded", messages[1]["action_status"])
|