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