]> granicus.if.org Git - python/commitdiff
asyncio tests: make quiet the logs of SSL handshake failures when running tests
authorVictor Stinner <victor.stinner@gmail.com>
Mon, 14 Jul 2014 20:26:34 +0000 (22:26 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Mon, 14 Jul 2014 20:26:34 +0000 (22:26 +0200)
in debug mode

Lib/asyncio/test_utils.py
Lib/test/test_asyncio/test_events.py
Lib/test/test_asyncio/test_selector_events.py

index 6abcaf1d37970a5de2ce0b8bb3ea140683a1a882..840bbf94a32f52313d10e13c620f03134b18c9eb 100644 (file)
@@ -3,6 +3,7 @@
 import collections
 import contextlib
 import io
+import logging
 import os
 import re
 import socket
@@ -28,6 +29,7 @@ from . import futures
 from . import selectors
 from . import tasks
 from .coroutines import coroutine
+from .log import logger
 
 
 if sys.platform == 'win32':  # pragma: no cover
@@ -401,3 +403,17 @@ class TestCase(unittest.TestCase):
 
     def tearDown(self):
         events.set_event_loop(None)
+
+
+@contextlib.contextmanager
+def disable_logger():
+    """Context manager to disable asyncio logger.
+
+    For example, it can be used to ignore warnings in debug mode.
+    """
+    old_level = logger.level
+    try:
+        logger.setLevel(logging.CRITICAL+1)
+        yield
+    finally:
+        logger.setLevel(old_level)
index 06552f874f8bca4825f8184206b2b8236c4460f3..b0657495ad4e6793e276972f979f5c6d49736fdf 100644 (file)
@@ -819,9 +819,10 @@ class EventLoopTestsMixin:
         # no CA loaded
         f_c = self.loop.create_connection(MyProto, host, port,
                                           ssl=sslcontext_client)
-        with self.assertRaisesRegex(ssl.SSLError,
-                                    'certificate verify failed '):
-            self.loop.run_until_complete(f_c)
+        with test_utils.disable_logger():
+            with self.assertRaisesRegex(ssl.SSLError,
+                                        'certificate verify failed '):
+                self.loop.run_until_complete(f_c)
 
         # close connection
         self.assertIsNone(proto.transport)
@@ -845,9 +846,10 @@ class EventLoopTestsMixin:
         f_c = self.loop.create_unix_connection(MyProto, path,
                                                ssl=sslcontext_client,
                                                server_hostname='invalid')
-        with self.assertRaisesRegex(ssl.SSLError,
-                                    'certificate verify failed '):
-            self.loop.run_until_complete(f_c)
+        with test_utils.disable_logger():
+            with self.assertRaisesRegex(ssl.SSLError,
+                                        'certificate verify failed '):
+                self.loop.run_until_complete(f_c)
 
         # close connection
         self.assertIsNone(proto.transport)
@@ -871,10 +873,11 @@ class EventLoopTestsMixin:
         # incorrect server_hostname
         f_c = self.loop.create_connection(MyProto, host, port,
                                           ssl=sslcontext_client)
-        with self.assertRaisesRegex(
-                ssl.CertificateError,
-                "hostname '127.0.0.1' doesn't match 'localhost'"):
-            self.loop.run_until_complete(f_c)
+        with test_utils.disable_logger():
+            with self.assertRaisesRegex(
+                    ssl.CertificateError,
+                    "hostname '127.0.0.1' doesn't match 'localhost'"):
+                self.loop.run_until_complete(f_c)
 
         # close connection
         proto.transport.close()
index 51869316007e239d49bf7a0c95e58761b5f3ede5..198b14fd6da054bb2b3abe6fe77b10201d84d513 100644 (file)
@@ -1105,13 +1105,13 @@ class SelectorSslTransportTests(test_utils.TestCase):
     def test_on_handshake_exc(self):
         exc = ValueError()
         self.sslsock.do_handshake.side_effect = exc
-        transport = _SelectorSslTransport(
-            self.loop, self.sock, self.protocol, self.sslcontext)
-        transport._waiter = asyncio.Future(loop=self.loop)
-        transport._on_handshake(None)
+        with test_utils.disable_logger():
+            waiter = asyncio.Future(loop=self.loop)
+            transport = _SelectorSslTransport(
+                self.loop, self.sock, self.protocol, self.sslcontext, waiter)
+        self.assertTrue(waiter.done())
+        self.assertIs(exc, waiter.exception())
         self.assertTrue(self.sslsock.close.called)
-        self.assertTrue(transport._waiter.done())
-        self.assertIs(exc, transport._waiter.exception())
 
     def test_on_handshake_base_exc(self):
         transport = _SelectorSslTransport(
@@ -1119,7 +1119,8 @@ class SelectorSslTransportTests(test_utils.TestCase):
         transport._waiter = asyncio.Future(loop=self.loop)
         exc = BaseException()
         self.sslsock.do_handshake.side_effect = exc
-        self.assertRaises(BaseException, transport._on_handshake, None)
+        with test_utils.disable_logger():
+            self.assertRaises(BaseException, transport._on_handshake, None)
         self.assertTrue(self.sslsock.close.called)
         self.assertTrue(transport._waiter.done())
         self.assertIs(exc, transport._waiter.exception())