fix: fix tests (not working properly, yet)

This commit is contained in:
nhnn 2024-07-10 12:43:00 +03:00
parent 60e0dcc6cd
commit e988f77419
2 changed files with 34 additions and 29 deletions

View file

@ -1,18 +1,23 @@
import strawberry import strawberry
from typing import Optional
from selfprivacy_api.utils.prometheus import PrometheusQueries, PrometheusQueryResult from selfprivacy_api.utils.prometheus import PrometheusQueries, PrometheusQueryResult
@strawberry.type @strawberry.type
class Monitoring: class Monitoring:
@staticmethod
@strawberry.field @strawberry.field
def disk_usage() -> PrometheusQueryResult: def disk_usage(start: Optional[int] = None, end: Optional[int] = None, step: int = 60) -> PrometheusQueryResult:
return PrometheusQueries.disk_usage() return PrometheusQueries.disk_usage(start, end, step)
@staticmethod
@strawberry.field @strawberry.field
def memory_usage() -> PrometheusQueryResult: def memory_usage(start: Optional[int] = None, end: Optional[int] = None, step: int = 60) -> PrometheusQueryResult:
return PrometheusQueries.memory_usage() return PrometheusQueries.memory_usage(start, end, step)
@staticmethod
@strawberry.field @strawberry.field
def cpu_usage() -> PrometheusQueryResult: def cpu_usage(start: Optional[int] = None, end: Optional[int] = None, step: int = 60) -> PrometheusQueryResult:
return PrometheusQueries.cpu_usage() c = PrometheusQueries.cpu_usage(start, end, step)
return c
return PrometheusQueries.cpu_usage(start, end, step)

View file

@ -15,7 +15,7 @@ def generate_mock_metrics(name: str):
"data": { "data": {
"monitoring": { "monitoring": {
f"{name}": { f"{name}": {
"resultType": "matrix", "result_type": "matrix",
"result": [ "result": [
{ {
"metric": {"instance": "127.0.0.1:9002"}, "metric": {"instance": "127.0.0.1:9002"},
@ -50,16 +50,16 @@ def generate_mock_metrics(name: str):
} }
MOCK_CPU_USAGE_RESPONSE = generate_mock_metrics("cpu_usage") MOCK_CPU_USAGE_RESPONSE = generate_mock_metrics("cpuUsage")
MOCK_DISK_USAGE_RESPONSE = generate_mock_metrics("disk_usage") MOCK_DISK_USAGE_RESPONSE = generate_mock_metrics("diskUsage")
MOCK_MEMORY_USAGE_RESPONSE = generate_mock_metrics("memory_usage") MOCK_MEMORY_USAGE_RESPONSE = generate_mock_metrics("memoryUsage")
def generate_mock_query(name): def generate_mock_query(name):
return f""" return f"""
query Query {{ query Query {{
monitoring {{ monitoring {{
{name} {name} {{ resultType, result }}
}} }}
}} }}
""" """
@ -67,9 +67,9 @@ def generate_mock_query(name):
def generate_mock_query_with_options(name): def generate_mock_query_with_options(name):
return f""" return f"""
query Query($start: int, $end: int, $step: int) {{ query Query($start: Int, $end: Int, $step: Int) {{
monitoring {{ monitoring {{
{name}(start: $start, end: $end, step: $step) {name}(start: $start, end: $end, step: $step) {{ resultType, result }}
}} }}
}} }}
""" """
@ -78,8 +78,8 @@ def generate_mock_query_with_options(name):
@pytest.fixture @pytest.fixture
def mock_cpu_usage(mocker): def mock_cpu_usage(mocker):
mock = mocker.patch( mock = mocker.patch(
"selfprivacy_api.utils.prometheus.PrometheusQueries._sent_query", "selfprivacy_api.utils.prometheus.PrometheusQueries._send_query",
return_value=MOCK_CPU_USAGE_RESPONSE["data"]["monitoring"]["cpu_usage"], return_value=MOCK_CPU_USAGE_RESPONSE["data"]["monitoring"]["cpuUsage"],
) )
return mock return mock
@ -87,8 +87,8 @@ def mock_cpu_usage(mocker):
@pytest.fixture @pytest.fixture
def mock_memory_usage(mocker): def mock_memory_usage(mocker):
mock = mocker.patch( mock = mocker.patch(
"selfprivacy_api.utils.prometheus.PrometheusQueries._sent_query", "selfprivacy_api.utils.prometheus.PrometheusQueries._send_query",
return_value=MOCK_MEMORY_USAGE_RESPONSE["data"]["monitoring"]["memory_usage"], return_value=MOCK_MEMORY_USAGE_RESPONSE["data"]["monitoring"]["memoryUsage"],
) )
return mock return mock
@ -96,8 +96,8 @@ def mock_memory_usage(mocker):
@pytest.fixture @pytest.fixture
def mock_disk_usage(mocker): def mock_disk_usage(mocker):
mock = mocker.patch( mock = mocker.patch(
"selfprivacy_api.utils.prometheus.PrometheusQueries._sent_query", "selfprivacy_api.utils.prometheus.PrometheusQueries._send_query",
return_value=MOCK_DISK_USAGE_RESPONSE["data"]["monitoring"]["disk_usage"], return_value=MOCK_DISK_USAGE_RESPONSE["data"]["monitoring"]["diskUsage"],
) )
return mock return mock
@ -105,7 +105,7 @@ def mock_disk_usage(mocker):
def test_graphql_get_disk_usage(client, authorized_client, mock_disk_usage): def test_graphql_get_disk_usage(client, authorized_client, mock_disk_usage):
response = authorized_client.post( response = authorized_client.post(
"/graphql", "/graphql",
json={"query": generate_mock_query("disk_usage")}, json={"query": generate_mock_query("diskUsage")},
) )
data = get_data(response) data = get_data(response)
@ -119,7 +119,7 @@ def test_graphql_get_disk_usage_with_options(
response = authorized_client.post( response = authorized_client.post(
"/graphql", "/graphql",
json={ json={
"query": generate_mock_query_with_options("disk_usage"), "query": generate_mock_query_with_options("diskUsage"),
"variables": { "variables": {
"start": 1720136108, "start": 1720136108,
"end": 1720137319, "end": 1720137319,
@ -136,7 +136,7 @@ def test_graphql_get_disk_usage_with_options(
def test_graphql_get_disk_usage_unauthorized(client): def test_graphql_get_disk_usage_unauthorized(client):
response = client.post( response = client.post(
"/graphql", "/graphql",
json={"query": generate_mock_query("disk_usage")}, json={"query": generate_mock_query("diskUsage")},
) )
assert_empty(response) assert_empty(response)
@ -144,7 +144,7 @@ def test_graphql_get_disk_usage_unauthorized(client):
def test_graphql_get_memory_usage(client, authorized_client, mock_memory_usage): def test_graphql_get_memory_usage(client, authorized_client, mock_memory_usage):
response = authorized_client.post( response = authorized_client.post(
"/graphql", "/graphql",
json={"query": generate_mock_query("memory_usage")}, json={"query": generate_mock_query("memoryUsage")},
) )
data = get_data(response) data = get_data(response)
@ -158,7 +158,7 @@ def test_graphql_get_memory_usage_with_options(
response = authorized_client.post( response = authorized_client.post(
"/graphql", "/graphql",
json={ json={
"query": generate_mock_query_with_options("memory_usage"), "query": generate_mock_query_with_options("memoryUsage"),
"variables": { "variables": {
"start": 1720136108, "start": 1720136108,
"end": 1720137319, "end": 1720137319,
@ -175,7 +175,7 @@ def test_graphql_get_memory_usage_with_options(
def test_graphql_get_memory_usage_unauthorized(client): def test_graphql_get_memory_usage_unauthorized(client):
response = client.post( response = client.post(
"/graphql", "/graphql",
json={"query": generate_mock_query("memory_usage")}, json={"query": generate_mock_query("memoryUsage")},
) )
assert_empty(response) assert_empty(response)
@ -183,7 +183,7 @@ def test_graphql_get_memory_usage_unauthorized(client):
def test_graphql_get_cpu_usage(client, authorized_client, mock_cpu_usage): def test_graphql_get_cpu_usage(client, authorized_client, mock_cpu_usage):
response = authorized_client.post( response = authorized_client.post(
"/graphql", "/graphql",
json={"query": generate_mock_query("cpu_usage")}, json={"query": generate_mock_query("cpuUsage")},
) )
data = get_data(response) data = get_data(response)
@ -195,7 +195,7 @@ def test_graphql_get_cpu_usage_with_options(client, authorized_client, mock_cpu_
response = authorized_client.post( response = authorized_client.post(
"/graphql", "/graphql",
json={ json={
"query": generate_mock_query_with_options("cpu_usage"), "query": generate_mock_query_with_options("cpuUsage"),
"variables": { "variables": {
"start": 1720136108, "start": 1720136108,
"end": 1720137319, "end": 1720137319,
@ -212,6 +212,6 @@ def test_graphql_get_cpu_usage_with_options(client, authorized_client, mock_cpu_
def test_graphql_get_cpu_usage_unauthorized(client): def test_graphql_get_cpu_usage_unauthorized(client):
response = client.post( response = client.post(
"/graphql", "/graphql",
json={"query": generate_mock_query("cpu_usage")}, json={"query": generate_mock_query("cpuUsage")},
) )
assert_empty(response) assert_empty(response)