From 492bcbbd128b5c3a3d4c4c9f9898e9ddac522713 Mon Sep 17 00:00:00 2001 From: fenn-cs Date: Fri, 13 Aug 2021 18:22:10 +0100 Subject: [PATCH] Refactored test_logs to be consistent with base testcases Signed-off-by: fenn-cs --- newsfragments/3758.other | 1 + src/allmydata/test/web/test_logs.py | 27 ++++++++++++++++----------- src/allmydata/util/eliotutil.py | 9 +++++++-- 3 files changed, 24 insertions(+), 13 deletions(-) create mode 100644 newsfragments/3758.other diff --git a/newsfragments/3758.other b/newsfragments/3758.other new file mode 100644 index 000000000..d0eb1d4c1 --- /dev/null +++ b/newsfragments/3758.other @@ -0,0 +1 @@ +Refactored test_logs, test_grid and test_root in web tests to use custom base test cases diff --git a/src/allmydata/test/web/test_logs.py b/src/allmydata/test/web/test_logs.py index 89ec7ba42..043541690 100644 --- a/src/allmydata/test/web/test_logs.py +++ b/src/allmydata/test/web/test_logs.py @@ -17,10 +17,8 @@ if PY2: import json -from twisted.trial import unittest from twisted.internet.defer import inlineCallbacks -from eliot import log_call from autobahn.twisted.testing import create_memory_agent, MemoryReactorClockResolver, create_pumper @@ -48,6 +46,7 @@ from .matchers import ( from ..common import ( SyncTestCase, + AsyncTestCase, ) from ...web.logs import ( @@ -55,6 +54,10 @@ from ...web.logs import ( TokenAuthenticatedWebSocketServerProtocol, ) +from ...util.eliotutil import ( + log_call_deferred +) + class StreamingEliotLogsTests(SyncTestCase): """ Tests for the log streaming resources created by ``create_log_resources``. @@ -75,18 +78,20 @@ class StreamingEliotLogsTests(SyncTestCase): ) -class TestStreamingLogs(unittest.TestCase): +class TestStreamingLogs(AsyncTestCase): """ Test websocket streaming of logs """ def setUp(self): + super(TestStreamingLogs, self).setUp() self.reactor = MemoryReactorClockResolver() self.pumper = create_pumper() self.agent = create_memory_agent(self.reactor, self.pumper, TokenAuthenticatedWebSocketServerProtocol) return self.pumper.start() def tearDown(self): + super(TestStreamingLogs, self).tearDown() return self.pumper.stop() @inlineCallbacks @@ -105,7 +110,7 @@ class TestStreamingLogs(unittest.TestCase): messages.append(json.loads(msg)) proto.on("message", got_message) - @log_call(action_type=u"test:cli:some-exciting-action") + @log_call_deferred(action_type=u"test:cli:some-exciting-action") def do_a_thing(arguments): pass @@ -114,10 +119,10 @@ class TestStreamingLogs(unittest.TestCase): proto.transport.loseConnection() yield proto.is_closed - self.assertEqual(len(messages), 2) - self.assertEqual(messages[0]["action_type"], "test:cli:some-exciting-action") - self.assertEqual(messages[0]["arguments"], - ["hello", "good-\\xff-day", 123, {"a": 35}, [None]]) - self.assertEqual(messages[1]["action_type"], "test:cli:some-exciting-action") - self.assertEqual("started", messages[0]["action_status"]) - self.assertEqual("succeeded", messages[1]["action_status"]) + self.assertThat(len(messages), Equals(3)) + self.assertThat(messages[0]["action_type"], Equals("test:cli:some-exciting-action")) + self.assertThat(messages[0]["arguments"], + Equals(["hello", "good-\\xff-day", 123, {"a": 35}, [None]])) + self.assertThat(messages[1]["action_type"], Equals("test:cli:some-exciting-action")) + self.assertThat("started", Equals(messages[0]["action_status"])) + self.assertThat("succeeded", Equals(messages[1]["action_status"])) diff --git a/src/allmydata/util/eliotutil.py b/src/allmydata/util/eliotutil.py index 4e48fbb9f..ec4c0bf97 100644 --- a/src/allmydata/util/eliotutil.py +++ b/src/allmydata/util/eliotutil.py @@ -87,7 +87,11 @@ from twisted.internet.defer import ( ) from twisted.application.service import Service -from .jsonbytes import AnyBytesJSONEncoder +from .jsonbytes import ( + AnyBytesJSONEncoder, + bytes_to_unicode +) + def validateInstanceOf(t): @@ -320,7 +324,8 @@ def log_call_deferred(action_type): def logged_f(*a, **kw): # Use the action's context method to avoid ending the action when # the `with` block ends. - with start_action(action_type=action_type).context(): + args = bytes_to_unicode(True, kw['arguments']) + with start_action(action_type=action_type, arguments=args).context(): # Use addActionFinish so that the action finishes when the # Deferred fires. d = maybeDeferred(f, *a, **kw)