blob: f20ad5f314b89fe6ec564f390aef4c887db172ea [file] [log] [blame]
#!/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()