blob: 3db77db9c1ad9f4027d4935891c3886869fd8c6c [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
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -07006"""Unit tests for chromite.scripts.cbuildbot_launch."""
Don Garrettc4114cc2016-11-01 20:04:06 -07007
8from __future__ import print_function
9
Don Garrett86881cb2017-02-15 15:41:55 -080010import mock
Don Garrett7ade05a2017-02-17 13:31:47 -080011import os
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -070012import time
Don Garrett86881cb2017-02-15 15:41:55 -080013
14from chromite.cbuildbot import repository
Don Garrett861e9182017-05-15 15:30:23 -070015from chromite.lib import constants
Don Garrett597ddff2017-02-17 18:29:37 -080016from chromite.lib import cros_build_lib
Don Garrettc4114cc2016-11-01 20:04:06 -070017from chromite.lib import cros_build_lib_unittest
Don Garrett7ade05a2017-02-17 13:31:47 -080018from chromite.lib import cros_test_lib
Don Garrett86881cb2017-02-15 15:41:55 -080019from chromite.lib import osutils
Don Garrett0c54ed72017-03-03 11:18:57 -080020from chromite.scripts import cbuildbot_launch
Don Garrett86881cb2017-02-15 15:41:55 -080021
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
Don Garrett6e5c6b92018-04-06 17:58:49 -0700120 def verifyCbuildbot(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 Garrett6e5c6b92018-04-06 17:58:49 -0700126 cbuildbot_launch.Cbuildbot('/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
Don Garrett6e5c6b92018-04-06 17:58:49 -0700132 def testCbuildbotSimple(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800133 """Ensure we invoke cbuildbot correctly."""
Don Garrett6e5c6b92018-04-06 17:58:49 -0700134 self.verifyCbuildbot(
Don Garrett597ddff2017-02-17 18:29:37 -0800135 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
Don Garrett6e5c6b92018-04-06 17:58:49 -0700139 def testCbuildbotNotFiltered(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800140 """Ensure we invoke cbuildbot correctly."""
Don Garrett6e5c6b92018-04-06 17:58:49 -0700141 self.verifyCbuildbot(
Don Garrett597ddff2017-02-17 18:29:37 -0800142 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
Don Garrett6e5c6b92018-04-06 17:58:49 -0700147 def testCbuildbotFiltered(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800148 """Ensure we invoke cbuildbot correctly."""
Don Garrett6e5c6b92018-04-06 17:58:49 -0700149 self.verifyCbuildbot(
Don Garrett597ddff2017-02-17 18:29:37 -0800150 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')
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700284 self.cache = os.path.join(self.buildroot, '.cache')
285 self.distfiles = os.path.join(self.cache, 'distfiles')
Don Garrett7ade05a2017-02-17 13:31:47 -0800286
Don Garrettf324bc32017-05-23 14:00:53 -0700287 self.mock_repo = mock.MagicMock()
Don Garrettbf90cdf2017-05-19 15:54:02 -0700288 self.mock_repo.directory = self.buildroot
Don Garrettf324bc32017-05-23 14:00:53 -0700289
Don Garrettacbb2392017-05-11 18:27:41 -0700290 self.metrics = {}
291
Don Garrett60967922017-04-12 18:51:44 -0700292 def populateBuildroot(self, state=None):
Don Garrett7ade05a2017-02-17 13:31:47 -0800293 """Create standard buildroot contents for cleanup."""
294 if state:
Don Garrettbf90cdf2017-05-19 15:54:02 -0700295 osutils.SafeMakedirs(self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800296 osutils.WriteFile(self.state, state)
297
298 # Create files.
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700299 for f in (self.repo, self.chroot, self.general, self.distfiles):
Don Garrette17e1d92017-04-12 15:28:19 -0700300 osutils.Touch(f, makedirs=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800301
Don Garrette17e1d92017-04-12 15:28:19 -0700302 def testNoBuildroot(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700303 """Test CleanBuildRoot with no history."""
Don Garrettf324bc32017-05-23 14:00:53 -0700304 self.mock_repo.branch = 'master'
305
Don Garrettbf90cdf2017-05-19 15:54:02 -0700306 cbuildbot_launch.CleanBuildRoot(
307 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800308
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700309 version, branch, distfiles_ts = cbuildbot_launch.GetState(self.root)
310 self.assertEqual(version, 2)
311 self.assertEqual(branch, 'master')
312 self.assertIsNotNone(distfiles_ts)
Don Garrett7ade05a2017-02-17 13:31:47 -0800313
314 def testBuildrootNoState(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700315 """Test CleanBuildRoot with no state information."""
Don Garrett7ade05a2017-02-17 13:31:47 -0800316 self.populateBuildroot()
Don Garrettf324bc32017-05-23 14:00:53 -0700317 self.mock_repo.branch = 'master'
Don Garrett7ade05a2017-02-17 13:31:47 -0800318
Don Garrettbf90cdf2017-05-19 15:54:02 -0700319 cbuildbot_launch.CleanBuildRoot(
320 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800321
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700322 version, branch, distfiles_ts = cbuildbot_launch.GetState(self.root)
323 self.assertEqual(version, 2)
324 self.assertEqual(branch, 'master')
325 self.assertIsNotNone(distfiles_ts)
326
Don Garrett60967922017-04-12 18:51:44 -0700327 self.assertNotExists(self.repo)
Don Garrett7ade05a2017-02-17 13:31:47 -0800328 self.assertNotExists(self.chroot)
Don Garrett60967922017-04-12 18:51:44 -0700329 self.assertNotExists(self.general)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700330 self.assertNotExists(self.distfiles)
Don Garrett60967922017-04-12 18:51:44 -0700331
332 def testBuildrootFormatMismatch(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700333 """Test CleanBuildRoot with no state information."""
Don Garrett125d4dc2017-04-25 16:26:03 -0700334 self.populateBuildroot('0 master')
Don Garrettf324bc32017-05-23 14:00:53 -0700335 self.mock_repo.branch = 'master'
Don Garrett60967922017-04-12 18:51:44 -0700336
Don Garrettbf90cdf2017-05-19 15:54:02 -0700337 cbuildbot_launch.CleanBuildRoot(
338 self.root, self.mock_repo, self.metrics)
Don Garrett60967922017-04-12 18:51:44 -0700339
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700340 version, branch, distfiles_ts = cbuildbot_launch.GetState(self.root)
341 self.assertEqual(version, 2)
342 self.assertEqual(branch, 'master')
343 self.assertIsNotNone(distfiles_ts)
344
Don Garrett60967922017-04-12 18:51:44 -0700345 self.assertNotExists(self.repo)
346 self.assertNotExists(self.chroot)
347 self.assertNotExists(self.general)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700348 self.assertNotExists(self.distfiles)
Don Garrett7ade05a2017-02-17 13:31:47 -0800349
350 def testBuildrootBranchChange(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700351 """Test CleanBuildRoot with a change in branches."""
352 self.populateBuildroot('2 branchA')
Don Garrettf324bc32017-05-23 14:00:53 -0700353 self.mock_repo.branch = 'branchB'
Benjamin Gordon59ba2f82017-08-28 15:31:06 -0600354 m = self.PatchObject(cros_build_lib, 'CleanupChrootMount')
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
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700359 version, branch, distfiles_ts = cbuildbot_launch.GetState(self.root)
360 self.assertEqual(version, 2)
361 self.assertEqual(branch, 'branchB')
362 self.assertIsNotNone(distfiles_ts)
363
Don Garrett7ade05a2017-02-17 13:31:47 -0800364 self.assertExists(self.repo)
365 self.assertNotExists(self.chroot)
366 self.assertExists(self.general)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700367 self.assertNotExists(self.distfiles)
Benjamin Gordon59ba2f82017-08-28 15:31:06 -0600368 m.assert_called()
Don Garrett7ade05a2017-02-17 13:31:47 -0800369
370 def testBuildrootBranchMatch(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700371 """Test CleanBuildRoot with no change in branch."""
372 self.populateBuildroot('2 branchA')
Don Garrettf324bc32017-05-23 14:00:53 -0700373 self.mock_repo.branch = 'branchA'
Don Garrett7ade05a2017-02-17 13:31:47 -0800374
Don Garrettbf90cdf2017-05-19 15:54:02 -0700375 cbuildbot_launch.CleanBuildRoot(
376 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800377
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700378 version, branch, distfiles_ts = cbuildbot_launch.GetState(self.root)
379 self.assertEqual(version, 2)
380 self.assertEqual(branch, 'branchA')
381 self.assertIsNotNone(distfiles_ts)
382
Don Garrett7ade05a2017-02-17 13:31:47 -0800383 self.assertExists(self.repo)
384 self.assertExists(self.chroot)
385 self.assertExists(self.general)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700386 self.assertExists(self.distfiles)
387
388 def testBuildrootDistfilesRecentCache(self):
389 """Test CleanBuildRoot does not delete distfiles when cache is recent."""
390 seed_distfiles_ts = time.time() - 60
391 self.populateBuildroot('2 branchA %f' % seed_distfiles_ts)
392 self.mock_repo.branch = 'branchA'
393
394 cbuildbot_launch.CleanBuildRoot(
395 self.root, self.mock_repo, self.metrics)
396
397 version, branch, distfiles_ts = cbuildbot_launch.GetState(self.root)
398 self.assertEqual(version, 2)
399 self.assertEqual(branch, 'branchA')
400 # Same cache creation timestamp is rewritten to state.
401 self.assertEqual(distfiles_ts, seed_distfiles_ts)
402
403 self.assertExists(self.repo)
404 self.assertExists(self.chroot)
405 self.assertExists(self.general)
406 self.assertExists(self.distfiles)
407
408 def testBuildrootDistfilesCacheExpired(self):
409 """Test CleanBuildRoot when the distfiles cache is too old."""
410 self.populateBuildroot('2 branchA 100.000000')
411 self.mock_repo.branch = 'branchA'
412
413 cbuildbot_launch.CleanBuildRoot(
414 self.root, self.mock_repo, self.metrics)
415
416 version, branch, distfiles_ts = cbuildbot_launch.GetState(self.root)
417 self.assertEqual(version, 2)
418 self.assertEqual(branch, 'branchA')
419 self.assertIsNotNone(distfiles_ts)
420
421 self.assertExists(self.repo)
422 self.assertExists(self.chroot)
423 self.assertExists(self.general)
424 self.assertNotExists(self.distfiles)
Don Garrette17e1d92017-04-12 15:28:19 -0700425
Don Garrettf324bc32017-05-23 14:00:53 -0700426 def testBuildrootRepoCleanFailure(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700427 """Test CleanBuildRoot with repo checkout failure."""
Don Garrettf324bc32017-05-23 14:00:53 -0700428 self.populateBuildroot('1 branchA')
429 self.mock_repo.branch = 'branchA'
430 self.mock_repo.BuildRootGitCleanup.side_effect = Exception
431
Don Garrettbf90cdf2017-05-19 15:54:02 -0700432 cbuildbot_launch.CleanBuildRoot(
433 self.root, self.mock_repo, self.metrics)
Don Garrettf324bc32017-05-23 14:00:53 -0700434
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700435 version, branch, distfiles_ts = cbuildbot_launch.GetState(self.root)
436 self.assertEqual(version, 2)
437 self.assertEqual(branch, 'branchA')
438 self.assertIsNotNone(distfiles_ts)
439
Don Garrettf324bc32017-05-23 14:00:53 -0700440 self.assertNotExists(self.repo)
441 self.assertNotExists(self.chroot)
442 self.assertNotExists(self.general)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700443 self.assertNotExists(self.distfiles)
Don Garrettf324bc32017-05-23 14:00:53 -0700444
Don Garrettbf90cdf2017-05-19 15:54:02 -0700445 def testGetState(self):
446 """Test GetState."""
Don Garrett60967922017-04-12 18:51:44 -0700447 # No root dir.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700448 results = cbuildbot_launch.GetState(self.root)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700449 self.assertEqual(results, (0, '', None))
Don Garrett60967922017-04-12 18:51:44 -0700450
451 # Empty root dir.
452 osutils.SafeMakedirs(self.root)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700453 results = cbuildbot_launch.GetState(self.root)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700454 self.assertEqual(results, (0, '', None))
Don Garrett60967922017-04-12 18:51:44 -0700455
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700456 # Empty contents
Don Garrett60967922017-04-12 18:51:44 -0700457 osutils.WriteFile(self.state, '')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700458 results = cbuildbot_launch.GetState(self.root)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700459 self.assertEqual(results, (0, '', None))
Don Garrett60967922017-04-12 18:51:44 -0700460
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700461 # Old format contents
Don Garrett60967922017-04-12 18:51:44 -0700462 osutils.WriteFile(self.state, 'happy-branch')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700463 results = cbuildbot_launch.GetState(self.root)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700464 self.assertEqual(results, (0, '', None))
Don Garrett60967922017-04-12 18:51:44 -0700465
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700466 # Expected contents, without distfiles timestamp
Don Garrett60967922017-04-12 18:51:44 -0700467 osutils.WriteFile(self.state, '1 happy-branch')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700468 results = cbuildbot_launch.GetState(self.root)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700469 self.assertEqual(results, (1, 'happy-branch', None))
Don Garrett60967922017-04-12 18:51:44 -0700470
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700471 # Expected contents
472 osutils.WriteFile(self.state, '1 happy-branch 1000.33')
473 version, branch, distfiles_ts = cbuildbot_launch.GetState(self.root)
474 self.assertEqual(version, 1)
475 self.assertEqual(branch, 'happy-branch')
476 self.assertEqual(distfiles_ts, 1000.33)
Don Garrett60967922017-04-12 18:51:44 -0700477
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700478 # Future layout version contents
479 osutils.WriteFile(self.state, '22 happy-branch 222')
480 version, branch, distfiles_ts = cbuildbot_launch.GetState(self.root)
481 self.assertEqual(version, 22)
482 self.assertEqual(branch, 'happy-branch')
483 self.assertEqual(distfiles_ts, 222)
484
485 # Read write
Don Garrettbf90cdf2017-05-19 15:54:02 -0700486 cbuildbot_launch.SetState('happy-branch', self.root)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700487 version, branch, distfiles_ts = cbuildbot_launch.GetState(self.root)
488 self.assertEqual(version, 2)
489 self.assertEqual(branch, 'happy-branch')
490 self.assertIsNotNone(distfiles_ts)
Don Garrett60967922017-04-12 18:51:44 -0700491
Don Garrettbf90cdf2017-05-19 15:54:02 -0700492 def testSetState(self):
493 """Test SetState."""
Don Garrett60967922017-04-12 18:51:44 -0700494 # Write out a state file.
495 osutils.SafeMakedirs(self.root)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700496 cbuildbot_launch.SetState('happy-branch', self.root)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700497 state_file_parts = osutils.ReadFile(self.state).split()
498 self.assertEqual(state_file_parts[:2], ['2', 'happy-branch'])
499 # Will flake if this test takes > 1 hour to run.
500 self.assertGreater(float(state_file_parts[2]), time.time() - 3600)
501
502 # Explicitly provide a timestamp
503 cbuildbot_launch.SetState('happy-branch', self.root,
504 333.33)
505 state_file_parts = osutils.ReadFile(self.state).split()
506 self.assertEqual(state_file_parts,
507 ['2', 'happy-branch', '333.330000'])
Don Garrett60967922017-04-12 18:51:44 -0700508
509 # Change to a future version.
510 self.PatchObject(cbuildbot_launch, 'BUILDROOT_BUILDROOT_LAYOUT', 22)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700511 cbuildbot_launch.SetState('happy-branch', self.root)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700512 state_file_parts = osutils.ReadFile(self.state).split()
513 self.assertEqual(state_file_parts[:2], ['22', 'happy-branch'])
514 # Will flake if this test takes > 1 hour to run.
515 self.assertGreater(float(state_file_parts[2]), time.time() - 3600)