Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2018 The ChromiumOS Authors |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Test cros_choose_profile.""" |
| 6 | |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 7 | import os |
| 8 | |
| 9 | from chromite.lib import commandline |
| 10 | from chromite.lib import cros_test_lib |
| 11 | from chromite.lib import osutils |
Sloan Johnson | a85640f | 2021-10-01 22:32:40 +0000 | [diff] [blame] | 12 | from chromite.lib import unittest_lib |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 13 | from chromite.scripts import cros_choose_profile |
| 14 | |
| 15 | |
| 16 | class ParseArgsTest(cros_test_lib.TestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 17 | """Tests for argument parsing and validation rules.""" |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 18 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 19 | def testInvalidArgs(self): |
| 20 | """Test invalid argument parsing.""" |
| 21 | with self.assertRaises(SystemExit): |
| 22 | cros_choose_profile.ParseArgs([]) |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 23 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 24 | with self.assertRaises(SystemExit): |
| 25 | cros_choose_profile.ParseArgs( |
| 26 | ["--profile", "profile", "--variant", "variant"] |
| 27 | ) |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 28 | |
| 29 | |
| 30 | class BoardTest(cros_test_lib.TestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 31 | """Tests for the Board class logic.""" |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 32 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 33 | def setUp(self): |
| 34 | """Set up the boards with the different construction variations.""" |
| 35 | # For readability's sake. |
| 36 | Board = cros_choose_profile.Board |
| 37 | self.board_variant1 = Board(board="board_variant") |
| 38 | self.board_variant2 = Board(board="board", variant="variant") |
| 39 | self.board_variant3 = Board(board_root="/build/board_variant") |
| 40 | self.board_variant4 = Board( |
| 41 | board="board_variant", board_root="/build/ignored_value" |
| 42 | ) |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 43 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 44 | def testBoardVariant(self): |
| 45 | """Board.{board, variant, board_variant} building tests.""" |
| 46 | self.assertEqual("board", self.board_variant1.board) |
| 47 | self.assertEqual("variant", self.board_variant1.variant) |
| 48 | self.assertEqual("board_variant", self.board_variant1.board_variant) |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 49 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 50 | self.assertEqual("board", self.board_variant2.board) |
| 51 | self.assertEqual("variant", self.board_variant2.variant) |
| 52 | self.assertEqual("board_variant", self.board_variant2.board_variant) |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 53 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 54 | self.assertEqual("board", self.board_variant3.board) |
| 55 | self.assertEqual("variant", self.board_variant3.variant) |
| 56 | self.assertEqual("board_variant", self.board_variant3.board_variant) |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 57 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 58 | self.assertEqual("board", self.board_variant4.board) |
| 59 | self.assertEqual("variant", self.board_variant4.variant) |
| 60 | self.assertEqual("board_variant", self.board_variant4.board_variant) |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 61 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 62 | def testRoot(self): |
| 63 | """Board.root tests.""" |
| 64 | self.assertEqual(self.board_variant1.root, self.board_variant2.root) |
| 65 | self.assertEqual(self.board_variant1.root, self.board_variant3.root) |
| 66 | self.assertEqual(self.board_variant1.root, self.board_variant4.root) |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 67 | |
| 68 | |
| 69 | class ProfileTest(cros_test_lib.TempDirTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 70 | """Tests for the Profile class and functions, and ChooseProfile.""" |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 71 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 72 | def setUp(self): |
| 73 | """Setup filesystem for the profile tests.""" |
| 74 | # Make sure everything will use the filesystem we're setting up. |
| 75 | cros_choose_profile.PathPrefixDecorator.prefix = self.tempdir |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 76 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 77 | D = cros_test_lib.Directory |
| 78 | filesystem = ( |
| 79 | D( |
| 80 | "board1-overlay", |
| 81 | ( |
| 82 | D( |
| 83 | "profiles", |
| 84 | ( |
| 85 | D("base", ("parent",)), |
| 86 | D("profile1", ("parent",)), |
| 87 | D("profile2", ("parent",)), |
| 88 | ), |
| 89 | ), |
| 90 | ), |
| 91 | ), |
| 92 | D( |
| 93 | "build", |
| 94 | ( |
| 95 | D( |
| 96 | "board1", |
| 97 | ( |
| 98 | D( |
| 99 | "etc", |
| 100 | ( |
| 101 | D( |
| 102 | "portage", () |
| 103 | ), # make.profile parent directory. |
| 104 | "make.conf.board_setup", |
| 105 | ), |
| 106 | ), |
| 107 | D("var", (D("cache", (D("edb", ("chromeos",)),)),)), |
| 108 | ), |
| 109 | ), |
| 110 | ), |
| 111 | ), |
| 112 | ) |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 113 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 114 | cros_test_lib.CreateOnDiskHierarchy(self.tempdir, filesystem) |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 115 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 116 | # Generate the required filesystem content. |
| 117 | # Build out the file names that need content. |
| 118 | b1_build_root = "/build/board1" |
| 119 | b1_board_setup = os.path.join( |
| 120 | b1_build_root, "etc/make.conf.board_setup" |
| 121 | ) |
| 122 | self.b1_sysroot = os.path.join(b1_build_root, "var/cache/edb/chromeos") |
| 123 | b1_profiles = "/board1-overlay/profiles" |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 124 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 125 | base_directory = os.path.join(b1_profiles, "base") |
| 126 | base_parent = os.path.join(base_directory, "parent") |
| 127 | p1_directory = os.path.join(b1_profiles, "profile1") |
| 128 | p1_parent = os.path.join(p1_directory, "parent") |
| 129 | p2_directory = os.path.join(b1_profiles, "profile2") |
| 130 | p2_parent = os.path.join(p2_directory, "parent") |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 131 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 132 | # Contents to write to the corresponding file. |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 133 | |
Alex Klein | d719740 | 2023-04-05 13:05:29 -0600 | [diff] [blame^] | 134 | # self.profile_override is assumed to be a profile name in |
| 135 | # testGetProfile. Update code there if this changes. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 136 | self.profile_override = "profile1" |
| 137 | path_contents = { |
| 138 | b1_board_setup: 'ARCH="arch"\nBOARD_OVERLAY="/board1-overlay"', |
| 139 | self.b1_sysroot: 'PROFILE_OVERRIDE="%s"' % self.profile_override, |
| 140 | base_parent: "base parent contents", |
| 141 | p1_parent: "profile1 parent contents", |
| 142 | p2_parent: "profile2 parent contents", |
| 143 | } |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 144 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 145 | for filepath, contents in path_contents.items(): |
| 146 | osutils.WriteFile(self._TempdirPath(filepath), contents) |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 147 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 148 | # make.conf needs to exist to correctly read back config. |
| 149 | unittest_lib.create_stub_make_conf(self._TempdirPath(b1_build_root)) |
Sloan Johnson | a85640f | 2021-10-01 22:32:40 +0000 | [diff] [blame] | 150 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 151 | # Mapping between profile argument and the expected parent contents. |
| 152 | self.profile_expected_parent = { |
| 153 | "base": path_contents[base_parent], |
| 154 | "profile1": path_contents[p1_parent], |
| 155 | "profile2": path_contents[p2_parent], |
| 156 | } |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 157 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 158 | # Mapping between the profile argument and the profile's directory. |
| 159 | self.profile_directory = { |
| 160 | "base": base_directory, |
| 161 | "profile1": p1_directory, |
| 162 | "profile2": p2_directory, |
| 163 | } |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 164 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 165 | # The make profile directory from which parent files are read. |
| 166 | self.board1_make_profile = "/build/board1/etc/portage/make.profile" |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 167 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 168 | self.board1 = cros_choose_profile.Board(board_root=b1_build_root) |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 169 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 170 | osutils.SafeSymlink( |
| 171 | self._TempdirPath(p1_directory), |
| 172 | self._TempdirPath(self.board1_make_profile), |
| 173 | ) |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 174 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 175 | def tearDown(self): |
| 176 | # Reset the prefix. |
| 177 | cros_choose_profile.PathPrefixDecorator.prefix = None |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 178 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 179 | def _TempdirPath(self, path): |
| 180 | """Join the tempdir base path to the given path.""" |
| 181 | # lstrip leading / to prevent it returning the path without the tempdir. |
| 182 | return os.path.join(self.tempdir, path.lstrip(os.sep)) |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 183 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 184 | def testChooseProfile(self): |
| 185 | """ChooseProfile tests: verify profiles are properly chosen.""" |
| 186 | b1_parent_path = self._TempdirPath( |
| 187 | os.path.join(self.board1_make_profile, "parent") |
| 188 | ) |
| 189 | # Verify initial state - profile1. |
| 190 | self.assertEqual( |
| 191 | self.profile_expected_parent["profile1"], |
| 192 | osutils.ReadFile(b1_parent_path), |
| 193 | ) |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 194 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 195 | for profile_name, parent in self.profile_expected_parent.items(): |
Alex Klein | d719740 | 2023-04-05 13:05:29 -0600 | [diff] [blame^] | 196 | # Call ChooseProfile for the given profile and check contents as |
| 197 | # specified by self.profile_expected_parent (built in setUp). |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 198 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 199 | profile_dir = self.profile_directory[profile_name] |
| 200 | profile = cros_choose_profile.Profile( |
| 201 | profile_name, profile_dir, profile_name |
| 202 | ) |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 203 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 204 | # Test the profile changing. |
| 205 | cros_choose_profile.ChooseProfile(self.board1, profile) |
| 206 | self.assertEqual(parent, osutils.ReadFile(b1_parent_path)) |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 207 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 208 | # Test the profile staying the same. |
| 209 | cros_choose_profile.ChooseProfile(self.board1, profile) |
| 210 | self.assertEqual(parent, osutils.ReadFile(b1_parent_path)) |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 211 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 212 | def testGetProfile(self): |
| 213 | """Test each profile parameter type behaves as expected when fetched.""" |
| 214 | # pylint: disable=protected-access |
| 215 | # Test an invalid profile name. |
| 216 | args = commandline.ArgumentNamespace(profile="doesnotexist") |
| 217 | self.assertRaises( |
| 218 | cros_choose_profile.ProfileDirectoryNotFoundError, |
| 219 | cros_choose_profile._GetProfile, |
| 220 | args, |
| 221 | self.board1, |
| 222 | ) |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 223 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 224 | # Profile values for following tests. |
| 225 | profile_name = self.profile_override |
| 226 | profile_path = self._TempdirPath(self.profile_directory[profile_name]) |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 227 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 228 | # Test using the profile name. |
| 229 | args = commandline.ArgumentNamespace(profile=profile_name) |
| 230 | profile = cros_choose_profile._GetProfile(args, self.board1) |
| 231 | self.assertEqual(profile_name, profile.name) |
| 232 | self.assertEqual(profile_path, profile.directory) |
| 233 | self.assertEqual(profile_name, profile.override) |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 234 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 235 | # Test using the profile path. |
| 236 | args = commandline.ArgumentNamespace(profile=profile_path) |
| 237 | profile = cros_choose_profile._GetProfile(args, self.board1) |
| 238 | self.assertEqual(profile_name, profile.name) |
| 239 | self.assertEqual(profile_path, profile.directory) |
| 240 | self.assertEqual(profile_path, profile.override) |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 241 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 242 | # Test using PROFILE_OVERRIDE. |
| 243 | args = commandline.ArgumentNamespace(profile=None) |
| 244 | profile = cros_choose_profile._GetProfile(args, self.board1) |
| 245 | self.assertEqual(profile_name, profile.name) |
| 246 | self.assertEqual(profile_path, profile.directory) |
| 247 | self.assertEqual(self.profile_override, profile.override) |
Alex Klein | a2ceb19 | 2018-08-17 11:19:32 -0600 | [diff] [blame] | 248 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 249 | # No override value, using default 'base'. |
| 250 | osutils.WriteFile(self._TempdirPath(self.b1_sysroot), "") |
| 251 | args = commandline.ArgumentNamespace(profile=None) |
| 252 | profile = cros_choose_profile._GetProfile(opts=args, board=self.board1) |
| 253 | self.assertEqual("base", profile.name) |
| 254 | self.assertEqual( |
| 255 | self._TempdirPath(self.profile_directory["base"]), profile.directory |
| 256 | ) |
| 257 | self.assertIsNone(profile.override) |