• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# Copyright 2018 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import os
17import shutil
18import tempfile
19import unittest
20
21import android_test_mapping_format
22
23
24VALID_TEST_MAPPING = r"""
25{
26  "presubmit": [
27    {
28      "name": "CtsWindowManagerDeviceTestCases",
29      "options": [
30        {
31          "include-annotation": "android.platform.test.annotations.Presubmit"
32        }
33      ]
34    }
35  ],
36  "postsubmit": [
37    {
38      "name": "CtsWindowManagerDeviceTestCases",
39      "host": true,
40      "preferred_targets": ["a", "b"],
41      "file_patterns": [".*\\.java"]
42    }
43  ],
44  "imports": [
45    {
46      "path": "frameworks/base/services/core/java/com/android/server/am"
47    },
48    {
49      "path": "frameworks/base/services/core/java/com/android/server/wm"
50    }
51  ]
52}
53"""
54
55BAD_JSON = """
56{wrong format}
57"""
58
59BAD_TEST_WRONG_KEY = """
60{
61  "presubmit": [
62    {
63      "bad_name": "CtsWindowManagerDeviceTestCases"
64    }
65  ]
66}
67"""
68
69BAD_TEST_WRONG_HOST_VALUE = """
70{
71  "presubmit": [
72    {
73      "name": "CtsWindowManagerDeviceTestCases",
74      "host": "bad_value"
75    }
76  ]
77}
78"""
79
80
81BAD_TEST_WRONG_PREFERRED_TARGETS_VALUE_NONE_LIST = """
82{
83  "presubmit": [
84    {
85      "name": "CtsWindowManagerDeviceTestCases",
86      "preferred_targets": "bad_value"
87    }
88  ]
89}
90"""
91
92BAD_TEST_WRONG_PREFERRED_TARGETS_VALUE_WRONG_TYPE = """
93{
94  "presubmit": [
95    {
96      "name": "CtsWindowManagerDeviceTestCases",
97      "preferred_targets": ["bad_value", 123]
98    }
99  ]
100}
101"""
102
103BAD_TEST_WRONG_OPTION = """
104{
105  "presubmit": [
106    {
107      "name": "CtsWindowManagerDeviceTestCases",
108      "options": [
109        {
110          "include-annotation": "android.platform.test.annotations.Presubmit",
111          "bad_option": "some_name"
112        }
113      ]
114    }
115  ]
116}
117"""
118
119BAD_IMPORT_WRONG_KEY = """
120{
121  "imports": [
122    {
123      "name": "frameworks/base/services/core/java/com/android/server/am"
124    }
125  ]
126}
127"""
128
129BAD_IMPORT_WRONG_IMPORT_VALUE = """
130{
131  "imports": [
132    {
133      "path": "frameworks/base/services/core/java/com/android/server/am",
134      "option": "something"
135    }
136  ]
137}
138"""
139
140BAD_FILE_PATTERNS = """
141{
142  "presubmit": [
143    {
144      "name": "CtsWindowManagerDeviceTestCases",
145      "file_patterns": ["pattern", 123]
146    }
147  ]
148}
149"""
150
151TEST_MAPPING_WITH_SUPPORTED_COMMENTS = r"""
152// supported comment
153{
154  // supported comment!@#$%^&*()_
155  "presubmit": [
156    {
157      "name": "CtsWindowManagerDeviceTestCases\"foo//baz",
158      "options": [
159        // supported comment!@#$%^&*()_
160        {
161          "include-annotation": "android.platform.test.annotations.Presubmit"
162        }
163      ]
164    }
165  ],
166  "imports": [
167    {
168      "path": "path1//path2//path3"
169    }
170  ]
171}
172"""
173
174TEST_MAPPING_WITH_NON_SUPPORTED_COMMENTS = """
175{ #non-supported comments
176  // supported comments
177  "presubmit": [#non-supported comments
178    {  // non-supported comments
179      "name": "CtsWindowManagerDeviceTestCases",
180    }
181  ]
182}
183"""
184
185
186class AndroidTestMappingFormatTests(unittest.TestCase):
187    """Unittest for android_test_mapping_format module."""
188
189    def setUp(self):
190        self.tempdir = tempfile.mkdtemp()
191        self.test_mapping_file = os.path.join(self.tempdir, 'TEST_MAPPING')
192
193    def tearDown(self):
194        shutil.rmtree(self.tempdir)
195
196    def test_valid_test_mapping(self):
197        """Verify that the check doesn't raise any error for valid test mapping.
198        """
199        with open(self.test_mapping_file, 'w') as f:
200            f.write(VALID_TEST_MAPPING)
201        with open(self.test_mapping_file, 'r') as f:
202            android_test_mapping_format.process_file(f.read())
203
204    def test_invalid_test_mapping_bad_json(self):
205        """Verify that TEST_MAPPING file with bad json can be detected."""
206        with open(self.test_mapping_file, 'w') as f:
207            f.write(BAD_JSON)
208        with open(self.test_mapping_file, 'r') as f:
209            self.assertRaises(
210                ValueError, android_test_mapping_format.process_file,
211                f.read())
212
213    def test_invalid_test_mapping_wrong_test_key(self):
214        """Verify that test config using wrong key can be detected."""
215        with open(self.test_mapping_file, 'w') as f:
216            f.write(BAD_TEST_WRONG_KEY)
217        with open(self.test_mapping_file, 'r') as f:
218            self.assertRaises(
219                android_test_mapping_format.InvalidTestMappingError,
220                android_test_mapping_format.process_file,
221                f.read())
222
223    def test_invalid_test_mapping_wrong_test_value(self):
224        """Verify that test config using wrong host value can be detected."""
225        with open(self.test_mapping_file, 'w') as f:
226            f.write(BAD_TEST_WRONG_HOST_VALUE)
227        with open(self.test_mapping_file, 'r') as f:
228            self.assertRaises(
229                android_test_mapping_format.InvalidTestMappingError,
230                android_test_mapping_format.process_file,
231                f.read())
232
233    def test_invalid_test_mapping_wrong_preferred_targets_value(self):
234        """Verify invalid preferred_targets are rejected."""
235        with open(self.test_mapping_file, 'w') as f:
236            f.write(BAD_TEST_WRONG_PREFERRED_TARGETS_VALUE_NONE_LIST)
237        with open(self.test_mapping_file, 'r') as f:
238            self.assertRaises(
239                android_test_mapping_format.InvalidTestMappingError,
240                android_test_mapping_format.process_file,
241                f.read())
242        with open(self.test_mapping_file, 'w') as f:
243            f.write(BAD_TEST_WRONG_PREFERRED_TARGETS_VALUE_WRONG_TYPE)
244        with open(self.test_mapping_file, 'r') as f:
245            self.assertRaises(
246                android_test_mapping_format.InvalidTestMappingError,
247                android_test_mapping_format.process_file,
248                f.read())
249
250    def test_invalid_test_mapping_wrong_test_option(self):
251        """Verify that test config using wrong option can be detected."""
252        with open(self.test_mapping_file, 'w') as f:
253            f.write(BAD_TEST_WRONG_OPTION)
254        with open(self.test_mapping_file, 'r') as f:
255            self.assertRaises(
256                android_test_mapping_format.InvalidTestMappingError,
257                android_test_mapping_format.process_file,
258                f.read())
259
260    def test_invalid_test_mapping_wrong_import_key(self):
261        """Verify that import setting using wrong key can be detected."""
262        with open(self.test_mapping_file, 'w') as f:
263            f.write(BAD_IMPORT_WRONG_KEY)
264        with open(self.test_mapping_file, 'r') as f:
265            self.assertRaises(
266                android_test_mapping_format.InvalidTestMappingError,
267                android_test_mapping_format.process_file,
268                f.read())
269
270    def test_invalid_test_mapping_wrong_import_value(self):
271        """Verify that import setting using wrong value can be detected."""
272        with open(self.test_mapping_file, 'w') as f:
273            f.write(BAD_IMPORT_WRONG_IMPORT_VALUE)
274        with open(self.test_mapping_file, 'r') as f:
275            self.assertRaises(
276                android_test_mapping_format.InvalidTestMappingError,
277                android_test_mapping_format.process_file,
278                f.read())
279
280    def test_invalid_test_mapping_file_patterns_value(self):
281        """Verify that file_patterns using wrong value can be detected."""
282        with open(self.test_mapping_file, 'w') as f:
283            f.write(BAD_FILE_PATTERNS)
284        with open(self.test_mapping_file, 'r') as f:
285            self.assertRaises(
286                android_test_mapping_format.InvalidTestMappingError,
287                android_test_mapping_format.process_file,
288                f.read())
289
290    def test_valid_test_mapping_file_with_supported_comments(self):
291        """Verify that '//'-format comment can be filtered."""
292        with open(self.test_mapping_file, 'w') as f:
293            f.write(TEST_MAPPING_WITH_SUPPORTED_COMMENTS)
294        with open(self.test_mapping_file, 'r') as f:
295            android_test_mapping_format.process_file(f.read())
296
297    def test_valid_test_mapping_file_with_non_supported_comments(self):
298        """Verify that non-supported comment can be detected."""
299        with open(self.test_mapping_file, 'w') as f:
300            f.write(TEST_MAPPING_WITH_NON_SUPPORTED_COMMENTS)
301        with open(self.test_mapping_file, 'r') as f:
302            self.assertRaises(
303                ValueError, android_test_mapping_format.process_file,
304                f.read())
305
306
307if __name__ == '__main__':
308    unittest.main()
309