| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 2 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 '''Unittest wrapper for chromeos_interface.py.''' | 6 '''Unittest wrapper for chromeos_interface.py.''' |
| 7 | 7 |
| 8 import os | 8 import os |
| 9 import subprocess | 9 import subprocess |
| 10 import sys |
| 10 import tempfile | 11 import tempfile |
| 11 import unittest | 12 import unittest |
| 12 | 13 |
| 13 import chromeos_interface | 14 import chromeos_interface |
| 14 | 15 |
| 16 def zero_recoverysw_count(vector_map): |
| 17 '''Calculate number of elements where recoverysw is zero in recovery mode''' |
| 18 zero_count = 0 |
| 19 for key, values in vector_map.iteritems(): |
| 20 if values['mainfw_type'] != 'recovery': |
| 21 continue |
| 22 if 'recoverysw_boot' in values and values['recoverysw_boot'] == '0': |
| 23 zero_count += 1 |
| 24 return zero_count |
| 25 |
| 15 # A set of crossystem output values pertinent to the tests. | 26 # A set of crossystem output values pertinent to the tests. |
| 16 CROSSYSTEM_SAMPLE = { | 27 CROSSYSTEM_SAMPLE = { |
| 17 'devsw_boot': '0', | 28 'devsw_boot': '0', |
| 18 'recoverysw_boot': '0', | 29 'recoverysw_boot': '0', |
| 19 'recovery_reason': '0', | 30 'recovery_reason': '0', |
| 20 'tried_fwb': '0', | 31 'tried_fwb': '0', |
| 21 'fwid': 'Alex.03.61.0734.0055G1.0020', | 32 'fwid': 'Alex.03.61.0734.0055G1.0020', |
| 22 'mainfw_act': 'A', | 33 'mainfw_act': 'A', |
| 23 'mainfw_type': 'normal', | 34 'mainfw_type': 'normal', |
| 24 'ecfw_act': 'RW', | 35 'ecfw_act': 'RW', |
| (...skipping 23 matching lines...) Expand all Loading... |
| 48 '''A wrapper to allow testing of ChromeOSInterface on a host.''' | 59 '''A wrapper to allow testing of ChromeOSInterface on a host.''' |
| 49 | 60 |
| 50 def __init__(self): | 61 def __init__(self): |
| 51 self.cs_values = CROSSYSTEM_SAMPLE | 62 self.cs_values = CROSSYSTEM_SAMPLE |
| 52 super(CrosIfMock, self).__init__(True) | 63 super(CrosIfMock, self).__init__(True) |
| 53 self.init() | 64 self.init() |
| 54 self.test_write_command = '' | 65 self.test_write_command = '' |
| 55 self.write_command_seen = False | 66 self.write_command_seen = False |
| 56 | 67 |
| 57 def run_shell_command(self, cmd): | 68 def run_shell_command(self, cmd): |
| 69 if cmd.startswith('crossystem') and not '=' in cmd: |
| 70 if ' ' in cmd: |
| 71 param = cmd.split()[1] |
| 72 return StdOut(self.cs_values[param]) |
| 73 else: |
| 74 return StdOut('\n'.join('%s=%s' % (x, y) for ( |
| 75 x, y) in CROSSYSTEM_SAMPLE.iteritems())) |
| 76 |
| 58 if not self.target_hosted(): | 77 if not self.target_hosted(): |
| 59 if cmd.startswith('crossystem') and not '=' in cmd: | |
| 60 if ' ' in cmd: | |
| 61 param = cmd.split()[1] | |
| 62 return StdOut(self.cs_values[param]) | |
| 63 else: | |
| 64 return StdOut('\n'.join('%s=%s' % (x, y) for ( | |
| 65 x, y) in CROSSYSTEM_SAMPLE.iteritems())) | |
| 66 | |
| 67 if cmd == 'rootdev -s': | 78 if cmd == 'rootdev -s': |
| 68 return StdOut('/dev/sda3') | 79 return StdOut('/dev/sda3') |
| 69 | 80 |
| 70 if cmd == self.test_write_command: | 81 if cmd == self.test_write_command: |
| 71 self.test_write_command = '' | 82 self.test_write_command = '' |
| 72 self.write_command_seen = True | 83 self.write_command_seen = True |
| 73 return StdOut('') | 84 return StdOut('') |
| 74 | 85 |
| 75 return super(CrosIfMock, self).run_shell_command(cmd) | 86 return super(CrosIfMock, self).run_shell_command(cmd) |
| 76 | 87 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 107 CROSSYSTEM_SAMPLE['mainfw_type'] = saved_mainfw_type | 118 CROSSYSTEM_SAMPLE['mainfw_type'] = saved_mainfw_type |
| 108 CROSSYSTEM_SAMPLE['mainfw_act'] = 'X' | 119 CROSSYSTEM_SAMPLE['mainfw_act'] = 'X' |
| 109 try: | 120 try: |
| 110 CHROS_IF.boot_state_vector() | 121 CHROS_IF.boot_state_vector() |
| 111 except chromeos_interface.ChromeOSInterfaceError, e: | 122 except chromeos_interface.ChromeOSInterfaceError, e: |
| 112 dump = '\n'.join('%s=%s' % (x, y) for ( | 123 dump = '\n'.join('%s=%s' % (x, y) for ( |
| 113 x, y) in CROSSYSTEM_SAMPLE.iteritems()) | 124 x, y) in CROSSYSTEM_SAMPLE.iteritems()) |
| 114 self.assertEqual(e[0], dump) | 125 self.assertEqual(e[0], dump) |
| 115 | 126 |
| 116 def test_crossystem_set(self): | 127 def test_crossystem_set(self): |
| 117 '''Test is appropriate crossystem command is invoked on assignments.''' | 128 '''Test if appropriate crossystem command is invoked on assignments.''' |
| 118 CHROS_IF.test_write_command = 'crossystem "xxx=zzz"' | 129 CHROS_IF.test_write_command = 'crossystem "xxx=zzz"' |
| 119 CHROS_IF.write_command_seen = False | 130 CHROS_IF.write_command_seen = False |
| 120 CHROS_IF.cs.xxx = 'zzz' | 131 CHROS_IF.cs.xxx = 'zzz' |
| 121 self.assertTrue(CHROS_IF.write_command_seen) | 132 self.assertTrue(CHROS_IF.write_command_seen) |
| 122 | 133 |
| 134 def test_run_bad_shell_command(self): |
| 135 '''Test if run_shell_command() raises exception on errors.''' |
| 136 |
| 137 saved = sys.stdout |
| 138 sys.stdout = open('/dev/null', 'w') |
| 139 try: |
| 140 CHROS_IF.run_shell_command('ls .') |
| 141 CHROS_IF.run_shell_command('nonexisting_command') |
| 142 except chromeos_interface.ChromeOSInterfaceError, e: |
| 143 self.assertEqual(e[0], 'command nonexisting_command failed') |
| 144 finally: |
| 145 sys.stdout = saved |
| 146 |
| 147 def test_mario_workwaround(self): |
| 148 '''Test if the vector map is adjusted on Mario.''' |
| 149 |
| 150 chros_if = CrosIfMock() |
| 151 zero_values_count = zero_recoverysw_count( |
| 152 chros_if.cs.VECTOR_MAPS[0]) |
| 153 self.assertNotEqual(zero_values_count, 0) |
| 154 |
| 155 CROSSYSTEM_SAMPLE['fwid'] = CROSSYSTEM_SAMPLE['fwid'].replace( |
| 156 'Alex', 'Mario') |
| 157 chros_if = CrosIfMock() |
| 158 zero_values_count = zero_recoverysw_count( |
| 159 chros_if.cs.VECTOR_MAPS[0]) |
| 160 self.assertEqual(zero_values_count, 0) |
| 123 | 161 |
| 124 if __name__ == '__main__': | 162 if __name__ == '__main__': |
| 125 unittest.main() | 163 unittest.main() |
| OLD | NEW |