summaryrefslogtreecommitdiffstats
path: root/tools/tryselect/test/test_perfcomparators.py
blob: 51f0bdb287629a48eba5f23065160a1added2e0b (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# 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 tempfile
from unittest import mock

import mozunit
import pytest
from tryselect.selectors.perfselector.perfcomparators import (
    BadComparatorArgs,
    BenchmarkComparator,
    ComparatorNotFound,
    get_comparator,
)


@pytest.mark.parametrize(
    "test_link",
    [
        "https://github.com/mozilla-mobile/firefox-android/pull/1627",
        "https://github.com/mozilla-mobile/firefox-android/pull/1876/"
        "commits/17c7350cc37a4a85cea140a7ce54e9fd037b5365",
    ],
)
def test_benchmark_comparator(test_link):
    def _verify_extra_args(extra_args):
        assert len(extra_args) == 3
        if "commit" in test_link:
            assert (
                "benchmark-revision=17c7350cc37a4a85cea140a7ce54e9fd037b5365"
                in extra_args
            )
        else:
            assert "benchmark-revision=sha-for-link" in extra_args
        assert "benchmark-repository=url-for-link" in extra_args
        assert "benchmark-branch=ref-for-link" in extra_args

    comparator = BenchmarkComparator(
        None, None, None, [f"base-link={test_link}", f"new-link={test_link}"]
    )

    with mock.patch("requests.get") as mocked_get:
        magic_get = mock.MagicMock()
        magic_get.json.return_value = {
            "head": {
                "repo": {
                    "html_url": "url-for-link",
                },
                "sha": "sha-for-link",
                "ref": "ref-for-link",
            }
        }
        magic_get.status_code = 200
        mocked_get.return_value = magic_get

        extra_args = []
        comparator.setup_base_revision(extra_args)
        _verify_extra_args(extra_args)

        extra_args = []
        comparator.setup_new_revision(extra_args)
        _verify_extra_args(extra_args)


def test_benchmark_comparator_no_pr_links():
    def _verify_extra_args(extra_args):
        assert len(extra_args) == 3
        assert "benchmark-revision=rev" in extra_args
        assert "benchmark-repository=link" in extra_args
        assert "benchmark-branch=fake" in extra_args

    comparator = BenchmarkComparator(
        None,
        None,
        None,
        [
            "base-repo=link",
            "base-branch=fake",
            "base-revision=rev",
            "new-repo=link",
            "new-branch=fake",
            "new-revision=rev",
        ],
    )

    with mock.patch("requests.get") as mocked_get:
        magic_get = mock.MagicMock()
        magic_get.json.return_value = {
            "head": {
                "repo": {
                    "html_url": "url-for-link",
                },
                "sha": "sha-for-link",
                "ref": "ref-for-link",
            }
        }
        magic_get.status_code = 200
        mocked_get.return_value = magic_get

        extra_args = []
        comparator.setup_base_revision(extra_args)
        _verify_extra_args(extra_args)

        extra_args = []
        comparator.setup_new_revision(extra_args)
        _verify_extra_args(extra_args)


def test_benchmark_comparator_bad_args():
    comparator = BenchmarkComparator(
        None,
        None,
        None,
        [
            "base-bad-args=val",
        ],
    )

    with pytest.raises(BadComparatorArgs):
        comparator.setup_base_revision([])


def test_get_comparator_bad_name():
    with pytest.raises(ComparatorNotFound):
        get_comparator("BadName")


def test_get_comparator_bad_script():
    with pytest.raises(ComparatorNotFound):
        with tempfile.NamedTemporaryFile() as tmpf:
            tmpf.close()
            get_comparator(tmpf.name)


def test_get_comparator_benchmark_name():
    comparator_klass = get_comparator("BenchmarkComparator")
    assert comparator_klass.__name__ == "BenchmarkComparator"


def test_get_comparator_benchmark_script():
    # If the get_comparator method is working for scripts, then
    # it should find the first defined class in this file, or the
    # first imported class that matches it
    comparator_klass = get_comparator(__file__)
    assert comparator_klass.__name__ == "BenchmarkComparator"


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