blob: c8a6d05d0546e8872fa53950848f8131b4afa38b [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 '..', '..'))
David James629febb2012-11-25 13:07:34 -080013from chromite.buildbot import constants
Ryan Cui6290f032012-11-20 15:44:43 -080014from chromite.lib import cros_build_lib
15from chromite.lib import cros_test_lib
16from chromite.scripts import cros_generate_sysroot as cros_gen
17from chromite.lib import osutils
18from chromite.lib import partial_mock
19
20
21Dir = cros_test_lib.Directory
22
23
24class 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
48BOARD = 'lumpy'
49TAR_NAME = 'test.tar.xz'
Ryan Cui6290f032012-11-20 15:44:43 -080050
51
Ryan Cui4d6fca92012-12-13 16:41:56 -080052class OverallTest(cros_test_lib.MockTempDirTestCase):
Ryan Cui6290f032012-11-20 15:44:43 -080053
54 def setUp(self):
55 self.cg_mock = CrosGenMock()
Ryan Cui4d6fca92012-12-13 16:41:56 -080056 self.StartPatcher(self.cg_mock)
Ryan Cui6290f032012-11-20 15:44:43 -080057
58 def testTarballGeneration(self):
59 """End-to-end test of tarball generation."""
Ryan Cui4d6fca92012-12-13 16:41:56 -080060 with mock.patch.object(cros_build_lib, 'IsInsideChroot'):
61 cros_build_lib.IsInsideChroot.returnvalue = True
62 cros_gen.main(
63 ['--board', BOARD, '--out-dir', self.tempdir,
64 '--out-file', TAR_NAME, '--package', constants.CHROME_CP])
65 self.cg_mock.VerifyTarball(os.path.join(self.tempdir, TAR_NAME))
Ryan Cui6290f032012-11-20 15:44:43 -080066
67
68class InterfaceTest(cros_test_lib.TempDirTestCase):
69 """Test Parsing and error checking functionality."""
70
71 BAD_TARGET_DIR = '/path/to/nowhere'
72
73 def _Parse(self, extra_args):
74 return cros_gen.ParseCommandLine(
75 ['--board', BOARD, '--out-dir', self.tempdir,
David James629febb2012-11-25 13:07:34 -080076 '--package', constants.CHROME_CP] + extra_args)
Ryan Cui6290f032012-11-20 15:44:43 -080077
78 def testDefaultTargetName(self):
79 """We are getting the right default target name."""
80 options = self._Parse([])
81 self.assertEquals(
82 options.out_file, 'sysroot_chromeos-base_chromeos-chrome.tar.xz')
83
84 def testExistingTarget(self):
85 """Erroring out on pre-existing target."""
86 options = self._Parse(['--out-file', TAR_NAME])
87 osutils.Touch(os.path.join(self.tempdir, TAR_NAME))
88 self.assertRaises(cros_build_lib.DieSystemExit,
89 cros_gen.FinishParsing, options)
90
91 def testNonExisting(self):
92 """Erroring out on non-existent output dir."""
93 options = cros_gen.ParseCommandLine(
94 ['--board', BOARD, '--out-dir', self.BAD_TARGET_DIR, '--package',
David James629febb2012-11-25 13:07:34 -080095 constants.CHROME_CP])
Ryan Cui6290f032012-11-20 15:44:43 -080096 self.assertRaises(cros_build_lib.DieSystemExit,
97 cros_gen.FinishParsing, options)
98
99
100if __name__ == '__main__':
101 cros_test_lib.main()