Piotr Kliczewski has uploaded a new change for review.
Change subject: tests: reduce noice after running detector test ......................................................................
tests: reduce noice after running detector test
We need to make sure that all the resources are closed properly during tear down. We want to check whether the tests pass using m2c and standard ssl module. As well as we want to get rid of AttributeError while interpreter shutdown after running test suite.
Change-Id: I608d83489f7bde8c44cf9f689eb1f84931531413 Signed-off-by: pkliczewski piotr.kliczewski@gmail.com --- M lib/yajsonrpc/betterAsyncore.py M tests/protocoldetectorTests.py 2 files changed, 66 insertions(+), 49 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/26/39726/1
diff --git a/lib/yajsonrpc/betterAsyncore.py b/lib/yajsonrpc/betterAsyncore.py index f54d580..c2f9513 100644 --- a/lib/yajsonrpc/betterAsyncore.py +++ b/lib/yajsonrpc/betterAsyncore.py @@ -17,9 +17,9 @@ # us to use. This does tries to reuse enought code from the original asyncore # while enabling compositing instead of inheritance. import asyncore -import socket import types from errno import EWOULDBLOCK +from socket import error
from vdsm.infra.eventfd import EventFD
@@ -112,7 +112,7 @@ return '' else: return data - except socket.error, why: + except error, why: # winsock sometimes raises ENOTCONN if why.args[0] == EWOULDBLOCK: return None @@ -128,7 +128,7 @@ if result == -1: return 0 return result - except socket.error, why: + except error, why: if why.args[0] == EWOULDBLOCK: return 0 elif why.args[0] in asyncore._DISCONNECTED: diff --git a/tests/protocoldetectorTests.py b/tests/protocoldetectorTests.py index 297537d..0ae7e9a 100644 --- a/tests/protocoldetectorTests.py +++ b/tests/protocoldetectorTests.py @@ -19,11 +19,11 @@ #
import errno -import socket import ssl import threading import time from contextlib import contextmanager +from socket import socket, error
from vdsm import sslutils from protocoldetector import MultiProtocolAcceptor @@ -41,10 +41,14 @@ NAME = None REQUIRED_SIZE = None
+ def __init__(self): + self.dispatcher = None + def detect(self, data): return data.startswith(self.NAME)
def handle_dispatcher(self, dispatcher, socket_address): + self.dispatcher = dispatcher client_socket = dispatcher.socket dispatcher.del_channel()
@@ -52,21 +56,25 @@ # Wait to detect case where the event loop steals data from # the socket after the dispatcher was removed. time.sleep(0.05) - try: - request = "" - while "\n" not in request: + request = "" + while "\n" not in request: + try: request += dispatcher.recv(1024) + except error: + # for some test we never receive data + pass
- response = self.response(request) - client_socket.setblocking(1) - client_socket.sendall(response) - finally: - client_socket.shutdown(socket.SHUT_RDWR) - client_socket.close() + response = self.response(request) + client_socket.setblocking(1) + client_socket.sendall(response)
t = threading.Thread(target=run) t.daemon = True t.start() + + def close(self): + if self.dispatcher is not None: + self.dispatcher.close()
class Echo(Detector): @@ -95,95 +103,100 @@ TIMEOUT = 2.0 GRACETIME = 0.5 CONCURRENCY = 5 - PERMUTATIONS = ((False,), (True,)) + PERMUTATIONS = ((False, False), (True, False), (True, True)) SSLCTX = sslutils.SSLContext(CRT_FILE, KEY_FILE, ca_cert=CRT_FILE) BUFSIZE = 512
def setUp(self): self.acceptor = None self.acceptor_address = None + self.detectors = [Uppercase(), Echo()]
def tearDown(self): if self.acceptor: self.acceptor.stop()
+ for detector in self.detectors: + detector.close() + # Testing
def test_reject_ssl_accept_error(self): self.start_acceptor(use_ssl=True) - with self.connect(use_ssl=False) as client: + with self.connect(use_ssl=False, use_m2c=False) as client: client.sendall("this is not ssl handshake\n") - self.assertRaises(socket.error, client.recv, self.BUFSIZE) + self.assertRaises(error, client.recv, self.BUFSIZE)
@permutations(PERMUTATIONS) - def test_reject_unknown_protocol(self, use_ssl): + def test_reject_unknown_protocol(self, use_ssl, use_m2c): self.start_acceptor(use_ssl) - self.check_reject(use_ssl) + self.check_reject(use_ssl, use_m2c)
@permutations(PERMUTATIONS) - def test_reject_concurrency(self, use_ssl): + def test_reject_concurrency(self, use_ssl, use_m2c): self.start_acceptor(use_ssl) - self.check_concurrently(self.check_reject, use_ssl) + self.check_concurrently(self.check_reject, use_ssl, use_m2c)
@permutations(PERMUTATIONS) - def test_detect_echo(self, use_ssl): + def test_detect_echo(self, use_ssl, use_m2c): self.start_acceptor(use_ssl) data = "echo testing is fun\n" - self.check_detect(use_ssl, data, data) + self.check_detect(use_ssl, use_m2c, data, data)
@permutations(PERMUTATIONS) - def test_detect_uppercase(self, use_ssl): + def test_detect_uppercase(self, use_ssl, use_m2c): self.start_acceptor(use_ssl) data = "uppercase testing is fun\n" - self.check_detect(use_ssl, data, data.upper()) + self.check_detect(use_ssl, use_m2c, data, data.upper())
@permutations(PERMUTATIONS) - def test_detect_concurrency(self, use_ssl): + def test_detect_concurrency(self, use_ssl, use_m2c): self.start_acceptor(use_ssl) data = "echo testing is fun\n" - self.check_concurrently(self.check_detect, use_ssl, data, data) + self.check_concurrently(self.check_detect, use_ssl, use_m2c, + data, data)
@permutations(PERMUTATIONS) - def test_detect_slow_client(self, use_ssl): + def test_detect_slow_client(self, use_ssl, use_m2c): self.start_acceptor(use_ssl) - self.check_slow_client(use_ssl) + self.check_slow_client(use_ssl, use_m2c)
@permutations(PERMUTATIONS) - def test_detect_slow_client_concurrency(self, use_ssl): + def test_detect_slow_client_concurrency(self, use_ssl, use_m2c): self.start_acceptor(use_ssl) - self.check_concurrently(self.check_slow_client, use_ssl) + self.check_concurrently(self.check_slow_client, use_ssl, use_m2c)
@permutations(PERMUTATIONS) - def test_reject_very_slow_client(self, use_ssl): + def test_reject_very_slow_client(self, use_ssl, use_m2c): self.start_acceptor(use_ssl) - self.check_very_slow_client(use_ssl) + self.check_very_slow_client(use_ssl, use_m2c)
@permutations(PERMUTATIONS) - def test_reject_very_slow_client_concurrency(self, use_ssl): + def test_reject_very_slow_client_concurrency(self, use_ssl, use_m2c): self.start_acceptor(use_ssl) - self.check_concurrently(self.check_very_slow_client, use_ssl) + self.check_concurrently(self.check_very_slow_client, use_ssl, use_m2c)
# Checking
- def check_detect(self, use_ssl, request, response): - with self.connect(use_ssl) as client: + def check_detect(self, use_ssl, use_m2c, request, response): + with self.connect(use_ssl, use_m2c) as client: client.sendall(request) self.assertEqual(client.recv(self.BUFSIZE), response)
- def check_reject(self, use_ssl): - with self.connect(use_ssl) as client: + def check_reject(self, use_ssl, use_m2c): + with self.connect(use_ssl, use_m2c) as client: client.sendall("no such protocol\n") self.check_disconnected(client)
- def check_slow_client(self, use_ssl): - with self.connect(use_ssl) as client: + def check_slow_client(self, use_ssl, use_m2c): + with self.connect(use_ssl, use_m2c) as client: time.sleep(self.acceptor.TIMEOUT - self.GRACETIME) data = "echo let me in\n" client.sendall(data) self.assertEqual(client.recv(self.BUFSIZE), data)
- def check_very_slow_client(self, use_ssl): - with self.connect(use_ssl) as client: + def check_very_slow_client(self, use_ssl, use_m2c): + with self.connect(use_ssl, use_m2c) as client: time.sleep(self.acceptor.TIMEOUT * 2 + self.GRACETIME) client.sendall("echo too slow probably\n") self.check_disconnected(client) @@ -191,7 +204,7 @@ def check_disconnected(self, client): try: data = client.recv(self.BUFSIZE) - except socket.error as e: + except error as e: self.assertEqual(e.errno, errno.ECONNRESET) else: self.assertEqual(data, '') @@ -202,21 +215,25 @@ self.acceptor = MultiProtocolAcceptor( '127.0.0.1', 0, sslctx=self.SSLCTX if use_ssl else None) self.acceptor.TIMEOUT = 1 - self.acceptor.add_detector(Echo()) - self.acceptor.add_detector(Uppercase()) + for detector in self.detectors: + self.acceptor.add_detector(detector) self.acceptor_address = self.acceptor._acceptor.socket.getsockname() t = threading.Thread(target=self.acceptor.serve_forever) t.deamon = True t.start()
@contextmanager - def connect(self, use_ssl): - s = socket.socket() + def connect(self, use_ssl, use_m2c): + s = socket() try: s.settimeout(self.TIMEOUT) if use_ssl: - s = ssl.wrap_socket(s, KEY_FILE, CRT_FILE, ca_certs=CRT_FILE, - server_side=False) + if use_m2c: + s = self.SSLCTX.wrapSocket(s) + else: + s = ssl.wrap_socket(s, KEY_FILE, CRT_FILE, + ca_certs=CRT_FILE, + server_side=False) s.connect(self.acceptor_address) yield s finally:
automation@ovirt.org has posted comments on this change.
Change subject: tests: reduce noice after running detector test ......................................................................
Patch Set 1:
* Update tracker::IGNORE, no Bug-Url found * Check Bug-Url::WARN, no bug url found, make sure header matches 'Bug-Url: ' and is a valid url. * Check merged to previous::IGNORE, Not in stable branch (['ovirt-3.5', 'ovirt-3.4', 'ovirt-3.3'])
oVirt Jenkins CI Server has posted comments on this change.
Change subject: tests: reduce noice after running detector test ......................................................................
Patch Set 1:
Build Started (1/2) -> http://jenkins.ovirt.org/job/vdsm_master_pep8_gerrit/17585/
Piotr Kliczewski has abandoned this change.
Change subject: tests: reduce noice after running detector test ......................................................................
Abandoned
Wrong change id
automation@ovirt.org has posted comments on this change.
Change subject: tests: reduce noice after running detector test ......................................................................
Patch Set 1:
* Update tracker::IGNORE, no Bug-Url found
oVirt Jenkins CI Server has posted comments on this change.
Change subject: tests: reduce noice after running detector test ......................................................................
Patch Set 1:
Build Started (2/2) -> http://jenkins.ovirt.org/job/vdsm_master_unit-tests_created/17759/
oVirt Jenkins CI Server has posted comments on this change.
Change subject: tests: reduce noice after running detector test ......................................................................
Patch Set 1:
Build Successful
http://jenkins.ovirt.org/job/vdsm_master_pep8_gerrit/17585/ : SUCCESS
http://jenkins.ovirt.org/job/vdsm_master_unit-tests_created/17759/ : SUCCESS
vdsm-patches@lists.fedorahosted.org