def test_ping_upload_worker_single_process(safe_httpserver): safe_httpserver.serve_content(b"", code=200) Glean._configuration.server_endpoint = safe_httpserver.url # This test requires us to write a few files in the pending pings # directory, to which language bindings have theoretically no access. # Manually create the path to that directory, at the risk of breaking # the test in the future, if that changes in the Rust code. pings_dir = Glean._data_dir / "pending_pings" pings_dir.mkdir() for _ in range(10): with (pings_dir / str(uuid.uuid4())).open("wb") as fd: fd.write(b"/data/path/\n") fd.write(b"{}\n") # Fire off two PingUploadWorker processing tasks at the same time. If # working correctly, p1 should finish entirely before p2 starts. # If these actually run in parallel, one or the other will try to send # deleted queued ping files and fail. p1 = PingUploadWorker._process() p2 = PingUploadWorker._process() p1.wait() assert p1.returncode == 0 p2.wait() assert p2.returncode == 0 assert 10 == len(safe_httpserver.requests)
def test_ping_upload_worker_single_process(safe_httpserver): safe_httpserver.serve_content(b"", code=200) Glean._configuration.server_endpoint = safe_httpserver.url pings_dir = PingUploadWorker.storage_directory() pings_dir.mkdir() for i in range(100): with (pings_dir / str(uuid.uuid4())).open("wb") as fd: fd.write(b"/data/path/\n") fd.write(b"{}\n") # Fire off two PingUploadWorker processing tasks at the same time. If # working correctly, p1 should finish entirely before p2 starts. # If these actually run in parallel, one or the other will try to send # deleted queued ping files and fail. p1 = PingUploadWorker._process() p2 = PingUploadWorker._process() p1.wait() assert p1.returncode == 0 p2.wait() assert p2.returncode == 0 assert 100 == len(safe_httpserver.requests)
def test_invalid_content(): pings_dir = PingUploadWorker.storage_directory() pings_dir.mkdir() with (pings_dir / str(uuid.uuid4())).open("wb") as fd: fd.write(b"\n") assert not PingUploadWorker.process() assert 0 == len(list(pings_dir.iterdir()))
def test_invalid_filename(): pings_dir = PingUploadWorker.storage_directory() pings_dir.mkdir() with (pings_dir / "ping").open("wb") as fd: fd.write(b"\n") assert PingUploadWorker._test_process_sync() assert 0 == len(list(pings_dir.iterdir()))
def test_unknown_scheme(): Glean._configuration.server_endpoint = "ftp://example.com/" pings_dir = PingUploadWorker.storage_directory() pings_dir.mkdir() with (pings_dir / str(uuid.uuid4())).open("wb") as fd: fd.write(b"/data/path/\n") fd.write(b"{}\n") assert False is PingUploadWorker._test_process_sync()
def test_unknown_scheme(): Glean._configuration.server_endpoint = "ftp://example.com/" pings_dir = PingUploadWorker.storage_directory() pings_dir.mkdir() with (pings_dir / str(uuid.uuid4())).open("wb") as fd: fd.write(b"/data/path/\n") fd.write(b"{}\n") with pytest.raises(ValueError): PingUploadWorker.process()
def test_500_error(safe_httpserver): safe_httpserver.serve_content(b"", code=500) Glean._configuration.server_endpoint = safe_httpserver.url pings_dir = PingUploadWorker.storage_directory() pings_dir.mkdir() with (pings_dir / str(uuid.uuid4())).open("wb") as fd: fd.write(b"/data/path/\n") fd.write(b"{}\n") assert not PingUploadWorker._test_process_sync() assert 1 == len(list(pings_dir.iterdir())) assert 1 == len(safe_httpserver.requests)
def test_tempdir_is_cleared_multiprocess(safe_httpserver): safe_httpserver.serve_content(b"", code=200) Glean._configuration.server_endpoint = safe_httpserver.url # This test requires us to write a few files in the pending pings # directory, to which language bindings have theoretically no access. # Manually create the path to that directory, at the risk of breaking # the test in the future, if that changes in the Rust code. pings_dir = Glean._data_dir / "pending_pings" pings_dir.mkdir() for _ in range(10): with (pings_dir / str(uuid.uuid4())).open("wb") as fd: fd.write(b"/data/path/\n") fd.write(b"{}\n") # Make sure that resetting while the PingUploadWorker is running doesn't # delete the directory out from under the PingUploadWorker. p1 = PingUploadWorker._process() Glean._reset() p1.wait() assert p1.returncode == 0 assert 10 == len(safe_httpserver.requests)
def test_tempdir_is_cleared_multiprocess(safe_httpserver): safe_httpserver.serve_content(b"", code=200) Glean._configuration.server_endpoint = safe_httpserver.url pings_dir = PingUploadWorker.storage_directory() pings_dir.mkdir() for i in range(100): with (pings_dir / str(uuid.uuid4())).open("wb") as fd: fd.write(b"/data/path/\n") fd.write(b"{}\n") # Make sure that resetting while the PingUploadWorker is running doesn't # delete the directory out from under the PingUploadWorker. p1 = PingUploadWorker._process() Glean._reset() p1.wait() assert p1.returncode == 0 assert 100 == len(safe_httpserver.requests)
def test_ping_upload_worker_single_process(safe_httpserver): safe_httpserver.serve_content(b"", code=200) Glean._configuration.server_endpoint = safe_httpserver.url # This test requires us to write a few files in the pending pings # directory, to which language bindings have theoretically no access. # Manually create the path to that directory, at the risk of breaking # the test in the future, if that changes in the Rust code. pings_dir = Glean._data_dir / "pending_pings" pings_dir.mkdir() for _ in range(5): with (pings_dir / str(uuid.uuid4())).open("wb") as fd: fd.write(b"/data/path/\n") fd.write(b"{}\n") # Fire off the first PingUploaderWorker process to handle the existing # pings. Then, in parallel, write more pings and fire off "new" # PingUploadWorker processes (some of which will be no-ops and just keeping # the existing worker running). p1 = PingUploadWorker._process() processes = [] for _ in range(5): with (pings_dir / str(uuid.uuid4())).open("wb") as fd: fd.write(b"/data/path/\n") fd.write(b"{}\n") processes.append(PingUploadWorker._process()) p1.wait() assert p1.returncode == 0 for p in processes: p.wait() assert p.returncode == 0 assert 10 == len(safe_httpserver.requests)
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(subprocess, "Popen", broken_process) custom_ping = PingType( name="store1", include_client_id=True, send_if_empty=True, reason_codes=[] ) custom_ping.submit() process = PingUploadWorker._process() process.wait() assert process.returncode == 0 assert 1 == len(safe_httpserver.requests)
def test_log_on_success(safe_httpserver, capfd): # We can't use caplog to catch log messages from the subprocess, but we can # use capsys to catch its stderr. safe_httpserver.serve_content(b"", code=200) Glean._configuration.server_endpoint = safe_httpserver.url Glean._simple_log_level = logging.INFO pings_dir = Glean._data_dir / "pending_pings" pings_dir.mkdir() with (pings_dir / str(uuid.uuid4())).open("wb") as fd: fd.write(b"/data/path/\n") fd.write(b"{}\n") p1 = PingUploadWorker._process() p1.wait() assert p1.returncode == 0 assert 1 == len(safe_httpserver.requests) captured = capfd.readouterr() assert "successfully sent 200" in str(captured.err)