Extract suitemaking out of driver

This creates a new module, suitemaker.py, which contains the
code used to create a suite.GabbiSuite, which contains
case.HTTPTestCase tests.

This limits driver.py to being mostly code that is necessary to
assemble the necessary information to create tests that are runnable
by python test harnesses. This is distinguished from runner.py
which assembles the necessary information to both create and _run_
tests itself.

The idea here is that if the interface presented by test_suite_from_dict
(both signature and that it returns a GabbiSuite) is preserved, then
anything in suitemaker.py is welcome to change. The signature on
test_suite_from_dict is the definition of what a caller (driver or runner
or whatever else) needs to have learned:

loader: the unittest.TestLoader which we might be able to default
        out of existence
test_base_name: names help with test grouping so useful
suite_dict: the main piece of the pie, the dict
test_directory: the place where `data` can load files from
host: the host any unqualified test goes to
port: the port any unqualified test goes to
fixture_module: the sole module from which fixtures come
intercept: the wsgi app to intercept if any
prefix: the url prefix

host and intercept are mutually exclusive

One can image it might be possible to replace host, port, prefix
with a URL instead. If that were done than host and url would be mutually
exclusive.

If the signature of test_suite_from_dict changes then stuff in both driver
and runner, in how they assemble the required info, would need to change.

Note: For reasons unclear these changes have exposed some state management
issues in test_runner, noted in a NOTE.
This commit is contained in:
Chris Dent 2016-06-03 15:07:57 +01:00
parent e5ace71190
commit 1b31ef02e4
10 changed files with 420 additions and 338 deletions

View File

@ -23,7 +23,6 @@ Each sequence becomes a TestSuite.
An entire directory of YAML files is a TestSuite of TestSuites.
"""
import copy
import glob
import inspect
import io
@ -32,158 +31,12 @@ import unittest
from unittest import suite
import uuid
import six
import yaml
from gabbi import case
from gabbi import handlers
from gabbi import httpclient
from gabbi import reporter
from gabbi import suite as gabbi_suite
RESPONSE_HANDLERS = [
handlers.ForbiddenHeadersResponseHandler,
handlers.HeadersResponseHandler,
handlers.StringResponseHandler,
handlers.JSONResponseHandler,
]
class GabbiFormatError(ValueError):
"""An exception to encapsulate poorly formed test data."""
pass
class TestMaker(object):
"""A class for encapsulating test invariants.
All of the tests in a single gabbi file have invariants which are
provided when creating each HTTPTestCase. It is not useful
to pass these around when making each test case. So they are
wrapped in this class which then has make_one_test called multiple
times to generate all the tests in the suite.
"""
def __init__(self, test_base_name, test_defaults, test_directory,
fixture_classes, loader, host, port, intercept, prefix):
self.test_base_name = test_base_name
self.test_defaults = test_defaults
self.default_keys = set(test_defaults.keys())
self.test_directory = test_directory
self.fixture_classes = fixture_classes
self.host = host
self.port = port
self.loader = loader
self.intercept = intercept
self.prefix = prefix
def make_one_test(self, test_dict, prior_test):
"""Create one single HTTPTestCase.
The returned HTTPTestCase is added to the TestSuite currently
being built (one per YAML file).
"""
test = copy.deepcopy(self.test_defaults)
try:
test_update(test, test_dict)
except KeyError as exc:
raise GabbiFormatError('invalid key in test: %s' % exc)
except AttributeError as exc:
if not isinstance(test_dict, dict):
raise GabbiFormatError(
'test chunk is not a dict at "%s"' % test_dict)
else:
# NOTE(cdent): Not clear this can happen but just in case.
raise GabbiFormatError(
'malformed test chunk "%s": %s' % (test_dict, exc))
test_name = self._set_test_name(test)
self._set_test_method_and_url(test, test_name)
self._validate_keys(test, test_name)
http_class = httpclient.get_http(verbose=test['verbose'],
caption=test['name'])
# Use metaclasses to build a class of the necessary type
# and name with relevant arguments.
klass = TestBuilder(test_name, (case.HTTPTestCase,),
{'test_data': test,
'test_directory': self.test_directory,
'fixtures': self.fixture_classes,
'http': http_class,
'host': self.host,
'intercept': self.intercept,
'port': self.port,
'prefix': self.prefix,
'prior': prior_test})
tests = self.loader.loadTestsFromTestCase(klass)
# Return the first (and only) test in the klass.
return tests._tests[0]
def _set_test_name(self, test):
"""Set the name of the test
The original name is lowercased and spaces are replaces with '_'.
The result is appended to the test_base_name, which is based on the
name of the input data file.
"""
if not test['name']:
raise GabbiFormatError('Test name missing in a test in %s.'
% self.test_base_name)
return '%s_%s' % (self.test_base_name,
test['name'].lower().replace(' ', '_'))
@staticmethod
def _set_test_method_and_url(test, test_name):
"""Extract the base URL and method for this test.
If there is an upper case key in the test, that is used as the
method and the value is used as the URL. If there is more than
one uppercase that is a GabbiFormatError.
If there is no upper case key then 'url' must be present.
"""
method_key = None
for key, val in six.iteritems(test):
if _is_method_shortcut(key):
if method_key:
raise GabbiFormatError(
'duplicate method/URL directive in "%s"' %
test_name)
test['method'] = key
test['url'] = val
method_key = key
if method_key:
del test[method_key]
if not test['url']:
raise GabbiFormatError('Test url missing in test %s.'
% test_name)
def _validate_keys(self, test, test_name):
"""Check for invalid keys.
If there are any, raise a GabbiFormatError.
"""
test_keys = set(test.keys())
if test_keys != self.default_keys:
raise GabbiFormatError(
'Invalid test keys used in test %s: %s'
% (test_name,
', '.join(list(test_keys - self.default_keys))))
class TestBuilder(type):
"""Metaclass to munge a dynamically created test."""
required_attributes = {'has_run': False}
def __new__(mcs, name, bases, attributes):
attributes.update(mcs.required_attributes)
return type.__new__(mcs, name, bases, attributes)
from gabbi import suitemaker
def build_tests(path, loader, host=None, port=8001, intercept=None,
@ -219,7 +72,7 @@ def build_tests(path, loader, host=None, port=8001, intercept=None,
# Initialize response handlers.
response_handlers = response_handlers or []
for handler in RESPONSE_HANDLERS + response_handlers:
for handler in handlers.RESPONSE_HANDLERS + response_handlers:
handler(case.HTTPTestCase)
top_suite = suite.TestSuite()
@ -236,9 +89,9 @@ def build_tests(path, loader, host=None, port=8001, intercept=None,
else:
suite_dict['defaults'] = {'ssl': True}
file_suite = test_suite_from_dict(loader, test_base_name, suite_dict,
path, host, port, fixture_module,
intercept, prefix)
file_suite = suitemaker.test_suite_from_dict(
loader, test_base_name, suite_dict, path, host, port,
fixture_module, intercept, prefix)
top_suite.addTest(file_suite)
return top_suite
@ -277,57 +130,6 @@ def load_yaml(yaml_file):
return yaml.safe_load(source.read())
def test_update(orig_dict, new_dict):
"""Modify test in place to update with new data."""
for key, val in six.iteritems(new_dict):
if key == 'data':
orig_dict[key] = val
elif isinstance(val, dict):
orig_dict[key].update(val)
elif isinstance(val, list):
orig_dict[key] = orig_dict.get(key, []) + val
else:
orig_dict[key] = val
def test_suite_from_dict(loader, test_base_name, suite_dict, test_directory,
host, port, fixture_module, intercept, prefix=''):
"""Generate a TestSuite from YAML-sourced data."""
try:
test_data = suite_dict['tests']
except KeyError:
raise GabbiFormatError('malformed test file, "tests" key required')
except TypeError:
# `suite_dict` appears not to be a dictionary; we cannot infer
# any details or suggestions on how to fix it, thus discarding
# the original exception in favor of a generic error
raise GabbiFormatError('malformed test file, invalid format')
# Merge global with per-suite defaults
default_test_dict = copy.deepcopy(case.HTTPTestCase.base_test)
local_defaults = _validate_defaults(suite_dict.get('defaults', {}))
test_update(default_test_dict, local_defaults)
# Establish any fixture classes used in this file.
fixtures = suite_dict.get('fixtures', None)
fixture_classes = []
if fixtures and fixture_module:
for fixture_class in fixtures:
fixture_classes.append(getattr(fixture_module, fixture_class))
test_maker = TestMaker(test_base_name, default_test_dict, test_directory,
fixture_classes, loader, host, port, intercept,
prefix)
file_suite = gabbi_suite.GabbiSuite()
prior_test = None
for test_dict in test_data:
this_test = test_maker.make_one_test(test_dict, prior_test)
file_suite.addTest(this_test)
prior_test = this_test
return file_suite
def test_suite_from_yaml(loader, test_base_name, test_yaml, test_directory,
host, port, fixture_module, intercept, prefix=''):
"""Legacy wrapper retained for backwards compatibility."""
@ -337,24 +139,6 @@ def test_suite_from_yaml(loader, test_base_name, test_yaml, test_directory,
warnings.simplefilter('default', DeprecationWarning)
warnings.warn('test_suite_from_yaml has been renamed to '
'test_suite_from_dict', DeprecationWarning, stacklevel=2)
return test_suite_from_dict(loader, test_base_name, test_yaml,
test_directory, host, port, fixture_module,
intercept, prefix)
def _validate_defaults(defaults):
"""Ensure default test settings are acceptable.
Raises GabbiFormatError for invalid settings.
"""
if any(_is_method_shortcut(key) for key in defaults):
raise GabbiFormatError('"METHOD: url" pairs not allowed in defaults')
return defaults
def _is_method_shortcut(key):
"""Is this test key indicating a request method.
It is a request method if it is all upper case.
"""
return key.isupper()
return suitemaker.test_suite_from_dict(
loader, test_base_name, test_yaml, test_directory, host, port,
fixture_module, intercept, prefix)

18
gabbi/exception.py Normal file
View File

@ -0,0 +1,18 @@
#
# 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.
"""Gabbi specific exceptions."""
class GabbiFormatError(ValueError):
"""An exception to encapsulate poorly formed test data."""
pass

View File

@ -158,3 +158,14 @@ class HeadersResponseHandler(ResponseHandler):
test.assertEqual(header_value, response_value,
'Expect header %s with value %s, got %s' %
(header, header_value, response[header]))
# A list of these handlers for easy traversal.
# TODO(cdent): We could automate this, but meh.
# When the content-handler changes are done this can be cleaned up.
RESPONSE_HANDLERS = [
ForbiddenHeadersResponseHandler,
HeadersResponseHandler,
StringResponseHandler,
JSONResponseHandler,
]

View File

@ -21,8 +21,9 @@ from six.moves.urllib import parse as urlparse
import yaml
from gabbi import case
from gabbi import driver
from gabbi import handlers
from gabbi.reporter import ConciseTestRunner
from gabbi import suitemaker
def run():
@ -117,7 +118,7 @@ def run():
for import_path in args.response_handlers or []:
for handler in load_response_handlers(import_path):
custom_response_handlers.append(handler)
for handler in driver.RESPONSE_HANDLERS + custom_response_handlers:
for handler in handlers.RESPONSE_HANDLERS + custom_response_handlers:
handler(case.HTTPTestCase)
data = yaml.safe_load(sys.stdin.read())
@ -129,10 +130,10 @@ def run():
else:
data['defaults'] = {'ssl': True}
loader = unittest.defaultTestLoader
suite = driver.test_suite_from_dict(loader, 'input', data, '.',
host, port, None, None,
prefix=prefix)
result = ConciseTestRunner(verbosity=2, failfast=args.failfast).run(suite)
test_suite = suitemaker.test_suite_from_dict(
loader, 'input', data, '.', host, port, None, None, prefix=prefix)
result = ConciseTestRunner(
verbosity=2, failfast=args.failfast).run(test_suite)
sys.exit(not result.wasSuccessful())

View File

@ -16,8 +16,7 @@ This suite has two features: the contained tests are ordered and there
are suite-level fixtures that operate as context managers.
"""
from unittest import case
from unittest import suite
import unittest
from wsgi_intercept import interceptor
@ -29,7 +28,7 @@ def noop(*args):
pass
class GabbiSuite(suite.TestSuite):
class GabbiSuite(unittest.TestSuite):
"""A TestSuite with fixtures.
The suite wraps the tests with a set of nested context managers that
@ -56,7 +55,7 @@ class GabbiSuite(suite.TestSuite):
result = super(GabbiSuite, self).run(result, debug)
else:
result = super(GabbiSuite, self).run(result, debug)
except case.SkipTest as exc:
except unittest.SkipTest as exc:
for test in self._tests:
result.addSkip(test, str(exc))
@ -72,7 +71,7 @@ class GabbiSuite(suite.TestSuite):
fix_object = fix()
fix_object.__enter__()
self.used_fixtures.append(fix_object)
except case.SkipTest as exc:
except unittest.SkipTest as exc:
# Disable the already collected tests that we now wish
# to skip.
for test in self:

235
gabbi/suitemaker.py Normal file
View File

@ -0,0 +1,235 @@
#
# 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.
"""The code that creates a suite of tests.
The key piece of code ``test_suite_from_dict``. It produces a
``gabbi.suite.GabbiSuite`` containing one or more
``gabbi.case.HTTPTestCase``.
"""
import copy
from gabbi import case
from gabbi.exception import GabbiFormatError
from gabbi import httpclient
from gabbi import suite
class TestMaker(object):
"""A class for encapsulating test invariants.
All of the tests in a single gabbi file have invariants which are
provided when creating each HTTPTestCase. It is not useful
to pass these around when making each test case. So they are
wrapped in this class which then has make_one_test called multiple
times to generate all the tests in the suite.
"""
def __init__(self, test_base_name, test_defaults, test_directory,
fixture_classes, loader, host, port, intercept, prefix):
self.test_base_name = test_base_name
self.test_defaults = test_defaults
self.default_keys = set(test_defaults.keys())
self.test_directory = test_directory
self.fixture_classes = fixture_classes
self.host = host
self.port = port
self.loader = loader
self.intercept = intercept
self.prefix = prefix
def make_one_test(self, test_dict, prior_test):
"""Create one single HTTPTestCase.
The returned HTTPTestCase is added to the TestSuite currently
being built (one per YAML file).
"""
test = copy.deepcopy(self.test_defaults)
try:
test_update(test, test_dict)
except KeyError as exc:
raise GabbiFormatError('invalid key in test: %s' % exc)
except AttributeError as exc:
if not isinstance(test_dict, dict):
raise GabbiFormatError(
'test chunk is not a dict at "%s"' % test_dict)
else:
# NOTE(cdent): Not clear this can happen but just in case.
raise GabbiFormatError(
'malformed test chunk "%s": %s' % (test_dict, exc))
test_name = self._set_test_name(test)
self._set_test_method_and_url(test, test_name)
self._validate_keys(test, test_name)
http_class = httpclient.get_http(verbose=test['verbose'],
caption=test['name'])
# Use metaclasses to build a class of the necessary type
# and name with relevant arguments.
klass = TestBuilder(test_name, (case.HTTPTestCase,),
{'test_data': test,
'test_directory': self.test_directory,
'fixtures': self.fixture_classes,
'http': http_class,
'host': self.host,
'intercept': self.intercept,
'port': self.port,
'prefix': self.prefix,
'prior': prior_test})
tests = self.loader.loadTestsFromTestCase(klass)
# Return the first (and only) test in the klass.
return tests._tests[0]
def _set_test_name(self, test):
"""Set the name of the test
The original name is lowercased and spaces are replaces with '_'.
The result is appended to the test_base_name, which is based on the
name of the input data file.
"""
if not test['name']:
raise GabbiFormatError('Test name missing in a test in %s.'
% self.test_base_name)
return '%s_%s' % (self.test_base_name,
test['name'].lower().replace(' ', '_'))
@staticmethod
def _set_test_method_and_url(test, test_name):
"""Extract the base URL and method for this test.
If there is an upper case key in the test, that is used as the
method and the value is used as the URL. If there is more than
one uppercase that is a GabbiFormatError.
If there is no upper case key then 'url' must be present.
"""
method_key = None
for key, val in test.items():
if _is_method_shortcut(key):
if method_key:
raise GabbiFormatError(
'duplicate method/URL directive in "%s"' %
test_name)
test['method'] = key
test['url'] = val
method_key = key
if method_key:
del test[method_key]
if not test['url']:
raise GabbiFormatError('Test url missing in test %s.'
% test_name)
def _validate_keys(self, test, test_name):
"""Check for invalid keys.
If there are any, raise a GabbiFormatError.
"""
test_keys = set(test.keys())
if test_keys != self.default_keys:
raise GabbiFormatError(
'Invalid test keys used in test %s: %s'
% (test_name,
', '.join(list(test_keys - self.default_keys))))
class TestBuilder(type):
"""Metaclass to munge a dynamically created test."""
required_attributes = {'has_run': False}
def __new__(mcs, name, bases, attributes):
attributes.update(mcs.required_attributes)
return type.__new__(mcs, name, bases, attributes)
def test_suite_from_dict(loader, test_base_name, suite_dict, test_directory,
host, port, fixture_module, intercept, prefix=''):
"""Generate a GabbiSuite from a dict represent a list of tests.
The dict takes the form:
:param fixtures: An optional list of fixture classes that this suite
can use.
:param defaults: An optional dictionary of default values to be used
in each test.
:param tests: A list of individual tests, themselves each being a
dictionary. TODO: link to the case.BASE_TEST.
"""
try:
test_data = suite_dict['tests']
except KeyError:
raise GabbiFormatError('malformed test file, "tests" key required')
except TypeError:
# `suite_dict` appears not to be a dictionary; we cannot infer
# any details or suggestions on how to fix it, thus discarding
# the original exception in favor of a generic error
raise GabbiFormatError('malformed test file, invalid format')
# Merge global with per-suite defaults
default_test_dict = copy.deepcopy(case.HTTPTestCase.base_test)
local_defaults = _validate_defaults(suite_dict.get('defaults', {}))
test_update(default_test_dict, local_defaults)
# Establish any fixture classes used in this file.
fixtures = suite_dict.get('fixtures', None)
fixture_classes = []
if fixtures and fixture_module:
for fixture_class in fixtures:
fixture_classes.append(getattr(fixture_module, fixture_class))
test_maker = TestMaker(test_base_name, default_test_dict, test_directory,
fixture_classes, loader, host, port, intercept,
prefix)
file_suite = suite.GabbiSuite()
prior_test = None
for test_dict in test_data:
this_test = test_maker.make_one_test(test_dict, prior_test)
file_suite.addTest(this_test)
prior_test = this_test
return file_suite
def test_update(orig_dict, new_dict):
"""Modify test in place to update with new data."""
for key, val in new_dict.items():
if key == 'data':
orig_dict[key] = val
elif isinstance(val, dict):
orig_dict[key].update(val)
elif isinstance(val, list):
orig_dict[key] = orig_dict.get(key, []) + val
else:
orig_dict[key] = val
def _is_method_shortcut(key):
"""Is this test key indicating a request method.
It is a request method if it is all upper case.
"""
return key.isupper()
def _validate_defaults(defaults):
"""Ensure default test settings are acceptable.
Raises GabbiFormatError for invalid settings.
"""
if any(_is_method_shortcut(key) for key in defaults):
raise GabbiFormatError('"METHOD: url" pairs not allowed in defaults')
return defaults

View File

@ -70,99 +70,3 @@ class DriverTest(unittest.TestCase):
first_test = suite._tests[0]._tests[0]
full_url = first_test._parse_url(first_test.test_data['url'])
self.assertEqual('http://localhost:8001/', full_url)
def test_tests_key_required(self):
test_yaml = {'name': 'house', 'url': '/'}
with self.assertRaises(driver.GabbiFormatError) as failure:
driver.test_suite_from_dict(self.loader, 'foo', test_yaml, '.',
'localhost', 80, None, None)
self.assertEqual('malformed test file, "tests" key required',
str(failure.exception))
def test_upper_dict_required(self):
test_yaml = [{'name': 'house', 'url': '/'}]
with self.assertRaises(driver.GabbiFormatError) as failure:
driver.test_suite_from_dict(self.loader, 'foo', test_yaml, '.',
'localhost', 80, None, None)
self.assertEqual('malformed test file, invalid format',
str(failure.exception))
def test_inner_list_required(self):
test_yaml = {'tests': {'name': 'house', 'url': '/'}}
with self.assertRaises(driver.GabbiFormatError) as failure:
driver.test_suite_from_dict(self.loader, 'foo', test_yaml, '.',
'localhost', 80, None, None)
self.assertIn('test chunk is not a dict at',
str(failure.exception))
def test_name_key_required(self):
test_yaml = {'tests': [{'url': '/'}]}
with self.assertRaises(driver.GabbiFormatError) as failure:
driver.test_suite_from_dict(self.loader, 'foo', test_yaml, '.',
'localhost', 80, None, None)
self.assertEqual('Test name missing in a test in foo.',
str(failure.exception))
def test_url_key_required(self):
test_yaml = {'tests': [{'name': 'missing url'}]}
with self.assertRaises(driver.GabbiFormatError) as failure:
driver.test_suite_from_dict(self.loader, 'foo', test_yaml, '.',
'localhost', 80, None, None)
self.assertEqual('Test url missing in test foo_missing_url.',
str(failure.exception))
def test_unsupported_key_errors(self):
test_yaml = {'tests': [{
'url': '/',
'name': 'simple',
'bad_key': 'wow',
}]}
with self.assertRaises(driver.GabbiFormatError) as failure:
driver.test_suite_from_dict(self.loader, 'foo', test_yaml, '.',
'localhost', 80, None, None)
self.assertIn("Invalid test keys used in test foo_simple:",
str(failure.exception))
def test_method_url_pair_format_error(self):
test_yaml = {'defaults': {'GET': '/foo'}, 'tests': []}
with self.assertRaises(driver.GabbiFormatError) as failure:
driver.test_suite_from_dict(self.loader, 'foo', test_yaml, '.',
'localhost', 80, None, None)
self.assertIn('"METHOD: url" pairs not allowed in defaults',
str(failure.exception))
def test_method_url_pair_duplication_format_error(self):
test_yaml = {'tests': [{
'GET': '/',
'POST': '/',
'name': 'duplicate methods',
}]}
with self.assertRaises(driver.GabbiFormatError) as failure:
driver.test_suite_from_dict(self.loader, 'foo', test_yaml, '.',
'localhost', 80, None, None)
self.assertIn(
'duplicate method/URL directive in "foo_duplicate_methods"',
str(failure.exception)
)
def test_dict_on_invalid_key(self):
test_yaml = {'tests': [{
'name': '...',
'GET': '/',
'response_html': {
'foo': 'hello',
'bar': 'world',
}
}]}
with self.assertRaises(driver.GabbiFormatError) as failure:
driver.test_suite_from_dict(self.loader, 'foo', test_yaml, '.',
'localhost', 80, None, None)
self.assertIn(
"invalid key in test: 'response_html'",
str(failure.exception)
)

View File

@ -17,8 +17,8 @@ import json
import unittest
from gabbi import case
from gabbi import driver
from gabbi import handlers
from gabbi import suitemaker
class HandlersTest(unittest.TestCase):
@ -31,8 +31,8 @@ class HandlersTest(unittest.TestCase):
def setUp(self):
super(HandlersTest, self).setUp()
self.test_class = case.HTTPTestCase
self.test = driver.TestBuilder('mytest', (self.test_class,),
{'test_data': {}})
self.test = suitemaker.TestBuilder('mytest', (self.test_class,),
{'test_data': {}})
def test_response_strings(self):
handler = handlers.StringResponseHandler(self.test_class)

View File

@ -13,6 +13,7 @@
"""Test that the CLI works as expected
"""
import copy
import mock
import sys
import unittest
@ -21,7 +22,8 @@ from uuid import uuid4
from six import StringIO
from wsgi_intercept.interceptor import Urllib3Interceptor
from gabbi import driver
from gabbi import case
from gabbi import exception
from gabbi import handlers
from gabbi import runner
from gabbi.tests.simple_wsgi import SimpleWsgi
@ -55,6 +57,9 @@ class RunnerTest(unittest.TestCase):
sys.stdout = self._stdout
sys.stderr = self._stderr
sys.argv = self._argv
# Cleanup the custom response_handler
case.HTTPTestCase.response_handlers = []
case.HTTPTestCase.base_test = copy.copy(case.BASE_TEST)
def test_target_url_parsing(self):
sys.argv = ['gabbi-run', 'http://%s:%s/foo' % (self.host, self.port)]
@ -74,6 +79,11 @@ class RunnerTest(unittest.TestCase):
self.assertSuccess(err)
def test_target_url_parsing_standard_port(self):
# NOTE(cdent): For reasons unclear this regularly fails in
# py.test and sometimes fails with testr. So there is
# some state that is not being properly cleard somewhere.
# Within SimpleWsgi, the environ thinks url_scheme is
# 'https'.
self.server = lambda: Urllib3Interceptor(
SimpleWsgi, self.host, 80, '')
sys.argv = ['gabbi-run', 'http://%s/foo' % self.host]
@ -99,7 +109,7 @@ class RunnerTest(unittest.TestCase):
GET: /
response_html: ...
""")
with self.assertRaises(driver.GabbiFormatError):
with self.assertRaises(exception.GabbiFormatError):
runner.run()
sys.argv.insert(1, "--response-handler")
@ -169,7 +179,7 @@ class RunnerTest(unittest.TestCase):
def test_exit_code(self):
sys.stdin = StringIO()
with self.assertRaises(driver.GabbiFormatError):
with self.assertRaises(exception.GabbiFormatError):
runner.run()
sys.stdin = StringIO("""
@ -223,7 +233,7 @@ class RunnerHostArgParse(unittest.TestCase):
@mock.patch('sys.exit')
@mock.patch('sys.stdin')
@mock.patch('gabbi.driver.test_suite_from_dict')
@mock.patch('gabbi.suitemaker.test_suite_from_dict')
@mock.patch('yaml.safe_load', return_value={})
def _test_hostport(self, url_or_host, expected_host,
portmock_yaml, mock_test_suite, mock_read, mock_exit,

View File

@ -0,0 +1,120 @@
#
# 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 unittest
from gabbi import exception
from gabbi import suitemaker
class SuiteMakerTest(unittest.TestCase):
def setUp(self):
super(SuiteMakerTest, self).setUp()
self.loader = unittest.defaultTestLoader
def test_tests_key_required(self):
test_yaml = {'name': 'house', 'url': '/'}
with self.assertRaises(exception.GabbiFormatError) as failure:
suitemaker.test_suite_from_dict(self.loader, 'foo', test_yaml, '.',
'localhost', 80, None, None)
self.assertEqual('malformed test file, "tests" key required',
str(failure.exception))
def test_upper_dict_required(self):
test_yaml = [{'name': 'house', 'url': '/'}]
with self.assertRaises(exception.GabbiFormatError) as failure:
suitemaker.test_suite_from_dict(self.loader, 'foo', test_yaml, '.',
'localhost', 80, None, None)
self.assertEqual('malformed test file, invalid format',
str(failure.exception))
def test_inner_list_required(self):
test_yaml = {'tests': {'name': 'house', 'url': '/'}}
with self.assertRaises(exception.GabbiFormatError) as failure:
suitemaker.test_suite_from_dict(self.loader, 'foo', test_yaml, '.',
'localhost', 80, None, None)
self.assertIn('test chunk is not a dict at',
str(failure.exception))
def test_name_key_required(self):
test_yaml = {'tests': [{'url': '/'}]}
with self.assertRaises(exception.GabbiFormatError) as failure:
suitemaker.test_suite_from_dict(self.loader, 'foo', test_yaml, '.',
'localhost', 80, None, None)
self.assertEqual('Test name missing in a test in foo.',
str(failure.exception))
def test_url_key_required(self):
test_yaml = {'tests': [{'name': 'missing url'}]}
with self.assertRaises(exception.GabbiFormatError) as failure:
suitemaker.test_suite_from_dict(self.loader, 'foo', test_yaml, '.',
'localhost', 80, None, None)
self.assertEqual('Test url missing in test foo_missing_url.',
str(failure.exception))
def test_unsupported_key_errors(self):
test_yaml = {'tests': [{
'url': '/',
'name': 'simple',
'bad_key': 'wow',
}]}
with self.assertRaises(exception.GabbiFormatError) as failure:
suitemaker.test_suite_from_dict(self.loader, 'foo', test_yaml, '.',
'localhost', 80, None, None)
self.assertIn("Invalid test keys used in test foo_simple:",
str(failure.exception))
def test_method_url_pair_format_error(self):
test_yaml = {'defaults': {'GET': '/foo'}, 'tests': []}
with self.assertRaises(exception.GabbiFormatError) as failure:
suitemaker.test_suite_from_dict(self.loader, 'foo', test_yaml, '.',
'localhost', 80, None, None)
self.assertIn('"METHOD: url" pairs not allowed in defaults',
str(failure.exception))
def test_method_url_pair_duplication_format_error(self):
test_yaml = {'tests': [{
'GET': '/',
'POST': '/',
'name': 'duplicate methods',
}]}
with self.assertRaises(exception.GabbiFormatError) as failure:
suitemaker.test_suite_from_dict(self.loader, 'foo', test_yaml, '.',
'localhost', 80, None, None)
self.assertIn(
'duplicate method/URL directive in "foo_duplicate_methods"',
str(failure.exception)
)
def test_dict_on_invalid_key(self):
test_yaml = {'tests': [{
'name': '...',
'GET': '/',
'response_html': {
'foo': 'hello',
'bar': 'world',
}
}]}
with self.assertRaises(exception.GabbiFormatError) as failure:
suitemaker.test_suite_from_dict(self.loader, 'foo', test_yaml, '.',
'localhost', 80, None, None)
self.assertIn(
"invalid key in test: 'response_html'",
str(failure.exception)
)