10
10
:license: BSD, see LICENSE for more details.
11
11
"""
12
12
import os
13
- import requests
14
13
from unittest import TestCase
15
- from mock import patch
16
- from mackerel .client import Client , MackerelClientError
14
+
15
+ import requests
16
+ from mackerel .client import (
17
+ Client ,
18
+ MackerelClientError ,
19
+ )
17
20
from mackerel .host import Host
21
+ from mock import patch
18
22
19
23
20
24
def dummy_response (m , filename , status_code = 200 ):
@@ -37,22 +41,22 @@ def setUpClass(cls):
37
41
38
42
@patch ('mackerel.client.requests.get' )
39
43
def test_should_get_hosts (self , m ):
40
- """ Client().get_hosts() should get host list. """
44
+ """Client().get_hosts() should get host list."""
41
45
dummy_response (m , 'fixtures/get_hosts.json' )
42
46
hosts = self .client .get_hosts ()
43
47
for host in hosts :
44
48
self .assertTrue (isinstance (host , Host ))
45
49
46
50
@patch ('mackerel.client.requests.get' )
47
51
def test_should_get_host (self , m ):
48
- """ Client().get_hosts() should get host. """
52
+ """Client().get_hosts() should get host."""
49
53
dummy_response (m , 'fixtures/get_host.json' )
50
54
host = self .client .get_host (self .id )
51
55
self .assertTrue (isinstance (host , Host ))
52
56
53
57
@patch ('mackerel.client.requests.post' )
54
58
def test_should_update_host_poweroff (self , m ):
55
- """ Client().update_host_status('poweroff') should return success. """
59
+ """Client().update_host_status('poweroff') should return success."""
56
60
dummy_response (m , 'fixtures/success.json' )
57
61
ret = self .client .update_host_status (self .id , 'poweroff' )
58
62
self .assertEqual (ret ['success' ], True )
@@ -63,7 +67,7 @@ def test_should_update_host_poweroff(self, m):
63
67
64
68
@patch ('mackerel.client.requests.post' )
65
69
def test_should_update_host_standby (self , m ):
66
- """ Client().update_host_status('standby') should return success. """
70
+ """Client().update_host_status('standby') should return success."""
67
71
dummy_response (m , 'fixtures/success.json' )
68
72
ret = self .client .update_host_status (self .id , 'standby' )
69
73
self .assertEqual (ret ['success' ], True )
@@ -75,7 +79,7 @@ def test_should_update_host_standby(self, m):
75
79
76
80
@patch ('mackerel.client.requests.post' )
77
81
def test_should_update_host_working (self , m ):
78
- """ Client().update_host_status('working') should return success. """
82
+ """Client().update_host_status('working') should return success."""
79
83
dummy_response (m , 'fixtures/success.json' )
80
84
ret = self .client .update_host_status ('2k48zsCx8ij' , 'working' )
81
85
self .assertEqual (ret ['success' ], True )
@@ -86,7 +90,7 @@ def test_should_update_host_working(self, m):
86
90
87
91
@patch ('mackerel.client.requests.post' )
88
92
def test_should_update_host_maintenance (self , m ):
89
- """ Client().update_host_status('maintenance') should return success. """
93
+ """Client().update_host_status('maintenance') should return success."""
90
94
dummy_response (m , 'fixtures/success.json' )
91
95
ret = self .client .update_host_status (self .id , 'maintenance' )
92
96
self .assertEqual (ret ['success' ], True )
@@ -96,75 +100,77 @@ def test_should_update_host_maintenance(self, m):
96
100
self .assertEqual (host .status , 'maintenance' )
97
101
98
102
def test_should_update_host_invalid (self ):
99
- """ Client().update_host_status('foo') should raise error. """
103
+ """Client().update_host_status('foo') should raise error."""
100
104
with self .assertRaises (MackerelClientError ):
101
105
self .client .update_host_status (self .id , 'foo' )
102
106
103
107
@patch ('mackerel.client.requests.post' )
104
108
def test_should_retire (self , m ):
105
- """ Client().retire_host() should return success. """
109
+ """Client().retire_host() should return success."""
106
110
dummy_response (m , 'fixtures/success.json' )
107
111
ret = self .client .retire_host (self .id )
108
112
self .assertEqual (ret ['success' ], True )
109
113
110
114
@patch ('mackerel.client.requests.get' )
111
115
def test_should_get_latest_metrics (self , m ):
112
- """ Client().get_latest_metrics() should get metrics. """
116
+ """Client().get_latest_metrics() should get metrics."""
113
117
dummy_response (m , 'fixtures/get_latest_metrics.json' )
114
- ret = self .client .get_latest_metrics ([self .id ],
115
- ['loadavg5' , 'memory.free' ])
118
+ ret = self .client .get_latest_metrics (
119
+ [self .id ],
120
+ ['loadavg5' , 'memory.free' ],
121
+ )
116
122
for k in ['loadavg5' , 'memory.free' ]:
117
123
self .assertTrue (k in ret ['tsdbLatest' ][self .id ].keys ())
118
124
119
125
@patch ('mackerel.client.requests.post' )
120
126
def test_should_post_metrics (self , m ):
121
- """ Client().post_metrics() should return success. """
127
+ """Client().post_metrics() should return success."""
122
128
dummy_response (m , 'fixtures/success.json' )
123
129
id = self .id
124
130
metrics = [
125
131
{
126
132
'hostId' : id , 'name' : 'custom.metrics.loadavg' ,
127
- 'time' : 1401537844 , 'value' : 1.4
133
+ 'time' : 1401537844 , 'value' : 1.4 ,
128
134
},
129
135
{
130
136
'hostId' : id , 'name' : 'custom.metrics.uptime' ,
131
- 'time' : 1401537844 , 'value' : 500
132
- }
137
+ 'time' : 1401537844 , 'value' : 500 ,
138
+ },
133
139
134
140
]
135
141
ret = self .client .post_metrics (metrics )
136
142
self .assertEqual (ret ['success' ], True )
137
143
138
144
@patch ('mackerel.client.requests.post' )
139
145
def test_should_post_service_metrics (self , m ):
140
- """ Client().post_service_metrics() should return success. """
146
+ """Client().post_service_metrics() should return success."""
141
147
dummy_response (m , 'fixtures/success.json' )
142
148
metrics = [
143
149
{
144
150
'name' : 'custom.metrics.latency' ,
145
- 'time' : 1401537844 , 'value' : 0.5
151
+ 'time' : 1401537844 , 'value' : 0.5 ,
146
152
},
147
153
{
148
154
'name' : 'custom.metrics.uptime' ,
149
- 'time' : 1401537844 , 'value' : 500
150
- }
155
+ 'time' : 1401537844 , 'value' : 500 ,
156
+ },
151
157
]
152
158
ret = self .client .post_service_metrics ('service_name' , metrics )
153
159
self .assertEqual (ret ['success' ], True )
154
160
155
161
@patch ('mackerel.client.requests.post' )
156
162
def test_should_raise_error_when_service_not_found (self , m ):
157
- """ Client().post_service_metrics() should raise error when service name not found. """
163
+ """Client().post_service_metrics() should raise error when service name not found."""
158
164
dummy_response (m , 'fixtures/error.json' , 404 )
159
165
metrics = [
160
166
{
161
167
'name' : 'custom.metrics.latency' ,
162
- 'time' : 1401537844 , 'value' : 0.5
168
+ 'time' : 1401537844 , 'value' : 0.5 ,
163
169
},
164
170
{
165
171
'name' : 'custom.metrics.uptime' ,
166
- 'time' : 1401537844 , 'value' : 500
167
- }
172
+ 'time' : 1401537844 , 'value' : 500 ,
173
+ },
168
174
]
169
175
with self .assertRaises (MackerelClientError ):
170
176
self .client .post_service_metrics ('foobarbaz' , metrics )
@@ -179,14 +185,14 @@ def setUpClass(cls):
179
185
180
186
@patch ('mackerel.client.requests.get' )
181
187
def test_should_get_ipaddress (self , m ):
182
- """ Host().ip_addr() should get ipaddress. """
188
+ """Host().ip_addr() should get ipaddress."""
183
189
dummy_response (m , 'fixtures/get_host.json' )
184
190
host = self .client .get_host (self .id )
185
191
self .assertEqual (host .ip_addr (), '10.0.2.15' )
186
192
187
193
@patch ('mackerel.client.requests.get' )
188
194
def test_should_get_macaddress (self , m ):
189
- """ Host().mac_addr() should get ipaddress. """
195
+ """Host().mac_addr() should get ipaddress."""
190
196
dummy_response (m , 'fixtures/get_host.json' )
191
197
host = self .client .get_host (self .id )
192
198
self .assertEqual (host .mac_addr (), '08:00:27:96:ed:36' )
0 commit comments