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
|
import os
import shutil
from unittest import mock
import pytest
from mozperftest.environment import SYSTEM, TEST
from mozperftest.test.mochitest import MissingMochitestInformation
from mozperftest.tests.support import (
EXAMPLE_MOCHITEST_TEST,
get_running_env,
)
def running_env(**kw):
return get_running_env(flavor="mochitest", **kw)
@mock.patch("mozperftest.test.mochitest.ON_TRY", new=False)
@mock.patch("mozperftest.utils.ON_TRY", new=False)
def test_mochitest_metrics(*mocked):
mach_cmd, metadata, env = running_env(
tests=[str(EXAMPLE_MOCHITEST_TEST)],
mochitest_extra_args=[],
)
sys = env.layers[SYSTEM]
mochitest = env.layers[TEST]
with mock.patch("moztest.resolve.TestResolver") as test_resolver_mock, mock.patch(
"mozperftest.test.functionaltestrunner.load_class_from_path"
) as load_class_path_mock, mock.patch(
"mozperftest.test.functionaltestrunner.mozlog.formatters.MachFormatter.__new__"
) as formatter_mock, mock.patch(
"mozperftest.test.mochitest.install_requirements_file"
):
formatter_mock.return_value = lambda x: x
def test_print(*args, **kwargs):
log_processor = kwargs.get("custom_handler")
log_processor.__call__('perfMetrics | { "fake": 0 }')
return 0
test_mock = mock.MagicMock()
test_mock.test = test_print
load_class_path_mock.return_value = test_mock
test_resolver_mock.resolve_metadata.return_value = (1, 1)
mach_cmd._spawn.return_value = test_resolver_mock
try:
with sys as s, mochitest as m:
m(s(metadata))
finally:
shutil.rmtree(mach_cmd._mach_context.state_dir)
res = metadata.get_results()
assert len(res) == 1
assert res[0]["name"] == "test_mochitest.html"
results = res[0]["results"]
assert results[0]["name"] == "fake"
assert results[0]["values"] == [0]
@mock.patch(
# This mock.patch actually patches the mochitest run_test_harness function
"runtests.run_test_harness"
)
@mock.patch(
# This mock.patch causes mochitest's runtests to be imported instead of
# others in the remote_run
"mochitest.runtests.run_test_harness",
new=mock.MagicMock(),
)
@mock.patch(
"mozperftest.test.functionaltestrunner.mozlog.formatters.MachFormatter.__new__"
)
@mock.patch(
"mozperftest.test.mochitest.install_requirements_file", new=mock.MagicMock()
)
@mock.patch(
"mozperftest.test.functionaltestrunner.load_class_from_path", new=mock.MagicMock()
)
@mock.patch("moztest.resolve.TestResolver", new=mock.MagicMock())
@mock.patch("mozperftest.test.mochitest.ON_TRY", new=True)
@mock.patch("mozperftest.utils.ON_TRY", new=True)
@mock.patch("mochitest.mochitest_options.MochitestArgumentParser", new=mock.MagicMock())
@mock.patch("manifestparser.TestManifest", new=mock.MagicMock())
def test_mochitest_ci_metrics(formatter_mock, run_test_harness_mock):
if not os.getenv("MOZ_FETCHES_DIR"):
os.environ["MOZ_FETCHES_DIR"] = "fake-path"
mach_cmd, metadata, env = running_env(
tests=[str(EXAMPLE_MOCHITEST_TEST)],
mochitest_extra_args=[],
mochitest_manifest="fake.ini",
mochitest_manifest_flavor="mocha",
)
system = env.layers[SYSTEM]
mochitest = env.layers[TEST]
formatter_mock.return_value = lambda x: x
def test_print(*args, **kwargs):
print('perfMetrics | { "fake": 0 }')
return 0
run_test_harness_mock.side_effect = test_print
try:
with system as s, mochitest as m:
m(s(metadata))
finally:
shutil.rmtree(mach_cmd._mach_context.state_dir)
res = metadata.get_results()
assert len(res) == 1
assert res[0]["name"] == "test_mochitest.html"
results = res[0]["results"]
assert results[0]["name"] == "fake"
assert results[0]["values"] == [0]
@mock.patch(
# This mock.patch actually patches the mochitest run_test_harness function
"runtests.run_test_harness",
new=mock.MagicMock(),
)
@mock.patch(
# This mock.patch causes mochitest's runtests to be imported instead of
# others in the remote_run
"mochitest.runtests.run_test_harness",
new=mock.MagicMock(),
)
@mock.patch(
"mozperftest.test.functionaltestrunner.mozlog.formatters.MachFormatter.__new__",
new=mock.MagicMock(),
)
@mock.patch(
"mozperftest.test.mochitest.install_requirements_file", new=mock.MagicMock()
)
@mock.patch(
"mozperftest.test.functionaltestrunner.load_class_from_path", new=mock.MagicMock()
)
@mock.patch("moztest.resolve.TestResolver", new=mock.MagicMock())
@mock.patch("mozperftest.test.mochitest.ON_TRY", new=True)
@mock.patch("mozperftest.utils.ON_TRY", new=True)
@mock.patch("mochitest.mochitest_options.MochitestArgumentParser", new=mock.MagicMock())
@mock.patch("manifestparser.TestManifest", new=mock.MagicMock())
def test_mochitest_ci_metrics_missing_manifest():
if not os.getenv("MOZ_FETCHES_DIR"):
os.environ["MOZ_FETCHES_DIR"] = "fake-path"
mach_cmd, metadata, env = running_env(
tests=[str(EXAMPLE_MOCHITEST_TEST)],
mochitest_extra_args=[],
mochitest_manifest_flavor="mocha",
)
system = env.layers[SYSTEM]
mochitest = env.layers[TEST]
try:
with pytest.raises(MissingMochitestInformation) as exc:
with system as s, mochitest as m:
m(s(metadata))
assert "manifest" in exc.value.args[0]
finally:
shutil.rmtree(mach_cmd._mach_context.state_dir)
res = metadata.get_results()
assert len(res) == 0
@mock.patch(
# This mock.patch actually patches the mochitest run_test_harness function
"runtests.run_test_harness",
new=mock.MagicMock(),
)
@mock.patch(
# This mock.patch causes mochitest's runtests to be imported instead of
# others in the remote_run
"mochitest.runtests.run_test_harness",
new=mock.MagicMock(),
)
@mock.patch(
"mozperftest.test.functionaltestrunner.mozlog.formatters.MachFormatter.__new__",
new=mock.MagicMock(),
)
@mock.patch(
"mozperftest.test.mochitest.install_requirements_file", new=mock.MagicMock()
)
@mock.patch(
"mozperftest.test.functionaltestrunner.load_class_from_path", new=mock.MagicMock()
)
@mock.patch("moztest.resolve.TestResolver", new=mock.MagicMock())
@mock.patch("mozperftest.test.mochitest.ON_TRY", new=True)
@mock.patch("mozperftest.utils.ON_TRY", new=True)
@mock.patch("mochitest.mochitest_options.MochitestArgumentParser", new=mock.MagicMock())
@mock.patch("manifestparser.TestManifest", new=mock.MagicMock())
def test_mochitest_ci_metrics_missing_flavor():
if not os.getenv("MOZ_FETCHES_DIR"):
os.environ["MOZ_FETCHES_DIR"] = "fake-path"
mach_cmd, metadata, env = running_env(
tests=[str(EXAMPLE_MOCHITEST_TEST)],
mochitest_extra_args=[],
mochitest_manifest="fake.ini",
)
system = env.layers[SYSTEM]
mochitest = env.layers[TEST]
try:
with pytest.raises(MissingMochitestInformation) as exc:
with system as s, mochitest as m:
m(s(metadata))
assert "flavor" in exc.value.args[0]
finally:
shutil.rmtree(mach_cmd._mach_context.state_dir)
res = metadata.get_results()
assert len(res) == 0
|