def test_strip_query_string(url): parsed_url = parse.urlparse(url) assert strip_query_string(url) == parse.urlunparse(( parsed_url.scheme, parsed_url.netloc, parsed_url.path, parsed_url.params, None, parsed_url.fragment, ))
def parse_dogstatsd_url(url): # type: (str) -> Dict[str, str] # url can be either of the form `udp://<host>:<port>` or `unix://<path>` # also support without url scheme included if url.startswith("/"): url = "unix://" + url elif "://" not in url: url = "udp://" + url parsed = parse.urlparse(url) if parsed.scheme == "unix": return dict(socket_path=parsed.path) elif parsed.scheme == "udp": return dict(host=parsed.hostname, port=parsed.port) else: raise ValueError("Unknown scheme `%s` for DogStatsD URL `{}`".format( parsed.scheme))
def get_dogstatsd_client(url): # type: (str) -> Optional[DogStatsd] if not url: return None # url can be either of the form `udp://<host>:<port>` or `unix://<path>` # also support without url scheme included if url.startswith("/"): url = "unix://" + url elif "://" not in url: url = "udp://" + url parsed = parse.urlparse(url) if parsed.scheme == "unix": return DogStatsd(socket_path=parsed.path) elif parsed.scheme == "udp": return DogStatsd(host=parsed.hostname, port=base.DEFAULT_PORT if parsed.port is None else parsed.port) raise ValueError("Unknown scheme `%s` for DogStatsD URL `{}`".format(parsed.scheme))
def test_should_skip_request(self): """ When calling should_skip_request with an enabled Pin and non-internal request returns False with a disabled Pin and non-internal request returns True with an enabled Pin and internal request returns True with a disabled Pin and internal request returns True """ # Enabled Pin and non-internal request self.tracer.enabled = True request = self.get_http_connection(SOCKET) pin = Pin.get_from(request) self.assertFalse(should_skip_request(pin, request)) # Disabled Pin and non-internal request self.tracer.enabled = False request = self.get_http_connection(SOCKET) pin = Pin.get_from(request) self.assertTrue(should_skip_request(pin, request)) # Enabled Pin and internal request self.tracer.enabled = True parsed = parse.urlparse(self.tracer.writer.agent_url) request = self.get_http_connection(parsed.hostname, parsed.port) pin = Pin.get_from(request) self.assertTrue(should_skip_request(pin, request)) # Disabled Pin and internal request self.tracer.enabled = False request = self.get_http_connection(parsed.hostname, parsed.port) pin = Pin.get_from(request) self.assertTrue(should_skip_request(pin, request))
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()