@@ -16,128 +16,114 @@ class DatasourceTestCase(unittest.TestCase):
16
16
def setUp (self ):
17
17
self .grafana = GrafanaApi (("admin" , "admin" ), host = "localhost" , url_path_prefix = "" , protocol = "http" )
18
18
19
-
20
19
@requests_mock .Mocker ()
21
20
def test_find_datasource (self , m ):
22
21
m .get (
23
22
"http://localhost/api/datasources/name/Prometheus" ,
24
23
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" : "" ,
37
36
"basicAuth" : False ,
38
37
"isDefault" : True ,
39
- "jsonData" :{
40
- "httpMethod" :"POST"
41
- },
42
- "readOnly" : False
43
- }
38
+ "jsonData" : {"httpMethod" : "POST" },
39
+ "readOnly" : False ,
40
+ },
44
41
)
45
42
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" )
48
45
49
46
@requests_mock .Mocker ()
50
47
def test_find_datasource_not_existing (self , m ):
51
48
m .get (
52
49
"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 ,
55
52
)
56
53
57
54
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" )
60
56
61
57
@requests_mock .Mocker ()
62
58
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 })
67
60
68
- result = self .grafana .datasource .get_datasource_id_by_name (' Prometheus' )
61
+ result = self .grafana .datasource .get_datasource_id_by_name (" Prometheus" )
69
62
self .assertEqual (result ["id" ], 1 )
70
63
71
-
72
64
@requests_mock .Mocker ()
73
65
def test_list_datasources (self , m ):
74
66
m .get (
75
67
"http://localhost/api/datasources" ,
76
68
json = [
77
69
{
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" : "" ,
90
82
"basicAuth" : False ,
91
83
"isDefault" : True ,
92
- "jsonData" :{
93
- "httpMethod" :"POST"
94
- },
95
- "readOnly" : False
84
+ "jsonData" : {"httpMethod" : "POST" },
85
+ "readOnly" : False ,
96
86
}
97
87
],
98
88
)
99
89
100
90
result = self .grafana .datasource .list_datasources ()
101
- self .assertEqual (result [0 ]["type" ], ' prometheus' )
91
+ self .assertEqual (result [0 ]["type" ], " prometheus" )
102
92
self .assertEqual (len (result ), 1 )
103
93
104
94
@requests_mock .Mocker ()
105
95
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
107
97
m .get (
108
98
"http://localhost/api/datasources/proxy/1/api/v1/query_range" ,
109
99
json = {
110
100
"status" : "success" ,
111
101
"data" : {
112
102
"resultType" : "matrix" ,
113
- "result" :[
103
+ "result" : [
114
104
{
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
+ ],
128
114
}
129
- ]
130
- }
131
- }
115
+ ],
116
+ },
117
+ },
132
118
)
133
119
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"}' ,
137
123
start = 1644164339 ,
138
124
end = 1644164639 ,
139
125
step = 60 ,
140
126
)
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" )
143
129
self .assertEqual (len (result ["data" ]["result" ][0 ]["values" ]), 6 )
0 commit comments