blob: 8f326ea9e4fac6d3ba0a88c14b77bb90ea770e63 [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):
Ryan Cuif1416f32013-01-22 18:43:41 -080058 self.cg_mock = self.StartPatcher(CrosGenMock())
Ryan Cui6290f032012-11-20 15:44:43 -080059
60 def testTarballGeneration(self):
61 """End-to-end test of tarball generation."""
Ryan Cui4d6fca92012-12-13 16:41:56 -080062 with mock.patch.object(cros_build_lib, 'IsInsideChroot'):
63 cros_build_lib.IsInsideChroot.returnvalue = True
64 cros_gen.main(
65 ['--board', BOARD, '--out-dir', self.tempdir,
66 '--out-file', TAR_NAME, '--package', constants.CHROME_CP])
67 self.cg_mock.VerifyTarball(os.path.join(self.tempdir, TAR_NAME))
Ryan Cui6290f032012-11-20 15:44:43 -080068
69
70class InterfaceTest(cros_test_lib.TempDirTestCase):
71 """Test Parsing and error checking functionality."""
72
73 BAD_TARGET_DIR = '/path/to/nowhere'
74
75 def _Parse(self, extra_args):
76 return cros_gen.ParseCommandLine(
77 ['--board', BOARD, '--out-dir', self.tempdir,
David James629febb2012-11-25 13:07:34 -080078 '--package', constants.CHROME_CP] + extra_args)
Ryan Cui6290f032012-11-20 15:44:43 -080079
80 def testDefaultTargetName(self):
81 """We are getting the right default target name."""
82 options = self._Parse([])
83 self.assertEquals(
84 options.out_file, 'sysroot_chromeos-base_chromeos-chrome.tar.xz')
85
86 def testExistingTarget(self):
87 """Erroring out on pre-existing target."""
88 options = self._Parse(['--out-file', TAR_NAME])
89 osutils.Touch(os.path.join(self.tempdir, TAR_NAME))
90 self.assertRaises(cros_build_lib.DieSystemExit,
91 cros_gen.FinishParsing, options)
92
93 def testNonExisting(self):
94 """Erroring out on non-existent output dir."""
95 options = cros_gen.ParseCommandLine(
96 ['--board', BOARD, '--out-dir', self.BAD_TARGET_DIR, '--package',
David James629febb2012-11-25 13:07:34 -080097 constants.CHROME_CP])
Ryan Cui6290f032012-11-20 15:44:43 -080098 self.assertRaises(cros_build_lib.DieSystemExit,
99 cros_gen.FinishParsing, options)
100
101
102if __name__ == '__main__':
103 cros_test_lib.main()