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 constraint_suite.""" 5 6import os 7import pathlib 8import tempfile 9import unittest 10 11from chromiumos.config.payload.config_bundle_pb2 import ConfigBundle 12from chromiumos.config.api.design_pb2 import Design 13from chromiumos.config.api.program_pb2 import Program 14 15from checker.constraint_suite import ConstraintSuite 16 17# Some tests just check no exceptions were raised, and will not call self.assert 18# methods 19# pylint: disable=no-self-use 20 21 22class ValidConstraintSuite(ConstraintSuite): 23 """A valid constraint suite for testing, that defines two checks.""" 24 25 # pylint: disable=missing-docstring 26 27 def _helper_method(self): 28 assert False, "helper_method should never be called" 29 30 def check_program_valid( 31 self, 32 program_config, 33 project_config, 34 factory_dir, 35 ): 36 del project_config, factory_dir 37 self.assertEqual(program_config.program_list[0].name, 'TestProgram1') 38 39 def check_project_valid( 40 self, 41 program_config, 42 project_config, 43 factory_dir, 44 ): 45 del program_config, factory_dir 46 self.assertEqual(project_config.design_list[0].name, 'TestDesign1') 47 48 49class FactoryDirSuite(ConstraintSuite): 50 """A valid constraint suite for testing that expects a file in factory_dir.""" 51 52 # pylint: disable=missing-docstring 53 54 def check_generated_config_present( 55 self, 56 program_config, 57 project_config, 58 factory_dir, 59 ): 60 del program_config, project_config 61 self.assertTrue(os.path.exists(os.path.join(factory_dir, 'test.txt'))) 62 63 64class InvalidConstraintSuite(ConstraintSuite): 65 """An invalid constraint suite for testing, that defines no checks.""" 66 67 # pylint: disable=missing-docstring 68 69 def helper_method1(self): 70 pass 71 72 def helper_method2(self): 73 pass 74 75 76class ConstraintSuiteTest(unittest.TestCase): 77 """Tests for constraint_suite.""" 78 79 def test_runs_checks(self): 80 """Tests running checks on a project that fulfills constraints.""" 81 program_config = ConfigBundle(program_list=[Program(name='TestProgram1')]) 82 project_config = ConfigBundle(design_list=[Design(name='TestDesign1')]) 83 84 ValidConstraintSuite().run_checks( 85 program_config=program_config, 86 project_config=project_config, 87 factory_dir=None) 88 89 def test_runs_checks_fails_constraint(self): 90 """Tests running checks on a project that violates constraints.""" 91 program_config = ConfigBundle(program_list=[Program(name='TestProgram2')]) 92 project_config = ConfigBundle(design_list=[Design(name='TestDesign1')]) 93 94 with self.assertRaisesRegex(AssertionError, 95 "'TestProgram2' != 'TestProgram1'"): 96 ValidConstraintSuite().run_checks( 97 program_config=program_config, 98 project_config=project_config, 99 factory_dir=None) 100 101 def test_checks_factory_dir(self): 102 """Tests running checks that a file in factory_dir is present.""" 103 104 with tempfile.TemporaryDirectory() as tmpdir: 105 with open(os.path.join(tmpdir, 'test.txt'), 'w', encoding='utf-8') as fp: 106 fp.write('test file\n') 107 108 FactoryDirSuite().run_checks( 109 program_config=None, 110 project_config=None, 111 factory_dir=pathlib.Path(tmpdir), 112 ) 113 114 def test_checks_factory_dir_violated(self): 115 """Tests running checks that fail because a file in factory_dir is 116 not present. 117 """ 118 119 with tempfile.TemporaryDirectory() as tmpdir: 120 with self.assertRaises(AssertionError): 121 FactoryDirSuite().run_checks( 122 program_config=None, 123 project_config=None, 124 factory_dir=pathlib.Path(tmpdir), 125 ) 126 127 def test_has_delegated_assertions(self): 128 """Tests that ConstraintSuites have assertion methods.""" 129 constraint_suite = ValidConstraintSuite() 130 self.assertTrue(hasattr(constraint_suite, 'assertTrue')) 131 with self.assertRaisesRegex(AssertionError, "1 != 2"): 132 constraint_suite.assertEqual(1, 2) 133