@@ -16,128 +16,114 @@ class DatasourceTestCase(unittest.TestCase):
1616 def setUp (self ):
1717 self .grafana = GrafanaApi (("admin" , "admin" ), host = "localhost" , url_path_prefix = "" , protocol = "http" )
1818
19-
2019 @requests_mock .Mocker ()
2120 def test_find_datasource (self , m ):
2221 m .get (
2322 "http://localhost/api/datasources/name/Prometheus" ,
2423 json = {
25- "id" :1 ,
26- "uid" :"h8KkCLt7z" ,
27- "orgId" :1 ,
28- "name" :"Prometheus" ,
29- "type" :"prometheus" ,
30- "typeName" :"Prometheus" ,
31- "typeLogoUrl" :"public/app/plugins/datasource/prometheus/img/prometheus_logo.svg" ,
32- "access" :"proxy" ,
33- "url" :"http://localhost:9090" ,
34- "password" :"" ,
35- "user" :"" ,
36- "database" :"" ,
24+ "id" : 1 ,
25+ "uid" : "h8KkCLt7z" ,
26+ "orgId" : 1 ,
27+ "name" : "Prometheus" ,
28+ "type" : "prometheus" ,
29+ "typeName" : "Prometheus" ,
30+ "typeLogoUrl" : "public/app/plugins/datasource/prometheus/img/prometheus_logo.svg" ,
31+ "access" : "proxy" ,
32+ "url" : "http://localhost:9090" ,
33+ "password" : "" ,
34+ "user" : "" ,
35+ "database" : "" ,
3736 "basicAuth" : False ,
3837 "isDefault" : True ,
39- "jsonData" :{
40- "httpMethod" :"POST"
41- },
42- "readOnly" : False
43- }
38+ "jsonData" : {"httpMethod" : "POST" },
39+ "readOnly" : False ,
40+ },
4441 )
4542
46- result = self .grafana .datasource .find_datasource (' Prometheus' )
47- self .assertEqual (result ["type" ], ' prometheus' )
43+ result = self .grafana .datasource .find_datasource (" Prometheus" )
44+ self .assertEqual (result ["type" ], " prometheus" )
4845
4946 @requests_mock .Mocker ()
5047 def test_find_datasource_not_existing (self , m ):
5148 m .get (
5249 "http://localhost/api/datasources/name/it_doesnot_exist" ,
53- json = {"message" : "Data source not found" },
54- status_code = 400
50+ json = {"message" : "Data source not found" },
51+ status_code = 400 ,
5552 )
5653
5754 with self .assertRaises (GrafanaBadInputError ):
58- result = self .grafana .datasource .find_datasource ('it_doesnot_exist' )
59-
55+ result = self .grafana .datasource .find_datasource ("it_doesnot_exist" )
6056
6157 @requests_mock .Mocker ()
6258 def test_get_datasource_id_by_name (self , m ):
63- m .get (
64- "http://localhost/api/datasources/id/Prometheus" ,
65- json = {"id" : 1 }
66- )
59+ m .get ("http://localhost/api/datasources/id/Prometheus" , json = {"id" : 1 })
6760
68- result = self .grafana .datasource .get_datasource_id_by_name (' Prometheus' )
61+ result = self .grafana .datasource .get_datasource_id_by_name (" Prometheus" )
6962 self .assertEqual (result ["id" ], 1 )
7063
71-
7264 @requests_mock .Mocker ()
7365 def test_list_datasources (self , m ):
7466 m .get (
7567 "http://localhost/api/datasources" ,
7668 json = [
7769 {
78- "id" :1 ,
79- "uid" :"h8KkCLt7z" ,
80- "orgId" :1 ,
81- "name" :"Prometheus" ,
82- "type" :"prometheus" ,
83- "typeName" :"Prometheus" ,
84- "typeLogoUrl" :"public/app/plugins/datasource/prometheus/img/prometheus_logo.svg" ,
85- "access" :"proxy" ,
86- "url" :"http://localhost:9090" ,
87- "password" :"" ,
88- "user" :"" ,
89- "database" :"" ,
70+ "id" : 1 ,
71+ "uid" : "h8KkCLt7z" ,
72+ "orgId" : 1 ,
73+ "name" : "Prometheus" ,
74+ "type" : "prometheus" ,
75+ "typeName" : "Prometheus" ,
76+ "typeLogoUrl" : "public/app/plugins/datasource/prometheus/img/prometheus_logo.svg" ,
77+ "access" : "proxy" ,
78+ "url" : "http://localhost:9090" ,
79+ "password" : "" ,
80+ "user" : "" ,
81+ "database" : "" ,
9082 "basicAuth" : False ,
9183 "isDefault" : True ,
92- "jsonData" :{
93- "httpMethod" :"POST"
94- },
95- "readOnly" : False
84+ "jsonData" : {"httpMethod" : "POST" },
85+ "readOnly" : False ,
9686 }
9787 ],
9888 )
9989
10090 result = self .grafana .datasource .list_datasources ()
101- self .assertEqual (result [0 ]["type" ], ' prometheus' )
91+ self .assertEqual (result [0 ]["type" ], " prometheus" )
10292 self .assertEqual (len (result ), 1 )
10393
10494 @requests_mock .Mocker ()
10595 def test_get_datasource_proxy_data (self , m ):
106- # http://localhost:3000/api/datasources/proxy/1/api/v1/query_range?query=up%7binstance%3d%22localhost:9090%22%7d&start=1644164339&end=1644164639&step=60
96+ # http://localhost:3000/api/datasources/proxy/1/api/v1/query_range?query=up%7binstance%3d%22localhost:9090%22%7d&start=1644164339&end=1644164639&step=60
10797 m .get (
10898 "http://localhost/api/datasources/proxy/1/api/v1/query_range" ,
10999 json = {
110100 "status" : "success" ,
111101 "data" : {
112102 "resultType" : "matrix" ,
113- "result" :[
103+ "result" : [
114104 {
115- "metric" :{
116- "__name__" :"up" ,
117- "instance" :"localhost:9090" ,
118- "job" :"prometheus"
119- },
120- "values" :[
121- [1644164339 ,"1" ],
122- [1644164399 ,"1" ],
123- [1644164459 ,"1" ],
124- [1644164519 ,"1" ],
125- [1644164579 ,"1" ],
126- [1644164639 ,"1" ]
127- ]
105+ "metric" : {"__name__" : "up" , "instance" : "localhost:9090" , "job" : "prometheus" },
106+ "values" : [
107+ [1644164339 , "1" ],
108+ [1644164399 , "1" ],
109+ [1644164459 , "1" ],
110+ [1644164519 , "1" ],
111+ [1644164579 , "1" ],
112+ [1644164639 , "1" ],
113+ ],
128114 }
129- ]
130- }
131- }
115+ ],
116+ },
117+ },
132118 )
133119 result = self .grafana .datasource .get_datasource_proxy_data (
134- 1 , # datasource_id
135- query_type = ' query_range' ,
136- expr = " up{instance=\ " localhost:9090\" }" ,
120+ 1 , # datasource_id
121+ query_type = " query_range" ,
122+ expr = ' up{instance="localhost:9090"}' ,
137123 start = 1644164339 ,
138124 end = 1644164639 ,
139125 step = 60 ,
140126 )
141- self .assertEqual (result ["status" ], ' success' )
142- self .assertEqual (result ["data" ]["result" ][0 ]["metric" ]["job" ], ' prometheus' )
127+ self .assertEqual (result ["status" ], " success" )
128+ self .assertEqual (result ["data" ]["result" ][0 ]["metric" ]["job" ], " prometheus" )
143129 self .assertEqual (len (result ["data" ]["result" ][0 ]["values" ]), 6 )
0 commit comments