Example #1
0
def test_config():
    sha = "4de21f8ea228a082d4f039c0c991ee41dfb6f9d8"
    try:
        Config.set(revision_sha=sha)
        assert scout_config.value("revision_sha") == sha
    finally:
        scout_config.reset_all()
def test_send_batch(error_service_thread):
    decompressed_body = {
        "notifier": "scout_apm_python",
        "root": WORKING_DIRECTORY,
        "environment": None,
        "problems": [{
            "foo": 0
        }, {
            "foo": 1
        }, {
            "foo": 2
        }, {
            "foo": 3
        }, {
            "foo": 4
        }],
    }
    try:
        with httpretty.enabled(allow_net_connect=False):
            httpretty.register_uri(
                httpretty.POST,
                "https://errors.scoutapm.com/apps/error.scout",
                body="Hello world!",
            )
            for i in range(5):
                ErrorServiceThread.send(error={"foo": i})
            ErrorServiceThread.wait_until_drained()

            request = httpretty.last_request()
            assert (json.loads(gzip_decompress(
                request.body).decode("utf-8")) == decompressed_body)
            assert request.headers.get("X-Error-Count") == "5"
    finally:
        scout_config.reset_all()
Example #3
0
def core_agent_manager(core_agent_dir):
    scout_config.set(core_agent_dir=core_agent_dir)
    core_agent_manager = CoreAgentManager()
    try:
        yield core_agent_manager
    finally:
        assert not core_agent_is_running()
        scout_config.reset_all()
Example #4
0
def test_create_filtered_path_path(path, params):
    # If config filtered_params is set to "path", expect we always get the path
    # back
    scout_config.set(uri_reporting="path")
    try:
        assert create_filtered_path(path, params) == path
    finally:
        scout_config.reset_all()
    def test_config_file_old_name_takes_precedence(self):
        scout_config.set(config_file="foo", core_agent_config_file="bar")

        try:
            result = CoreAgentManager().config_file()
        finally:
            scout_config.reset_all()

        assert result == ["--config-file", "foo"]
    def test_log_level_old_name_takes_precedence(self):
        scout_config.set(log_level="foo", core_agent_log_level="bar")

        try:
            result = CoreAgentManager().log_level()
        finally:
            scout_config.reset_all()

        assert result == ["--log-level", "foo"]
    def test_log_level(self):
        scout_config.set(core_agent_log_level="foo")

        try:
            result = CoreAgentManager().log_level()
        finally:
            scout_config.reset_all()

        assert result == ["--log-level", "foo"]
    def test_socket_path_tcp(self):
        scout_config.set(core_agent_socket_path="tcp://127.0.0.1:7894")

        try:
            result = CoreAgentManager().socket_path()
        finally:
            scout_config.reset_all()

        assert result == ["--tcp", "127.0.0.1:7894"]
Example #9
0
def test_ignore(path, expected):
    scout_config.set(ignore=["/health"])

    try:
        result = ignore_path(path)
    finally:
        scout_config.reset_all()

    assert result == expected
Example #10
0
def test_ignore_multiple_prefixes(path, expected):
    scout_config.set(ignore=["/health", "/api"])

    try:
        result = ignore_path(path)
    finally:
        scout_config.reset_all()

    assert result == expected
    def test_socket_path_path(self):
        scout_config.set(core_agent_socket_path="/tmp/foo.sock")

        try:
            result = CoreAgentManager().socket_path()
        finally:
            scout_config.reset_all()

        assert result == ["--socket", "/tmp/foo.sock"]
Example #12
0
def test_error_capture_skip(value, error_monitor_errors):
    scout_config.set(errors_enabled=True)

    try:
        Error.capture(value)
    finally:
        scout_config.reset_all()

    assert len(error_monitor_errors) == 0
def reset_config():
    """
    Reset scout configuration after a test
    """
    try:
        yield
    finally:
        # Reset Scout configuration.
        scout_config.reset_all()
    def test_config_file(self):
        scout_config.set(core_agent_config_file="foo")

        try:
            result = CoreAgentManager().config_file()
        finally:
            scout_config.reset_all()

        assert result == ["--config-file", "foo"]
def core_agent_manager(core_agent_dir):
    # Shorten path to socket to prevent core-agent from failing with:
    #   Error opening listener on socket: Custom { kind: InvalidInput,
    #   error: StringError("path must be shorter than SUN_LEN") }
    socket_path = "{}/test.sock".format(core_agent_dir)
    scout_config.set(core_agent_dir=core_agent_dir, socket_path=socket_path)
    core_agent_manager = CoreAgentManager()
    try:
        yield core_agent_manager
    finally:
        assert not is_running(core_agent_manager)
        scout_config.reset_all()
def test_send_api_error(error_service_thread, caplog):
    try:
        with httpretty.enabled(allow_net_connect=False):
            httpretty.register_uri(
                httpretty.POST,
                "https://errors.scoutapm.com/apps/error.scout",
                body="Unexpected Error",
                status=500,
            )
            ErrorServiceThread.send(error={"foo": "BØØM!"})
            ErrorServiceThread.wait_until_drained()
    finally:
        scout_config.reset_all()
    assert caplog.record_tuples[-1][0] == "scout_apm.core.error_service"
    assert caplog.record_tuples[-1][1] == logging.DEBUG
    assert caplog.record_tuples[-1][2].startswith(
        "ErrorServiceThread 500 response error on _send:")
def test_ensure_all_installed_one_disabled(caplog):
    scout_config.set(disabled_instruments=["jinja2"])

    try:
        ensure_all_installed()
    finally:
        scout_config.reset_all()

    instruments_record_tuples = [
        t for t in caplog.record_tuples if t[0] == "scout_apm.instruments"
    ]
    assert len(instruments_record_tuples) == 1
    assert instruments_record_tuples[0] == (
        "scout_apm.instruments",
        logging.INFO,
        "jinja2 instrument is disabled. Skipping.",
    )
Example #18
0
def test_error_capture(error_monitor_errors, tracked_request):
    scout_config.set(errors_enabled=True)
    tracked_request.tag("spam", "eggs")

    request_path = "/test/"
    request_params = {"page": 1}
    session = {"step": 0}
    custom_controller = "test-controller"
    custom_params = {"foo": "bar"}
    try:
        try:
            1 / 0
        except ZeroDivisionError as exc:
            Error.capture(
                exc,
                request_path=request_path,
                request_params=request_params,
                session=session,
                custom_controller=custom_controller,
                custom_params=custom_params,
            )
    finally:
        scout_config.reset_all()

    assert len(error_monitor_errors) == 1
    error = error_monitor_errors[0]
    filepath, line, func_str = error["trace"][0].split(":")
    assert filepath.endswith("tests/integration/test_api.py")
    # The line number changes between python versions. Make sure it's not empty.
    assert line
    assert func_str == "in test_error_capture"
    assert error["exception_class"] == "ZeroDivisionError"
    assert error["message"] == "division by zero"
    assert error["context"] == {
        "spam": "eggs",
        "custom_params": {"foo": "bar"},
    }
    assert error["request_uri"] == request_path
    assert error["request_params"] == {"page": "1"}
    assert error["request_session"] == {"step": "0"}
    assert error["request_components"] == {
        "module": None,
        "controller": custom_controller,
        "action": None,
    }
def test_download_and_launch(path, core_agent_manager):
    if path is not None:
        scout_config.set(core_agent_socket_path=path)

    try:
        result = core_agent_manager.launch()

        assert result is True

        time.sleep(0.10)  # wait for agent to start running
        for _ in range(10):
            if core_agent_is_running():
                break
            time.sleep(0.1)
        else:
            raise AssertionError("Could not find core agent running")

        terminate_core_agent_processes()
    finally:
        scout_config.reset_all()
def test_send(config, decoded_body, expected_headers, expected_uri,
              error_service_thread):
    scout_config.set(**config)

    try:
        with httpretty.enabled(allow_net_connect=False):
            httpretty.register_uri(
                httpretty.POST,
                expected_uri,
                body="Hello World!",
            )
            ErrorServiceThread.send(error={"foo": "BØØM!"})
            ErrorServiceThread.wait_until_drained()

            request = httpretty.last_request()
            assert (json.loads(gzip_decompress(
                request.body).decode("utf-8")) == decoded_body)
            assert request.headers.get("X-Error-Count") == "1"
    finally:
        scout_config.reset_all()
 def test_from_socket_path(self):
     scout_config.set(socket_path="/tmp/my.sock")
     try:
         assert get_socket_path() == "/tmp/my.sock"
     finally:
         scout_config.reset_all()
 def test_from_core_agent_socket_path(self):
     scout_config.set(core_agent_socket_path="/tmp/that.sock")
     try:
         assert get_socket_path() == "/tmp/that.sock"
     finally:
         scout_config.reset_all()
 def test_not_tcp(self):
     scout_config.set(core_agent_socket_path="/tmp/that.sock")
     try:
         assert not get_socket_path().is_tcp
     finally:
         scout_config.reset_all()
 def test_tcp(self):
     scout_config.set(core_agent_socket_path="tcp://127.0.0.1:1234")
     try:
         assert get_socket_path().is_tcp
     finally:
         scout_config.reset_all()