blob: 248786e9b4eafaa687db9a602afbda1f1e02d804 [file] [log] [blame]
Dan Shi45c9c682018-08-20 16:13:10 -07001#!/usr/bin/python
2# -*- coding:utf-8 -*-
3# Copyright 2018 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import os
18import shutil
19import tempfile
20import unittest
21
22import android_test_mapping_format
23
24
Dan Shica7136c2019-01-07 15:41:05 -080025VALID_TEST_MAPPING = r"""
Dan Shi45c9c682018-08-20 16:13:10 -070026{
27 "presubmit": [
28 {
29 "name": "CtsWindowManagerDeviceTestCases",
30 "options": [
31 {
32 "include-annotation": "android.platform.test.annotations.Presubmit"
33 }
34 ]
35 }
36 ],
37 "postsubmit": [
38 {
Dan Shiea3ae032018-11-27 16:54:43 -080039 "name": "CtsWindowManagerDeviceTestCases",
Dan Shi6dccf742018-12-10 15:08:56 -080040 "host": true,
Dan Shica7136c2019-01-07 15:41:05 -080041 "preferred_targets": ["a", "b"],
42 "file_patterns": [".*\\.java"]
Dan Shi45c9c682018-08-20 16:13:10 -070043 }
44 ],
45 "imports": [
46 {
47 "path": "frameworks/base/services/core/java/com/android/server/am"
48 },
49 {
50 "path": "frameworks/base/services/core/java/com/android/server/wm"
51 }
52 ]
53}
54"""
55
56BAD_JSON = """
57{wrong format}
58"""
59
60BAD_TEST_WRONG_KEY = """
61{
62 "presubmit": [
63 {
Dan Shi6dccf742018-12-10 15:08:56 -080064 "bad_name": "CtsWindowManagerDeviceTestCases"
Dan Shi45c9c682018-08-20 16:13:10 -070065 }
Dan Shiea3ae032018-11-27 16:54:43 -080066 ]
67}
68"""
69
70BAD_TEST_WRONG_HOST_VALUE = """
71{
72 "presubmit": [
73 {
74 "name": "CtsWindowManagerDeviceTestCases",
75 "host": "bad_value"
76 }
77 ]
Dan Shi45c9c682018-08-20 16:13:10 -070078}
79"""
80
Dan Shi6dccf742018-12-10 15:08:56 -080081
82BAD_TEST_WRONG_PREFERRED_TARGETS_VALUE_NONE_LIST = """
83{
84 "presubmit": [
85 {
86 "name": "CtsWindowManagerDeviceTestCases",
87 "preferred_targets": "bad_value"
88 }
89 ]
90}
91"""
92
93BAD_TEST_WRONG_PREFERRED_TARGETS_VALUE_WRONG_TYPE = """
94{
95 "presubmit": [
96 {
97 "name": "CtsWindowManagerDeviceTestCases",
98 "preferred_targets": ["bad_value", 123]
99 }
100 ]
101}
102"""
103
Dan Shi45c9c682018-08-20 16:13:10 -0700104BAD_TEST_WRONG_OPTION = """
105{
106 "presubmit": [
107 {
108 "name": "CtsWindowManagerDeviceTestCases",
109 "options": [
110 {
111 "include-annotation": "android.platform.test.annotations.Presubmit",
112 "bad_option": "some_name"
113 }
114 ]
115 }
Dan Shi6dccf742018-12-10 15:08:56 -0800116 ]
Dan Shi45c9c682018-08-20 16:13:10 -0700117}
118"""
119
120BAD_IMPORT_WRONG_KEY = """
121{
122 "imports": [
123 {
124 "name": "frameworks/base/services/core/java/com/android/server/am"
125 }
126 ]
127}
128"""
129
130BAD_IMPORT_WRONG_IMPORT_VALUE = """
131{
132 "imports": [
133 {
134 "path": "frameworks/base/services/core/java/com/android/server/am",
135 "option": "something"
136 }
137 ]
138}
139"""
140
Dan Shica7136c2019-01-07 15:41:05 -0800141BAD_FILE_PATTERNS = """
142{
143 "presubmit": [
144 {
145 "name": "CtsWindowManagerDeviceTestCases",
146 "file_patterns": ["pattern", 123]
147 }
148 ]
149}
150"""
151
Dan Shi45c9c682018-08-20 16:13:10 -0700152
153class AndroidTestMappingFormatTests(unittest.TestCase):
154 """Unittest for android_test_mapping_format module."""
155
156 def setUp(self):
157 self.tempdir = tempfile.mkdtemp()
158 self.test_mapping_file = os.path.join(self.tempdir, 'TEST_MAPPING')
159
160 def tearDown(self):
161 shutil.rmtree(self.tempdir)
162
163 def test_valid_test_mapping(self):
164 """Verify that the check doesn't raise any error for valid test mapping.
165 """
166 with open(self.test_mapping_file, 'w') as f:
167 f.write(VALID_TEST_MAPPING)
168 android_test_mapping_format.process_file(self.test_mapping_file)
169
Dan Shi6dccf742018-12-10 15:08:56 -0800170 def test_invalid_test_mapping_bad_json(self):
171 """Verify that TEST_MAPPING file with bad json can be detected."""
172 with open(self.test_mapping_file, 'w') as f:
173 f.write(BAD_JSON)
174 self.assertRaises(
175 ValueError, android_test_mapping_format.process_file,
176 self.test_mapping_file)
177
Dan Shi45c9c682018-08-20 16:13:10 -0700178 def test_invalid_test_mapping_wrong_test_key(self):
179 """Verify that test config using wrong key can be detected."""
180 with open(self.test_mapping_file, 'w') as f:
181 f.write(BAD_TEST_WRONG_KEY)
182 self.assertRaises(
183 android_test_mapping_format.InvalidTestMappingError,
184 android_test_mapping_format.process_file,
185 self.test_mapping_file)
186
Dan Shiea3ae032018-11-27 16:54:43 -0800187 def test_invalid_test_mapping_wrong_test_value(self):
188 """Verify that test config using wrong host value can be detected."""
189 with open(self.test_mapping_file, 'w') as f:
190 f.write(BAD_TEST_WRONG_HOST_VALUE)
191 self.assertRaises(
192 android_test_mapping_format.InvalidTestMappingError,
193 android_test_mapping_format.process_file,
194 self.test_mapping_file)
195
Dan Shi6dccf742018-12-10 15:08:56 -0800196 def test_invalid_test_mapping_wrong_preferred_targets_value(self):
197 """Verify invalid preferred_targets are rejected."""
198 with open(self.test_mapping_file, 'w') as f:
199 f.write(BAD_TEST_WRONG_PREFERRED_TARGETS_VALUE_NONE_LIST)
200 self.assertRaises(
201 android_test_mapping_format.InvalidTestMappingError,
202 android_test_mapping_format.process_file,
203 self.test_mapping_file)
204 with open(self.test_mapping_file, 'w') as f:
205 f.write(BAD_TEST_WRONG_PREFERRED_TARGETS_VALUE_WRONG_TYPE)
206 self.assertRaises(
207 android_test_mapping_format.InvalidTestMappingError,
208 android_test_mapping_format.process_file,
209 self.test_mapping_file)
210
Dan Shi45c9c682018-08-20 16:13:10 -0700211 def test_invalid_test_mapping_wrong_test_option(self):
212 """Verify that test config using wrong option can be detected."""
213 with open(self.test_mapping_file, 'w') as f:
214 f.write(BAD_TEST_WRONG_OPTION)
215 self.assertRaises(
216 android_test_mapping_format.InvalidTestMappingError,
217 android_test_mapping_format.process_file,
218 self.test_mapping_file)
219
220 def test_invalid_test_mapping_wrong_import_key(self):
221 """Verify that import setting using wrong key can be detected."""
222 with open(self.test_mapping_file, 'w') as f:
223 f.write(BAD_IMPORT_WRONG_KEY)
224 self.assertRaises(
225 android_test_mapping_format.InvalidTestMappingError,
226 android_test_mapping_format.process_file,
227 self.test_mapping_file)
228
229 def test_invalid_test_mapping_wrong_import_value(self):
230 """Verify that import setting using wrong value can be detected."""
231 with open(self.test_mapping_file, 'w') as f:
232 f.write(BAD_IMPORT_WRONG_IMPORT_VALUE)
233 self.assertRaises(
234 android_test_mapping_format.InvalidTestMappingError,
235 android_test_mapping_format.process_file,
236 self.test_mapping_file)
237
Dan Shica7136c2019-01-07 15:41:05 -0800238 def test_invalid_test_mapping_file_patterns_value(self):
239 """Verify that file_patterns using wrong value can be detected."""
240 with open(self.test_mapping_file, 'w') as f:
241 f.write(BAD_FILE_PATTERNS)
242 self.assertRaises(
243 android_test_mapping_format.InvalidTestMappingError,
244 android_test_mapping_format.process_file,
245 self.test_mapping_file)
246
Dan Shi45c9c682018-08-20 16:13:10 -0700247
248if __name__ == '__main__':
249 unittest.main()