Bug 1456415 - Test generated artifact definitions json. r=chutten
authorJan-Erik Rediger <jrediger@mozilla.com>
Fri, 04 May 2018 06:29:00 +0300
changeset 473159 c5329a24af4fdcb5ce8feac93d42630bb2d4e96b
parent 473158 c1605fca9350b723f6cb7d85e9c8a508aaa48285
child 473160 218a57b6f00e1d8da9660858fb58498b984ddadc
push id1728
push userjlund@mozilla.com
push dateMon, 18 Jun 2018 21:12:27 +0000
treeherdermozilla-release@c296fde26f5f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerschutten
bugs1456415
milestone61.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 1456415 - Test generated artifact definitions json. r=chutten MozReview-Commit-ID: IM9v7QS4Hiw
toolkit/components/telemetry/tests/python/python.ini
toolkit/components/telemetry/tests/python/test_gen_event_data_json.py
toolkit/components/telemetry/tests/python/test_gen_scalar_data_json.py
--- a/toolkit/components/telemetry/tests/python/python.ini
+++ b/toolkit/components/telemetry/tests/python/python.ini
@@ -1,2 +1,4 @@
+[test_gen_event_data_json.py]
+[test_gen_scalar_data_json.py]
 [test_histogramtools_non_strict.py]
-[test_histogramtools_strict.py]
\ No newline at end of file
+[test_histogramtools_strict.py]
new file mode 100644
--- /dev/null
+++ b/toolkit/components/telemetry/tests/python/test_gen_event_data_json.py
@@ -0,0 +1,88 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+import json
+import mozunit
+import os
+import sys
+import tempfile
+import unittest
+from StringIO import StringIO
+from os import path
+
+TELEMETRY_ROOT_PATH = path.abspath(path.join(path.dirname(__file__), path.pardir, path.pardir))
+sys.path.append(TELEMETRY_ROOT_PATH)
+import gen_event_data   # noqa: E402
+
+
+class TestEventDataJson(unittest.TestCase):
+
+    def test_JSON_definitions_generation(self):
+        EVENTS_YAML = """
+with.optout:
+  testme1:
+    objects: ["test1"]
+    bug_numbers: [1456415]
+    notification_emails: ["telemetry-client-dev@mozilla.org"]
+    record_in_processes: ["main"]
+    description: opt-out event
+    release_channel_collection: opt-out
+    expiry_version: never
+    extra_keys:
+      message: a message 1
+with.optin:
+  testme2:
+    objects: ["test2"]
+    bug_numbers: [1456415]
+    notification_emails: ["telemetry-client-dev@mozilla.org"]
+    record_in_processes: ["main"]
+    description: opt-in event
+    release_channel_collection: opt-in
+    expiry_version: never
+    extra_keys:
+      message: a message 2
+        """
+
+        EXPECTED_JSON = {
+            "with.optout": {
+                "testme1": {
+                    "objects": ["test1"],
+                    "expired": False,
+                    "methods": ["testme1"],
+                    "extra_keys": ["message"],
+                    "record_on_release": True
+                }
+            },
+            "with.optin": {
+                "testme2": {
+                    "objects": ["test2"],
+                    "expired": False,
+                    "methods": ["testme2"],
+                    "extra_keys": ["message"],
+                    "record_on_release": False
+                }
+            },
+        }
+
+        io = StringIO()
+        try:
+            tmpfile = tempfile.NamedTemporaryFile(suffix=".json", delete=False)
+            # Write the event definition to the temporary file
+            tmpfile.write(EVENTS_YAML)
+            tmpfile.close()
+
+            # Let the parser generate the artifact definitions
+            gen_event_data.generate_JSON_definitions(io, tmpfile.name)
+        finally:
+            if tmpfile:
+                os.unlink(tmpfile.name)
+
+        event_definitions = json.loads(io.getvalue())
+
+        # Check that it generated the correct data
+        self.assertEqual(EXPECTED_JSON, event_definitions)
+
+
+if __name__ == '__main__':
+    mozunit.main()
new file mode 100644
--- /dev/null
+++ b/toolkit/components/telemetry/tests/python/test_gen_scalar_data_json.py
@@ -0,0 +1,83 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+import json
+import mozunit
+import os
+import sys
+import tempfile
+import unittest
+from StringIO import StringIO
+from os import path
+
+TELEMETRY_ROOT_PATH = path.abspath(path.join(path.dirname(__file__), path.pardir, path.pardir))
+sys.path.append(TELEMETRY_ROOT_PATH)
+import gen_scalar_data   # noqa: E402
+
+
+class TestScalarDataJson(unittest.TestCase):
+
+    def test_JSON_definitions_generation(self):
+        SCALARS_YAML = """
+newscalar:
+  withoptin:
+    bug_numbers:
+      - 1456415
+    description: opt-in scalar
+    expires: never
+    kind: uint
+    notification_emails: ["telemetry-client-dev@mozilla.org"]
+    record_in_processes: ["main"]
+    release_channel_collection: opt-in
+    keyed: false
+  withoptout:
+    bug_numbers:
+      - 1456415
+    description: opt-out scalar
+    expires: never
+    kind: string
+    notification_emails: ["telemetry-client-dev@mozilla.org"]
+    record_in_processes: ["main"]
+    release_channel_collection: opt-out
+    keyed: false
+        """
+
+        EXPECTED_JSON = {
+            "newscalar": {
+                "withoptout": {
+                    "kind": "nsITelemetry::SCALAR_TYPE_STRING",
+                    "expired": False,
+                    "record_on_release": True,
+                    "keyed": False
+                },
+                "withoptin": {
+                    "kind": "nsITelemetry::SCALAR_TYPE_COUNT",
+                    "expired": False,
+                    "record_on_release": False,
+                    "keyed": False
+                }
+            }
+        }
+
+        io = StringIO()
+        try:
+            tmpfile = tempfile.NamedTemporaryFile(suffix=".json", delete=False)
+            # Write the scalar definition to the temporary file
+            tmpfile.write(SCALARS_YAML)
+            tmpfile.close()
+
+            # Let the parser generate the artifact definitions
+            gen_scalar_data.generate_JSON_definitions(io, tmpfile.name)
+        finally:
+            if tmpfile:
+                os.unlink(tmpfile.name)
+
+        scalar_definitions = json.loads(io.getvalue())
+
+        # Check that it generated the correct data
+        self.assertEqual(EXPECTED_JSON, scalar_definitions)
+
+
+if __name__ == '__main__':
+    mozunit.main()