blob: ef6b7fe66286602c7002e06f8d62e120da1af469 [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 Garrett0c54ed72017-03-03 11:18:57 -0800102 options = cbuildbot_launch.PreParseArguments(args)
Don Garrett597ddff2017-02-17 18:29:37 -0800103
104 self.PatchObject(
105 cros_build_lib, 'GetTargetChromiteApiVersion', autospec=True,
106 return_value=version)
107
Don Garrettbf90cdf2017-05-19 15:54:02 -0700108 cbuildbot_launch.RunCbuildbot('/cbuildbot_buildroot', options)
Don Garrett597ddff2017-02-17 18:29:37 -0800109
110 self.assertCommandCalled(
Don Garrettbf90cdf2017-05-19 15:54:02 -0700111 expected_cmd, cwd='/cbuildbot_buildroot', error_code_ok=True)
Don Garrett597ddff2017-02-17 18:29:37 -0800112
113 def testRunCbuildbotSimple(self):
114 """Ensure we invoke cbuildbot correctly."""
115 self.verifyRunCbuildbot(
116 self.ARGS_BASE + self.ARGS_CONFIG,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700117 self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE,
Don Garrett597ddff2017-02-17 18:29:37 -0800118 (0, 4))
119
120 def testRunCbuildbotNotFiltered(self):
121 """Ensure we invoke cbuildbot correctly."""
122 self.verifyRunCbuildbot(
123 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700124 (self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE +
125 self.ARGS_GIT_CACHE),
Don Garrett597ddff2017-02-17 18:29:37 -0800126 (0, 4))
127
128 def testRunCbuildbotFiltered(self):
129 """Ensure we invoke cbuildbot correctly."""
130 self.verifyRunCbuildbot(
131 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700132 self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE,
Don Garrett597ddff2017-02-17 18:29:37 -0800133 (0, 2))
Don Garrettc4114cc2016-11-01 20:04:06 -0700134
Don Garrett86881cb2017-02-15 15:41:55 -0800135 def testMainMin(self):
136 """Test a minimal set of command line options."""
Don Garrett597ddff2017-02-17 18:29:37 -0800137 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
138 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
Don Garrett861e9182017-05-15 15:30:23 -0700139 autospec=True, return_value=(constants.REEXEC_API_MAJOR,
140 constants.REEXEC_API_MINOR))
Don Garrettf324bc32017-05-23 14:00:53 -0700141 mock_repo = mock.MagicMock()
142 mock_repo.branch = 'master'
Don Garrettbf90cdf2017-05-19 15:54:02 -0700143 mock_repo.directory = '/root/repository'
144
Don Garrettf324bc32017-05-23 14:00:53 -0700145 mock_repo_create = self.PatchObject(repository, 'RepoRepository',
146 autospec=True, return_value=mock_repo)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700147 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot',
Don Garrett0c54ed72017-03-03 11:18:57 -0800148 autospec=True)
149 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800150 autospec=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800151
Don Garrettbf90cdf2017-05-19 15:54:02 -0700152 cbuildbot_launch._main(['-r', '/root', 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700153
Don Garrettf324bc32017-05-23 14:00:53 -0700154 # Did we create the repo instance correctly?
155 self.assertEqual(mock_repo_create.mock_calls,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700156 [mock.call(EXPECTED_MANIFEST_URL, '/root/repository',
Don Garrettf324bc32017-05-23 14:00:53 -0700157 git_cache_dir=None, branch='master')])
158
Don Garrett7ade05a2017-02-17 13:31:47 -0800159 # Ensure we clean, as expected.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700160 self.assertEqual(mock_clean.mock_calls, [
161 mock.call('/root', mock_repo, {'branch_name': 'master'})])
Don Garrett7ade05a2017-02-17 13:31:47 -0800162
Don Garrett86881cb2017-02-15 15:41:55 -0800163 # Ensure we checkout, as expected.
164 self.assertEqual(mock_checkout.mock_calls,
Don Garrettf324bc32017-05-23 14:00:53 -0700165 [mock.call(mock_repo)])
Don Garrettc4114cc2016-11-01 20:04:06 -0700166
Don Garrett86881cb2017-02-15 15:41:55 -0800167 # Ensure we invoke cbuildbot, as expected.
168 self.assertCommandCalled(
Don Garrettbf90cdf2017-05-19 15:54:02 -0700169 ['/root/repository/chromite/bin/cbuildbot',
170 'config', '-r', '/root/repository'],
171 cwd='/root/repository',
Don Garrettacbb2392017-05-11 18:27:41 -0700172 error_code_ok=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700173
Don Garrett86881cb2017-02-15 15:41:55 -0800174 def testMainMax(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800175 """Test a larger set of command line options."""
176 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
177 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
Don Garrett861e9182017-05-15 15:30:23 -0700178 autospec=True, return_value=(constants.REEXEC_API_MAJOR,
179 constants.REEXEC_API_MINOR))
Don Garrettf324bc32017-05-23 14:00:53 -0700180 mock_repo = mock.MagicMock()
181 mock_repo.branch = 'branch'
Don Garrettbf90cdf2017-05-19 15:54:02 -0700182 mock_repo.directory = '/root/repository'
183
Don Garrettf324bc32017-05-23 14:00:53 -0700184 mock_repo_create = self.PatchObject(repository, 'RepoRepository',
185 autospec=True, return_value=mock_repo)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700186 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot',
Don Garrett0c54ed72017-03-03 11:18:57 -0800187 autospec=True)
188 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800189 autospec=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700190
Don Garrettbf90cdf2017-05-19 15:54:02 -0700191 cbuildbot_launch._main(['--buildroot', '/root',
Don Garrettacbb2392017-05-11 18:27:41 -0700192 '--branch', 'branch',
193 '--git-cache-dir', '/git-cache',
194 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700195
Don Garrettf324bc32017-05-23 14:00:53 -0700196 # Did we create the repo instance correctly?
197 self.assertEqual(mock_repo_create.mock_calls,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700198 [mock.call(EXPECTED_MANIFEST_URL, '/root/repository',
Don Garrettf324bc32017-05-23 14:00:53 -0700199 git_cache_dir='/git-cache', branch='branch')])
200
Don Garrett7ade05a2017-02-17 13:31:47 -0800201 # Ensure we clean, as expected.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700202 self.assertEqual(mock_clean.mock_calls, [
203 mock.call('/root', mock_repo, {'branch_name': 'branch'})])
Don Garrett7ade05a2017-02-17 13:31:47 -0800204
Don Garrett86881cb2017-02-15 15:41:55 -0800205 # Ensure we checkout, as expected.
206 self.assertEqual(mock_checkout.mock_calls,
Don Garrettf324bc32017-05-23 14:00:53 -0700207 [mock.call(mock_repo)])
Don Garrett86881cb2017-02-15 15:41:55 -0800208
209 # Ensure we invoke cbuildbot, as expected.
210 self.assertCommandCalled(
Don Garrettbf90cdf2017-05-19 15:54:02 -0700211 ['/root/repository/chromite/bin/cbuildbot',
Don Garrett597ddff2017-02-17 18:29:37 -0800212 'config',
Don Garrettbf90cdf2017-05-19 15:54:02 -0700213 '--buildroot', '/root/repository',
Don Garrett125d4dc2017-04-25 16:26:03 -0700214 '--branch', 'branch',
Don Garrett597ddff2017-02-17 18:29:37 -0800215 '--git-cache-dir', '/git-cache'],
Don Garrettbf90cdf2017-05-19 15:54:02 -0700216 cwd='/root/repository',
Don Garrettacbb2392017-05-11 18:27:41 -0700217 error_code_ok=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800218
219
Don Garrettbf90cdf2017-05-19 15:54:02 -0700220class CleanBuildRootTest(cros_test_lib.MockTempDirTestCase):
221 """Tests for CleanBuildRoot method."""
Don Garrett7ade05a2017-02-17 13:31:47 -0800222
223 def setUp(self):
224 """Create standard buildroot contents for cleanup."""
Don Garrettbf90cdf2017-05-19 15:54:02 -0700225 self.root = os.path.join(self.tempdir)
Don Garrette17e1d92017-04-12 15:28:19 -0700226 self.state = os.path.join(self.root, '.cbuildbot_launch_state')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700227 self.buildroot = os.path.join(self.root, 'buildroot')
228 self.repo = os.path.join(self.buildroot, '.repo/repo')
229 self.chroot = os.path.join(self.buildroot, 'chroot/chroot')
230 self.general = os.path.join(self.buildroot, 'general/general')
Don Garrett39963602017-02-27 14:41:58 -0800231 # TODO: Add .cache, and distfiles.
Don Garrett7ade05a2017-02-17 13:31:47 -0800232
Don Garrettf324bc32017-05-23 14:00:53 -0700233 self.mock_repo = mock.MagicMock()
Don Garrettbf90cdf2017-05-19 15:54:02 -0700234 self.mock_repo.directory = self.buildroot
Don Garrettf324bc32017-05-23 14:00:53 -0700235
Don Garrettacbb2392017-05-11 18:27:41 -0700236 self.metrics = {}
237
Don Garrett60967922017-04-12 18:51:44 -0700238 def populateBuildroot(self, state=None):
Don Garrett7ade05a2017-02-17 13:31:47 -0800239 """Create standard buildroot contents for cleanup."""
240 if state:
Don Garrettbf90cdf2017-05-19 15:54:02 -0700241 osutils.SafeMakedirs(self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800242 osutils.WriteFile(self.state, state)
243
244 # Create files.
245 for f in (self.repo, self.chroot, self.general):
Don Garrette17e1d92017-04-12 15:28:19 -0700246 osutils.Touch(f, makedirs=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800247
Don Garrette17e1d92017-04-12 15:28:19 -0700248 def testNoBuildroot(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700249 """Test CleanBuildRoot with no history."""
Don Garrettf324bc32017-05-23 14:00:53 -0700250 self.mock_repo.branch = 'master'
251
Don Garrettbf90cdf2017-05-19 15:54:02 -0700252 cbuildbot_launch.CleanBuildRoot(
253 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800254
Don Garrettbf90cdf2017-05-19 15:54:02 -0700255 self.assertEqual(osutils.ReadFile(self.state), '2 master')
Don Garrett7ade05a2017-02-17 13:31:47 -0800256
257 def testBuildrootNoState(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700258 """Test CleanBuildRoot with no state information."""
Don Garrett7ade05a2017-02-17 13:31:47 -0800259 self.populateBuildroot()
Don Garrettf324bc32017-05-23 14:00:53 -0700260 self.mock_repo.branch = 'master'
Don Garrett7ade05a2017-02-17 13:31:47 -0800261
Don Garrettbf90cdf2017-05-19 15:54:02 -0700262 cbuildbot_launch.CleanBuildRoot(
263 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800264
Don Garrettbf90cdf2017-05-19 15:54:02 -0700265 self.assertEqual(osutils.ReadFile(self.state), '2 master')
Don Garrett60967922017-04-12 18:51:44 -0700266 self.assertNotExists(self.repo)
Don Garrett7ade05a2017-02-17 13:31:47 -0800267 self.assertNotExists(self.chroot)
Don Garrett60967922017-04-12 18:51:44 -0700268 self.assertNotExists(self.general)
269
270 def testBuildrootFormatMismatch(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700271 """Test CleanBuildRoot with no state information."""
Don Garrett125d4dc2017-04-25 16:26:03 -0700272 self.populateBuildroot('0 master')
Don Garrettf324bc32017-05-23 14:00:53 -0700273 self.mock_repo.branch = 'master'
Don Garrett60967922017-04-12 18:51:44 -0700274
Don Garrettbf90cdf2017-05-19 15:54:02 -0700275 cbuildbot_launch.CleanBuildRoot(
276 self.root, self.mock_repo, self.metrics)
Don Garrett60967922017-04-12 18:51:44 -0700277
Don Garrettbf90cdf2017-05-19 15:54:02 -0700278 self.assertEqual(osutils.ReadFile(self.state), '2 master')
Don Garrett60967922017-04-12 18:51:44 -0700279 self.assertNotExists(self.repo)
280 self.assertNotExists(self.chroot)
281 self.assertNotExists(self.general)
Don Garrett7ade05a2017-02-17 13:31:47 -0800282
283 def testBuildrootBranchChange(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700284 """Test CleanBuildRoot with a change in branches."""
285 self.populateBuildroot('2 branchA')
Don Garrettf324bc32017-05-23 14:00:53 -0700286 self.mock_repo.branch = 'branchB'
Don Garrett7ade05a2017-02-17 13:31:47 -0800287
Don Garrettbf90cdf2017-05-19 15:54:02 -0700288 cbuildbot_launch.CleanBuildRoot(
289 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800290
Don Garrettbf90cdf2017-05-19 15:54:02 -0700291 self.assertEqual(osutils.ReadFile(self.state), '2 branchB')
Don Garrett7ade05a2017-02-17 13:31:47 -0800292 self.assertExists(self.repo)
293 self.assertNotExists(self.chroot)
294 self.assertExists(self.general)
295
296 def testBuildrootBranchMatch(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700297 """Test CleanBuildRoot with no change in branch."""
298 self.populateBuildroot('2 branchA')
Don Garrettf324bc32017-05-23 14:00:53 -0700299 self.mock_repo.branch = 'branchA'
Don Garrett7ade05a2017-02-17 13:31:47 -0800300
Don Garrettbf90cdf2017-05-19 15:54:02 -0700301 cbuildbot_launch.CleanBuildRoot(
302 self.root, self.mock_repo, self.metrics)
Don Garrett7ade05a2017-02-17 13:31:47 -0800303
Don Garrettbf90cdf2017-05-19 15:54:02 -0700304 self.assertEqual(osutils.ReadFile(self.state), '2 branchA')
Don Garrett7ade05a2017-02-17 13:31:47 -0800305 self.assertExists(self.repo)
306 self.assertExists(self.chroot)
307 self.assertExists(self.general)
Don Garrette17e1d92017-04-12 15:28:19 -0700308
Don Garrettf324bc32017-05-23 14:00:53 -0700309 def testBuildrootRepoCleanFailure(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700310 """Test CleanBuildRoot with repo checkout failure."""
Don Garrettf324bc32017-05-23 14:00:53 -0700311 self.populateBuildroot('1 branchA')
312 self.mock_repo.branch = 'branchA'
313 self.mock_repo.BuildRootGitCleanup.side_effect = Exception
314
Don Garrettbf90cdf2017-05-19 15:54:02 -0700315 cbuildbot_launch.CleanBuildRoot(
316 self.root, self.mock_repo, self.metrics)
Don Garrettf324bc32017-05-23 14:00:53 -0700317
Don Garrettbf90cdf2017-05-19 15:54:02 -0700318 self.assertEqual(osutils.ReadFile(self.state), '2 branchA')
Don Garrettf324bc32017-05-23 14:00:53 -0700319 self.assertNotExists(self.repo)
320 self.assertNotExists(self.chroot)
321 self.assertNotExists(self.general)
322
Don Garrettbf90cdf2017-05-19 15:54:02 -0700323 def testGetState(self):
324 """Test GetState."""
Don Garrett60967922017-04-12 18:51:44 -0700325 # No root dir.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700326 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700327 self.assertEqual(results, (0, ''))
328
329 # Empty root dir.
330 osutils.SafeMakedirs(self.root)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700331 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700332 self.assertEqual(results, (0, ''))
333
334 # Empty Contents
335 osutils.WriteFile(self.state, '')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700336 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700337 self.assertEqual(results, (0, ''))
338
339 # Old Format Contents
340 osutils.WriteFile(self.state, 'happy-branch')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700341 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700342 self.assertEqual(results, (0, ''))
343
344 # Expected Contents
345 osutils.WriteFile(self.state, '1 happy-branch')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700346 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700347 self.assertEqual(results, (1, 'happy-branch'))
348
349 # Future Contents
350 osutils.WriteFile(self.state, '22 master')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700351 results = cbuildbot_launch.GetState(self.root)
Don Garrett60967922017-04-12 18:51:44 -0700352 self.assertEqual(results, (22, 'master'))
353
354 # Read Write
Don Garrettbf90cdf2017-05-19 15:54:02 -0700355 cbuildbot_launch.SetState('happy-branch', self.root)
356 results = cbuildbot_launch.GetState(self.root)
357 self.assertEqual(results, (2, 'happy-branch'))
Don Garrett60967922017-04-12 18:51:44 -0700358
Don Garrettbf90cdf2017-05-19 15:54:02 -0700359 def testSetState(self):
360 """Test SetState."""
Don Garrett60967922017-04-12 18:51:44 -0700361 # Write out a state file.
362 osutils.SafeMakedirs(self.root)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700363 cbuildbot_launch.SetState('happy-branch', self.root)
364 self.assertEqual(osutils.ReadFile(self.state), '2 happy-branch')
Don Garrett60967922017-04-12 18:51:44 -0700365
366 # Change to a future version.
367 self.PatchObject(cbuildbot_launch, 'BUILDROOT_BUILDROOT_LAYOUT', 22)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700368 cbuildbot_launch.SetState('happy-branch', self.root)
Don Garrett60967922017-04-12 18:51:44 -0700369 self.assertEqual(osutils.ReadFile(self.state), '22 happy-branch')