File: local_device_instrumentation_test_run_test.py

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (244 lines) | stat: -rwxr-xr-x 7,954 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env vpython3
# Copyright 2017 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Tests for local_device_instrumentation_test_run."""

# pylint: disable=protected-access


import unittest

import random
import os
import sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__),
    '../../..')))

from pylib.base import base_test_result
from pylib.base import mock_environment
from pylib.base import mock_test_instance
from pylib.local.device import local_device_instrumentation_test_run


class LocalDeviceInstrumentationTestRunTest(unittest.TestCase):

  def setUp(self):
    super().setUp()
    self._env = mock_environment.MockEnvironment()
    self._ti = mock_test_instance.MockTestInstance()
    self._obj = (
        local_device_instrumentation_test_run.LocalDeviceInstrumentationTestRun(
            self._env, self._ti))

  # TODO(crbug.com/41361955): Decide whether the _ShouldRetry hook is worth
  # retaining and remove these tests if not.

  def testShouldRetry_failure(self):
    test = {
        'annotations': {},
        'class': 'SadTest',
        'method': 'testFailure',
    }
    result = base_test_result.BaseTestResult(
        'SadTest.testFailure', base_test_result.ResultType.FAIL)
    self.assertTrue(self._obj._ShouldRetry(test, result))

  def testShouldRetry_retryOnFailure(self):
    test = {
        'annotations': {'RetryOnFailure': None},
        'class': 'SadTest',
        'method': 'testRetryOnFailure',
    }
    result = base_test_result.BaseTestResult(
        'SadTest.testRetryOnFailure', base_test_result.ResultType.FAIL)
    self.assertTrue(self._obj._ShouldRetry(test, result))

  def testShouldRetry_notRun(self):
    test = {
        'annotations': {},
        'class': 'SadTest',
        'method': 'testNotRun',
    }
    result = base_test_result.BaseTestResult(
        'SadTest.testNotRun', base_test_result.ResultType.NOTRUN)
    self.assertTrue(self._obj._ShouldRetry(test, result))

  def testIsWPRRecordReplayTest_matchedWithKey(self):
    test = {
        'annotations': {
            'Feature': {
                'value': ['WPRRecordReplayTest', 'dummy']
            }
        },
        'class': 'WPRDummyTest',
        'method': 'testRun',
    }
    self.assertTrue(
        local_device_instrumentation_test_run._IsWPRRecordReplayTest(test))

  def testIsWPRRecordReplayTest_noMatchedKey(self):
    test = {
        'annotations': {
            'Feature': {
                'value': ['abc', 'dummy']
            }
        },
        'class': 'WPRDummyTest',
        'method': 'testRun',
    }
    self.assertFalse(
        local_device_instrumentation_test_run._IsWPRRecordReplayTest(test))

  def testGetWPRArchivePath_matchedWithKey(self):
    test = {
        'annotations': {
            'WPRArchiveDirectory': {
                'value': 'abc'
            }
        },
        'class': 'WPRDummyTest',
        'method': 'testRun',
    }
    self.assertEqual(
        local_device_instrumentation_test_run._GetWPRArchivePath(test), 'abc')

  def testGetWPRArchivePath_noMatchedWithKey(self):
    test = {
        'annotations': {
            'Feature': {
                'value': 'abc'
            }
        },
        'class': 'WPRDummyTest',
        'method': 'testRun',
    }
    self.assertFalse(
        local_device_instrumentation_test_run._GetWPRArchivePath(test))

  def testIsRenderTest_matchedWithKey(self):
    test = {
        'annotations': {
            'Feature': {
                'value': ['RenderTest', 'dummy']
            }
        },
        'class': 'DummyTest',
        'method': 'testRun',
    }
    self.assertTrue(local_device_instrumentation_test_run._IsRenderTest(test))

  def testIsRenderTest_noMatchedKey(self):
    test = {
        'annotations': {
            'Feature': {
                'value': ['abc', 'dummy']
            }
        },
        'class': 'DummyTest',
        'method': 'testRun',
    }
    self.assertFalse(local_device_instrumentation_test_run._IsRenderTest(test))

  def testReplaceUncommonChars(self):
    original = 'abc#edf'
    self.assertEqual(
        local_device_instrumentation_test_run._ReplaceUncommonChars(original),
        'abc__edf')
    original = 'abc#edf#hhf'
    self.assertEqual(
        local_device_instrumentation_test_run._ReplaceUncommonChars(original),
        'abc__edf__hhf')
    original = 'abcedfhhf'
    self.assertEqual(
        local_device_instrumentation_test_run._ReplaceUncommonChars(original),
        'abcedfhhf')
    original = None
    with self.assertRaises(ValueError):
      local_device_instrumentation_test_run._ReplaceUncommonChars(original)
    original = ''
    with self.assertRaises(ValueError):
      local_device_instrumentation_test_run._ReplaceUncommonChars(original)

  def test_ApplyExternalSharding(self):
    test1_batch1 = create_test(
        {'Batch': {'value': 'batch1'}}, 'com.example.TestA', 'test1')
    test2 = create_test(
        {'Features$EnableFeatures': {'value': 'defg'}},
        'com.example.TestB', 'test2')
    test3 = create_test({}, 'com.example.TestC', 'test3')
    test3_multiprocess = create_test(
        {}, 'com.example.TestC', 'test3__multiprocess_mode')
    test4_batch1 = create_test(
        {'Batch': {'value': 'batch1'}}, 'com.example.TestD', 'test4')
    test5_batch1 = create_test(
        {'Batch': {'value': 'batch1'}}, 'com.example.TestE', 'test5')
    test6 = create_test({}, 'com.example.TestF', 'test6')
    test7 = create_test({}, 'com.example.TestG', 'test7')
    test8 = create_test({}, 'com.example.TestH', 'test8')

    tests = [
        test1_batch1, test2, test3, test3_multiprocess, test4_batch1,
        test5_batch1, test6, test7, test8
    ]
    expected_shard0 = [test8]
    expected_shard1 = [
        [test1_batch1, test4_batch1, test5_batch1],
        test3_multiprocess,
        test7,
        test3,
        test6,
        test2,
    ]
    # Shuffle the tests two times to check if the output is deterministic.
    random.shuffle(tests)
    self.assertListEqual(self._obj._ApplyExternalSharding(tests, 0, 2),
                         expected_shard0)
    self.assertListEqual(self._obj._ApplyExternalSharding(tests, 1, 2),
                         expected_shard1)
    random.shuffle(tests)
    self.assertListEqual(self._obj._ApplyExternalSharding(tests, 0, 2),
                         expected_shard0)
    self.assertListEqual(self._obj._ApplyExternalSharding(tests, 1, 2),
                         expected_shard1)

  def test_GetTestsToRetry(self):
    test1_batch1 = create_test(
        {'Batch': {'value': 'batch1'}}, 'com.example.TestA', 'test1')
    test2_batch1 = create_test(
        {'Batch': {'value': 'batch1'}}, 'com.example.TestB', 'test2')
    test3 = create_test({}, 'com.example.TestC', 'test3')
    test4 = create_test({}, 'com.example.TestD', 'test4')
    test_data = [
        (test1_batch1, base_test_result.ResultType.PASS),
        (test2_batch1, base_test_result.ResultType.FAIL),
        (test3, base_test_result.ResultType.PASS),
        (test4, base_test_result.ResultType.FAIL),
    ]
    all_tests = [[test1_batch1, test2_batch1], test3, test4]
    try_results = base_test_result.TestRunResults()
    for test, test_result in test_data:
      try_results.AddResult(
          base_test_result.BaseTestResult(self._obj._GetUniqueTestName(test),
                                          test_result))
    actual_retry = self._obj._GetTestsToRetry(all_tests, try_results)
    expected_retry = [
        [test2_batch1],
        test4,
    ]
    self.assertListEqual(actual_retry, expected_retry)


def create_test(annotation_dict, class_name, method_name):
  # Helper function to generate test dict
  test = {
        'annotations': annotation_dict,
        'class': class_name,
        'method': method_name
  }
  return test


if __name__ == '__main__':
  unittest.main(verbosity=2)