예제 #1
0
def pytest_funcarg__api(request):
    server = request.getfuncargvalue("server")
    api = API("XXX", "XXX", "uk")  # locale does not matter here!
    api.host = "%s:%s" % server.server_address
    if hasattr(request, "param"):  # for tests generated by pytest_generate_tests
        api.VERSION = request.param["version"]
        api.REQUESTS_PER_SECOND = 10000  # just for here!
    return api
def pytest_funcarg__api(request):
    server = request.getfuncargvalue('server')
    api = API('XXX', 'XXX', 'uk') # locale does not matter here!
    api.host = '%s:%s' % server.server_address
    if hasattr(request, 'param'): # for tests generated by pytest_generate_tests
        api.VERSION = request.param['version']
        api.REQUESTS_PER_SECOND = 10000 # just for here!
    return api
예제 #3
0
def pytest_funcarg__api(request):
    server = request.getfuncargvalue('server')
    api = API('XXX', 'XXX', 'uk')  # locale does not matter here!
    api.host = '%s:%s' % server.server_address
    if hasattr(request,
               'param'):  # for tests generated by pytest_generate_tests
        api.VERSION = request.param['version']
        api.REQUESTS_PER_SECOND = 10000  # just for here!
    return api
def pytest_funcarg__api(request):
    """
    Initialises API for each test call (formerly done with ``setup_method()``).
    """
    server = request.getfuncargvalue('server')
    url_reg = re.compile(r'^http://(?P<host>[\w\-\.]+)(?P<path>/onca/xml.*)$')

    # the following parameters are injected by pytest_generate_tests
    locale = request.param['locale']
    version = request.param['version']
    xml_response = request.param['xml_response']

    processor = TESTABLE_PROCESSORS[request.param['processor']]
    if isinstance(processor, type):
        processor = processor()

    api = API(locale=locale, processor=processor)
    api.VERSION = version
    api.REQUESTS_PER_SECOND = 10000 # just for here!

    def counter(fnc):
        """
        Wrapper function for ``_fetch`` which

        1. keeps track of the times has been called and adjusts the path to the
           corresponding XML response
        2. Fetches any response that has not been cached from the live servers
        """
        api._count = 0
        def wrapped(url):
            api._count += 1
            path = xml_response
            if api._count > 1:
                root, ext = os.path.splitext(path)
                path = '%s-%i%s' % (root, api._count, ext)
            try:
                if request.config.option.fetch == 'all':
                    raise ResponseRequired
                try:
                    content = open(path, 'r').read()
                    # If the XML response has been previously fetched compare
                    # request arguments in order to see if there are any changes
                    cached_params = utils.arguments_from_cached_xml(content)
                    current_params = utils.arguments_from_url(url)
                    if cached_params != current_params:
                        raise ArgumentMismatch
                except IOError:
                    if request.config.option.fetch in ('outdated', 'missing'):
                        raise ResponseRequired
                    raise pytest.skip('No cached XML response found!')
                except ArgumentMismatch:
                    if request.config.option.fetch == 'outdated':
                        raise ResponseRequired
                    return pytest.skip('Cached arguments in %s differ from the '
                        'ones currently tested against!' % path)
                        #'\ncached=%r\ncurrent=%r' % (path,
                        #cached_params, current_params))
                except AttributeError:
                    # XML for error messages have no Argument elements!
                    pass
            except ResponseRequired:
                # fetch XML via urllib2 rather than directly via
                # lxml.etree.parse() to avoid, for instance, problems with HTTP
                # 403 errors
                try:
                    req = urllib2.Request(url, headers={'User-Agent': USER_AGENT})
                    xml = urllib2.urlopen(req).read()
                except urllib2.HTTPError, e:
                    xml = e.read()
                root = lxml.etree.fromstring(xml)
                # overwrite sensitive information in XML document.
                for arg in root.xpath('//aws:Argument',
                        namespaces={'aws': root.nsmap[None]}):
                    if arg.get('Name') in ('Signature', 'AWSAccessKeyId',
                                                             'AssociateTag'):
                        arg.set('Value', 'X'*15)
                content = lxml.etree.tostring(root, pretty_print=True)
                # complain loudly about missing credentials
                # UNLESS it was actually on purpose!
                if ('MissingClientTokenId' in content
                and getattr(request.function, 'refetch', True)):
                    raise pytest.fail('Cannot fetch XML response without credentials!')
                if not os.path.exists(os.path.dirname(path)):
                    os.mkdir(os.path.dirname(path))
                open(path, 'wb').write(content)
            # We simply exchange the real host with the local one now!
            # Note: Although strictly speaking it does not matter which URL is
            # called exactly, to appeal to one's sense of correctness, let's
            # keep at least the correct path!
            url = url_reg.sub(r'%s\g<path>' % server.url, url)
            server.serve_content(content)
            return fnc(url)
        return wrapped
def pytest_funcarg__api(request):
    """
    Initialises API for each test call (formerly done with ``setup_method()``).
    """
    server = request.getfuncargvalue('server')
    url_reg = re.compile(r'^http://(?P<host>[\w\-\.]+)(?P<path>/onca/xml.*)$')

    # the following parameters are injected by pytest_generate_tests
    locale = request.param.locale
    version = request.param.version
    xml_response = request.param.xml_response

    processor = TESTABLE_PROCESSORS[request.param.processor]
    api = API(locale=locale, processor=processor)
    api.VERSION = version
    api.REQUESTS_PER_SECOND = 10000  # just for here!

    def counter(fnc):
        """
        Wrapper function for ``_fetch`` which

        1. keeps track of the times has been called and adjusts the path to the
           corresponding XML response
        2. Fetches any response that has not been cached from the live servers
        """
        api._count = 0

        def wrapped(url):
            api._count += 1
            path = xml_response
            if api._count > 1:
                root, ext = os.path.splitext(path)
                path = '%s-%i%s' % (root, api._count, ext)
            try:
                if request.config.option.fetch == 'all':
                    raise ResponseRequired
                try:
                    content = open(path, 'r').read()
                    # If the XML response has been previously fetched compare
                    # request arguments in order to see if there are any changes
                    cached_params = utils.arguments_from_cached_xml(content)
                    current_params = utils.arguments_from_url(url)
                    if cached_params != current_params:
                        raise ArgumentMismatch
                except IOError:
                    if request.config.option.fetch in ('outdated', 'missing'):
                        raise ResponseRequired
                    raise pytest.skip('No cached XML response found!')
                except ArgumentMismatch:
                    if request.config.option.fetch == 'outdated':
                        raise ResponseRequired
                    msg = ('Cached arguments in %s differ from the ones '
                           'currently tested against!\ncached=%r\ncurrent=%r' %
                           (path, cached_params, current_params))
                    return pytest.skip(msg)
                except AttributeError:
                    # XML for error messages have no Argument elements!
                    pass
            except ResponseRequired:
                # fetch XML via urllib2 rather than directly via
                # lxml.etree.parse() to avoid, for instance, problems with HTTP
                # 403 errors
                resp = requests.get(url, headers={'User-Agent': USER_AGENT})
                root = lxml.etree.fromstring(resp.text)
                # overwrite sensitive information in XML document.
                for arg in root.xpath('//aws:Argument',
                                      namespaces={'aws': root.nsmap[None]}):
                    if arg.get('Name') in ('Signature', 'AWSAccessKeyId',
                                           'AssociateTag'):
                        arg.set('Value', 'X' * 15)
                content = lxml.etree.tostring(root, pretty_print=True)
                # complain loudly about missing credentials
                # UNLESS it was actually on purpose!
                if (six.b('MissingClientTokenId') in content
                        and getattr(request.function, 'refetch', True)):
                    raise pytest.fail(
                        'Cannot fetch XML response without credentials!')
                if not os.path.exists(os.path.dirname(path)):
                    os.mkdir(os.path.dirname(path))
                open(path, 'wb').write(content)
            # We simply exchange the real host with the local one now!
            # Note: Although strictly speaking it does not matter which URL is
            # called exactly, to appeal to one's sense of correctness, let's
            # keep at least the correct path!
            url = url_reg.sub(r'%s\g<path>' % server.url, url)
            server.serve_content(content)
            return fnc(url)

        return wrapped

    api._fetch = counter(api._fetch)
    return api