Use ZuulWebFixture in tests
This reduces duplication and increases consistency. Also, an extra logfile.close() call is removed in test_streaming (there is a cleanup handler which closes the file). Change-Id: I940ba07a48bade03b6b1f3ced9621d156f7755ae
This commit is contained in:
parent
56be6165ae
commit
57c0a67f89
|
@ -1882,17 +1882,21 @@ class WebProxyFixture(fixtures.Fixture):
|
||||||
|
|
||||||
|
|
||||||
class ZuulWebFixture(fixtures.Fixture):
|
class ZuulWebFixture(fixtures.Fixture):
|
||||||
def __init__(self, gearman_server_port, connections):
|
def __init__(self, gearman_server_port, connections, info=None):
|
||||||
super(ZuulWebFixture, self).__init__()
|
super(ZuulWebFixture, self).__init__()
|
||||||
self.gearman_server_port = gearman_server_port
|
self.gearman_server_port = gearman_server_port
|
||||||
self.connections = connections
|
self.connections = connections
|
||||||
|
if info is None:
|
||||||
|
self.info = zuul.model.WebInfo()
|
||||||
|
else:
|
||||||
|
self.info = info
|
||||||
|
|
||||||
def _setUp(self):
|
def _setUp(self):
|
||||||
# Start the web server
|
# Start the web server
|
||||||
self.web = zuul.web.ZuulWeb(
|
self.web = zuul.web.ZuulWeb(
|
||||||
listen_address='127.0.0.1', listen_port=0,
|
listen_address='::', listen_port=0,
|
||||||
gear_server='127.0.0.1', gear_port=self.gearman_server_port,
|
gear_server='127.0.0.1', gear_port=self.gearman_server_port,
|
||||||
info=zuul.model.WebInfo(),
|
info=self.info,
|
||||||
connections=self.connections)
|
connections=self.connections)
|
||||||
self.web.start()
|
self.web.start()
|
||||||
self.addCleanup(self.web.stop)
|
self.addCleanup(self.web.stop)
|
||||||
|
|
|
@ -22,9 +22,8 @@ from unittest import skip
|
||||||
|
|
||||||
import git
|
import git
|
||||||
|
|
||||||
import zuul.web
|
|
||||||
|
|
||||||
from tests.base import ZuulTestCase, simple_layout, random_sha1
|
from tests.base import ZuulTestCase, simple_layout, random_sha1
|
||||||
|
from tests.base import ZuulWebFixture
|
||||||
|
|
||||||
|
|
||||||
class TestGithubDriver(ZuulTestCase):
|
class TestGithubDriver(ZuulTestCase):
|
||||||
|
@ -784,12 +783,9 @@ class TestGithubWebhook(ZuulTestCase):
|
||||||
super(TestGithubWebhook, self).setUp()
|
super(TestGithubWebhook, self).setUp()
|
||||||
|
|
||||||
# Start the web server
|
# Start the web server
|
||||||
self.web = zuul.web.ZuulWeb(
|
self.web = self.useFixture(
|
||||||
listen_address='127.0.0.1', listen_port=0,
|
ZuulWebFixture(self.gearman_server.port,
|
||||||
gear_server='127.0.0.1', gear_port=self.gearman_server.port,
|
self.connections))
|
||||||
connections=self.connections)
|
|
||||||
self.web.start()
|
|
||||||
self.addCleanup(self.web.stop)
|
|
||||||
|
|
||||||
host = '127.0.0.1'
|
host = '127.0.0.1'
|
||||||
# Wait until web server is started
|
# Wait until web server is started
|
||||||
|
|
|
@ -28,7 +28,7 @@ import zuul.web
|
||||||
import zuul.lib.log_streamer
|
import zuul.lib.log_streamer
|
||||||
import zuul.lib.fingergw
|
import zuul.lib.fingergw
|
||||||
import tests.base
|
import tests.base
|
||||||
from tests.base import iterate_timeout
|
from tests.base import iterate_timeout, ZuulWebFixture
|
||||||
|
|
||||||
from ws4py.client import WebSocketBaseClient
|
from ws4py.client import WebSocketBaseClient
|
||||||
|
|
||||||
|
@ -230,6 +230,11 @@ class TestStreaming(tests.base.AnsibleZuulTestCase):
|
||||||
By making the 1024th character in the log file a multi-byte character
|
By making the 1024th character in the log file a multi-byte character
|
||||||
(here, the Euro character), we can test this.
|
(here, the Euro character), we can test this.
|
||||||
'''
|
'''
|
||||||
|
# Start the web server
|
||||||
|
web = self.useFixture(
|
||||||
|
ZuulWebFixture(self.gearman_server.port,
|
||||||
|
self.connections))
|
||||||
|
|
||||||
# Start the finger streamer daemon
|
# Start the finger streamer daemon
|
||||||
streamer = zuul.lib.log_streamer.LogStreamer(
|
streamer = zuul.lib.log_streamer.LogStreamer(
|
||||||
self.host, 0, self.executor_server.jobdir_root)
|
self.host, 0, self.executor_server.jobdir_root)
|
||||||
|
@ -280,29 +285,12 @@ class TestStreaming(tests.base.AnsibleZuulTestCase):
|
||||||
logfile = open(ansible_log, 'r')
|
logfile = open(ansible_log, 'r')
|
||||||
self.addCleanup(logfile.close)
|
self.addCleanup(logfile.close)
|
||||||
|
|
||||||
# Start the web server
|
|
||||||
web_server = zuul.web.ZuulWeb(
|
|
||||||
listen_address='::', listen_port=0,
|
|
||||||
gear_server='127.0.0.1', gear_port=self.gearman_server.port,
|
|
||||||
static_path=tempfile.gettempdir(),
|
|
||||||
connections=self.connections)
|
|
||||||
web_server.start()
|
|
||||||
self.addCleanup(web_server.stop)
|
|
||||||
|
|
||||||
# Wait until web server is started
|
|
||||||
for x in iterate_timeout(30, "web server to start"):
|
|
||||||
port = web_server.port
|
|
||||||
try:
|
|
||||||
with socket.create_connection((self.host, port)):
|
|
||||||
break
|
|
||||||
except ConnectionRefusedError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# Start a thread with the websocket client
|
# Start a thread with the websocket client
|
||||||
ws_client_event = threading.Event()
|
ws_client_event = threading.Event()
|
||||||
self.ws_client_results = ''
|
self.ws_client_results = ''
|
||||||
ws_client_thread = threading.Thread(
|
ws_client_thread = threading.Thread(
|
||||||
target=self.runWSClient, args=(port, build.uuid, ws_client_event)
|
target=self.runWSClient, args=(web.port, build.uuid,
|
||||||
|
ws_client_event)
|
||||||
)
|
)
|
||||||
ws_client_thread.start()
|
ws_client_thread.start()
|
||||||
ws_client_event.wait()
|
ws_client_event.wait()
|
||||||
|
@ -324,6 +312,11 @@ class TestStreaming(tests.base.AnsibleZuulTestCase):
|
||||||
self.assertEqual(file_contents, self.ws_client_results)
|
self.assertEqual(file_contents, self.ws_client_results)
|
||||||
|
|
||||||
def test_websocket_streaming(self):
|
def test_websocket_streaming(self):
|
||||||
|
# Start the web server
|
||||||
|
web = self.useFixture(
|
||||||
|
ZuulWebFixture(self.gearman_server.port,
|
||||||
|
self.connections))
|
||||||
|
|
||||||
# Start the finger streamer daemon
|
# Start the finger streamer daemon
|
||||||
streamer = zuul.lib.log_streamer.LogStreamer(
|
streamer = zuul.lib.log_streamer.LogStreamer(
|
||||||
self.host, 0, self.executor_server.jobdir_root)
|
self.host, 0, self.executor_server.jobdir_root)
|
||||||
|
@ -367,29 +360,12 @@ class TestStreaming(tests.base.AnsibleZuulTestCase):
|
||||||
logfile = open(ansible_log, 'r')
|
logfile = open(ansible_log, 'r')
|
||||||
self.addCleanup(logfile.close)
|
self.addCleanup(logfile.close)
|
||||||
|
|
||||||
# Start the web server
|
|
||||||
web_server = zuul.web.ZuulWeb(
|
|
||||||
listen_address='::', listen_port=0,
|
|
||||||
gear_server='127.0.0.1', gear_port=self.gearman_server.port,
|
|
||||||
static_path=tempfile.gettempdir(),
|
|
||||||
connections=self.connections)
|
|
||||||
web_server.start()
|
|
||||||
self.addCleanup(web_server.stop)
|
|
||||||
|
|
||||||
# Wait until web server is started
|
|
||||||
for x in iterate_timeout(30, "connection to web server"):
|
|
||||||
port = web_server.port
|
|
||||||
try:
|
|
||||||
with socket.create_connection((self.host, port)):
|
|
||||||
break
|
|
||||||
except ConnectionRefusedError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# Start a thread with the websocket client
|
# Start a thread with the websocket client
|
||||||
ws_client_event = threading.Event()
|
ws_client_event = threading.Event()
|
||||||
self.ws_client_results = ''
|
self.ws_client_results = ''
|
||||||
ws_client_thread = threading.Thread(
|
ws_client_thread = threading.Thread(
|
||||||
target=self.runWSClient, args=(port, build.uuid, ws_client_event)
|
target=self.runWSClient, args=(web.port, build.uuid,
|
||||||
|
ws_client_event)
|
||||||
)
|
)
|
||||||
ws_client_thread.start()
|
ws_client_thread.start()
|
||||||
ws_client_event.wait()
|
ws_client_event.wait()
|
||||||
|
@ -405,7 +381,6 @@ class TestStreaming(tests.base.AnsibleZuulTestCase):
|
||||||
self.waitUntilSettled()
|
self.waitUntilSettled()
|
||||||
|
|
||||||
file_contents = logfile.read()
|
file_contents = logfile.read()
|
||||||
logfile.close()
|
|
||||||
self.log.debug("\n\nFile contents: %s\n\n", file_contents)
|
self.log.debug("\n\nFile contents: %s\n\n", file_contents)
|
||||||
self.log.debug("\n\nStreamed: %s\n\n", self.ws_client_results)
|
self.log.debug("\n\nStreamed: %s\n\n", self.ws_client_results)
|
||||||
self.assertEqual(file_contents, self.ws_client_results)
|
self.assertEqual(file_contents, self.ws_client_results)
|
||||||
|
|
|
@ -25,6 +25,7 @@ import requests
|
||||||
import zuul.web
|
import zuul.web
|
||||||
|
|
||||||
from tests.base import ZuulTestCase, ZuulDBTestCase, FIXTURE_DIR
|
from tests.base import ZuulTestCase, ZuulDBTestCase, FIXTURE_DIR
|
||||||
|
from tests.base import ZuulWebFixture
|
||||||
|
|
||||||
|
|
||||||
class FakeConfig(object):
|
class FakeConfig(object):
|
||||||
|
@ -45,6 +46,16 @@ class BaseTestWeb(ZuulTestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(BaseTestWeb, self).setUp()
|
super(BaseTestWeb, self).setUp()
|
||||||
|
|
||||||
|
self.zuul_ini_config = FakeConfig(self.config_ini_data)
|
||||||
|
|
||||||
|
# Start the web server
|
||||||
|
self.web = self.useFixture(
|
||||||
|
ZuulWebFixture(
|
||||||
|
self.gearman_server.port,
|
||||||
|
self.connections,
|
||||||
|
info=zuul.model.WebInfo.fromConfig(self.zuul_ini_config)))
|
||||||
|
|
||||||
self.executor_server.hold_jobs_in_build = True
|
self.executor_server.hold_jobs_in_build = True
|
||||||
A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
|
A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
|
||||||
A.addApproval('Code-Review', 2)
|
A.addApproval('Code-Review', 2)
|
||||||
|
@ -54,17 +65,6 @@ class BaseTestWeb(ZuulTestCase):
|
||||||
self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
|
self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
|
||||||
self.waitUntilSettled()
|
self.waitUntilSettled()
|
||||||
|
|
||||||
self.zuul_ini_config = FakeConfig(self.config_ini_data)
|
|
||||||
# Start the web server
|
|
||||||
self.web = zuul.web.ZuulWeb(
|
|
||||||
listen_address='127.0.0.1', listen_port=0,
|
|
||||||
gear_server='127.0.0.1', gear_port=self.gearman_server.port,
|
|
||||||
info=zuul.model.WebInfo.fromConfig(self.zuul_ini_config),
|
|
||||||
connections=self.connections
|
|
||||||
)
|
|
||||||
self.web.start()
|
|
||||||
self.addCleanup(self.web.stop)
|
|
||||||
|
|
||||||
self.host = 'localhost'
|
self.host = 'localhost'
|
||||||
self.port = self.web.port
|
self.port = self.web.port
|
||||||
# Wait until web server is started
|
# Wait until web server is started
|
||||||
|
|
Loading…
Reference in New Issue