Exemplo n.º 1
0
def test_dont_schedule_pings_if_there_is_no_ping_content(safe_httpserver):
    safe_httpserver.serve_content(b"", code=200)

    custom_ping = PingType(
        name="store1", include_client_id=True, send_if_empty=False, reason_codes=[]
    )

    custom_ping.submit()

    assert 0 == len(safe_httpserver.requests)
Exemplo n.º 2
0
def test_presubmit_makes_a_valid_ping(tmpdir, ping_schema_url, monkeypatch):
    # Bug 1648140: Submitting a ping prior to initialize meant that the core
    # metrics wouldn't yet be set.

    info_path = Path(str(tmpdir)) / "info.txt"

    Glean._reset()

    ping_name = "preinit_ping"
    ping = PingType(name=ping_name,
                    include_client_id=True,
                    send_if_empty=True,
                    reason_codes=[])

    # This test relies on testing mode to be disabled, since we need to prove the
    # real-world async behaviour of this.
    Dispatcher._testing_mode = False
    Dispatcher._queue_initial_tasks = True

    # Submit a ping prior to calling initialize
    ping.submit()
    Glean._initialize_with_tempdir_for_testing(
        application_id=GLEAN_APP_ID,
        application_version=glean_version,
        upload_enabled=True,
        configuration=Glean._configuration,
    )

    monkeypatch.setattr(Glean._configuration, "ping_uploader",
                        _RecordingUploader(info_path))

    # Wait until the work is complete
    Dispatcher._task_worker._queue.join()

    while not info_path.exists():
        time.sleep(0.1)

    with info_path.open("r") as fd:
        url_path = fd.readline()
        serialized_ping = fd.readline()

    print(url_path)
    assert ping_name == url_path.split("/")[3]

    assert 0 == validate_ping.validate_ping(
        io.StringIO(serialized_ping),
        sys.stdout,
        schema_url=ping_schema_url,
    )
Exemplo n.º 3
0
def test_collect(ping_schema_url):
    counter_metric = CounterMetricType(
        disabled=False,
        category="telemetry",
        lifetime=Lifetime.APPLICATION,
        name="counter_metric",
        send_in_pings=["store1"],
    )

    custom_ping = PingType(
        name="store1", include_client_id=True, send_if_empty=False, reason_codes=[]
    )

    counter_metric.add(10)

    json_content = Glean.test_collect(custom_ping)

    assert isinstance(json_content, str)

    json_tree = json.loads(json_content)

    assert 10 == json_tree["metrics"]["counter"]["telemetry.counter_metric"]

    assert 0 == validate_ping.validate_ping(
        io.StringIO(json_content), sys.stdout, schema_url=ping_schema_url
    )
Exemplo n.º 4
0
def test_flipping_upload_enabled_respects_order_of_events(tmpdir, monkeypatch):
    Glean._reset()

    info_path = Path(str(tmpdir)) / "info.txt"

    # We create a ping and a metric before we initialize Glean
    ping = PingType(
        name="sample_ping_1",
        include_client_id=True,
        send_if_empty=True,
        reason_codes=[],
    )

    # This test relies on testing mode to be disabled, since we need to prove the
    # real-world async behaviour of this.
    Dispatcher._testing_mode = False
    Dispatcher._queue_initial_tasks = True

    configuration = Glean._configuration
    configuration.ping_uploader = _RecordingUploader(info_path)
    Glean._initialize_with_tempdir_for_testing(
        application_id=GLEAN_APP_ID,
        application_version=glean_version,
        upload_enabled=True,
        configuration=Glean._configuration,
    )

    # Glean might still be initializing. Disable upload.
    Glean.set_upload_enabled(False)
    # Submit a custom ping.
    ping.submit()

    # Wait until the work is complete
    Dispatcher._task_worker._queue.join()

    while not info_path.exists():
        time.sleep(0.1)

    with info_path.open("r") as fd:
        url_path = fd.readline()

    # Validate we got the deletion-request ping
    assert "deletion-request" == url_path.split("/")[3]
Exemplo n.º 5
0
def test_dont_allow_multiprocessing(monkeypatch, safe_httpserver):
    safe_httpserver.serve_content(b"", code=200)

    Glean._configuration.server_endpoint = safe_httpserver.url
    Glean._configuration._allow_multiprocessing = False

    # Monkey-patch the multiprocessing API to be broken so we can assert it isn't used
    def broken_process(*args, **kwargs):
        assert False, "shouldn't be called"

    monkeypatch.setattr(multiprocessing, "Process", broken_process)

    custom_ping = PingType(name="store1",
                           include_client_id=True,
                           send_if_empty=True,
                           reason_codes=[])

    custom_ping.submit()

    assert 1 == len(safe_httpserver.requests)
Exemplo n.º 6
0
def test_sending_of_custom_pings(safe_httpserver):
    safe_httpserver.serve_content(b"", code=200)
    Glean._configuration.server_endpoint = safe_httpserver.url

    counter_metric = CounterMetricType(
        disabled=False,
        category="telemetry",
        lifetime=Lifetime.APPLICATION,
        name="counter_metric",
        send_in_pings=["store1"],
    )

    custom_ping = PingType(name="store1",
                           include_client_id=True,
                           send_if_empty=False,
                           reason_codes=[])

    counter_metric.add()

    # Need a mutable object and plain booleans are not.
    callback_was_called = [False]

    def check_custom_ping(reason):
        assert reason is None
        assert 1 == counter_metric.test_get_value()
        callback_was_called[0] = True

    custom_ping.test_before_next_submit(check_custom_ping)
    custom_ping.submit()

    assert callback_was_called[0]

    assert 1 == len(safe_httpserver.requests)
Exemplo n.º 7
0
def test_dont_schedule_pings_if_metrics_disabled(safe_httpserver):
    safe_httpserver.serve_content(b"", code=200)

    counter_metric = CounterMetricType(
        disabled=False,
        category="telemetry",
        lifetime=Lifetime.APPLICATION,
        name="counter_metric",
        send_in_pings=["store1"],
    )

    custom_ping = PingType(
        name="store1", include_client_id=True, send_if_empty=False, reason_codes=[]
    )

    counter_metric.add(10)

    Glean.set_upload_enabled(False)

    custom_ping.submit()

    assert 0 == len(safe_httpserver.requests)
Exemplo n.º 8
0
def test_ping_collection_must_happen_after_currently_scheduled_metrics_recordings(
    tmpdir, ping_schema_url,
):
    # Given the following block of code:
    #
    # metrics.metric.a.set("SomeTestValue")
    # Glean.submit_pings(["custom-ping-1"])
    #
    # This test ensures that "custom-ping-1" contains "metric.a" with a value of "SomeTestValue"
    # when the ping is collected.

    info_path = Path(str(tmpdir)) / "info.txt"

    real_uploader = Glean._configuration.ping_uploader
    test_uploader = _RecordingUploader(info_path)
    Glean._configuration.ping_uploader = test_uploader

    Glean._configuration.log_pings = True

    ping_name = "custom_ping_1"
    ping = PingType(
        name=ping_name, include_client_id=True, send_if_empty=False, reason_codes=[]
    )
    string_metric = StringMetricType(
        disabled=False,
        category="category",
        lifetime=Lifetime.PING,
        name="string_metric",
        send_in_pings=[ping_name],
    )

    # This test relies on testing mode to be disabled, since we need to prove the
    # real-world async behaviour of this.
    Dispatcher._testing_mode = False

    # This is the important part of the test. Even though both the metrics API and
    # sendPings are async and off the main thread, "SomeTestValue" should be recorded,
    # the order of the calls must be preserved.
    test_value = "SomeTestValue"
    string_metric.set(test_value)
    ping.submit()

    # Wait until the work is complete
    Dispatcher._task_worker._queue.join()

    while not info_path.exists():
        time.sleep(0.1)

    with info_path.open("r") as fd:
        url_path = fd.readline()
        serialized_ping = fd.readline()

    assert ping_name == url_path.split("/")[3]

    json_content = json.loads(serialized_ping)

    assert 0 == validate_ping.validate_ping(
        io.StringIO(serialized_ping), sys.stdout, schema_url=ping_schema_url,
    )

    assert {"category.string_metric": test_value} == json_content["metrics"]["string"]

    Glean._configuration.ping_uploader = real_uploader