1# Copyright 2020 The ChromiumOS Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4"""Tests for check_topology.""" 5 6import unittest 7 8from checker.common_checks.check_topology import TopologyConstraintSuite 9 10from chromiumos.config.payload.config_bundle_pb2 import ConfigBundle 11from chromiumos.config.api.component_pb2 import Component 12from chromiumos.config.api.design_pb2 import Design 13from chromiumos.config.api.hardware_topology_pb2 import HardwareTopology 14from chromiumos.config.api.topology_pb2 import (HardwareFeatures, Topology) 15 16 17class CheckIdsTest(unittest.TestCase): 18 """Tests for check_topology.""" 19 20 def setUp(self): 21 """Creates objects to be shared between tests.""" 22 self.screen_1_topology = Topology( 23 id="part1", 24 type=Topology.SCREEN, 25 description={'EN': 'The first type of screen.'}, 26 hardware_feature=HardwareFeatures( 27 screen=HardwareFeatures.Screen( 28 panel_properties=Component.DisplayPanel.Properties( 29 diagonal_milliinch=10)))) 30 31 self.screen_2_topology = Topology( 32 id="part2", 33 type=Topology.SCREEN, 34 description={'EN': 'The second type of screen.'}, 35 hardware_feature=HardwareFeatures( 36 screen=HardwareFeatures.Screen( 37 panel_properties=Component.DisplayPanel.Properties( 38 diagonal_milliinch=20)))) 39 40 self.keyboard_1_topology = Topology( 41 id="part1", 42 type=Topology.KEYBOARD, 43 description={'EN': 'The first type of keyboard.'}, 44 hardware_feature=HardwareFeatures( 45 keyboard=HardwareFeatures.Keyboard( 46 backlight=HardwareFeatures.PRESENT)), 47 ) 48 49 def test_check_topologies_consistent(self): 50 """Tests no assertions thrown when topologies are consistent.""" 51 # Screen topologies differ in id and type between configs. The same keyboard 52 # topology is used by both configs (the entire message, including id and 53 # type match). 54 # 55 # Note that screen_1_topology and keyboard_1_topology share the same id 56 # ("part1"), but are a different type, so do not violate the constraint. 57 project_config = ConfigBundle(design_list=[ 58 Design(configs=[ 59 Design.Config( 60 hardware_topology=HardwareTopology( 61 screen=self.screen_1_topology, 62 keyboard=self.keyboard_1_topology)), 63 Design.Config( 64 hardware_topology=HardwareTopology( 65 screen=self.screen_2_topology, 66 keyboard=self.keyboard_1_topology)) 67 ]) 68 ]) 69 70 TopologyConstraintSuite().run_checks( 71 program_config=None, 72 project_config=project_config, 73 factory_dir=None, 74 ) 75 76 def test_check_topologies_consistent_violated(self): 77 """Tests assertion thrown when topologies are inconsistent.""" 78 # Create a copy of the first screen topology, but change the milliinch 79 # value. Now, a given id and type maps to two different messages. 80 invalid_screen_topology = Topology() 81 invalid_screen_topology.CopyFrom(self.screen_1_topology) 82 invalid_screen = invalid_screen_topology.hardware_feature.screen 83 invalid_screen.panel_properties.diagonal_milliinch = 20 84 85 project_config = ConfigBundle(design_list=[ 86 Design(configs=[ 87 Design.Config( 88 hardware_topology=HardwareTopology( 89 screen=self.screen_1_topology, 90 keyboard=self.keyboard_1_topology)), 91 Design.Config( 92 hardware_topology=HardwareTopology( 93 screen=invalid_screen_topology, 94 keyboard=self.keyboard_1_topology)) 95 ]) 96 ]) 97 98 with self.assertRaisesRegex( 99 AssertionError, 100 r'Two different messages found for id and type \(part1, SCREEN\)*'): 101 TopologyConstraintSuite().run_checks( 102 program_config=None, 103 project_config=project_config, 104 factory_dir=None, 105 ) 106 107 def test_check_topologies_consistent_violated_across_designs(self): 108 """Tests assertion thrown when topologies are inconsistent across designs. 109 """ 110 # Create a copy of the first screen topology, but change the milliinch 111 # value. Now, a given id and type maps to two different messages. 112 invalid_screen_topology = Topology() 113 invalid_screen_topology.CopyFrom(self.screen_1_topology) 114 invalid_screen = invalid_screen_topology.hardware_feature.screen 115 invalid_screen.panel_properties.diagonal_milliinch = 20 116 117 project_config = ConfigBundle(design_list=[ 118 Design(configs=[ 119 Design.Config( 120 hardware_topology=HardwareTopology( 121 screen=self.screen_1_topology, 122 keyboard=self.keyboard_1_topology)), 123 ]), 124 Design(configs=[ 125 Design.Config( 126 hardware_topology=HardwareTopology( 127 screen=invalid_screen_topology, 128 keyboard=self.keyboard_1_topology)) 129 ]) 130 ]) 131 132 with self.assertRaisesRegex( 133 AssertionError, 134 r'Two different messages found for id and type \(part1, SCREEN\)*'): 135 TopologyConstraintSuite().run_checks( 136 program_config=None, 137 project_config=project_config, 138 factory_dir=None, 139 ) 140