blob: ffb40ad663312fb39fd1e4c029d3d46a339e85be [file] [log] [blame]
Ryan Cui6290f032012-11-20 15:44:43 -08001#!/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
7import mock
8import os
9import sys
10
11sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
12 '..', '..'))
13from chromite.lib import cros_build_lib
14from chromite.lib import cros_test_lib
15from chromite.scripts import cros_generate_sysroot as cros_gen
16from chromite.lib import osutils
17from chromite.lib import partial_mock
18
19
20Dir = cros_test_lib.Directory
21
22
23class CrosGenMock(partial_mock.PartialMock):
24 TARGET = 'chromite.scripts.cros_generate_sysroot.GenerateSysroot'
25 ATTRS = ('_InstallToolchain', '_InstallKernelHeaders',
26 '_InstallBuildDependencies')
27
28 TOOLCHAIN = 'toolchain'
29 KERNEL_HEADERS = 'kernel_headers'
30 BUILD_DEPS = 'build-deps'
31
32 def _InstallToolchain(self, inst):
33 osutils.Touch(os.path.join(inst.sysroot, self.TOOLCHAIN))
34
35 def _InstallKernelHeaders(self, inst):
36 osutils.Touch(os.path.join(inst.sysroot, self.KERNEL_HEADERS))
37
38 def _InstallBuildDependencies(self, inst):
39 osutils.Touch(os.path.join(inst.sysroot, self.BUILD_DEPS))
40
41 def VerifyTarball(self, tarball):
42 dir_struct = [Dir('.', []), self.TOOLCHAIN, self.KERNEL_HEADERS,
43 self.BUILD_DEPS]
44 cros_test_lib.VerifyTarball(tarball, dir_struct)
45
46
47BOARD = 'lumpy'
48TAR_NAME = 'test.tar.xz'
49CHROME_PKG = 'chromeos-base/chromeos-chrome'
50
51
52class 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,
68 '--out-file', TAR_NAME, '--package', CHROME_PKG])
69 self.cg_mock.VerifyTarball(os.path.join(self.tempdir, TAR_NAME))
70
71
72class 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,
80 '--package', CHROME_PKG] + extra_args)
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',
99 CHROME_PKG])
100 self.assertRaises(cros_build_lib.DieSystemExit,
101 cros_gen.FinishParsing, options)
102
103
104if __name__ == '__main__':
105 cros_test_lib.main()