blob: 3fdbc4dc6585b5b26521f95aaea032bea6a7daac [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)
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700170 mock_cleanup_chroot = self.PatchObject(cbuildbot_launch, 'CleanupChroot',
171 autospec=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800172
Don Garrettbf90cdf2017-05-19 15:54:02 -0700173 cbuildbot_launch._main(['-r', '/root', 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700174
Don Garrettf324bc32017-05-23 14:00:53 -0700175 # Did we create the repo instance correctly?
176 self.assertEqual(mock_repo_create.mock_calls,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700177 [mock.call(EXPECTED_MANIFEST_URL, '/root/repository',
Don Garrettf324bc32017-05-23 14:00:53 -0700178 git_cache_dir=None, branch='master')])
179
Don Garrett7ade05a2017-02-17 13:31:47 -0800180 # Ensure we clean, as expected.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700181 self.assertEqual(mock_clean.mock_calls, [
Don Garrettd1d90dd2017-06-13 17:35:52 -0700182 mock.call('/root', mock_repo,
183 {
184 'branch_name': 'master',
185 'tryjob': False,
186 'build_config': 'config',
187 })])
Don Garrett7ade05a2017-02-17 13:31:47 -0800188
Don Garrett86881cb2017-02-15 15:41:55 -0800189 # Ensure we checkout, as expected.
190 self.assertEqual(mock_checkout.mock_calls,
Don Garrettf324bc32017-05-23 14:00:53 -0700191 [mock.call(mock_repo)])
Don Garrettc4114cc2016-11-01 20:04:06 -0700192
Don Garrett86881cb2017-02-15 15:41:55 -0800193 # Ensure we invoke cbuildbot, as expected.
194 self.assertCommandCalled(
Don Garrett5cd946b2017-07-20 13:42:20 -0700195 [
196 '/root/repository/chromite/bin/cbuildbot',
197 'config',
198 '-r', '/root/repository',
199 '--ts-mon-task-num', '1',
200 ],
Don Garretta50bf492017-09-28 18:33:02 -0700201 extra_env={'PATH': mock.ANY},
Don Garrettbf90cdf2017-05-19 15:54:02 -0700202 cwd='/root/repository',
Don Garrettacbb2392017-05-11 18:27:41 -0700203 error_code_ok=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700204
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700205 # Ensure we clean the chroot, as expected.
206 self.assertEqual(mock_cleanup_chroot.mock_calls, [
207 mock.call('/root/repository')])
208
Don Garrett86881cb2017-02-15 15:41:55 -0800209 def testMainMax(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800210 """Test a larger set of command line options."""
211 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
212 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
Don Garrett861e9182017-05-15 15:30:23 -0700213 autospec=True, return_value=(constants.REEXEC_API_MAJOR,
214 constants.REEXEC_API_MINOR))
Don Garrettf324bc32017-05-23 14:00:53 -0700215 mock_repo = mock.MagicMock()
216 mock_repo.branch = 'branch'
Don Garrettbf90cdf2017-05-19 15:54:02 -0700217 mock_repo.directory = '/root/repository'
218
Don Garrettf324bc32017-05-23 14:00:53 -0700219 mock_repo_create = self.PatchObject(repository, 'RepoRepository',
220 autospec=True, return_value=mock_repo)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700221 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot',
Don Garrett0c54ed72017-03-03 11:18:57 -0800222 autospec=True)
223 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800224 autospec=True)
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700225 mock_cleanup_chroot = self.PatchObject(cbuildbot_launch, 'CleanupChroot',
226 autospec=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700227
Don Garrettbf90cdf2017-05-19 15:54:02 -0700228 cbuildbot_launch._main(['--buildroot', '/root',
Don Garrettacbb2392017-05-11 18:27:41 -0700229 '--branch', 'branch',
230 '--git-cache-dir', '/git-cache',
Don Garrettd1d90dd2017-06-13 17:35:52 -0700231 '--remote-trybot',
Don Garrettacbb2392017-05-11 18:27:41 -0700232 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700233
Don Garrettf324bc32017-05-23 14:00:53 -0700234 # Did we create the repo instance correctly?
235 self.assertEqual(mock_repo_create.mock_calls,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700236 [mock.call(EXPECTED_MANIFEST_URL, '/root/repository',
Don Garrettf324bc32017-05-23 14:00:53 -0700237 git_cache_dir='/git-cache', branch='branch')])
238
Don Garrett7ade05a2017-02-17 13:31:47 -0800239 # Ensure we clean, as expected.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700240 self.assertEqual(mock_clean.mock_calls, [
Don Garrettd1d90dd2017-06-13 17:35:52 -0700241 mock.call('/root',
242 mock_repo,
243 {
244 'branch_name': 'branch',
245 'tryjob': True,
246 'build_config': 'config',
247 })])
Don Garrett7ade05a2017-02-17 13:31:47 -0800248
Don Garrett86881cb2017-02-15 15:41:55 -0800249 # Ensure we checkout, as expected.
250 self.assertEqual(mock_checkout.mock_calls,
Don Garrettf324bc32017-05-23 14:00:53 -0700251 [mock.call(mock_repo)])
Don Garrett86881cb2017-02-15 15:41:55 -0800252
253 # Ensure we invoke cbuildbot, as expected.
254 self.assertCommandCalled(
Don Garrett5cd946b2017-07-20 13:42:20 -0700255 [
256 '/root/repository/chromite/bin/cbuildbot',
257 'config',
258 '--buildroot', '/root/repository',
259 '--branch', 'branch',
260 '--git-cache-dir', '/git-cache',
261 '--remote-trybot',
262 '--ts-mon-task-num', '1',
263 ],
Don Garretta50bf492017-09-28 18:33:02 -0700264 extra_env={'PATH': mock.ANY},
Don Garrettbf90cdf2017-05-19 15:54:02 -0700265 cwd='/root/repository',
Don Garrettacbb2392017-05-11 18:27:41 -0700266 error_code_ok=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800267
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700268 # Ensure we clean the chroot, as expected.
269 self.assertEqual(mock_cleanup_chroot.mock_calls, [
270 mock.call('/root/repository')])
271
Don Garrett7ade05a2017-02-17 13:31:47 -0800272
Don Garrettbf90cdf2017-05-19 15:54:02 -0700273class CleanBuildRootTest(cros_test_lib.MockTempDirTestCase):
274 """Tests for CleanBuildRoot method."""
Don Garrett7ade05a2017-02-17 13:31:47 -0800275
276 def setUp(self):
277 """Create standard buildroot contents for cleanup."""
Don Garrettbf90cdf2017-05-19 15:54:02 -0700278 self.root = os.path.join(self.tempdir)
Don Garrette17e1d92017-04-12 15:28:19 -0700279 self.state = os.path.join(self.root, '.cbuildbot_launch_state')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700280 self.buildroot = os.path.join(self.root, 'buildroot')
281 self.repo = os.path.join(self.buildroot, '.repo/repo')
282 self.chroot = os.path.join(self.buildroot, 'chroot/chroot')
283 self.general = os.path.join(self.buildroot, 'general/general')
Stephen Barberd8e0f112018-04-05 06:46:08 +0000284 # TODO: Add .cache, and distfiles.
Don Garrett7ade05a2017-02-17 13:31:47 -0800285
Don Garrettf324bc32017-05-23 14:00:53 -0700286 self.mock_repo = mock.MagicMock()
Don Garrettbf90cdf2017-05-19 15:54:02 -0700287 self.mock_repo.directory = self.buildroot
Don Garrettf324bc32017-05-23 14:00:53 -0700288
Don Garrettacbb2392017-05-11 18:27:41 -0700289 self.metrics = {}
290
Don Garrett60967922017-04-12 18:51:44 -0700291 def populateBuildroot(self, state=None):
Don Garrett7ade05a2017-02-17 13:31:47 -0800292 """Create standard buildroot contents for cleanup."""
293 if state:
Don Garrettbf90cdf2017-05-19 15:54:02 -0700294 osutils.SafeMakedirs(self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800295 osutils.WriteFile(self.state, state)
296
297 # Create files.
Stephen Barberd8e0f112018-04-05 06:46:08 +0000298 for f in (self.repo, self.chroot, self.general):
Don Garrette17e1d92017-04-12 15:28:19 -0700299 osutils.Touch(f, makedirs=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800300
Don Garrette17e1d92017-04-12 15:28:19 -0700301 def testNoBuildroot(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700302 """Test CleanBuildRoot with no history."""
Don Garrettf324bc32017-05-23 14:00:53 -0700303 self.mock_repo.branch = 'master'
304
Don Garrettbf90cdf2017-05-19 15:54:02 -0700305 cbuildbot_launch.CleanBuildRoot(
306 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800307
Stephen Barberd8e0f112018-04-05 06:46:08 +0000308 self.assertEqual(osutils.ReadFile(self.state), '2 master')
Don Garrett7ade05a2017-02-17 13:31:47 -0800309
310 def testBuildrootNoState(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700311 """Test CleanBuildRoot with no state information."""
Don Garrett7ade05a2017-02-17 13:31:47 -0800312 self.populateBuildroot()
Don Garrettf324bc32017-05-23 14:00:53 -0700313 self.mock_repo.branch = 'master'
Don Garrett7ade05a2017-02-17 13:31:47 -0800314
Don Garrettbf90cdf2017-05-19 15:54:02 -0700315 cbuildbot_launch.CleanBuildRoot(
316 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800317
Stephen Barberd8e0f112018-04-05 06:46:08 +0000318 self.assertEqual(osutils.ReadFile(self.state), '2 master')
Don Garrett60967922017-04-12 18:51:44 -0700319 self.assertNotExists(self.repo)
Don Garrett7ade05a2017-02-17 13:31:47 -0800320 self.assertNotExists(self.chroot)
Don Garrett60967922017-04-12 18:51:44 -0700321 self.assertNotExists(self.general)
322
323 def testBuildrootFormatMismatch(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700324 """Test CleanBuildRoot with no state information."""
Don Garrett125d4dc2017-04-25 16:26:03 -0700325 self.populateBuildroot('0 master')
Don Garrettf324bc32017-05-23 14:00:53 -0700326 self.mock_repo.branch = 'master'
Don Garrett60967922017-04-12 18:51:44 -0700327
Don Garrettbf90cdf2017-05-19 15:54:02 -0700328 cbuildbot_launch.CleanBuildRoot(
329 self.root, self.mock_repo, self.metrics)
Don Garrett60967922017-04-12 18:51:44 -0700330
Stephen Barberd8e0f112018-04-05 06:46:08 +0000331 self.assertEqual(osutils.ReadFile(self.state), '2 master')
Don Garrett60967922017-04-12 18:51:44 -0700332 self.assertNotExists(self.repo)
333 self.assertNotExists(self.chroot)
334 self.assertNotExists(self.general)
Don Garrett7ade05a2017-02-17 13:31:47 -0800335
336 def testBuildrootBranchChange(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700337 """Test CleanBuildRoot with a change in branches."""
338 self.populateBuildroot('2 branchA')
Don Garrettf324bc32017-05-23 14:00:53 -0700339 self.mock_repo.branch = 'branchB'
Benjamin Gordon59ba2f82017-08-28 15:31:06 -0600340 m = self.PatchObject(cros_build_lib, 'CleanupChrootMount')
Don Garrett7ade05a2017-02-17 13:31:47 -0800341
Don Garrettbf90cdf2017-05-19 15:54:02 -0700342 cbuildbot_launch.CleanBuildRoot(
343 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800344
Stephen Barberd8e0f112018-04-05 06:46:08 +0000345 self.assertEqual(osutils.ReadFile(self.state), '2 branchB')
Don Garrett7ade05a2017-02-17 13:31:47 -0800346 self.assertExists(self.repo)
347 self.assertNotExists(self.chroot)
348 self.assertExists(self.general)
Benjamin Gordon59ba2f82017-08-28 15:31:06 -0600349 m.assert_called()
Don Garrett7ade05a2017-02-17 13:31:47 -0800350
351 def testBuildrootBranchMatch(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700352 """Test CleanBuildRoot with no change in branch."""
353 self.populateBuildroot('2 branchA')
Don Garrettf324bc32017-05-23 14:00:53 -0700354 self.mock_repo.branch = 'branchA'
Don Garrett7ade05a2017-02-17 13:31:47 -0800355
Don Garrettbf90cdf2017-05-19 15:54:02 -0700356 cbuildbot_launch.CleanBuildRoot(
357 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800358
Stephen Barberd8e0f112018-04-05 06:46:08 +0000359 self.assertEqual(osutils.ReadFile(self.state), '2 branchA')
Don Garrett7ade05a2017-02-17 13:31:47 -0800360 self.assertExists(self.repo)
361 self.assertExists(self.chroot)
362 self.assertExists(self.general)
Don Garrette17e1d92017-04-12 15:28:19 -0700363
Don Garrettf324bc32017-05-23 14:00:53 -0700364 def testBuildrootRepoCleanFailure(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700365 """Test CleanBuildRoot with repo checkout failure."""
Don Garrettf324bc32017-05-23 14:00:53 -0700366 self.populateBuildroot('1 branchA')
367 self.mock_repo.branch = 'branchA'
368 self.mock_repo.BuildRootGitCleanup.side_effect = Exception
369
Don Garrettbf90cdf2017-05-19 15:54:02 -0700370 cbuildbot_launch.CleanBuildRoot(
371 self.root, self.mock_repo, self.metrics)
Don Garrettf324bc32017-05-23 14:00:53 -0700372
Stephen Barberd8e0f112018-04-05 06:46:08 +0000373 self.assertEqual(osutils.ReadFile(self.state), '2 branchA')
Don Garrettf324bc32017-05-23 14:00:53 -0700374 self.assertNotExists(self.repo)
375 self.assertNotExists(self.chroot)
376 self.assertNotExists(self.general)
377
Don Garrettbf90cdf2017-05-19 15:54:02 -0700378 def testGetState(self):
379 """Test GetState."""
Don Garrett60967922017-04-12 18:51:44 -0700380 # No root dir.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700381 results = cbuildbot_launch.GetState(self.root)
Stephen Barberd8e0f112018-04-05 06:46:08 +0000382 self.assertEqual(results, (0, ''))
Don Garrett60967922017-04-12 18:51:44 -0700383
384 # Empty root dir.
385 osutils.SafeMakedirs(self.root)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700386 results = cbuildbot_launch.GetState(self.root)
Stephen Barberd8e0f112018-04-05 06:46:08 +0000387 self.assertEqual(results, (0, ''))
Don Garrett60967922017-04-12 18:51:44 -0700388
Stephen Barberd8e0f112018-04-05 06:46:08 +0000389 # Empty Contents
Don Garrett60967922017-04-12 18:51:44 -0700390 osutils.WriteFile(self.state, '')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700391 results = cbuildbot_launch.GetState(self.root)
Stephen Barberd8e0f112018-04-05 06:46:08 +0000392 self.assertEqual(results, (0, ''))
Don Garrett60967922017-04-12 18:51:44 -0700393
Stephen Barberd8e0f112018-04-05 06:46:08 +0000394 # Old Format Contents
Don Garrett60967922017-04-12 18:51:44 -0700395 osutils.WriteFile(self.state, 'happy-branch')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700396 results = cbuildbot_launch.GetState(self.root)
Stephen Barberd8e0f112018-04-05 06:46:08 +0000397 self.assertEqual(results, (0, ''))
Don Garrett60967922017-04-12 18:51:44 -0700398
Stephen Barberd8e0f112018-04-05 06:46:08 +0000399 # Expected Contents
Don Garrett60967922017-04-12 18:51:44 -0700400 osutils.WriteFile(self.state, '1 happy-branch')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700401 results = cbuildbot_launch.GetState(self.root)
Stephen Barberd8e0f112018-04-05 06:46:08 +0000402 self.assertEqual(results, (1, 'happy-branch'))
Don Garrett60967922017-04-12 18:51:44 -0700403
Stephen Barberd8e0f112018-04-05 06:46:08 +0000404 # Future Contents
405 osutils.WriteFile(self.state, '22 master')
406 results = cbuildbot_launch.GetState(self.root)
407 self.assertEqual(results, (22, 'master'))
Don Garrett60967922017-04-12 18:51:44 -0700408
Stephen Barberd8e0f112018-04-05 06:46:08 +0000409 # Read Write
Don Garrettbf90cdf2017-05-19 15:54:02 -0700410 cbuildbot_launch.SetState('happy-branch', self.root)
Stephen Barberd8e0f112018-04-05 06:46:08 +0000411 results = cbuildbot_launch.GetState(self.root)
412 self.assertEqual(results, (2, 'happy-branch'))
Don Garrett60967922017-04-12 18:51:44 -0700413
Don Garrettbf90cdf2017-05-19 15:54:02 -0700414 def testSetState(self):
415 """Test SetState."""
Don Garrett60967922017-04-12 18:51:44 -0700416 # Write out a state file.
417 osutils.SafeMakedirs(self.root)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700418 cbuildbot_launch.SetState('happy-branch', self.root)
Stephen Barberd8e0f112018-04-05 06:46:08 +0000419 self.assertEqual(osutils.ReadFile(self.state), '2 happy-branch')
Don Garrett60967922017-04-12 18:51:44 -0700420
421 # Change to a future version.
422 self.PatchObject(cbuildbot_launch, 'BUILDROOT_BUILDROOT_LAYOUT', 22)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700423 cbuildbot_launch.SetState('happy-branch', self.root)
Stephen Barberd8e0f112018-04-05 06:46:08 +0000424 self.assertEqual(osutils.ReadFile(self.state), '22 happy-branch')