hobo/tests/test_health_api.py

103 lines
3.4 KiB
Python

import json
from mock import MagicMock
import pytest
import requests
import socket
from django.utils import timezone
from hobo.environment.models import Authentic, Combo
pytestmark = pytest.mark.django_db
def get_entry(ls, title):
"""
Search a list of dictionnaries
"""
entries = [i for i in ls if i['title'] == title]
if len(entries) > 1:
raise(Exception('There is more than 1 %s' % title))
return entries[0]
@pytest.fixture
def services(request):
now = timezone.now()
a = Authentic(title='blues', slug='blues', base_url='https://blues.example.publik',
last_operational_check_timestamp=now,
last_operational_success_timestamp=now)
a.save()
c = Combo(title='jazz', slug='jazz', base_url='https://jazz.example.publik')
c.save()
def test_response(app, services, monkeypatch):
monkeypatch.setattr(socket, 'gethostbyname', lambda x: '176.31.123.109')
monkeypatch.setattr(requests, 'get', lambda x, verify: MagicMock(status_code=200))
response = app.get('/api/health/')
assert response.status_code == 200
content = json.loads(response.content)
assert len(content) == 2
assert content[0]['title'] == 'blues'
assert content[1]['title'] == 'jazz'
def test_is_resolvable(app, services, monkeypatch):
def gethostname(netloc):
if netloc == "jazz.example.publik":
return '176.31.123.109'
else:
raise socket.gaierror
monkeypatch.setattr(socket, 'gethostbyname', gethostname)
monkeypatch.setattr(requests, 'get', lambda x, verify: MagicMock(status_code=200))
response = app.get('/api/health/')
content = json.loads(response.content)
blues = get_entry(content, 'blues')
jazz = get_entry(content, 'jazz')
assert not blues['is_resolvable']
assert jazz['is_resolvable']
def test_is_running(app, services, monkeypatch):
def get(url, verify):
if url == 'https://jazz.example.publik/manage/':
return MagicMock(status_code=200)
else:
return MagicMock(status_code=404)
monkeypatch.setattr(socket, 'gethostbyname', lambda x: '176.31.123.109')
monkeypatch.setattr(requests, 'get', get)
response = app.get('/api/health/')
content = json.loads(response.content)
blues = get_entry(content, 'blues')
jazz = get_entry(content, 'jazz')
assert not blues['is_running']
assert jazz['is_running']
def test_is_operational(app, services, monkeypatch):
monkeypatch.setattr(socket, 'gethostbyname', lambda x: '176.31.123.109')
monkeypatch.setattr(requests, 'get', lambda x, verify: MagicMock(status_code=200))
response = app.get('/api/health/')
content = json.loads(response.content)
blues = get_entry(content, 'blues')
jazz = get_entry(content, 'jazz')
assert blues['is_operational']
assert not jazz['is_operational']
def test_has_valid_certificate(app, services, monkeypatch):
def get(url, verify):
if 'blues.example.publik' in url or not verify:
return MagicMock(status_code=200)
else:
raise requests.exceptions.SSLError
monkeypatch.setattr(socket, 'gethostbyname', lambda x: '176.31.123.109')
monkeypatch.setattr(requests, 'get', get)
response = app.get('/api/health/')
content = json.loads(response.content)
blues = get_entry(content, 'blues')
jazz = get_entry(content, 'jazz')
assert blues['has_valid_certificate']
assert not jazz['has_valid_certificate']