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