|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +//! This integration test should model how the RLB is used when embedded in another Rust application |
| 6 | +//! (e.g. FOG/Firefox Desktop). |
| 7 | +//! |
| 8 | +//! We write a single test scenario per file to avoid any state keeping across runs |
| 9 | +//! (different files run as different processes). |
| 10 | +
|
| 11 | +mod common; |
| 12 | + |
| 13 | +use std::{fs, io::Read}; |
| 14 | + |
| 15 | +use crossbeam_channel::{bounded, Sender}; |
| 16 | +use flate2::read::GzDecoder; |
| 17 | +use glean::{net, ConfigurationBuilder}; |
| 18 | +use serde_json::Value as JsonValue; |
| 19 | + |
| 20 | +// Define a fake uploader that reports when and what it uploads. |
| 21 | +#[derive(Debug)] |
| 22 | +struct ReportingUploader { |
| 23 | + sender: Sender<JsonValue>, |
| 24 | +} |
| 25 | + |
| 26 | +impl net::PingUploader for ReportingUploader { |
| 27 | + fn upload(&self, upload_request: net::CapablePingUploadRequest) -> net::UploadResult { |
| 28 | + let upload_request = upload_request.capable(|_| true).unwrap(); |
| 29 | + let body = upload_request.body; |
| 30 | + let decode = |body: Vec<u8>| { |
| 31 | + let mut gzip_decoder = GzDecoder::new(&body[..]); |
| 32 | + let mut s = String::with_capacity(body.len()); |
| 33 | + |
| 34 | + gzip_decoder |
| 35 | + .read_to_string(&mut s) |
| 36 | + .ok() |
| 37 | + .map(|_| &s[..]) |
| 38 | + .or_else(|| std::str::from_utf8(&body).ok()) |
| 39 | + .and_then(|payload| serde_json::from_str(payload).ok()) |
| 40 | + .unwrap() |
| 41 | + }; |
| 42 | + |
| 43 | + self.sender.send(decode(body)).unwrap(); |
| 44 | + net::UploadResult::http_status(200) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/// Test scenario: Write a client ID to the backup file and check that it's used after initialization. |
| 49 | +#[test] |
| 50 | +fn test_pre_post_init_health_pings_exist() { |
| 51 | + common::enable_test_logging(); |
| 52 | + |
| 53 | + // Create a custom configuration to use a validating uploader. |
| 54 | + let dir = tempfile::tempdir().unwrap(); |
| 55 | + let tmpname = dir.path().to_path_buf(); |
| 56 | + |
| 57 | + let client_id = "e03cc2de-bc8b-4f9c-862f-b474d910899e"; |
| 58 | + |
| 59 | + // We write a random but fixed client ID, without there being a Glean database. |
| 60 | + let clientid_txt = tmpname.join("client_id.txt"); |
| 61 | + fs::write(&clientid_txt, client_id.as_bytes()).unwrap(); |
| 62 | + |
| 63 | + let (tx, rx) = bounded(1); |
| 64 | + let cfg = ConfigurationBuilder::new(true, tmpname.clone(), "health-ping-test") |
| 65 | + .with_server_endpoint("invalid-test-host") |
| 66 | + .with_use_core_mps(false) |
| 67 | + .with_uploader(ReportingUploader { sender: tx }) |
| 68 | + .build(); |
| 69 | + common::initialize(cfg); |
| 70 | + |
| 71 | + glean_core::glean_test_destroy_glean(false, Some(tmpname.display().to_string())); |
| 72 | + |
| 73 | + // Check for the initialization pings. |
| 74 | + // Wait for the ping to arrive. |
| 75 | + let payload = rx.recv().unwrap(); |
| 76 | + |
| 77 | + let exception_state = &payload["metrics"]["string"]["glean.health.exception_state"]; |
| 78 | + assert_eq!("empty-db", exception_state); |
| 79 | + let exception_uuid = &payload["metrics"]["uuid"]["glean.health_recovered_client_id"]; |
| 80 | + assert_eq!(&JsonValue::Null, exception_uuid); |
| 81 | + |
| 82 | + // TODO(bug 1996862): We don't run the mitigation yet. |
| 83 | + //let ping_client_id = &payload["client_info"]["client_id"]; |
| 84 | + //assert_eq!(client_id, ping_client_id); |
| 85 | +} |
0 commit comments