blob: ece44ae23b37ff8f1fd83f6e44236df471b8be92 [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']
95 ARGS_GIT_CACHE = ['--git-cache-dir', '/git-cache']
96 ARGS_CONFIG = ['config']
Ben Zhang30e5aac2017-05-31 15:02:12 +000097 CMD = ['/buildroot/chromite/bin/cbuildbot']
Don Garrett597ddff2017-02-17 18:29:37 -080098
99 def verifyRunCbuildbot(self, args, expected_cmd, version):
Don Garrett86881cb2017-02-15 15:41:55 -0800100 """Ensure we invoke cbuildbot correctly."""
Don Garrett0c54ed72017-03-03 11:18:57 -0800101 options = cbuildbot_launch.PreParseArguments(args)
Don Garrett597ddff2017-02-17 18:29:37 -0800102
103 self.PatchObject(
104 cros_build_lib, 'GetTargetChromiteApiVersion', autospec=True,
105 return_value=version)
106
Ben Zhang30e5aac2017-05-31 15:02:12 +0000107 cbuildbot_launch.RunCbuildbot(options)
Don Garrett597ddff2017-02-17 18:29:37 -0800108
109 self.assertCommandCalled(
Ben Zhang30e5aac2017-05-31 15:02:12 +0000110 expected_cmd, cwd=options.buildroot, error_code_ok=True)
Don Garrett597ddff2017-02-17 18:29:37 -0800111
112 def testRunCbuildbotSimple(self):
113 """Ensure we invoke cbuildbot correctly."""
114 self.verifyRunCbuildbot(
115 self.ARGS_BASE + self.ARGS_CONFIG,
Ben Zhang30e5aac2017-05-31 15:02:12 +0000116 self.CMD + self.ARGS_CONFIG + self.ARGS_BASE,
Don Garrett597ddff2017-02-17 18:29:37 -0800117 (0, 4))
118
119 def testRunCbuildbotNotFiltered(self):
120 """Ensure we invoke cbuildbot correctly."""
121 self.verifyRunCbuildbot(
122 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
Ben Zhang30e5aac2017-05-31 15:02:12 +0000123 self.CMD + self.ARGS_CONFIG + self.ARGS_BASE + 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,
Ben Zhang30e5aac2017-05-31 15:02:12 +0000130 self.CMD + self.ARGS_CONFIG + self.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'
141 mock_repo_create = self.PatchObject(repository, 'RepoRepository',
142 autospec=True, return_value=mock_repo)
Don Garrett0c54ed72017-03-03 11:18:57 -0800143 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildroot',
144 autospec=True)
145 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800146 autospec=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800147
Ben Zhang30e5aac2017-05-31 15:02:12 +0000148 cbuildbot_launch._main(['--buildroot', '/buildroot', 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700149
Don Garrettf324bc32017-05-23 14:00:53 -0700150 # Did we create the repo instance correctly?
151 self.assertEqual(mock_repo_create.mock_calls,
Ben Zhang30e5aac2017-05-31 15:02:12 +0000152 [mock.call(EXPECTED_MANIFEST_URL, '/buildroot',
Don Garrettf324bc32017-05-23 14:00:53 -0700153 git_cache_dir=None, branch='master')])
154
Don Garrett7ade05a2017-02-17 13:31:47 -0800155 # Ensure we clean, as expected.
Ben Zhang30e5aac2017-05-31 15:02:12 +0000156 self.assertEqual(mock_clean.mock_calls,
157 [mock.call('/buildroot', mock_repo,
158 {'branch_name': 'master'})])
Don Garrett7ade05a2017-02-17 13:31:47 -0800159
Don Garrett86881cb2017-02-15 15:41:55 -0800160 # Ensure we checkout, as expected.
161 self.assertEqual(mock_checkout.mock_calls,
Don Garrettf324bc32017-05-23 14:00:53 -0700162 [mock.call(mock_repo)])
Don Garrettc4114cc2016-11-01 20:04:06 -0700163
Don Garrett86881cb2017-02-15 15:41:55 -0800164 # Ensure we invoke cbuildbot, as expected.
165 self.assertCommandCalled(
Ben Zhang30e5aac2017-05-31 15:02:12 +0000166 ['/buildroot/chromite/bin/cbuildbot',
167 'config', '--buildroot', '/buildroot'],
168 cwd='/buildroot',
Don Garrettacbb2392017-05-11 18:27:41 -0700169 error_code_ok=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700170
Don Garrett86881cb2017-02-15 15:41:55 -0800171 def testMainMax(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800172 """Test a larger set of command line options."""
173 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
174 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
Don Garrett861e9182017-05-15 15:30:23 -0700175 autospec=True, return_value=(constants.REEXEC_API_MAJOR,
176 constants.REEXEC_API_MINOR))
Don Garrettf324bc32017-05-23 14:00:53 -0700177 mock_repo = mock.MagicMock()
178 mock_repo.branch = 'branch'
179 mock_repo_create = self.PatchObject(repository, 'RepoRepository',
180 autospec=True, return_value=mock_repo)
Don Garrett0c54ed72017-03-03 11:18:57 -0800181 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildroot',
182 autospec=True)
183 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800184 autospec=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700185
Ben Zhang30e5aac2017-05-31 15:02:12 +0000186 cbuildbot_launch._main(['--buildroot', '/buildroot',
Don Garrettacbb2392017-05-11 18:27:41 -0700187 '--branch', 'branch',
188 '--git-cache-dir', '/git-cache',
189 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700190
Don Garrettf324bc32017-05-23 14:00:53 -0700191 # Did we create the repo instance correctly?
192 self.assertEqual(mock_repo_create.mock_calls,
Ben Zhang30e5aac2017-05-31 15:02:12 +0000193 [mock.call(EXPECTED_MANIFEST_URL, '/buildroot',
Don Garrettf324bc32017-05-23 14:00:53 -0700194 git_cache_dir='/git-cache', branch='branch')])
195
Don Garrett7ade05a2017-02-17 13:31:47 -0800196 # Ensure we clean, as expected.
Ben Zhang30e5aac2017-05-31 15:02:12 +0000197 self.assertEqual(mock_clean.mock_calls,
198 [mock.call('/buildroot', mock_repo,
199 {'branch_name': 'branch'})])
Don Garrett7ade05a2017-02-17 13:31:47 -0800200
Don Garrett86881cb2017-02-15 15:41:55 -0800201 # Ensure we checkout, as expected.
202 self.assertEqual(mock_checkout.mock_calls,
Don Garrettf324bc32017-05-23 14:00:53 -0700203 [mock.call(mock_repo)])
Don Garrett86881cb2017-02-15 15:41:55 -0800204
205 # Ensure we invoke cbuildbot, as expected.
206 self.assertCommandCalled(
Ben Zhang30e5aac2017-05-31 15:02:12 +0000207 ['/buildroot/chromite/bin/cbuildbot',
Don Garrett597ddff2017-02-17 18:29:37 -0800208 'config',
Ben Zhang30e5aac2017-05-31 15:02:12 +0000209 '--buildroot', '/buildroot',
Don Garrett125d4dc2017-04-25 16:26:03 -0700210 '--branch', 'branch',
Don Garrett597ddff2017-02-17 18:29:37 -0800211 '--git-cache-dir', '/git-cache'],
Ben Zhang30e5aac2017-05-31 15:02:12 +0000212 cwd='/buildroot',
Don Garrettacbb2392017-05-11 18:27:41 -0700213 error_code_ok=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800214
215
Don Garrett60967922017-04-12 18:51:44 -0700216class CleanBuildrootTest(cros_test_lib.MockTempDirTestCase):
Don Garrett7ade05a2017-02-17 13:31:47 -0800217 """Tests for CleanBuildroot method."""
218
219 def setUp(self):
220 """Create standard buildroot contents for cleanup."""
Ben Zhang30e5aac2017-05-31 15:02:12 +0000221 self.root = os.path.join(self.tempdir, 'buildroot')
Don Garrette17e1d92017-04-12 15:28:19 -0700222 self.state = os.path.join(self.root, '.cbuildbot_launch_state')
Ben Zhang30e5aac2017-05-31 15:02:12 +0000223 self.repo = os.path.join(self.root, '.repo/repo')
224 self.chroot = os.path.join(self.root, 'chroot/chroot')
225 self.general = os.path.join(self.root, 'general/general')
Don Garrett39963602017-02-27 14:41:58 -0800226 # TODO: Add .cache, and distfiles.
Don Garrett7ade05a2017-02-17 13:31:47 -0800227
Don Garrettf324bc32017-05-23 14:00:53 -0700228 self.mock_repo = mock.MagicMock()
229
Don Garrettacbb2392017-05-11 18:27:41 -0700230 self.metrics = {}
231
Don Garrett60967922017-04-12 18:51:44 -0700232 def populateBuildroot(self, state=None):
Don Garrett7ade05a2017-02-17 13:31:47 -0800233 """Create standard buildroot contents for cleanup."""
Ben Zhang30e5aac2017-05-31 15:02:12 +0000234 osutils.SafeMakedirs(self.root)
235
Don Garrett7ade05a2017-02-17 13:31:47 -0800236 if state:
237 osutils.WriteFile(self.state, state)
238
239 # Create files.
240 for f in (self.repo, self.chroot, self.general):
Don Garrette17e1d92017-04-12 15:28:19 -0700241 osutils.Touch(f, makedirs=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800242
Don Garrette17e1d92017-04-12 15:28:19 -0700243 def testNoBuildroot(self):
Don Garrett7ade05a2017-02-17 13:31:47 -0800244 """Test CleanBuildroot with no history."""
Don Garrettf324bc32017-05-23 14:00:53 -0700245 self.mock_repo.branch = 'master'
246
Ben Zhang30e5aac2017-05-31 15:02:12 +0000247 cbuildbot_launch.CleanBuildroot(self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800248
Ben Zhang30e5aac2017-05-31 15:02:12 +0000249 self.assertEqual(osutils.ReadFile(self.state), '1 master')
Don Garrett7ade05a2017-02-17 13:31:47 -0800250
251 def testBuildrootNoState(self):
252 """Test CleanBuildroot with no state information."""
253 self.populateBuildroot()
Don Garrettf324bc32017-05-23 14:00:53 -0700254 self.mock_repo.branch = 'master'
Don Garrett7ade05a2017-02-17 13:31:47 -0800255
Ben Zhang30e5aac2017-05-31 15:02:12 +0000256 cbuildbot_launch.CleanBuildroot(self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800257
Ben Zhang30e5aac2017-05-31 15:02:12 +0000258 self.assertEqual(osutils.ReadFile(self.state), '1 master')
Don Garrett60967922017-04-12 18:51:44 -0700259 self.assertNotExists(self.repo)
Don Garrett7ade05a2017-02-17 13:31:47 -0800260 self.assertNotExists(self.chroot)
Don Garrett60967922017-04-12 18:51:44 -0700261 self.assertNotExists(self.general)
262
263 def testBuildrootFormatMismatch(self):
264 """Test CleanBuildroot with no state information."""
Don Garrett125d4dc2017-04-25 16:26:03 -0700265 self.populateBuildroot('0 master')
Don Garrettf324bc32017-05-23 14:00:53 -0700266 self.mock_repo.branch = 'master'
Don Garrett60967922017-04-12 18:51:44 -0700267
Ben Zhang30e5aac2017-05-31 15:02:12 +0000268 cbuildbot_launch.CleanBuildroot(self.root, self.mock_repo, self.metrics)
Don Garrett60967922017-04-12 18:51:44 -0700269
Ben Zhang30e5aac2017-05-31 15:02:12 +0000270 self.assertEqual(osutils.ReadFile(self.state), '1 master')
Don Garrett60967922017-04-12 18:51:44 -0700271 self.assertNotExists(self.repo)
272 self.assertNotExists(self.chroot)
273 self.assertNotExists(self.general)
Don Garrett7ade05a2017-02-17 13:31:47 -0800274
275 def testBuildrootBranchChange(self):
276 """Test CleanBuildroot with a change in branches."""
Ben Zhang30e5aac2017-05-31 15:02:12 +0000277 self.populateBuildroot('1 branchA')
Don Garrettf324bc32017-05-23 14:00:53 -0700278 self.mock_repo.branch = 'branchB'
Don Garrett7ade05a2017-02-17 13:31:47 -0800279
Ben Zhang30e5aac2017-05-31 15:02:12 +0000280 cbuildbot_launch.CleanBuildroot(self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800281
Ben Zhang30e5aac2017-05-31 15:02:12 +0000282 self.assertEqual(osutils.ReadFile(self.state), '1 branchB')
Don Garrett7ade05a2017-02-17 13:31:47 -0800283 self.assertExists(self.repo)
284 self.assertNotExists(self.chroot)
285 self.assertExists(self.general)
286
287 def testBuildrootBranchMatch(self):
288 """Test CleanBuildroot with no change in branch."""
Ben Zhang30e5aac2017-05-31 15:02:12 +0000289 self.populateBuildroot('1 branchA')
Don Garrettf324bc32017-05-23 14:00:53 -0700290 self.mock_repo.branch = 'branchA'
Don Garrett7ade05a2017-02-17 13:31:47 -0800291
Ben Zhang30e5aac2017-05-31 15:02:12 +0000292 cbuildbot_launch.CleanBuildroot(self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800293
Ben Zhang30e5aac2017-05-31 15:02:12 +0000294 self.assertEqual(osutils.ReadFile(self.state), '1 branchA')
Don Garrett7ade05a2017-02-17 13:31:47 -0800295 self.assertExists(self.repo)
296 self.assertExists(self.chroot)
297 self.assertExists(self.general)
Don Garrette17e1d92017-04-12 15:28:19 -0700298
Don Garrettf324bc32017-05-23 14:00:53 -0700299 def testBuildrootRepoCleanFailure(self):
300 """Test CleanBuildroot with repo checkout failure."""
301 self.populateBuildroot('1 branchA')
302 self.mock_repo.branch = 'branchA'
303 self.mock_repo.BuildRootGitCleanup.side_effect = Exception
304
Ben Zhang30e5aac2017-05-31 15:02:12 +0000305 cbuildbot_launch.CleanBuildroot(self.root, self.mock_repo, self.metrics)
Don Garrettf324bc32017-05-23 14:00:53 -0700306
Ben Zhang30e5aac2017-05-31 15:02:12 +0000307 self.assertEqual(osutils.ReadFile(self.state), '1 branchA')
Don Garrettf324bc32017-05-23 14:00:53 -0700308 self.assertNotExists(self.repo)
309 self.assertNotExists(self.chroot)
310 self.assertNotExists(self.general)
311
Don Garrett60967922017-04-12 18:51:44 -0700312 def testGetBuildrootState(self):
313 """Test GetBuildrootState."""
314 # No root dir.
315 results = cbuildbot_launch.GetBuildrootState(self.root)
316 self.assertEqual(results, (0, ''))
317
318 # Empty root dir.
319 osutils.SafeMakedirs(self.root)
320 results = cbuildbot_launch.GetBuildrootState(self.root)
321 self.assertEqual(results, (0, ''))
322
323 # Empty Contents
324 osutils.WriteFile(self.state, '')
325 results = cbuildbot_launch.GetBuildrootState(self.root)
326 self.assertEqual(results, (0, ''))
327
328 # Old Format Contents
329 osutils.WriteFile(self.state, 'happy-branch')
330 results = cbuildbot_launch.GetBuildrootState(self.root)
331 self.assertEqual(results, (0, ''))
332
333 # Expected Contents
334 osutils.WriteFile(self.state, '1 happy-branch')
335 results = cbuildbot_launch.GetBuildrootState(self.root)
336 self.assertEqual(results, (1, 'happy-branch'))
337
338 # Future Contents
339 osutils.WriteFile(self.state, '22 master')
340 results = cbuildbot_launch.GetBuildrootState(self.root)
341 self.assertEqual(results, (22, 'master'))
342
343 # Read Write
344 cbuildbot_launch.SetBuildrootState('happy-branch', self.root)
345 results = cbuildbot_launch.GetBuildrootState(self.root)
Ben Zhang30e5aac2017-05-31 15:02:12 +0000346 self.assertEqual(results, (1, 'happy-branch'))
Don Garrett60967922017-04-12 18:51:44 -0700347
348 def testSetBuildrootState(self):
349 """Test SetBuildrootState."""
350 # Write out a state file.
351 osutils.SafeMakedirs(self.root)
352 cbuildbot_launch.SetBuildrootState('happy-branch', self.root)
Ben Zhang30e5aac2017-05-31 15:02:12 +0000353 self.assertEqual(osutils.ReadFile(self.state), '1 happy-branch')
Don Garrett60967922017-04-12 18:51:44 -0700354
355 # Change to a future version.
356 self.PatchObject(cbuildbot_launch, 'BUILDROOT_BUILDROOT_LAYOUT', 22)
357 cbuildbot_launch.SetBuildrootState('happy-branch', self.root)
358 self.assertEqual(osutils.ReadFile(self.state), '22 happy-branch')