File: itest_utils.py

package info (click to toggle)
python-marathon 0.13.0-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 460 kB
  • sloc: python: 1,969; makefile: 185; sh: 58
file content (58 lines) | stat: -rw-r--r-- 1,459 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import errno
from functools import wraps
import os
import signal
import time

import requests


class TimeoutError(Exception):
    pass


def timeout(seconds=10, error_message=os.strerror(errno.ETIME)):
    def decorator(func):
        def _handle_timeout(signum, frame):
            raise TimeoutError(error_message)

        def wrapper(*args, **kwargs):
            signal.signal(signal.SIGALRM, _handle_timeout)
            signal.alarm(seconds)
            try:
                result = func(*args, **kwargs)
            finally:
                signal.alarm(0)
            return result

        return wraps(func)(wrapper)

    return decorator


@timeout(30)
def wait_for_marathon():
    """Blocks until marathon is up"""
    marathon_service = get_marathon_connection_string()
    while True:
        print('Connecting to marathon on %s' % marathon_service)
        try:
            response = requests.get(
                'http://%s/ping' % marathon_service, timeout=2)
        except (
            requests.exceptions.ConnectionError,
            requests.exceptions.Timeout,
        ):
            time.sleep(2)
            continue
        if response.status_code == 200:
            print("Marathon is up and running!")
            break


def get_marathon_connection_string():
    # only reliable way I can detect travis..
    if '/travis/' in os.environ.get('PATH'):
        return 'localhost:8080'
    else:
        return "localhost:18080"