blob: 50e5e45bdba1ac2f5a2bee268abae2f95d5bfe60 [file] [log] [blame]
Ryan Cui6290f032012-11-20 15:44:43 -08001#!/usr/bin/python
Ryan Cui6290f032012-11-20 15:44:43 -08002# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
Don Garrett25f309a2014-03-19 14:02:12 -07006"""Unittests for cros_generate_sysroot."""
7
Mike Frysinger383367e2014-09-16 15:06:17 -04008from __future__ import print_function
9
Ryan Cui6290f032012-11-20 15:44:43 -080010import os
11import sys
12
13sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
14 '..', '..'))
Don Garrett88b8d782014-05-13 17:30:55 -070015from chromite.cbuildbot import constants
Ryan Cui6290f032012-11-20 15:44:43 -080016from chromite.lib import cros_build_lib
17from chromite.lib import cros_test_lib
18from chromite.scripts import cros_generate_sysroot as cros_gen
19from chromite.lib import osutils
20from chromite.lib import partial_mock
21
Brian Harringe7524372012-12-23 02:02:56 -080022# TODO(build): Finish test wrapper (http://crosbug.com/37517).
23# Until then, this has to be after the chromite imports.
24import mock
25
Ryan Cui6290f032012-11-20 15:44:43 -080026
27Dir = cros_test_lib.Directory
28
29
30class CrosGenMock(partial_mock.PartialMock):
Don Garrett25f309a2014-03-19 14:02:12 -070031 """Helper class to Mock out cros_generate_sysroot.GenerateSysroot."""
32
Ryan Cui6290f032012-11-20 15:44:43 -080033 TARGET = 'chromite.scripts.cros_generate_sysroot.GenerateSysroot'
34 ATTRS = ('_InstallToolchain', '_InstallKernelHeaders',
35 '_InstallBuildDependencies')
36
37 TOOLCHAIN = 'toolchain'
38 KERNEL_HEADERS = 'kernel_headers'
39 BUILD_DEPS = 'build-deps'
40
41 def _InstallToolchain(self, inst):
42 osutils.Touch(os.path.join(inst.sysroot, self.TOOLCHAIN))
43
44 def _InstallKernelHeaders(self, inst):
45 osutils.Touch(os.path.join(inst.sysroot, self.KERNEL_HEADERS))
46
47 def _InstallBuildDependencies(self, inst):
48 osutils.Touch(os.path.join(inst.sysroot, self.BUILD_DEPS))
49
50 def VerifyTarball(self, tarball):
51 dir_struct = [Dir('.', []), self.TOOLCHAIN, self.KERNEL_HEADERS,
52 self.BUILD_DEPS]
53 cros_test_lib.VerifyTarball(tarball, dir_struct)
54
55
56BOARD = 'lumpy'
57TAR_NAME = 'test.tar.xz'
Ryan Cui6290f032012-11-20 15:44:43 -080058
59
Ryan Cui4d6fca92012-12-13 16:41:56 -080060class OverallTest(cros_test_lib.MockTempDirTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -070061 """Tests for cros_generate_sysroot."""
Ryan Cui6290f032012-11-20 15:44:43 -080062
63 def setUp(self):
Ryan Cuif1416f32013-01-22 18:43:41 -080064 self.cg_mock = self.StartPatcher(CrosGenMock())
Ryan Cui6290f032012-11-20 15:44:43 -080065
66 def testTarballGeneration(self):
67 """End-to-end test of tarball generation."""
Ryan Cui4d6fca92012-12-13 16:41:56 -080068 with mock.patch.object(cros_build_lib, 'IsInsideChroot'):
69 cros_build_lib.IsInsideChroot.returnvalue = True
70 cros_gen.main(
71 ['--board', BOARD, '--out-dir', self.tempdir,
72 '--out-file', TAR_NAME, '--package', constants.CHROME_CP])
73 self.cg_mock.VerifyTarball(os.path.join(self.tempdir, TAR_NAME))
Ryan Cui6290f032012-11-20 15:44:43 -080074
75
76class InterfaceTest(cros_test_lib.TempDirTestCase):
77 """Test Parsing and error checking functionality."""
78
79 BAD_TARGET_DIR = '/path/to/nowhere'
80
81 def _Parse(self, extra_args):
82 return cros_gen.ParseCommandLine(
83 ['--board', BOARD, '--out-dir', self.tempdir,
David James629febb2012-11-25 13:07:34 -080084 '--package', constants.CHROME_CP] + extra_args)
Ryan Cui6290f032012-11-20 15:44:43 -080085
86 def testDefaultTargetName(self):
87 """We are getting the right default target name."""
88 options = self._Parse([])
89 self.assertEquals(
90 options.out_file, 'sysroot_chromeos-base_chromeos-chrome.tar.xz')
91
92 def testExistingTarget(self):
93 """Erroring out on pre-existing target."""
94 options = self._Parse(['--out-file', TAR_NAME])
95 osutils.Touch(os.path.join(self.tempdir, TAR_NAME))
96 self.assertRaises(cros_build_lib.DieSystemExit,
97 cros_gen.FinishParsing, options)
98
99 def testNonExisting(self):
100 """Erroring out on non-existent output dir."""
101 options = cros_gen.ParseCommandLine(
102 ['--board', BOARD, '--out-dir', self.BAD_TARGET_DIR, '--package',
David James629febb2012-11-25 13:07:34 -0800103 constants.CHROME_CP])
Ryan Cui6290f032012-11-20 15:44:43 -0800104 self.assertRaises(cros_build_lib.DieSystemExit,
105 cros_gen.FinishParsing, options)
106
107
108if __name__ == '__main__':
109 cros_test_lib.main()