mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-29 11:26:25 -05:00
Checking readiness of main app in Controller instead of javascript requests, which break with CORS.
This commit is contained in:
parent
8c294dad39
commit
ef32e8b42d
7 changed files with 227 additions and 43 deletions
48
controller/test/test_readiness.py
Normal file
48
controller/test/test_readiness.py
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
"""
|
||||
Tests for readiness check functions.
|
||||
"""
|
||||
|
||||
from unittest import mock
|
||||
|
||||
import aiohttp
|
||||
import pytest
|
||||
|
||||
from controller.lib.readiness import (
|
||||
_get_api_url,
|
||||
_get_app_url,
|
||||
check_api_ready,
|
||||
check_app_ready,
|
||||
)
|
||||
|
||||
|
||||
class TestReadinessUrls:
|
||||
|
||||
def test_api_url_docker_mode(self):
|
||||
with mock.patch("controller.lib.readiness.is_docker_mode", return_value=True):
|
||||
assert _get_api_url() == "http://api:8081/api/echo"
|
||||
|
||||
def test_api_url_systemd_mode(self):
|
||||
with mock.patch("controller.lib.readiness.is_docker_mode", return_value=False):
|
||||
assert _get_api_url() == "http://127.0.0.1:8081/api/echo"
|
||||
|
||||
def test_app_url_docker_mode(self):
|
||||
with mock.patch("controller.lib.readiness.is_docker_mode", return_value=True):
|
||||
assert _get_app_url() == "http://app:3000"
|
||||
|
||||
def test_app_url_systemd_mode(self):
|
||||
with mock.patch("controller.lib.readiness.is_docker_mode", return_value=False):
|
||||
assert _get_app_url() == "http://127.0.0.1:3000"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
class TestReadinessChecks:
|
||||
|
||||
async def test_check_api_ready_returns_false_on_connection_error(self):
|
||||
with mock.patch("aiohttp.ClientSession.get", side_effect=aiohttp.ClientError()):
|
||||
result = await check_api_ready()
|
||||
assert result is False
|
||||
|
||||
async def test_check_app_ready_returns_false_on_connection_error(self):
|
||||
with mock.patch("aiohttp.ClientSession.get", side_effect=aiohttp.ClientError()):
|
||||
result = await check_app_ready()
|
||||
assert result is False
|
||||
43
controller/test/test_ready_api.py
Normal file
43
controller/test/test_ready_api.py
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
"""
|
||||
Tests for the /api/ready readiness endpoint.
|
||||
"""
|
||||
|
||||
from unittest import mock
|
||||
|
||||
|
||||
class TestReadyEndpoint:
|
||||
"""Tests for /api/ready endpoint."""
|
||||
|
||||
def test_ready_both_down(self, test_client):
|
||||
with mock.patch("controller.api.ready.check_api_ready", return_value=False):
|
||||
with mock.patch("controller.api.ready.check_app_ready", return_value=False):
|
||||
response = test_client.get("/api/ready")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["api"] is False
|
||||
assert data["app"] is False
|
||||
|
||||
def test_ready_both_up(self, test_client):
|
||||
with mock.patch("controller.api.ready.check_api_ready", return_value=True):
|
||||
with mock.patch("controller.api.ready.check_app_ready", return_value=True):
|
||||
response = test_client.get("/api/ready")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["api"] is True
|
||||
assert data["app"] is True
|
||||
|
||||
def test_ready_api_up_app_down(self, test_client):
|
||||
with mock.patch("controller.api.ready.check_api_ready", return_value=True):
|
||||
with mock.patch("controller.api.ready.check_app_ready", return_value=False):
|
||||
response = test_client.get("/api/ready")
|
||||
data = response.json()
|
||||
assert data["api"] is True
|
||||
assert data["app"] is False
|
||||
|
||||
def test_ready_api_down_app_up(self, test_client):
|
||||
with mock.patch("controller.api.ready.check_api_ready", return_value=False):
|
||||
with mock.patch("controller.api.ready.check_app_ready", return_value=True):
|
||||
response = test_client.get("/api/ready")
|
||||
data = response.json()
|
||||
assert data["api"] is False
|
||||
assert data["app"] is True
|
||||
Loading…
Add table
Add a link
Reference in a new issue