tests: add tests for monitoring (#140)

Co-authored-by: nhnn <nhnn@disroot.org>
Co-authored-by: Inex Code <inex.code@selfprivacy.org>
Reviewed-on: https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api/pulls/140
Reviewed-by: Inex Code <inex.code@selfprivacy.org>
Co-authored-by: dettlaff <dettlaff@riseup.net>
Co-committed-by: dettlaff <dettlaff@riseup.net>
This commit is contained in:
dettlaff 2024-08-16 15:36:07 +03:00 committed by Inex Code
parent 55bbb0f3cc
commit 391e4802b2
4 changed files with 751 additions and 688 deletions

View file

@ -1,7 +1,4 @@
{ {
"python.formatting.provider": "black",
"python.linting.pylintEnabled": true,
"python.linting.enabled": true,
"python.testing.pytestArgs": [ "python.testing.pytestArgs": [
"tests" "tests"
], ],

View file

@ -119,7 +119,11 @@ class MonitoringQueries:
def _clean_slice_id(slice_id: str, clean_id: bool) -> str: def _clean_slice_id(slice_id: str, clean_id: bool) -> str:
"""Slices come in form of `/slice_name.slice`, we need to remove the `.slice` and `/` part.""" """Slices come in form of `/slice_name.slice`, we need to remove the `.slice` and `/` part."""
if clean_id: if clean_id:
return slice_id.split(".")[0].split("/")[1] parts = slice_id.split(".")[0].split("/")
if len(parts) > 1:
return parts[1]
else:
raise ValueError(f"Incorrect format slice_id: {slice_id}")
return slice_id return slice_id
@staticmethod @staticmethod
@ -131,7 +135,8 @@ class MonitoringQueries:
map( map(
lambda x: MonitoringMetric( lambda x: MonitoringMetric(
metric_id=MonitoringQueries._clean_slice_id( metric_id=MonitoringQueries._clean_slice_id(
x["metric"][id_key], clean_id=clean_id x["metric"].get(id_key, "/unknown.slice"),
clean_id=clean_id,
), ),
values=[ values=[
MonitoringQueries._prometheus_value_to_monitoring_value( MonitoringQueries._prometheus_value_to_monitoring_value(
@ -147,7 +152,7 @@ class MonitoringQueries:
map( map(
lambda x: MonitoringMetric( lambda x: MonitoringMetric(
metric_id=MonitoringQueries._clean_slice_id( metric_id=MonitoringQueries._clean_slice_id(
x["metric"][id_key], clean_id=clean_id x["metric"].get(id_key, "/unknown.slice"), clean_id=clean_id
), ),
values=list( values=list(
map( map(

View file

@ -2,252 +2,784 @@
# pylint: disable=unused-argument # pylint: disable=unused-argument
# pylint: disable=missing-function-docstring # pylint: disable=missing-function-docstring
# from dataclasses import dataclass from datetime import datetime
# from datetime import datetime from typing import Optional
# from typing import List, Dict import pytest
# import pytest
# from tests.test_graphql.common import ( from selfprivacy_api.models.services import ServiceStatus
# assert_empty,
# get_data,
# )
# MOCK_VALUES = [ from tests.test_graphql.common import (
# [1720135748, "3.75"], assert_empty,
# [1720135808, "4.525000000139698"], get_data,
# [1720135868, "4.541666666433841"], )
# [1720135928, "4.574999999798209"],
# [1720135988, "4.579166666759804"],
# [1720136048, "3.8791666664959195"],
# [1720136108, "4.5458333333954215"],
# [1720136168, "4.566666666651145"],
# [1720136228, "4.791666666666671"],
# [1720136288, "4.720833333364382"],
# [1720136348, "3.9624999999068677"],
# [1720136408, "4.6875"],
# [1720136468, "4.404166666790843"],
# [1720136528, "4.31666666680637"],
# [1720136588, "4.358333333317816"],
# [1720136648, "3.7083333334885538"],
# [1720136708, "4.558333333116025"],
# [1720136768, "4.729166666511446"],
# [1720136828, "4.75416666672875"],
# [1720136888, "4.624999999844775"],
# [1720136948, "3.9041666667132375"],
# ]
# @dataclass @pytest.fixture
# class DumbResponse: def mock_get_status_active(mocker):
# status_code: int mock = mocker.patch(
# json_data: dict "selfprivacy_api.graphql.queries.monitoring.Prometheus.get_status",
return_value=ServiceStatus.ACTIVE,
# def json(self): )
# return self.json_data return mock
# def generate_prometheus_response(result_type: str, result: List[Dict]): @pytest.fixture
# return DumbResponse( def mock_send_range_query(request, mocker):
# status_code=200, param = request.param
# json_data={"data": {"resultType": result_type, "result": result}},
# ) def send_query(
query: str, start: int, end: int, step: int, result_type: Optional[str] = None
):
return {
"resultType": "matrix",
"result": list(
map(
lambda x: {
"metric": {param[0]: f"metric-{x}"},
"values": [[0, "zero"]],
},
range(0, param[1]),
)
),
}
mock = mocker.patch(
"selfprivacy_api.utils.monitoring.MonitoringQueries._send_range_query",
send_query,
)
return mock
# MOCK_SINGLE_METRIC_PROMETHEUS_RESPONSE = generate_prometheus_response( @pytest.fixture
# "matrix", [{"values": MOCK_VALUES}] def mock_send_query(request, mocker):
# ) param = request.param
# MOCK_MULTIPLE_METRIC_DEVICE_PROMETHEUS_RESPONSE = generate_prometheus_response(
# "matrix",
# [
# {"metric": {"device": "a"}, "values": MOCK_VALUES},
# {"metric": {"device": "b"}, "values": MOCK_VALUES},
# {"metric": {"device": "c"}, "values": MOCK_VALUES},
# ],
# )
# # def generate_mock_metrics(name: str): def send_query(query: str, result_type: Optional[str] = None):
# # return { return {
# # "data": { "resultType": "matrix",
# # "monitoring": { "result": list(
# # f"{name}": { map(
# # "resultType": "matrix", lambda x: {
# # "result": [ "metric": {param[0]: f"metric-{x}"},
# # { "values": [[0, f"/slice_name_{x}.slice"]],
# # "metric": {"instance": "127.0.0.1:9002"}, },
# # "values": , range(0, param[1]),
# # } )
# # ], ),
# # } }
# # }
# # } mock = mocker.patch(
# # } "selfprivacy_api.utils.monitoring.MonitoringQueries._send_query",
send_query,
)
return mock
# # MOCK_CPU_USAGE_RESPONSE = generate_mock_metrics("cpuUsage") # ....
# # MOCK_DISK_USAGE_RESPONSE = generate_mock_metrics("diskUsage")
# # MOCK_MEMORY_USAGE_RESPONSE = generate_mock_metrics("memoryUsage")
# def generate_mock_query(name): CPU_USAGE_QUERY = """
# return f""" query {
# query Query {{ monitoring {
# monitoring {{ cpuUsage {
# {name} {{ resultType, result }} start
# }} end
# }} step
# """ overallUsage {
... on MonitoringValues {
values {
timestamp
value
}
}
... on MonitoringQueryError {
error
}
}
}
}
}
"""
CPU_USAGE_QUERY_WITH_OPTIONS = """
query Query($end: DateTime, $start: DateTime, $step: Int) {
monitoring {
cpuUsage(end: $end, start: $start, step: $step) {
end
overallUsage {
... on MonitoringValues {
values {
timestamp
value
}
}
... on MonitoringQueryError {
error
}
}
start
step
}
}
}
"""
MEMORY_USAGE_QUERY = """
query Query {
monitoring {
memoryUsage {
averageUsageByService {
... on MonitoringMetrics {
metrics {
metricId
values {
timestamp
value
}
}
}
... on MonitoringQueryError {
error
}
}
end
maxUsageByService {
... on MonitoringMetrics {
metrics {
metricId
values {
timestamp
value
}
}
}
... on MonitoringQueryError {
error
}
}
overallUsage {
... on MonitoringValues {
values {
timestamp
value
}
}
... on MonitoringQueryError {
error
}
}
start
step
}
}
}
"""
MEMORY_USAGE_QUERY_WITH_OPTIONS = """
query Query($end: DateTime, $start: DateTime, $step: Int) {
monitoring {
memoryUsage(end: $end, start: $start, step: $step) {
averageUsageByService {
... on MonitoringMetrics {
metrics {
metricId
values {
timestamp
value
}
}
}
... on MonitoringQueryError {
error
}
}
end
maxUsageByService {
... on MonitoringMetrics {
metrics {
metricId
values {
timestamp
value
}
}
}
... on MonitoringQueryError {
error
}
}
overallUsage {
... on MonitoringValues {
values {
timestamp
value
}
}
... on MonitoringQueryError {
error
}
}
start
step
}
}
}
"""
NETWORK_USAGE_QUERY = """
query Query {
monitoring {
networkUsage {
end
start
step
overallUsage {
... on MonitoringMetrics {
metrics {
metricId
values {
timestamp
value
}
}
}
... on MonitoringQueryError {
error
}
}
}
}
}
"""
NETWORK_USAGE_QUERY_WITH_OPTIONS = """
query Query($end: DateTime, $start: DateTime, $step: Int) {
monitoring {
networkUsage(end: $end, start: $start, step: $step) {
end
overallUsage {
... on MonitoringMetrics {
metrics {
metricId
values {
timestamp
value
}
}
}
... on MonitoringQueryError {
error
}
}
start
step
}
}
}
"""
DISK_USAGE_QUERY = """
query Query {
monitoring {
diskUsage {
start
end
step
overallUsage {
... on MonitoringMetrics {
metrics {
metricId
values {
timestamp
value
}
}
}
... on MonitoringQueryError {
error
}
}
}
}
}
"""
DISK_USAGE_QUERY_WITH_OPTIONS = """
query Query($end: DateTime, $start: DateTime, $step: Int) {
monitoring {
diskUsage(end: $end, start: $start, step: $step) {
end
overallUsage {
... on MonitoringMetrics {
metrics {
metricId
values {
timestamp
value
}
}
}
... on MonitoringQueryError {
error
}
}
start
step
}
}
}
"""
# def generate_mock_query_with_options(name): @pytest.mark.parametrize("mock_send_range_query", [["device", 2]], indirect=True)
# return f""" def test_graphql_get_disk_usage(
# query Query($start: DateTime, $end: DateTime, $step: Int) {{ client,
# monitoring {{ authorized_client,
# {name}(start: $start, end: $end, step: $step) {{ resultType, result }} mock_send_range_query,
# }} mock_get_status_active,
# }} ):
# """ response = authorized_client.post(
"/graphql",
json={"query": DISK_USAGE_QUERY},
)
data = get_data(response)
assert data == {
"monitoring": {
"diskUsage": {
"start": None,
"end": None,
"step": 60,
"overallUsage": {
"metrics": [
{
"metricId": "metric-0",
"values": [
{"timestamp": "1970-01-01T00:00:00", "value": "zero"}
],
},
{
"metricId": "metric-1",
"values": [
{"timestamp": "1970-01-01T00:00:00", "value": "zero"}
],
},
]
},
}
}
}
# def prometheus_result_from_dict(dict): @pytest.mark.parametrize("mock_send_range_query", [["device", 2]], indirect=True)
# # return MonitoringQueryResult(result_type=dict["resultType"], result=dict["result"]) def test_graphql_get_disk_usage_with_options(
# return dict client,
authorized_client,
mock_send_range_query,
mock_get_status_active,
):
response = authorized_client.post(
"/graphql",
json={
"query": DISK_USAGE_QUERY_WITH_OPTIONS,
"variables": {
"start": datetime.fromtimestamp(1720136108).isoformat(),
"end": datetime.fromtimestamp(1720137319).isoformat(),
"step": 90,
},
},
)
data = get_data(response)
assert data == {
"monitoring": {
"diskUsage": {
"start": "2024-07-04T23:35:08",
"end": "2024-07-04T23:55:19",
"step": 90,
"overallUsage": {
"metrics": [
{
"metricId": "metric-0",
"values": [
{"timestamp": "1970-01-01T00:00:00", "value": "zero"}
],
},
{
"metricId": "metric-1",
"values": [
{"timestamp": "1970-01-01T00:00:00", "value": "zero"}
],
},
]
},
}
}
}
# @pytest.fixture def test_graphql_get_disk_usage_unauthorized(client):
# def mock_cpu_usage(mocker): response = client.post(
# mock = mocker.patch( "/graphql",
# "selfprivacy_api.utils.prometheus.PrometheusQueries._send_query", json={"query": DISK_USAGE_QUERY},
# return_value=MOCK_CPU_USAGE_RESPONSE["data"]["monitoring"]["cpuUsage"], )
# ) assert_empty(response)
# return mock
# @pytest.fixture @pytest.mark.parametrize("mock_send_range_query", [["device", 2]], indirect=True)
# def mock_memory_usage(mocker): @pytest.mark.parametrize("mock_send_query", [["device", 2]], indirect=True)
# mock = mocker.patch( def test_graphql_get_memory_usage(
# "selfprivacy_api.utils.prometheus.PrometheusQueries._send_query", client,
# return_value=prometheus_result_from_dict( authorized_client,
# MOCK_MEMORY_USAGE_RESPONSE["data"]["monitoring"]["memoryUsage"] mock_send_query,
# ), mock_send_range_query,
# ) mock_get_status_active,
# return mock ):
response = authorized_client.post(
"/graphql",
json={"query": MEMORY_USAGE_QUERY},
)
data = get_data(response)
assert data == {
"monitoring": {
"memoryUsage": {
"averageUsageByService": {
"metrics": [
{
"metricId": "unknown",
"values": [
{
"timestamp": "1970-01-01T00:00:00",
"value": "/slice_name_0.slice",
},
],
},
{
"metricId": "unknown",
"values": [
{
"timestamp": "1970-01-01T00:00:00",
"value": "/slice_name_1.slice",
},
],
},
],
},
"end": None,
"maxUsageByService": {
"metrics": [
{
"metricId": "unknown",
"values": [
{
"timestamp": "1970-01-01T00:00:00",
"value": "/slice_name_0.slice",
},
],
},
{
"metricId": "unknown",
"values": [
{
"timestamp": "1970-01-01T00:00:00",
"value": "/slice_name_1.slice",
},
],
},
],
},
"overallUsage": {
"values": [
{
"timestamp": "1970-01-01T00:00:00",
"value": "zero",
},
],
},
"start": None,
"step": 60,
},
},
}
# @pytest.fixture @pytest.mark.parametrize("mock_send_range_query", [["device", 2]], indirect=True)
# def mock_disk_usage(mocker): @pytest.mark.parametrize("mock_send_query", [["device", 2]], indirect=True)
# mock = mocker.patch( def test_graphql_get_memory_usage_with_options(
# "selfprivacy_api.utils.prometheus.PrometheusQueries._send_query", client,
# return_value=prometheus_result_from_dict( authorized_client,
# MOCK_DISK_USAGE_RESPONSE["data"]["monitoring"]["diskUsage"] mock_send_query,
# ), mock_send_range_query,
# ) mock_get_status_active,
# return mock ):
response = authorized_client.post(
"/graphql",
json={
"query": MEMORY_USAGE_QUERY_WITH_OPTIONS,
"variables": {
"start": datetime.fromtimestamp(1720136108).isoformat(),
"end": datetime.fromtimestamp(1720137319).isoformat(),
"step": 90,
},
},
)
data = get_data(response)
assert data == {
"monitoring": {
"memoryUsage": {
"averageUsageByService": {
"metrics": [
{
"metricId": "unknown",
"values": [
{
"timestamp": "1970-01-01T00:00:00",
"value": "/slice_name_0.slice",
},
],
},
{
"metricId": "unknown",
"values": [
{
"timestamp": "1970-01-01T00:00:00",
"value": "/slice_name_1.slice",
},
],
},
],
},
"end": "2024-07-04T23:55:19",
"maxUsageByService": {
"metrics": [
{
"metricId": "unknown",
"values": [
{
"timestamp": "1970-01-01T00:00:00",
"value": "/slice_name_0.slice",
},
],
},
{
"metricId": "unknown",
"values": [
{
"timestamp": "1970-01-01T00:00:00",
"value": "/slice_name_1.slice",
},
],
},
],
},
"overallUsage": {
"values": [
{
"timestamp": "1970-01-01T00:00:00",
"value": "zero",
},
],
},
"start": "2024-07-04T23:35:08",
"step": 90,
},
},
}
# def test_graphql_get_disk_usage(client, authorized_client, mock_disk_usage): def test_graphql_get_memory_usage_unauthorized(client):
# response = authorized_client.post( response = client.post(
# "/graphql", "/graphql",
# json={"query": generate_mock_query("diskUsage")}, json={"query": MEMORY_USAGE_QUERY},
# ) )
assert_empty(response)
# data = get_data(response)
# assert data == MOCK_DISK_USAGE_RESPONSE["data"]
# def test_graphql_get_disk_usage_with_options( @pytest.mark.parametrize("mock_send_range_query", [["device", 2]], indirect=True)
# client, authorized_client, mock_disk_usage def test_graphql_get_cpu_usage(
# ): client,
# response = authorized_client.post( authorized_client,
# "/graphql", mock_send_range_query,
# json={ mock_get_status_active,
# "query": generate_mock_query_with_options("diskUsage"), ):
# "variables": { response = authorized_client.post(
# "start": datetime.fromtimestamp(1720136108).isoformat(), "/graphql",
# "end": datetime.fromtimestamp(1720137319).isoformat(), json={"query": CPU_USAGE_QUERY},
# "step": 90, )
# },
# },
# )
# data = get_data(response) data = get_data(response)
# assert data == MOCK_DISK_USAGE_RESPONSE["data"] assert data == {
"monitoring": {
"cpuUsage": {
"end": None,
"overallUsage": {
"values": [
{
"timestamp": "1970-01-01T00:00:00",
"value": "zero",
},
],
},
"start": None,
"step": 60,
},
},
}
# def test_graphql_get_disk_usage_unauthorized(client): @pytest.mark.parametrize("mock_send_range_query", [["device", 2]], indirect=True)
# response = client.post( def test_graphql_get_cpu_usage_with_options(
# "/graphql", client,
# json={"query": generate_mock_query("diskUsage")}, authorized_client,
# ) mock_send_range_query,
# assert_empty(response) mock_get_status_active,
):
response = authorized_client.post(
"/graphql",
json={
"query": CPU_USAGE_QUERY_WITH_OPTIONS,
"variables": {
"start": datetime.fromtimestamp(1720136108).isoformat(),
"end": datetime.fromtimestamp(1720137319).isoformat(),
"step": 90,
},
},
)
data = get_data(response)
assert data == {
"monitoring": {
"cpuUsage": {
"end": "2024-07-04T23:55:19",
"overallUsage": {
"values": [
{
"timestamp": "1970-01-01T00:00:00",
"value": "zero",
},
],
},
"start": "2024-07-04T23:35:08",
"step": 90,
},
},
}
# def test_graphql_get_memory_usage(client, authorized_client, mock_memory_usage): def test_graphql_get_cpu_usage_unauthorized(client):
# response = authorized_client.post( response = client.post(
# "/graphql", "/graphql",
# json={"query": generate_mock_query("memoryUsage")}, json={"query": CPU_USAGE_QUERY},
# ) )
assert_empty(response)
# data = get_data(response)
# assert data == MOCK_MEMORY_USAGE_RESPONSE["data"]
# def test_graphql_get_memory_usage_with_options( @pytest.mark.parametrize("mock_send_range_query", [["device", 2]], indirect=True)
# client, authorized_client, mock_memory_usage def test_graphql_get_network_usage(
# ): client,
# response = authorized_client.post( authorized_client,
# "/graphql", mock_send_range_query,
# json={ mock_get_status_active,
# "query": generate_mock_query_with_options("memoryUsage"), ):
# "variables": { response = authorized_client.post(
# "start": datetime.fromtimestamp(1720136108).isoformat(), "/graphql",
# "end": datetime.fromtimestamp(1720137319).isoformat(), json={"query": NETWORK_USAGE_QUERY},
# "step": 90, )
# },
# },
# )
# data = get_data(response) data = get_data(response)
# assert data == MOCK_MEMORY_USAGE_RESPONSE["data"] assert data == {
"monitoring": {
"networkUsage": {
"end": None,
"overallUsage": {
"metrics": [
{
"metricId": "/unknown.slice",
"values": [
{
"timestamp": "1970-01-01T00:00:00",
"value": "zero",
},
],
},
{
"metricId": "/unknown.slice",
"values": [
{
"timestamp": "1970-01-01T00:00:00",
"value": "zero",
},
],
},
],
},
"start": None,
"step": 60,
},
},
}
# def test_graphql_get_memory_usage_unauthorized(client): @pytest.mark.parametrize("mock_send_range_query", [["device", 2]], indirect=True)
# response = client.post( def test_graphql_get_network_usage_with_options(
# "/graphql", client,
# json={"query": generate_mock_query("memoryUsage")}, authorized_client,
# ) mock_send_range_query,
# assert_empty(response) mock_get_status_active,
):
response = authorized_client.post(
"/graphql",
json={
"query": NETWORK_USAGE_QUERY_WITH_OPTIONS,
"variables": {
"start": datetime.fromtimestamp(1720136108).isoformat(),
"end": datetime.fromtimestamp(1720137319).isoformat(),
"step": 90,
},
},
)
data = get_data(response)
assert data == {
"monitoring": {
"networkUsage": {
"end": "2024-07-04T23:55:19",
"overallUsage": {
"metrics": [
{
"metricId": "/unknown.slice",
"values": [
{
"timestamp": "1970-01-01T00:00:00",
"value": "zero",
},
],
},
{
"metricId": "/unknown.slice",
"values": [
{
"timestamp": "1970-01-01T00:00:00",
"value": "zero",
},
],
},
],
},
"start": "2024-07-04T23:35:08",
"step": 90,
},
},
}
# def test_graphql_get_cpu_usage(client, authorized_client, mock_cpu_usage): def test_graphql_get_network_usage_unauthorized(client):
# response = authorized_client.post( response = client.post(
# "/graphql", "/graphql",
# json={"query": generate_mock_query("cpuUsage")}, json={"query": NETWORK_USAGE_QUERY},
# ) )
assert_empty(response)
# data = get_data(response)
# assert data == MOCK_CPU_USAGE_RESPONSE["data"]
# def test_graphql_get_cpu_usage_with_options(client, authorized_client, mock_cpu_usage):
# response = authorized_client.post(
# "/graphql",
# json={
# "query": generate_mock_query_with_options("cpuUsage"),
# "variables": {
# "start": datetime.fromtimestamp(1720136108).isoformat(),
# "end": datetime.fromtimestamp(1720137319).isoformat(),
# "step": 90,
# },
# },
# )
# data = get_data(response)
# assert data == MOCK_CPU_USAGE_RESPONSE["data"]
# def test_graphql_get_cpu_usage_unauthorized(client):
# response = client.post(
# "/graphql",
# json={"query": generate_mock_query("cpuUsage")},
# )
# assert_empty(response)

View file

@ -1,471 +0,0 @@
# pylint: disable=redefined-outer-name
# pylint: disable=unused-argument
# pylint: disable=missing-function-docstring
# TODO(def): Finish this please.
# from datetime import datetime
# import pytest
# from selfprivacy_api.models.services import ServiceStatus
# from tests.test_graphql.common import (
# assert_empty,
# get_data,
# )
# @pytest.fixture
# def mock_get_status_active(mocker):
# mock = mocker.patch(
# "selfprivacy_api.graphql.queries.monitoring.Prometheus.get_status",
# return_value=ServiceStatus.ACTIVE,
# )
# return mock
# @pytest.fixture
# def mock_send_query(mocker):
# mock = mocker.patch(
# "selfprivacy_api.utils.monitoring.MonitoringQueries._send_range_query",
# # "selfprivacy_api.graphql.queries.monitoring._send_query",
# return_value=["test result"],
# )
# return mock
# # ....
# CPU_USAGE_QUERY = """
# query {
# monitoring {
# cpuUsage {
# start
# end
# step
# overallUsage {
# ... on MonitoringValues {
# values {
# timestamp
# value
# }
# }
# ... on MonitoringQueryError {
# error
# }
# }
# }
# }
# }
# """
# CPU_USAGE_QUERY_WITH_OPTIONS = """
# query Query($end: String!, $start: String!, $step: String!) {
# monitoring {
# cpuUsage(end: $end, start: $start, step: $step) {
# end
# overallUsage {
# ... on MonitoringValues {
# values {
# timestamp
# value
# }
# }
# ... on MonitoringQueryError {
# error
# }
# }
# start
# step
# }
# }
# }
# """
# MEMORY_USAGE_QUERY = """
# query Query {
# monitoring {
# memoryUsage {
# averageUsageByService {
# ... on MonitoringMetrics {
# metrics {
# metricId
# values {
# timestamp
# value
# }
# }
# }
# ... on MonitoringQueryError {
# error
# }
# }
# end
# maxUsageByService {
# ... on MonitoringMetrics {
# metrics {
# metricId
# values {
# timestamp
# value
# }
# }
# }
# ... on MonitoringQueryError {
# error
# }
# }
# overallUsage {
# ... on MonitoringValues {
# values {
# timestamp
# value
# }
# }
# ... on MonitoringQueryError {
# error
# }
# }
# start
# step
# }
# }
# }
# """
# MEMORY_USAGE_QUERY_WITH_OPTIONS = """
# query Query($end: String!, $start: String!, $step: String!) {
# monitoring {
# memoryUsage(end: $end, start: $start, step: $step) {
# averageUsageByService {
# ... on MonitoringMetrics {
# metrics {
# metricId
# values {
# timestamp
# value
# }
# }
# }
# ... on MonitoringQueryError {
# error
# }
# }
# end
# maxUsageByService {
# ... on MonitoringMetrics {
# metrics {
# metricId
# values {
# timestamp
# value
# }
# }
# }
# ... on MonitoringQueryError {
# error
# }
# }
# overallUsage {
# ... on MonitoringValues {
# values {
# timestamp
# value
# }
# }
# ... on MonitoringQueryError {
# error
# }
# }
# start
# step
# }
# }
# }
# """
# NETWORK_USAGE_QUERY = """
# query Query {
# monitoring {
# networkUsage {
# end
# start
# step
# overallUsage {
# ... on MonitoringMetrics {
# metrics {
# metricId
# values {
# timestamp
# value
# }
# }
# }
# ... on MonitoringQueryError {
# error
# }
# }
# }
# }
# }
# """
# NETWORK_USAGE_QUERY_WITH_OPTIONS = """
# query Query($end: String!, $start: String!, $step: String!) {
# monitoring {
# networkUsage(end: $end, start: $start, step: $step) {
# end
# overallUsage {
# ... on MonitoringMetrics {
# metrics {
# metricId
# values {
# timestamp
# value
# }
# }
# }
# ... on MonitoringQueryError {
# error
# }
# }
# start
# step
# }
# }
# }
# """
# DISK_USAGE_QUERY = """
# query Query {
# monitoring {
# diskUsage {
# __typename
# start
# end
# step
# overallUsage {
# ... on MonitoringMetrics {
# metrics {
# metricId
# values {
# timestamp
# value
# }
# }
# }
# ... on MonitoringQueryError {
# error
# }
# }
# }
# }
# }
# """
# DISK_USAGE_QUERY_WITH_OPTIONS = """
# query Query($end: String!, $start: String!, $step: String!) {
# monitoring {
# diskUsage(end: $end, start: $start, step: $step) {
# end
# overallUsage {
# ... on MonitoringMetrics {
# metrics {
# metricId
# values {
# timestamp
# value
# }
# }
# }
# ... on MonitoringQueryError {
# error
# }
# }
# start
# step
# }
# }
# }
# """
# def test_graphql_get_disk_usage(
# client,
# authorized_client,
# mock_send_query,
# mock_get_status_active,
# ):
# response = authorized_client.post(
# "/graphql",
# json={"query": DISK_USAGE_QUERY},
# )
# data = get_data(response)
# assert data == ["test result"]
# def test_graphql_get_disk_usage_with_options(
# client,
# authorized_client,
# mock_send_query,
# mock_get_status_active,
# ):
# response = authorized_client.post(
# "/graphql",
# json={
# "query": DISK_USAGE_QUERY,
# "variables": {
# "start": datetime.fromtimestamp(1720136108).isoformat(),
# "end": datetime.fromtimestamp(1720137319).isoformat(),
# "step": 90,
# },
# },
# )
# data = get_data(response)
# assert data == ["test result"]
# def test_graphql_get_disk_usage_unauthorized(client):
# response = client.post(
# "/graphql",
# json={"query": DISK_USAGE_QUERY},
# )
# assert_empty(response)
# def test_graphql_get_memory_usage(
# client,
# authorized_client,
# mock_send_query,
# mock_get_status_active,
# ):
# response = authorized_client.post(
# "/graphql",
# json={"query": MEMORY_USAGE_QUERY},
# )
# data = get_data(response)
# assert data == ["test result"]
# def test_graphql_get_memory_usage_with_options(
# client,
# authorized_client,
# mock_send_query,
# mock_get_status_active,
# ):
# response = authorized_client.post(
# "/graphql",
# json={
# "query": MEMORY_USAGE_QUERY_WITH_OPTIONS,
# "variables": {
# "start": datetime.fromtimestamp(1720136108).isoformat(),
# "end": datetime.fromtimestamp(1720137319).isoformat(),
# "step": 90,
# },
# },
# )
# data = get_data(response)
# assert data == ["test result"]
# def test_graphql_get_memory_usage_unauthorized(client):
# response = client.post(
# "/graphql",
# json={"query": MEMORY_USAGE_QUERY},
# )
# assert_empty(response)
# def test_graphql_get_cpu_usage(
# client,
# authorized_client,
# mock_send_query,
# mock_get_status_active,
# ):
# response = authorized_client.post(
# "/graphql",
# json={"query": CPU_USAGE_QUERY},
# )
# data = get_data(response)
# assert data == ["test result"]
# def test_graphql_get_cpu_usage_with_options(
# client,
# authorized_client,
# mock_send_query,
# mock_get_status_active,
# ):
# response = authorized_client.post(
# "/graphql",
# json={
# "query": CPU_USAGE_QUERY_WITH_OPTIONS,
# "variables": {
# "start": datetime.fromtimestamp(1720136108).isoformat(),
# "end": datetime.fromtimestamp(1720137319).isoformat(),
# "step": 90,
# },
# },
# )
# data = get_data(response)
# assert data == ["test result"]
# def test_graphql_get_cpu_usage_unauthorized(client):
# response = client.post(
# "/graphql",
# json={"query": CPU_USAGE_QUERY},
# )
# assert_empty(response)
# def test_graphql_get_network_usage(
# client,
# authorized_client,
# mock_send_query,
# mock_get_status_active,
# ):
# response = authorized_client.post(
# "/graphql",
# json={"query": NETWORK_USAGE_QUERY},
# )
# data = get_data(response)
# assert data == ["test result"]
# def test_graphql_get_network_usage_with_options(
# client,
# authorized_client,
# mock_send_query,
# mock_get_status_active,
# ):
# response = authorized_client.post(
# "/graphql",
# json={
# "query": NETWORK_USAGE_QUERY_WITH_OPTIONS,
# "variables": {
# "start": datetime.fromtimestamp(1720136108).isoformat(),
# "end": datetime.fromtimestamp(1720137319).isoformat(),
# "step": 90,
# },
# },
# )
# data = get_data(response)
# assert data == ["test result"]
# def test_graphql_get_network_usage_unauthorized(client):
# response = client.post(
# "/graphql",
# json={"query": NETWORK_USAGE_QUERY},
# )
# assert_empty(response)