File: histogram_configuration_model_test_histograms.py

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (594 lines) | stat: -rw-r--r-- 16,772 bytes parent folder | download | duplicates (9)
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# Copyright 2020 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os
from parameterized import parameterized
import sys
import unittest

sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common'))
import etree_util

import histogram_configuration_model

XML_RIGHT_ORDER = """
<histogram-configuration>

<!-- Histogram definitions -->

<histograms>

<histogram name="hist.a" enum="enum1" expires_after="2019-11-02">
  <owner>owner1@chromium.org</owner>
  <owner>owner2@chromium.org</owner>
  <summary>Summary text</summary>
</histogram>

<histogram name="hist.b" expires_after="M85">
  <owner>owner1@chromium.org</owner>
  <owner>owner2@chromium.org</owner>
  <summary>Summary text</summary>
</histogram>

<histogram name="hist.c" enum="enum3" expires_after="never">
  <owner>owner1@chromium.org</owner>
  <owner>owner2@chromium.org</owner>
  <summary>Summary text</summary>
</histogram>

</histograms>

<histogram_suffixes_list>

<histogram_suffixes name="suffix1" separator="." ordering="prefix">
  <suffix base="true" name="suffix_name" label="label"/>
  <affected-histogram name="histogram1"/>
  <affected-histogram name="histogram2"/>
  <affected-histogram name="histogram3"/>
</histogram_suffixes>

<histogram_suffixes name="suffix2" separator="_" ordering="prefix,2">
  <suffix name="suffix_name" label="label"/>
  <affected-histogram name="histogram1"/>
  <affected-histogram name="histogram2"/>
  <affected-histogram name="histogram3"/>
</histogram_suffixes>

</histogram_suffixes_list>

</histogram-configuration>
""".strip()

PRETTY_XML = """
<histogram-configuration>

<!-- Histogram definitions -->

<histograms>

<histogram base="true" name="hist.a" expires_after="2019-11-02">
<!-- Comment in histogram -->

  <owner>owner1@chromium.org</owner>
  <owner>owner2@chromium.org</owner>
  <component>Component&gt;Subcomponent</component>
  <summary>Summary text</summary>
</histogram>

</histograms>

<histogram_suffixes_list>

<histogram_suffixes name="suffix1" separator="." ordering="prefix">
<!-- Comment in histogram_suffixes -->

  <suffix base="true" name="suffix_name" label="label"/>
  <suffix name="suffix_name" label="label"/>
  <affected-histogram name="histogram1"/>
</histogram_suffixes>

</histogram_suffixes_list>

</histogram-configuration>
""".strip()

XML_WRONG_ATTRIBUTE_ORDER = """
<histogram-configuration>

<!-- Histogram definitions -->

<histograms>

<histogram expires_after="2019-11-02" name="hist.a" base="true" >
<!-- Comment in histogram -->

  <owner>owner1@chromium.org</owner>
  <owner>owner2@chromium.org</owner>
  <component>Component&gt;Subcomponent</component>
  <summary>Summary text</summary>
</histogram>

</histograms>

<histogram_suffixes_list>

<histogram_suffixes name="suffix1" separator="." ordering="prefix">
<!-- Comment in histogram_suffixes -->

  <suffix name="suffix_name" base="true" label="label"/>
  <suffix label="label" name="suffix_name"/>
  <affected-histogram name="histogram1"/>
</histogram_suffixes>

</histogram_suffixes_list>

</histogram-configuration>
""".strip()

XML_MISSING_SEPARATOR = """
<histogram-configuration>

<!-- Histogram definitions -->

<histograms>

<histogram base="true" name="hist.a" expires_after="2019-11-02">
<!-- Comment in histogram -->

  <owner>owner1@chromium.org</owner>
  <owner>owner2@chromium.org</owner>
  <summary>Summary text</summary>
</histogram>

</histograms>

<histogram_suffixes_list>

<histogram_suffixes name="suffix1" ordering="prefix">
<!-- Comment in histogram_suffixes -->

  <suffix base="true" name="suffix_name" label="label"/>
  <suffix name="suffix_name" label="label"/>
  <affected-histogram name="histogram1"/>
</histogram_suffixes>

</histogram_suffixes_list>

</histogram-configuration>
""".strip()

XML_WRONG_INDENT = """
<histogram-configuration>

<!-- Histogram definitions -->

  <histograms>

  <histogram base="true" name="hist.a" expires_after="2019-11-02">
  <!-- Comment in histogram -->

      <owner>owner1@chromium.org</owner>
      <owner>owner2@chromium.org</owner>
      <component>Component&gt;Subcomponent</component>
    <summary>Summary text</summary>
  </histogram>

  </histograms>

  <histogram_suffixes_list>

  <histogram_suffixes name="suffix1" separator="." ordering="prefix">
  <!-- Comment in histogram_suffixes -->

      <suffix base="true" name="suffix_name" label="label"/>
      <suffix name="suffix_name" label="label"/>
      <affected-histogram name="histogram1"/>
  </histogram_suffixes>

  </histogram_suffixes_list>

</histogram-configuration>
""".strip()

XML_WRONG_SINGLELINE = """
<histogram-configuration>

<!-- Histogram definitions -->

<histograms>

<histogram base="true" name="hist.a" expires_after="2019-11-02">
<!-- Comment in histogram -->

  <owner>
    owner1@chromium.org
  </owner>
  <owner>owner2@chromium.org</owner>
  <component>
    Component&gt;Subcomponent
  </component>
  <summary>
    Summary text
  </summary>
</histogram>

</histograms>

<histogram_suffixes_list>

<histogram_suffixes name="suffix1" separator="." ordering="prefix">
<!-- Comment in histogram_suffixes -->

  <suffix base="true" name="suffix_name" label="label"/>
  <suffix name="suffix_name" label="label"/>
  <affected-histogram name="histogram1"/>
</histogram_suffixes>

</histogram_suffixes_list>

</histogram-configuration>
""".strip()

XML_WRONG_LINEBREAK = """
<histogram-configuration>

<!-- Histogram definitions -->
<histograms>

<histogram base="true" name="hist.a" expires_after="2019-11-02">
<!-- Comment in histogram -->
  <owner>owner1@chromium.org</owner>
  <owner>owner2@chromium.org</owner>
  <component>Component&gt;Subcomponent</component>
  <summary>Summary text</summary>

</histogram>
</histograms>

<histogram_suffixes_list>
<histogram_suffixes name="suffix1" separator="." ordering="prefix">
<!-- Comment in histogram_suffixes -->
  <suffix base="true" name="suffix_name" label="label"/>
  <suffix name="suffix_name" label="label"/>
  <affected-histogram name="histogram1"/>
</histogram_suffixes>
</histogram_suffixes_list>


</histogram-configuration>
""".strip()

XML_WRONG_CHILDREN_ORDER = """
<histogram-configuration>

<!-- Histogram definitions -->

<histograms>

<histogram base="true" name="hist.a" expires_after="2019-11-02">
<!-- Comment in histogram -->

  <owner>owner1@chromium.org</owner>
  <summary>Summary text</summary>
  <component>Component&gt;Subcomponent</component>
  <owner>owner2@chromium.org</owner>
</histogram>

</histograms>

<histogram_suffixes_list>

<histogram_suffixes name="suffix1" separator="." ordering="prefix">
<!-- Comment in histogram_suffixes -->

  <suffix base="true" name="suffix_name" label="label"/>
  <suffix name="suffix_name" label="label"/>
  <affected-histogram name="histogram1"/>
</histogram_suffixes>

</histogram_suffixes_list>

</histogram-configuration>
""".strip()

XML_WRONG_ORDER = """
<histogram-configuration>

<!-- Histogram definitions -->

<histograms>

<histogram name="hist.c" enum="enum3" expires_after="never">
  <owner>owner1@chromium.org</owner>
  <owner>owner2@chromium.org</owner>
  <summary>Summary text</summary>
</histogram>

<histogram name="hist.a" enum="enum1" expires_after="2019-11-02">
  <owner>owner1@chromium.org</owner>
  <owner>owner2@chromium.org</owner>
  <summary>Summary text</summary>
</histogram>

<histogram name="hist.b" expires_after="M85">
  <owner>owner1@chromium.org</owner>
  <owner>owner2@chromium.org</owner>
  <summary>Summary text</summary>
</histogram>

</histograms>

<histogram_suffixes_list>

<histogram_suffixes name="suffix2" separator="_" ordering="prefix,2">
  <suffix name="suffix_name" label="label"/>
  <affected-histogram name="histogram1"/>
  <affected-histogram name="histogram2"/>
  <affected-histogram name="histogram3"/>
</histogram_suffixes>

<histogram_suffixes name="suffix1" separator="." ordering="prefix">
  <suffix base="true" name="suffix_name" label="label"/>
  <affected-histogram name="histogram1"/>
  <affected-histogram name="histogram2"/>
  <affected-histogram name="histogram3"/>
</histogram_suffixes>

</histogram_suffixes_list>

</histogram-configuration>
""".strip()

PRETTY_XML_WITH_TOKEN = """
<histogram-configuration>

<!-- Histogram definitions -->

<histograms>

<variants name="OmniboxProviderVersion">
  <variant name="" summary="all versions"/>
  <variant name=".Provider2" summary="the second version"/>
</variants>

<histogram name="Omnibox{version}{content}.Time" units="ms"
    expires_after="2020-12-25">
  <owner>me@google.com</owner>
  <summary>
    The length of time taken by {version} of {content} provider's synchronous
    pass.
  </summary>
  <token key="version" variants="OmniboxProviderVersion"/>
  <token key="content">
    <variant name=".ExtensionApp" summary="ExtensionApp">
      <owner>you@google.com</owner>
    </variant>
    <variant name=".HistoryContents" summary="HistoryContents"/>
    <variant name=".HistoryQuick" summary="HistoryQuick"/>
  </token>
</histogram>

</histograms>

</histogram-configuration>
""".strip()

XML_WRONG_VARIANT_CHILDREN_ORDER = """
<histogram-configuration>

<!-- Histogram definitions -->

<histograms>

<variants name="OmniboxProviderVersion">
  <variant name="" summary="all versions"/>
  <variant name=".Provider2" summary="the second version"/>
</variants>

<histogram name="Omnibox{version}{content}.Time" units="ms"
    expires_after="2020-12-25">
  <owner>me@google.com</owner>
  <summary>
    The length of time taken by {version} of {content} provider's synchronous
    pass.
  </summary>
  <token key="version" variants="OmniboxProviderVersion"/>
  <token key="content">
    <variant name=".ExtensionApp" summary="ExtensionApp">
      <owner>you@google.com</owner>
    </variant>
    <variant name=".HistoryContents" summary="HistoryContents"/>
    <variant name=".HistoryQuick" summary="HistoryQuick"/>
  </token>
</histogram>

</histograms>

</histogram-configuration>
""".strip()

XML_WRONG_VARIANT_ORDER = """
<histogram-configuration>

<!-- Histogram definitions -->

<histograms>

<variants name="OmniboxProviderVersion">
  <variant name="" summary="all versions"/>
  <variant name=".Provider2" summary="the second version"/>
</variants>

<histogram name="Omnibox{version}{content}.Time" units="ms"
    expires_after="2020-12-25">
  <owner>me@google.com</owner>
  <summary>
    The length of time taken by {version} of {content} provider's synchronous
    pass.
  </summary>
  <token key="version" variants="OmniboxProviderVersion"/>
  <token key="content">
    <variant name=".ExtensionApp" summary="ExtensionApp">
      <owner>you@google.com</owner>
    </variant>
    <variant name=".HistoryQuick" summary="HistoryQuick"/>
    <variant name=".HistoryContents" summary="HistoryContents"/>
  </token>
</histogram>

</histograms>

</histogram-configuration>
""".strip()

XML_WRONG_HISTOGRAM_VARIANTS_ORDER = """
<histogram-configuration>

<!-- Histogram definitions -->

<histograms>

<histogram name="Omnibox{version}{content}.Time" units="ms"
    expires_after="2020-12-25">
  <owner>me@google.com</owner>
  <summary>
    The length of time taken by {version} of {content} provider's synchronous
    pass.
  </summary>
  <token key="version" variants="OmniboxProviderVersion"/>
  <token key="content">
    <variant name=".ExtensionApp" summary="ExtensionApp">
      <owner>you@google.com</owner>
    </variant>
    <variant name=".HistoryContents" summary="HistoryContents"/>
    <variant name=".HistoryQuick" summary="HistoryQuick"/>
  </token>
</histogram>

<variants name="OmniboxProviderVersion">
  <variant name="" summary="all versions"/>
  <variant name=".Provider2" summary="the second version"/>
</variants>

</histograms>

</histogram-configuration>
""".strip()


class HistogramXmlTest(unittest.TestCase):
  @parameterized.expand([
      # Test prettify already pretty XML to verify the pretty-printed version
      # is the same.
      ('AlreadyPrettyXml', PRETTY_XML, PRETTY_XML),
      ('AttributeOrder', XML_WRONG_ATTRIBUTE_ORDER, PRETTY_XML),
      ('Indent', XML_WRONG_INDENT, PRETTY_XML),
      ('SingleLine', XML_WRONG_SINGLELINE, PRETTY_XML),
      ('LineBreak', XML_WRONG_LINEBREAK, PRETTY_XML),

      # Test prettify already pretty XML with right order to verify the
      # pretty-printed version is the same.
      ('AlreadyPrettyXmlRightOrder', XML_RIGHT_ORDER, XML_RIGHT_ORDER),
      ('HistogramAndSuffixOrder', XML_WRONG_ORDER, XML_RIGHT_ORDER),
      ('ChildrenOrder', XML_WRONG_CHILDREN_ORDER, PRETTY_XML),
  ])
  def testPrettify(self, _, input_xml, expected_xml):
    result = histogram_configuration_model.PrettifyTree(
        etree_util.ParseXMLString(input_xml))
    self.maxDiff = None
    self.assertMultiLineEqual(expected_xml, result.strip())

  def testMissingRequiredAttribute(self):
    with self.assertRaises(Exception) as context:
      histogram_configuration_model.PrettifyTree(
          etree_util.ParseXMLString(XML_MISSING_SEPARATOR))
    self.assertIn('separator', str(context.exception))
    self.assertIn('Missing attribute', str(context.exception))

  @parameterized.expand([
      # The "base" attribute of <suffix> only allows
      # "true", "True", "false" or "False"
      ('BadSuffixBaseBoolean', XML_RIGHT_ORDER, 'true', 'yes'),

      # The "expires-after" attribute of <histogram> only allows:
      # Date given in the format YYYY-{M, MM}-{D, DD},
      # Milestone given in the format e.g. M81
      # or "never" or "Never"
      ('BadExpiresAfterDate', PRETTY_XML, '2019-11-02', 'Nov 2019'),
      ('BadExpiresAfterDateSeparator', PRETTY_XML, '2019-11-02', '2019/11/02'),
      ('BadExpiresAfterMilestone', XML_RIGHT_ORDER, 'M85', 'Milestone 85'),
      ('BadExpiresAfterNever', XML_RIGHT_ORDER, 'never', 'NEVER'),
      ('BadExpiresAfterOtherIllegalWords', XML_RIGHT_ORDER, 'never', 'hello'),

      # The "enum" attribute of <histogram> only allows alphanumeric characters
      # and punctuations "." and "_". It does not allow space.
      ('BadEnumNameIllegalPunctuation', XML_RIGHT_ORDER, 'enum1', 'enum:1'),
      ('BadEnumNameWithSpace', XML_RIGHT_ORDER, 'enum1', 'enum 1'),

      # The "ordering" attribute of <histogram_suffixes> only allow
      # "suffix", "prefix" or "prefix," followed by a non-negative integer
      ('BadOrderingIllegalPunctuation', XML_RIGHT_ORDER, 'prefix,2', 'prefix-2'
       ),
      ('BadOrderingNonNumber', XML_RIGHT_ORDER, 'prefix,2', 'prefix,two'),
  ])
  def testRegex(self, _, pretty_input_xml, original_string, bad_string):
    BAD_XML = pretty_input_xml.replace(original_string, bad_string)
    with self.assertRaises(ValueError) as context:
      histogram_configuration_model.PrettifyTree(
          etree_util.ParseXMLString(BAD_XML))
    self.assertIn(bad_string, str(context.exception))
    self.assertIn('does not match regex', str(context.exception))

  @parameterized.expand([
      # Test prettify already pretty XML to verify the pretty-printed version
      # is the same.
      ('AlreadyPrettyXml', PRETTY_XML_WITH_TOKEN, PRETTY_XML_WITH_TOKEN),
      ('ChildrenOrder', XML_WRONG_VARIANT_CHILDREN_ORDER,
       PRETTY_XML_WITH_TOKEN),
      ('VariantOrder', XML_WRONG_VARIANT_ORDER, PRETTY_XML_WITH_TOKEN),
      ('HistogramVariantsOrder', XML_WRONG_HISTOGRAM_VARIANTS_ORDER,
       PRETTY_XML_WITH_TOKEN),
  ])
  def testTokenPrettify(self, _, input_xml, expected_xml):
    result = histogram_configuration_model.PrettifyTree(
        etree_util.ParseXMLString(input_xml))
    self.maxDiff = None
    self.assertMultiLineEqual(expected_xml, result.strip())

  def testIndividualTagParsing_improvement(self):
    """Tests that <improvement> has the right format and can be parsed."""

    improvement_tag_good = '<improvement direction="HIGHER_IS_BETTER"/>'
    improvement_tag_bad = ' <improvement>HIGHER_IS_BETTER</improvement>'
    config = """
<histogram-configuration>

<histograms>

<histogram name="Histogram.With.ImprovementTag" expires_after="M100">
  <owner>owner1@chromium.org</owner>
  {improvement_tag}
  <summary>The improvement tag says higher value is good!</summary>
</histogram>

</histograms>

</histogram-configuration>"""

    config_good = config.format(improvement_tag=improvement_tag_good)
    config_bad = config.format(improvement_tag=improvement_tag_bad)

    result = histogram_configuration_model.PrettifyTree(
        etree_util.ParseXMLString(config_good))
    self.maxDiff = None
    self.assertMultiLineEqual(config_good.strip(), result.strip())

    with self.assertRaisesRegex(ValueError,
                                'direction "" does not match regex'):
      histogram_configuration_model.PrettifyTree(
          etree_util.ParseXMLString(config_bad))


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