blob: 3e6f75b423dbd121efe29905f04574ad1ee0b8ff [file] [log] [blame]
Don Garrettc4114cc2016-11-01 20:04:06 -07001# Copyright 2016 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Unit tests for chromite.lib.git and helpers for testing that module."""
6
7from __future__ import print_function
8
Don Garrett86881cb2017-02-15 15:41:55 -08009import mock
Don Garrett7ade05a2017-02-17 13:31:47 -080010import os
Don Garrett86881cb2017-02-15 15:41:55 -080011
12from chromite.cbuildbot import repository
Don Garrett861e9182017-05-15 15:30:23 -070013from chromite.lib import constants
Don Garrett597ddff2017-02-17 18:29:37 -080014from chromite.lib import cros_build_lib
Don Garrettc4114cc2016-11-01 20:04:06 -070015from chromite.lib import cros_build_lib_unittest
Don Garrett7ade05a2017-02-17 13:31:47 -080016from chromite.lib import cros_test_lib
Don Garrett86881cb2017-02-15 15:41:55 -080017from chromite.lib import osutils
Don Garrett0c54ed72017-03-03 11:18:57 -080018from chromite.scripts import cbuildbot_launch
Don Garrett86881cb2017-02-15 15:41:55 -080019
Don Garrett60967922017-04-12 18:51:44 -070020
Don Garrett86881cb2017-02-15 15:41:55 -080021EXPECTED_MANIFEST_URL = 'https://chrome-internal-review.googlesource.com/chromeos/manifest-internal' # pylint: disable=line-too-long
Don Garrettc4114cc2016-11-01 20:04:06 -070022
23
Don Garrettacbb2392017-05-11 18:27:41 -070024# It's reasonable for unittests to look at internals.
25# pylint: disable=protected-access
26
27
Don Garrett8d314792017-05-18 13:11:42 -070028class FakeException(Exception):
29 """Test exception to raise during tests."""
30
31
Don Garrett0c54ed72017-03-03 11:18:57 -080032class CbuildbotLaunchTest(cros_test_lib.MockTestCase):
33 """Tests for cbuildbot_launch script."""
Don Garrettc4114cc2016-11-01 20:04:06 -070034
Don Garrett86881cb2017-02-15 15:41:55 -080035 def testPreParseArguments(self):
Don Garrettc4114cc2016-11-01 20:04:06 -070036 """Test that we can correctly extract branch values from cbuildbot args."""
Don Garrett597ddff2017-02-17 18:29:37 -080037 CASES = (
38 (['--buildroot', '/buildroot', 'daisy-incremental'],
39 (None, '/buildroot', None)),
40
41 (['--buildbot', '--buildroot', '/buildroot',
42 '--git-cache-dir', '/git-cache',
43 '-b', 'release-R57-9202.B',
44 'daisy-incremental'],
45 ('release-R57-9202.B', '/buildroot', '/git-cache')),
46
47 (['--debug', '--buildbot', '--notests',
48 '--buildroot', '/buildroot',
49 '--git-cache-dir', '/git-cache',
50 '--branch', 'release-R57-9202.B',
51 'daisy-incremental'],
52 ('release-R57-9202.B', '/buildroot', '/git-cache')),
Don Garrettc4114cc2016-11-01 20:04:06 -070053 )
54
Don Garrett597ddff2017-02-17 18:29:37 -080055 for cmd_args, expected in CASES:
56 expected_branch, expected_buildroot, expected_cache_dir = expected
57
Don Garrett0c54ed72017-03-03 11:18:57 -080058 options = cbuildbot_launch.PreParseArguments(cmd_args)
Don Garrett597ddff2017-02-17 18:29:37 -080059
60 self.assertEqual(options.branch, expected_branch)
61 self.assertEqual(options.buildroot, expected_buildroot)
62 self.assertEqual(options.git_cache_dir, expected_cache_dir)
Don Garrett86881cb2017-02-15 15:41:55 -080063
Don Garrettf324bc32017-05-23 14:00:53 -070064 def testInitialCheckout(self):
Don Garrett86881cb2017-02-15 15:41:55 -080065 """Test InitialCheckout with minimum settings."""
Don Garrettf324bc32017-05-23 14:00:53 -070066 mock_repo = mock.MagicMock()
67 mock_repo.branch = 'branch'
Don Garrett86881cb2017-02-15 15:41:55 -080068
Don Garrettf324bc32017-05-23 14:00:53 -070069 cbuildbot_launch.InitialCheckout(mock_repo)
Don Garrett86881cb2017-02-15 15:41:55 -080070
71 self.assertEqual(mock_repo.mock_calls, [
Don Garrettf324bc32017-05-23 14:00:53 -070072 mock.call.Sync(detach=True),
Don Garrett8d314792017-05-18 13:11:42 -070073 ])
74
Don Garrettf15d65b2017-04-12 12:39:55 -070075 def testConfigureGlobalEnvironment(self):
Don Garrett60967922017-04-12 18:51:44 -070076 """Ensure that we can setup our global runtime environment correctly."""
Don Garrett86fec482017-05-17 18:13:33 -070077
78 os.environ.pop('LANG', None)
79 os.environ['LC_MONETARY'] = 'bad'
80
Don Garrettf15d65b2017-04-12 12:39:55 -070081 cbuildbot_launch.ConfigureGlobalEnvironment()
82
Don Garrett86fec482017-05-17 18:13:33 -070083 # Verify umask is updated.
Don Garrettf15d65b2017-04-12 12:39:55 -070084 self.assertEqual(os.umask(0), 0o22)
85
Don Garrett86fec482017-05-17 18:13:33 -070086 # Verify ENVs are cleaned up.
87 self.assertEqual(os.environ['LANG'], 'en_US.UTF-8')
88 self.assertNotIn('LC_MONETARY', os.environ)
89
Don Garrettf15d65b2017-04-12 12:39:55 -070090
Don Garrett597ddff2017-02-17 18:29:37 -080091class RunTests(cros_build_lib_unittest.RunCommandTestCase):
Don Garrett0c54ed72017-03-03 11:18:57 -080092 """Tests for cbuildbot_launch script."""
Don Garrett597ddff2017-02-17 18:29:37 -080093
94 ARGS_BASE = ['--buildroot', '/buildroot']
Don Garrettbf90cdf2017-05-19 15:54:02 -070095 EXPECTED_ARGS_BASE = ['--buildroot', '/cbuildbot_buildroot']
Don Garrett597ddff2017-02-17 18:29:37 -080096 ARGS_GIT_CACHE = ['--git-cache-dir', '/git-cache']
97 ARGS_CONFIG = ['config']
Don Garrettbf90cdf2017-05-19 15:54:02 -070098 CMD = ['/cbuildbot_buildroot/chromite/bin/cbuildbot']
Don Garrett597ddff2017-02-17 18:29:37 -080099
100 def verifyRunCbuildbot(self, args, expected_cmd, version):
Don Garrett86881cb2017-02-15 15:41:55 -0800101 """Ensure we invoke cbuildbot correctly."""
Don Garrett597ddff2017-02-17 18:29:37 -0800102 self.PatchObject(
103 cros_build_lib, 'GetTargetChromiteApiVersion', autospec=True,
104 return_value=version)
105
Don Garrettd1d90dd2017-06-13 17:35:52 -0700106 cbuildbot_launch.RunCbuildbot('/cbuildbot_buildroot', args)
Don Garrett597ddff2017-02-17 18:29:37 -0800107
108 self.assertCommandCalled(
Don Garrettbf90cdf2017-05-19 15:54:02 -0700109 expected_cmd, cwd='/cbuildbot_buildroot', error_code_ok=True)
Don Garrett597ddff2017-02-17 18:29:37 -0800110
111 def testRunCbuildbotSimple(self):
112 """Ensure we invoke cbuildbot correctly."""
113 self.verifyRunCbuildbot(
114 self.ARGS_BASE + self.ARGS_CONFIG,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700115 self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE,
Don Garrett597ddff2017-02-17 18:29:37 -0800116 (0, 4))
117
118 def testRunCbuildbotNotFiltered(self):
119 """Ensure we invoke cbuildbot correctly."""
120 self.verifyRunCbuildbot(
121 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700122 (self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE +
123 self.ARGS_GIT_CACHE),
Don Garrett597ddff2017-02-17 18:29:37 -0800124 (0, 4))
125
126 def testRunCbuildbotFiltered(self):
127 """Ensure we invoke cbuildbot correctly."""
128 self.verifyRunCbuildbot(
129 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700130 self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE,
Don Garrett597ddff2017-02-17 18:29:37 -0800131 (0, 2))
Don Garrettc4114cc2016-11-01 20:04:06 -0700132
Don Garrett86881cb2017-02-15 15:41:55 -0800133 def testMainMin(self):
134 """Test a minimal set of command line options."""
Don Garrett597ddff2017-02-17 18:29:37 -0800135 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
136 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
Don Garrett861e9182017-05-15 15:30:23 -0700137 autospec=True, return_value=(constants.REEXEC_API_MAJOR,
138 constants.REEXEC_API_MINOR))
Don Garrettf324bc32017-05-23 14:00:53 -0700139 mock_repo = mock.MagicMock()
140 mock_repo.branch = 'master'
Don Garrettbf90cdf2017-05-19 15:54:02 -0700141 mock_repo.directory = '/root/repository'
142
Don Garrettf324bc32017-05-23 14:00:53 -0700143 mock_repo_create = self.PatchObject(repository, 'RepoRepository',
144 autospec=True, return_value=mock_repo)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700145 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot',
Don Garrett0c54ed72017-03-03 11:18:57 -0800146 autospec=True)
147 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800148 autospec=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800149
Don Garrettbf90cdf2017-05-19 15:54:02 -0700150 cbuildbot_launch._main(['-r', '/root', 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700151
Don Garrettf324bc32017-05-23 14:00:53 -0700152 # Did we create the repo instance correctly?
153 self.assertEqual(mock_repo_create.mock_calls,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700154 [mock.call(EXPECTED_MANIFEST_URL, '/root/repository',
Don Garrettf324bc32017-05-23 14:00:53 -0700155 git_cache_dir=None, branch='master')])
156
Don Garrett7ade05a2017-02-17 13:31:47 -0800157 # Ensure we clean, as expected.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700158 self.assertEqual(mock_clean.mock_calls, [
Don Garrettd1d90dd2017-06-13 17:35:52 -0700159 mock.call('/root', mock_repo,
160 {
161 'branch_name': 'master',
162 'tryjob': False,
163 'build_config': 'config',
164 })])
Don Garrett7ade05a2017-02-17 13:31:47 -0800165
Don Garrett86881cb2017-02-15 15:41:55 -0800166 # Ensure we checkout, as expected.
167 self.assertEqual(mock_checkout.mock_calls,
Don Garrettf324bc32017-05-23 14:00:53 -0700168 [mock.call(mock_repo)])
Don Garrettc4114cc2016-11-01 20:04:06 -0700169
Don Garrett86881cb2017-02-15 15:41:55 -0800170 # Ensure we invoke cbuildbot, as expected.
171 self.assertCommandCalled(
Don Garrettbf90cdf2017-05-19 15:54:02 -0700172 ['/root/repository/chromite/bin/cbuildbot',
173 'config', '-r', '/root/repository'],
174 cwd='/root/repository',
Don Garrettacbb2392017-05-11 18:27:41 -0700175 error_code_ok=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700176
Don Garrett86881cb2017-02-15 15:41:55 -0800177 def testMainMax(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800178 """Test a larger set of command line options."""
179 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
180 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
Don Garrett861e9182017-05-15 15:30:23 -0700181 autospec=True, return_value=(constants.REEXEC_API_MAJOR,
182 constants.REEXEC_API_MINOR))
Don Garrettf324bc32017-05-23 14:00:53 -0700183 mock_repo = mock.MagicMock()
184 mock_repo.branch = 'branch'
Don Garrettbf90cdf2017-05-19 15:54:02 -0700185 mock_repo.directory = '/root/repository'
186
Don Garrettf324bc32017-05-23 14:00:53 -0700187 mock_repo_create = self.PatchObject(repository, 'RepoRepository',
188 autospec=True, return_value=mock_repo)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700189 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot',
Don Garrett0c54ed72017-03-03 11:18:57 -0800190 autospec=True)
191 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800192 autospec=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700193
Don Garrettbf90cdf2017-05-19 15:54:02 -0700194 cbuildbot_launch._main(['--buildroot', '/root',
Don Garrettacbb2392017-05-11 18:27:41 -0700195 '--branch', 'branch',
196 '--git-cache-dir', '/git-cache',
Don Garrettd1d90dd2017-06-13 17:35:52 -0700197 '--remote-trybot',
Don Garrettacbb2392017-05-11 18:27:41 -0700198 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700199
Don Garrettf324bc32017-05-23 14:00:53 -0700200 # Did we create the repo instance correctly?
201 self.assertEqual(mock_repo_create.mock_calls,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700202 [mock.call(EXPECTED_MANIFEST_URL, '/root/repository',
Don Garrettf324bc32017-05-23 14:00:53 -0700203 git_cache_dir='/git-cache', branch='branch')])
204
Don Garrett7ade05a2017-02-17 13:31:47 -0800205 # Ensure we clean, as expected.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700206 self.assertEqual(mock_clean.mock_calls, [
Don Garrettd1d90dd2017-06-13 17:35:52 -0700207 mock.call('/root',
208 mock_repo,
209 {
210 'branch_name': 'branch',
211 'tryjob': True,
212 'build_config': 'config',
213 })])
Don Garrett7ade05a2017-02-17 13:31:47 -0800214
Don Garrett86881cb2017-02-15 15:41:55 -0800215 # Ensure we checkout, as expected.
216 self.assertEqual(mock_checkout.mock_calls,
Don Garrettf324bc32017-05-23 14:00:53 -0700217 [mock.call(mock_repo)])
Don Garrett86881cb2017-02-15 15:41:55 -0800218
219 # Ensure we invoke cbuildbot, as expected.
220 self.assertCommandCalled(
Don Garrettbf90cdf2017-05-19 15:54:02 -0700221 ['/root/repository/chromite/bin/cbuildbot',
Don Garrett597ddff2017-02-17 18:29:37 -0800222 'config',
Don Garrettbf90cdf2017-05-19 15:54:02 -0700223 '--buildroot', '/root/repository',
Don Garrett125d4dc2017-04-25 16:26:03 -0700224 '--branch', 'branch',
Don Garrettd1d90dd2017-06-13 17:35:52 -0700225 '--git-cache-dir', '/git-cache',
226 '--remote-trybot'],
Don Garrettbf90cdf2017-05-19 15:54:02 -0700227 cwd='/root/repository',
Don Garrettacbb2392017-05-11 18:27:41 -0700228 error_code_ok=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800229
230
Don Garrettbf90cdf2017-05-19 15:54:02 -0700231class CleanBuildRootTest(cros_test_lib.MockTempDirTestCase):
232 """Tests for CleanBuildRoot method."""
Don Garrett7ade05a2017-02-17 13:31:47 -0800233
234 def setUp(self):
235 """Create standard buildroot contents for cleanup."""
Don Garrettbf90cdf2017-05-19 15:54:02 -0700236 self.root = os.path.join(self.tempdir)
Don Garrette17e1d92017-04-12 15:28:19 -0700237 self.state = os.path.join(self.root, '.cbuildbot_launch_state')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700238 self.buildroot = os.path.join(self.root, 'buildroot')
239 self.repo = os.path.join(self.buildroot, '.repo/repo')
240 self.chroot = os.path.join(self.buildroot, 'chroot/chroot')
241 self.general = os.path.join(self.buildroot, 'general/general')
Don Garrett39963602017-02-27 14:41:58 -0800242 # TODO: Add .cache, and distfiles.
Don Garrett7ade05a2017-02-17 13:31:47 -0800243
Don Garrettf324bc32017-05-23 14:00:53 -0700244 self.mock_repo = mock.MagicMock()
Don Garrettbf90cdf2017-05-19 15:54:02 -0700245 self.mock_repo.directory = self.buildroot
Don Garrettf324bc32017-05-23 14:00:53 -0700246
Don Garrettacbb2392017-05-11 18:27:41 -0700247 self.metrics = {}
248
Don Garrett60967922017-04-12 18:51:44 -0700249 def populateBuildroot(self, state=None):
Don Garrett7ade05a2017-02-17 13:31:47 -0800250 """Create standard buildroot contents for cleanup."""
251 if state:
Don Garrettbf90cdf2017-05-19 15:54:02 -0700252 osutils.SafeMakedirs(self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800253 osutils.WriteFile(self.state, state)
254
255 # Create files.
256 for f in (self.repo, self.chroot, self.general):
Don Garrette17e1d92017-04-12 15:28:19 -0700257 osutils.Touch(f, makedirs=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800258
Don Garrette17e1d92017-04-12 15:28:19 -0700259 def testNoBuildroot(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700260 """Test CleanBuildRoot with no history."""
Don Garrettf324bc32017-05-23 14:00:53 -0700261 self.mock_repo.branch = 'master'
262
Don Garrettbf90cdf2017-05-19 15:54:02 -0700263 cbuildbot_launch.CleanBuildRoot(
264 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800265
Don Garrettbf90cdf2017-05-19 15:54:02 -0700266 self.assertEqual(osutils.ReadFile(self.state), '2 master')
Don Garrett7ade05a2017-02-17 13:31:47 -0800267
268 def testBuildrootNoState(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700269 """Test CleanBuildRoot with no state information."""
Don Garrett7ade05a2017-02-17 13:31:47 -0800270 self.populateBuildroot()
Don Garrettf324bc32017-05-23 14:00:53 -0700271 self.mock_repo.branch = 'master'
Don Garrett7ade05a2017-02-17 13:31:47 -0800272
Don Garrettbf90cdf2017-05-19 15:54:02 -0700273 cbuildbot_launch.CleanBuildRoot(
274 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800275
Don Garrettbf90cdf2017-05-19 15:54:02 -0700276 self.assertEqual(osutils.ReadFile(self.state), '2 master')
Don Garrett60967922017-04-12 18:51:44 -0700277 self.assertNotExists(self.repo)
Don Garrett7ade05a2017-02-17 13:31:47 -0800278 self.assertNotExists(self.chroot)
Don Garrett60967922017-04-12 18:51:44 -0700279 self.assertNotExists(self.general)
280
281 def testBuildrootFormatMismatch(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700282 """Test CleanBuildRoot with no state information."""
Don Garrett125d4dc2017-04-25 16:26:03 -0700283 self.populateBuildroot('0 master')
Don Garrettf324bc32017-05-23 14:00:53 -0700284 self.mock_repo.branch = 'master'
Don Garrett60967922017-04-12 18:51:44 -0700285
Don Garrettbf90cdf2017-05-19 15:54:02 -0700286 cbuildbot_launch.CleanBuildRoot(
287 self.root, self.mock_repo, self.metrics)
Don Garrett60967922017-04-12 18:51:44 -0700288
Don Garrettbf90cdf2017-05-19 15:54:02 -0700289 self.assertEqual(osutils.ReadFile(self.state), '2 master')
Don Garrett60967922017-04-12 18:51:44 -0700290 self.assertNotExists(self.repo)
291 self.assertNotExists(self.chroot)
292 self.assertNotExists(self.general)
Don Garrett7ade05a2017-02-17 13:31:47 -0800293
294 def testBuildrootBranchChange(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700295 """Test CleanBuildRoot with a change in branches."""
296 self.populateBuildroot('2 branchA')
Don Garrettf324bc32017-05-23 14:00:53 -0700297 self.mock_repo.branch = 'branchB'
Don Garrett7ade05a2017-02-17 13:31:47 -0800298
Don Garrettbf90cdf2017-05-19 15:54:02 -0700299 cbuildbot_launch.CleanBuildRoot(
300 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800301
Don Garrettbf90cdf2017-05-19 15:54:02 -0700302 self.assertEqual(osutils.ReadFile(self.state), '2 branchB')
Don Garrett7ade05a2017-02-17 13:31:47 -0800303 self.assertExists(self.repo)
304 self.assertNotExists(self.chroot)
305 self.assertExists(self.general)
306
307 def testBuildrootBranchMatch(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700308 """Test CleanBuildRoot with no change in branch."""
309 self.populateBuildroot('2 branchA')
Don Garrettf324bc32017-05-23 14:00:53 -0700310 self.mock_repo.branch = 'branchA'
Don Garrett7ade05a2017-02-17 13:31:47 -0800311
Don Garrettbf90cdf2017-05-19 15:54:02 -0700312 cbuildbot_launch.CleanBuildRoot(
313 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800314
Don Garrettbf90cdf2017-05-19 15:54:02 -0700315 self.assertEqual(osutils.ReadFile(self.state), '2 branchA')
Don Garrett7ade05a2017-02-17 13:31:47 -0800316 self.assertExists(self.repo)
317 self.assertExists(self.chroot)
318 self.assertExists(self.general)
Don Garrette17e1d92017-04-12 15:28:19 -0700319
Don Garrettf324bc32017-05-23 14:00:53 -0700320 def testBuildrootRepoCleanFailure(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700321 """Test CleanBuildRoot with repo checkout failure."""
Don Garrettf324bc32017-05-23 14:00:53 -0700322 self.populateBuildroot('1 branchA')
323 self.mock_repo.branch = 'branchA'
324 self.mock_repo.BuildRootGitCleanup.side_effect = Exception
325
Don Garrettbf90cdf2017-05-19 15:54:02 -0700326 cbuildbot_launch.CleanBuildRoot(
327 self.root, self.mock_repo, self.metrics)
Don Garrettf324bc32017-05-23 14:00:53 -0700328
Don Garrettbf90cdf2017-05-19 15:54:02 -0700329 self.assertEqual(osutils.ReadFile(self.state), '2 branchA')
Don Garrettf324bc32017-05-23 14:00:53 -0700330 self.assertNotExists(self.repo)
331 self.assertNotExists(self.chroot)
332 self.assertNotExists(self.general)
333
Don Garrettbf90cdf2017-05-19 15:54:02 -0700334 def testGetState(self):
335 """Test GetState."""
Don Garrett60967922017-04-12 18:51:44 -0700336 # No root dir.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700337 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700338 self.assertEqual(results, (0, ''))
339
340 # Empty root dir.
341 osutils.SafeMakedirs(self.root)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700342 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700343 self.assertEqual(results, (0, ''))
344
345 # Empty Contents
346 osutils.WriteFile(self.state, '')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700347 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700348 self.assertEqual(results, (0, ''))
349
350 # Old Format Contents
351 osutils.WriteFile(self.state, 'happy-branch')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700352 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700353 self.assertEqual(results, (0, ''))
354
355 # Expected Contents
356 osutils.WriteFile(self.state, '1 happy-branch')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700357 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700358 self.assertEqual(results, (1, 'happy-branch'))
359
360 # Future Contents
361 osutils.WriteFile(self.state, '22 master')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700362 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700363 self.assertEqual(results, (22, 'master'))
364
365 # Read Write
Don Garrettbf90cdf2017-05-19 15:54:02 -0700366 cbuildbot_launch.SetState('happy-branch', self.root)
367 results = cbuildbot_launch.GetState(self.root)
368 self.assertEqual(results, (2, 'happy-branch'))
Don Garrett60967922017-04-12 18:51:44 -0700369
Don Garrettbf90cdf2017-05-19 15:54:02 -0700370 def testSetState(self):
371 """Test SetState."""
Don Garrett60967922017-04-12 18:51:44 -0700372 # Write out a state file.
373 osutils.SafeMakedirs(self.root)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700374 cbuildbot_launch.SetState('happy-branch', self.root)
375 self.assertEqual(osutils.ReadFile(self.state), '2 happy-branch')
Don Garrett60967922017-04-12 18:51:44 -0700376
377 # Change to a future version.
378 self.PatchObject(cbuildbot_launch, 'BUILDROOT_BUILDROOT_LAYOUT', 22)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700379 cbuildbot_launch.SetState('happy-branch', self.root)
Don Garrett60967922017-04-12 18:51:44 -0700380 self.assertEqual(osutils.ReadFile(self.state), '22 happy-branch')