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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2021 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 model.py."""
# TODO(crbug.com/1148168): Set up these tests to run on the tryjobs.
import model
import unittest
from model import Model
class ModelTest(unittest.TestCase):
"""Tests for model.py"""
def assert_project(self, project, name, id_, summary, owners,
key_rotation_period):
self.assertEqual(project.name, name)
self.assertEqual(project.id, id_)
self.assertEqual(project.summary.strip(), summary)
self.assertEqual(len(project.owners), len(owners))
for actual, expected in zip(project.owners, owners):
self.assertEqual(actual, expected)
self.assertEqual(int(project.key_rotation_period), key_rotation_period)
def assert_event(self, event, name, summary):
self.assertEqual(event.name, name)
self.assertEqual(event.summary.strip(), summary)
def assert_metric(self, metric, name, type_, summary):
self.assertEqual(metric.name, name)
self.assertEqual(metric.type, type_)
self.assertEqual(metric.summary.strip(), summary)
def assert_model_raises(self, xml):
raised = False
try:
Model(xml)
except ValueError:
raised = True
self.assertTrue(raised)
def test_valid_xml(self):
xml = """\
<structured-metrics>
<project name="ProjectOne">
<owner>test1@chromium.org</owner>
<owner>test2@chromium.org</owner>
<id>none</id>
<scope>profile</scope>
<summary> Test project. </summary>
<key-rotation>65</key-rotation>
<event name="EventOne">
<summary> Test event. </summary>
<metric name="MetricOne" type="int">
<summary> Test metric. </summary>
</metric>
<metric name="MetricTwo" type="hmac-string">
<summary> Test metric. </summary>
</metric>
</event>
<event name="EventTwo">
<summary> Test event. </summary>
<metric name="MetricThree" type="int">
<summary> Test metric. </summary>
</metric>
</event>
</project>
<project name="ProjectTwo">
<owner>test@chromium.org</owner>
<id>uma</id>
<scope>device</scope>
<summary> Test project. </summary>
<event name="EventThree">
<summary> Test event. </summary>
<metric name="MetricFour" type="int">
<summary> Test metric. </summary>
</metric>
</event>
</project>
</structured-metrics>"""
data = Model(xml)
self.assertEqual(len(data.projects), 2)
project_one, project_two = data.projects
self.assert_project(project_one, 'ProjectOne', 'none', 'Test project.',
('test1@chromium.org', 'test2@chromium.org'), 65)
self.assert_project(project_two, 'ProjectTwo', 'uma', 'Test project.',
('test@chromium.org', ),
model.DEFAULT_KEY_ROTATION_PERIOD)
self.assertEqual(len(project_one.events), 2)
self.assertEqual(len(project_two.events), 1)
event_one, event_two = project_one.events
event_three, = project_two.events
self.assert_event(event_one, 'EventOne', 'Test event.')
self.assert_event(event_two, 'EventTwo', 'Test event.')
self.assert_event(event_three, 'EventThree', 'Test event.')
self.assertEqual(len(event_one.metrics), 2)
self.assertEqual(len(event_two.metrics), 1)
self.assertEqual(len(event_three.metrics), 1)
metric_one, metric_two = event_one.metrics
metric_three, = event_two.metrics
metric_four, = event_three.metrics
self.assert_metric(metric_one, 'MetricOne', 'int', 'Test metric.')
self.assert_metric(metric_two, 'MetricTwo', 'hmac-string', 'Test metric.')
self.assert_metric(metric_three, 'MetricThree', 'int', 'Test metric.')
self.assert_metric(metric_four, 'MetricFour', 'int', 'Test metric.')
def test_owners_validation(self):
# No owner for project.
self.assert_model_raises("""\
<structured-metrics>
<project name="project">
<id>uma</id>
<summary> Test project. </summary>
<event name="EventThree">
<summary> Test event. </summary>
<metric name="MetricFour" type="int">
<summary> Test metric. </summary>
</metric>
</event>
</project>
</structured-metrics>""")
# Owner is username not email.
self.assert_model_raises("""\
<structured-metrics>
<project name="project">
<owner>test@</owner>
<id>uma</id>
<summary> Test project. </summary>
<event name="EventThree">
<summary> Test event. </summary>
<metric name="MetricFour" type="int">
<summary> Test metric. </summary>
</metric>
</event>
</project>
</structured-metrics>""")
def test_id_validation(self):
# Missing ID
self.assert_model_raises("""\
<structured-metrics>
<project name="MyProject">
<owner>test@chromium.org</owner>
<summary> Test project. </summary>
<event name="MyEvent">
<summary> Test event. </summary>
<metric name="MyMetric" type="int">
<summary> Test metric. </summary>
</metric>
</event>
</project>
</structured-metrics>""")
# Invalid ID
self.assert_model_raises("""\
<structured-metrics>
<project name="MyProject">
<owner>test@chromium.org</owner>
<id>invalid value</id>
<summary> Test project. </summary>
<event name="MyEvent">
<summary> Test event. </summary>
<metric name="MyMetric" type="int">
<summary> Test metric. </summary>
</metric>
</event>
</project>
</structured-metrics>""")
def test_type_validation(self):
# Missing type
self.assert_model_raises("""\
<structured-metrics>
<project name="MyProject">
<owner>test@chromium.org</owner>
<id>none</id>
<summary> Test project. </summary>
<event name="MyEvent">
<summary> Test event. </summary>
<metric name="MyMetric">
<summary> Test metric. </summary>
</metric>
</event>
</project>
</structured-metrics>""")
# Invalid type
self.assert_model_raises("""\
<structured-metrics>
<project name="MyProject">
<owner>test@chromium.org</owner>
<id>none</id>
<summary> Test project. </summary>
<event name="MyEvent">
<summary> Test event. </summary>
<metric name="MyMetric" type="invalid value">
<summary> Test metric. </summary>
</metric>
</event>
</project>
</structured-metrics>""")
def test_duplicate_summaries(self):
self.assert_model_raises("""\
<structured-metrics>
<project name="MyProject">
<owner>test@chromium.org</owner>
<id>none</id>
<summary> Test project. </summary>
<summary> Test project. </summary>
<event name="MyEvent">
<summary> Test event. </summary>
<metric name="MyMetric" type="int">
<summary> Test metric. </summary>
</metric>
</event>
</project>
</structured-metrics>""")
def test_duplicate_project_names(self):
# Two projects with name "Duplicate"
self.assert_model_raises("""\
<structured-metrics>
<project name="Duplicate">
<owner>test@</owner>
<id>uma</id>
<summary> Test project. </summary>
<event name="MyEvent">
<summary> Test event. </summary>
<metric name="MyMetric" type="int">
<summary> Test metric. </summary>
</metric>
</event>
</project>
<project name="Duplicate">
<owner>test@</owner>
<id>uma</id>
<summary> Test project. </summary>
<event name="MyEvent">
<summary> Test event. </summary>
<metric name="MyMetric" type="int">
<summary> Test metric. </summary>
</metric>
</event>
</project>
</structured-metrics>""")
def test_duplicate_event_names(self):
# Two events with name "Duplicate"
self.assert_model_raises("""\
<structured-metrics>
<project name="MyProject">
<owner>test@</owner>
<id>uma</id>
<summary> Test project. </summary>
<event name="Duplicate">
<summary> Test event. </summary>
<metric name="MyMetric" type="int">
<summary> Test metric. </summary>
</metric>
</event>
<event name="Duplicate">
<summary> Test event. </summary>
<metric name="MyMetric" type="int">
<summary> Test metric. </summary>
</metric>
</event>
</project>
</structured-metrics>""")
def test_duplicate_metric_names(self):
# Two metrics with name "Duplicate"
self.assert_model_raises("""\
<structured-metrics>
<project name="MyProject">
<owner>test@</owner>
<id>uma</id>
<summary> Test project. </summary>
<event name="MyEvent">
<summary> Test event. </summary>
<metric name="Duplicate" type="int">
<summary> Test metric. </summary>
</metric>
<metric name="Duplicate" type="int">
<summary> Test metric. </summary>
</metric>
</event>
</project>
</structured-metrics>""")
def test_key_rotation_validation(self):
# Key rotation not a number.
self.assert_model_raises("""\
<structured-metrics>
<project name="project">
<id>uma</id>
<summary> Test project. </summary>
<owner>test@chromium.org</owner>
<key-rotation>NaN123</key-rotation>
<event name="EventThree">
<summary> Test event. </summary>
<metric name="MetricFour" type="int">
<summary> Test metric. </summary>
</metric>
</event>
</project>
</structured-metrics>""")
if __name__ == '__main__':
unittest.main()
|