blob: 60548c5d68d7ca7266183978c2dd3fb131b0f163 [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 Garrettd1d90dd2017-06-13 17:35:52 -0700106 cbuildbot_launch.RunCbuildbot('/cbuildbot_buildroot', args)
Don Garrett597ddff2017-02-17 18:29:37 -0800107
108 self.assertCommandCalled(
Don Garrettbf90cdf2017-05-19 15:54:02 -0700109 expected_cmd, cwd='/cbuildbot_buildroot', error_code_ok=True)
Don Garrett597ddff2017-02-17 18:29:37 -0800110
111 def testRunCbuildbotSimple(self):
112 """Ensure we invoke cbuildbot correctly."""
113 self.verifyRunCbuildbot(
114 self.ARGS_BASE + self.ARGS_CONFIG,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700115 self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE,
Don Garrett597ddff2017-02-17 18:29:37 -0800116 (0, 4))
117
118 def testRunCbuildbotNotFiltered(self):
119 """Ensure we invoke cbuildbot correctly."""
120 self.verifyRunCbuildbot(
121 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700122 (self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE +
123 self.ARGS_GIT_CACHE),
Don Garrett597ddff2017-02-17 18:29:37 -0800124 (0, 4))
125
126 def testRunCbuildbotFiltered(self):
127 """Ensure we invoke cbuildbot correctly."""
128 self.verifyRunCbuildbot(
129 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700130 self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE,
Don Garrett597ddff2017-02-17 18:29:37 -0800131 (0, 2))
Don Garrettc4114cc2016-11-01 20:04:06 -0700132
Don Garrett86881cb2017-02-15 15:41:55 -0800133 def testMainMin(self):
134 """Test a minimal set of command line options."""
Don Garrett597ddff2017-02-17 18:29:37 -0800135 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
136 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
Don Garrett861e9182017-05-15 15:30:23 -0700137 autospec=True, return_value=(constants.REEXEC_API_MAJOR,
138 constants.REEXEC_API_MINOR))
Don Garrettf324bc32017-05-23 14:00:53 -0700139 mock_repo = mock.MagicMock()
140 mock_repo.branch = 'master'
Don Garrettbf90cdf2017-05-19 15:54:02 -0700141 mock_repo.directory = '/root/repository'
142
Don Garrettf324bc32017-05-23 14:00:53 -0700143 mock_repo_create = self.PatchObject(repository, 'RepoRepository',
144 autospec=True, return_value=mock_repo)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700145 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot',
Don Garrett0c54ed72017-03-03 11:18:57 -0800146 autospec=True)
147 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800148 autospec=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800149
Don Garrettbf90cdf2017-05-19 15:54:02 -0700150 cbuildbot_launch._main(['-r', '/root', 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700151
Don Garrettf324bc32017-05-23 14:00:53 -0700152 # Did we create the repo instance correctly?
153 self.assertEqual(mock_repo_create.mock_calls,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700154 [mock.call(EXPECTED_MANIFEST_URL, '/root/repository',
Don Garrettf324bc32017-05-23 14:00:53 -0700155 git_cache_dir=None, branch='master')])
156
Don Garrett7ade05a2017-02-17 13:31:47 -0800157 # Ensure we clean, as expected.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700158 self.assertEqual(mock_clean.mock_calls, [
Don Garrettd1d90dd2017-06-13 17:35:52 -0700159 mock.call('/root', mock_repo,
160 {
161 'branch_name': 'master',
162 'tryjob': False,
163 'build_config': 'config',
164 })])
Don Garrett7ade05a2017-02-17 13:31:47 -0800165
Don Garrett86881cb2017-02-15 15:41:55 -0800166 # Ensure we checkout, as expected.
167 self.assertEqual(mock_checkout.mock_calls,
Don Garrettf324bc32017-05-23 14:00:53 -0700168 [mock.call(mock_repo)])
Don Garrettc4114cc2016-11-01 20:04:06 -0700169
Don Garrett86881cb2017-02-15 15:41:55 -0800170 # Ensure we invoke cbuildbot, as expected.
171 self.assertCommandCalled(
Don Garrett5cd946b2017-07-20 13:42:20 -0700172 [
173 '/root/repository/chromite/bin/cbuildbot',
174 'config',
175 '-r', '/root/repository',
176 '--ts-mon-task-num', '1',
177 ],
Don Garrettbf90cdf2017-05-19 15:54:02 -0700178 cwd='/root/repository',
Don Garrettacbb2392017-05-11 18:27:41 -0700179 error_code_ok=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700180
Don Garrett86881cb2017-02-15 15:41:55 -0800181 def testMainMax(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800182 """Test a larger set of command line options."""
183 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
184 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
Don Garrett861e9182017-05-15 15:30:23 -0700185 autospec=True, return_value=(constants.REEXEC_API_MAJOR,
186 constants.REEXEC_API_MINOR))
Don Garrettf324bc32017-05-23 14:00:53 -0700187 mock_repo = mock.MagicMock()
188 mock_repo.branch = 'branch'
Don Garrettbf90cdf2017-05-19 15:54:02 -0700189 mock_repo.directory = '/root/repository'
190
Don Garrettf324bc32017-05-23 14:00:53 -0700191 mock_repo_create = self.PatchObject(repository, 'RepoRepository',
192 autospec=True, return_value=mock_repo)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700193 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot',
Don Garrett0c54ed72017-03-03 11:18:57 -0800194 autospec=True)
195 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800196 autospec=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700197
Don Garrettbf90cdf2017-05-19 15:54:02 -0700198 cbuildbot_launch._main(['--buildroot', '/root',
Don Garrettacbb2392017-05-11 18:27:41 -0700199 '--branch', 'branch',
200 '--git-cache-dir', '/git-cache',
Don Garrettd1d90dd2017-06-13 17:35:52 -0700201 '--remote-trybot',
Don Garrettacbb2392017-05-11 18:27:41 -0700202 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700203
Don Garrettf324bc32017-05-23 14:00:53 -0700204 # Did we create the repo instance correctly?
205 self.assertEqual(mock_repo_create.mock_calls,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700206 [mock.call(EXPECTED_MANIFEST_URL, '/root/repository',
Don Garrettf324bc32017-05-23 14:00:53 -0700207 git_cache_dir='/git-cache', branch='branch')])
208
Don Garrett7ade05a2017-02-17 13:31:47 -0800209 # Ensure we clean, as expected.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700210 self.assertEqual(mock_clean.mock_calls, [
Don Garrettd1d90dd2017-06-13 17:35:52 -0700211 mock.call('/root',
212 mock_repo,
213 {
214 'branch_name': 'branch',
215 'tryjob': True,
216 'build_config': 'config',
217 })])
Don Garrett7ade05a2017-02-17 13:31:47 -0800218
Don Garrett86881cb2017-02-15 15:41:55 -0800219 # Ensure we checkout, as expected.
220 self.assertEqual(mock_checkout.mock_calls,
Don Garrettf324bc32017-05-23 14:00:53 -0700221 [mock.call(mock_repo)])
Don Garrett86881cb2017-02-15 15:41:55 -0800222
223 # Ensure we invoke cbuildbot, as expected.
224 self.assertCommandCalled(
Don Garrett5cd946b2017-07-20 13:42:20 -0700225 [
226 '/root/repository/chromite/bin/cbuildbot',
227 'config',
228 '--buildroot', '/root/repository',
229 '--branch', 'branch',
230 '--git-cache-dir', '/git-cache',
231 '--remote-trybot',
232 '--ts-mon-task-num', '1',
233 ],
Don Garrettbf90cdf2017-05-19 15:54:02 -0700234 cwd='/root/repository',
Don Garrettacbb2392017-05-11 18:27:41 -0700235 error_code_ok=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800236
237
Don Garrettbf90cdf2017-05-19 15:54:02 -0700238class CleanBuildRootTest(cros_test_lib.MockTempDirTestCase):
239 """Tests for CleanBuildRoot method."""
Don Garrett7ade05a2017-02-17 13:31:47 -0800240
241 def setUp(self):
242 """Create standard buildroot contents for cleanup."""
Don Garrettbf90cdf2017-05-19 15:54:02 -0700243 self.root = os.path.join(self.tempdir)
Don Garrette17e1d92017-04-12 15:28:19 -0700244 self.state = os.path.join(self.root, '.cbuildbot_launch_state')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700245 self.buildroot = os.path.join(self.root, 'buildroot')
246 self.repo = os.path.join(self.buildroot, '.repo/repo')
247 self.chroot = os.path.join(self.buildroot, 'chroot/chroot')
248 self.general = os.path.join(self.buildroot, 'general/general')
Don Garrett39963602017-02-27 14:41:58 -0800249 # TODO: Add .cache, and distfiles.
Don Garrett7ade05a2017-02-17 13:31:47 -0800250
Don Garrettf324bc32017-05-23 14:00:53 -0700251 self.mock_repo = mock.MagicMock()
Don Garrettbf90cdf2017-05-19 15:54:02 -0700252 self.mock_repo.directory = self.buildroot
Don Garrettf324bc32017-05-23 14:00:53 -0700253
Don Garrettacbb2392017-05-11 18:27:41 -0700254 self.metrics = {}
255
Don Garrett60967922017-04-12 18:51:44 -0700256 def populateBuildroot(self, state=None):
Don Garrett7ade05a2017-02-17 13:31:47 -0800257 """Create standard buildroot contents for cleanup."""
258 if state:
Don Garrettbf90cdf2017-05-19 15:54:02 -0700259 osutils.SafeMakedirs(self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800260 osutils.WriteFile(self.state, state)
261
262 # Create files.
263 for f in (self.repo, self.chroot, self.general):
Don Garrette17e1d92017-04-12 15:28:19 -0700264 osutils.Touch(f, makedirs=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800265
Don Garrette17e1d92017-04-12 15:28:19 -0700266 def testNoBuildroot(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700267 """Test CleanBuildRoot with no history."""
Don Garrettf324bc32017-05-23 14:00:53 -0700268 self.mock_repo.branch = 'master'
269
Don Garrettbf90cdf2017-05-19 15:54:02 -0700270 cbuildbot_launch.CleanBuildRoot(
271 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800272
Don Garrettbf90cdf2017-05-19 15:54:02 -0700273 self.assertEqual(osutils.ReadFile(self.state), '2 master')
Don Garrett7ade05a2017-02-17 13:31:47 -0800274
275 def testBuildrootNoState(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700276 """Test CleanBuildRoot with no state information."""
Don Garrett7ade05a2017-02-17 13:31:47 -0800277 self.populateBuildroot()
Don Garrettf324bc32017-05-23 14:00:53 -0700278 self.mock_repo.branch = 'master'
Don Garrett7ade05a2017-02-17 13:31:47 -0800279
Don Garrettbf90cdf2017-05-19 15:54:02 -0700280 cbuildbot_launch.CleanBuildRoot(
281 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800282
Don Garrettbf90cdf2017-05-19 15:54:02 -0700283 self.assertEqual(osutils.ReadFile(self.state), '2 master')
Don Garrett60967922017-04-12 18:51:44 -0700284 self.assertNotExists(self.repo)
Don Garrett7ade05a2017-02-17 13:31:47 -0800285 self.assertNotExists(self.chroot)
Don Garrett60967922017-04-12 18:51:44 -0700286 self.assertNotExists(self.general)
287
288 def testBuildrootFormatMismatch(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700289 """Test CleanBuildRoot with no state information."""
Don Garrett125d4dc2017-04-25 16:26:03 -0700290 self.populateBuildroot('0 master')
Don Garrettf324bc32017-05-23 14:00:53 -0700291 self.mock_repo.branch = 'master'
Don Garrett60967922017-04-12 18:51:44 -0700292
Don Garrettbf90cdf2017-05-19 15:54:02 -0700293 cbuildbot_launch.CleanBuildRoot(
294 self.root, self.mock_repo, self.metrics)
Don Garrett60967922017-04-12 18:51:44 -0700295
Don Garrettbf90cdf2017-05-19 15:54:02 -0700296 self.assertEqual(osutils.ReadFile(self.state), '2 master')
Don Garrett60967922017-04-12 18:51:44 -0700297 self.assertNotExists(self.repo)
298 self.assertNotExists(self.chroot)
299 self.assertNotExists(self.general)
Don Garrett7ade05a2017-02-17 13:31:47 -0800300
301 def testBuildrootBranchChange(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700302 """Test CleanBuildRoot with a change in branches."""
303 self.populateBuildroot('2 branchA')
Don Garrettf324bc32017-05-23 14:00:53 -0700304 self.mock_repo.branch = 'branchB'
Benjamin Gordon59ba2f82017-08-28 15:31:06 -0600305 m = self.PatchObject(cros_build_lib, 'CleanupChrootMount')
Don Garrett7ade05a2017-02-17 13:31:47 -0800306
Don Garrettbf90cdf2017-05-19 15:54:02 -0700307 cbuildbot_launch.CleanBuildRoot(
308 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800309
Don Garrettbf90cdf2017-05-19 15:54:02 -0700310 self.assertEqual(osutils.ReadFile(self.state), '2 branchB')
Don Garrett7ade05a2017-02-17 13:31:47 -0800311 self.assertExists(self.repo)
312 self.assertNotExists(self.chroot)
313 self.assertExists(self.general)
Benjamin Gordon59ba2f82017-08-28 15:31:06 -0600314 m.assert_called()
Don Garrett7ade05a2017-02-17 13:31:47 -0800315
316 def testBuildrootBranchMatch(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700317 """Test CleanBuildRoot with no change in branch."""
318 self.populateBuildroot('2 branchA')
Don Garrettf324bc32017-05-23 14:00:53 -0700319 self.mock_repo.branch = 'branchA'
Don Garrett7ade05a2017-02-17 13:31:47 -0800320
Don Garrettbf90cdf2017-05-19 15:54:02 -0700321 cbuildbot_launch.CleanBuildRoot(
322 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800323
Don Garrettbf90cdf2017-05-19 15:54:02 -0700324 self.assertEqual(osutils.ReadFile(self.state), '2 branchA')
Don Garrett7ade05a2017-02-17 13:31:47 -0800325 self.assertExists(self.repo)
326 self.assertExists(self.chroot)
327 self.assertExists(self.general)
Don Garrette17e1d92017-04-12 15:28:19 -0700328
Don Garrettf324bc32017-05-23 14:00:53 -0700329 def testBuildrootRepoCleanFailure(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700330 """Test CleanBuildRoot with repo checkout failure."""
Don Garrettf324bc32017-05-23 14:00:53 -0700331 self.populateBuildroot('1 branchA')
332 self.mock_repo.branch = 'branchA'
333 self.mock_repo.BuildRootGitCleanup.side_effect = Exception
334
Don Garrettbf90cdf2017-05-19 15:54:02 -0700335 cbuildbot_launch.CleanBuildRoot(
336 self.root, self.mock_repo, self.metrics)
Don Garrettf324bc32017-05-23 14:00:53 -0700337
Don Garrettbf90cdf2017-05-19 15:54:02 -0700338 self.assertEqual(osutils.ReadFile(self.state), '2 branchA')
Don Garrettf324bc32017-05-23 14:00:53 -0700339 self.assertNotExists(self.repo)
340 self.assertNotExists(self.chroot)
341 self.assertNotExists(self.general)
342
Don Garrettbf90cdf2017-05-19 15:54:02 -0700343 def testGetState(self):
344 """Test GetState."""
Don Garrett60967922017-04-12 18:51:44 -0700345 # No root dir.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700346 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700347 self.assertEqual(results, (0, ''))
348
349 # Empty root dir.
350 osutils.SafeMakedirs(self.root)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700351 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700352 self.assertEqual(results, (0, ''))
353
354 # Empty Contents
355 osutils.WriteFile(self.state, '')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700356 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700357 self.assertEqual(results, (0, ''))
358
359 # Old Format Contents
360 osutils.WriteFile(self.state, 'happy-branch')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700361 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700362 self.assertEqual(results, (0, ''))
363
364 # Expected Contents
365 osutils.WriteFile(self.state, '1 happy-branch')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700366 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700367 self.assertEqual(results, (1, 'happy-branch'))
368
369 # Future Contents
370 osutils.WriteFile(self.state, '22 master')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700371 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700372 self.assertEqual(results, (22, 'master'))
373
374 # Read Write
Don Garrettbf90cdf2017-05-19 15:54:02 -0700375 cbuildbot_launch.SetState('happy-branch', self.root)
376 results = cbuildbot_launch.GetState(self.root)
377 self.assertEqual(results, (2, 'happy-branch'))
Don Garrett60967922017-04-12 18:51:44 -0700378
Don Garrettbf90cdf2017-05-19 15:54:02 -0700379 def testSetState(self):
380 """Test SetState."""
Don Garrett60967922017-04-12 18:51:44 -0700381 # Write out a state file.
382 osutils.SafeMakedirs(self.root)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700383 cbuildbot_launch.SetState('happy-branch', self.root)
384 self.assertEqual(osutils.ReadFile(self.state), '2 happy-branch')
Don Garrett60967922017-04-12 18:51:44 -0700385
386 # Change to a future version.
387 self.PatchObject(cbuildbot_launch, 'BUILDROOT_BUILDROOT_LAYOUT', 22)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700388 cbuildbot_launch.SetState('happy-branch', self.root)
Don Garrett60967922017-04-12 18:51:44 -0700389 self.assertEqual(osutils.ReadFile(self.state), '22 happy-branch')