blob: 90801c5b19dc37a8dbf9b2901ee788b5bf7ba770 [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
Ryan Cui6290f032012-11-20 15:44:43 -08007import os
8import sys
9
10sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
11 '..', '..'))
David James629febb2012-11-25 13:07:34 -080012from chromite.buildbot import constants
Ryan Cui6290f032012-11-20 15:44:43 -080013from 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
Brian Harringe7524372012-12-23 02:02:56 -080019# TODO(build): Finish test wrapper (http://crosbug.com/37517).
20# Until then, this has to be after the chromite imports.
21import mock
22
Ryan Cui6290f032012-11-20 15:44:43 -080023
24Dir = cros_test_lib.Directory
25
26
27class CrosGenMock(partial_mock.PartialMock):
28 TARGET = 'chromite.scripts.cros_generate_sysroot.GenerateSysroot'
29 ATTRS = ('_InstallToolchain', '_InstallKernelHeaders',
30 '_InstallBuildDependencies')
31
32 TOOLCHAIN = 'toolchain'
33 KERNEL_HEADERS = 'kernel_headers'
34 BUILD_DEPS = 'build-deps'
35
36 def _InstallToolchain(self, inst):
37 osutils.Touch(os.path.join(inst.sysroot, self.TOOLCHAIN))
38
39 def _InstallKernelHeaders(self, inst):
40 osutils.Touch(os.path.join(inst.sysroot, self.KERNEL_HEADERS))
41
42 def _InstallBuildDependencies(self, inst):
43 osutils.Touch(os.path.join(inst.sysroot, self.BUILD_DEPS))
44
45 def VerifyTarball(self, tarball):
46 dir_struct = [Dir('.', []), self.TOOLCHAIN, self.KERNEL_HEADERS,
47 self.BUILD_DEPS]
48 cros_test_lib.VerifyTarball(tarball, dir_struct)
49
50
51BOARD = 'lumpy'
52TAR_NAME = 'test.tar.xz'
Ryan Cui6290f032012-11-20 15:44:43 -080053
54
Ryan Cui4d6fca92012-12-13 16:41:56 -080055class OverallTest(cros_test_lib.MockTempDirTestCase):
Ryan Cui6290f032012-11-20 15:44:43 -080056
57 def setUp(self):
58 self.cg_mock = CrosGenMock()
Ryan Cui4d6fca92012-12-13 16:41:56 -080059 self.StartPatcher(self.cg_mock)
Ryan Cui6290f032012-11-20 15:44:43 -080060
61 def testTarballGeneration(self):
62 """End-to-end test of tarball generation."""
Ryan Cui4d6fca92012-12-13 16:41:56 -080063 with mock.patch.object(cros_build_lib, 'IsInsideChroot'):
64 cros_build_lib.IsInsideChroot.returnvalue = True
65 cros_gen.main(
66 ['--board', BOARD, '--out-dir', self.tempdir,
67 '--out-file', TAR_NAME, '--package', constants.CHROME_CP])
68 self.cg_mock.VerifyTarball(os.path.join(self.tempdir, TAR_NAME))
Ryan Cui6290f032012-11-20 15:44:43 -080069
70
71class InterfaceTest(cros_test_lib.TempDirTestCase):
72 """Test Parsing and error checking functionality."""
73
74 BAD_TARGET_DIR = '/path/to/nowhere'
75
76 def _Parse(self, extra_args):
77 return cros_gen.ParseCommandLine(
78 ['--board', BOARD, '--out-dir', self.tempdir,
David James629febb2012-11-25 13:07:34 -080079 '--package', constants.CHROME_CP] + extra_args)
Ryan Cui6290f032012-11-20 15:44:43 -080080
81 def testDefaultTargetName(self):
82 """We are getting the right default target name."""
83 options = self._Parse([])
84 self.assertEquals(
85 options.out_file, 'sysroot_chromeos-base_chromeos-chrome.tar.xz')
86
87 def testExistingTarget(self):
88 """Erroring out on pre-existing target."""
89 options = self._Parse(['--out-file', TAR_NAME])
90 osutils.Touch(os.path.join(self.tempdir, TAR_NAME))
91 self.assertRaises(cros_build_lib.DieSystemExit,
92 cros_gen.FinishParsing, options)
93
94 def testNonExisting(self):
95 """Erroring out on non-existent output dir."""
96 options = cros_gen.ParseCommandLine(
97 ['--board', BOARD, '--out-dir', self.BAD_TARGET_DIR, '--package',
David James629febb2012-11-25 13:07:34 -080098 constants.CHROME_CP])
Ryan Cui6290f032012-11-20 15:44:43 -080099 self.assertRaises(cros_build_lib.DieSystemExit,
100 cros_gen.FinishParsing, options)
101
102
103if __name__ == '__main__':
104 cros_test_lib.main()