• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# Copyright (C) 2010 Google Inc. All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8#     * Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10#     * Redistributions in binary form must reproduce the above
11# copyright notice, this list of conditions and the following disclaimer
12# in the documentation and/or other materials provided with the
13# distribution.
14#     * Neither the name of Google Inc. nor the names of its
15# contributors may be used to endorse or promote products derived from
16# this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30"""Base class with common routines between the Apache and Lighttpd servers."""
31
32import logging
33import os
34import time
35import urllib
36
37
38class HttpServerBase(object):
39
40    def __init__(self, port_obj):
41        self._port_obj = port_obj
42
43    def wait_for_action(self, action):
44        """Repeat the action for 20 seconds or until it succeeds. Returns
45        whether it succeeded."""
46        start_time = time.time()
47        while time.time() - start_time < 20:
48            if action():
49                return True
50            time.sleep(1)
51
52        return False
53
54    def is_server_running_on_all_ports(self):
55        """Returns whether the server is running on all the desired ports."""
56        for mapping in self.mappings:
57            if 'sslcert' in mapping:
58                http_suffix = 's'
59            else:
60                http_suffix = ''
61
62            url = 'http%s://127.0.0.1:%d/' % (http_suffix, mapping['port'])
63
64            try:
65                response = urllib.urlopen(url)
66                logging.debug("Server running at %s" % url)
67            except IOError:
68                logging.debug("Server NOT running at %s" % url)
69                return False
70
71        return True
72
73    def remove_log_files(self, folder, starts_with):
74        files = os.listdir(folder)
75        for file in files:
76            if file.startswith(starts_with):
77                full_path = os.path.join(folder, file)
78                os.remove(full_path)
79