blob: 612a707bdf1a23f4110ad6068306ef653b3ddc2a [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 Garrett597ddff2017-02-17 18:29:37 -080091class RunTests(cros_build_lib_unittest.RunCommandTestCase):
Don Garrett0c54ed72017-03-03 11:18:57 -080092 """Tests for cbuildbot_launch script."""
Don Garrett597ddff2017-02-17 18:29:37 -080093
94 ARGS_BASE = ['--buildroot', '/buildroot']
Don Garrett5cd946b2017-07-20 13:42:20 -070095 EXPECTED_ARGS_BASE = ['--buildroot', '/cbuildbot_buildroot']
Don Garrett597ddff2017-02-17 18:29:37 -080096 ARGS_GIT_CACHE = ['--git-cache-dir', '/git-cache']
97 ARGS_CONFIG = ['config']
Don Garrettbf90cdf2017-05-19 15:54:02 -070098 CMD = ['/cbuildbot_buildroot/chromite/bin/cbuildbot']
Don Garrett597ddff2017-02-17 18:29:37 -080099
100 def verifyRunCbuildbot(self, args, expected_cmd, version):
Don Garrett86881cb2017-02-15 15:41:55 -0800101 """Ensure we invoke cbuildbot correctly."""
Don Garrett597ddff2017-02-17 18:29:37 -0800102 self.PatchObject(
103 cros_build_lib, 'GetTargetChromiteApiVersion', autospec=True,
104 return_value=version)
105
Don Garretta50bf492017-09-28 18:33:02 -0700106 cbuildbot_launch.RunCbuildbot('/cbuildbot_buildroot', '/depot_tools', args)
Don Garrett597ddff2017-02-17 18:29:37 -0800107
108 self.assertCommandCalled(
Don Garretta50bf492017-09-28 18:33:02 -0700109 expected_cmd, extra_env={'PATH': mock.ANY},
110 cwd='/cbuildbot_buildroot', error_code_ok=True)
Don Garrett597ddff2017-02-17 18:29:37 -0800111
112 def testRunCbuildbotSimple(self):
113 """Ensure we invoke cbuildbot correctly."""
114 self.verifyRunCbuildbot(
115 self.ARGS_BASE + self.ARGS_CONFIG,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700116 self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE,
Don Garrett597ddff2017-02-17 18:29:37 -0800117 (0, 4))
118
119 def testRunCbuildbotNotFiltered(self):
120 """Ensure we invoke cbuildbot correctly."""
121 self.verifyRunCbuildbot(
122 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700123 (self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE +
124 self.ARGS_GIT_CACHE),
Don Garrett597ddff2017-02-17 18:29:37 -0800125 (0, 4))
126
127 def testRunCbuildbotFiltered(self):
128 """Ensure we invoke cbuildbot correctly."""
129 self.verifyRunCbuildbot(
130 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700131 self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE,
Don Garrett597ddff2017-02-17 18:29:37 -0800132 (0, 2))
Don Garrettc4114cc2016-11-01 20:04:06 -0700133
Don Garrett86881cb2017-02-15 15:41:55 -0800134 def testMainMin(self):
135 """Test a minimal set of command line options."""
Don Garrett597ddff2017-02-17 18:29:37 -0800136 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
137 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
Don Garrett861e9182017-05-15 15:30:23 -0700138 autospec=True, return_value=(constants.REEXEC_API_MAJOR,
139 constants.REEXEC_API_MINOR))
Don Garrettf324bc32017-05-23 14:00:53 -0700140 mock_repo = mock.MagicMock()
141 mock_repo.branch = 'master'
Don Garrettbf90cdf2017-05-19 15:54:02 -0700142 mock_repo.directory = '/root/repository'
143
Don Garrettf324bc32017-05-23 14:00:53 -0700144 mock_repo_create = self.PatchObject(repository, 'RepoRepository',
145 autospec=True, return_value=mock_repo)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700146 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot',
Don Garrett0c54ed72017-03-03 11:18:57 -0800147 autospec=True)
148 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800149 autospec=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800150
Don Garrettbf90cdf2017-05-19 15:54:02 -0700151 cbuildbot_launch._main(['-r', '/root', 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700152
Don Garrettf324bc32017-05-23 14:00:53 -0700153 # Did we create the repo instance correctly?
154 self.assertEqual(mock_repo_create.mock_calls,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700155 [mock.call(EXPECTED_MANIFEST_URL, '/root/repository',
Don Garrettf324bc32017-05-23 14:00:53 -0700156 git_cache_dir=None, branch='master')])
157
Don Garrett7ade05a2017-02-17 13:31:47 -0800158 # Ensure we clean, as expected.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700159 self.assertEqual(mock_clean.mock_calls, [
Don Garrettd1d90dd2017-06-13 17:35:52 -0700160 mock.call('/root', mock_repo,
161 {
162 'branch_name': 'master',
163 'tryjob': False,
164 'build_config': 'config',
165 })])
Don Garrett7ade05a2017-02-17 13:31:47 -0800166
Don Garrett86881cb2017-02-15 15:41:55 -0800167 # Ensure we checkout, as expected.
168 self.assertEqual(mock_checkout.mock_calls,
Don Garrettf324bc32017-05-23 14:00:53 -0700169 [mock.call(mock_repo)])
Don Garrettc4114cc2016-11-01 20:04:06 -0700170
Don Garrett86881cb2017-02-15 15:41:55 -0800171 # Ensure we invoke cbuildbot, as expected.
172 self.assertCommandCalled(
Don Garrett5cd946b2017-07-20 13:42:20 -0700173 [
174 '/root/repository/chromite/bin/cbuildbot',
175 'config',
176 '-r', '/root/repository',
177 '--ts-mon-task-num', '1',
178 ],
Don Garretta50bf492017-09-28 18:33:02 -0700179 extra_env={'PATH': mock.ANY},
Don Garrettbf90cdf2017-05-19 15:54:02 -0700180 cwd='/root/repository',
Don Garrettacbb2392017-05-11 18:27:41 -0700181 error_code_ok=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700182
Don Garrett86881cb2017-02-15 15:41:55 -0800183 def testMainMax(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800184 """Test a larger set of command line options."""
185 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
186 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
Don Garrett861e9182017-05-15 15:30:23 -0700187 autospec=True, return_value=(constants.REEXEC_API_MAJOR,
188 constants.REEXEC_API_MINOR))
Don Garrettf324bc32017-05-23 14:00:53 -0700189 mock_repo = mock.MagicMock()
190 mock_repo.branch = 'branch'
Don Garrettbf90cdf2017-05-19 15:54:02 -0700191 mock_repo.directory = '/root/repository'
192
Don Garrettf324bc32017-05-23 14:00:53 -0700193 mock_repo_create = self.PatchObject(repository, 'RepoRepository',
194 autospec=True, return_value=mock_repo)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700195 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot',
Don Garrett0c54ed72017-03-03 11:18:57 -0800196 autospec=True)
197 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800198 autospec=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700199
Don Garrettbf90cdf2017-05-19 15:54:02 -0700200 cbuildbot_launch._main(['--buildroot', '/root',
Don Garrettacbb2392017-05-11 18:27:41 -0700201 '--branch', 'branch',
202 '--git-cache-dir', '/git-cache',
Don Garrettd1d90dd2017-06-13 17:35:52 -0700203 '--remote-trybot',
Don Garrettacbb2392017-05-11 18:27:41 -0700204 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700205
Don Garrettf324bc32017-05-23 14:00:53 -0700206 # Did we create the repo instance correctly?
207 self.assertEqual(mock_repo_create.mock_calls,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700208 [mock.call(EXPECTED_MANIFEST_URL, '/root/repository',
Don Garrettf324bc32017-05-23 14:00:53 -0700209 git_cache_dir='/git-cache', branch='branch')])
210
Don Garrett7ade05a2017-02-17 13:31:47 -0800211 # Ensure we clean, as expected.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700212 self.assertEqual(mock_clean.mock_calls, [
Don Garrettd1d90dd2017-06-13 17:35:52 -0700213 mock.call('/root',
214 mock_repo,
215 {
216 'branch_name': 'branch',
217 'tryjob': True,
218 'build_config': 'config',
219 })])
Don Garrett7ade05a2017-02-17 13:31:47 -0800220
Don Garrett86881cb2017-02-15 15:41:55 -0800221 # Ensure we checkout, as expected.
222 self.assertEqual(mock_checkout.mock_calls,
Don Garrettf324bc32017-05-23 14:00:53 -0700223 [mock.call(mock_repo)])
Don Garrett86881cb2017-02-15 15:41:55 -0800224
225 # Ensure we invoke cbuildbot, as expected.
226 self.assertCommandCalled(
Don Garrett5cd946b2017-07-20 13:42:20 -0700227 [
228 '/root/repository/chromite/bin/cbuildbot',
229 'config',
230 '--buildroot', '/root/repository',
231 '--branch', 'branch',
232 '--git-cache-dir', '/git-cache',
233 '--remote-trybot',
234 '--ts-mon-task-num', '1',
235 ],
Don Garretta50bf492017-09-28 18:33:02 -0700236 extra_env={'PATH': mock.ANY},
Don Garrettbf90cdf2017-05-19 15:54:02 -0700237 cwd='/root/repository',
Don Garrettacbb2392017-05-11 18:27:41 -0700238 error_code_ok=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800239
240
Don Garrettbf90cdf2017-05-19 15:54:02 -0700241class CleanBuildRootTest(cros_test_lib.MockTempDirTestCase):
242 """Tests for CleanBuildRoot method."""
Don Garrett7ade05a2017-02-17 13:31:47 -0800243
244 def setUp(self):
245 """Create standard buildroot contents for cleanup."""
Don Garrettbf90cdf2017-05-19 15:54:02 -0700246 self.root = os.path.join(self.tempdir)
Don Garrette17e1d92017-04-12 15:28:19 -0700247 self.state = os.path.join(self.root, '.cbuildbot_launch_state')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700248 self.buildroot = os.path.join(self.root, 'buildroot')
249 self.repo = os.path.join(self.buildroot, '.repo/repo')
250 self.chroot = os.path.join(self.buildroot, 'chroot/chroot')
251 self.general = os.path.join(self.buildroot, 'general/general')
Don Garrett39963602017-02-27 14:41:58 -0800252 # TODO: Add .cache, and distfiles.
Don Garrett7ade05a2017-02-17 13:31:47 -0800253
Don Garrettf324bc32017-05-23 14:00:53 -0700254 self.mock_repo = mock.MagicMock()
Don Garrettbf90cdf2017-05-19 15:54:02 -0700255 self.mock_repo.directory = self.buildroot
Don Garrettf324bc32017-05-23 14:00:53 -0700256
Don Garrettacbb2392017-05-11 18:27:41 -0700257 self.metrics = {}
258
Don Garrett60967922017-04-12 18:51:44 -0700259 def populateBuildroot(self, state=None):
Don Garrett7ade05a2017-02-17 13:31:47 -0800260 """Create standard buildroot contents for cleanup."""
261 if state:
Don Garrettbf90cdf2017-05-19 15:54:02 -0700262 osutils.SafeMakedirs(self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800263 osutils.WriteFile(self.state, state)
264
265 # Create files.
266 for f in (self.repo, self.chroot, self.general):
Don Garrette17e1d92017-04-12 15:28:19 -0700267 osutils.Touch(f, makedirs=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800268
Don Garrette17e1d92017-04-12 15:28:19 -0700269 def testNoBuildroot(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700270 """Test CleanBuildRoot with no history."""
Don Garrettf324bc32017-05-23 14:00:53 -0700271 self.mock_repo.branch = 'master'
272
Don Garrettbf90cdf2017-05-19 15:54:02 -0700273 cbuildbot_launch.CleanBuildRoot(
274 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800275
Don Garrettbf90cdf2017-05-19 15:54:02 -0700276 self.assertEqual(osutils.ReadFile(self.state), '2 master')
Don Garrett7ade05a2017-02-17 13:31:47 -0800277
278 def testBuildrootNoState(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700279 """Test CleanBuildRoot with no state information."""
Don Garrett7ade05a2017-02-17 13:31:47 -0800280 self.populateBuildroot()
Don Garrettf324bc32017-05-23 14:00:53 -0700281 self.mock_repo.branch = 'master'
Don Garrett7ade05a2017-02-17 13:31:47 -0800282
Don Garrettbf90cdf2017-05-19 15:54:02 -0700283 cbuildbot_launch.CleanBuildRoot(
284 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800285
Don Garrettbf90cdf2017-05-19 15:54:02 -0700286 self.assertEqual(osutils.ReadFile(self.state), '2 master')
Don Garrett60967922017-04-12 18:51:44 -0700287 self.assertNotExists(self.repo)
Don Garrett7ade05a2017-02-17 13:31:47 -0800288 self.assertNotExists(self.chroot)
Don Garrett60967922017-04-12 18:51:44 -0700289 self.assertNotExists(self.general)
290
291 def testBuildrootFormatMismatch(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700292 """Test CleanBuildRoot with no state information."""
Don Garrett125d4dc2017-04-25 16:26:03 -0700293 self.populateBuildroot('0 master')
Don Garrettf324bc32017-05-23 14:00:53 -0700294 self.mock_repo.branch = 'master'
Don Garrett60967922017-04-12 18:51:44 -0700295
Don Garrettbf90cdf2017-05-19 15:54:02 -0700296 cbuildbot_launch.CleanBuildRoot(
297 self.root, self.mock_repo, self.metrics)
Don Garrett60967922017-04-12 18:51:44 -0700298
Don Garrettbf90cdf2017-05-19 15:54:02 -0700299 self.assertEqual(osutils.ReadFile(self.state), '2 master')
Don Garrett60967922017-04-12 18:51:44 -0700300 self.assertNotExists(self.repo)
301 self.assertNotExists(self.chroot)
302 self.assertNotExists(self.general)
Don Garrett7ade05a2017-02-17 13:31:47 -0800303
304 def testBuildrootBranchChange(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700305 """Test CleanBuildRoot with a change in branches."""
306 self.populateBuildroot('2 branchA')
Don Garrettf324bc32017-05-23 14:00:53 -0700307 self.mock_repo.branch = 'branchB'
Benjamin Gordon59ba2f82017-08-28 15:31:06 -0600308 m = self.PatchObject(cros_build_lib, 'CleanupChrootMount')
Don Garrett7ade05a2017-02-17 13:31:47 -0800309
Don Garrettbf90cdf2017-05-19 15:54:02 -0700310 cbuildbot_launch.CleanBuildRoot(
311 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800312
Don Garrettbf90cdf2017-05-19 15:54:02 -0700313 self.assertEqual(osutils.ReadFile(self.state), '2 branchB')
Don Garrett7ade05a2017-02-17 13:31:47 -0800314 self.assertExists(self.repo)
315 self.assertNotExists(self.chroot)
316 self.assertExists(self.general)
Benjamin Gordon59ba2f82017-08-28 15:31:06 -0600317 m.assert_called()
Don Garrett7ade05a2017-02-17 13:31:47 -0800318
319 def testBuildrootBranchMatch(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700320 """Test CleanBuildRoot with no change in branch."""
321 self.populateBuildroot('2 branchA')
Don Garrettf324bc32017-05-23 14:00:53 -0700322 self.mock_repo.branch = 'branchA'
Don Garrett7ade05a2017-02-17 13:31:47 -0800323
Don Garrettbf90cdf2017-05-19 15:54:02 -0700324 cbuildbot_launch.CleanBuildRoot(
325 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800326
Don Garrettbf90cdf2017-05-19 15:54:02 -0700327 self.assertEqual(osutils.ReadFile(self.state), '2 branchA')
Don Garrett7ade05a2017-02-17 13:31:47 -0800328 self.assertExists(self.repo)
329 self.assertExists(self.chroot)
330 self.assertExists(self.general)
Don Garrette17e1d92017-04-12 15:28:19 -0700331
Don Garrettf324bc32017-05-23 14:00:53 -0700332 def testBuildrootRepoCleanFailure(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700333 """Test CleanBuildRoot with repo checkout failure."""
Don Garrettf324bc32017-05-23 14:00:53 -0700334 self.populateBuildroot('1 branchA')
335 self.mock_repo.branch = 'branchA'
336 self.mock_repo.BuildRootGitCleanup.side_effect = Exception
337
Don Garrettbf90cdf2017-05-19 15:54:02 -0700338 cbuildbot_launch.CleanBuildRoot(
339 self.root, self.mock_repo, self.metrics)
Don Garrettf324bc32017-05-23 14:00:53 -0700340
Don Garrettbf90cdf2017-05-19 15:54:02 -0700341 self.assertEqual(osutils.ReadFile(self.state), '2 branchA')
Don Garrettf324bc32017-05-23 14:00:53 -0700342 self.assertNotExists(self.repo)
343 self.assertNotExists(self.chroot)
344 self.assertNotExists(self.general)
345
Don Garrettbf90cdf2017-05-19 15:54:02 -0700346 def testGetState(self):
347 """Test GetState."""
Don Garrett60967922017-04-12 18:51:44 -0700348 # No root dir.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700349 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700350 self.assertEqual(results, (0, ''))
351
352 # Empty root dir.
353 osutils.SafeMakedirs(self.root)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700354 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700355 self.assertEqual(results, (0, ''))
356
357 # Empty Contents
358 osutils.WriteFile(self.state, '')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700359 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700360 self.assertEqual(results, (0, ''))
361
362 # Old Format Contents
363 osutils.WriteFile(self.state, 'happy-branch')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700364 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700365 self.assertEqual(results, (0, ''))
366
367 # Expected Contents
368 osutils.WriteFile(self.state, '1 happy-branch')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700369 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700370 self.assertEqual(results, (1, 'happy-branch'))
371
372 # Future Contents
373 osutils.WriteFile(self.state, '22 master')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700374 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700375 self.assertEqual(results, (22, 'master'))
376
377 # Read Write
Don Garrettbf90cdf2017-05-19 15:54:02 -0700378 cbuildbot_launch.SetState('happy-branch', self.root)
379 results = cbuildbot_launch.GetState(self.root)
380 self.assertEqual(results, (2, 'happy-branch'))
Don Garrett60967922017-04-12 18:51:44 -0700381
Don Garrettbf90cdf2017-05-19 15:54:02 -0700382 def testSetState(self):
383 """Test SetState."""
Don Garrett60967922017-04-12 18:51:44 -0700384 # Write out a state file.
385 osutils.SafeMakedirs(self.root)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700386 cbuildbot_launch.SetState('happy-branch', self.root)
387 self.assertEqual(osutils.ReadFile(self.state), '2 happy-branch')
Don Garrett60967922017-04-12 18:51:44 -0700388
389 # Change to a future version.
390 self.PatchObject(cbuildbot_launch, 'BUILDROOT_BUILDROOT_LAYOUT', 22)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700391 cbuildbot_launch.SetState('happy-branch', self.root)
Don Garrett60967922017-04-12 18:51:44 -0700392 self.assertEqual(osutils.ReadFile(self.state), '22 happy-branch')