blob: 6ab46783df6fe8a643b7e0dcf058e4ea70551ac1 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2018 The ChromiumOS Authors
Alex Kleina2ceb192018-08-17 11:19:32 -06002# 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 Kleina2ceb192018-08-17 11:19:32 -06007import os
8
9from chromite.lib import commandline
10from chromite.lib import cros_test_lib
11from chromite.lib import osutils
Sloan Johnsona85640f2021-10-01 22:32:40 +000012from chromite.lib import unittest_lib
Alex Kleina2ceb192018-08-17 11:19:32 -060013from chromite.scripts import cros_choose_profile
14
15
16class ParseArgsTest(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060017 """Tests for argument parsing and validation rules."""
Alex Kleina2ceb192018-08-17 11:19:32 -060018
Alex Klein1699fab2022-09-08 08:46:06 -060019 def testInvalidArgs(self):
20 """Test invalid argument parsing."""
21 with self.assertRaises(SystemExit):
22 cros_choose_profile.ParseArgs([])
Alex Kleina2ceb192018-08-17 11:19:32 -060023
Alex Klein1699fab2022-09-08 08:46:06 -060024 with self.assertRaises(SystemExit):
25 cros_choose_profile.ParseArgs(
26 ["--profile", "profile", "--variant", "variant"]
27 )
Alex Kleina2ceb192018-08-17 11:19:32 -060028
29
30class BoardTest(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060031 """Tests for the Board class logic."""
Alex Kleina2ceb192018-08-17 11:19:32 -060032
Alex Klein1699fab2022-09-08 08:46:06 -060033 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 Kleina2ceb192018-08-17 11:19:32 -060043
Alex Klein1699fab2022-09-08 08:46:06 -060044 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 Kleina2ceb192018-08-17 11:19:32 -060049
Alex Klein1699fab2022-09-08 08:46:06 -060050 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 Kleina2ceb192018-08-17 11:19:32 -060053
Alex Klein1699fab2022-09-08 08:46:06 -060054 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 Kleina2ceb192018-08-17 11:19:32 -060057
Alex Klein1699fab2022-09-08 08:46:06 -060058 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 Kleina2ceb192018-08-17 11:19:32 -060061
Alex Klein1699fab2022-09-08 08:46:06 -060062 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 Kleina2ceb192018-08-17 11:19:32 -060067
68
69class ProfileTest(cros_test_lib.TempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060070 """Tests for the Profile class and functions, and ChooseProfile."""
Alex Kleina2ceb192018-08-17 11:19:32 -060071
Alex Klein1699fab2022-09-08 08:46:06 -060072 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 Kleina2ceb192018-08-17 11:19:32 -060076
Alex Klein1699fab2022-09-08 08:46:06 -060077 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 Kleina2ceb192018-08-17 11:19:32 -0600113
Alex Klein1699fab2022-09-08 08:46:06 -0600114 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, filesystem)
Alex Kleina2ceb192018-08-17 11:19:32 -0600115
Alex Klein1699fab2022-09-08 08:46:06 -0600116 # 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 Kleina2ceb192018-08-17 11:19:32 -0600124
Alex Klein1699fab2022-09-08 08:46:06 -0600125 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 Kleina2ceb192018-08-17 11:19:32 -0600131
Alex Klein1699fab2022-09-08 08:46:06 -0600132 # Contents to write to the corresponding file.
Alex Kleina2ceb192018-08-17 11:19:32 -0600133
Alex Kleind7197402023-04-05 13:05:29 -0600134 # self.profile_override is assumed to be a profile name in
135 # testGetProfile. Update code there if this changes.
Alex Klein1699fab2022-09-08 08:46:06 -0600136 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 Kleina2ceb192018-08-17 11:19:32 -0600144
Alex Klein1699fab2022-09-08 08:46:06 -0600145 for filepath, contents in path_contents.items():
146 osutils.WriteFile(self._TempdirPath(filepath), contents)
Alex Kleina2ceb192018-08-17 11:19:32 -0600147
Alex Klein1699fab2022-09-08 08:46:06 -0600148 # make.conf needs to exist to correctly read back config.
149 unittest_lib.create_stub_make_conf(self._TempdirPath(b1_build_root))
Sloan Johnsona85640f2021-10-01 22:32:40 +0000150
Alex Klein1699fab2022-09-08 08:46:06 -0600151 # 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 Kleina2ceb192018-08-17 11:19:32 -0600157
Alex Klein1699fab2022-09-08 08:46:06 -0600158 # 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 Kleina2ceb192018-08-17 11:19:32 -0600164
Alex Klein1699fab2022-09-08 08:46:06 -0600165 # The make profile directory from which parent files are read.
166 self.board1_make_profile = "/build/board1/etc/portage/make.profile"
Alex Kleina2ceb192018-08-17 11:19:32 -0600167
Alex Klein1699fab2022-09-08 08:46:06 -0600168 self.board1 = cros_choose_profile.Board(board_root=b1_build_root)
Alex Kleina2ceb192018-08-17 11:19:32 -0600169
Alex Klein1699fab2022-09-08 08:46:06 -0600170 osutils.SafeSymlink(
171 self._TempdirPath(p1_directory),
172 self._TempdirPath(self.board1_make_profile),
173 )
Alex Kleina2ceb192018-08-17 11:19:32 -0600174
Alex Klein1699fab2022-09-08 08:46:06 -0600175 def tearDown(self):
176 # Reset the prefix.
177 cros_choose_profile.PathPrefixDecorator.prefix = None
Alex Kleina2ceb192018-08-17 11:19:32 -0600178
Alex Klein1699fab2022-09-08 08:46:06 -0600179 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 Kleina2ceb192018-08-17 11:19:32 -0600183
Alex Klein1699fab2022-09-08 08:46:06 -0600184 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 Kleina2ceb192018-08-17 11:19:32 -0600194
Alex Klein1699fab2022-09-08 08:46:06 -0600195 for profile_name, parent in self.profile_expected_parent.items():
Alex Kleind7197402023-04-05 13:05:29 -0600196 # Call ChooseProfile for the given profile and check contents as
197 # specified by self.profile_expected_parent (built in setUp).
Alex Kleina2ceb192018-08-17 11:19:32 -0600198
Alex Klein1699fab2022-09-08 08:46:06 -0600199 profile_dir = self.profile_directory[profile_name]
200 profile = cros_choose_profile.Profile(
201 profile_name, profile_dir, profile_name
202 )
Alex Kleina2ceb192018-08-17 11:19:32 -0600203
Alex Klein1699fab2022-09-08 08:46:06 -0600204 # Test the profile changing.
205 cros_choose_profile.ChooseProfile(self.board1, profile)
206 self.assertEqual(parent, osutils.ReadFile(b1_parent_path))
Alex Kleina2ceb192018-08-17 11:19:32 -0600207
Alex Klein1699fab2022-09-08 08:46:06 -0600208 # Test the profile staying the same.
209 cros_choose_profile.ChooseProfile(self.board1, profile)
210 self.assertEqual(parent, osutils.ReadFile(b1_parent_path))
Alex Kleina2ceb192018-08-17 11:19:32 -0600211
Alex Klein1699fab2022-09-08 08:46:06 -0600212 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 Kleina2ceb192018-08-17 11:19:32 -0600223
Alex Klein1699fab2022-09-08 08:46:06 -0600224 # Profile values for following tests.
225 profile_name = self.profile_override
226 profile_path = self._TempdirPath(self.profile_directory[profile_name])
Alex Kleina2ceb192018-08-17 11:19:32 -0600227
Alex Klein1699fab2022-09-08 08:46:06 -0600228 # 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 Kleina2ceb192018-08-17 11:19:32 -0600234
Alex Klein1699fab2022-09-08 08:46:06 -0600235 # 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 Kleina2ceb192018-08-17 11:19:32 -0600241
Alex Klein1699fab2022-09-08 08:46:06 -0600242 # 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 Kleina2ceb192018-08-17 11:19:32 -0600248
Alex Klein1699fab2022-09-08 08:46:06 -0600249 # 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)