forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wpr_runner_test.py
209 lines (175 loc) · 6.61 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
# Copyright 2019 The Chromium Authors. All rights reserved.
# 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
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 _: '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: 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_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 = [
'Test Case \'-[a 1]\' started.',
'Test Case \'-[a 1]\' has uninteresting logs.',
'Test Case \'-[a 1]\' passed (0.1 seconds)',
'Test Case \'-[b 2]\' started.',
'Test Case \'-[b 2]\' passed (0.1 seconds)',
'Test Case \'-[c 3]\' started.',
'Test Case \'-[c 3]\' has interesting failure info.',
'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, shards=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()