Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / chromite / scripts / cros_generate_sysroot_unittest.py
1 #!/usr/bin/python
2 # 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
6 """Unittests for cros_generate_sysroot."""
7
8 from __future__ import print_function
9
10 import os
11 import sys
12
13 sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
14                                 '..', '..'))
15 from chromite.cbuildbot import constants
16 from chromite.lib import cros_build_lib
17 from chromite.lib import cros_test_lib
18 from chromite.scripts import cros_generate_sysroot as cros_gen
19 from chromite.lib import osutils
20 from chromite.lib import partial_mock
21
22 # TODO(build): Finish test wrapper (http://crosbug.com/37517).
23 # Until then, this has to be after the chromite imports.
24 import mock
25
26
27 Dir = cros_test_lib.Directory
28
29
30 class CrosGenMock(partial_mock.PartialMock):
31   """Helper class to Mock out cros_generate_sysroot.GenerateSysroot."""
32
33   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
56 BOARD = 'lumpy'
57 TAR_NAME = 'test.tar.xz'
58
59
60 class OverallTest(cros_test_lib.MockTempDirTestCase):
61   """Tests for cros_generate_sysroot."""
62
63   def setUp(self):
64     self.cg_mock = self.StartPatcher(CrosGenMock())
65
66   def testTarballGeneration(self):
67     """End-to-end test of tarball generation."""
68     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))
74
75
76 class 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,
84          '--package', constants.CHROME_CP] + extra_args)
85
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',
103          constants.CHROME_CP])
104     self.assertRaises(cros_build_lib.DieSystemExit,
105                       cros_gen.FinishParsing, options)
106
107
108 if __name__ == '__main__':
109   cros_test_lib.main()