Ryan Cui | 6290f03 | 2012-11-20 15:44:43 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | # Copyright (c) 2012 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 | |
Ryan Cui | 6290f03 | 2012-11-20 15:44:43 -0800 | [diff] [blame] | 7 | import os |
| 8 | import sys |
| 9 | |
| 10 | sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), |
| 11 | '..', '..')) |
David James | 629febb | 2012-11-25 13:07:34 -0800 | [diff] [blame] | 12 | from chromite.buildbot import constants |
Ryan Cui | 6290f03 | 2012-11-20 15:44:43 -0800 | [diff] [blame] | 13 | from chromite.lib import cros_build_lib |
| 14 | from chromite.lib import cros_test_lib |
| 15 | from chromite.scripts import cros_generate_sysroot as cros_gen |
| 16 | from chromite.lib import osutils |
| 17 | from chromite.lib import partial_mock |
| 18 | |
Brian Harring | e752437 | 2012-12-23 02:02:56 -0800 | [diff] [blame] | 19 | # TODO(build): Finish test wrapper (http://crosbug.com/37517). |
| 20 | # Until then, this has to be after the chromite imports. |
| 21 | import mock |
| 22 | |
Ryan Cui | 6290f03 | 2012-11-20 15:44:43 -0800 | [diff] [blame] | 23 | |
| 24 | Dir = cros_test_lib.Directory |
| 25 | |
| 26 | |
| 27 | class CrosGenMock(partial_mock.PartialMock): |
| 28 | TARGET = 'chromite.scripts.cros_generate_sysroot.GenerateSysroot' |
| 29 | ATTRS = ('_InstallToolchain', '_InstallKernelHeaders', |
| 30 | '_InstallBuildDependencies') |
| 31 | |
| 32 | TOOLCHAIN = 'toolchain' |
| 33 | KERNEL_HEADERS = 'kernel_headers' |
| 34 | BUILD_DEPS = 'build-deps' |
| 35 | |
| 36 | def _InstallToolchain(self, inst): |
| 37 | osutils.Touch(os.path.join(inst.sysroot, self.TOOLCHAIN)) |
| 38 | |
| 39 | def _InstallKernelHeaders(self, inst): |
| 40 | osutils.Touch(os.path.join(inst.sysroot, self.KERNEL_HEADERS)) |
| 41 | |
| 42 | def _InstallBuildDependencies(self, inst): |
| 43 | osutils.Touch(os.path.join(inst.sysroot, self.BUILD_DEPS)) |
| 44 | |
| 45 | def VerifyTarball(self, tarball): |
| 46 | dir_struct = [Dir('.', []), self.TOOLCHAIN, self.KERNEL_HEADERS, |
| 47 | self.BUILD_DEPS] |
| 48 | cros_test_lib.VerifyTarball(tarball, dir_struct) |
| 49 | |
| 50 | |
| 51 | BOARD = 'lumpy' |
| 52 | TAR_NAME = 'test.tar.xz' |
Ryan Cui | 6290f03 | 2012-11-20 15:44:43 -0800 | [diff] [blame] | 53 | |
| 54 | |
Ryan Cui | 4d6fca9 | 2012-12-13 16:41:56 -0800 | [diff] [blame] | 55 | class OverallTest(cros_test_lib.MockTempDirTestCase): |
Ryan Cui | 6290f03 | 2012-11-20 15:44:43 -0800 | [diff] [blame] | 56 | |
| 57 | def setUp(self): |
Ryan Cui | f1416f3 | 2013-01-22 18:43:41 -0800 | [diff] [blame] | 58 | self.cg_mock = self.StartPatcher(CrosGenMock()) |
Ryan Cui | 6290f03 | 2012-11-20 15:44:43 -0800 | [diff] [blame] | 59 | |
| 60 | def testTarballGeneration(self): |
| 61 | """End-to-end test of tarball generation.""" |
Ryan Cui | 4d6fca9 | 2012-12-13 16:41:56 -0800 | [diff] [blame] | 62 | with mock.patch.object(cros_build_lib, 'IsInsideChroot'): |
| 63 | cros_build_lib.IsInsideChroot.returnvalue = True |
| 64 | cros_gen.main( |
| 65 | ['--board', BOARD, '--out-dir', self.tempdir, |
| 66 | '--out-file', TAR_NAME, '--package', constants.CHROME_CP]) |
| 67 | self.cg_mock.VerifyTarball(os.path.join(self.tempdir, TAR_NAME)) |
Ryan Cui | 6290f03 | 2012-11-20 15:44:43 -0800 | [diff] [blame] | 68 | |
| 69 | |
| 70 | class InterfaceTest(cros_test_lib.TempDirTestCase): |
| 71 | """Test Parsing and error checking functionality.""" |
| 72 | |
| 73 | BAD_TARGET_DIR = '/path/to/nowhere' |
| 74 | |
| 75 | def _Parse(self, extra_args): |
| 76 | return cros_gen.ParseCommandLine( |
| 77 | ['--board', BOARD, '--out-dir', self.tempdir, |
David James | 629febb | 2012-11-25 13:07:34 -0800 | [diff] [blame] | 78 | '--package', constants.CHROME_CP] + extra_args) |
Ryan Cui | 6290f03 | 2012-11-20 15:44:43 -0800 | [diff] [blame] | 79 | |
| 80 | def testDefaultTargetName(self): |
| 81 | """We are getting the right default target name.""" |
| 82 | options = self._Parse([]) |
| 83 | self.assertEquals( |
| 84 | options.out_file, 'sysroot_chromeos-base_chromeos-chrome.tar.xz') |
| 85 | |
| 86 | def testExistingTarget(self): |
| 87 | """Erroring out on pre-existing target.""" |
| 88 | options = self._Parse(['--out-file', TAR_NAME]) |
| 89 | osutils.Touch(os.path.join(self.tempdir, TAR_NAME)) |
| 90 | self.assertRaises(cros_build_lib.DieSystemExit, |
| 91 | cros_gen.FinishParsing, options) |
| 92 | |
| 93 | def testNonExisting(self): |
| 94 | """Erroring out on non-existent output dir.""" |
| 95 | options = cros_gen.ParseCommandLine( |
| 96 | ['--board', BOARD, '--out-dir', self.BAD_TARGET_DIR, '--package', |
David James | 629febb | 2012-11-25 13:07:34 -0800 | [diff] [blame] | 97 | constants.CHROME_CP]) |
Ryan Cui | 6290f03 | 2012-11-20 15:44:43 -0800 | [diff] [blame] | 98 | self.assertRaises(cros_build_lib.DieSystemExit, |
| 99 | cros_gen.FinishParsing, options) |
| 100 | |
| 101 | |
| 102 | if __name__ == '__main__': |
| 103 | cros_test_lib.main() |