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

Side by Side Diff: client/cros/login.py

Issue 5740004: Move site_chrome_test, site_logging, site_log_reader, site_crash_test into cros dir. (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/autotest.git@master
Patch Set: patch Created 10 years 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
OLDNEW
1 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. 1 # Copyright (c) 2010 The Chromium OS 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 import errno, logging, os, re, signal, subprocess, time 5 import errno, logging, os, re, signal, subprocess, time
6 import common 6 import common
7 import constants as chromeos_constants, cryptohome, ui 7 import constants as chromeos_constants, cryptohome, log_reader, ui
8 from autotest_lib.client.bin import test, utils, site_log_reader 8 from autotest_lib.client.bin import utils
9 from autotest_lib.client.common_lib import error 9 from autotest_lib.client.common_lib import error
10 10
11 11
12 _DEFAULT_TIMEOUT = 30 12 _DEFAULT_TIMEOUT = 30
13 13
14 14
15 class TimeoutError(error.TestError): 15 class TimeoutError(error.TestError):
16 """Error raised when we time out while waiting on a condition.""" 16 """Error raised when we time out while waiting on a condition."""
17 pass 17 pass
18 18
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 83
84 return False 84 return False
85 85
86 86
87 def logged_in(): 87 def logged_in():
88 # this file is created when the session_manager emits start-user-session 88 # this file is created when the session_manager emits start-user-session
89 # and removed when the session_manager emits stop-user-session 89 # and removed when the session_manager emits stop-user-session
90 return os.path.exists(chromeos_constants.LOGGED_IN_MAGIC_FILE) 90 return os.path.exists(chromeos_constants.LOGGED_IN_MAGIC_FILE)
91 91
92 92
93 def process_crashed(process, log_reader): 93 def process_crashed(process, logreader):
kmixter1 2010/12/16 03:12:57 suggest renaming class and using the standard _ se
ericli 2010/12/17 21:48:39 With the new naming, now it makes more sense. Done
94 """Checks the log watched by |log_reader| to see if a crash was reported 94 """Checks the log watched by |logreader| to see if a crash was reported
95 for |process|. 95 for |process|.
96 96
97 Returns True if so, False if not. 97 Returns True if so, False if not.
98 """ 98 """
99 return log_reader.can_find('Received crash notification for %s' % process) 99 return logreader.can_find('Received crash notification for %s' % process)
100 100
101 101
102 def wait_for_condition(condition, timeout_msg, timeout, process, log_reader, 102 def wait_for_condition(condition, timeout_msg, timeout, process, logreader,
103 crash_msg): 103 crash_msg):
104 try: 104 try:
105 utils.poll_for_condition( 105 utils.poll_for_condition(
106 condition, 106 condition,
107 TimeoutError(timeout_msg), 107 TimeoutError(timeout_msg),
108 timeout=timeout) 108 timeout=timeout)
109 except TimeoutError, e: 109 except TimeoutError, e:
110 # We could fail faster if necessary, but it'd be more complicated. 110 # We could fail faster if necessary, but it'd be more complicated.
111 if process_crashed(process, log_reader): 111 if process_crashed(process, logreader):
112 raise CrashError(crash_msg) 112 raise CrashError(crash_msg)
113 else: 113 else:
114 raise e 114 raise e
115 115
116 116
117 def attempt_login(username, password, timeout=_DEFAULT_TIMEOUT): 117 def attempt_login(username, password, timeout=_DEFAULT_TIMEOUT):
118 """Attempt to log in. 118 """Attempt to log in.
119 119
120 Args: 120 Args:
121 username: str username for login 121 username: str username for login
122 password: str password for login 122 password: str password for login
123 timeout: float number of seconds to wait 123 timeout: float number of seconds to wait
124 124
125 Raises: 125 Raises:
126 TimeoutError: login didn't complete before timeout 126 TimeoutError: login didn't complete before timeout
127 UnexpectedCondition: login manager is not running, or user is already 127 UnexpectedCondition: login manager is not running, or user is already
128 logged in. 128 logged in.
129 """ 129 """
130 logging.info("Attempting to login using autox.py and (%s, %s)" % 130 logging.info("Attempting to login using autox.py and (%s, %s)" %
131 (username, password)) 131 (username, password))
132 132
133 if not __get_session_manager_pid(): 133 if not __get_session_manager_pid():
134 raise UnexpectedCondition("Session manager is not running") 134 raise UnexpectedCondition("Session manager is not running")
135 135
136 if logged_in(): 136 if logged_in():
137 raise UnexpectedCondition("Already logged in") 137 raise UnexpectedCondition("Already logged in")
138 138
139 # Mark /var/log/messages now; we'll run through all subsequent log messages 139 # Mark /var/log/messages now; we'll run through all subsequent log messages
140 # if we couldn't log in to see if the browser crashed. 140 # if we couldn't log in to see if the browser crashed.
141 log_reader = site_log_reader.LogReader() 141 my_log_reader = log_reader.LogReader()
142 log_reader.set_start_by_current() 142 my_log_reader.set_start_by_current()
143 143
144 ax = ui.get_autox() 144 ax = ui.get_autox()
145 # navigate to login screen 145 # navigate to login screen
146 ax.send_hotkey("Ctrl+Alt+L") 146 ax.send_hotkey("Ctrl+Alt+L")
147 # escape out of any login screen menus (e.g., the network selection menu) 147 # escape out of any login screen menus (e.g., the network selection menu)
148 ax.send_hotkey("Escape") 148 ax.send_hotkey("Escape")
149 time.sleep(0.5) 149 time.sleep(0.5)
150 if (username): 150 if (username):
151 # focus username 151 # focus username
152 ax.send_hotkey("Alt+U") 152 ax.send_hotkey("Alt+U")
153 ax.send_text(username) 153 ax.send_text(username)
154 # TODO(rginda): remove Tab after http://codereview.chromium.org/1390003 154 # TODO(rginda): remove Tab after http://codereview.chromium.org/1390003
155 ax.send_hotkey("Tab") 155 ax.send_hotkey("Tab")
156 # focus password 156 # focus password
157 ax.send_hotkey("Alt+P") 157 ax.send_hotkey("Alt+P")
158 ax.send_text(password) 158 ax.send_text(password)
159 ax.send_hotkey("Return") 159 ax.send_hotkey("Return")
160 else: 160 else:
161 ax.send_hotkey("Alt+B") # Browse without signing-in 161 ax.send_hotkey("Alt+B") # Browse without signing-in
162 162
163 wait_for_condition(condition=logged_in, 163 wait_for_condition(condition=logged_in,
164 timeout_msg='Timed out waiting for login', 164 timeout_msg='Timed out waiting for login',
165 timeout=timeout, 165 timeout=timeout,
166 process='chrome', 166 process='chrome',
167 log_reader=log_reader, 167 logreader=my_log_reader,
168 crash_msg='Chrome crashed during login') 168 crash_msg='Chrome crashed during login')
169 169
170 170
171 def attempt_logout(timeout=_DEFAULT_TIMEOUT): 171 def attempt_logout(timeout=_DEFAULT_TIMEOUT):
172 """Attempt to log out by killing Chrome. 172 """Attempt to log out by killing Chrome.
173 173
174 Args: 174 Args:
175 timeout: float number of seconds to wait 175 timeout: float number of seconds to wait
176 176
177 Raises: 177 Raises:
178 TimeoutError: logout didn't complete before timeout 178 TimeoutError: logout didn't complete before timeout
179 UnexpectedCondition: user is not logged in 179 UnexpectedCondition: user is not logged in
180 """ 180 """
181 if not logged_in(): 181 if not logged_in():
182 raise UnexpectedCondition('Already logged out') 182 raise UnexpectedCondition('Already logged out')
183 183
184 oldpid = __get_session_manager_pid() 184 oldpid = __get_session_manager_pid()
185 185
186 # Mark /var/log/messages now; we'll run through all subsequent log messages 186 # Mark /var/log/messages now; we'll run through all subsequent log messages
187 # if we couldn't TERM and restart the session manager. 187 # if we couldn't TERM and restart the session manager.
188 log_reader = site_log_reader.LogReader() 188 my_log_reader = log_reader.LogReader()
189 log_reader.set_start_by_current() 189 my_log_reader.set_start_by_current()
190 190
191 # Gracefully exiting the session manager causes the user's session to end. 191 # Gracefully exiting the session manager causes the user's session to end.
192 utils.system('pkill -TERM -o ^%s$' % chromeos_constants.SESSION_MANAGER) 192 utils.system('pkill -TERM -o ^%s$' % chromeos_constants.SESSION_MANAGER)
193 193
194 wait_for_condition( 194 wait_for_condition(
195 condition=lambda: __session_manager_restarted(oldpid), 195 condition=lambda: __session_manager_restarted(oldpid),
196 timeout_msg='Timed out waiting for logout', 196 timeout_msg='Timed out waiting for logout',
197 timeout=timeout, 197 timeout=timeout,
198 process='session_manager', 198 process='session_manager',
199 log_reader=log_reader, 199 logreader=my_log_reader,
200 crash_msg='session_manager crashed while shutting down.') 200 crash_msg='session_manager crashed while shutting down.')
201 201
202 202
203 def wait_for_browser(timeout=_DEFAULT_TIMEOUT): 203 def wait_for_browser(timeout=_DEFAULT_TIMEOUT):
204 """Wait until a Chrome process is running. 204 """Wait until a Chrome process is running.
205 205
206 Args: 206 Args:
207 timeout: float number of seconds to wait 207 timeout: float number of seconds to wait
208 208
209 Raises: 209 Raises:
210 TimeoutError: Chrome didn't start before timeout 210 TimeoutError: Chrome didn't start before timeout
211 """ 211 """
212 # Mark /var/log/messages now; we'll run through all subsequent log messages 212 # Mark /var/log/messages now; we'll run through all subsequent log messages
213 # if we couldn't start chrome to see if the browser crashed. 213 # if we couldn't start chrome to see if the browser crashed.
214 log_reader = site_log_reader.LogReader() 214 my_log_reader = log_reader.LogReader()
215 log_reader.set_start_by_current() 215 my_log_reader.set_start_by_current()
216 wait_for_condition( 216 wait_for_condition(
217 lambda: os.system('pgrep ^%s$' % chromeos_constants.BROWSER) == 0, 217 lambda: os.system('pgrep ^%s$' % chromeos_constants.BROWSER) == 0,
218 timeout_msg='Timed out waiting for Chrome to start', 218 timeout_msg='Timed out waiting for Chrome to start',
219 timeout=timeout, 219 timeout=timeout,
220 process='chrome', 220 process='chrome',
221 log_reader=log_reader, 221 logreader=my_log_reader,
222 crash_msg='Chrome crashed while starting up.') 222 crash_msg='Chrome crashed while starting up.')
223 223
224 224
225 def wait_for_cryptohome(timeout=_DEFAULT_TIMEOUT): 225 def wait_for_cryptohome(timeout=_DEFAULT_TIMEOUT):
226 """Wait until cryptohome is mounted. 226 """Wait until cryptohome is mounted.
227 227
228 Args: 228 Args:
229 timeout: float number of seconds to wait 229 timeout: float number of seconds to wait
230 230
231 Raises: 231 Raises:
232 TimeoutError: cryptohome wasn't mounted before timeout 232 TimeoutError: cryptohome wasn't mounted before timeout
233 """ 233 """
234 # Mark /var/log/messages now; we'll run through all subsequent log messages 234 # Mark /var/log/messages now; we'll run through all subsequent log messages
235 # if we couldn't get the browser up to see if the browser crashed. 235 # if we couldn't get the browser up to see if the browser crashed.
236 log_reader = site_log_reader.LogReader() 236 my_log_reader = log_reader.LogReader()
237 log_reader.set_start_by_current() 237 my_log_reader.set_start_by_current()
238 wait_for_condition( 238 wait_for_condition(
239 condition=lambda: cryptohome.is_mounted(), 239 condition=lambda: cryptohome.is_mounted(),
240 timeout_msg='Timed out waiting for cryptohome to be mounted', 240 timeout_msg='Timed out waiting for cryptohome to be mounted',
241 timeout=timeout, 241 timeout=timeout,
242 process='cryptohomed', 242 process='cryptohomed',
243 log_reader=log_reader, 243 logreader=my_log_reader,
244 crash_msg='cryptohomed crashed during mount attempt') 244 crash_msg='cryptohomed crashed during mount attempt')
245 245
246 246
247 def wait_for_login_prompt(timeout=_DEFAULT_TIMEOUT): 247 def wait_for_login_prompt(timeout=_DEFAULT_TIMEOUT):
248 """Wait the login prompt is on screen and ready 248 """Wait the login prompt is on screen and ready
249 249
250 Args: 250 Args:
251 timeout: float number of seconds to wait 251 timeout: float number of seconds to wait
252 252
253 Raises: 253 Raises:
254 TimeoutError: Login prompt didn't get up before timeout 254 TimeoutError: Login prompt didn't get up before timeout
255 """ 255 """
256 # Mark /var/log/messages now; we'll run through all subsequent log messages 256 # Mark /var/log/messages now; we'll run through all subsequent log messages
257 # if we couldn't get the browser up to see if the browser crashed. 257 # if we couldn't get the browser up to see if the browser crashed.
258 log_reader = site_log_reader.LogReader() 258 my_log_reader = log_reader.LogReader()
259 log_reader.set_start_by_current() 259 my_log_reader.set_start_by_current()
260 wait_for_condition( 260 wait_for_condition(
261 condition=lambda: os.access( 261 condition=lambda: os.access(
262 chromeos_constants.LOGIN_PROMPT_READY_MAGIC_FILE, os.F_OK), 262 chromeos_constants.LOGIN_PROMPT_READY_MAGIC_FILE, os.F_OK),
263 timeout_msg='Timed out waiting for login prompt', 263 timeout_msg='Timed out waiting for login prompt',
264 timeout=timeout, 264 timeout=timeout,
265 process='chrome', 265 process='chrome',
266 log_reader=log_reader, 266 logreader=my_log_reader,
267 crash_msg='Chrome crashed before the login prompt.') 267 crash_msg='Chrome crashed before the login prompt.')
268 268
269 269
270 def wait_for_window_manager(timeout=_DEFAULT_TIMEOUT): 270 def wait_for_window_manager(timeout=_DEFAULT_TIMEOUT):
271 """Wait until the window manager is running. 271 """Wait until the window manager is running.
272 272
273 Args: 273 Args:
274 timeout: float number of seconds to wait 274 timeout: float number of seconds to wait
275 275
276 Raises: 276 Raises:
277 TimeoutError: window manager didn't start before timeout 277 TimeoutError: window manager didn't start before timeout
278 """ 278 """
279 utils.poll_for_condition( 279 utils.poll_for_condition(
280 lambda: not os.system('pgrep ^%s$' % chromeos_constants.WINDOW_MANAGER), 280 lambda: not os.system('pgrep ^%s$' % chromeos_constants.WINDOW_MANAGER),
281 TimeoutError('Timed out waiting for window manager to start'), 281 TimeoutError('Timed out waiting for window manager to start'),
282 timeout=timeout) 282 timeout=timeout)
283 283
284 284
285 def wait_for_initial_chrome_window(timeout=_DEFAULT_TIMEOUT): 285 def wait_for_initial_chrome_window(timeout=_DEFAULT_TIMEOUT):
286 """Wait until the initial Chrome window is mapped. 286 """Wait until the initial Chrome window is mapped.
287 287
288 Args: 288 Args:
289 timeout: float number of seconds to wait 289 timeout: float number of seconds to wait
290 290
291 Raises: 291 Raises:
292 TimeoutError: Chrome window wasn't mapped before timeout 292 TimeoutError: Chrome window wasn't mapped before timeout
293 """ 293 """
294 # Mark /var/log/messages now; we'll run through all subsequent log messages 294 # Mark /var/log/messages now; we'll run through all subsequent log messages
295 # if we couldn't get the browser up to see if the browser crashed. 295 # if we couldn't get the browser up to see if the browser crashed.
296 log_reader = site_log_reader.LogReader() 296 my_log_reader = log_reader.LogReader()
297 log_reader.set_start_by_current() 297 my_log_reader.set_start_by_current()
298 wait_for_condition( 298 wait_for_condition(
299 lambda: os.access( 299 lambda: os.access(
300 chromeos_constants.CHROME_WINDOW_MAPPED_MAGIC_FILE, os.F_OK), 300 chromeos_constants.CHROME_WINDOW_MAPPED_MAGIC_FILE, os.F_OK),
301 'Timed out waiting for initial Chrome window', 301 'Timed out waiting for initial Chrome window',
302 timeout=timeout, 302 timeout=timeout,
303 process='chrome', 303 process='chrome',
304 log_reader=log_reader, 304 logreader=my_log_reader,
305 crash_msg='Chrome crashed before first tab rendered.') 305 crash_msg='Chrome crashed before first tab rendered.')
306 306
307 307
308 def nuke_login_manager(): 308 def nuke_login_manager():
309 nuke_process_by_name('session_manager') 309 nuke_process_by_name('session_manager')
310 wait_for_browser() 310 wait_for_browser()
311 311
312 312
313 def nuke_process_by_name(name, with_prejudice=False): 313 def nuke_process_by_name(name, with_prejudice=False):
314 pid = int(utils.system_output('pgrep -o ^%s$' % name).split()[0]) 314 pid = int(utils.system_output('pgrep -o ^%s$' % name).split()[0])
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 except (IOError, OSError) as error: 367 except (IOError, OSError) as error:
368 logging.error(error) 368 logging.error(error)
369 369
370 # Restart the UI. 370 # Restart the UI.
371 nuke_login_manager() 371 nuke_login_manager()
372 utils.poll_for_condition( 372 utils.poll_for_condition(
373 lambda: __session_manager_restarted(oldpid), 373 lambda: __session_manager_restarted(oldpid),
374 TimeoutError('Timed out waiting for logout'), 374 TimeoutError('Timed out waiting for logout'),
375 timeout) 375 timeout)
376 wait_for_login_prompt() 376 wait_for_login_prompt()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698