@@ -51,7 +51,8 @@ def create_datasource(docker_grafana):
51
51
Create a Grafana data source from a test case.
52
52
After the test case finished, it will remove the data source again.
53
53
54
- https://docs.pytest.org/en/4.6.x/fixture.html#factories-as-fixtures
54
+ - https://grafana.com/docs/grafana/latest/http_api/data_source/
55
+ - https://docs.pytest.org/en/4.6.x/fixture.html#factories-as-fixtures
55
56
"""
56
57
57
58
# Reference to `grafana-client`.
@@ -60,14 +61,16 @@ def create_datasource(docker_grafana):
60
61
# Keep track of the datasource ids in order to delete them afterwards.
61
62
datasource_ids = []
62
63
63
- def _create_datasource (name : str , type : str , access : str ):
64
+ def _create_datasource (name : str , type : str , access : str , ** kwargs ):
65
+ datasource = dict (name = name , type = type , access = access )
66
+ datasource .update (kwargs )
64
67
try :
65
- response = grafana .datasource .create_datasource (dict ( name = name , type = type , access = access ) )
68
+ response = grafana .datasource .create_datasource (datasource )
66
69
datasource_id = response ["datasource" ]["id" ]
67
70
datasource_ids .append (datasource_id )
68
71
except GrafanaClientError as ex :
69
- # TODO: Mimic the original response `{'datasource': {'id': 5, 'uid': 'u9wNRyEnk', 'orgId': 1, ...` .
70
- # in order to make the removal work .
72
+ # TODO: Mimic the original response in order to make the removal work .
73
+ # `{'datasource': {'id': 5, 'uid': 'u9wNRyEnk', 'orgId': 1, ...` .
71
74
if not re .match (
72
75
"Client Error 409: Data source with (the )?same name already exists" , str (ex ), re .IGNORECASE
73
76
):
@@ -80,13 +83,77 @@ def _create_datasource(name: str, type: str, access: str):
80
83
grafana .datasource .delete_datasource_by_id (datasource_id )
81
84
82
85
86
+ @pytest .fixture
87
+ def create_folder (docker_grafana ):
88
+ """
89
+ Create a Grafana folder from a test case.
90
+ After the test case finished, it will remove the dashboard again.
91
+
92
+ - https://grafana.com/docs/grafana/latest/http_api/folder/
93
+ - https://docs.pytest.org/en/4.6.x/fixture.html#factories-as-fixtures
94
+ """
95
+
96
+ # Reference to `grafana-client`.
97
+ grafana = GrafanaWtf .grafana_client_factory (docker_grafana )
98
+
99
+ # Keep track of the dashboard uids in order to delete them afterwards.
100
+ folder_uids = []
101
+
102
+ # https://docs.pytest.org/en/4.6.x/fixture.html#factories-as-fixtures
103
+ def _create_folder (title : str , uid : str = None ):
104
+
105
+ # Create dashboard in Grafana.
106
+ try :
107
+ response = grafana .folder .create_folder (title = title , uid = uid )
108
+ folder_id = response ["id" ]
109
+ folder_uid = response ["uid" ]
110
+
111
+ # Response is like:
112
+ """
113
+ {
114
+ "id": 44,
115
+ "uid": "iga1UrEnz",
116
+ "title": "Testdrive",
117
+ "url": "/dashboards/f/iga1UrEnz/testdrive",
118
+ "hasAcl": false,
119
+ "canSave": true,
120
+ "canEdit": true,
121
+ "canAdmin": true,
122
+ "createdBy": "admin",
123
+ "created": "2022-03-22T23:44:38Z",
124
+ "updatedBy": "admin",
125
+ "updated": "2022-03-22T23:44:38Z",
126
+ "version": 1
127
+ }
128
+ """
129
+
130
+ folder_uids .append (folder_uid )
131
+ return folder_id
132
+ except GrafanaClientError as ex :
133
+ # TODO: Mimic the original response in order to make the removal work.
134
+ if not re .match (
135
+ "Client Error 409: a folder or dashboard in the general folder with the same name already exists" ,
136
+ str (ex ),
137
+ re .IGNORECASE ,
138
+ ):
139
+ raise
140
+
141
+ yield _create_folder
142
+
143
+ # Delete dashboard again.
144
+ if folder_uids :
145
+ for folder_uid in folder_uids :
146
+ grafana .folder .delete_folder (uid = folder_uid )
147
+
148
+
83
149
@pytest .fixture
84
150
def create_dashboard (docker_grafana ):
85
151
"""
86
152
Create a Grafana dashboard from a test case.
87
153
After the test case finished, it will remove the dashboard again.
88
154
89
- https://docs.pytest.org/en/4.6.x/fixture.html#factories-as-fixtures
155
+ - https://grafana.com/docs/grafana/latest/http_api/dashboard/
156
+ - https://docs.pytest.org/en/4.6.x/fixture.html#factories-as-fixtures
90
157
"""
91
158
92
159
# Reference to `grafana-client`.
@@ -96,11 +163,15 @@ def create_dashboard(docker_grafana):
96
163
dashboard_uids = []
97
164
98
165
# https://docs.pytest.org/en/4.6.x/fixture.html#factories-as-fixtures
99
- def _create_dashboard (title : str , datasource : str ):
166
+ def _create_dashboard (dashboard : dict = None , folder_id : str = None , folder_uid : str = None ):
100
167
101
168
# Create dashboard in Grafana.
102
- dashboard = mkdashboard (title = title , datasource = datasource )
103
- response = grafana .dashboard .update_dashboard (dashboard = {"dashboard" : dashboard , "overwrite" : True })
169
+ payload = {"dashboard" : dashboard , "overwrite" : True }
170
+ if folder_id :
171
+ payload ["folderId" ] = folder_id
172
+ if folder_uid :
173
+ payload ["folderUid" ] = folder_uid
174
+ response = grafana .dashboard .update_dashboard (dashboard = payload )
104
175
105
176
# Response is like:
106
177
# {'id': 3, 'slug': 'foo', 'status': 'success', 'uid': 'iO0xgE2nk', 'url': '/d/iO0xgE2nk/foo', 'version': 1}
@@ -116,6 +187,37 @@ def _create_dashboard(title: str, datasource: str):
116
187
grafana .dashboard .delete_dashboard (dashboard_uid = dashboard_uid )
117
188
118
189
190
+ @pytest .fixture
191
+ def ldi_resources (create_datasource , create_folder , create_dashboard ):
192
+ """
193
+ Create a Grafana dashboard from a test case.
194
+ After the test case finished, it will remove the dashboard again.
195
+
196
+ https://docs.pytest.org/en/4.6.x/fixture.html#factories-as-fixtures
197
+ """
198
+
199
+ # Create LDI datasource.
200
+ create_datasource (
201
+ name = "ldi_v2" ,
202
+ type = "influxdb" ,
203
+ access = "proxy" ,
204
+ url = "http://localhost:8086/" ,
205
+ user = "root" ,
206
+ password = "root" ,
207
+ database = "ldi_v2" ,
208
+ secureJsonData = {"password" : "root" },
209
+ )
210
+
211
+ # Create folder.
212
+ folder_id = create_folder (title = "Testdrive" , uid = "testdrive" )
213
+
214
+ # Create LDI dashboards.
215
+ for file in Path ("tests/grafana/dashboards" ).glob ("*.json" ):
216
+ with open (file , "r" ) as f :
217
+ dashboard = json .load (f )
218
+ create_dashboard (dashboard = dashboard , folder_id = folder_id )
219
+
220
+
119
221
def mkdashboard (title : str , datasource : str ):
120
222
"""
121
223
Build dashboard with single panel.
0 commit comments