blob: 99db582a771cc6be2433e56ee494e9474facba3b [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
Don Garrettc4114cc2016-11-01 20:04:06 -07002# Copyright 2016 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"""Unit tests for chromite.lib.git and helpers for testing that module."""
7
8from __future__ import print_function
9
Don Garrett86881cb2017-02-15 15:41:55 -080010import mock
Don Garrett7ade05a2017-02-17 13:31:47 -080011import os
Don Garrett86881cb2017-02-15 15:41:55 -080012
13from chromite.cbuildbot import repository
Don Garrett861e9182017-05-15 15:30:23 -070014from chromite.lib import constants
Don Garrett597ddff2017-02-17 18:29:37 -080015from chromite.lib import cros_build_lib
Don Garrettc4114cc2016-11-01 20:04:06 -070016from chromite.lib import cros_build_lib_unittest
Don Garrett7ade05a2017-02-17 13:31:47 -080017from chromite.lib import cros_test_lib
Don Garrett86881cb2017-02-15 15:41:55 -080018from chromite.lib import osutils
Don Garrett0c54ed72017-03-03 11:18:57 -080019from chromite.scripts import cbuildbot_launch
Don Garrett86881cb2017-02-15 15:41:55 -080020
Don Garrett60967922017-04-12 18:51:44 -070021
Don Garrett86881cb2017-02-15 15:41:55 -080022EXPECTED_MANIFEST_URL = 'https://chrome-internal-review.googlesource.com/chromeos/manifest-internal' # pylint: disable=line-too-long
Don Garrettc4114cc2016-11-01 20:04:06 -070023
24
Don Garrettacbb2392017-05-11 18:27:41 -070025# It's reasonable for unittests to look at internals.
26# pylint: disable=protected-access
27
28
Don Garrett8d314792017-05-18 13:11:42 -070029class FakeException(Exception):
30 """Test exception to raise during tests."""
31
32
Don Garrett0c54ed72017-03-03 11:18:57 -080033class CbuildbotLaunchTest(cros_test_lib.MockTestCase):
34 """Tests for cbuildbot_launch script."""
Don Garrettc4114cc2016-11-01 20:04:06 -070035
Don Garrett86881cb2017-02-15 15:41:55 -080036 def testPreParseArguments(self):
Don Garrettc4114cc2016-11-01 20:04:06 -070037 """Test that we can correctly extract branch values from cbuildbot args."""
Don Garrett597ddff2017-02-17 18:29:37 -080038 CASES = (
39 (['--buildroot', '/buildroot', 'daisy-incremental'],
40 (None, '/buildroot', None)),
41
42 (['--buildbot', '--buildroot', '/buildroot',
43 '--git-cache-dir', '/git-cache',
44 '-b', 'release-R57-9202.B',
45 'daisy-incremental'],
46 ('release-R57-9202.B', '/buildroot', '/git-cache')),
47
48 (['--debug', '--buildbot', '--notests',
49 '--buildroot', '/buildroot',
50 '--git-cache-dir', '/git-cache',
51 '--branch', 'release-R57-9202.B',
52 'daisy-incremental'],
53 ('release-R57-9202.B', '/buildroot', '/git-cache')),
Don Garrettc4114cc2016-11-01 20:04:06 -070054 )
55
Don Garrett597ddff2017-02-17 18:29:37 -080056 for cmd_args, expected in CASES:
57 expected_branch, expected_buildroot, expected_cache_dir = expected
58
Don Garrett0c54ed72017-03-03 11:18:57 -080059 options = cbuildbot_launch.PreParseArguments(cmd_args)
Don Garrett597ddff2017-02-17 18:29:37 -080060
61 self.assertEqual(options.branch, expected_branch)
62 self.assertEqual(options.buildroot, expected_buildroot)
63 self.assertEqual(options.git_cache_dir, expected_cache_dir)
Don Garrett86881cb2017-02-15 15:41:55 -080064
Don Garrettf324bc32017-05-23 14:00:53 -070065 def testInitialCheckout(self):
Don Garrett86881cb2017-02-15 15:41:55 -080066 """Test InitialCheckout with minimum settings."""
Don Garrettf324bc32017-05-23 14:00:53 -070067 mock_repo = mock.MagicMock()
68 mock_repo.branch = 'branch'
Don Garrett86881cb2017-02-15 15:41:55 -080069
Don Garrettf324bc32017-05-23 14:00:53 -070070 cbuildbot_launch.InitialCheckout(mock_repo)
Don Garrett86881cb2017-02-15 15:41:55 -080071
72 self.assertEqual(mock_repo.mock_calls, [
Don Garrettf324bc32017-05-23 14:00:53 -070073 mock.call.Sync(detach=True),
Don Garrett8d314792017-05-18 13:11:42 -070074 ])
75
Don Garrettf15d65b2017-04-12 12:39:55 -070076 def testConfigureGlobalEnvironment(self):
Don Garrett60967922017-04-12 18:51:44 -070077 """Ensure that we can setup our global runtime environment correctly."""
Don Garrett86fec482017-05-17 18:13:33 -070078
79 os.environ.pop('LANG', None)
80 os.environ['LC_MONETARY'] = 'bad'
81
Don Garrettf15d65b2017-04-12 12:39:55 -070082 cbuildbot_launch.ConfigureGlobalEnvironment()
83
Don Garrett86fec482017-05-17 18:13:33 -070084 # Verify umask is updated.
Don Garrettf15d65b2017-04-12 12:39:55 -070085 self.assertEqual(os.umask(0), 0o22)
86
Don Garrett86fec482017-05-17 18:13:33 -070087 # Verify ENVs are cleaned up.
88 self.assertEqual(os.environ['LANG'], 'en_US.UTF-8')
89 self.assertNotIn('LC_MONETARY', os.environ)
90
Don Garrettf15d65b2017-04-12 12:39:55 -070091
Don Garrett066e6f52017-09-28 19:14:01 -070092class RunDepotToolsEnsureBootstrap(cros_build_lib_unittest.RunCommandTestCase,
93 cros_test_lib.TempDirTestCase):
94 """Test the helper function DepotToolsEnsureBootstrap."""
95
96 def testEnsureBootstrap(self):
97 """Verify that the script is run if present."""
98 script = os.path.join(self.tempdir, 'ensure_bootstrap')
99 osutils.Touch(script, makedirs=True)
100
101 cbuildbot_launch.DepotToolsEnsureBootstrap(self.tempdir)
102 self.assertCommandCalled(
103 [script], extra_env={'PATH': mock.ANY}, cwd=self.tempdir)
104
105 def testEnsureBootstrapMissing(self):
106 """Verify that the script is NOT run if not present."""
107 cbuildbot_launch.DepotToolsEnsureBootstrap(self.tempdir)
108 self.assertEqual(self.rc.call_count, 0)
109
110
Don Garrett597ddff2017-02-17 18:29:37 -0800111class RunTests(cros_build_lib_unittest.RunCommandTestCase):
Don Garrett0c54ed72017-03-03 11:18:57 -0800112 """Tests for cbuildbot_launch script."""
Don Garrett597ddff2017-02-17 18:29:37 -0800113
114 ARGS_BASE = ['--buildroot', '/buildroot']
Don Garrett5cd946b2017-07-20 13:42:20 -0700115 EXPECTED_ARGS_BASE = ['--buildroot', '/cbuildbot_buildroot']
Don Garrett597ddff2017-02-17 18:29:37 -0800116 ARGS_GIT_CACHE = ['--git-cache-dir', '/git-cache']
117 ARGS_CONFIG = ['config']
Don Garrettbf90cdf2017-05-19 15:54:02 -0700118 CMD = ['/cbuildbot_buildroot/chromite/bin/cbuildbot']
Don Garrett597ddff2017-02-17 18:29:37 -0800119
120 def verifyRunCbuildbot(self, args, expected_cmd, version):
Don Garrett86881cb2017-02-15 15:41:55 -0800121 """Ensure we invoke cbuildbot correctly."""
Don Garrett597ddff2017-02-17 18:29:37 -0800122 self.PatchObject(
123 cros_build_lib, 'GetTargetChromiteApiVersion', autospec=True,
124 return_value=version)
125
Don Garretta50bf492017-09-28 18:33:02 -0700126 cbuildbot_launch.RunCbuildbot('/cbuildbot_buildroot', '/depot_tools', args)
Don Garrett597ddff2017-02-17 18:29:37 -0800127
128 self.assertCommandCalled(
Don Garretta50bf492017-09-28 18:33:02 -0700129 expected_cmd, extra_env={'PATH': mock.ANY},
130 cwd='/cbuildbot_buildroot', error_code_ok=True)
Don Garrett597ddff2017-02-17 18:29:37 -0800131
132 def testRunCbuildbotSimple(self):
133 """Ensure we invoke cbuildbot correctly."""
134 self.verifyRunCbuildbot(
135 self.ARGS_BASE + self.ARGS_CONFIG,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700136 self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE,
Don Garrett597ddff2017-02-17 18:29:37 -0800137 (0, 4))
138
139 def testRunCbuildbotNotFiltered(self):
140 """Ensure we invoke cbuildbot correctly."""
141 self.verifyRunCbuildbot(
142 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700143 (self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE +
144 self.ARGS_GIT_CACHE),
Don Garrett597ddff2017-02-17 18:29:37 -0800145 (0, 4))
146
147 def testRunCbuildbotFiltered(self):
148 """Ensure we invoke cbuildbot correctly."""
149 self.verifyRunCbuildbot(
150 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700151 self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE,
Don Garrett597ddff2017-02-17 18:29:37 -0800152 (0, 2))
Don Garrettc4114cc2016-11-01 20:04:06 -0700153
Don Garrett86881cb2017-02-15 15:41:55 -0800154 def testMainMin(self):
155 """Test a minimal set of command line options."""
Don Garrett597ddff2017-02-17 18:29:37 -0800156 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
157 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
Don Garrett861e9182017-05-15 15:30:23 -0700158 autospec=True, return_value=(constants.REEXEC_API_MAJOR,
159 constants.REEXEC_API_MINOR))
Don Garrettf324bc32017-05-23 14:00:53 -0700160 mock_repo = mock.MagicMock()
161 mock_repo.branch = 'master'
Don Garrettbf90cdf2017-05-19 15:54:02 -0700162 mock_repo.directory = '/root/repository'
163
Don Garrettf324bc32017-05-23 14:00:53 -0700164 mock_repo_create = self.PatchObject(repository, 'RepoRepository',
165 autospec=True, return_value=mock_repo)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700166 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot',
Don Garrett0c54ed72017-03-03 11:18:57 -0800167 autospec=True)
168 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800169 autospec=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800170
Don Garrettbf90cdf2017-05-19 15:54:02 -0700171 cbuildbot_launch._main(['-r', '/root', 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700172
Don Garrettf324bc32017-05-23 14:00:53 -0700173 # Did we create the repo instance correctly?
174 self.assertEqual(mock_repo_create.mock_calls,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700175 [mock.call(EXPECTED_MANIFEST_URL, '/root/repository',
Don Garrettf324bc32017-05-23 14:00:53 -0700176 git_cache_dir=None, branch='master')])
177
Don Garrett7ade05a2017-02-17 13:31:47 -0800178 # Ensure we clean, as expected.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700179 self.assertEqual(mock_clean.mock_calls, [
Don Garrettd1d90dd2017-06-13 17:35:52 -0700180 mock.call('/root', mock_repo,
181 {
182 'branch_name': 'master',
183 'tryjob': False,
184 'build_config': 'config',
185 })])
Don Garrett7ade05a2017-02-17 13:31:47 -0800186
Don Garrett86881cb2017-02-15 15:41:55 -0800187 # Ensure we checkout, as expected.
188 self.assertEqual(mock_checkout.mock_calls,
Don Garrettf324bc32017-05-23 14:00:53 -0700189 [mock.call(mock_repo)])
Don Garrettc4114cc2016-11-01 20:04:06 -0700190
Don Garrett86881cb2017-02-15 15:41:55 -0800191 # Ensure we invoke cbuildbot, as expected.
192 self.assertCommandCalled(
Don Garrett5cd946b2017-07-20 13:42:20 -0700193 [
194 '/root/repository/chromite/bin/cbuildbot',
195 'config',
196 '-r', '/root/repository',
197 '--ts-mon-task-num', '1',
198 ],
Don Garretta50bf492017-09-28 18:33:02 -0700199 extra_env={'PATH': mock.ANY},
Don Garrettbf90cdf2017-05-19 15:54:02 -0700200 cwd='/root/repository',
Don Garrettacbb2392017-05-11 18:27:41 -0700201 error_code_ok=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700202
Don Garrett86881cb2017-02-15 15:41:55 -0800203 def testMainMax(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800204 """Test a larger set of command line options."""
205 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
206 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
Don Garrett861e9182017-05-15 15:30:23 -0700207 autospec=True, return_value=(constants.REEXEC_API_MAJOR,
208 constants.REEXEC_API_MINOR))
Don Garrettf324bc32017-05-23 14:00:53 -0700209 mock_repo = mock.MagicMock()
210 mock_repo.branch = 'branch'
Don Garrettbf90cdf2017-05-19 15:54:02 -0700211 mock_repo.directory = '/root/repository'
212
Don Garrettf324bc32017-05-23 14:00:53 -0700213 mock_repo_create = self.PatchObject(repository, 'RepoRepository',
214 autospec=True, return_value=mock_repo)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700215 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot',
Don Garrett0c54ed72017-03-03 11:18:57 -0800216 autospec=True)
217 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800218 autospec=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700219
Don Garrettbf90cdf2017-05-19 15:54:02 -0700220 cbuildbot_launch._main(['--buildroot', '/root',
Don Garrettacbb2392017-05-11 18:27:41 -0700221 '--branch', 'branch',
222 '--git-cache-dir', '/git-cache',
Don Garrettd1d90dd2017-06-13 17:35:52 -0700223 '--remote-trybot',
Don Garrettacbb2392017-05-11 18:27:41 -0700224 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700225
Don Garrettf324bc32017-05-23 14:00:53 -0700226 # Did we create the repo instance correctly?
227 self.assertEqual(mock_repo_create.mock_calls,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700228 [mock.call(EXPECTED_MANIFEST_URL, '/root/repository',
Don Garrettf324bc32017-05-23 14:00:53 -0700229 git_cache_dir='/git-cache', branch='branch')])
230
Don Garrett7ade05a2017-02-17 13:31:47 -0800231 # Ensure we clean, as expected.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700232 self.assertEqual(mock_clean.mock_calls, [
Don Garrettd1d90dd2017-06-13 17:35:52 -0700233 mock.call('/root',
234 mock_repo,
235 {
236 'branch_name': 'branch',
237 'tryjob': True,
238 'build_config': 'config',
239 })])
Don Garrett7ade05a2017-02-17 13:31:47 -0800240
Don Garrett86881cb2017-02-15 15:41:55 -0800241 # Ensure we checkout, as expected.
242 self.assertEqual(mock_checkout.mock_calls,
Don Garrettf324bc32017-05-23 14:00:53 -0700243 [mock.call(mock_repo)])
Don Garrett86881cb2017-02-15 15:41:55 -0800244
245 # Ensure we invoke cbuildbot, as expected.
246 self.assertCommandCalled(
Don Garrett5cd946b2017-07-20 13:42:20 -0700247 [
248 '/root/repository/chromite/bin/cbuildbot',
249 'config',
250 '--buildroot', '/root/repository',
251 '--branch', 'branch',
252 '--git-cache-dir', '/git-cache',
253 '--remote-trybot',
254 '--ts-mon-task-num', '1',
255 ],
Don Garretta50bf492017-09-28 18:33:02 -0700256 extra_env={'PATH': mock.ANY},
Don Garrettbf90cdf2017-05-19 15:54:02 -0700257 cwd='/root/repository',
Don Garrettacbb2392017-05-11 18:27:41 -0700258 error_code_ok=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800259
260
Don Garrettbf90cdf2017-05-19 15:54:02 -0700261class CleanBuildRootTest(cros_test_lib.MockTempDirTestCase):
262 """Tests for CleanBuildRoot method."""
Don Garrett7ade05a2017-02-17 13:31:47 -0800263
264 def setUp(self):
265 """Create standard buildroot contents for cleanup."""
Don Garrettbf90cdf2017-05-19 15:54:02 -0700266 self.root = os.path.join(self.tempdir)
Don Garrette17e1d92017-04-12 15:28:19 -0700267 self.state = os.path.join(self.root, '.cbuildbot_launch_state')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700268 self.buildroot = os.path.join(self.root, 'buildroot')
269 self.repo = os.path.join(self.buildroot, '.repo/repo')
270 self.chroot = os.path.join(self.buildroot, 'chroot/chroot')
271 self.general = os.path.join(self.buildroot, 'general/general')
Don Garrett39963602017-02-27 14:41:58 -0800272 # TODO: Add .cache, and distfiles.
Don Garrett7ade05a2017-02-17 13:31:47 -0800273
Don Garrettf324bc32017-05-23 14:00:53 -0700274 self.mock_repo = mock.MagicMock()
Don Garrettbf90cdf2017-05-19 15:54:02 -0700275 self.mock_repo.directory = self.buildroot
Don Garrettf324bc32017-05-23 14:00:53 -0700276
Don Garrettacbb2392017-05-11 18:27:41 -0700277 self.metrics = {}
278
Don Garrett60967922017-04-12 18:51:44 -0700279 def populateBuildroot(self, state=None):
Don Garrett7ade05a2017-02-17 13:31:47 -0800280 """Create standard buildroot contents for cleanup."""
281 if state:
Don Garrettbf90cdf2017-05-19 15:54:02 -0700282 osutils.SafeMakedirs(self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800283 osutils.WriteFile(self.state, state)
284
285 # Create files.
286 for f in (self.repo, self.chroot, self.general):
Don Garrette17e1d92017-04-12 15:28:19 -0700287 osutils.Touch(f, makedirs=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800288
Don Garrette17e1d92017-04-12 15:28:19 -0700289 def testNoBuildroot(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700290 """Test CleanBuildRoot with no history."""
Don Garrettf324bc32017-05-23 14:00:53 -0700291 self.mock_repo.branch = 'master'
292
Don Garrettbf90cdf2017-05-19 15:54:02 -0700293 cbuildbot_launch.CleanBuildRoot(
294 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800295
Don Garrettbf90cdf2017-05-19 15:54:02 -0700296 self.assertEqual(osutils.ReadFile(self.state), '2 master')
Don Garrett7ade05a2017-02-17 13:31:47 -0800297
298 def testBuildrootNoState(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700299 """Test CleanBuildRoot with no state information."""
Don Garrett7ade05a2017-02-17 13:31:47 -0800300 self.populateBuildroot()
Don Garrettf324bc32017-05-23 14:00:53 -0700301 self.mock_repo.branch = 'master'
Don Garrett7ade05a2017-02-17 13:31:47 -0800302
Don Garrettbf90cdf2017-05-19 15:54:02 -0700303 cbuildbot_launch.CleanBuildRoot(
304 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800305
Don Garrettbf90cdf2017-05-19 15:54:02 -0700306 self.assertEqual(osutils.ReadFile(self.state), '2 master')
Don Garrett60967922017-04-12 18:51:44 -0700307 self.assertNotExists(self.repo)
Don Garrett7ade05a2017-02-17 13:31:47 -0800308 self.assertNotExists(self.chroot)
Don Garrett60967922017-04-12 18:51:44 -0700309 self.assertNotExists(self.general)
310
311 def testBuildrootFormatMismatch(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700312 """Test CleanBuildRoot with no state information."""
Don Garrett125d4dc2017-04-25 16:26:03 -0700313 self.populateBuildroot('0 master')
Don Garrettf324bc32017-05-23 14:00:53 -0700314 self.mock_repo.branch = 'master'
Don Garrett60967922017-04-12 18:51:44 -0700315
Don Garrettbf90cdf2017-05-19 15:54:02 -0700316 cbuildbot_launch.CleanBuildRoot(
317 self.root, self.mock_repo, self.metrics)
Don Garrett60967922017-04-12 18:51:44 -0700318
Don Garrettbf90cdf2017-05-19 15:54:02 -0700319 self.assertEqual(osutils.ReadFile(self.state), '2 master')
Don Garrett60967922017-04-12 18:51:44 -0700320 self.assertNotExists(self.repo)
321 self.assertNotExists(self.chroot)
322 self.assertNotExists(self.general)
Don Garrett7ade05a2017-02-17 13:31:47 -0800323
324 def testBuildrootBranchChange(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700325 """Test CleanBuildRoot with a change in branches."""
326 self.populateBuildroot('2 branchA')
Don Garrettf324bc32017-05-23 14:00:53 -0700327 self.mock_repo.branch = 'branchB'
Benjamin Gordon59ba2f82017-08-28 15:31:06 -0600328 m = self.PatchObject(cros_build_lib, 'CleanupChrootMount')
Don Garrett7ade05a2017-02-17 13:31:47 -0800329
Don Garrettbf90cdf2017-05-19 15:54:02 -0700330 cbuildbot_launch.CleanBuildRoot(
331 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800332
Don Garrettbf90cdf2017-05-19 15:54:02 -0700333 self.assertEqual(osutils.ReadFile(self.state), '2 branchB')
Don Garrett7ade05a2017-02-17 13:31:47 -0800334 self.assertExists(self.repo)
335 self.assertNotExists(self.chroot)
336 self.assertExists(self.general)
Benjamin Gordon59ba2f82017-08-28 15:31:06 -0600337 m.assert_called()
Don Garrett7ade05a2017-02-17 13:31:47 -0800338
339 def testBuildrootBranchMatch(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700340 """Test CleanBuildRoot with no change in branch."""
341 self.populateBuildroot('2 branchA')
Don Garrettf324bc32017-05-23 14:00:53 -0700342 self.mock_repo.branch = 'branchA'
Don Garrett7ade05a2017-02-17 13:31:47 -0800343
Don Garrettbf90cdf2017-05-19 15:54:02 -0700344 cbuildbot_launch.CleanBuildRoot(
345 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800346
Don Garrettbf90cdf2017-05-19 15:54:02 -0700347 self.assertEqual(osutils.ReadFile(self.state), '2 branchA')
Don Garrett7ade05a2017-02-17 13:31:47 -0800348 self.assertExists(self.repo)
349 self.assertExists(self.chroot)
350 self.assertExists(self.general)
Don Garrette17e1d92017-04-12 15:28:19 -0700351
Don Garrettf324bc32017-05-23 14:00:53 -0700352 def testBuildrootRepoCleanFailure(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700353 """Test CleanBuildRoot with repo checkout failure."""
Don Garrettf324bc32017-05-23 14:00:53 -0700354 self.populateBuildroot('1 branchA')
355 self.mock_repo.branch = 'branchA'
356 self.mock_repo.BuildRootGitCleanup.side_effect = Exception
357
Don Garrettbf90cdf2017-05-19 15:54:02 -0700358 cbuildbot_launch.CleanBuildRoot(
359 self.root, self.mock_repo, self.metrics)
Don Garrettf324bc32017-05-23 14:00:53 -0700360
Don Garrettbf90cdf2017-05-19 15:54:02 -0700361 self.assertEqual(osutils.ReadFile(self.state), '2 branchA')
Don Garrettf324bc32017-05-23 14:00:53 -0700362 self.assertNotExists(self.repo)
363 self.assertNotExists(self.chroot)
364 self.assertNotExists(self.general)
365
Don Garrettbf90cdf2017-05-19 15:54:02 -0700366 def testGetState(self):
367 """Test GetState."""
Don Garrett60967922017-04-12 18:51:44 -0700368 # No root dir.
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, (0, ''))
371
372 # Empty root dir.
373 osutils.SafeMakedirs(self.root)
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, (0, ''))
376
377 # Empty Contents
378 osutils.WriteFile(self.state, '')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700379 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700380 self.assertEqual(results, (0, ''))
381
382 # Old Format Contents
383 osutils.WriteFile(self.state, 'happy-branch')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700384 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700385 self.assertEqual(results, (0, ''))
386
387 # Expected Contents
388 osutils.WriteFile(self.state, '1 happy-branch')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700389 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700390 self.assertEqual(results, (1, 'happy-branch'))
391
392 # Future Contents
393 osutils.WriteFile(self.state, '22 master')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700394 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700395 self.assertEqual(results, (22, 'master'))
396
397 # Read Write
Don Garrettbf90cdf2017-05-19 15:54:02 -0700398 cbuildbot_launch.SetState('happy-branch', self.root)
399 results = cbuildbot_launch.GetState(self.root)
400 self.assertEqual(results, (2, 'happy-branch'))
Don Garrett60967922017-04-12 18:51:44 -0700401
Don Garrettbf90cdf2017-05-19 15:54:02 -0700402 def testSetState(self):
403 """Test SetState."""
Don Garrett60967922017-04-12 18:51:44 -0700404 # Write out a state file.
405 osutils.SafeMakedirs(self.root)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700406 cbuildbot_launch.SetState('happy-branch', self.root)
407 self.assertEqual(osutils.ReadFile(self.state), '2 happy-branch')
Don Garrett60967922017-04-12 18:51:44 -0700408
409 # Change to a future version.
410 self.PatchObject(cbuildbot_launch, 'BUILDROOT_BUILDROOT_LAYOUT', 22)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700411 cbuildbot_launch.SetState('happy-branch', self.root)
Don Garrett60967922017-04-12 18:51:44 -0700412 self.assertEqual(osutils.ReadFile(self.state), '22 happy-branch')