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