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