summaryrefslogtreecommitdiffstats
path: root/tools/lint/test/test_condprof_addons.py
blob: e1401a7119ff40117482f31ea7b2047f60301a74 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import importlib
import tempfile
from pathlib import Path
from unittest import mock

import mozunit
import requests

LINTER = "condprof-addons"


def linter_module_mocks(
    customizations_path=".", browsertime_fetches_path="browsertime.yml", **othermocks
):
    return mock.patch.multiple(
        LINTER,
        CUSTOMIZATIONS_PATH=Path(customizations_path),
        BROWSERTIME_FETCHES_PATH=Path(browsertime_fetches_path),
        **othermocks,
    )


def linter_class_mocks(**mocks):
    return mock.patch.multiple(
        f"{LINTER}.CondprofAddonsLinter",
        **mocks,
    )


# Sanity check (make sure linter message includes the xpi filename).
def test_get_missing_xpi_msg(lint, paths):
    condprof_addons = importlib.import_module("condprof-addons")
    with linter_class_mocks(
        get_firefox_addons_tar_names=mock.Mock(return_value=list()),
    ):
        instance = condprof_addons.CondprofAddonsLinter(
            topsrcdir=paths()[0], logger=mock.Mock()
        )
        assert instance.get_missing_xpi_msg("test.xpi").startswith(
            "test.xpi is missing"
        )


def test_xpi_missing_from_firefox_addons_tar(lint, paths):
    fixture_customizations = paths("with-missing-xpi.json")
    with linter_module_mocks(), linter_class_mocks(
        get_firefox_addons_tar_names=mock.Mock(return_value=list()),
    ):
        logger_mock = mock.Mock()
        lint(fixture_customizations, logger=logger_mock)
        assert logger_mock.lint_error.call_count == 1
        assert Path(fixture_customizations[0]).samefile(
            logger_mock.lint_error.call_args.kwargs["path"]
        )
        importlib.import_module("condprof-addons")
        assert "non-existing.xpi" in logger_mock.lint_error.call_args.args[0]


def test_xpi_all_found_in_firefox_addons_tar(lint, paths):
    get_tarnames_mock = mock.Mock(
        return_value=["an-extension.xpi", "another-extension.xpi"]
    )
    read_json_mock = mock.Mock(
        return_value={
            "addons": {
                "an-extension": "http://localhost/ext/an-extension.xpi",
                "another-extension": "http://localhost/ext/another-extension.xpi",
            }
        }
    )

    with linter_module_mocks(), linter_class_mocks(
        get_firefox_addons_tar_names=get_tarnames_mock, read_json=read_json_mock
    ):
        logger_mock = mock.Mock()
        # Compute a fake condprof customization path, the content is
        # going to be the read_json_mock.return_value and so the
        # fixture file does not actually exists.
        fixture_customizations = paths("fake-condprof-config.json")
        lint(
            fixture_customizations,
            logger=logger_mock,
            config={"include": paths(), "extensions": ["json", "yml"]},
        )
        assert read_json_mock.call_count == 1
        assert get_tarnames_mock.call_count == 1
        assert logger_mock.lint_error.call_count == 0


def test_lint_error_on_missing_or_invalid_firefoxaddons_fetch_task(
    lint,
    paths,
):
    read_json_mock = mock.Mock(return_value=dict())
    read_yaml_mock = mock.Mock(return_value=dict())
    # Verify that an explicit linter error is reported if the fetch task is not found.
    with linter_module_mocks(), linter_class_mocks(
        read_json=read_json_mock, read_yaml=read_yaml_mock
    ):
        logger_mock = mock.Mock()
        fixture_customizations = paths("fake-condprof-config.json")
        condprof_addons = importlib.import_module("condprof-addons")

        def assert_linter_error(yaml_mock_value, expected_msg):
            logger_mock.reset_mock()
            read_yaml_mock.return_value = yaml_mock_value
            lint(fixture_customizations, logger=logger_mock)
            assert logger_mock.lint_error.call_count == 1
            expected_path = condprof_addons.BROWSERTIME_FETCHES_PATH
            assert logger_mock.lint_error.call_args.kwargs["path"] == expected_path
            assert logger_mock.lint_error.call_args.args[0] == expected_msg

        # Mock a yaml file that is not including the expected firefox-addons fetch task.
        assert_linter_error(
            yaml_mock_value=dict(), expected_msg=condprof_addons.ERR_FETCH_TASK_MISSING
        )
        # Mock a yaml file where firefox-addons is missing the fetch attribute.
        assert_linter_error(
            yaml_mock_value={"firefox-addons": {}},
            expected_msg=condprof_addons.ERR_FETCH_TASK_MISSING,
        )
        # Mock a yaml file where firefox-addons add-prefix is missing.
        assert_linter_error(
            yaml_mock_value={"firefox-addons": {"fetch": {}}},
            expected_msg=condprof_addons.ERR_FETCH_TASK_ADDPREFIX,
        )
        # Mock a yaml file where firefox-addons add-prefix is invalid.
        assert_linter_error(
            yaml_mock_value={
                "firefox-addons": {"fetch": {"add-prefix": "invalid-subdir-name/"}}
            },
            expected_msg=condprof_addons.ERR_FETCH_TASK_ADDPREFIX,
        )


def test_get_xpi_list_from_fetch_dir(lint, paths):
    # Verify that when executed on the CI, the helper method looks for the xpi files
    # in the MOZ_FETCHES_DIR subdir where they are expected to be unpacked by the
    # fetch task.
    with linter_module_mocks(
        MOZ_AUTOMATION=1, MOZ_FETCHES_DIR=paths("fake-fetches-dir")[0]
    ):
        condprof_addons = importlib.import_module("condprof-addons")
        logger_mock = mock.Mock()
        Path(paths("browsertime.yml")[0])

        linter = condprof_addons.CondprofAddonsLinter(
            topsrcdir=paths()[0], logger=logger_mock
        )
        results = linter.tar_xpi_filenames

        results.sort()
        assert results == ["fake-ext-01.xpi", "fake-ext-02.xpi"]


def test_get_xpi_list_from_downloaded_tar(lint, paths):
    def mocked_download_tar(firefox_addons_tar_url, tar_tmp_path):
        tar_tmp_path.write_bytes(Path(paths("firefox-addons-fake.tar")[0]).read_bytes())

    download_firefox_addons_tar_mock = mock.Mock()
    download_firefox_addons_tar_mock.side_effect = mocked_download_tar

    # Verify that when executed locally on a developer machine, the tar archive is downloaded
    # and the list of xpi files included in it returned by the helper method.
    with tempfile.TemporaryDirectory() as tempdir, linter_module_mocks(
        MOZ_AUTOMATION=0,
        tempdir=tempdir,
    ), linter_class_mocks(
        download_firefox_addons_tar=download_firefox_addons_tar_mock,
    ):
        condprof_addons = importlib.import_module("condprof-addons")
        logger_mock = mock.Mock()
        Path(paths("browsertime.yml")[0])

        linter = condprof_addons.CondprofAddonsLinter(
            topsrcdir=paths()[0], logger=logger_mock
        )
        results = linter.tar_xpi_filenames
        assert len(results) > 0
        print("List of addons found in the downloaded file archive:", results)
        assert all(filename.endswith(".xpi") for filename in results)
        assert download_firefox_addons_tar_mock.call_count == 1


@mock.patch("requests.get")
def test_error_on_downloading_tar(requests_get_mock, lint, paths):
    # Verify that when executed locally and the tar archive fails to download
    # the linter does report an explicit linting error with the http error included.
    with tempfile.TemporaryDirectory() as tempdir, linter_module_mocks(
        MOZ_AUTOMATION=0, tempdir=tempdir
    ):
        condprof_addons = importlib.import_module("condprof-addons")
        logger_mock = mock.Mock()
        response_mock = mock.Mock()
        response_mock.raise_for_status.side_effect = requests.exceptions.HTTPError(
            "MOCK_ERROR"
        )
        requests_get_mock.return_value = response_mock
        Path(paths("browsertime.yml")[0])

        linter = condprof_addons.CondprofAddonsLinter(
            topsrcdir=paths()[0], logger=logger_mock
        )

        assert (
            logger_mock.lint_error.call_args.kwargs["path"]
            == condprof_addons.BROWSERTIME_FETCHES_PATH
        )
        assert (
            logger_mock.lint_error.call_args.args[0]
            == f"{condprof_addons.ERR_FETCH_TASK_ARCHIVE}, MOCK_ERROR"
        )
        assert requests_get_mock.call_count == 1
        assert len(linter.tar_xpi_filenames) == 0


@mock.patch("requests.get")
def test_error_on_opening_tar(requests_get_mock, lint, paths):
    # Verify that when executed locally and the tar archive fails to open
    # the linter does report an explicit linting error with the tarfile error included.
    with tempfile.TemporaryDirectory() as tempdir, linter_module_mocks(
        MOZ_AUTOMATION=0, tempdir=tempdir
    ):
        condprof_addons = importlib.import_module("condprof-addons")
        logger_mock = mock.Mock()
        response_mock = mock.Mock()
        response_mock.raise_for_status.return_value = None

        def mock_iter_content(chunk_size):
            yield b"fake tar content"
            yield b"expected to trigger tarfile.ReadError"

        response_mock.iter_content.side_effect = mock_iter_content
        requests_get_mock.return_value = response_mock
        Path(paths("browsertime.yml")[0])

        linter = condprof_addons.CondprofAddonsLinter(
            topsrcdir=paths()[0], logger=logger_mock
        )

        assert (
            logger_mock.lint_error.call_args.kwargs["path"]
            == condprof_addons.BROWSERTIME_FETCHES_PATH
        )
        actual_msg = logger_mock.lint_error.call_args.args[0]
        print("Got linter error message:", actual_msg)
        assert actual_msg.startswith(
            f"{condprof_addons.ERR_FETCH_TASK_ARCHIVE}, file could not be opened successfully"
        )
        assert requests_get_mock.call_count == 1
        assert len(linter.tar_xpi_filenames) == 0


def test_lint_all_customization_files_when_linting_browsertime_yml(
    lint,
    paths,
):
    get_tarnames_mock = mock.Mock(return_value=["an-extension.xpi"])
    read_json_mock = mock.Mock(
        return_value={
            "addons": {"an-extension": "http://localhost/ext/an-extension.xpi"}
        }
    )
    with linter_module_mocks(
        customizations_path="fake-customizations-dir",
    ), linter_class_mocks(
        get_firefox_addons_tar_names=get_tarnames_mock,
        read_json=read_json_mock,
    ):
        logger_mock = mock.Mock()
        importlib.import_module("condprof-addons")
        # When mozlint detects a change to the ci fetch browser.yml support file,
        # condprof-addons linter is called for the entire customizations dir path
        # and we expect that to be expanded to the list of the json customizations
        # files from that directory path.
        lint(paths("fake-customizations-dir"), logger=logger_mock)
        # Expect read_json_mock to be called once per each of the json files
        # found in the fixture dir.
        assert read_json_mock.call_count == 3
        assert get_tarnames_mock.call_count == 1
        assert logger_mock.lint_error.call_count == 0


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