blob: 5d374bfcb3325fed58a548e06e204a2f48996de0 [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
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,
David James629febb2012-11-25 13:07:34 -080068 '--out-file', TAR_NAME, '--package', constants.CHROME_CP])
Ryan Cui6290f032012-11-20 15:44:43 -080069 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,
David James629febb2012-11-25 13:07:34 -080080 '--package', constants.CHROME_CP] + extra_args)
Ryan Cui6290f032012-11-20 15:44:43 -080081
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 James629febb2012-11-25 13:07:34 -080099 constants.CHROME_CP])
Ryan Cui6290f032012-11-20 15:44:43 -0800100 self.assertRaises(cros_build_lib.DieSystemExit,
101 cros_gen.FinishParsing, options)
102
103
104if __name__ == '__main__':
105 cros_test_lib.main()