2 # Copyright (c) 2012 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.
6 """Unittests for cros_generate_sysroot."""
8 from __future__ import print_function
13 sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
15 from chromite.cbuildbot import constants
16 from chromite.lib import cros_build_lib
17 from chromite.lib import cros_test_lib
18 from chromite.scripts import cros_generate_sysroot as cros_gen
19 from chromite.lib import osutils
20 from chromite.lib import partial_mock
22 # TODO(build): Finish test wrapper (http://crosbug.com/37517).
23 # Until then, this has to be after the chromite imports.
27 Dir = cros_test_lib.Directory
30 class CrosGenMock(partial_mock.PartialMock):
31 """Helper class to Mock out cros_generate_sysroot.GenerateSysroot."""
33 TARGET = 'chromite.scripts.cros_generate_sysroot.GenerateSysroot'
34 ATTRS = ('_InstallToolchain', '_InstallKernelHeaders',
35 '_InstallBuildDependencies')
37 TOOLCHAIN = 'toolchain'
38 KERNEL_HEADERS = 'kernel_headers'
39 BUILD_DEPS = 'build-deps'
41 def _InstallToolchain(self, inst):
42 osutils.Touch(os.path.join(inst.sysroot, self.TOOLCHAIN))
44 def _InstallKernelHeaders(self, inst):
45 osutils.Touch(os.path.join(inst.sysroot, self.KERNEL_HEADERS))
47 def _InstallBuildDependencies(self, inst):
48 osutils.Touch(os.path.join(inst.sysroot, self.BUILD_DEPS))
50 def VerifyTarball(self, tarball):
51 dir_struct = [Dir('.', []), self.TOOLCHAIN, self.KERNEL_HEADERS,
53 cros_test_lib.VerifyTarball(tarball, dir_struct)
57 TAR_NAME = 'test.tar.xz'
60 class OverallTest(cros_test_lib.MockTempDirTestCase):
61 """Tests for cros_generate_sysroot."""
64 self.cg_mock = self.StartPatcher(CrosGenMock())
66 def testTarballGeneration(self):
67 """End-to-end test of tarball generation."""
68 with mock.patch.object(cros_build_lib, 'IsInsideChroot'):
69 cros_build_lib.IsInsideChroot.returnvalue = True
71 ['--board', BOARD, '--out-dir', self.tempdir,
72 '--out-file', TAR_NAME, '--package', constants.CHROME_CP])
73 self.cg_mock.VerifyTarball(os.path.join(self.tempdir, TAR_NAME))
76 class InterfaceTest(cros_test_lib.TempDirTestCase):
77 """Test Parsing and error checking functionality."""
79 BAD_TARGET_DIR = '/path/to/nowhere'
81 def _Parse(self, extra_args):
82 return cros_gen.ParseCommandLine(
83 ['--board', BOARD, '--out-dir', self.tempdir,
84 '--package', constants.CHROME_CP] + extra_args)
86 def testDefaultTargetName(self):
87 """We are getting the right default target name."""
88 options = self._Parse([])
90 options.out_file, 'sysroot_chromeos-base_chromeos-chrome.tar.xz')
92 def testExistingTarget(self):
93 """Erroring out on pre-existing target."""
94 options = self._Parse(['--out-file', TAR_NAME])
95 osutils.Touch(os.path.join(self.tempdir, TAR_NAME))
96 self.assertRaises(cros_build_lib.DieSystemExit,
97 cros_gen.FinishParsing, options)
99 def testNonExisting(self):
100 """Erroring out on non-existent output dir."""
101 options = cros_gen.ParseCommandLine(
102 ['--board', BOARD, '--out-dir', self.BAD_TARGET_DIR, '--package',
103 constants.CHROME_CP])
104 self.assertRaises(cros_build_lib.DieSystemExit,
105 cros_gen.FinishParsing, options)
108 if __name__ == '__main__':