File: test_python_server.py

package info (click to toggle)
glean-parser 15.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,260 kB
  • sloc: python: 7,033; ruby: 100; makefile: 87
file content (71 lines) | stat: -rw-r--r-- 1,812 bytes parent folder | download
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
# -*- coding: utf-8 -*-

# Any copyright is dedicated to the Public Domain.
# http://creativecommons.org/publicdomain/zero/1.0/

from pathlib import Path
import io
import json

import shutil
import subprocess


from glean_parser import translate
from glean_parser import validate_ping


ROOT = Path(__file__).parent


def test_no_event_metrics(tmp_path):
    """If no event metrics are provided parser should fail
    and no files should be generated"""

    translate.translate(
        [],
        "python_server",
        tmp_path,
    )
    assert all(False for _ in tmp_path.iterdir())


def test_logging(tmp_path):
    """Test that generated code can be used to log events in the right format."""

    glean_module_path = tmp_path / "glean"

    # generate logging code
    translate.translate(
        [
            ROOT / "data" / "server_metrics_with_event.yaml",
        ],
        "python_server",
        glean_module_path,
    )

    # copy ROOT / "test-py" / "test.py" to tmpdir
    shutil.copy(ROOT / "test-py" / "test.py", tmp_path)

    # run test script
    logged_output = subprocess.check_output(["python3", "test.py"], cwd=tmp_path).decode(
        "utf-8"
    )

    logged_output = json.loads(logged_output)
    fields = logged_output["Fields"]
    payload = fields["payload"]

    # validate that ping envelope contains all the required fields
    assert "glean-server-event" == logged_output["Type"]
    assert "accounts_backend" == fields["document_namespace"]
    assert "events" == fields["document_type"]
    assert "1" == fields["document_version"]
    assert "Mozilla/5.0 ..." == fields["user_agent"]

    schema_url = (
        "https://raw.githubusercontent.com/mozilla-services/"
        "mozilla-pipeline-schemas/main/"
        "schemas/glean/glean/glean.1.schema.json"
    )