Upstream version 8.36.161.0
[platform/framework/web/crosswalk.git] / src / third_party / chromite / scripts / cros_generate_sysroot_unittest.py
1 #!/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
7 """Unittests for cros_generate_sysroot."""
8
9 import os
10 import sys
11
12 sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
13                                 '..', '..'))
14 from chromite.buildbot import constants
15 from chromite.lib import cros_build_lib
16 from chromite.lib import cros_test_lib
17 from chromite.scripts import cros_generate_sysroot as cros_gen
18 from chromite.lib import osutils
19 from chromite.lib import partial_mock
20
21 # TODO(build): Finish test wrapper (http://crosbug.com/37517).
22 # Until then, this has to be after the chromite imports.
23 import mock
24
25
26 Dir = cros_test_lib.Directory
27
28
29 class CrosGenMock(partial_mock.PartialMock):
30   """Helper class to Mock out cros_generate_sysroot.GenerateSysroot."""
31
32   TARGET = 'chromite.scripts.cros_generate_sysroot.GenerateSysroot'
33   ATTRS = ('_InstallToolchain', '_InstallKernelHeaders',
34            '_InstallBuildDependencies')
35
36   TOOLCHAIN = 'toolchain'
37   KERNEL_HEADERS = 'kernel_headers'
38   BUILD_DEPS = 'build-deps'
39
40   def _InstallToolchain(self, inst):
41     osutils.Touch(os.path.join(inst.sysroot, self.TOOLCHAIN))
42
43   def _InstallKernelHeaders(self, inst):
44     osutils.Touch(os.path.join(inst.sysroot, self.KERNEL_HEADERS))
45
46   def _InstallBuildDependencies(self, inst):
47     osutils.Touch(os.path.join(inst.sysroot, self.BUILD_DEPS))
48
49   def VerifyTarball(self, tarball):
50     dir_struct = [Dir('.', []), self.TOOLCHAIN, self.KERNEL_HEADERS,
51                   self.BUILD_DEPS]
52     cros_test_lib.VerifyTarball(tarball, dir_struct)
53
54
55 BOARD = 'lumpy'
56 TAR_NAME = 'test.tar.xz'
57
58
59 class OverallTest(cros_test_lib.MockTempDirTestCase):
60   """Tests for cros_generate_sysroot."""
61
62   def setUp(self):
63     self.cg_mock = self.StartPatcher(CrosGenMock())
64
65   def testTarballGeneration(self):
66     """End-to-end test of tarball generation."""
67     with mock.patch.object(cros_build_lib, 'IsInsideChroot'):
68       cros_build_lib.IsInsideChroot.returnvalue = True
69       cros_gen.main(
70           ['--board', BOARD, '--out-dir', self.tempdir,
71            '--out-file', TAR_NAME, '--package', constants.CHROME_CP])
72       self.cg_mock.VerifyTarball(os.path.join(self.tempdir, TAR_NAME))
73
74
75 class InterfaceTest(cros_test_lib.TempDirTestCase):
76   """Test Parsing and error checking functionality."""
77
78   BAD_TARGET_DIR = '/path/to/nowhere'
79
80   def _Parse(self, extra_args):
81     return cros_gen.ParseCommandLine(
82         ['--board', BOARD, '--out-dir', self.tempdir,
83          '--package', constants.CHROME_CP] + extra_args)
84
85   def testDefaultTargetName(self):
86     """We are getting the right default target name."""
87     options = self._Parse([])
88     self.assertEquals(
89         options.out_file, 'sysroot_chromeos-base_chromeos-chrome.tar.xz')
90
91   def testExistingTarget(self):
92     """Erroring out on pre-existing target."""
93     options = self._Parse(['--out-file', TAR_NAME])
94     osutils.Touch(os.path.join(self.tempdir, TAR_NAME))
95     self.assertRaises(cros_build_lib.DieSystemExit,
96                       cros_gen.FinishParsing, options)
97
98   def testNonExisting(self):
99     """Erroring out on non-existent output dir."""
100     options = cros_gen.ParseCommandLine(
101         ['--board', BOARD, '--out-dir', self.BAD_TARGET_DIR, '--package',
102          constants.CHROME_CP])
103     self.assertRaises(cros_build_lib.DieSystemExit,
104                       cros_gen.FinishParsing, options)
105
106
107 if __name__ == '__main__':
108   cros_test_lib.main()