OLD | NEW |
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 logging, os, utils, signal, time | 5 import logging, os, utils, signal, time |
6 from autotest_lib.client.bin import chromeos_constants, site_cryptohome, test | 6 from autotest_lib.client.bin import chromeos_constants, site_cryptohome |
| 7 from autotest_lib.client.bin import site_utils, test |
7 from autotest_lib.client.common_lib import error, site_ui | 8 from autotest_lib.client.common_lib import error, site_ui |
8 | 9 |
9 | 10 |
| 11 class TimeoutError(error.TestError): |
| 12 """Error returned if we time out while waiting on a condition.""" |
| 13 pass |
| 14 |
| 15 |
10 def setup_autox(test): | 16 def setup_autox(test): |
11 test.job.setup_dep(['autox']) | 17 test.job.setup_dep(['autox']) |
12 # create a empty srcdir to prevent the error that checks .version file | 18 # create a empty srcdir to prevent the error that checks .version file |
13 if not os.path.exists(test.srcdir): | 19 if not os.path.exists(test.srcdir): |
14 os.mkdir(test.srcdir) | 20 os.mkdir(test.srcdir) |
15 | 21 |
16 | 22 |
17 def logged_in(): | 23 def logged_in(): |
18 # this file is created when the session_manager emits start-user-session | 24 # this file is created when the session_manager emits start-user-session |
19 # and removed when the session_manager emits stop-user-session | 25 # and removed when the session_manager emits stop-user-session |
20 return os.path.exists(chromeos_constants.LOGGED_IN_MAGIC_FILE) | 26 return os.path.exists(chromeos_constants.LOGGED_IN_MAGIC_FILE) |
21 | 27 |
22 | 28 |
| 29 # TODO: Update this to use the Python-based autox instead. |
23 def attempt_login(test, script_file, timeout=10): | 30 def attempt_login(test, script_file, timeout=10): |
| 31 """Attempt to log in. |
| 32 |
| 33 Args: |
| 34 script: str filename of autox JSON script |
| 35 timeout: float number of seconds to wait |
| 36 |
| 37 Raises: |
| 38 error.TestFail: autox program exited with failure |
| 39 TimeoutError: login didn't complete before timeout |
| 40 """ |
24 dep = 'autox' | 41 dep = 'autox' |
25 dep_dir = os.path.join(test.autodir, 'deps', dep) | 42 dep_dir = os.path.join(test.autodir, 'deps', dep) |
26 test.job.install_pkg(dep, 'dep', dep_dir) | 43 test.job.install_pkg(dep, 'dep', dep_dir) |
27 | 44 |
28 autox_binary = '%s/%s' % (dep_dir, 'autox') | 45 autox_binary = '%s/%s' % (dep_dir, 'autox') |
29 autox_script = os.path.join(test.job.configdir, script_file) | 46 autox_script = os.path.join(test.job.configdir, script_file) |
30 | 47 |
| 48 # TODO: Use something more robust that checks whether the login window is |
| 49 # mapped. |
| 50 wait_for_browser() |
31 try: | 51 try: |
32 utils.system(site_ui.xcommand('%s %s' % (autox_binary, autox_script))) | 52 utils.system(site_ui.xcommand('%s %s' % (autox_binary, autox_script))) |
33 except error.CmdError, e: | 53 except error.CmdError, e: |
34 logging.debug(e) | 54 logging.debug(e) |
35 raise error.TestFail('AutoX program failed to login for test user') | 55 raise error.TestFail('AutoX program failed to login for test user') |
36 | 56 |
37 start_time = time.time() | 57 site_utils.poll_for_condition( |
38 while time.time() - start_time < timeout: | 58 lambda: logged_in(), |
39 if logged_in(): | 59 TimeoutError('Timed out while waiting to be logged in'), |
40 break | 60 timeout=timeout) |
41 time.sleep(1) | |
42 else: | |
43 return False | |
44 return True | |
45 | 61 |
46 | 62 |
47 def attempt_logout(timeout=10): | 63 def attempt_logout(timeout=10): |
| 64 """Attempt to log out by killing Chrome. |
| 65 |
| 66 Args: |
| 67 timeout: float number of seconds to wait |
| 68 |
| 69 Raises: |
| 70 TimeoutError: logout didn't complete before timeout |
| 71 """ |
48 # Gracefully exiting chrome causes the user's session to end. | 72 # Gracefully exiting chrome causes the user's session to end. |
| 73 wait_for_initial_chrome_window() |
49 utils.system('pkill -TERM -o ^%s$' % chromeos_constants.BROWSER) | 74 utils.system('pkill -TERM -o ^%s$' % chromeos_constants.BROWSER) |
50 start_time = time.time() | 75 site_utils.poll_for_condition( |
51 while time.time() - start_time < timeout: | 76 lambda: not logged_in(), |
52 if not logged_in(): | 77 TimeoutError('Timed out while waiting for logout'), |
53 break | 78 timeout=timeout) |
54 time.sleep(1) | |
55 else: | |
56 return False | |
57 return True | |
58 | 79 |
59 | 80 |
60 def wait_for_browser(timeout=10): | 81 def wait_for_browser(timeout=10): |
61 # Wait until the login manager is back up before trying to use it. | 82 """Wait until a Chrome process is running. |
62 # I don't use utils.system here because I don't want to fail | 83 |
63 # if pgrep returns non-zero, I just want to wait and try again. | 84 Args: |
64 start_time = time.time() | 85 timeout: float number of seconds to wait |
65 while time.time() - start_time < timeout: | 86 |
66 # 0 is returned on success. | 87 Raises: |
67 if os.system('pgrep ^%s$' % chromeos_constants.BROWSER) == 0: | 88 TimeoutError: Chrome didn't start before timeout |
68 break; | 89 """ |
69 time.sleep(1) | 90 site_utils.poll_for_condition( |
70 else: | 91 lambda: os.system('pgrep ^%s$' % chromeos_constants.BROWSER) == 0, |
71 return False | 92 TimeoutError('Timed out waiting for Chrome to start'), |
72 return True | 93 timeout=timeout) |
73 | 94 |
74 | 95 |
75 def wait_for_cryptohome(timeout=10): | 96 def wait_for_cryptohome(timeout=10): |
76 start_time = time.time() | 97 """Wait until cryptohome is mounted. |
77 while time.time() - start_time < timeout: | 98 |
78 if site_cryptohome.is_mounted(): | 99 Args: |
79 break; | 100 timeout: float number of seconds to wait |
80 time.sleep(1) | 101 |
81 else: | 102 Raises: |
82 return False | 103 TimeoutError: cryptohome wasn't mounted before timeout |
83 return True | 104 """ |
| 105 site_utils.poll_for_condition( |
| 106 lambda: site_cryptohome.is_mounted(), |
| 107 TimeoutError('Timed out waiting for cryptohome to be mounted'), |
| 108 timeout=timeout) |
84 | 109 |
85 | 110 |
86 def wait_for_screensaver(timeout=10, raise_error=True): | 111 def wait_for_screensaver(timeout=10): |
87 # Wait until the screensaver starts | 112 """Wait until xscreensaver is responding. |
88 start_time = time.time() | |
89 while time.time() - start_time < timeout: | |
90 if 0 == os.system(site_ui.xcommand('xscreensaver-command -version')): | |
91 break | |
92 time.sleep(1) | |
93 else: | |
94 if raise_error: | |
95 raise error.TestFail('Unable to communicate with ' + | |
96 'xscreensaver after %i seconds' % | |
97 time.time() - start_time) | |
98 return False | |
99 | 113 |
100 return True | 114 Args: |
| 115 timeout: float number of seconds to wait |
| 116 |
| 117 Raises: |
| 118 TimeoutError: xscreensaver didn't respond before timeout |
| 119 """ |
| 120 site_utils.poll_for_condition( |
| 121 lambda: os.system( |
| 122 site_ui.xcommand('xscreensaver-command -version')) == 0, |
| 123 TimeoutError('Timed out waiting for xscreensaver to respond'), |
| 124 timeout=timeout) |
101 | 125 |
102 | 126 |
103 def wait_for_window_manager(timeout=20): | 127 def wait_for_window_manager(timeout=20): |
104 """Wait until the window manager is running.""" | 128 """Wait until the window manager is running. |
105 start_time = time.time() | 129 |
106 while time.time() - start_time < timeout: | 130 Args: |
107 if os.system('pgrep ^%s$' % chromeos_constants.WINDOW_MANAGER) == 0: | 131 timeout: float number of seconds to wait |
108 return True | 132 |
109 time.sleep(0.1) | 133 Raises: |
110 return False | 134 TimeoutError: window manager didn't start before timeout |
| 135 """ |
| 136 site_utils.poll_for_condition( |
| 137 lambda: not os.system('pgrep ^%s$' % chromeos_constants.WINDOW_MANAGER), |
| 138 TimeoutError('Timed out waiting for window manager to start'), |
| 139 timeout=timeout) |
| 140 |
| 141 |
| 142 def wait_for_initial_chrome_window(timeout=20): |
| 143 """Wait until the initial Chrome window is mapped. |
| 144 |
| 145 Args: |
| 146 timeout: float number of seconds to wait |
| 147 |
| 148 Raises: |
| 149 TimeoutError: Chrome window wasn't mapped before timeout |
| 150 """ |
| 151 site_utils.poll_for_condition( |
| 152 lambda: os.access( |
| 153 chromeos_constants.CHROME_WINDOW_MAPPED_MAGIC_FILE, os.F_OK), |
| 154 TimeoutError('Timed out waiting for initial Chrome window'), |
| 155 timeout=timeout) |
111 | 156 |
112 | 157 |
113 def nuke_login_manager(): | 158 def nuke_login_manager(): |
114 nuke_process_by_name('session_manager') | 159 nuke_process_by_name('session_manager') |
115 wait_for_browser() | 160 wait_for_browser() |
116 | 161 |
117 | 162 |
118 def nuke_process_by_name(name, with_prejudice=False): | 163 def nuke_process_by_name(name, with_prejudice=False): |
119 pid = int(utils.system_output('pgrep -o ^%s$' % name)) | 164 pid = int(utils.system_output('pgrep -o ^%s$' % name)) |
120 if with_prejudice: | 165 if with_prejudice: |
121 utils.nuke_pid(pid, [signal.SIGKILL]) | 166 utils.nuke_pid(pid, [signal.SIGKILL]) |
122 else: | 167 else: |
123 utils.nuke_pid(pid) | 168 utils.nuke_pid(pid) |
OLD | NEW |