• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
2# pylint: disable=missing-docstring
3
4import unittest
5
6import common
7
8from autotest_lib.server.hosts import cros_host
9from autotest_lib.server.hosts import servo_host
10
11CROSSYSTEM_RESULT = '''
12fwb_tries              = 0                              # Fake comment
13fw_vboot2              = 1                              # Fake comment
14fwid                   = Google_Reef.9933.0.0           # Fake comment
15fwupdate_tries         = 0                              #
16fw_tried               = B                              #
17fw_try_count           = 0                              #
18'''
19
20class MockCmd(object):
21    """Simple mock command with base command and results"""
22
23    def __init__(self, cmd, exit_status, stdout):
24        self.cmd = cmd
25        self.stdout = stdout
26        self.exit_status = exit_status
27
28
29class MockHost(cros_host.CrosHost):
30    """Simple host for running mock'd host commands"""
31
32    def __init__(self, *args):
33        self._mock_cmds = {c.cmd: c for c in args}
34
35    def run(self, command, **kwargs):
36        """Finds the matching result by command value"""
37        mock_cmd = self._mock_cmds[command]
38        file_out = kwargs.get('stdout_tee', None)
39        if file_out:
40            file_out.write(mock_cmd.stdout)
41        return mock_cmd
42
43
44class GetPlatformModelTests(unittest.TestCase):
45    """Unit tests for CrosHost.get_platform_model"""
46
47    def test_mosys_succeeds(self):
48        host = MockHost(MockCmd('mosys platform model', 0, 'coral\n'))
49        self.assertEqual(host.get_platform(), 'coral')
50
51    def test_mosys_fails(self):
52        host = MockHost(
53            MockCmd('mosys platform model', 1, ''),
54            MockCmd('crossystem', 0, CROSSYSTEM_RESULT))
55        self.assertEqual(host.get_platform(), 'reef')
56
57
58class DictFilteringTestCase(unittest.TestCase):
59
60    """Tests for dict filtering methods on CrosHost."""
61
62    def test_get_chameleon_arguments(self):
63        got = cros_host.CrosHost.get_chameleon_arguments({
64            'chameleon_host': 'host',
65            'spam': 'eggs',
66        })
67        self.assertEqual(got, {'chameleon_host': 'host'})
68
69    def test_get_plankton_arguments(self):
70        got = cros_host.CrosHost.get_plankton_arguments({
71            'plankton_host': 'host',
72            'spam': 'eggs',
73        })
74        self.assertEqual(got, {'plankton_host': 'host'})
75
76    def test_get_servo_arguments(self):
77        got = cros_host.CrosHost.get_servo_arguments({
78            servo_host.SERVO_HOST_ATTR: 'host',
79            'spam': 'eggs',
80        })
81        self.assertEqual(got, {servo_host.SERVO_HOST_ATTR: 'host'})
82
83
84if __name__ == "__main__":
85    unittest.main()
86