Debian Codegraph @

third_party/rust/glean/tests/collection_enabled.rs

raw
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::io::Read;
14
15 use crossbeam_channel::bounded;
16
17 use crossbeam_channel::Sender;
18 use crossbeam_channel::TryRecvError;
19 use flate2::read::GzDecoder;
20 use glean::net;
21 use glean::ClientInfoMetrics;
22 use glean::ConfigurationBuilder;
23 use pings::nofollows;
24 use serde_json::Value as JsonValue;
25
26 mod pings {
27 use super::*;
28 use glean::private::PingType;
29 use once_cell::sync::Lazy;
30
31 #[allow(non_upper_case_globals)]
32 pub static nofollows: Lazy<PingType> = Lazy::new(|| {
33 common::PingBuilder::new("nofollows")
34 .with_send_if_empty(true)
35 .with_include_info_sections(true) // WITH info sections
36 .with_enabled(false)
37 .with_follows_collection_enabled(false)
38 .with_include_client_id(true)
39 .build()
40 });
41
42 #[allow(non_upper_case_globals)]
43 pub static manual: Lazy<PingType> = Lazy::new(|| {
44 common::PingBuilder::new("manual")
45 .with_send_if_empty(true)
46 .build()
47 });
48 }
49
50 // Define a fake uploader that reports when and what it uploads.
51 #[derive(Debug)]
52 struct ReportingUploader {
53 sender: Sender<JsonValue>,
54 }
55
56 impl net::PingUploader for ReportingUploader {
57 fn upload(&self, upload_request: net::CapablePingUploadRequest) -> net::UploadResult {
58 let upload_request = upload_request.capable(|_| true).unwrap();
59 let body = upload_request.body;
60 let decode = |body: Vec<u8>| {
61 let mut gzip_decoder = GzDecoder::new(&body[..]);
62 let mut s = String::with_capacity(body.len());
63
64 gzip_decoder
65 .read_to_string(&mut s)
66 .ok()
67 .map(|_| &s[..])
68 .or_else(|| std::str::from_utf8(&body).ok())
69 .and_then(|payload| serde_json::from_str(payload).ok())
70 .unwrap()
71 };
72
73 self.sender.send(decode(body)).unwrap();
74 net::UploadResult::http_status(200)
75 }
76 }
77
78 /// Test scenario:
79 ///
80 /// * Glean has _some_ data already stored.
81 /// * Glean is started with collection-enabled=false.
82 /// * Most data is cleared, but not `client_info`` (except `client_id`)
83 /// * Pings with `follows_collection_enabled=false` still have the `client_info` filled in.
84 #[test]
85 fn nofollows_contains_client_info_when_collection_disabled() {
86 common::enable_test_logging();
87
88 // Create a custom configuration to use our reporting uploader.
89 let dir = tempfile::tempdir().unwrap();
90 let tmpname = dir.path().to_path_buf();
91
92 // collection-enabled = true
93 // Forces database to be created with data, then clears data.
94 // Keeps `first_run_date`.
95 // Ensures the _next_ init is NOT a first-run.
96 let cfg = ConfigurationBuilder::new(true, tmpname.clone(), "glean-fc")
97 .with_server_endpoint("invalid-test-host")
98 .with_use_core_mps(false)
99 .build();
100 common::initialize(cfg);
101 glean::set_upload_enabled(false);
102 glean::shutdown();
103
104 // collection-enabled = false
105 let (tx, rx) = bounded(1);
106 let cfg = ConfigurationBuilder::new(false, tmpname.clone(), "glean-fc")
107 .with_server_endpoint("invalid-test-host")
108 .with_use_core_mps(false)
109 .with_uploader(ReportingUploader { sender: tx })
110 .build();
111 // Same as `common::initialize`.
112 let client_info = ClientInfoMetrics {
113 app_build: "1.0.0".to_string(),
114 app_display_version: "1.0.0".to_string(),
115 channel: Some("testing".to_string()),
116 locale: Some("xx-XX".to_string()),
117 };
118 glean::test_reset_glean(cfg, client_info, false);
119
120 _ = &*pings::nofollows;
121 _ = &*pings::manual;
122 nofollows.set_enabled(true);
123
124 pings::manual.submit(None);
125 pings::nofollows.submit(None);
126
127 // Wait for the ping to arrive.
128 let payload = rx.recv().unwrap();
129
130 let client_info = payload["client_info"].as_object().unwrap();
131 // General client info is set
132 assert!(client_info["app_build"].is_string());
133 assert!(client_info["architecture"].is_string());
134 assert!(client_info["os"].is_string());
135 assert!(client_info["telemetry_sdk_build"].is_string());
136 // No client_id
137 assert_eq!(None, client_info.get("client_id"));
138
139 // No second ping received.
140 assert!(matches!(rx.try_recv(), Err(TryRecvError::Empty)));
141
142 // Now we enable collection.
143 // This should give us a client ID.
144 glean::set_collection_enabled(true);
145
146 pings::manual.submit(None);
147 let payload = rx.recv().unwrap();
148 let client_info = payload["client_info"].as_object().unwrap();
149 // General client info is set
150 assert!(client_info["app_build"].is_string());
151 assert!(client_info["architecture"].is_string());
152 assert!(client_info["os"].is_string());
153 assert!(client_info["telemetry_sdk_build"].is_string());
154 // No client_id
155 let client_id = client_info["client_id"].as_str().unwrap();
156
157 pings::nofollows.submit(None);
158 let payload = rx.recv().unwrap();
159 let client_info = payload["client_info"].as_object().unwrap();
160 // General client info is set
161 assert!(client_info["app_build"].is_string());
162 assert!(client_info["architecture"].is_string());
163 assert!(client_info["os"].is_string());
164 assert!(client_info["telemetry_sdk_build"].is_string());
165 // No client_id
166 let nf_client_id = client_info["client_id"].as_str().unwrap();
167
168 assert_eq!(client_id, nf_client_id);
169
170 glean::shutdown();
171 }