forked from linode/linode_api4-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_monitor.py
More file actions
158 lines (125 loc) · 4.93 KB
/
Copy pathtest_monitor.py
File metadata and controls
158 lines (125 loc) · 4.93 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from test.integration.helpers import (
get_test_label,
send_request_when_resource_available,
wait_for_condition,
)
import pytest
from linode_api4 import LinodeClient
from linode_api4.objects import (
MonitorDashboard,
MonitorMetricsDefinition,
MonitorService,
MonitorServiceToken,
)
# List all dashboards
def test_get_all_dashboards(test_linode_client):
client = test_linode_client
dashboards = client.monitor.dashboards()
assert isinstance(dashboards[0], MonitorDashboard)
dashboard_get = dashboards[0]
get_service_type = dashboard_get.service_type
# Fetch Dashboard by ID
dashboard_by_id = client.load(MonitorDashboard, 1)
assert isinstance(dashboard_by_id, MonitorDashboard)
assert dashboard_by_id.id == 1
# #Fetch Dashboard by service_type
dashboards_by_svc = client.monitor.dashboards(service_type=get_service_type)
assert isinstance(dashboards_by_svc[0], MonitorDashboard)
assert dashboards_by_svc[0].service_type == get_service_type
def test_filter_and_group_by(test_linode_client):
client = test_linode_client
dashboards_by_svc = client.monitor.dashboards(service_type="linode")
assert isinstance(dashboards_by_svc[0], MonitorDashboard)
# Get the first dashboard for linode service type
dashboard = dashboards_by_svc[0]
assert dashboard.service_type == "linode"
# Ensure the dashboard has widgets
assert hasattr(
dashboard, "widgets"
), "Dashboard should have widgets attribute"
assert dashboard.widgets is not None, "Dashboard widgets should not be None"
assert (
len(dashboard.widgets) > 0
), "Dashboard should have at least one widget"
# Test the first widget's group_by and filters fields
widget = dashboard.widgets[0]
# Test group_by field type
group_by = widget.group_by
assert group_by is None or isinstance(
group_by, list
), "group_by should be None or list type"
if group_by is not None:
for item in group_by:
assert isinstance(item, str), "group_by items should be strings"
# Test filters field type
filters = widget.filters
assert filters is None or isinstance(
filters, list
), "filters should be None or list type"
if filters is not None:
from linode_api4.objects.monitor import Filter
for filter_item in filters:
assert isinstance(
filter_item, Filter
), "filter items should be Filter objects"
assert hasattr(
filter_item, "dimension_label"
), "Filter should have dimension_label"
assert hasattr(
filter_item, "operator"
), "Filter should have operator"
assert hasattr(filter_item, "value"), "Filter should have value"
# List supported services
def test_get_supported_services(test_linode_client):
client = test_linode_client
supported_services = client.monitor.services()
assert isinstance(supported_services[0], MonitorService)
get_supported_service = supported_services[0].service_type
# Get details for a particular service
service_details = client.load(MonitorService, get_supported_service)
assert isinstance(service_details, MonitorService)
assert service_details.service_type == get_supported_service
# Get Metric definition details for that particular service
metric_definitions = client.monitor.metric_definitions(
service_type=get_supported_service
)
assert isinstance(metric_definitions[0], MonitorMetricsDefinition)
# Test Helpers
def get_db_engine_id(client: LinodeClient, engine: str):
engines = client.database.engines()
engine_id = ""
for e in engines:
if e.engine == engine:
engine_id = e.id
return str(engine_id)
@pytest.fixture(scope="session")
def test_create_and_test_db(test_linode_client):
client = test_linode_client
label = get_test_label() + "-sqldb"
region = "us-ord"
engine_id = get_db_engine_id(client, "mysql")
dbtype = "g6-standard-1"
db = client.database.mysql_create(
label=label,
region=region,
engine=engine_id,
ltype=dbtype,
cluster_size=None,
)
def get_db_status():
return db.status == "active"
# TAKES 15-30 MINUTES TO FULLY PROVISION DB
wait_for_condition(60, 2000, get_db_status)
yield db
send_request_when_resource_available(300, db.delete)
def test_my_db_functionality(test_linode_client, test_create_and_test_db):
client = test_linode_client
assert test_create_and_test_db.status == "active"
entity_id = test_create_and_test_db.id
# create token for the particular service
token = client.monitor.create_token(
service_type="dbaas", entity_ids=[entity_id]
)
assert isinstance(token, MonitorServiceToken)
assert len(token.token) > 0, "Token should not be empty"
assert hasattr(token, "token"), "Response object has no 'token' attribute"