forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwpr_runner_test.py
executable file
·283 lines (232 loc) · 8.58 KB
/
wpr_runner_test.py
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
#!/usr/bin/env vpython3
# Copyright 2019 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Unittests for wpr_runner.py."""
import glob
import logging
import os
import subprocess
import unittest
from unittest.mock import Mock, call
import iossim_util
import test_runner
import test_runner_test
import wpr_runner
class WprProxySimulatorTestRunnerTest(test_runner_test.TestCase):
"""Tests for test_runner.WprProxySimulatorTestRunner."""
def setUp(self):
super(WprProxySimulatorTestRunnerTest, self).setUp()
self.mock(test_runner, 'get_current_xcode_info', lambda: {
'version': 'test version', 'build': 'test build', 'path': 'test/path'})
self.mock(test_runner.subprocess,
'check_output', lambda _: b'fake-bundle-id')
self.mock(os.path, 'abspath', lambda path: '/abs/path/to/%s' % path)
self.mock(os.path, 'exists', lambda _: True)
self.mock(test_runner.TestRunner, 'set_sigterm_handler',
lambda self, handler: 0)
self.mock(test_runner.SimulatorTestRunner, 'getSimulator',
lambda _: 'fake-id')
self.mock(test_runner.SimulatorTestRunner, 'deleteSimulator',
lambda a, b: True)
self.mock(wpr_runner.WprProxySimulatorTestRunner,
'copy_trusted_certificate', lambda a, b: True)
self.mock(iossim_util, 'get_simulator',
lambda a, b: 'E4E66320-177A-450A-9BA1-488D85B7278E')
def test_app_not_found(self):
"""Ensures AppNotFoundError is raised."""
self.mock(os.path, 'exists', lambda p: not p.endswith('bad-host-app-path'))
with self.assertRaises(test_runner.AppNotFoundError):
wpr_runner.WprProxySimulatorTestRunner(
'fake-app',
'bad-host-app-path',
'fake-iossim',
'replay-path',
'platform',
'os',
'wpr-tools-path',
'out-dir',
)
def test_replay_path_not_found(self):
"""Ensures ReplayPathNotFoundError is raised."""
self.mock(os.path, 'exists', lambda p: not p.endswith('bad-replay-path'))
with self.assertRaises(wpr_runner.ReplayPathNotFoundError):
wpr_runner.WprProxySimulatorTestRunner(
'fake-app',
'fake-host-app',
'fake-iossim',
'bad-replay-path',
'platform',
'os',
'wpr-tools-path',
'out-dir',
)
def test_wpr_tools_not_found(self):
"""Ensures WprToolsNotFoundError is raised."""
self.mock(os.path, 'exists', lambda p: not p.endswith('bad-tools-path'))
with self.assertRaises(wpr_runner.WprToolsNotFoundError):
wpr_runner.WprProxySimulatorTestRunner(
'fake-app',
'fake-host-app',
'fake-iossim',
'replay-path',
'platform',
'os',
'bad-tools-path',
'out-dir',
)
def test_cert_not_found(self):
"""Ensures CertPathNotFoundError is raised"""
runner = wpr_runner.WprProxySimulatorTestRunner(
'fake-app',
'fake-host-app',
'fake-iossim',
'replay-path',
'platform',
'os',
'wpr-tools-path',
'out-dir',
)
class FakeOsPath():
def __init__(self):
self.numCalls = 0
def __call__(self, a):
if self.numCalls == 0:
self.numCalls += 1
return True
self.numCalls += 1
return not a.endswith('wpr-tools-path/web_page_replay_go/wpr_cert.pem')
self.mock(os.path, 'exists', FakeOsPath())
self.mock(subprocess, 'check_call', lambda *args: None)
self.unmock(wpr_runner.WprProxySimulatorTestRunner,
'copy_trusted_certificate')
with self.assertRaises(wpr_runner.CertPathNotFoundError):
runner.copy_trusted_certificate('fake-udid')
def test_copy_cert(self):
"""Ensures right commands are issued to copy cert"""
runner = wpr_runner.WprProxySimulatorTestRunner(
'fake-app',
'fake-host-app',
'fake-iossim',
'replay-path',
'platform',
'os',
'wpr-tools-path',
'out-dir',
)
self.unmock(wpr_runner.WprProxySimulatorTestRunner,
'copy_trusted_certificate')
check_call_mock = Mock()
self.mock(subprocess, 'check_call', check_call_mock)
runner.copy_trusted_certificate('UDID')
calls = [
call(['xcrun', 'simctl', 'boot', 'UDID']),
call([
'xcrun', 'simctl', 'keychain', 'UDID', 'add-root-cert',
'wpr-tools-path/web_page_replay_go/wpr_cert.pem'
]),
call(['xcrun', 'simctl', 'shutdown', 'UDID'])
]
check_call_mock.assert_has_calls(calls)
# ensure subprocess.check_call was only called 3 times
self.assertEqual(check_call_mock.call_count, 3)
def test_init(self):
"""Ensures instance is created."""
tr = wpr_runner.WprProxySimulatorTestRunner(
'fake-app',
'fake-host-app',
'fake-iossim',
'replay-path',
'platform',
'os',
'wpr-tools-path',
'out-dir',
)
self.assertTrue(tr)
def run_wpr_test(self, test_filter=[], invert=False):
"""Wrapper that mocks the _run method and returns its result."""
class FakeStdout:
def __init__(self):
self.line_index = 0
self.lines = [
b'Test Case \'-[a 1]\' started.',
b'Test Case \'-[a 1]\' has uninteresting logs.',
b'Test Case \'-[a 1]\' passed (0.1 seconds)',
b'Test Case \'-[b 2]\' started.',
b'Test Case \'-[b 2]\' passed (0.1 seconds)',
b'Test Case \'-[c 3]\' started.',
b'Test Case \'-[c 3]\' has interesting failure info.',
b'Test Case \'-[c 3]\' failed (0.1 seconds)',
]
def readline(self):
if self.line_index < len(self.lines):
return_line = self.lines[self.line_index]
self.line_index += 1
return return_line
else:
return None
class FakeProcess:
def __init__(self):
self.stdout = FakeStdout()
self.returncode = 0
def stdout(self):
return self.stdout
def wait(self):
return
def popen(recipe_cmd, env, stdout, stderr):
return FakeProcess()
tr = wpr_runner.WprProxySimulatorTestRunner(
'fake-app',
'fake-host-app',
'fake-iossim',
'replay-path',
'platform',
'os',
'wpr-tools-path',
'out-dir',
)
self.mock(wpr_runner.WprProxySimulatorTestRunner, 'wprgo_start',
lambda a, b: None)
self.mock(wpr_runner.WprProxySimulatorTestRunner, 'wprgo_stop',
lambda _: None)
self.mock(wpr_runner.WprProxySimulatorTestRunner, 'get_wpr_test_command',
lambda a, b, c: ["command", "arg"])
self.mock(os.path, 'isfile', lambda _: True)
self.mock(glob, 'glob', lambda _: ["file1", "file2"])
self.mock(subprocess, 'Popen', popen)
tr.xctest_path = 'fake.xctest'
cmd = {'invert': invert, 'test_filter': test_filter}
return tr._run(cmd=cmd, clones=1)
def test_run_no_filter(self):
"""Ensures the _run method can handle passed and failed tests."""
result = self.run_wpr_test()
self.assertIn('file1.a/1', result.passed_tests())
self.assertIn('file1.b/2', result.passed_tests())
self.assertIn('file1.c/3', result.failed_tests())
self.assertIn('file2.a/1', result.passed_tests())
self.assertIn('file2.b/2', result.passed_tests())
self.assertIn('file2.c/3', result.failed_tests())
def test_run_with_filter(self):
"""Ensures the _run method works with a filter."""
result = self.run_wpr_test(test_filter=["file1"], invert=False)
self.assertIn('file1.a/1', result.passed_tests())
self.assertIn('file1.b/2', result.passed_tests())
self.assertIn('file1.c/3', result.failed_tests())
self.assertNotIn('file2.a/1', result.passed_tests())
self.assertNotIn('file2.b/2', result.passed_tests())
self.assertNotIn('file2.c/3', result.failed_tests())
def test_run_with_inverted_filter(self):
"""Ensures the _run method works with an inverted filter."""
result = self.run_wpr_test(test_filter=["file1"], invert=True)
self.assertNotIn('file1.a/1', result.passed_tests())
self.assertNotIn('file1.b/2', result.passed_tests())
self.assertNotIn('file1.c/3', result.failed_tests())
self.assertIn('file2.a/1', result.passed_tests())
self.assertIn('file2.b/2', result.passed_tests())
self.assertIn('file2.c/3', result.failed_tests())
if __name__ == '__main__':
logging.basicConfig(
format='[%(asctime)s:%(levelname)s] %(message)s',
level=logging.DEBUG,
datefmt='%I:%M:%S')
unittest.main()