-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path__init__.py
More file actions
51 lines (41 loc) · 1.76 KB
/
__init__.py
File metadata and controls
51 lines (41 loc) · 1.76 KB
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
import logging
import random
import requests_mock
import time
from django.conf import settings
from django.test.runner import DiscoverRunner
def mock_port(*args, **kwargs):
return 5000
# Mock out router requests and add in some jitter
# Used for application is available in router checks
def fake_responses(request, context):
responses = [
# increasing the chance of 404
{'text': 'Not Found', 'status_code': 404},
{'text': 'Not Found', 'status_code': 404},
{'text': 'Not Found', 'status_code': 404},
{'text': 'Not Found', 'status_code': 404},
{'text': 'OK', 'status_code': 200},
{'text': 'Gateway timeout', 'status_code': 504},
{'text': 'Bad gateway', 'status_code': 502},
]
random.shuffle(responses)
response = responses.pop()
context.status_code = response['status_code']
context.reason = response['text']
# Random float x, 1.0 <= x < 4.0 for some sleep jitter
time.sleep(random.uniform(1, 4))
return response['text']
url = 'http://{}:{}'.format(settings.ROUTER_HOST, settings.ROUTER_PORT)
adapter = requests_mock.Adapter()
adapter.register_uri('GET', url + '/', text=fake_responses)
adapter.register_uri('GET', url + '/health', text=fake_responses)
adapter.register_uri('GET', url + '/healthz', text=fake_responses)
class SilentDjangoTestSuiteRunner(DiscoverRunner):
"""Prevents api log messages from cluttering the console during tests."""
def run_tests(self, test_labels, extra_tests=None, **kwargs):
"""Run tests with all but critical log messages disabled."""
# hide any log messages less than critical
logging.disable(logging.ERROR)
return super(SilentDjangoTestSuiteRunner, self).run_tests(
test_labels, extra_tests, **kwargs)