From 5e5eea778f76e6746524b73e65c07df864037e05 Mon Sep 17 00:00:00 2001 From: Thomas Herve Date: Mon, 14 Sep 2015 10:18:20 +0200 Subject: [PATCH] WebSocket functional tests This adds infrastructure for websocket functional tests, and a new tox target running them. Change-Id: Ib4ab6cbaaf4a7295e6a6f2f26b2e9e935e3b88ef --- test-requirements.txt | 1 + tox.ini | 7 ++ zaqar/tests/functional/base.py | 11 +-- zaqar/tests/functional/websocket/__init__.py | 0 .../tests/functional/websocket/test_queues.py | 72 +++++++++++++++++++ .../tests/functional/wsgi/v1_1/test_queues.py | 2 +- 6 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 zaqar/tests/functional/websocket/__init__.py create mode 100644 zaqar/tests/functional/websocket/test_queues.py diff --git a/test-requirements.txt b/test-requirements.txt index 5c07d6737..f009698c2 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -10,6 +10,7 @@ mock>=1.2 # Backends redis>=2.10.0 pymongo>=3.0.2 +websocket-client>=0.32.0 # LGPLv2+ # Unit testing coverage>=3.6 diff --git a/tox.ini b/tox.ini index 646c2d799..01f2bf46a 100644 --- a/tox.ini +++ b/tox.ini @@ -23,6 +23,13 @@ setenv = ZAQAR_TEST_MONGODB=1 setenv = JIT_FLAG=--jit off {[testenv]setenv} +[testenv:integration] +setenv = ZAQAR_TEST_MONGODB=1 + ZAQAR_TEST_SLOW=1 + ZAQAR_TEST_INTEGRATION=1 + {[testenv]setenv} +commands = python setup.py testr --slowest --testr-args='--concurrency 1 zaqar.tests.functional' + [tox:jenkins] downloadcache = ~/cache/pip diff --git a/zaqar/tests/functional/base.py b/zaqar/tests/functional/base.py index 6df8ad489..0ec8bbe56 100644 --- a/zaqar/tests/functional/base.py +++ b/zaqar/tests/functional/base.py @@ -75,6 +75,10 @@ class FunctionalTestBase(testing.TestBase): self.resource_defaults = transport_base.ResourceDefaults(self.mconf) + # Always register options + wrapper = bootstrap.Bootstrap(self.mconf) + wrapper.transport + if _TEST_INTEGRATION: # TODO(kgriffs): This code should be replaced to use # an external wsgi server instance. @@ -92,10 +96,9 @@ class FunctionalTestBase(testing.TestBase): self.mconf.pooling = True self.mconf.admin_mode = True - boot = bootstrap.Bootstrap(self.mconf) - self.addCleanup(boot.storage.close) - self.addCleanup(boot.control.close) - self.client = http.WSGIClient(boot.transport.app) + self.addCleanup(wrapper.storage.close) + self.addCleanup(wrapper.control.close) + self.client = http.WSGIClient(wrapper.transport.app) self.headers = helpers.create_zaqar_headers(self.cfg) diff --git a/zaqar/tests/functional/websocket/__init__.py b/zaqar/tests/functional/websocket/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/zaqar/tests/functional/websocket/test_queues.py b/zaqar/tests/functional/websocket/test_queues.py new file mode 100644 index 000000000..2a83baf4c --- /dev/null +++ b/zaqar/tests/functional/websocket/test_queues.py @@ -0,0 +1,72 @@ +# Copyright (c) 2015 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import json +import uuid + +from testtools import testcase +import websocket + +from zaqar.tests.functional import base + + +class TestQueues(base.V1_1FunctionalTestBase): + + config_file = 'websocket_mongodb.conf' + server_class = base.ZaqarServer + + def setUp(self): + if not base._TEST_INTEGRATION: + raise testcase.TestSkipped('Only run in integration mode') + super(TestQueues, self).setUp() + self.project_id = str(uuid.uuid4()) + self.headers = {'Client-ID': str(uuid.uuid4()), + 'X-Project-ID': self.project_id} + self.client = websocket.create_connection('ws://localhost:9000/') + self.addCleanup(self.client.close) + + def test_list_empty(self): + self.client.send( + json.dumps({'action': 'queue_list', 'headers': self.headers})) + response = json.loads(self.client.recv()) + self.assertEqual( + {'body': {'queues': []}, + 'headers': {'status': 200}, + 'request': {'action': 'queue_list', 'body': {}, 'api': 'v2', + 'headers': self.headers}}, + response) + + def test_list(self): + self.client.send( + json.dumps({'action': 'queue_create', + 'body': {'queue_name': 'my_queue'}, + 'headers': self.headers})) + response = json.loads(self.client.recv()) + self.assertEqual( + {'body': 'Queue my_queue created.', + 'headers': {'status': 201}, + 'request': {'action': 'queue_create', + 'body': {'queue_name': 'my_queue'}, 'api': 'v2', + 'headers': self.headers}}, + response) + self.client.send( + json.dumps({'action': 'queue_list', 'headers': self.headers})) + response = json.loads(self.client.recv()) + self.assertEqual( + {'body': {'queues': [{'name': 'my_queue'}]}, + 'headers': {'status': 200}, + 'request': {'action': 'queue_list', 'body': {}, 'api': 'v2', + 'headers': self.headers}}, + response) diff --git a/zaqar/tests/functional/wsgi/v1_1/test_queues.py b/zaqar/tests/functional/wsgi/v1_1/test_queues.py index 9426e6bdc..5f984e562 100644 --- a/zaqar/tests/functional/wsgi/v1_1/test_queues.py +++ b/zaqar/tests/functional/wsgi/v1_1/test_queues.py @@ -19,7 +19,7 @@ import uuid import ddt import six -from zaqar.tests.functional import base # noqa +from zaqar.tests.functional import base from zaqar.tests.functional import helpers