Esempio n. 1
0
        def wrapper(*args, **kwargs):
            if len(args) > 1:
                self = args[0]
                clsname = self.__class__.__name__
            else:
                clsname = ""

            module = inspect.getmodule(f)

            # Use the fully qualified function name as a unique test token to
            # identify the snapshot.
            token = "{}{}{}.{}".format(module.__name__, "." if clsname else "",
                                       clsname, f.__name__)

            conn = httplib.HTTPConnection(tracer.writer.api.hostname,
                                          tracer.writer.api.port)
            try:
                # Signal the start of this test case to the test agent.
                try:
                    conn.request("GET", "/test/start?token=%s" % token)
                except Exception as e:
                    pytest.fail("Could not connect to test agent: %s" % str(e),
                                pytrace=False)

                r = conn.getresponse()
                if r.status != 200:
                    # The test agent returns nice error messages we can forward to the user.
                    raise SnapshotFailed(r.read().decode())

                # Run the test.
                ret = f(*args, **kwargs)

                # Flush out any remnant traces.
                tracer.writer.flush_queue()

                # Query for the results of the test.
                conn = httplib.HTTPConnection(tracer.writer.api.hostname,
                                              tracer.writer.api.port)
                conn.request(
                    "GET", "/test/snapshot?ignores=%s&token=%s" %
                    (",".join(ignores), token))
                r = conn.getresponse()
                if r.status != 200:
                    raise SnapshotFailed(r.read().decode())
                return ret
            except SnapshotFailed as e:
                # Fail the test if a failure has occurred and print out the
                # message we got from the test agent.
                pytest.fail(str(e), pytrace=False)
            finally:
                conn.close()
Esempio n. 2
0
 def _put(self, endpoint, data, count=0):
     conn = httplib.HTTPConnection(self.hostname, self.port)
     conn.request('HEAD', endpoint, data, self._headers)
     return Response.from_http_response(conn.getresponse())
Esempio n. 3
0
 def get_http_connection(self, *args, **kwargs):
     conn = httplib.HTTPConnection(*args, **kwargs)
     Pin.override(conn, tracer=self.tracer)
     return conn
Esempio n. 4
0
    def wrapper(wrapped, instance, args, kwargs):
        if len(args) > 1:
            self = args[0]
            clsname = self.__class__.__name__
        else:
            clsname = ""

        if include_tracer:
            tracer = Tracer()
        else:
            tracer = ddtrace.tracer

        module = inspect.getmodule(wrapped)

        # Use the fully qualified function name as a unique test token to
        # identify the snapshot.
        token = "{}{}{}.{}".format(module.__name__, "." if clsname else "",
                                   clsname, wrapped.__name__)

        # Use variant that applies to update test token. One must apply. If none
        # apply, the test should have been marked as skipped.
        if variants:
            applicable_variant_ids = [k for (k, v) in variants.items() if v]
            assert len(applicable_variant_ids) == 1
            variant_id = applicable_variant_ids[0]
            token = "{}_{}".format(token, variant_id) if variant_id else token

        parsed = parse.urlparse(tracer.writer.agent_url)
        conn = httplib.HTTPConnection(parsed.hostname, parsed.port)
        try:
            # clear queue in case traces have been generated before test case is
            # itself run
            try:
                tracer.writer.flush_queue()
            except Exception as e:
                pytest.fail("Could not flush the queue before test case: %s" %
                            str(e),
                            pytrace=True)

            if async_mode:
                # Patch the tracer writer to include the test token header for all requests.
                tracer.writer._headers["X-Datadog-Test-Token"] = token
            else:
                # Signal the start of this test case to the test agent.
                try:
                    conn.request("GET", "/test/start?token=%s" % token)
                except Exception as e:
                    pytest.fail("Could not connect to test agent: %s" % str(e),
                                pytrace=False)
                else:
                    r = conn.getresponse()
                    if r.status != 200:
                        # The test agent returns nice error messages we can forward to the user.
                        raise SnapshotFailed(r.read())

            # Run the test.
            try:
                if include_tracer:
                    kwargs["tracer"] = tracer
                ret = wrapped(*args, **kwargs)
                # Force a flush so all traces are submitted.
                tracer.writer.flush_queue()
            finally:
                if async_mode:
                    del tracer.writer._headers["X-Datadog-Test-Token"]

            # Query for the results of the test.
            conn = httplib.HTTPConnection(parsed.hostname, parsed.port)
            conn.request(
                "GET", "/test/snapshot?ignores=%s&token=%s" %
                (",".join(ignores), token))
            r = conn.getresponse()
            if r.status != 200:
                raise SnapshotFailed(r.read())
            return ret
        except SnapshotFailed as e:
            # Fail the test if a failure has occurred and print out the
            # message we got from the test agent.
            pytest.fail(to_unicode(e.args[0]), pytrace=False)
        except Exception as e:
            # Even though it's unlikely any traces have been sent, make the
            # final request to the test agent so that the test case is finished.
            conn = httplib.HTTPConnection(parsed.hostname, parsed.port)
            conn.request(
                "GET", "/test/snapshot?ignores=%s&token=%s" %
                (",".join(ignores), token))
            conn.getresponse()
            pytest.fail("Unexpected test failure during snapshot test: %s" %
                        str(e),
                        pytrace=True)
        finally:
            conn.close()
Esempio n. 5
0
 def _put(self, endpoint, data):
     conn = httplib.HTTPConnection(self.hostname, self.port)
     conn.request("HEAD", endpoint, data, self._headers)
     return conn.getresponse()