Huyen Nguyen | 34260ee | 2010-10-06 16:38:18 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 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 |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Utilities to test the autoupdate process. |
| 7 | """ |
| 8 | |
Huyen Nguyen | 8d31f98 | 2010-12-08 12:42:26 -0800 | [diff] [blame] | 9 | from autotest_lib.client.common_lib import error, utils |
Huyen Nguyen | 7c8db5c | 2011-01-13 15:11:48 -0800 | [diff] [blame] | 10 | import logging, os, socket, subprocess, urllib2 |
Huyen Nguyen | 34260ee | 2010-10-06 16:38:18 -0700 | [diff] [blame] | 11 | |
Huyen Nguyen | 9e6c3c7 | 2010-10-13 16:13:07 -0700 | [diff] [blame] | 12 | DEVSERVER_PORT = 8080 |
Huyen Nguyen | 34260ee | 2010-10-06 16:38:18 -0700 | [diff] [blame] | 13 | |
Huyen Nguyen | 9e6c3c7 | 2010-10-13 16:13:07 -0700 | [diff] [blame] | 14 | CMD_TIMEOUT = 120 |
Huyen Nguyen | 34260ee | 2010-10-06 16:38:18 -0700 | [diff] [blame] | 15 | |
Huyen Nguyen | 9e6c3c7 | 2010-10-13 16:13:07 -0700 | [diff] [blame] | 16 | CWD = os.getcwd() |
| 17 | DEVSERVER_SRC = os.path.join('/home', os.environ['USER'], 'trunk', |
| 18 | 'src', 'platform', 'dev') |
Huyen Nguyen | 8d31f98 | 2010-12-08 12:42:26 -0800 | [diff] [blame] | 19 | DEVSERVER_LOG = os.path.join(CWD, 'devserver.log') |
Huyen Nguyen | 9e6c3c7 | 2010-10-13 16:13:07 -0700 | [diff] [blame] | 20 | |
| 21 | class AutoUpdateTester(): |
| 22 | |
Huyen Nguyen | 7c8db5c | 2011-01-13 15:11:48 -0800 | [diff] [blame] | 23 | def __init__(self): |
Huyen Nguyen | 9e6c3c7 | 2010-10-13 16:13:07 -0700 | [diff] [blame] | 24 | """Copy devserver source into current working directory. |
| 25 | """ |
Huyen Nguyen | b90c884 | 2011-01-11 12:25:35 -0800 | [diff] [blame] | 26 | self.devserver_url = 'http://%s:%s' % (socket.gethostname(), |
| 27 | DEVSERVER_PORT) |
| 28 | |
Huyen Nguyen | 34260ee | 2010-10-06 16:38:18 -0700 | [diff] [blame] | 29 | |
Huyen Nguyen | 9e6c3c7 | 2010-10-13 16:13:07 -0700 | [diff] [blame] | 30 | def is_devserver_running(self): |
Huyen Nguyen | 9e6c3c7 | 2010-10-13 16:13:07 -0700 | [diff] [blame] | 31 | try: |
Huyen Nguyen | b90c884 | 2011-01-11 12:25:35 -0800 | [diff] [blame] | 32 | resp = urllib2.urlopen(self.devserver_url) |
Huyen Nguyen | 9e6c3c7 | 2010-10-13 16:13:07 -0700 | [diff] [blame] | 33 | except urllib2.URLError: |
| 34 | return False |
| 35 | if resp is None: |
| 36 | return False |
| 37 | return True |
Huyen Nguyen | 34260ee | 2010-10-06 16:38:18 -0700 | [diff] [blame] | 38 | |
| 39 | |
Huyen Nguyen | 7c8db5c | 2011-01-13 15:11:48 -0800 | [diff] [blame] | 40 | def start_devserver(self, image_path): |
Huyen Nguyen | 9e6c3c7 | 2010-10-13 16:13:07 -0700 | [diff] [blame] | 41 | """Start devserver |
Huyen Nguyen | 9e6c3c7 | 2010-10-13 16:13:07 -0700 | [diff] [blame] | 42 | """ |
Huyen Nguyen | 9e6c3c7 | 2010-10-13 16:13:07 -0700 | [diff] [blame] | 43 | if self.is_devserver_running(): |
| 44 | logging.info('Devserver is already running') |
| 45 | raise error.TestFail('Please kill devserver before running test.') |
Huyen Nguyen | 34260ee | 2010-10-06 16:38:18 -0700 | [diff] [blame] | 46 | |
Huyen Nguyen | 4928532 | 2010-12-01 17:03:28 -0800 | [diff] [blame] | 47 | logging.info('Starting devserver...') |
Huyen Nguyen | 34260ee | 2010-10-06 16:38:18 -0700 | [diff] [blame] | 48 | |
Chris Sosa | ef81923 | 2012-08-21 15:08:21 -0700 | [diff] [blame] | 49 | opts = '--image %s' % image_path |
Huyen Nguyen | 8d31f98 | 2010-12-08 12:42:26 -0800 | [diff] [blame] | 50 | cmd = 'python devserver.py %s &>%s &' % (opts, DEVSERVER_LOG) |
| 51 | logging.info('devserver cmd: %s' % cmd) |
Huyen Nguyen | 9e6c3c7 | 2010-10-13 16:13:07 -0700 | [diff] [blame] | 52 | |
Huyen Nguyen | 9e6c3c7 | 2010-10-13 16:13:07 -0700 | [diff] [blame] | 53 | try: |
Huyen Nguyen | 8d31f98 | 2010-12-08 12:42:26 -0800 | [diff] [blame] | 54 | subprocess.Popen(cmd, shell=True, cwd=DEVSERVER_SRC) |
| 55 | except OSError, e: |
| 56 | raise Exception('Could not start devserver: %s' % e.child_traceback) |
Huyen Nguyen | 34260ee | 2010-10-06 16:38:18 -0700 | [diff] [blame] | 57 | |
Huyen Nguyen | 34260ee | 2010-10-06 16:38:18 -0700 | [diff] [blame] | 58 | |
Huyen Nguyen | 8d31f98 | 2010-12-08 12:42:26 -0800 | [diff] [blame] | 59 | def kill_devserver(self): |
| 60 | """Kill devserver. |
Huyen Nguyen | 9e6c3c7 | 2010-10-13 16:13:07 -0700 | [diff] [blame] | 61 | """ |
Huyen Nguyen | 8d31f98 | 2010-12-08 12:42:26 -0800 | [diff] [blame] | 62 | logging.info('Killing devserver...') |
| 63 | pkill_cmd = 'pkill -f devserver' |
| 64 | subprocess.Popen(pkill_cmd, shell=True) |
Huyen Nguyen | b90c884 | 2011-01-11 12:25:35 -0800 | [diff] [blame] | 65 | |
| 66 | |
| 67 | def get_devserver_url(self): |
| 68 | """Return devserver_url""" |
| 69 | return self.devserver_url |