OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 # TODO(nduca): Rewrite what some of these tests to use mocks instead of | 5 # TODO(nduca): Rewrite what some of these tests to use mocks instead of |
6 # actually talking to the device. This would improve our coverage quite | 6 # actually talking to the device. This would improve our coverage quite |
7 # a bit. | 7 # a bit. |
8 | 8 |
9 import socket | 9 import socket |
10 import tempfile | 10 import tempfile |
11 import unittest | 11 import unittest |
12 import mock | 12 import mock |
13 | 13 |
14 from telemetry.core import cros_interface | 14 from telemetry.core import cros_interface |
15 from telemetry import decorators | 15 from telemetry import decorators |
16 from telemetry.internal import forwarders | 16 from telemetry.internal import forwarders |
17 from telemetry.internal.forwarders import cros_forwarder | 17 from telemetry.internal.forwarders import cros_forwarder |
18 from telemetry.testing import options_for_unittests | 18 from telemetry.testing import options_for_unittests |
19 | 19 |
20 | 20 |
21 class CrOSInterfaceTest(unittest.TestCase): | 21 class CrOSInterfaceTest(unittest.TestCase): |
22 | 22 |
23 def _GetCRI(self): | 23 def _GetCRI(self): |
24 remote = options_for_unittests.GetCopy().cros_remote | 24 remote = options_for_unittests.GetCopy().cros_remote |
25 remote_ssh_port = options_for_unittests.GetCopy().cros_remote_ssh_port | 25 remote_ssh_port = options_for_unittests.GetCopy().cros_remote_ssh_port |
26 return cros_interface.CrOSInterface( | 26 return cros_interface.CrOSInterface( |
27 remote, remote_ssh_port, | 27 remote, remote_ssh_port, |
28 options_for_unittests.GetCopy().cros_ssh_identity) | 28 options_for_unittests.GetCopy().cros_ssh_identity) |
29 | 29 |
30 @decorators.Enabled('cros-chrome') | 30 @decorators.Enabled('chromeos') |
31 def testPushContents(self): | 31 def testPushContents(self): |
32 with self._GetCRI() as cri: | 32 with self._GetCRI() as cri: |
33 cri.RunCmdOnDevice(['rm', '-rf', '/tmp/testPushContents']) | 33 tmp_file = '/tmp/testPushContents' |
34 cri.PushContents('hello world', '/tmp/testPushContents') | 34 test_contents = 'hello world' |
35 contents = cri.GetFileContents('/tmp/testPushContents') | 35 cri.RmRF(tmp_file) |
36 self.assertEquals(contents, 'hello world') | 36 cri.PushContents(test_contents, tmp_file) |
| 37 contents = cri.GetFileContents(tmp_file) |
| 38 self.assertEquals(contents, test_contents) |
37 | 39 |
38 @decorators.Enabled('cros-chrome') | 40 @decorators.Enabled('chromeos') |
39 def testExists(self): | 41 def testExists(self): |
40 with self._GetCRI() as cri: | 42 with self._GetCRI() as cri: |
41 self.assertTrue(cri.FileExistsOnDevice('/proc/cpuinfo')) | 43 self.assertTrue(cri.FileExistsOnDevice('/proc/cpuinfo')) |
42 self.assertTrue(cri.FileExistsOnDevice('/etc/passwd')) | 44 self.assertTrue(cri.FileExistsOnDevice('/etc/passwd')) |
43 self.assertFalse(cri.FileExistsOnDevice('/etc/sdlfsdjflskfjsflj')) | 45 self.assertFalse(cri.FileExistsOnDevice('/etc/sdlfsdjflskfjsflj')) |
44 | 46 |
45 @decorators.Enabled('linux') | 47 @decorators.Enabled('chromeos') |
46 def testExistsLocal(self): | 48 def testGetFileContents(self): |
47 with cros_interface.CrOSInterface() as cri: | |
48 self.assertTrue(cri.FileExistsOnDevice('/proc/cpuinfo')) | |
49 self.assertTrue(cri.FileExistsOnDevice('/etc/passwd')) | |
50 self.assertFalse(cri.FileExistsOnDevice('/etc/sdlfsdjflskfjsflj')) | |
51 | |
52 @decorators.Enabled('cros-chrome') | |
53 def testGetFileContents(self): # pylint: disable=no-self-use | |
54 with self._GetCRI() as cri: | 49 with self._GetCRI() as cri: |
55 hosts = cri.GetFileContents('/etc/lsb-release') | 50 hosts = cri.GetFileContents('/etc/lsb-release') |
56 self.assertTrue('CHROMEOS' in hosts) | 51 self.assertTrue('CHROMEOS' in hosts) |
57 | 52 |
58 @decorators.Enabled('cros-chrome') | 53 @decorators.Enabled('chromeos') |
59 def testGetFileContentsNonExistent(self): | |
60 with self._GetCRI() as cri: | |
61 f = tempfile.NamedTemporaryFile() | |
62 cri.PushContents('testGetFileNonExistent', f.name) | |
63 cri.RmRF(f.name) | |
64 self.assertRaises(OSError, lambda: cri.GetFileContents(f.name)) | |
65 | |
66 @decorators.Enabled('cros-chrome') | |
67 def testGetFile(self): # pylint: disable=no-self-use | 54 def testGetFile(self): # pylint: disable=no-self-use |
68 with self._GetCRI() as cri: | 55 with self._GetCRI() as cri: |
69 f = tempfile.NamedTemporaryFile() | 56 f = tempfile.NamedTemporaryFile() |
70 cri.GetFile('/etc/lsb-release', f.name) | 57 cri.GetFile('/etc/lsb-release', f.name) |
71 with open(f.name, 'r') as f2: | 58 with open(f.name, 'r') as f2: |
72 res = f2.read() | 59 res = f2.read() |
73 self.assertTrue('CHROMEOS' in res) | 60 self.assertTrue('CHROMEOS' in res) |
74 | 61 |
75 @decorators.Enabled('cros-chrome') | 62 @decorators.Enabled('chromeos') |
76 def testGetFileNonExistent(self): | 63 def testGetFileNonExistent(self): |
77 with self._GetCRI() as cri: | 64 with self._GetCRI() as cri: |
78 f = tempfile.NamedTemporaryFile() | 65 f = tempfile.NamedTemporaryFile() |
79 cri.PushContents('testGetFileNonExistent', f.name) | 66 cri.PushContents('testGetFileNonExistent', f.name) |
80 cri.RmRF(f.name) | 67 cri.RmRF(f.name) |
81 self.assertRaises(OSError, lambda: cri.GetFile(f.name)) | 68 self.assertRaises(OSError, lambda: cri.GetFile(f.name)) |
82 | 69 |
83 @decorators.Enabled('cros-chrome') | 70 @decorators.Enabled('chromeos') |
84 def testIsServiceRunning(self): | 71 def testIsServiceRunning(self): |
85 with self._GetCRI() as cri: | 72 with self._GetCRI() as cri: |
86 self.assertTrue(cri.IsServiceRunning('openssh-server')) | 73 self.assertTrue(cri.IsServiceRunning('openssh-server')) |
87 | 74 |
88 @decorators.Enabled('linux') | 75 # TODO(achuith): Fix this test. crbug.com/619767. |
89 def testIsServiceRunningLocal(self): | 76 @decorators.Disabled('all') |
90 with cros_interface.CrOSInterface() as cri: | |
91 self.assertTrue(cri.IsServiceRunning('dbus')) | |
92 | |
93 @decorators.Enabled('cros-chrome') | |
94 def testGetRemotePortAndIsHTTPServerRunningOnPort(self): | 77 def testGetRemotePortAndIsHTTPServerRunningOnPort(self): |
95 with self._GetCRI() as cri: | 78 with self._GetCRI() as cri: |
96 # Create local server. | 79 # Create local server. |
97 sock = socket.socket() | 80 sock = socket.socket() |
98 sock.bind(('', 0)) | 81 sock.bind(('', 0)) |
99 port = sock.getsockname()[1] | 82 port = sock.getsockname()[1] |
100 sock.listen(0) | 83 sock.listen(0) |
101 | 84 |
102 # Get remote port and ensure that it was unused. | 85 # Get remote port and ensure that it was unused. |
103 remote_port = cri.GetRemotePort() | 86 remote_port = cri.GetRemotePort() |
(...skipping 13 matching lines...) Expand all Loading... |
117 self.assertTrue(cri.GetRemotePort() != remote_port) | 100 self.assertTrue(cri.GetRemotePort() != remote_port) |
118 | 101 |
119 # Close forwarder and local server ports. | 102 # Close forwarder and local server ports. |
120 forwarder.Close() | 103 forwarder.Close() |
121 sock.close() | 104 sock.close() |
122 | 105 |
123 # Device should no longer be able to connect to remote_port since it is no | 106 # Device should no longer be able to connect to remote_port since it is no |
124 # longer in use. | 107 # longer in use. |
125 self.assertFalse(cri.IsHTTPServerRunningOnPort(remote_port)) | 108 self.assertFalse(cri.IsHTTPServerRunningOnPort(remote_port)) |
126 | 109 |
127 @decorators.Enabled('cros-chrome') | 110 @decorators.Enabled('chromeos') |
128 def testGetRemotePortReservedPorts(self): | 111 def testGetRemotePortReservedPorts(self): |
129 with self._GetCRI() as cri: | 112 with self._GetCRI() as cri: |
130 # Should return 2 separate ports even though the first one isn't | 113 # Should return 2 separate ports even though the first one isn't |
131 # technically being used yet. | 114 # technically being used yet. |
132 remote_port_1 = cri.GetRemotePort() | 115 remote_port_1 = cri.GetRemotePort() |
133 remote_port_2 = cri.GetRemotePort() | 116 remote_port_2 = cri.GetRemotePort() |
134 | 117 |
135 self.assertTrue(remote_port_1 != remote_port_2) | 118 self.assertTrue(remote_port_1 != remote_port_2) |
136 | 119 |
137 @decorators.Enabled('cros-chrome') | 120 # TODO(achuith): Doesn't work in VMs. |
| 121 @decorators.Disabled('all') |
138 def testTakeScreenshotWithPrefix(self): | 122 def testTakeScreenshotWithPrefix(self): |
139 with self._GetCRI() as cri: | 123 with self._GetCRI() as cri: |
140 | |
141 def _Cleanup(): | 124 def _Cleanup(): |
142 cri.RmRF('/var/log/screenshots/test-prefix*') | 125 cri.RmRF('/var/log/screenshots/test-prefix*') |
143 | 126 |
144 _Cleanup() | 127 _Cleanup() |
145 self.assertTrue(cri.TakeScreenshotWithPrefix('test-prefix')) | 128 self.assertTrue(cri.TakeScreenshotWithPrefix('test-prefix')) |
146 self.assertTrue(cri.FileExistsOnDevice( | 129 self.assertTrue(cri.FileExistsOnDevice( |
147 '/var/log/screenshots/test-prefix-0.png')) | 130 '/var/log/screenshots/test-prefix-0.png')) |
148 _Cleanup() | 131 _Cleanup() |
149 | 132 |
150 @decorators.Enabled('chromeos') | 133 @decorators.Enabled('chromeos') |
151 def testLsbReleaseValue(self): | 134 def testLsbReleaseValue(self): |
152 with self._GetCRI() as cri: | 135 with self._GetCRI() as cri: |
153 build_num = cri.LsbReleaseValue('CHROMEOS_RELEASE_BUILD_NUMBER', None) | 136 build_num = cri.LsbReleaseValue('CHROMEOS_RELEASE_BUILD_NUMBER', None) |
154 self.assertTrue(build_num.isdigit()) | 137 self.assertTrue(build_num.isdigit()) |
155 device_type = cri.GetDeviceTypeName() | 138 device_type = cri.GetDeviceTypeName() |
156 self.assertTrue(device_type.isalpha()) | 139 self.assertTrue(device_type.isalpha()) |
157 | 140 |
158 # TODO(tengs): It would be best if we can filter this test and other tests | 141 @decorators.Enabled('chromeos') |
159 # that need to be run locally based on the platform of the system browser. | |
160 @decorators.Enabled('linux') | |
161 def testEscapeCmdArguments(self): | 142 def testEscapeCmdArguments(self): |
162 """Commands and their arguments that are executed through the cros | 143 """Commands and their arguments that are executed through the cros |
163 interface should follow bash syntax. This test needs to run on remotely | 144 interface should follow bash syntax. This test needs to run on remotely |
164 and locally on the device to check for consistency. | 145 and locally on the device to check for consistency. |
165 """ | 146 """ |
166 options = options_for_unittests.GetCopy() | 147 options = options_for_unittests.GetCopy() |
167 with cros_interface.CrOSInterface(options.cros_remote, | 148 with cros_interface.CrOSInterface(options.cros_remote, |
168 options.cros_remote_ssh_port, | 149 options.cros_remote_ssh_port, |
169 options.cros_ssh_identity) as cri: | 150 options.cros_ssh_identity) as cri: |
170 | 151 |
171 # Check arguments with no special characters | 152 # Check arguments with no special characters |
172 stdout, _ = cri.RunCmdOnDevice(['echo', '--arg1=value1', '--arg2=value2', | 153 stdout, _ = cri.RunCmdOnDevice(['echo', '--arg1=value1', '--arg2=value2', |
173 '--arg3="value3"']) | 154 '--arg3="value3"']) |
174 assert stdout.strip() == '--arg1=value1 --arg2=value2 --arg3=value3' | 155 assert stdout.strip() == '--arg1=value1 --arg2=value2 --arg3=value3' |
175 | 156 |
176 # Check argument with special characters escaped | 157 # Check argument with special characters escaped |
177 stdout, _ = cri.RunCmdOnDevice(['echo', '--arg=A\\; echo \\"B\\"']) | 158 stdout, _ = cri.RunCmdOnDevice(['echo', '--arg=A\\; echo \\"B\\"']) |
178 assert stdout.strip() == '--arg=A; echo "B"' | 159 assert stdout.strip() == '--arg=A; echo "B"' |
179 | 160 |
180 # Check argument with special characters in quotes | 161 # Check argument with special characters in quotes |
181 stdout, _ = cri.RunCmdOnDevice(['echo', "--arg='$HOME;;$PATH'"]) | 162 stdout, _ = cri.RunCmdOnDevice(['echo', "--arg='$HOME;;$PATH'"]) |
182 assert stdout.strip() == "--arg=$HOME;;$PATH" | 163 assert stdout.strip() == "--arg=$HOME;;$PATH" |
183 | 164 |
184 @decorators.Enabled('cros-chrome', 'linux') | 165 @decorators.Enabled('chromeos') |
185 @mock.patch.object(cros_interface.CrOSInterface, 'RunCmdOnDevice') | 166 @mock.patch.object(cros_interface.CrOSInterface, 'RunCmdOnDevice') |
186 def testTryLoginSuccess(self, mock_run_cmd): | 167 def testTryLoginSuccess(self, mock_run_cmd): |
187 mock_run_cmd.return_value = ('root\n', '') | 168 mock_run_cmd.return_value = ('root\n', '') |
188 cri = cros_interface.CrOSInterface( | 169 cri = cros_interface.CrOSInterface( |
189 "testhostname", 22, options_for_unittests.GetCopy().cros_ssh_identity) | 170 "testhostname", 22, options_for_unittests.GetCopy().cros_ssh_identity) |
190 cri.TryLogin() | 171 cri.TryLogin() |
191 mock_run_cmd.assert_called_once_with(['echo', '$USER'], quiet=True) | 172 mock_run_cmd.assert_called_once_with(['echo', '$USER'], quiet=True) |
192 | 173 |
193 @decorators.Enabled('cros-chrome', 'linux') | 174 @decorators.Enabled('chromeos') |
194 @mock.patch.object(cros_interface.CrOSInterface, 'RunCmdOnDevice') | 175 @mock.patch.object(cros_interface.CrOSInterface, 'RunCmdOnDevice') |
195 def testTryLoginStderr(self, mock_run_cmd): | 176 def testTryLoginStderr(self, mock_run_cmd): |
196 cri = cros_interface.CrOSInterface( | 177 cri = cros_interface.CrOSInterface( |
197 "testhostname", 22, options_for_unittests.GetCopy().cros_ssh_identity) | 178 "testhostname", 22, options_for_unittests.GetCopy().cros_ssh_identity) |
198 | 179 |
199 mock_run_cmd.return_value = ('', 'Host key verification failed') | 180 mock_run_cmd.return_value = ('', 'Host key verification failed') |
200 self.assertRaises(cros_interface.LoginException, cri.TryLogin) | 181 self.assertRaises(cros_interface.LoginException, cri.TryLogin) |
201 self.assertRaisesRegexp(cros_interface.LoginException, | 182 self.assertRaisesRegexp(cros_interface.LoginException, |
202 r'.*host key verification failed..*', cri.TryLogin) | 183 r'.*host key verification failed..*', cri.TryLogin) |
203 | 184 |
(...skipping 13 matching lines...) Expand all Loading... |
217 | 198 |
218 mock_run_cmd.return_value = ('', 'Fallback error case') | 199 mock_run_cmd.return_value = ('', 'Fallback error case') |
219 self.assertRaisesRegexp(cros_interface.LoginException, | 200 self.assertRaisesRegexp(cros_interface.LoginException, |
220 r'While logging into .*, got .*', cri.TryLogin) | 201 r'While logging into .*, got .*', cri.TryLogin) |
221 | 202 |
222 mock_run_cmd.return_value = ('', 'Could not resolve hostname') | 203 mock_run_cmd.return_value = ('', 'Could not resolve hostname') |
223 self.assertRaisesRegexp(cros_interface.DNSFailureException, | 204 self.assertRaisesRegexp(cros_interface.DNSFailureException, |
224 r'Unable to resolve the hostname for:.*', | 205 r'Unable to resolve the hostname for:.*', |
225 cri.TryLogin) | 206 cri.TryLogin) |
226 | 207 |
227 @decorators.Enabled('cros-chrome', 'linux') | 208 @decorators.Enabled('chromeos') |
228 @mock.patch.object(cros_interface.CrOSInterface, 'RunCmdOnDevice') | 209 @mock.patch.object(cros_interface.CrOSInterface, 'RunCmdOnDevice') |
229 def testTryLoginStdout(self, mock_run_cmd): | 210 def testTryLoginStdout(self, mock_run_cmd): |
230 mock_run_cmd.return_value = ('notrooot', '') | 211 mock_run_cmd.return_value = ('notrooot', '') |
231 cri = cros_interface.CrOSInterface( | 212 cri = cros_interface.CrOSInterface( |
232 "testhostname", 22, options_for_unittests.GetCopy().cros_ssh_identity) | 213 "testhostname", 22, options_for_unittests.GetCopy().cros_ssh_identity) |
233 self.assertRaisesRegexp(cros_interface.LoginException, | 214 self.assertRaisesRegexp(cros_interface.LoginException, |
234 r'Logged into .*, expected \$USER=root, but got .*', | 215 r'Logged into .*, expected \$USER=root, but got .*', |
235 cri.TryLogin) | 216 cri.TryLogin) |
OLD | NEW |