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.
7 """Unittests for cros_generate_sysroot."""
12 sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
14 from chromite.buildbot import constants
15 from chromite.lib import cros_build_lib
16 from chromite.lib import cros_test_lib
17 from chromite.scripts import cros_generate_sysroot as cros_gen
18 from chromite.lib import osutils
19 from chromite.lib import partial_mock
21 # TODO(build): Finish test wrapper (http://crosbug.com/37517).
22 # Until then, this has to be after the chromite imports.
26 Dir = cros_test_lib.Directory
29 class CrosGenMock(partial_mock.PartialMock):
30 """Helper class to Mock out cros_generate_sysroot.GenerateSysroot."""
32 TARGET = 'chromite.scripts.cros_generate_sysroot.GenerateSysroot'
33 ATTRS = ('_InstallToolchain', '_InstallKernelHeaders',
34 '_InstallBuildDependencies')
36 TOOLCHAIN = 'toolchain'
37 KERNEL_HEADERS = 'kernel_headers'
38 BUILD_DEPS = 'build-deps'
40 def _InstallToolchain(self, inst):
41 osutils.Touch(os.path.join(inst.sysroot, self.TOOLCHAIN))
43 def _InstallKernelHeaders(self, inst):
44 osutils.Touch(os.path.join(inst.sysroot, self.KERNEL_HEADERS))
46 def _InstallBuildDependencies(self, inst):
47 osutils.Touch(os.path.join(inst.sysroot, self.BUILD_DEPS))
49 def VerifyTarball(self, tarball):
50 dir_struct = [Dir('.', []), self.TOOLCHAIN, self.KERNEL_HEADERS,
52 cros_test_lib.VerifyTarball(tarball, dir_struct)
56 TAR_NAME = 'test.tar.xz'
59 class OverallTest(cros_test_lib.MockTempDirTestCase):
60 """Tests for cros_generate_sysroot."""
63 self.cg_mock = self.StartPatcher(CrosGenMock())
65 def testTarballGeneration(self):
66 """End-to-end test of tarball generation."""
67 with mock.patch.object(cros_build_lib, 'IsInsideChroot'):
68 cros_build_lib.IsInsideChroot.returnvalue = True
70 ['--board', BOARD, '--out-dir', self.tempdir,
71 '--out-file', TAR_NAME, '--package', constants.CHROME_CP])
72 self.cg_mock.VerifyTarball(os.path.join(self.tempdir, TAR_NAME))
75 class InterfaceTest(cros_test_lib.TempDirTestCase):
76 """Test Parsing and error checking functionality."""
78 BAD_TARGET_DIR = '/path/to/nowhere'
80 def _Parse(self, extra_args):
81 return cros_gen.ParseCommandLine(
82 ['--board', BOARD, '--out-dir', self.tempdir,
83 '--package', constants.CHROME_CP] + extra_args)
85 def testDefaultTargetName(self):
86 """We are getting the right default target name."""
87 options = self._Parse([])
89 options.out_file, 'sysroot_chromeos-base_chromeos-chrome.tar.xz')
91 def testExistingTarget(self):
92 """Erroring out on pre-existing target."""
93 options = self._Parse(['--out-file', TAR_NAME])
94 osutils.Touch(os.path.join(self.tempdir, TAR_NAME))
95 self.assertRaises(cros_build_lib.DieSystemExit,
96 cros_gen.FinishParsing, options)
98 def testNonExisting(self):
99 """Erroring out on non-existent output dir."""
100 options = cros_gen.ParseCommandLine(
101 ['--board', BOARD, '--out-dir', self.BAD_TARGET_DIR, '--package',
102 constants.CHROME_CP])
103 self.assertRaises(cros_build_lib.DieSystemExit,
104 cros_gen.FinishParsing, options)
107 if __name__ == '__main__':