summaryrefslogtreecommitdiffstats
path: root/python/mozperftest/mozperftest/tests/test_change_detector.py
blob: ee9fa5fa0f2d8f096a9101b5db158c57a149199b (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
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python
# 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 pathlib
from unittest import mock

import mozunit
import pytest

from mozperftest.tests.support import temp_file
from mozperftest.tools import PerformanceChangeDetected, run_change_detector


def test_change_detector_basic(kwargs=None, return_value=({}, {})):
    mocked_detector = mock.MagicMock()
    mocked_detector_module = mock.MagicMock()
    mocked_detector_module.ChangeDetector = mocked_detector

    with mock.patch.dict(
        "sys.modules",
        {
            "mozperftest_tools.regression_detector": mocked_detector_module,
        },
    ):
        mocked_detector.return_value.detect_changes.return_value = return_value

        with temp_file() as f:
            parent_dir = pathlib.Path(f).parent

            if kwargs is None:
                kwargs = {
                    "test_name": "browsertime-test",
                    "new_test_name": None,
                    "platform": "test-platform/opt",
                    "new_platform": None,
                    "base_branch": "try",
                    "new_branch": "try",
                    "base_revision": "99",
                    "new_revision": "99",
                }

            run_change_detector(parent_dir, kwargs)

        mocked_detector.return_value.detect_changes.assert_called()

    return mocked_detector_module


def test_change_detector_with_task_name():
    test_change_detector_basic(
        {
            "task_names": ["test-platform/opt-browsertime-test"],
            "new_test_name": None,
            "platform": None,
            "new_platform": None,
            "base_branch": "try",
            "new_branch": "try",
            "base_revision": "99",
            "new_revision": "99",
        }
    )


def test_change_detector_option_failure():
    with pytest.raises(Exception):
        test_change_detector_basic(
            {
                "test_name": None,
                "new_test_name": None,
                "platform": "test-platform/opt",
                "new_platform": None,
                "base_branch": "try",
                "new_branch": "try",
                "base_revision": "99",
                "new_revision": "99",
            }
        )

    with pytest.raises(Exception):
        test_change_detector_basic(
            {
                "test_name": "browsertime-test",
                "new_test_name": None,
                "platform": None,
                "new_platform": None,
                "base_branch": "try",
                "new_branch": "try",
                "base_revision": "99",
                "new_revision": "99",
            }
        )


def test_change_detector_with_detection():
    with pytest.raises(PerformanceChangeDetected):
        test_change_detector_basic(
            {
                "task_names": ["test-platform/opt-browsertime-test"],
                "new_test_name": None,
                "platform": None,
                "new_platform": None,
                "base_branch": "try",
                "new_branch": "try",
                "base_revision": "99",
                "new_revision": "99",
            },
            (["detection"], {"warm": {"metric": {"detection": [99]}}, "cold": {}}),
        )


if __name__ == "__main__":
    mozunit.main()