blob: aff339bab6854f079309f510b67740b36b808644 [file] [log] [blame]
Don Garrettc4114cc2016-11-01 20:04:06 -07001# Copyright 2016 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"""Unit tests for chromite.lib.git and helpers for testing that module."""
6
7from __future__ import print_function
8
Don Garrett86881cb2017-02-15 15:41:55 -08009import mock
Don Garrett7ade05a2017-02-17 13:31:47 -080010import os
Don Garrett86881cb2017-02-15 15:41:55 -080011
12from chromite.cbuildbot import repository
Don Garrett861e9182017-05-15 15:30:23 -070013from chromite.lib import constants
Don Garrett597ddff2017-02-17 18:29:37 -080014from chromite.lib import cros_build_lib
Don Garrettc4114cc2016-11-01 20:04:06 -070015from chromite.lib import cros_build_lib_unittest
Don Garrett7ade05a2017-02-17 13:31:47 -080016from chromite.lib import cros_test_lib
Don Garrett86881cb2017-02-15 15:41:55 -080017from chromite.lib import osutils
Don Garrett0c54ed72017-03-03 11:18:57 -080018from chromite.scripts import cbuildbot_launch
Don Garrett86881cb2017-02-15 15:41:55 -080019
Don Garrett60967922017-04-12 18:51:44 -070020
Don Garrett86881cb2017-02-15 15:41:55 -080021EXPECTED_MANIFEST_URL = 'https://chrome-internal-review.googlesource.com/chromeos/manifest-internal' # pylint: disable=line-too-long
Don Garrettc4114cc2016-11-01 20:04:06 -070022
23
Don Garrettacbb2392017-05-11 18:27:41 -070024# It's reasonable for unittests to look at internals.
25# pylint: disable=protected-access
26
27
Don Garrett8d314792017-05-18 13:11:42 -070028class FakeException(Exception):
29 """Test exception to raise during tests."""
30
31
Don Garrett0c54ed72017-03-03 11:18:57 -080032class CbuildbotLaunchTest(cros_test_lib.MockTestCase):
33 """Tests for cbuildbot_launch script."""
Don Garrettc4114cc2016-11-01 20:04:06 -070034
Don Garrett86881cb2017-02-15 15:41:55 -080035 def testPreParseArguments(self):
Don Garrettc4114cc2016-11-01 20:04:06 -070036 """Test that we can correctly extract branch values from cbuildbot args."""
Don Garrett597ddff2017-02-17 18:29:37 -080037 CASES = (
38 (['--buildroot', '/buildroot', 'daisy-incremental'],
39 (None, '/buildroot', None)),
40
41 (['--buildbot', '--buildroot', '/buildroot',
42 '--git-cache-dir', '/git-cache',
43 '-b', 'release-R57-9202.B',
44 'daisy-incremental'],
45 ('release-R57-9202.B', '/buildroot', '/git-cache')),
46
47 (['--debug', '--buildbot', '--notests',
48 '--buildroot', '/buildroot',
49 '--git-cache-dir', '/git-cache',
50 '--branch', 'release-R57-9202.B',
51 'daisy-incremental'],
52 ('release-R57-9202.B', '/buildroot', '/git-cache')),
Don Garrettc4114cc2016-11-01 20:04:06 -070053 )
54
Don Garrett597ddff2017-02-17 18:29:37 -080055 for cmd_args, expected in CASES:
56 expected_branch, expected_buildroot, expected_cache_dir = expected
57
Don Garrett0c54ed72017-03-03 11:18:57 -080058 options = cbuildbot_launch.PreParseArguments(cmd_args)
Don Garrett597ddff2017-02-17 18:29:37 -080059
60 self.assertEqual(options.branch, expected_branch)
61 self.assertEqual(options.buildroot, expected_buildroot)
62 self.assertEqual(options.git_cache_dir, expected_cache_dir)
Don Garrett86881cb2017-02-15 15:41:55 -080063
Don Garrettf324bc32017-05-23 14:00:53 -070064 def testInitialCheckout(self):
Don Garrett86881cb2017-02-15 15:41:55 -080065 """Test InitialCheckout with minimum settings."""
Don Garrettf324bc32017-05-23 14:00:53 -070066 mock_repo = mock.MagicMock()
67 mock_repo.branch = 'branch'
Don Garrett86881cb2017-02-15 15:41:55 -080068
Don Garrettf324bc32017-05-23 14:00:53 -070069 cbuildbot_launch.InitialCheckout(mock_repo)
Don Garrett86881cb2017-02-15 15:41:55 -080070
71 self.assertEqual(mock_repo.mock_calls, [
Don Garrettf324bc32017-05-23 14:00:53 -070072 mock.call.Sync(detach=True),
Don Garrett8d314792017-05-18 13:11:42 -070073 ])
74
Don Garrettf15d65b2017-04-12 12:39:55 -070075 def testConfigureGlobalEnvironment(self):
Don Garrett60967922017-04-12 18:51:44 -070076 """Ensure that we can setup our global runtime environment correctly."""
Don Garrett86fec482017-05-17 18:13:33 -070077
78 os.environ.pop('LANG', None)
79 os.environ['LC_MONETARY'] = 'bad'
80
Don Garrettf15d65b2017-04-12 12:39:55 -070081 cbuildbot_launch.ConfigureGlobalEnvironment()
82
Don Garrett86fec482017-05-17 18:13:33 -070083 # Verify umask is updated.
Don Garrettf15d65b2017-04-12 12:39:55 -070084 self.assertEqual(os.umask(0), 0o22)
85
Don Garrett86fec482017-05-17 18:13:33 -070086 # Verify ENVs are cleaned up.
87 self.assertEqual(os.environ['LANG'], 'en_US.UTF-8')
88 self.assertNotIn('LC_MONETARY', os.environ)
89
Don Garrettf15d65b2017-04-12 12:39:55 -070090
Don Garrett066e6f52017-09-28 19:14:01 -070091class RunDepotToolsEnsureBootstrap(cros_build_lib_unittest.RunCommandTestCase,
92 cros_test_lib.TempDirTestCase):
93 """Test the helper function DepotToolsEnsureBootstrap."""
94
95 def testEnsureBootstrap(self):
96 """Verify that the script is run if present."""
97 script = os.path.join(self.tempdir, 'ensure_bootstrap')
98 osutils.Touch(script, makedirs=True)
99
100 cbuildbot_launch.DepotToolsEnsureBootstrap(self.tempdir)
101 self.assertCommandCalled(
102 [script], extra_env={'PATH': mock.ANY}, cwd=self.tempdir)
103
104 def testEnsureBootstrapMissing(self):
105 """Verify that the script is NOT run if not present."""
106 cbuildbot_launch.DepotToolsEnsureBootstrap(self.tempdir)
107 self.assertEqual(self.rc.call_count, 0)
108
109
Don Garrett597ddff2017-02-17 18:29:37 -0800110class RunTests(cros_build_lib_unittest.RunCommandTestCase):
Don Garrett0c54ed72017-03-03 11:18:57 -0800111 """Tests for cbuildbot_launch script."""
Don Garrett597ddff2017-02-17 18:29:37 -0800112
113 ARGS_BASE = ['--buildroot', '/buildroot']
Don Garrett5cd946b2017-07-20 13:42:20 -0700114 EXPECTED_ARGS_BASE = ['--buildroot', '/cbuildbot_buildroot']
Don Garrett597ddff2017-02-17 18:29:37 -0800115 ARGS_GIT_CACHE = ['--git-cache-dir', '/git-cache']
116 ARGS_CONFIG = ['config']
Don Garrettbf90cdf2017-05-19 15:54:02 -0700117 CMD = ['/cbuildbot_buildroot/chromite/bin/cbuildbot']
Don Garrett597ddff2017-02-17 18:29:37 -0800118
119 def verifyRunCbuildbot(self, args, expected_cmd, version):
Don Garrett86881cb2017-02-15 15:41:55 -0800120 """Ensure we invoke cbuildbot correctly."""
Don Garrett597ddff2017-02-17 18:29:37 -0800121 self.PatchObject(
122 cros_build_lib, 'GetTargetChromiteApiVersion', autospec=True,
123 return_value=version)
124
Don Garretta50bf492017-09-28 18:33:02 -0700125 cbuildbot_launch.RunCbuildbot('/cbuildbot_buildroot', '/depot_tools', args)
Don Garrett597ddff2017-02-17 18:29:37 -0800126
127 self.assertCommandCalled(
Don Garretta50bf492017-09-28 18:33:02 -0700128 expected_cmd, extra_env={'PATH': mock.ANY},
129 cwd='/cbuildbot_buildroot', error_code_ok=True)
Don Garrett597ddff2017-02-17 18:29:37 -0800130
131 def testRunCbuildbotSimple(self):
132 """Ensure we invoke cbuildbot correctly."""
133 self.verifyRunCbuildbot(
134 self.ARGS_BASE + self.ARGS_CONFIG,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700135 self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE,
Don Garrett597ddff2017-02-17 18:29:37 -0800136 (0, 4))
137
138 def testRunCbuildbotNotFiltered(self):
139 """Ensure we invoke cbuildbot correctly."""
140 self.verifyRunCbuildbot(
141 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700142 (self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE +
143 self.ARGS_GIT_CACHE),
Don Garrett597ddff2017-02-17 18:29:37 -0800144 (0, 4))
145
146 def testRunCbuildbotFiltered(self):
147 """Ensure we invoke cbuildbot correctly."""
148 self.verifyRunCbuildbot(
149 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700150 self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE,
Don Garrett597ddff2017-02-17 18:29:37 -0800151 (0, 2))
Don Garrettc4114cc2016-11-01 20:04:06 -0700152
Don Garrett86881cb2017-02-15 15:41:55 -0800153 def testMainMin(self):
154 """Test a minimal set of command line options."""
Don Garrett597ddff2017-02-17 18:29:37 -0800155 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
156 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
Don Garrett861e9182017-05-15 15:30:23 -0700157 autospec=True, return_value=(constants.REEXEC_API_MAJOR,
158 constants.REEXEC_API_MINOR))
Don Garrettf324bc32017-05-23 14:00:53 -0700159 mock_repo = mock.MagicMock()
160 mock_repo.branch = 'master'
Don Garrettbf90cdf2017-05-19 15:54:02 -0700161 mock_repo.directory = '/root/repository'
162
Don Garrettf324bc32017-05-23 14:00:53 -0700163 mock_repo_create = self.PatchObject(repository, 'RepoRepository',
164 autospec=True, return_value=mock_repo)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700165 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot',
Don Garrett0c54ed72017-03-03 11:18:57 -0800166 autospec=True)
167 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800168 autospec=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800169
Don Garrettbf90cdf2017-05-19 15:54:02 -0700170 cbuildbot_launch._main(['-r', '/root', 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700171
Don Garrettf324bc32017-05-23 14:00:53 -0700172 # Did we create the repo instance correctly?
173 self.assertEqual(mock_repo_create.mock_calls,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700174 [mock.call(EXPECTED_MANIFEST_URL, '/root/repository',
Don Garrettf324bc32017-05-23 14:00:53 -0700175 git_cache_dir=None, branch='master')])
176
Don Garrett7ade05a2017-02-17 13:31:47 -0800177 # Ensure we clean, as expected.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700178 self.assertEqual(mock_clean.mock_calls, [
Don Garrettd1d90dd2017-06-13 17:35:52 -0700179 mock.call('/root', mock_repo,
180 {
181 'branch_name': 'master',
182 'tryjob': False,
183 'build_config': 'config',
184 })])
Don Garrett7ade05a2017-02-17 13:31:47 -0800185
Don Garrett86881cb2017-02-15 15:41:55 -0800186 # Ensure we checkout, as expected.
187 self.assertEqual(mock_checkout.mock_calls,
Don Garrettf324bc32017-05-23 14:00:53 -0700188 [mock.call(mock_repo)])
Don Garrettc4114cc2016-11-01 20:04:06 -0700189
Don Garrett86881cb2017-02-15 15:41:55 -0800190 # Ensure we invoke cbuildbot, as expected.
191 self.assertCommandCalled(
Don Garrett5cd946b2017-07-20 13:42:20 -0700192 [
193 '/root/repository/chromite/bin/cbuildbot',
194 'config',
195 '-r', '/root/repository',
196 '--ts-mon-task-num', '1',
197 ],
Don Garretta50bf492017-09-28 18:33:02 -0700198 extra_env={'PATH': mock.ANY},
Don Garrettbf90cdf2017-05-19 15:54:02 -0700199 cwd='/root/repository',
Don Garrettacbb2392017-05-11 18:27:41 -0700200 error_code_ok=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700201
Don Garrett86881cb2017-02-15 15:41:55 -0800202 def testMainMax(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800203 """Test a larger set of command line options."""
204 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
205 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
Don Garrett861e9182017-05-15 15:30:23 -0700206 autospec=True, return_value=(constants.REEXEC_API_MAJOR,
207 constants.REEXEC_API_MINOR))
Don Garrettf324bc32017-05-23 14:00:53 -0700208 mock_repo = mock.MagicMock()
209 mock_repo.branch = 'branch'
Don Garrettbf90cdf2017-05-19 15:54:02 -0700210 mock_repo.directory = '/root/repository'
211
Don Garrettf324bc32017-05-23 14:00:53 -0700212 mock_repo_create = self.PatchObject(repository, 'RepoRepository',
213 autospec=True, return_value=mock_repo)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700214 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot',
Don Garrett0c54ed72017-03-03 11:18:57 -0800215 autospec=True)
216 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800217 autospec=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700218
Don Garrettbf90cdf2017-05-19 15:54:02 -0700219 cbuildbot_launch._main(['--buildroot', '/root',
Don Garrettacbb2392017-05-11 18:27:41 -0700220 '--branch', 'branch',
221 '--git-cache-dir', '/git-cache',
Don Garrettd1d90dd2017-06-13 17:35:52 -0700222 '--remote-trybot',
Don Garrettacbb2392017-05-11 18:27:41 -0700223 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700224
Don Garrettf324bc32017-05-23 14:00:53 -0700225 # Did we create the repo instance correctly?
226 self.assertEqual(mock_repo_create.mock_calls,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700227 [mock.call(EXPECTED_MANIFEST_URL, '/root/repository',
Don Garrettf324bc32017-05-23 14:00:53 -0700228 git_cache_dir='/git-cache', branch='branch')])
229
Don Garrett7ade05a2017-02-17 13:31:47 -0800230 # Ensure we clean, as expected.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700231 self.assertEqual(mock_clean.mock_calls, [
Don Garrettd1d90dd2017-06-13 17:35:52 -0700232 mock.call('/root',
233 mock_repo,
234 {
235 'branch_name': 'branch',
236 'tryjob': True,
237 'build_config': 'config',
238 })])
Don Garrett7ade05a2017-02-17 13:31:47 -0800239
Don Garrett86881cb2017-02-15 15:41:55 -0800240 # Ensure we checkout, as expected.
241 self.assertEqual(mock_checkout.mock_calls,
Don Garrettf324bc32017-05-23 14:00:53 -0700242 [mock.call(mock_repo)])
Don Garrett86881cb2017-02-15 15:41:55 -0800243
244 # Ensure we invoke cbuildbot, as expected.
245 self.assertCommandCalled(
Don Garrett5cd946b2017-07-20 13:42:20 -0700246 [
247 '/root/repository/chromite/bin/cbuildbot',
248 'config',
249 '--buildroot', '/root/repository',
250 '--branch', 'branch',
251 '--git-cache-dir', '/git-cache',
252 '--remote-trybot',
253 '--ts-mon-task-num', '1',
254 ],
Don Garretta50bf492017-09-28 18:33:02 -0700255 extra_env={'PATH': mock.ANY},
Don Garrettbf90cdf2017-05-19 15:54:02 -0700256 cwd='/root/repository',
Don Garrettacbb2392017-05-11 18:27:41 -0700257 error_code_ok=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800258
259
Don Garrettbf90cdf2017-05-19 15:54:02 -0700260class CleanBuildRootTest(cros_test_lib.MockTempDirTestCase):
261 """Tests for CleanBuildRoot method."""
Don Garrett7ade05a2017-02-17 13:31:47 -0800262
263 def setUp(self):
264 """Create standard buildroot contents for cleanup."""
Don Garrettbf90cdf2017-05-19 15:54:02 -0700265 self.root = os.path.join(self.tempdir)
Don Garrette17e1d92017-04-12 15:28:19 -0700266 self.state = os.path.join(self.root, '.cbuildbot_launch_state')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700267 self.buildroot = os.path.join(self.root, 'buildroot')
268 self.repo = os.path.join(self.buildroot, '.repo/repo')
269 self.chroot = os.path.join(self.buildroot, 'chroot/chroot')
270 self.general = os.path.join(self.buildroot, 'general/general')
Don Garrett39963602017-02-27 14:41:58 -0800271 # TODO: Add .cache, and distfiles.
Don Garrett7ade05a2017-02-17 13:31:47 -0800272
Don Garrettf324bc32017-05-23 14:00:53 -0700273 self.mock_repo = mock.MagicMock()
Don Garrettbf90cdf2017-05-19 15:54:02 -0700274 self.mock_repo.directory = self.buildroot
Don Garrettf324bc32017-05-23 14:00:53 -0700275
Don Garrettacbb2392017-05-11 18:27:41 -0700276 self.metrics = {}
277
Don Garrett60967922017-04-12 18:51:44 -0700278 def populateBuildroot(self, state=None):
Don Garrett7ade05a2017-02-17 13:31:47 -0800279 """Create standard buildroot contents for cleanup."""
280 if state:
Don Garrettbf90cdf2017-05-19 15:54:02 -0700281 osutils.SafeMakedirs(self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800282 osutils.WriteFile(self.state, state)
283
284 # Create files.
285 for f in (self.repo, self.chroot, self.general):
Don Garrette17e1d92017-04-12 15:28:19 -0700286 osutils.Touch(f, makedirs=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800287
Don Garrette17e1d92017-04-12 15:28:19 -0700288 def testNoBuildroot(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700289 """Test CleanBuildRoot with no history."""
Don Garrettf324bc32017-05-23 14:00:53 -0700290 self.mock_repo.branch = 'master'
291
Don Garrettbf90cdf2017-05-19 15:54:02 -0700292 cbuildbot_launch.CleanBuildRoot(
293 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800294
Don Garrettbf90cdf2017-05-19 15:54:02 -0700295 self.assertEqual(osutils.ReadFile(self.state), '2 master')
Don Garrett7ade05a2017-02-17 13:31:47 -0800296
297 def testBuildrootNoState(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700298 """Test CleanBuildRoot with no state information."""
Don Garrett7ade05a2017-02-17 13:31:47 -0800299 self.populateBuildroot()
Don Garrettf324bc32017-05-23 14:00:53 -0700300 self.mock_repo.branch = 'master'
Don Garrett7ade05a2017-02-17 13:31:47 -0800301
Don Garrettbf90cdf2017-05-19 15:54:02 -0700302 cbuildbot_launch.CleanBuildRoot(
303 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800304
Don Garrettbf90cdf2017-05-19 15:54:02 -0700305 self.assertEqual(osutils.ReadFile(self.state), '2 master')
Don Garrett60967922017-04-12 18:51:44 -0700306 self.assertNotExists(self.repo)
Don Garrett7ade05a2017-02-17 13:31:47 -0800307 self.assertNotExists(self.chroot)
Don Garrett60967922017-04-12 18:51:44 -0700308 self.assertNotExists(self.general)
309
310 def testBuildrootFormatMismatch(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700311 """Test CleanBuildRoot with no state information."""
Don Garrett125d4dc2017-04-25 16:26:03 -0700312 self.populateBuildroot('0 master')
Don Garrettf324bc32017-05-23 14:00:53 -0700313 self.mock_repo.branch = 'master'
Don Garrett60967922017-04-12 18:51:44 -0700314
Don Garrettbf90cdf2017-05-19 15:54:02 -0700315 cbuildbot_launch.CleanBuildRoot(
316 self.root, self.mock_repo, self.metrics)
Don Garrett60967922017-04-12 18:51:44 -0700317
Don Garrettbf90cdf2017-05-19 15:54:02 -0700318 self.assertEqual(osutils.ReadFile(self.state), '2 master')
Don Garrett60967922017-04-12 18:51:44 -0700319 self.assertNotExists(self.repo)
320 self.assertNotExists(self.chroot)
321 self.assertNotExists(self.general)
Don Garrett7ade05a2017-02-17 13:31:47 -0800322
323 def testBuildrootBranchChange(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700324 """Test CleanBuildRoot with a change in branches."""
325 self.populateBuildroot('2 branchA')
Don Garrettf324bc32017-05-23 14:00:53 -0700326 self.mock_repo.branch = 'branchB'
Benjamin Gordon59ba2f82017-08-28 15:31:06 -0600327 m = self.PatchObject(cros_build_lib, 'CleanupChrootMount')
Don Garrett7ade05a2017-02-17 13:31:47 -0800328
Don Garrettbf90cdf2017-05-19 15:54:02 -0700329 cbuildbot_launch.CleanBuildRoot(
330 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800331
Don Garrettbf90cdf2017-05-19 15:54:02 -0700332 self.assertEqual(osutils.ReadFile(self.state), '2 branchB')
Don Garrett7ade05a2017-02-17 13:31:47 -0800333 self.assertExists(self.repo)
334 self.assertNotExists(self.chroot)
335 self.assertExists(self.general)
Benjamin Gordon59ba2f82017-08-28 15:31:06 -0600336 m.assert_called()
Don Garrett7ade05a2017-02-17 13:31:47 -0800337
338 def testBuildrootBranchMatch(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700339 """Test CleanBuildRoot with no change in branch."""
340 self.populateBuildroot('2 branchA')
Don Garrettf324bc32017-05-23 14:00:53 -0700341 self.mock_repo.branch = 'branchA'
Don Garrett7ade05a2017-02-17 13:31:47 -0800342
Don Garrettbf90cdf2017-05-19 15:54:02 -0700343 cbuildbot_launch.CleanBuildRoot(
344 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800345
Don Garrettbf90cdf2017-05-19 15:54:02 -0700346 self.assertEqual(osutils.ReadFile(self.state), '2 branchA')
Don Garrett7ade05a2017-02-17 13:31:47 -0800347 self.assertExists(self.repo)
348 self.assertExists(self.chroot)
349 self.assertExists(self.general)
Don Garrette17e1d92017-04-12 15:28:19 -0700350
Don Garrettf324bc32017-05-23 14:00:53 -0700351 def testBuildrootRepoCleanFailure(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700352 """Test CleanBuildRoot with repo checkout failure."""
Don Garrettf324bc32017-05-23 14:00:53 -0700353 self.populateBuildroot('1 branchA')
354 self.mock_repo.branch = 'branchA'
355 self.mock_repo.BuildRootGitCleanup.side_effect = Exception
356
Don Garrettbf90cdf2017-05-19 15:54:02 -0700357 cbuildbot_launch.CleanBuildRoot(
358 self.root, self.mock_repo, self.metrics)
Don Garrettf324bc32017-05-23 14:00:53 -0700359
Don Garrettbf90cdf2017-05-19 15:54:02 -0700360 self.assertEqual(osutils.ReadFile(self.state), '2 branchA')
Don Garrettf324bc32017-05-23 14:00:53 -0700361 self.assertNotExists(self.repo)
362 self.assertNotExists(self.chroot)
363 self.assertNotExists(self.general)
364
Don Garrettbf90cdf2017-05-19 15:54:02 -0700365 def testGetState(self):
366 """Test GetState."""
Don Garrett60967922017-04-12 18:51:44 -0700367 # No root dir.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700368 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700369 self.assertEqual(results, (0, ''))
370
371 # Empty root dir.
372 osutils.SafeMakedirs(self.root)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700373 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700374 self.assertEqual(results, (0, ''))
375
376 # Empty Contents
377 osutils.WriteFile(self.state, '')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700378 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700379 self.assertEqual(results, (0, ''))
380
381 # Old Format Contents
382 osutils.WriteFile(self.state, 'happy-branch')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700383 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700384 self.assertEqual(results, (0, ''))
385
386 # Expected Contents
387 osutils.WriteFile(self.state, '1 happy-branch')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700388 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700389 self.assertEqual(results, (1, 'happy-branch'))
390
391 # Future Contents
392 osutils.WriteFile(self.state, '22 master')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700393 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700394 self.assertEqual(results, (22, 'master'))
395
396 # Read Write
Don Garrettbf90cdf2017-05-19 15:54:02 -0700397 cbuildbot_launch.SetState('happy-branch', self.root)
398 results = cbuildbot_launch.GetState(self.root)
399 self.assertEqual(results, (2, 'happy-branch'))
Don Garrett60967922017-04-12 18:51:44 -0700400
Don Garrettbf90cdf2017-05-19 15:54:02 -0700401 def testSetState(self):
402 """Test SetState."""
Don Garrett60967922017-04-12 18:51:44 -0700403 # Write out a state file.
404 osutils.SafeMakedirs(self.root)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700405 cbuildbot_launch.SetState('happy-branch', self.root)
406 self.assertEqual(osutils.ReadFile(self.state), '2 happy-branch')
Don Garrett60967922017-04-12 18:51:44 -0700407
408 # Change to a future version.
409 self.PatchObject(cbuildbot_launch, 'BUILDROOT_BUILDROOT_LAYOUT', 22)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700410 cbuildbot_launch.SetState('happy-branch', self.root)
Don Garrett60967922017-04-12 18:51:44 -0700411 self.assertEqual(osutils.ReadFile(self.state), '22 happy-branch')