Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
Python cleanup: make nosetests the default test runner, coverage, fla…
Browse files Browse the repository at this point in the history
…ke8 (#417)

* Upstream seems to prefer nosetests

* Make requirements-dev.txt include requirements.txt

* Report coverage after builds

* Run flake8 after builds

* test_marathon_lb_haproxy_options: Remove unused imports

* Fix simple flake8 violations

* Fix ip_cache size setting

* flake8 now passes
  • Loading branch information
JayH5 authored and brndnmtthws committed Feb 15, 2017
1 parent 0bf3618 commit f52632d
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,8 @@ PRs are welcome, but here are a few general guidelines:
- Update/include tests for new functionality. To install dependencies and run tests:

```
pip install -r requirements.txt -r requirements-dev.txt
pytest --pep8 --cov
pip install -r requirements-dev.txt
nosetests
```
- Use the pre-commit hook to automatically generate docs:

Expand Down
8 changes: 6 additions & 2 deletions build.bash
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ else
PIPCMD='pip3'
fi

$PIPCMD install -r requirements.txt -r requirements-dev.txt nose
$PIPCMD install -r requirements-dev.txt

PYTHONPATH=. nosetests
PYTHONPATH=. coverage run "$(which nosetests)"

coverage report

flake8 .
1 change: 1 addition & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,7 @@ def __init__(self, name, func, description, perServicePort=True):
self.func = func
self.description = description


labels = []
labels.append(Label(name='AUTH',
func=set_auth,
Expand Down
1 change: 1 addition & 0 deletions haproxy_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def wait_on_haproxy_pipe(pipefd):
return False
return True


pipefd = create_haproxy_pipe()

pid = os.fork()
Expand Down
8 changes: 4 additions & 4 deletions marathon_lb.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,13 @@

import dateutil.parser
import requests
from six.moves.urllib import parse

from common import (get_marathon_auth_params, set_logging_args,
set_marathon_auth_args, setup_logging)
from config import ConfigTemplater, label_keys
from lrucache import LRUCache
from utils import (CurlHttpEventStream, get_task_ip_and_ports,
ServicePortAssigner, set_ip_cache)
from utils import (CurlHttpEventStream, get_task_ip_and_ports, ip_cache,
ServicePortAssigner)


logger = logging.getLogger('marathon_lb')
Expand Down Expand Up @@ -1213,6 +1212,7 @@ def get_health_check(app, portIndex):
return check
return None


healthCheckResultCache = LRUCache()


Expand Down Expand Up @@ -1719,7 +1719,7 @@ def process_sse_events(marathon, processor):
# initialize health check LRU cache
if args.health_check:
healthCheckResultCache = LRUCache(args.lru_cache_capacity)
set_ip_cache(LRUCache(args.lru_cache_capacity))
ip_cache.set(LRUCache(args.lru_cache_capacity))

# Marathon API connector
marathon = Marathon(args.marathon,
Expand Down
7 changes: 4 additions & 3 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-r requirements.txt
coverage
flake8
mock
pytest
pytest-cov
pytest-pep8
nose
4 changes: 0 additions & 4 deletions tests/test_marathon_lb_haproxy_options.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import copy
import json
import unittest
import os

import marathon_lb
from tests.test_marathon_lb import TestMarathonUpdateHaproxy


Expand Down
23 changes: 17 additions & 6 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def iter_lines(self):

@staticmethod
def _split_lines_from_chunks(chunks):
#same behaviour as requests' Response.iter_lines(...)
# same behaviour as requests' Response.iter_lines(...)

pending = None
for chunk in chunks:
Expand All @@ -243,22 +243,33 @@ def _split_lines_from_chunks(chunks):


def resolve_ip(host):
cached_ip = ip_cache.get(host, None)
cached_ip = ip_cache.get().get(host, None)
if cached_ip:
return cached_ip
else:
try:
logger.debug("trying to resolve ip address for host %s", host)
ip = socket.gethostbyname(host)
ip_cache.set(host, ip)
ip_cache.get().set(host, ip)
return ip
except socket.gaierror:
return None
ip_cache = LRUCache()


def set_ip_cache(new_ip_cache):
ip_cache = new_ip_cache
class LRUCacheSingleton(object):
def __init__(self):
self.lru_cache = None

def get(self):
if self.lru_cache is None:
self.lru_cache = LRUCache()
return self.lru_cache

def set(self, lru_cache):
self.lru_cache = lru_cache


ip_cache = LRUCacheSingleton()


def is_ip_per_task(app):
Expand Down

0 comments on commit f52632d

Please sign in to comment.