summaryrefslogtreecommitdiffstats
path: root/toolkit/components/telemetry/tests/python/test_gen_event_data_json.py
blob: 5caa4cebbc598a22309a6e89a0f3195083b997a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 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 os
import sys
import tempfile
import unittest
from io import StringIO
from os import path

import mozunit

TELEMETRY_ROOT_PATH = path.abspath(
    path.join(path.dirname(__file__), path.pardir, path.pardir)
)
sys.path.append(TELEMETRY_ROOT_PATH)
# The generators live in "build_scripts", account for that.
# NOTE: if the generators are moved, this logic will need to be updated.
sys.path.append(path.join(TELEMETRY_ROOT_PATH, "build_scripts"))
import gen_event_data  # noqa: E402


class TestEventDataJson(unittest.TestCase):
    maxDiff = None

    def test_JSON_definitions_generation(self):
        EVENTS_YAML = b"""
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
    products:
      - firefox
    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
    products: ['firefox', 'fennec']
    extra_keys:
      message: a message 2
        """

        EXPECTED_JSON = {
            "with.optout": {
                "testme1": {
                    "objects": ["test1"],
                    "expired": False,
                    "expires": "never",
                    "methods": ["testme1"],
                    "extra_keys": ["message"],
                    "record_on_release": True,
                    "products": ["firefox"],
                }
            },
            "with.optin": {
                "testme2": {
                    "objects": ["test2"],
                    "expired": False,
                    "expires": "never",
                    "methods": ["testme2"],
                    "extra_keys": ["message"],
                    "record_on_release": False,
                    "products": ["firefox", "fennec"],
                }
            },
        }

        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()