Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(186)

Side by Side Diff: test_chromeos_interface.py

Issue 6849042: Introduce crossystem wrapper in SAFT. (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/saft.git@master
Patch Set: Do not cache crossystem output. Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« chromeos_interface.py ('K') | « runtests.sh ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 subprocess 9 import subprocess
10 import tempfile
9 import unittest 11 import unittest
10 12
11 import chromeos_interface 13 import chromeos_interface
12 14
13 CHROS_IF = chromeos_interface.ChromeOSInterface(True) 15 # A set of crossystem output values pertinent to the tests.
16 CROSSYSTEM_SAMPLE = {
17 'devsw_boot': '0',
18 'recoverysw_boot': '0',
19 'recovery_reason': '0',
20 'tried_fwb': '0',
21 'fwid': 'Alex.03.61.0734.0055G1.0020',
22 'mainfw_act': 'A',
23 'mainfw_type': 'normal',
24 'ecfw_act': 'RW',
25 'fwb_tries': '0',
26 }
27
28 class StdOut(object):
29 '''
30 An object to simulate the stdout file returned by subprocess.Popen().
31
32 @text - a potentially multiline string to represent the expected output of
33 the command.
34 '''
35
36 def __init__(self, text):
37 (fd, self.tmp_file) = tempfile.mkstemp()
38 os.write(fd, text)
39 os.close(fd)
40 self.stdout = open(self.tmp_file, 'r')
41
42 def __del__(self):
43 self.stdout.close()
44 os.remove(self.tmp_file)
45
46
47 class CrosIfMock(chromeos_interface.ChromeOSInterface):
48 '''A wrapper to allow testing of ChromeOSInterface on a host.'''
49
50 def __init__(self):
51 self.cs_values = CROSSYSTEM_SAMPLE
52 super(CrosIfMock, self).__init__(True)
53 self.init()
54 self.test_write_command = ''
55 self.write_command_seen = False
56
57 def run_shell_command(self, cmd):
58 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':
68 return StdOut('/dev/sda3')
69
70 if cmd == self.test_write_command:
71 self.test_write_command = ''
72 self.write_command_seen = True
73 return StdOut('')
74
75 return super(CrosIfMock, self).run_shell_command(cmd)
76
77
78 CHROS_IF = CrosIfMock()
14 79
15 class TestShellCommands(unittest.TestCase): 80 class TestShellCommands(unittest.TestCase):
16 81
17 TEST_CMD = 'ls /etc' 82 TEST_CMD = 'ls /etc'
18 83
19 def setUp(self): 84 def setUp(self):
20 '''Cache text expected to be generated by the command.''' 85 '''Cache text expected to be generated by the command.'''
21 self.expected_text = subprocess.Popen( 86 self.expected_text = subprocess.Popen(
22 self.TEST_CMD, shell=True, 87 self.TEST_CMD, shell=True,
23 stdout=subprocess.PIPE).stdout.read().strip() 88 stdout=subprocess.PIPE).stdout.read().strip()
24 89
25 def test_run_shell_command(self): 90 def test_run_shell_command(self):
26 '''Verify that the process provides correct stdout.''' 91 '''Verify that the process provides correct stdout.'''
27 proc = CHROS_IF.run_shell_command(self.TEST_CMD) 92 proc = CHROS_IF.run_shell_command(self.TEST_CMD)
28 self.assertEqual(proc.stdout.read().strip(), self.expected_text) 93 self.assertEqual(proc.stdout.read().strip(), self.expected_text)
29 94
30 def test_run_run_command_get_output(self): 95 def test_run_run_command_get_output(self):
31 '''Verify that the command generates correct output.''' 96 '''Verify that the command generates correct output.'''
32 live_text = CHROS_IF.run_shell_command_get_output(self.TEST_CMD) 97 live_text = CHROS_IF.run_shell_command_get_output(self.TEST_CMD)
33 self.assertEqual(live_text, self.expected_text.split('\n')) 98 self.assertEqual(live_text, self.expected_text.split('\n'))
34 99
35 def test_boot_state_vector(self): 100 def test_boot_state_vector(self):
36 '''Verify the format (not the contents) of the boot vector.''' 101 '''Verify the format (not the contents) of the boot vector.'''
37 bv = CHROS_IF.boot_state_vector() 102 bv = CHROS_IF.boot_state_vector()
38 self.assertEqual(bv.count(':'), 4) 103 self.assertEqual(bv, '1:1:1:0:3')
39 self.assertEqual(len(bv), 9) 104 saved_mainfw_type = CROSSYSTEM_SAMPLE['mainfw_type']
105 del(CROSSYSTEM_SAMPLE['mainfw_type'])
106 self.assertRaises(KeyError, CHROS_IF.boot_state_vector)
107 CROSSYSTEM_SAMPLE['mainfw_type'] = saved_mainfw_type
108 CROSSYSTEM_SAMPLE['mainfw_act'] = 'X'
109 try:
110 CHROS_IF.boot_state_vector()
111 except chromeos_interface.ChromeOSInterfaceError, e:
112 dump = '\n'.join('%s=%s' % (x, y) for (
113 x, y) in CROSSYSTEM_SAMPLE.iteritems())
114 self.assertEqual(e[0], dump)
115
116 def test_crossystem_set(self):
117 '''Test is appropriate crossystem command is invoked on assignments.'''
118 CHROS_IF.test_write_command = 'crossystem "xxx=zzz"'
119 CHROS_IF.write_command_seen = False
120 CHROS_IF.cs.xxx = 'zzz'
121 self.assertTrue(CHROS_IF.write_command_seen)
40 122
41 123
42 if __name__ == '__main__': 124 if __name__ == '__main__':
43 unittest.main() 125 unittest.main()
OLDNEW
« chromeos_interface.py ('K') | « runtests.sh ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698