Chris Sosa | aef4cb9 | 2011-02-28 16:00:14 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | """Unittests for commands. Needs to be run inside of chroot for mox.""" |
| 8 | |
| 9 | import __builtin__ |
| 10 | import mox |
| 11 | import os |
| 12 | import shutil |
| 13 | import sys |
| 14 | import unittest |
| 15 | |
| 16 | import constants |
| 17 | sys.path.append(constants.SOURCE_ROOT) |
| 18 | import chromite.buildbot.cbuildbot_commands as commands |
| 19 | import chromite.lib.cros_build_lib as cros_lib |
| 20 | |
| 21 | class CBuildBotTest(mox.MoxTestBase): |
| 22 | |
| 23 | def setUp(self): |
| 24 | mox.MoxTestBase.setUp(self) |
| 25 | # Always stub RunCommmand out as we use it in every method. |
| 26 | self.mox.StubOutWithMock(cros_lib, 'OldRunCommand') |
| 27 | self.tracking_branch = 'cros/master' |
| 28 | self._test_repos = [['kernel', 'third_party/kernel/files'], |
| 29 | ['login_manager', 'platform/login_manager'] |
| 30 | ] |
| 31 | self._test_cros_workon_packages = ( |
| 32 | 'chromeos-base/kernel\nchromeos-base/chromeos-login\n') |
| 33 | self._test_board = 'test-board' |
| 34 | self._buildroot = '.' |
| 35 | self._test_dict = {'kernel': ['chromos-base/kernel', 'dev-util/perf'], |
| 36 | 'cros': ['chromos-base/libcros'] |
| 37 | } |
| 38 | self._test_string = 'kernel.git@12345test cros.git@12333test' |
| 39 | self._test_string += ' crosutils.git@blahblah' |
| 40 | self._revision_file = 'test-revisions.pfq' |
| 41 | self._test_parsed_string_array = [['chromeos-base/kernel', '12345test'], |
| 42 | ['dev-util/perf', '12345test'], |
| 43 | ['chromos-base/libcros', '12345test']] |
| 44 | self._overlays = ['%s/src/third_party/chromiumos-overlay' % self._buildroot] |
| 45 | self._chroot_overlays = [ |
| 46 | cros_lib.ReinterpretPathForChroot(p) for p in self._overlays |
| 47 | ] |
| 48 | |
| 49 | def testArchiveTestResults(self): |
| 50 | """Test if we can archive the latest results dir to Google Storage.""" |
| 51 | # Set vars for call. |
| 52 | self.mox.StubOutWithMock(shutil, 'rmtree') |
| 53 | buildroot = '/fake_dir' |
| 54 | archive_tarball = os.path.join(buildroot, 'test_results.tgz') |
| 55 | test_results_dir = 'fake_results_dir' |
| 56 | |
| 57 | # Convenience variables to make archive easier to understand. |
| 58 | path_to_results = os.path.join(buildroot, 'chroot', test_results_dir) |
| 59 | |
| 60 | cros_lib.OldRunCommand(['sudo', 'chmod', '-R', 'a+rw', path_to_results]) |
| 61 | cros_lib.OldRunCommand(['tar', 'czf', archive_tarball, path_to_results]) |
| 62 | shutil.rmtree(path_to_results) |
| 63 | self.mox.ReplayAll() |
| 64 | return_ball = commands.ArchiveTestResults(buildroot, test_results_dir) |
| 65 | self.mox.VerifyAll() |
| 66 | self.assertEqual(return_ball, archive_tarball) |
| 67 | |
| 68 | |
| 69 | def testUprevAllPackages(self): |
| 70 | """Test if we get None in revisions.pfq indicating Full Builds.""" |
| 71 | drop_file = commands._PACKAGE_FILE % {'buildroot': self._buildroot} |
| 72 | cros_lib.OldRunCommand( |
| 73 | ['../../chromite/buildbot/cros_mark_as_stable', '--all', |
| 74 | '--board=%s' % self._test_board, |
| 75 | '--overlays=%s' % ':'.join(self._chroot_overlays), |
| 76 | '--tracking_branch=cros/master', |
| 77 | '--drop_file=%s' % cros_lib.ReinterpretPathForChroot(drop_file), |
| 78 | 'commit'], |
| 79 | cwd='%s/src/scripts' % self._buildroot, |
| 80 | enter_chroot=True) |
| 81 | |
| 82 | self.mox.ReplayAll() |
| 83 | commands.UprevPackages(self._buildroot, self.tracking_branch, |
| 84 | self._test_board, self._overlays) |
| 85 | self.mox.VerifyAll() |
| 86 | |
| 87 | def testUploadPublicPrebuilts(self): |
| 88 | """Test _UploadPrebuilts with a public location.""" |
| 89 | binhost = 'http://www.example.com' |
| 90 | binhosts = [binhost, None] |
| 91 | check = mox.And(mox.IsA(list), mox.In(binhost), mox.Not(mox.In(None)), |
| 92 | mox.In('gs://chromeos-prebuilt'), mox.In('preflight')) |
| 93 | cros_lib.OldRunCommand(check, cwd=os.path.dirname(commands.__file__)) |
| 94 | self.mox.ReplayAll() |
| 95 | commands.UploadPrebuilts(self._buildroot, self._test_board, 'public', |
| 96 | binhosts, 'preflight', None) |
| 97 | self.mox.VerifyAll() |
| 98 | |
| 99 | def testUploadPrivatePrebuilts(self): |
| 100 | """Test _UploadPrebuilts with a private location.""" |
| 101 | binhost = 'http://www.example.com' |
| 102 | binhosts = [binhost, None] |
| 103 | check = mox.And(mox.IsA(list), mox.In(binhost), mox.Not(mox.In(None)), |
| 104 | mox.In('chromeos-images:/var/www/prebuilt/'), |
| 105 | mox.In('chrome')) |
| 106 | cros_lib.OldRunCommand(check, cwd=os.path.dirname(commands.__file__)) |
| 107 | self.mox.ReplayAll() |
| 108 | commands.UploadPrebuilts(self._buildroot, self._test_board, 'private', |
| 109 | binhosts, 'chrome', 'tot') |
| 110 | self.mox.VerifyAll() |
| 111 | |
Ryan Cui | 4057aa2 | 2011-04-01 14:08:29 -0700 | [diff] [blame^] | 112 | def testRepoSyncRetriesFail(self): |
| 113 | """Case where we exceed default retry attempts.""" |
| 114 | cros_lib.OldRunCommand(mox.In('sync'), |
| 115 | cwd=self._buildroot).AndRaise(Exception("failed")) |
| 116 | cros_lib.OldRunCommand(mox.In('sync'), |
| 117 | cwd=self._buildroot).AndRaise(Exception("failed")) |
| 118 | cros_lib.OldRunCommand(mox.In('sync'), |
| 119 | cwd=self._buildroot).AndRaise(Exception("failed")) |
| 120 | self.mox.ReplayAll() |
| 121 | self.assertRaises(Exception, lambda: commands._RepoSync(self._buildroot)) |
| 122 | self.mox.VerifyAll() |
| 123 | |
| 124 | def testRepoSyncRetriesPass(self): |
| 125 | """Case where we fail twice and pass on the third try.""" |
| 126 | cros_lib.OldRunCommand(mox.In('sync'), |
| 127 | cwd=self._buildroot).AndRaise(Exception("failed")) |
| 128 | cros_lib.OldRunCommand(mox.In('sync'), |
| 129 | cwd=self._buildroot).AndRaise(Exception("failed")) |
| 130 | cros_lib.OldRunCommand(mox.In('sync'), cwd=self._buildroot) |
| 131 | cros_lib.OldRunCommand(mox.In('forall'), cwd=self._buildroot) |
| 132 | cros_lib.OldRunCommand(mox.In('manifest'), cwd=self._buildroot) |
| 133 | self.mox.ReplayAll() |
| 134 | commands._RepoSync(self._buildroot) |
| 135 | self.mox.VerifyAll() |
| 136 | |
| 137 | def testRepoSyncCustomRetriesFail(self): |
| 138 | """Case where _RepoSync is called with a custom retry value of 1. Should |
| 139 | throw exception after first failure.""" |
| 140 | cros_lib.OldRunCommand(mox.In('sync'), |
| 141 | cwd=self._buildroot).AndRaise(Exception("failed")) |
| 142 | self.mox.ReplayAll() |
| 143 | self.assertRaises( |
| 144 | Exception, |
| 145 | lambda: commands._RepoSync(self._buildroot, retries=1)) |
| 146 | self.mox.VerifyAll() |
| 147 | |
| 148 | def testRepoSyncCustomRetriesPass(self): |
| 149 | """Case where _RepoSync is called with a custom retry value of 2 and passes |
| 150 | the second time.""" |
| 151 | cros_lib.OldRunCommand(mox.In('sync'), |
| 152 | cwd=self._buildroot).AndRaise(Exception("failed")) |
| 153 | cros_lib.OldRunCommand(mox.In('sync'), cwd=self._buildroot) |
| 154 | cros_lib.OldRunCommand(mox.In('forall'), cwd=self._buildroot) |
| 155 | cros_lib.OldRunCommand(mox.In('manifest'), cwd=self._buildroot) |
| 156 | self.mox.ReplayAll() |
| 157 | commands._RepoSync(self._buildroot, retries=2) |
| 158 | self.mox.VerifyAll() |
| 159 | |
Chris Sosa | aef4cb9 | 2011-02-28 16:00:14 -0800 | [diff] [blame] | 160 | |
| 161 | if __name__ == '__main__': |
| 162 | unittest.main() |