From 211883d09a8b5cd313deb02f6755623d5029fc18 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Wed, 6 Sep 2017 08:40:47 -0500 Subject: [PATCH] Update tests to use AF_INET6 zuul_console now properly uses AF_INET6, but some of our tests were still using AF_INET. There are some sporadic failures with the streaming test that show some connection errors. It's possible AF_INET6 is a red herring, but it's also not incorrect to set it, so let's go ahead and update it. While we're at it - use the create_connection form like we do in zuul_stream rather than the s = socket.socket ; s.connect form. Change-Id: I278efb939cb6540b5fe2fb1a06495d9be5a7f997 --- tests/base.py | 2 +- tests/unit/test_log_streamer.py | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/base.py b/tests/base.py index 421480969d..abd78f4154 100755 --- a/tests/base.py +++ b/tests/base.py @@ -1195,7 +1195,7 @@ class FakeStatsd(threading.Thread): def __init__(self): threading.Thread.__init__(self) self.daemon = True - self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + self.sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) self.sock.bind(('', 0)) self.port = self.sock.getsockname()[1] self.wake_read, self.wake_write = os.pipe() diff --git a/tests/unit/test_log_streamer.py b/tests/unit/test_log_streamer.py index f47a8c87f8..03d3563d06 100644 --- a/tests/unit/test_log_streamer.py +++ b/tests/unit/test_log_streamer.py @@ -22,6 +22,7 @@ import os import os.path import socket import tempfile +import testtools import threading import time @@ -46,16 +47,13 @@ class TestLogStreamer(tests.base.BaseTestCase): streamer = self.startStreamer(port) self.addCleanup(streamer.stop) - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.addCleanup(s.close) - self.assertEqual(0, s.connect_ex((self.host, port))) + s = socket.create_connection((self.host, port)) s.close() streamer.stop() - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.addCleanup(s.close) - self.assertNotEqual(0, s.connect_ex((self.host, port))) + with testtools.ExpectedException(ConnectionRefusedError): + s = socket.create_connection((self.host, port)) s.close() @@ -80,8 +78,7 @@ class TestStreaming(tests.base.AnsibleZuulTestCase): root = tempfile.gettempdir() self.streamer = zuul.lib.log_streamer.LogStreamer(None, self.host, port, root) - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.connect((self.host, port)) + s = socket.create_connection((self.host, port)) self.addCleanup(s.close) req = '%s\n' % build_uuid @@ -237,8 +234,11 @@ class TestStreaming(tests.base.AnsibleZuulTestCase): self.addCleanup(web_server.stop) # Wait until web server is started - with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: - while s.connect_ex((self.host, 9000)): + while True: + try: + with socket.create_connection((self.host, 9000)): + break + except ConnectionRefusedError: time.sleep(0.1) # Start a thread with the websocket client