Bug 1135759 - Adds test coverage for submission path r=janerik
authordanielleleb <danielleleb12@gmail.com>
Tue, 12 Feb 2019 15:12:19 +0000
changeset 458742 e0fbd0f5aab7
parent 458741 9571930df748
child 458743 0bdbfacae180
push id35548
push useropoprus@mozilla.com
push dateWed, 13 Feb 2019 09:48:26 +0000
treeherdermozilla-central@93e37c529818 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjanerik
bugs1135759
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1135759 - Adds test coverage for submission path r=janerik Differential Revision: https://phabricator.services.mozilla.com/D18500
toolkit/components/telemetry/tests/unit/head.js
toolkit/components/telemetry/tests/unit/test_TelemetryController.js
toolkit/components/telemetry/tests/unit/test_TelemetrySend.js
--- a/toolkit/components/telemetry/tests/unit/head.js
+++ b/toolkit/components/telemetry/tests/unit/head.js
@@ -168,16 +168,61 @@ function decodeRequestPayload(request) {
   }
 
   // Check for canary value
   Assert.notEqual(TelemetryUtils.knownClientID, payload.clientId, "Known clientId should never appear in a ping on the server");
 
   return payload;
 }
 
+function checkPingFormat(aPing, aType, aHasClientId, aHasEnvironment) {
+  const APP_VERSION = "1";
+  const APP_NAME = "XPCShell";
+  const PING_FORMAT_VERSION = 4;
+  const PLATFORM_VERSION = "1.9.2";
+  const MANDATORY_PING_FIELDS = [
+    "type", "id", "creationDate", "version", "application", "payload",
+  ];
+
+  const APPLICATION_TEST_DATA = {
+    buildId: gAppInfo.appBuildID,
+    name: APP_NAME,
+    version: APP_VERSION,
+    displayVersion: AppConstants.MOZ_APP_VERSION_DISPLAY,
+    vendor: "Mozilla",
+    platformVersion: PLATFORM_VERSION,
+    xpcomAbi: "noarch-spidermonkey",
+  };
+
+  // Check that the ping contains all the mandatory fields.
+  for (let f of MANDATORY_PING_FIELDS) {
+    Assert.ok(f in aPing, f + " must be available.");
+  }
+
+  Assert.equal(aPing.type, aType, "The ping must have the correct type.");
+  Assert.equal(aPing.version, PING_FORMAT_VERSION, "The ping must have the correct version.");
+
+  // Test the application section.
+  for (let f in APPLICATION_TEST_DATA) {
+    Assert.equal(aPing.application[f], APPLICATION_TEST_DATA[f],
+                 f + " must have the correct value.");
+  }
+
+  // We can't check the values for channel and architecture. Just make
+  // sure they are in.
+  Assert.ok("architecture" in aPing.application,
+            "The application section must have an architecture field.");
+  Assert.ok("channel" in aPing.application,
+            "The application section must have a channel field.");
+
+  // Check the clientId and environment fields, as needed.
+  Assert.equal("clientId" in aPing, aHasClientId);
+  Assert.equal("environment" in aPing, aHasEnvironment);
+}
+
 function wrapWithExceptionHandler(f) {
   function wrapper(...args) {
     try {
       f(...args);
     } catch (ex) {
       if (typeof(ex) != "object") {
         throw ex;
       }
--- a/toolkit/components/telemetry/tests/unit/test_TelemetryController.js
+++ b/toolkit/components/telemetry/tests/unit/test_TelemetryController.js
@@ -122,23 +122,16 @@ add_task(async function test_simplePing(
   // Update the Telemetry Server preference with the address of the local server.
   // Otherwise we might end up sending stuff to a non-existing server after
   // |TelemetryController.testReset| is called.
   Preferences.set(TelemetryUtils.Preferences.Server, "http://localhost:" + PingServer.port);
 
   await sendPing(false, false);
   let request = await PingServer.promiseNextRequest();
 
-  // Check that we have a version query parameter in the URL.
-  Assert.notEqual(request.queryString, "");
-
-  // Make sure the version in the query string matches the new ping format version.
-  let params = request.queryString.split("&");
-  Assert.ok(params.find(p => p == ("v=" + PING_FORMAT_VERSION)));
-
   let ping = decodeRequestPayload(request);
   checkPingFormat(ping, TEST_PING_TYPE, false, false);
 });
 
 add_task(async function test_disableDataUpload() {
   const OPTIN_PROBE = "telemetry.data_upload_optin";
   const isUnified = Preferences.get(TelemetryUtils.Preferences.Unified, false);
   if (!isUnified) {
--- a/toolkit/components/telemetry/tests/unit/test_TelemetrySend.js
+++ b/toolkit/components/telemetry/tests/unit/test_TelemetrySend.js
@@ -41,16 +41,32 @@ function setPingLastModified(id, timesta
 function waitForTimer() {
   return new Promise(resolve => {
     fakePingSendTimer((callback, timeout) => {
       resolve([callback, timeout]);
     }, () => {});
   });
 }
 
+function sendPing(aSendClientId, aSendEnvironment) {
+  const TEST_PING_TYPE = "test-ping-type";
+
+  if (PingServer.started) {
+    TelemetrySend.setServer("http://localhost:" + PingServer.port);
+  } else {
+    TelemetrySend.setServer("http://doesnotexist");
+  }
+
+  let options = {
+    addClientId: aSendClientId,
+    addEnvironment: aSendEnvironment,
+  };
+  return TelemetryController.submitExternalPing(TEST_PING_TYPE, {}, options);
+}
+
 // Allow easy faking of readable ping ids.
 // This helps with debugging issues with e.g. ordering in the send logic.
 function fakePingId(type, number) {
   const HEAD = "93bd0011-2c8f-4e1c-bee0-";
   const TAIL = "000000000000";
   const N = String(number);
   const id = HEAD + type + TAIL.slice(type.length, -N.length) + N;
   fakeGeneratePingId(() => id);
@@ -77,16 +93,22 @@ var checkPingsSaved = async function(pin
 
 function histogramValueCount(h) {
   return Object.values(h.values).reduce((a, b) => a + b, 0);
 }
 
 add_task(async function test_setup() {
   // Trigger a proper telemetry init.
   do_get_profile(true);
+
+  // Addon manager needs a profile directory.
+  loadAddonManager("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2");
+  finishAddonManagerStartup();
+  fakeIntlReady();
+
   // Make sure we don't generate unexpected pings due to pref changes.
   await setEmptyPrefWatchlist();
   Services.prefs.setBoolPref(TelemetryUtils.Preferences.HealthPingEnabled, true);
   TelemetryStopwatch.setTestModeEnabled(true);
 });
 
 // Test the ping sending logic.
 add_task(async function test_sendPendingPings() {
@@ -528,16 +550,51 @@ add_task(async function test_sendCheckOv
   const ping = await PingServer.promiseNextPing();
   Assert.equal(ping.type, TEST_PING_TYPE, "Must receive a ping of the expected type");
 
   // Restore the test mode and disable the override.
   TelemetrySend.setTestModeEnabled(true);
   Services.prefs.clearUserPref(TelemetryUtils.Preferences.OverrideOfficialCheck);
 });
 
+add_task(async function test_submissionPath() {
+  const PING_FORMAT_VERSION = 4;
+  const TEST_PING_TYPE = "test-ping-type";
+
+  await TelemetrySend.reset();
+  PingServer.clearRequests();
+
+  await sendPing(false, false);
+
+  // Fetch the request from the server.
+  let request = await PingServer.promiseNextRequest();
+
+  // Get the payload.
+  let ping = decodeRequestPayload(request);
+  checkPingFormat(ping, TEST_PING_TYPE, false, false);
+
+  let app = ping.application;
+  let pathComponents = [
+    ping.id, ping.type, app.name, app.version, app.channel, app.buildId,
+  ];
+
+  let urlComponents = request.path.split("/");
+
+  for (let i = 0; i < pathComponents.length; i++) {
+    Assert.ok(urlComponents.includes(pathComponents[i]), `Path should include ${pathComponents[i]}`);
+  }
+
+  // Check that we have a version query parameter in the URL.
+  Assert.notEqual(request.queryString, "");
+
+  // Make sure the version in the query string matches the new ping format version.
+  let params = request.queryString.split("&");
+  Assert.ok(params.find(p => p == ("v=" + PING_FORMAT_VERSION)));
+});
+
 add_task(async function testCookies() {
   const TEST_TYPE = "test-cookies";
 
   await TelemetrySend.reset();
   PingServer.clearRequests();
 
   let uri = Services.io.newURI("http://localhost:" + PingServer.port);
   Services.cookies.setCookieString(uri, null, "cookie-time=yes", null);