def _test_hostport(self, url_or_host, expected_host, provided_prefix=None, expected_port=None, expected_prefix=None, expected_ssl=False): host, port, prefix, ssl = utils.host_info_from_target( url_or_host, provided_prefix) # normalize hosts, they are case insensitive self.assertEqual(expected_host.lower(), host.lower()) # port can be a string or int depending on the inputs self.assertEqual(expected_port, port) self.assertEqual(expected_prefix, prefix) self.assertEqual(expected_ssl, ssl)
def run(): """Run simple tests from STDIN. This command provides a way to run a set of tests encoded in YAML that is provided on STDIN. No fixtures are supported, so this is primarily designed for use with real running services. Host and port information may be provided in three different ways: * In the URL value of the tests. * In a `host` or `host:port` argument on the command line. * In a URL on the command line. An example run might looks like this:: gabbi-run example.com:9999 < mytest.yaml or:: gabbi-run http://example.com:999 < mytest.yaml It is also possible to provide a URL prefix which can be useful if the target application might be mounted in different locations. An example:: gabbi-run example.com:9999 /mountpoint < mytest.yaml or:: gabbi-run http://example.com:9999/mountpoint < mytest.yaml Use `-x` or `--failfast` to abort after the first error or failure:: gabbi-run -x example.com:9999 /mountpoint < mytest.yaml Use `-v` or `--verbose` with a value of `all`, `headers` or `body` to turn on verbosity for all tests being run. Multiple files may be named as arguments, separated from other arguments by a ``--``. Each file will be run as a separate test suite:: gabbi-run http://example.com -- /path/to/x.yaml /path/to/y.yaml Output is formatted as unittest summary information. """ parser = _make_argparser() argv, input_files = extract_file_paths(sys.argv) args = parser.parse_args(argv[1:]) host, port, prefix, force_ssl = utils.host_info_from_target( args.target, args.prefix) handler_objects = initialize_handlers(args.response_handlers) verbosity = args.verbosity failfast = args.failfast failure = False if not input_files: success = run_suite(sys.stdin, handler_objects, host, port, prefix, force_ssl, failfast, verbosity=verbosity) failure = not success else: for input_file in input_files: with open(input_file, 'r') as fh: data_dir = os.path.dirname(input_file) success = run_suite(fh, handler_objects, host, port, prefix, force_ssl, failfast, data_dir=data_dir, verbosity=verbosity) if not failure: # once failed, this is considered immutable failure = not success if failure and failfast: break sys.exit(failure)
def build_tests(path, loader, host=None, port=8001, intercept=None, test_loader_name=None, fixture_module=None, response_handlers=None, content_handlers=None, prefix='', require_ssl=False, cert_validate=True, url=None, inner_fixtures=None, verbose=False, use_prior_test=True, safe_yaml=True): """Read YAML files from a directory to create tests. Each YAML file represents a list of HTTP requests. :param path: The directory where yaml files are located. :param loader: The TestLoader. :param host: The host to test against. Do not use with ``intercept``. :param port: The port to test against. Used with ``host``. :param intercept: WSGI app factory for wsgi-intercept. :param test_loader_name: Base name for test classes. Use this to align the naming of the tests with other tests in a system. :param fixture_module: Python module containing fixture classes. :param response_handers: :class:`~gabbi.handlers.ResponseHandler` classes. :type response_handlers: List of ResponseHandler classes. :param content_handlers: ContentHandler classes. :type content_handlers: List of ContentHandler classes. :param prefix: A URL prefix for all URLs that are not fully qualified. :param url: A full URL to test against. Replaces host, port and prefix. :param require_ssl: If ``True``, make all tests default to using SSL. :param inner_fixtures: A list of ``Fixtures`` to use with each individual test request. :type inner_fixtures: List of classes with setUp and cleanUp methods to be used as fixtures. :param verbose: If ``True`` or ``'all'``, make tests verbose by default ``'headers'`` and ``'body'`` are also accepted. :param use_prior_test: If ``True``, uses prior test to create ordered sequence of tests :param safe_yaml: If ``True``, recognizes only standard YAML tags and not Python object :param cert_validate: If ``False`` ssl server certificate will be ignored, further it will not be validated if provided (set cert_reqs=CERT_NONE to the Http object) :rtype: TestSuite containing multiple TestSuites (one for each YAML file). """ # If url is being used, reset host, port and prefix. if url: host, port, prefix, force_ssl = utils.host_info_from_target(url) if force_ssl and not require_ssl: require_ssl = force_ssl # Exit immediately if we have no host to access, either via a real host # or an intercept. if not ((host is not None) ^ bool(intercept)): raise AssertionError( 'must specify exactly one of host or url, or intercept') # If the client has not provided a name to use as our base, # create one so that tests are effectively namespaced. if test_loader_name is None: all_test_base_name = inspect.stack()[1] all_test_base_name = os.path.splitext( os.path.basename(all_test_base_name[1]))[0] else: all_test_base_name = None # Initialize response and content handlers. This is effectively # duplication of effort but not results. This allows for # backwards compatibility for existing callers. response_handlers = response_handlers or [] content_handlers = content_handlers or [] handler_objects = [] for handler in (content_handlers + response_handlers + handlers.RESPONSE_HANDLERS): handler_objects.append(handler()) top_suite = suite.TestSuite() for test_file in glob.iglob('%s/*.yaml' % path): if '_' in os.path.basename(test_file): warnings.warn( exception.GabbiSyntaxWarning( "'_' in test filename %s. This can break suite grouping." % test_file)) if intercept: host = str(uuid.uuid4()) suite_dict = utils.load_yaml(yaml_file=test_file, safe=safe_yaml) test_base_name = os.path.splitext(os.path.basename(test_file))[0] if all_test_base_name: test_base_name = '%s_%s' % (all_test_base_name, test_base_name) if require_ssl: if 'defaults' in suite_dict: suite_dict['defaults']['ssl'] = True else: suite_dict['defaults'] = {'ssl': True} if any((verbose == opt for opt in [True, 'all', 'headers', 'body'])): if 'defaults' in suite_dict: suite_dict['defaults']['verbose'] = verbose else: suite_dict['defaults'] = {'verbose': verbose} if not cert_validate: if 'defaults' in suite_dict: suite_dict['defaults']['cert_validate'] = False else: suite_dict['defaults'] = {'cert_validate': False} if not use_prior_test: if 'defaults' in suite_dict: suite_dict['defaults']['use_prior_test'] = use_prior_test else: suite_dict['defaults'] = {'use_prior_test': use_prior_test} file_suite = suitemaker.test_suite_from_dict( loader, test_base_name, suite_dict, path, host, port, fixture_module, intercept, prefix=prefix, test_loader_name=test_loader_name, handlers=handler_objects, inner_fixtures=inner_fixtures) top_suite.addTest(file_suite) return top_suite
def build_tests(path, loader, host=None, port=8001, intercept=None, test_loader_name=None, fixture_module=None, response_handlers=None, content_handlers=None, prefix='', require_ssl=False, url=None, inner_fixtures=None, verbose=False, use_prior_test=True, safe_yaml=True): """Read YAML files from a directory to create tests. Each YAML file represents a list of HTTP requests. :param path: The directory where yaml files are located. :param loader: The TestLoader. :param host: The host to test against. Do not use with ``intercept``. :param port: The port to test against. Used with ``host``. :param intercept: WSGI app factory for wsgi-intercept. :param test_loader_name: Base name for test classes. Use this to align the naming of the tests with other tests in a system. :param fixture_module: Python module containing fixture classes. :param response_handers: :class:`~gabbi.handlers.ResponseHandler` classes. :type response_handlers: List of ResponseHandler classes. :param content_handlers: ContentHandler classes. :type content_handlers: List of ContentHandler classes. :param prefix: A URL prefix for all URLs that are not fully qualified. :param url: A full URL to test against. Replaces host, port and prefix. :param require_ssl: If ``True``, make all tests default to using SSL. :param inner_fixtures: A list of ``Fixtures`` to use with each individual test request. :param verbose: If ``True`` or ``'all'``, make tests verbose by default ``'headers'`` and ``'body'`` are also accepted. :param use_prior_test: If ``True``, uses prior test to create ordered sequence of tests :param safe_yaml: If ``True``, recognizes only standard YAML tags and not Python object :type inner_fixtures: List of fixtures.Fixture classes. :rtype: TestSuite containing multiple TestSuites (one for each YAML file). """ # If url is being used, reset host, port and prefix. if url: host, port, prefix, force_ssl = utils.host_info_from_target(url) if force_ssl and not require_ssl: require_ssl = force_ssl # Exit immediately if we have no host to access, either via a real host # or an intercept. if not ((host is not None) ^ bool(intercept)): raise AssertionError( 'must specify exactly one of host or url, or intercept') # If the client has not provided a name to use as our base, # create one so that tests are effectively namespaced. if test_loader_name is None: all_test_base_name = inspect.stack()[1] all_test_base_name = os.path.splitext( os.path.basename(all_test_base_name[1]))[0] else: all_test_base_name = None # Initialize response and content handlers. This is effectively # duplication of effort but not results. This allows for # backwards compatibility for existing callers. response_handlers = response_handlers or [] content_handlers = content_handlers or [] handler_objects = [] for handler in (content_handlers + response_handlers + handlers.RESPONSE_HANDLERS): handler_objects.append(handler()) top_suite = suite.TestSuite() for test_file in glob.iglob('%s/*.yaml' % path): if '_' in os.path.basename(test_file): warnings.warn(exception.GabbiSyntaxWarning( "'_' in test filename %s. This can break suite grouping." % test_file)) if intercept: host = str(uuid.uuid4()) suite_dict = utils.load_yaml(yaml_file=test_file, safe=safe_yaml) test_base_name = os.path.splitext(os.path.basename(test_file))[0] if all_test_base_name: test_base_name = '%s_%s' % (all_test_base_name, test_base_name) if require_ssl: if 'defaults' in suite_dict: suite_dict['defaults']['ssl'] = True else: suite_dict['defaults'] = {'ssl': True} if any((verbose == opt for opt in [True, 'all', 'headers', 'body'])): if 'defaults' in suite_dict: suite_dict['defaults']['verbose'] = verbose else: suite_dict['defaults'] = {'verbose': verbose} if not use_prior_test: if 'defaults' in suite_dict: suite_dict['defaults']['use_prior_test'] = use_prior_test else: suite_dict['defaults'] = {'use_prior_test': use_prior_test} file_suite = suitemaker.test_suite_from_dict( loader, test_base_name, suite_dict, path, host, port, fixture_module, intercept, prefix=prefix, test_loader_name=test_loader_name, handlers=handler_objects, inner_fixtures=inner_fixtures) top_suite.addTest(file_suite) return top_suite
def run(): """Run simple tests from STDIN. This command provides a way to run a set of tests encoded in YAML that is provided on STDIN. No fixtures are supported, so this is primarily designed for use with real running services. Host and port information may be provided in three different ways: * In the URL value of the tests. * In a `host` or `host:port` argument on the command line. * In a URL on the command line. An example run might looks like this:: gabbi-run example.com:9999 < mytest.yaml or:: gabbi-run http://example.com:999 < mytest.yaml It is also possible to provide a URL prefix which can be useful if the target application might be mounted in different locations. An example:: gabbi-run example.com:9999 /mountpoint < mytest.yaml or:: gabbi-run http://example.com:9999/mountpoint < mytest.yaml Use `-x` or `--failfast` to abort after the first error or failure:: gabbi-run -x example.com:9999 /mountpoint < mytest.yaml Use `-v` or `--verbose` with a value of `all`, `headers` or `body` to turn on verbosity for all tests being run. Multiple files may be named as arguments, separated from other arguments by a ``--``. Each file will be run as a separate test suite:: gabbi-run http://example.com -- /path/to/x.yaml /path/to/y.yaml Output is formatted as unittest summary information. """ parser = _make_argparser() argv, input_files = extract_file_paths(sys.argv) args = parser.parse_args(argv[1:]) host, port, prefix, force_ssl = utils.host_info_from_target( args.target, args.prefix) handler_objects = initialize_handlers(args.response_handlers) verbosity = args.verbosity failfast = args.failfast failure = False # Keep track of file names that have failures. failures = [] if not input_files: success = run_suite(sys.stdin, handler_objects, host, port, prefix, force_ssl, failfast, verbosity=verbosity, safe_yaml=args.safe_yaml) failure = not success else: for input_file in input_files: name = os.path.splitext(os.path.basename(input_file))[0] with open(input_file, 'r') as fh: data_dir = os.path.dirname(input_file) success = run_suite(fh, handler_objects, host, port, prefix, force_ssl, failfast, data_dir=data_dir, verbosity=verbosity, name=name, safe_yaml=args.safe_yaml) if not success: failures.append(input_file) if not failure: # once failed, this is considered immutable failure = not success if failure and failfast: break if failures: print("There were failures in the following files:", file=sys.stderr) print('\n'.join(failures), file=sys.stderr) sys.exit(failure)