Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 2 | # 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 Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 6 | """Unit tests for chromite.scripts.cbuildbot_launch.""" |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 7 | |
| 8 | from __future__ import print_function |
| 9 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 10 | import mock |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 11 | import os |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 12 | import time |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 13 | |
| 14 | from chromite.cbuildbot import repository |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 15 | from chromite.lib import build_summary |
Don Garrett | 861e918 | 2017-05-15 15:30:23 -0700 | [diff] [blame] | 16 | from chromite.lib import constants |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 17 | from chromite.lib import cros_build_lib |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 18 | from chromite.lib import cros_build_lib_unittest |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 19 | from chromite.lib import cros_test_lib |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 20 | from chromite.lib import osutils |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 21 | from chromite.scripts import cbuildbot_launch |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 22 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 23 | EXPECTED_MANIFEST_URL = 'https://chrome-internal-review.googlesource.com/chromeos/manifest-internal' # pylint: disable=line-too-long |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 24 | |
| 25 | |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 26 | # It's reasonable for unittests to look at internals. |
| 27 | # pylint: disable=protected-access |
| 28 | |
| 29 | |
Don Garrett | 8d31479 | 2017-05-18 13:11:42 -0700 | [diff] [blame] | 30 | class FakeException(Exception): |
| 31 | """Test exception to raise during tests.""" |
| 32 | |
| 33 | |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 34 | class CbuildbotLaunchTest(cros_test_lib.MockTestCase): |
| 35 | """Tests for cbuildbot_launch script.""" |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 36 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 37 | def testPreParseArguments(self): |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 38 | """Test that we can correctly extract branch values from cbuildbot args.""" |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 39 | CASES = ( |
| 40 | (['--buildroot', '/buildroot', 'daisy-incremental'], |
| 41 | (None, '/buildroot', None)), |
| 42 | |
| 43 | (['--buildbot', '--buildroot', '/buildroot', |
| 44 | '--git-cache-dir', '/git-cache', |
| 45 | '-b', 'release-R57-9202.B', |
| 46 | 'daisy-incremental'], |
| 47 | ('release-R57-9202.B', '/buildroot', '/git-cache')), |
| 48 | |
| 49 | (['--debug', '--buildbot', '--notests', |
| 50 | '--buildroot', '/buildroot', |
| 51 | '--git-cache-dir', '/git-cache', |
| 52 | '--branch', 'release-R57-9202.B', |
| 53 | 'daisy-incremental'], |
| 54 | ('release-R57-9202.B', '/buildroot', '/git-cache')), |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 55 | ) |
| 56 | |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 57 | for cmd_args, expected in CASES: |
| 58 | expected_branch, expected_buildroot, expected_cache_dir = expected |
| 59 | |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 60 | options = cbuildbot_launch.PreParseArguments(cmd_args) |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 61 | |
| 62 | self.assertEqual(options.branch, expected_branch) |
| 63 | self.assertEqual(options.buildroot, expected_buildroot) |
| 64 | self.assertEqual(options.git_cache_dir, expected_cache_dir) |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 65 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 66 | def testInitialCheckout(self): |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 67 | """Test InitialCheckout with minimum settings.""" |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 68 | mock_repo = mock.MagicMock() |
| 69 | mock_repo.branch = 'branch' |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 70 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 71 | cbuildbot_launch.InitialCheckout(mock_repo) |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 72 | |
| 73 | self.assertEqual(mock_repo.mock_calls, [ |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 74 | mock.call.Sync(detach=True), |
Don Garrett | 8d31479 | 2017-05-18 13:11:42 -0700 | [diff] [blame] | 75 | ]) |
| 76 | |
Don Garrett | f15d65b | 2017-04-12 12:39:55 -0700 | [diff] [blame] | 77 | def testConfigureGlobalEnvironment(self): |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 78 | """Ensure that we can setup our global runtime environment correctly.""" |
Don Garrett | 86fec48 | 2017-05-17 18:13:33 -0700 | [diff] [blame] | 79 | |
| 80 | os.environ.pop('LANG', None) |
| 81 | os.environ['LC_MONETARY'] = 'bad' |
| 82 | |
Don Garrett | f15d65b | 2017-04-12 12:39:55 -0700 | [diff] [blame] | 83 | cbuildbot_launch.ConfigureGlobalEnvironment() |
| 84 | |
Don Garrett | 86fec48 | 2017-05-17 18:13:33 -0700 | [diff] [blame] | 85 | # Verify umask is updated. |
Don Garrett | f15d65b | 2017-04-12 12:39:55 -0700 | [diff] [blame] | 86 | self.assertEqual(os.umask(0), 0o22) |
| 87 | |
Don Garrett | 86fec48 | 2017-05-17 18:13:33 -0700 | [diff] [blame] | 88 | # Verify ENVs are cleaned up. |
| 89 | self.assertEqual(os.environ['LANG'], 'en_US.UTF-8') |
| 90 | self.assertNotIn('LC_MONETARY', os.environ) |
| 91 | |
Don Garrett | f15d65b | 2017-04-12 12:39:55 -0700 | [diff] [blame] | 92 | |
Don Garrett | 066e6f5 | 2017-09-28 19:14:01 -0700 | [diff] [blame] | 93 | class RunDepotToolsEnsureBootstrap(cros_build_lib_unittest.RunCommandTestCase, |
| 94 | cros_test_lib.TempDirTestCase): |
| 95 | """Test the helper function DepotToolsEnsureBootstrap.""" |
| 96 | |
| 97 | def testEnsureBootstrap(self): |
| 98 | """Verify that the script is run if present.""" |
| 99 | script = os.path.join(self.tempdir, 'ensure_bootstrap') |
| 100 | osutils.Touch(script, makedirs=True) |
| 101 | |
| 102 | cbuildbot_launch.DepotToolsEnsureBootstrap(self.tempdir) |
| 103 | self.assertCommandCalled( |
| 104 | [script], extra_env={'PATH': mock.ANY}, cwd=self.tempdir) |
| 105 | |
| 106 | def testEnsureBootstrapMissing(self): |
| 107 | """Verify that the script is NOT run if not present.""" |
| 108 | cbuildbot_launch.DepotToolsEnsureBootstrap(self.tempdir) |
| 109 | self.assertEqual(self.rc.call_count, 0) |
| 110 | |
| 111 | |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 112 | class RunTests(cros_build_lib_unittest.RunCommandTestCase): |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 113 | """Tests for cbuildbot_launch script.""" |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 114 | |
| 115 | ARGS_BASE = ['--buildroot', '/buildroot'] |
Don Garrett | 5cd946b | 2017-07-20 13:42:20 -0700 | [diff] [blame] | 116 | EXPECTED_ARGS_BASE = ['--buildroot', '/cbuildbot_buildroot'] |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 117 | ARGS_GIT_CACHE = ['--git-cache-dir', '/git-cache'] |
| 118 | ARGS_CONFIG = ['config'] |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 119 | CMD = ['/cbuildbot_buildroot/chromite/bin/cbuildbot'] |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 120 | |
Don Garrett | 6e5c6b9 | 2018-04-06 17:58:49 -0700 | [diff] [blame] | 121 | def verifyCbuildbot(self, args, expected_cmd, version): |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 122 | """Ensure we invoke cbuildbot correctly.""" |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 123 | self.PatchObject( |
| 124 | cros_build_lib, 'GetTargetChromiteApiVersion', autospec=True, |
| 125 | return_value=version) |
| 126 | |
Don Garrett | 6e5c6b9 | 2018-04-06 17:58:49 -0700 | [diff] [blame] | 127 | cbuildbot_launch.Cbuildbot('/cbuildbot_buildroot', '/depot_tools', args) |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 128 | |
| 129 | self.assertCommandCalled( |
Don Garrett | a50bf49 | 2017-09-28 18:33:02 -0700 | [diff] [blame] | 130 | expected_cmd, extra_env={'PATH': mock.ANY}, |
| 131 | cwd='/cbuildbot_buildroot', error_code_ok=True) |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 132 | |
Don Garrett | 6e5c6b9 | 2018-04-06 17:58:49 -0700 | [diff] [blame] | 133 | def testCbuildbotSimple(self): |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 134 | """Ensure we invoke cbuildbot correctly.""" |
Don Garrett | 6e5c6b9 | 2018-04-06 17:58:49 -0700 | [diff] [blame] | 135 | self.verifyCbuildbot( |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 136 | self.ARGS_BASE + self.ARGS_CONFIG, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 137 | self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE, |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 138 | (0, 4)) |
| 139 | |
Don Garrett | 6e5c6b9 | 2018-04-06 17:58:49 -0700 | [diff] [blame] | 140 | def testCbuildbotNotFiltered(self): |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 141 | """Ensure we invoke cbuildbot correctly.""" |
Don Garrett | 6e5c6b9 | 2018-04-06 17:58:49 -0700 | [diff] [blame] | 142 | self.verifyCbuildbot( |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 143 | self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 144 | (self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE + |
| 145 | self.ARGS_GIT_CACHE), |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 146 | (0, 4)) |
| 147 | |
Don Garrett | 6e5c6b9 | 2018-04-06 17:58:49 -0700 | [diff] [blame] | 148 | def testCbuildbotFiltered(self): |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 149 | """Ensure we invoke cbuildbot correctly.""" |
Don Garrett | 6e5c6b9 | 2018-04-06 17:58:49 -0700 | [diff] [blame] | 150 | self.verifyCbuildbot( |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 151 | self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 152 | self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE, |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 153 | (0, 2)) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 154 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 155 | def testMainMin(self): |
| 156 | """Test a minimal set of command line options.""" |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 157 | self.PatchObject(osutils, 'SafeMakedirs', autospec=True) |
| 158 | self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion', |
Don Garrett | 861e918 | 2017-05-15 15:30:23 -0700 | [diff] [blame] | 159 | autospec=True, return_value=(constants.REEXEC_API_MAJOR, |
| 160 | constants.REEXEC_API_MINOR)) |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 161 | mock_repo = mock.MagicMock() |
| 162 | mock_repo.branch = 'master' |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 163 | mock_repo.directory = '/root/repository' |
| 164 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 165 | mock_repo_create = self.PatchObject(repository, 'RepoRepository', |
| 166 | autospec=True, return_value=mock_repo) |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 167 | mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot', |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 168 | autospec=True) |
| 169 | mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout', |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 170 | autospec=True) |
Benjamin Gordon | aee36b8 | 2018-02-05 14:25:26 -0700 | [diff] [blame] | 171 | mock_cleanup_chroot = self.PatchObject(cbuildbot_launch, 'CleanupChroot', |
| 172 | autospec=True) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 173 | mock_set_last_build_state = self.PatchObject( |
| 174 | cbuildbot_launch, 'SetLastBuildState', autospec=True) |
| 175 | |
| 176 | expected_build_state = build_summary.BuildSummary( |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 177 | build_number=0, master_build_id=0, status=mock.ANY, |
| 178 | buildroot_layout=2, branch='master') |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 179 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 180 | cbuildbot_launch._main(['-r', '/root', 'config']) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 181 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 182 | # Did we create the repo instance correctly? |
| 183 | self.assertEqual(mock_repo_create.mock_calls, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 184 | [mock.call(EXPECTED_MANIFEST_URL, '/root/repository', |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 185 | git_cache_dir=None, branch='master')]) |
| 186 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 187 | # Ensure we clean, as expected. |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 188 | self.assertEqual(mock_clean.mock_calls, [ |
Don Garrett | d1d90dd | 2017-06-13 17:35:52 -0700 | [diff] [blame] | 189 | mock.call('/root', mock_repo, |
| 190 | { |
| 191 | 'branch_name': 'master', |
| 192 | 'tryjob': False, |
| 193 | 'build_config': 'config', |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 194 | }, |
| 195 | expected_build_state)]) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 196 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 197 | # Ensure we checkout, as expected. |
| 198 | self.assertEqual(mock_checkout.mock_calls, |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 199 | [mock.call(mock_repo)]) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 200 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 201 | # Ensure we invoke cbuildbot, as expected. |
| 202 | self.assertCommandCalled( |
Don Garrett | 5cd946b | 2017-07-20 13:42:20 -0700 | [diff] [blame] | 203 | [ |
| 204 | '/root/repository/chromite/bin/cbuildbot', |
| 205 | 'config', |
| 206 | '-r', '/root/repository', |
| 207 | '--ts-mon-task-num', '1', |
| 208 | ], |
Don Garrett | a50bf49 | 2017-09-28 18:33:02 -0700 | [diff] [blame] | 209 | extra_env={'PATH': mock.ANY}, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 210 | cwd='/root/repository', |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 211 | error_code_ok=True) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 212 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 213 | # Ensure we saved the final state, as expected. |
| 214 | self.assertEqual(expected_build_state.status, |
| 215 | constants.BUILDER_STATUS_PASSED) |
| 216 | self.assertEqual(mock_set_last_build_state.mock_calls, [ |
| 217 | mock.call('/root', expected_build_state)]) |
| 218 | |
Benjamin Gordon | aee36b8 | 2018-02-05 14:25:26 -0700 | [diff] [blame] | 219 | # Ensure we clean the chroot, as expected. |
| 220 | self.assertEqual(mock_cleanup_chroot.mock_calls, [ |
| 221 | mock.call('/root/repository')]) |
| 222 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 223 | def testMainMax(self): |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 224 | """Test a larger set of command line options.""" |
| 225 | self.PatchObject(osutils, 'SafeMakedirs', autospec=True) |
| 226 | self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion', |
Don Garrett | 861e918 | 2017-05-15 15:30:23 -0700 | [diff] [blame] | 227 | autospec=True, return_value=(constants.REEXEC_API_MAJOR, |
| 228 | constants.REEXEC_API_MINOR)) |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 229 | mock_repo = mock.MagicMock() |
| 230 | mock_repo.branch = 'branch' |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 231 | mock_repo.directory = '/root/repository' |
| 232 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 233 | mock_summary = build_summary.BuildSummary( |
| 234 | build_number=313, |
| 235 | master_build_id=123123123, |
| 236 | status=constants.BUILDER_STATUS_FAILED, |
| 237 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 238 | branch='branch') |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 239 | |
| 240 | mock_get_last_build_state = self.PatchObject( |
| 241 | cbuildbot_launch, 'GetLastBuildState', autospec=True, |
| 242 | return_value=mock_summary) |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 243 | mock_repo_create = self.PatchObject(repository, 'RepoRepository', |
| 244 | autospec=True, return_value=mock_repo) |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 245 | mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot', |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 246 | autospec=True) |
| 247 | mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout', |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 248 | autospec=True) |
Benjamin Gordon | aee36b8 | 2018-02-05 14:25:26 -0700 | [diff] [blame] | 249 | mock_cleanup_chroot = self.PatchObject(cbuildbot_launch, 'CleanupChroot', |
| 250 | autospec=True) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 251 | mock_set_last_build_state = self.PatchObject( |
| 252 | cbuildbot_launch, 'SetLastBuildState', autospec=True) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 253 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 254 | cbuildbot_launch._main(['--buildroot', '/root', |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 255 | '--branch', 'branch', |
| 256 | '--git-cache-dir', '/git-cache', |
Don Garrett | d1d90dd | 2017-06-13 17:35:52 -0700 | [diff] [blame] | 257 | '--remote-trybot', |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 258 | '--master-build-id', '123456789', |
| 259 | '--buildnumber', '314', |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 260 | 'config']) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 261 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 262 | # Did we create the repo instance correctly? |
| 263 | self.assertEqual(mock_repo_create.mock_calls, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 264 | [mock.call(EXPECTED_MANIFEST_URL, '/root/repository', |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 265 | git_cache_dir='/git-cache', branch='branch')]) |
| 266 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 267 | # Ensure we look up the previous status. |
| 268 | self.assertEqual(mock_get_last_build_state.mock_calls, [ |
| 269 | mock.call('/root')]) |
| 270 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 271 | # Ensure we clean, as expected. |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 272 | self.assertEqual(mock_clean.mock_calls, [ |
Don Garrett | d1d90dd | 2017-06-13 17:35:52 -0700 | [diff] [blame] | 273 | mock.call('/root', |
| 274 | mock_repo, |
| 275 | { |
| 276 | 'branch_name': 'branch', |
| 277 | 'tryjob': True, |
| 278 | 'build_config': 'config', |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 279 | }, |
| 280 | build_summary.BuildSummary( |
| 281 | build_number=314, |
| 282 | master_build_id=123456789, |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 283 | status=mock.ANY, |
| 284 | branch='branch', |
| 285 | buildroot_layout=2 |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 286 | ))]) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 287 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 288 | # Ensure we checkout, as expected. |
| 289 | self.assertEqual(mock_checkout.mock_calls, |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 290 | [mock.call(mock_repo)]) |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 291 | |
| 292 | # Ensure we invoke cbuildbot, as expected. |
| 293 | self.assertCommandCalled( |
Don Garrett | 5cd946b | 2017-07-20 13:42:20 -0700 | [diff] [blame] | 294 | [ |
| 295 | '/root/repository/chromite/bin/cbuildbot', |
| 296 | 'config', |
| 297 | '--buildroot', '/root/repository', |
| 298 | '--branch', 'branch', |
| 299 | '--git-cache-dir', '/git-cache', |
| 300 | '--remote-trybot', |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 301 | '--master-build-id', '123456789', |
| 302 | '--buildnumber', '314', |
| 303 | '--previous-build-state', |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 304 | 'eyJzdGF0dXMiOiAiZmFpbCIsICJtYXN0ZXJfYnVpbGRfaWQiOiAxMjMxMjMxMj' |
| 305 | 'MsICJidWlsZF9udW1iZXIiOiAzMTMsICJidWlsZHJvb3RfbGF5b3V0IjogMiwg' |
| 306 | 'ImJyYW5jaCI6ICJicmFuY2gifQ==', |
Don Garrett | 5cd946b | 2017-07-20 13:42:20 -0700 | [diff] [blame] | 307 | '--ts-mon-task-num', '1', |
| 308 | ], |
Don Garrett | a50bf49 | 2017-09-28 18:33:02 -0700 | [diff] [blame] | 309 | extra_env={'PATH': mock.ANY}, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 310 | cwd='/root/repository', |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 311 | error_code_ok=True) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 312 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 313 | # Ensure we write the final build state, as expected. |
| 314 | final_state = build_summary.BuildSummary( |
| 315 | build_number=314, |
| 316 | master_build_id=123456789, |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 317 | status=constants.BUILDER_STATUS_PASSED, |
| 318 | buildroot_layout=2, |
| 319 | branch='branch') |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 320 | self.assertEqual(mock_set_last_build_state.mock_calls, [ |
| 321 | mock.call('/root', final_state)]) |
| 322 | |
Benjamin Gordon | aee36b8 | 2018-02-05 14:25:26 -0700 | [diff] [blame] | 323 | # Ensure we clean the chroot, as expected. |
| 324 | self.assertEqual(mock_cleanup_chroot.mock_calls, [ |
| 325 | mock.call('/root/repository')]) |
| 326 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 327 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 328 | class CleanBuildRootTest(cros_test_lib.MockTempDirTestCase): |
| 329 | """Tests for CleanBuildRoot method.""" |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 330 | |
| 331 | def setUp(self): |
| 332 | """Create standard buildroot contents for cleanup.""" |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 333 | self.root = os.path.join(self.tempdir) |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 334 | self.state = os.path.join(self.root, '.cbuildbot_launch_state') |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 335 | self.previous_build_state = os.path.join( |
| 336 | self.root, '.cbuildbot_build_state.json') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 337 | self.buildroot = os.path.join(self.root, 'buildroot') |
| 338 | self.repo = os.path.join(self.buildroot, '.repo/repo') |
| 339 | self.chroot = os.path.join(self.buildroot, 'chroot/chroot') |
| 340 | self.general = os.path.join(self.buildroot, 'general/general') |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 341 | self.cache = os.path.join(self.buildroot, '.cache') |
| 342 | self.distfiles = os.path.join(self.cache, 'distfiles') |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 343 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 344 | self.mock_repo = mock.MagicMock() |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 345 | self.mock_repo.directory = self.buildroot |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 346 | |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 347 | self.metrics = {} |
| 348 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 349 | def populateBuildroot(self, state=None, previous_build_state=None): |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 350 | """Create standard buildroot contents for cleanup.""" |
| 351 | if state: |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 352 | osutils.SafeMakedirs(self.root) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 353 | osutils.WriteFile(self.state, state) |
| 354 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 355 | if previous_build_state: |
| 356 | osutils.SafeMakedirs(self.root) |
| 357 | osutils.WriteFile(self.previous_build_state, previous_build_state) |
| 358 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 359 | # Create files. |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 360 | for f in (self.repo, self.chroot, self.general, self.distfiles): |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 361 | osutils.Touch(f, makedirs=True) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 362 | |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 363 | def testNoBuildroot(self): |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 364 | """Test CleanBuildRoot with no history.""" |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 365 | self.mock_repo.branch = 'master' |
| 366 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 367 | build_state = build_summary.BuildSummary( |
| 368 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 369 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 370 | branch='master') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 371 | cbuildbot_launch.CleanBuildRoot( |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 372 | self.root, self.mock_repo, self.metrics, build_state) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 373 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 374 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 375 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 376 | self.assertEqual(new_summary.branch, 'master') |
| 377 | self.assertIsNotNone(new_summary.distfiles_ts) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 378 | self.assertEqual(new_summary, build_state) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 379 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 380 | self.assertNotExists(self.state) |
| 381 | self.assertExists(self.previous_build_state) |
| 382 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 383 | def testBuildrootNoState(self): |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 384 | """Test CleanBuildRoot with no state information.""" |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 385 | self.populateBuildroot() |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 386 | self.mock_repo.branch = 'master' |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 387 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 388 | build_state = build_summary.BuildSummary( |
| 389 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 390 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 391 | branch='master') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 392 | cbuildbot_launch.CleanBuildRoot( |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 393 | self.root, self.mock_repo, self.metrics, build_state) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 394 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 395 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 396 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 397 | self.assertEqual(new_summary.branch, 'master') |
| 398 | self.assertIsNotNone(new_summary.distfiles_ts) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 399 | self.assertEqual(new_summary, build_state) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 400 | |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 401 | self.assertNotExists(self.repo) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 402 | self.assertNotExists(self.chroot) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 403 | self.assertNotExists(self.general) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 404 | self.assertNotExists(self.distfiles) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 405 | self.assertNotExists(self.state) |
| 406 | self.assertExists(self.previous_build_state) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 407 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 408 | def testBuildrootFormatMismatchMigration(self): |
| 409 | """Test CleanBuildRoot with format mismatch migrated from old data.""" |
Don Garrett | 125d4dc | 2017-04-25 16:26:03 -0700 | [diff] [blame] | 410 | self.populateBuildroot('0 master') |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 411 | self.mock_repo.branch = 'master' |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 412 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 413 | build_state = build_summary.BuildSummary( |
| 414 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 415 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 416 | branch='master') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 417 | cbuildbot_launch.CleanBuildRoot( |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 418 | self.root, self.mock_repo, self.metrics, build_state) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 419 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 420 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 421 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 422 | self.assertEqual(new_summary.branch, 'master') |
| 423 | self.assertIsNotNone(new_summary.distfiles_ts) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 424 | self.assertEqual(new_summary, build_state) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 425 | |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 426 | self.assertNotExists(self.repo) |
| 427 | self.assertNotExists(self.chroot) |
| 428 | self.assertNotExists(self.general) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 429 | self.assertNotExists(self.distfiles) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 430 | self.assertNotExists(self.state) |
| 431 | self.assertExists(self.previous_build_state) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 432 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 433 | def testBuildrootFormatMismatch(self): |
| 434 | """Test CleanBuildRoot with buildroot layout mismatch.""" |
| 435 | old_build_state = build_summary.BuildSummary( |
| 436 | status=constants.BUILDER_STATUS_PASSED, |
| 437 | buildroot_layout=1, |
| 438 | branch='master') |
| 439 | self.populateBuildroot(previous_build_state=old_build_state.to_json()) |
| 440 | self.mock_repo.branch = 'master' |
| 441 | |
| 442 | build_state = build_summary.BuildSummary( |
| 443 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 444 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 445 | branch='master') |
| 446 | cbuildbot_launch.CleanBuildRoot( |
| 447 | self.root, self.mock_repo, self.metrics, build_state) |
| 448 | |
| 449 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
| 450 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 451 | self.assertEqual(new_summary.branch, 'master') |
| 452 | self.assertIsNotNone(new_summary.distfiles_ts) |
| 453 | self.assertEqual(new_summary, build_state) |
| 454 | |
| 455 | self.assertNotExists(self.repo) |
| 456 | self.assertNotExists(self.chroot) |
| 457 | self.assertNotExists(self.general) |
| 458 | self.assertNotExists(self.distfiles) |
| 459 | self.assertNotExists(self.state) |
| 460 | self.assertExists(self.previous_build_state) |
| 461 | |
| 462 | def testBuildrootBranchChangeMigration(self): |
| 463 | """Test CleanBuildRoot with a change in branches migrated from old state.""" |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 464 | self.populateBuildroot('2 branchA') |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 465 | self.mock_repo.branch = 'branchB' |
Benjamin Gordon | 59ba2f8 | 2017-08-28 15:31:06 -0600 | [diff] [blame] | 466 | m = self.PatchObject(cros_build_lib, 'CleanupChrootMount') |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 467 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 468 | build_state = build_summary.BuildSummary( |
| 469 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 470 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 471 | branch='branchB') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 472 | cbuildbot_launch.CleanBuildRoot( |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 473 | self.root, self.mock_repo, self.metrics, build_state) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 474 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 475 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 476 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 477 | self.assertEqual(new_summary.branch, 'branchB') |
| 478 | self.assertIsNotNone(new_summary.distfiles_ts) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 479 | self.assertEqual(new_summary, build_state) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 480 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 481 | self.assertExists(self.repo) |
| 482 | self.assertNotExists(self.chroot) |
| 483 | self.assertExists(self.general) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 484 | self.assertNotExists(self.distfiles) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 485 | self.assertNotExists(self.state) |
| 486 | self.assertExists(self.previous_build_state) |
Benjamin Gordon | 59ba2f8 | 2017-08-28 15:31:06 -0600 | [diff] [blame] | 487 | m.assert_called() |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 488 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 489 | def testBuildrootBranchChange(self): |
| 490 | """Test CleanBuildRoot with a change in branches.""" |
| 491 | old_build_state = build_summary.BuildSummary( |
| 492 | status=constants.BUILDER_STATUS_PASSED, |
| 493 | buildroot_layout=2, |
| 494 | branch='branchA') |
| 495 | self.populateBuildroot(previous_build_state=old_build_state.to_json()) |
| 496 | self.mock_repo.branch = 'branchB' |
| 497 | m = self.PatchObject(cros_build_lib, 'CleanupChrootMount') |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 498 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 499 | build_state = build_summary.BuildSummary( |
| 500 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 501 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 502 | branch='branchB') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 503 | cbuildbot_launch.CleanBuildRoot( |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 504 | self.root, self.mock_repo, self.metrics, build_state) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 505 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 506 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 507 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 508 | self.assertEqual(new_summary.branch, 'branchB') |
| 509 | self.assertIsNotNone(new_summary.distfiles_ts) |
| 510 | self.assertEqual(new_summary, build_state) |
| 511 | |
| 512 | self.assertExists(self.repo) |
| 513 | self.assertNotExists(self.chroot) |
| 514 | self.assertExists(self.general) |
| 515 | self.assertNotExists(self.distfiles) |
| 516 | self.assertNotExists(self.state) |
| 517 | self.assertExists(self.previous_build_state) |
| 518 | m.assert_called() |
| 519 | |
| 520 | def testBuildrootBranchMatchMigration(self): |
| 521 | """Test CleanBuildRoot with no change in branch migrated from old state.""" |
| 522 | self.populateBuildroot('2 branchA') |
| 523 | self.mock_repo.branch = 'branchA' |
| 524 | |
| 525 | build_state = build_summary.BuildSummary( |
| 526 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 527 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 528 | branch='branchA') |
| 529 | cbuildbot_launch.CleanBuildRoot( |
| 530 | self.root, self.mock_repo, self.metrics, build_state) |
| 531 | |
| 532 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
| 533 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 534 | self.assertEqual(new_summary.branch, 'branchA') |
| 535 | self.assertIsNotNone(new_summary.distfiles_ts) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 536 | self.assertEqual(new_summary, build_state) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 537 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 538 | self.assertExists(self.repo) |
| 539 | self.assertExists(self.chroot) |
| 540 | self.assertExists(self.general) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 541 | self.assertExists(self.distfiles) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 542 | self.assertNotExists(self.state) |
| 543 | self.assertExists(self.previous_build_state) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 544 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 545 | def testBuildrootBranchMatch(self): |
| 546 | """Test CleanBuildRoot with no change in branch.""" |
| 547 | old_build_state = build_summary.BuildSummary( |
| 548 | status=constants.BUILDER_STATUS_PASSED, |
| 549 | buildroot_layout=2, |
| 550 | branch='branchA') |
| 551 | self.populateBuildroot(previous_build_state=old_build_state.to_json()) |
| 552 | self.mock_repo.branch = 'branchA' |
| 553 | |
| 554 | build_state = build_summary.BuildSummary( |
| 555 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 556 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 557 | branch='branchA') |
| 558 | cbuildbot_launch.CleanBuildRoot( |
| 559 | self.root, self.mock_repo, self.metrics, build_state) |
| 560 | |
| 561 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
| 562 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 563 | self.assertEqual(new_summary.branch, 'branchA') |
| 564 | self.assertIsNotNone(new_summary.distfiles_ts) |
| 565 | self.assertEqual(new_summary, build_state) |
| 566 | |
| 567 | self.assertExists(self.repo) |
| 568 | self.assertExists(self.chroot) |
| 569 | self.assertExists(self.general) |
| 570 | self.assertExists(self.distfiles) |
| 571 | self.assertNotExists(self.state) |
| 572 | self.assertExists(self.previous_build_state) |
| 573 | |
| 574 | def testBuildrootDistfilesRecentCacheMigration(self): |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 575 | """Test CleanBuildRoot does not delete distfiles when cache is recent.""" |
| 576 | seed_distfiles_ts = time.time() - 60 |
| 577 | self.populateBuildroot('2 branchA %f' % seed_distfiles_ts) |
| 578 | self.mock_repo.branch = 'branchA' |
| 579 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 580 | build_state = build_summary.BuildSummary( |
| 581 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 582 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 583 | branch='branchA') |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 584 | cbuildbot_launch.CleanBuildRoot( |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 585 | self.root, self.mock_repo, self.metrics, build_state) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 586 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 587 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 588 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 589 | self.assertEqual(new_summary.branch, 'branchA') |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 590 | # Same cache creation timestamp is rewritten to state. |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 591 | self.assertEqual(new_summary.distfiles_ts, seed_distfiles_ts) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 592 | self.assertEqual(new_summary, build_state) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 593 | |
| 594 | self.assertExists(self.repo) |
| 595 | self.assertExists(self.chroot) |
| 596 | self.assertExists(self.general) |
| 597 | self.assertExists(self.distfiles) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 598 | self.assertNotExists(self.state) |
| 599 | self.assertExists(self.previous_build_state) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 600 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 601 | def testBuildrootDistfilesRecentCache(self): |
| 602 | """Test CleanBuildRoot does not delete distfiles when cache is recent.""" |
| 603 | seed_distfiles_ts = time.time() - 60 |
| 604 | old_build_state = build_summary.BuildSummary( |
| 605 | status=constants.BUILDER_STATUS_PASSED, |
| 606 | buildroot_layout=2, |
| 607 | branch='branchA', |
| 608 | distfiles_ts=seed_distfiles_ts) |
| 609 | self.populateBuildroot(previous_build_state=old_build_state.to_json()) |
| 610 | self.mock_repo.branch = 'branchA' |
| 611 | |
| 612 | build_state = build_summary.BuildSummary( |
| 613 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 614 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 615 | branch='branchA') |
| 616 | cbuildbot_launch.CleanBuildRoot( |
| 617 | self.root, self.mock_repo, self.metrics, build_state) |
| 618 | |
| 619 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
| 620 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 621 | self.assertEqual(new_summary.branch, 'branchA') |
| 622 | # Same cache creation timestamp is rewritten to state. |
| 623 | self.assertEqual(new_summary.distfiles_ts, seed_distfiles_ts) |
| 624 | self.assertEqual(new_summary, build_state) |
| 625 | |
| 626 | self.assertExists(self.repo) |
| 627 | self.assertExists(self.chroot) |
| 628 | self.assertExists(self.general) |
| 629 | self.assertExists(self.distfiles) |
| 630 | self.assertNotExists(self.state) |
| 631 | self.assertExists(self.previous_build_state) |
| 632 | |
| 633 | def testBuildrootDistfilesCacheExpiredMigration(self): |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 634 | """Test CleanBuildRoot when the distfiles cache is too old.""" |
| 635 | self.populateBuildroot('2 branchA 100.000000') |
| 636 | self.mock_repo.branch = 'branchA' |
| 637 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 638 | build_state = build_summary.BuildSummary( |
| 639 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 640 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 641 | branch='branchA') |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 642 | cbuildbot_launch.CleanBuildRoot( |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 643 | self.root, self.mock_repo, self.metrics, build_state) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 644 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 645 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 646 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 647 | self.assertEqual(new_summary.branch, 'branchA') |
| 648 | self.assertIsNotNone(new_summary.distfiles_ts) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 649 | self.assertEqual(new_summary, build_state) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 650 | |
| 651 | self.assertExists(self.repo) |
| 652 | self.assertExists(self.chroot) |
| 653 | self.assertExists(self.general) |
| 654 | self.assertNotExists(self.distfiles) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 655 | self.assertNotExists(self.state) |
| 656 | self.assertExists(self.previous_build_state) |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 657 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 658 | def testBuildrootDistfilesCacheExpired(self): |
| 659 | """Test CleanBuildRoot when the distfiles cache is too old.""" |
| 660 | old_build_state = build_summary.BuildSummary( |
| 661 | status=constants.BUILDER_STATUS_PASSED, |
| 662 | buildroot_layout=2, |
| 663 | branch='branchA', |
| 664 | distfiles_ts=100.0) |
| 665 | self.populateBuildroot(previous_build_state=old_build_state.to_json()) |
| 666 | self.mock_repo.branch = 'branchA' |
| 667 | |
| 668 | build_state = build_summary.BuildSummary( |
| 669 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 670 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 671 | branch='branchA') |
| 672 | cbuildbot_launch.CleanBuildRoot( |
| 673 | self.root, self.mock_repo, self.metrics, build_state) |
| 674 | |
| 675 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
| 676 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 677 | self.assertEqual(new_summary.branch, 'branchA') |
| 678 | self.assertIsNotNone(new_summary.distfiles_ts) |
| 679 | self.assertEqual(new_summary, build_state) |
| 680 | |
| 681 | self.assertExists(self.repo) |
| 682 | self.assertExists(self.chroot) |
| 683 | self.assertExists(self.general) |
| 684 | self.assertNotExists(self.distfiles) |
| 685 | self.assertNotExists(self.state) |
| 686 | self.assertExists(self.previous_build_state) |
| 687 | |
| 688 | def testBuildrootRepoCleanFailureMigration(self): |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 689 | """Test CleanBuildRoot with repo checkout failure.""" |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 690 | self.populateBuildroot('1 branchA') |
| 691 | self.mock_repo.branch = 'branchA' |
| 692 | self.mock_repo.BuildRootGitCleanup.side_effect = Exception |
| 693 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 694 | build_state = build_summary.BuildSummary( |
| 695 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 696 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 697 | branch='branchA') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 698 | cbuildbot_launch.CleanBuildRoot( |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 699 | self.root, self.mock_repo, self.metrics, build_state) |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 700 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 701 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 702 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 703 | self.assertEqual(new_summary.branch, 'branchA') |
| 704 | self.assertIsNotNone(new_summary.distfiles_ts) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 705 | self.assertEqual(new_summary, build_state) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 706 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 707 | self.assertNotExists(self.repo) |
| 708 | self.assertNotExists(self.chroot) |
| 709 | self.assertNotExists(self.general) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 710 | self.assertNotExists(self.distfiles) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 711 | self.assertNotExists(self.state) |
| 712 | self.assertExists(self.previous_build_state) |
| 713 | |
| 714 | def testBuildrootRepoCleanFailure(self): |
| 715 | """Test CleanBuildRoot with repo checkout failure.""" |
| 716 | old_build_state = build_summary.BuildSummary( |
| 717 | status=constants.BUILDER_STATUS_PASSED, |
| 718 | buildroot_layout=1, |
| 719 | branch='branchA') |
| 720 | self.populateBuildroot(previous_build_state=old_build_state.to_json()) |
| 721 | self.mock_repo.branch = 'branchA' |
| 722 | self.mock_repo.BuildRootGitCleanup.side_effect = Exception |
| 723 | |
| 724 | build_state = build_summary.BuildSummary( |
| 725 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 726 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 727 | branch='branchA') |
| 728 | cbuildbot_launch.CleanBuildRoot( |
| 729 | self.root, self.mock_repo, self.metrics, build_state) |
| 730 | |
| 731 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
| 732 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 733 | self.assertEqual(new_summary.branch, 'branchA') |
| 734 | self.assertIsNotNone(new_summary.distfiles_ts) |
| 735 | self.assertEqual(new_summary, build_state) |
| 736 | |
| 737 | self.assertNotExists(self.repo) |
| 738 | self.assertNotExists(self.chroot) |
| 739 | self.assertNotExists(self.general) |
| 740 | self.assertNotExists(self.distfiles) |
| 741 | self.assertNotExists(self.state) |
| 742 | self.assertExists(self.previous_build_state) |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 743 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 744 | def testGetState(self): |
| 745 | """Test GetState.""" |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 746 | # No root dir. |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 747 | results = cbuildbot_launch.GetState(self.root) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 748 | self.assertEqual(results, (0, '', None)) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 749 | |
| 750 | # Empty root dir. |
| 751 | osutils.SafeMakedirs(self.root) |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 752 | results = cbuildbot_launch.GetState(self.root) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 753 | self.assertEqual(results, (0, '', None)) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 754 | |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 755 | # Empty contents |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 756 | osutils.WriteFile(self.state, '') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 757 | results = cbuildbot_launch.GetState(self.root) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 758 | self.assertEqual(results, (0, '', None)) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 759 | |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 760 | # Old format contents |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 761 | osutils.WriteFile(self.state, 'happy-branch') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 762 | results = cbuildbot_launch.GetState(self.root) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 763 | self.assertEqual(results, (0, '', None)) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 764 | |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 765 | # Expected contents, without distfiles timestamp |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 766 | osutils.WriteFile(self.state, '1 happy-branch') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 767 | results = cbuildbot_launch.GetState(self.root) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 768 | self.assertEqual(results, (1, 'happy-branch', None)) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 769 | |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 770 | # Expected contents |
| 771 | osutils.WriteFile(self.state, '1 happy-branch 1000.33') |
| 772 | version, branch, distfiles_ts = cbuildbot_launch.GetState(self.root) |
| 773 | self.assertEqual(version, 1) |
| 774 | self.assertEqual(branch, 'happy-branch') |
| 775 | self.assertEqual(distfiles_ts, 1000.33) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 776 | |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 777 | # Future layout version contents |
| 778 | osutils.WriteFile(self.state, '22 happy-branch 222') |
| 779 | version, branch, distfiles_ts = cbuildbot_launch.GetState(self.root) |
| 780 | self.assertEqual(version, 22) |
| 781 | self.assertEqual(branch, 'happy-branch') |
| 782 | self.assertEqual(distfiles_ts, 222) |
| 783 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 784 | def testGetCurrentBuildStateNoArgs(self): |
| 785 | """Tests GetCurrentBuildState without arguments.""" |
| 786 | options = cbuildbot_launch.PreParseArguments([ |
| 787 | '--buildroot', self.root, 'config' |
| 788 | ]) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 789 | state = cbuildbot_launch.GetCurrentBuildState(options, 'master') |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 790 | |
| 791 | expected_state = build_summary.BuildSummary( |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 792 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 793 | buildroot_layout=2, |
| 794 | branch='master') |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 795 | self.assertEqual(state, expected_state) |
| 796 | |
| 797 | def testGetCurrentBuildStateHasArgs(self): |
| 798 | """Tests GetCurrentBuildState with arguments.""" |
| 799 | options = cbuildbot_launch.PreParseArguments([ |
| 800 | '--buildroot', self.root, |
| 801 | '--buildnumber', '20', |
| 802 | '--master-build-id', '50', |
| 803 | 'config' |
| 804 | ]) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 805 | state = cbuildbot_launch.GetCurrentBuildState(options, 'branchA') |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 806 | |
| 807 | expected_state = build_summary.BuildSummary( |
| 808 | build_number=20, |
| 809 | master_build_id=50, |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 810 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 811 | buildroot_layout=2, |
| 812 | branch='branchA') |
| 813 | self.assertEqual(state, expected_state) |
| 814 | |
| 815 | def testGetCurrentBuildStateLayout(self): |
| 816 | """Test that GetCurrentBuildState uses the current buildroot layout.""" |
| 817 | # Change to a future version. |
| 818 | self.PatchObject(cbuildbot_launch, 'BUILDROOT_BUILDROOT_LAYOUT', 22) |
| 819 | |
| 820 | options = cbuildbot_launch.PreParseArguments([ |
| 821 | '--buildroot', self.root, 'config' |
| 822 | ]) |
| 823 | state = cbuildbot_launch.GetCurrentBuildState(options, 'branchA') |
| 824 | |
| 825 | expected_state = build_summary.BuildSummary( |
| 826 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 827 | buildroot_layout=22, |
| 828 | branch='branchA') |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 829 | self.assertEqual(state, expected_state) |
| 830 | |
| 831 | def testGetLastBuildStateNoFile(self): |
| 832 | """Tests GetLastBuildState if the file is missing.""" |
| 833 | osutils.SafeMakedirs(self.root) |
| 834 | state = cbuildbot_launch.GetLastBuildState(self.root) |
| 835 | self.assertEqual(state, build_summary.BuildSummary()) |
| 836 | |
| 837 | def testGetLastBuildStateBadFile(self): |
| 838 | """Tests GetLastBuildState if the file contains invalid JSON.""" |
| 839 | osutils.SafeMakedirs(self.root) |
| 840 | osutils.WriteFile(self.previous_build_state, '}}') |
| 841 | state = cbuildbot_launch.GetLastBuildState(self.root) |
| 842 | self.assertEqual(state, build_summary.BuildSummary()) |
| 843 | |
| 844 | def testGetLastBuildStateMissingBuildStatus(self): |
| 845 | """Tests GetLastBuildState if the file doesn't have a valid status.""" |
| 846 | osutils.SafeMakedirs(self.root) |
| 847 | osutils.WriteFile(self.previous_build_state, '{"build_number": "3"}') |
| 848 | state = cbuildbot_launch.GetLastBuildState(self.root) |
| 849 | self.assertEqual(state, build_summary.BuildSummary()) |
| 850 | |
| 851 | def testGetLastBuildStateGoodFile(self): |
| 852 | """Tests GetLastBuildState on a good file.""" |
| 853 | osutils.SafeMakedirs(self.root) |
| 854 | osutils.WriteFile( |
| 855 | self.previous_build_state, |
| 856 | '{"build_number": 1, "master_build_id": 3, "status": "pass"}') |
| 857 | state = cbuildbot_launch.GetLastBuildState(self.root) |
| 858 | self.assertEqual( |
| 859 | state, |
| 860 | build_summary.BuildSummary( |
| 861 | build_number=1, master_build_id=3, status='pass')) |
| 862 | |
| 863 | def testSetLastBuildState(self): |
| 864 | """Verifies that SetLastBuildState writes to the expected file.""" |
| 865 | osutils.SafeMakedirs(self.root) |
| 866 | old_state = build_summary.BuildSummary( |
| 867 | build_number=314, |
| 868 | master_build_id=2178, |
| 869 | status=constants.BUILDER_STATUS_PASSED) |
| 870 | cbuildbot_launch.SetLastBuildState(self.root, old_state) |
| 871 | |
| 872 | saved_state = osutils.ReadFile(self.previous_build_state) |
| 873 | new_state = build_summary.BuildSummary() |
| 874 | new_state.from_json(saved_state) |
| 875 | |
| 876 | self.assertEqual(old_state, new_state) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame^] | 877 | |
| 878 | def testSetLastBuildStateMigration(self): |
| 879 | """Verifies that SetLastBuildState deletes the old state file.""" |
| 880 | osutils.SafeMakedirs(self.root) |
| 881 | osutils.WriteFile(self.state, '1 happy-branch') |
| 882 | state = build_summary.BuildSummary( |
| 883 | build_number=314, |
| 884 | master_build_id=2178, |
| 885 | status=constants.BUILDER_STATUS_PASSED) |
| 886 | cbuildbot_launch.SetLastBuildState(self.root, state) |
| 887 | |
| 888 | self.assertExists(self.previous_build_state) |
| 889 | self.assertNotExists(self.state) |