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