def test_full_uri_connect():
    # An exception will be raised here if there's a problem with the response
    AgentProtocol.connect(
        "Python Agent Test (test_full_uri_payloads)",
        [],
        [],
        global_settings(),
        client_cls=FullUriClient,
    )
def test_connect_metadata(monkeypatch):
    monkeypatch.setenv("NEW_RELIC_METADATA_FOOBAR", "foobar")
    monkeypatch.setenv("_NEW_RELIC_METADATA_WRONG", "wrong")
    protocol = AgentProtocol.connect(
        APP_NAME,
        LINKED_APPS,
        ENVIRONMENT,
        finalize_application_settings(),
        client_cls=HttpClientRecorder,
    )
    connect = HttpClientRecorder.SENT[1]
    assert connect.params["method"] == "connect"
    connect_payload = json_decode(connect.payload.decode("utf-8"))[0]
    assert connect_payload["metadata"] == {"NEW_RELIC_METADATA_FOOBAR": "foobar"}
def test_span_event_harvest_config(connect_response_fields):
    client_cls = functools.partial(
        CustomTestClient, connect_response_fields=connect_response_fields)

    protocol = AgentProtocol.connect('app_name',
                                     LINKED_APPLICATIONS,
                                     ENVIRONMENT,
                                     global_settings(),
                                     client_cls=client_cls)

    if connect_response_fields and connect_response_fields[
            "span_event_harvest_config"]:
        expected = 123
    else:
        from newrelic.core.config import SPAN_EVENT_RESERVOIR_SIZE
        expected = SPAN_EVENT_RESERVOIR_SIZE
    assert protocol.configuration.event_harvest_config.harvest_limits.span_event_data == expected
def test_server_side_config_precedence():
    connect_response_fields = {
        u'agent_config': {
            u'span_events.enabled': True
        },
        u'span_events.enabled': False,
    }
    client_cls = functools.partial(
        CustomTestClient, connect_response_fields=connect_response_fields)

    protocol = AgentProtocol.connect('app_name',
                                     LINKED_APPLICATIONS,
                                     ENVIRONMENT,
                                     global_settings(),
                                     client_cls=client_cls)

    assert protocol.configuration.span_events.enabled is False
def test_blob():
    request_headers_map = {u'X-Foo': u'Bar'}
    connect_response_fields = {u"request_headers_map": request_headers_map}

    client_cls = functools.partial(
        CustomTestClient, connect_response_fields=connect_response_fields)

    protocol = AgentProtocol.connect('app_name',
                                     LINKED_APPLICATIONS,
                                     ENVIRONMENT,
                                     global_settings(),
                                     client_cls=client_cls)

    protocol.send("shutdown")

    headers = protocol.client.headers[-1]
    assert headers == {
        "Content-Type": "application/json",
        "X-Foo": "Bar",
    }
def test_no_blob_behavior(headers_map_present):
    if headers_map_present:
        connect_response_fields = {u"request_headers_map": None}
        client_cls = functools.partial(
            CustomTestClient, connect_response_fields=connect_response_fields)
    else:
        client_cls = functools.partial(CustomTestClient,
                                       connect_response_fields=DEFAULT)

    protocol = AgentProtocol.connect('app_name',
                                     LINKED_APPLICATIONS,
                                     ENVIRONMENT,
                                     global_settings(),
                                     client_cls=client_cls)

    protocol.send("shutdown")

    headers = protocol.client.headers[-1]
    assert headers == {
        "Content-Type": "application/json",
    }
def test_connect(with_aws, with_pcf, with_gcp, with_azure, with_docker,
                 with_kubernetes, with_ip):
    global AWS, AZURE, GCP, PCF, BOOT_ID, DOCKER, KUBERNETES, IP_ADDRESS
    if not with_aws:
        AWS = Exception
    if not with_pcf:
        PCF = Exception
    if not with_gcp:
        GCP = Exception
    if not with_azure:
        AZURE = Exception
    if not with_docker:
        DOCKER = Exception
    if not with_kubernetes:
        KUBERNETES = Exception
    if not with_ip:
        IP_ADDRESS = None
    settings = finalize_application_settings({
        "browser_monitoring.loader": BROWSER_MONITORING_LOADER,
        "browser_monitoring.debug": BROWSER_MONITORING_DEBUG,
        "capture_params": CAPTURE_PARAMS,
        "process_host.display_name": DISPLAY_NAME,
        "transaction_tracer.record_sql": RECORD_SQL,
        "high_security": HIGH_SECURITY,
        "labels": LABELS,
        "utilization.detect_aws": with_aws,
        "utilization.detect_pcf": with_pcf,
        "utilization.detect_gcp": with_gcp,
        "utilization.detect_azure": with_azure,
        "utilization.detect_docker": with_docker,
        "utilization.detect_kubernetes": with_kubernetes,
        "event_harvest_config": {
            "harvest_limits": {
                "analytic_event_data": ANALYTIC_EVENT_DATA,
                "span_event_data": SPAN_EVENT_DATA,
                "custom_event_data": CUSTOM_EVENT_DATA,
                "error_event_data": ERROR_EVENT_DATA,
            }
        },
    })
    protocol = AgentProtocol.connect(
        APP_NAME,
        LINKED_APPS,
        ENVIRONMENT,
        settings,
        client_cls=HttpClientRecorder,
    )

    # verify there are exactly 3 calls to HttpClientRecorder
    assert len(HttpClientRecorder.SENT) == 3

    # Verify preconnect call
    preconnect = HttpClientRecorder.SENT[0]
    assert preconnect.params["method"] == "preconnect"
    assert preconnect.payload == b"[]"

    # Verify connect call
    connect = HttpClientRecorder.SENT[1]
    assert connect.params["method"] == "connect"
    connect_payload = json_decode(connect.payload.decode("utf-8"))
    connect_payload_asserts(
        connect_payload,
        with_aws=with_aws,
        with_pcf=with_pcf,
        with_gcp=with_gcp,
        with_azure=with_azure,
        with_docker=with_docker,
        with_kubernetes=with_kubernetes,
    )

    # Verify agent_settings call is done with the finalized settings
    agent_settings = HttpClientRecorder.SENT[2]
    assert agent_settings.params["method"] == "agent_settings"
    agent_settings_payload = json_decode(
        agent_settings.payload.decode("utf-8"))
    assert len(agent_settings_payload) == 1
    agent_settings_payload = agent_settings_payload[0]

    # Finalized settings will have a non-None agent_run_id
    assert agent_settings_payload["agent_run_id"] is not None
    assert protocol.configuration.agent_run_id is not None

    # Verify that agent settings sent have converted null, containers, and
    # unserializable types to string
    assert agent_settings_payload["proxy_host"] == "None"
    assert agent_settings_payload["attributes.include"] == "[]"
    assert agent_settings_payload["feature_flag"] == str(set())
    assert isinstance(agent_settings_payload["attribute_filter"],
                      six.string_types)

    # Verify that the connection is closed
    assert HttpClientRecorder.STATE == 0