blob: a14fe951ae9dc5e42f171887ddd0c4320cc130cf [file] [log] [blame]
Huyen Nguyen34260ee2010-10-06 16:38:18 -07001#!/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 Nguyen8d31f982010-12-08 12:42:26 -08009from autotest_lib.client.common_lib import error, utils
Huyen Nguyen7c8db5c2011-01-13 15:11:48 -080010import logging, os, socket, subprocess, urllib2
Huyen Nguyen34260ee2010-10-06 16:38:18 -070011
Huyen Nguyen9e6c3c72010-10-13 16:13:07 -070012DEVSERVER_PORT = 8080
Huyen Nguyen34260ee2010-10-06 16:38:18 -070013
Huyen Nguyen9e6c3c72010-10-13 16:13:07 -070014CMD_TIMEOUT = 120
Huyen Nguyen34260ee2010-10-06 16:38:18 -070015
Huyen Nguyen9e6c3c72010-10-13 16:13:07 -070016CWD = os.getcwd()
17DEVSERVER_SRC = os.path.join('/home', os.environ['USER'], 'trunk',
18 'src', 'platform', 'dev')
Huyen Nguyen8d31f982010-12-08 12:42:26 -080019DEVSERVER_LOG = os.path.join(CWD, 'devserver.log')
Huyen Nguyen9e6c3c72010-10-13 16:13:07 -070020
21class AutoUpdateTester():
22
Huyen Nguyen7c8db5c2011-01-13 15:11:48 -080023 def __init__(self):
Huyen Nguyen9e6c3c72010-10-13 16:13:07 -070024 """Copy devserver source into current working directory.
25 """
Huyen Nguyenb90c8842011-01-11 12:25:35 -080026 self.devserver_url = 'http://%s:%s' % (socket.gethostname(),
27 DEVSERVER_PORT)
28
Huyen Nguyen34260ee2010-10-06 16:38:18 -070029
Huyen Nguyen9e6c3c72010-10-13 16:13:07 -070030 def is_devserver_running(self):
Huyen Nguyen9e6c3c72010-10-13 16:13:07 -070031 try:
Huyen Nguyenb90c8842011-01-11 12:25:35 -080032 resp = urllib2.urlopen(self.devserver_url)
Huyen Nguyen9e6c3c72010-10-13 16:13:07 -070033 except urllib2.URLError:
34 return False
35 if resp is None:
36 return False
37 return True
Huyen Nguyen34260ee2010-10-06 16:38:18 -070038
39
Huyen Nguyen7c8db5c2011-01-13 15:11:48 -080040 def start_devserver(self, image_path):
Huyen Nguyen9e6c3c72010-10-13 16:13:07 -070041 """Start devserver
Huyen Nguyen9e6c3c72010-10-13 16:13:07 -070042 """
Huyen Nguyen9e6c3c72010-10-13 16:13:07 -070043 if self.is_devserver_running():
44 logging.info('Devserver is already running')
45 raise error.TestFail('Please kill devserver before running test.')
Huyen Nguyen34260ee2010-10-06 16:38:18 -070046
Huyen Nguyen49285322010-12-01 17:03:28 -080047 logging.info('Starting devserver...')
Huyen Nguyen34260ee2010-10-06 16:38:18 -070048
Chris Sosaef819232012-08-21 15:08:21 -070049 opts = '--image %s' % image_path
Huyen Nguyen8d31f982010-12-08 12:42:26 -080050 cmd = 'python devserver.py %s &>%s &' % (opts, DEVSERVER_LOG)
51 logging.info('devserver cmd: %s' % cmd)
Huyen Nguyen9e6c3c72010-10-13 16:13:07 -070052
Huyen Nguyen9e6c3c72010-10-13 16:13:07 -070053 try:
Huyen Nguyen8d31f982010-12-08 12:42:26 -080054 subprocess.Popen(cmd, shell=True, cwd=DEVSERVER_SRC)
55 except OSError, e:
56 raise Exception('Could not start devserver: %s' % e.child_traceback)
Huyen Nguyen34260ee2010-10-06 16:38:18 -070057
Huyen Nguyen34260ee2010-10-06 16:38:18 -070058
Huyen Nguyen8d31f982010-12-08 12:42:26 -080059 def kill_devserver(self):
60 """Kill devserver.
Huyen Nguyen9e6c3c72010-10-13 16:13:07 -070061 """
Huyen Nguyen8d31f982010-12-08 12:42:26 -080062 logging.info('Killing devserver...')
63 pkill_cmd = 'pkill -f devserver'
64 subprocess.Popen(pkill_cmd, shell=True)
Huyen Nguyenb90c8842011-01-11 12:25:35 -080065
66
67 def get_devserver_url(self):
68 """Return devserver_url"""
69 return self.devserver_url