Upstream version 8.36.161.0
[platform/framework/web/crosswalk.git] / src / third_party / chromite / cros / commands / cros_build_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 """This module tests the cros image command."""
8
9 import os
10 import sys
11
12 sys.path.insert(0, os.path.abspath('%s/../../../..' % __file__))
13 from chromite.cros.commands import cros_build
14 from chromite.cros.commands import init_unittest
15 from chromite.lib import cros_build_lib
16 from chromite.lib import cros_test_lib
17 from chromite.lib import parallel_unittest
18 from chromite.lib import partial_mock
19
20 # TODO(build): Finish test wrapper (http://crosbug.com/37517).
21 # Until then, this has to be after the chromite imports.
22 import mock
23
24
25 class MockBuildCommand(init_unittest.MockCommand):
26   """Mock out the build command."""
27   TARGET = 'chromite.cros.commands.cros_build.BuildCommand'
28   TARGET_CLASS = cros_build.BuildCommand
29
30   def Run(self, inst):
31     packages = cros_build.GetToolchainPackages()
32     with mock.patch.object(cros_build, 'GetToolchainPackages') as tc_mock:
33       tc_mock.return_value = packages
34       with parallel_unittest.ParallelMock():
35         init_unittest.MockCommand.Run(self, inst)
36
37
38 class BuildCommandTest(cros_test_lib.TestCase):
39   """Test class for our BuildCommand class."""
40
41   def testSuccess(self):
42     """Test that successful commands work."""
43     cmds = [['--host', 'power_manager'],
44             ['--board=foo', 'power_manager'],
45             ['--board=foo', '--debug', 'power_manager'],
46             ['--board=foo', '--no-deps', 'power_manager'],
47             ['--board=foo', '--no-chroot-update', 'power_manager']]
48     for cmd in cmds:
49       with MockBuildCommand(cmd) as build:
50         build.inst.Run()
51         if '--no-deps' not in cmd:
52           self.assertFalse(build.inst.chroot_update)
53
54   def testFailedDeps(self):
55     """Tests that failures are detected correctly."""
56     # pylint: disable=W0212
57     args = ['--board=foo', 'power_manager']
58     with MockBuildCommand(args) as build:
59       cmd = partial_mock.In('--backtrack=0')
60       build.rc_mock.AddCmdResult(cmd=cmd, returncode=1, error='error\n')
61       ex = self.assertRaises2(cros_build_lib.RunCommandError, build.inst.Run)
62       self.assertTrue(cros_build.BuildCommand._BAD_DEPEND_MSG in ex.msg)
63
64   def testGetToolchainPackages(self):
65     """Test GetToolchainPackages function without mocking."""
66     packages = cros_build.GetToolchainPackages()
67     self.assertTrue(packages)
68
69
70 if __name__ == '__main__':
71   cros_test_lib.main()