-
Notifications
You must be signed in to change notification settings - Fork 5
/
apitocsv_test.py
290 lines (239 loc) · 9.99 KB
/
apitocsv_test.py
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import os
import unittest
from unittest.mock import patch
from apitocsv import *
@patch('qumulo.rest_client.RestClient')
class ApiToCsvTest(unittest.TestCase):
def tearDown(self):
for path in os.listdir(os.getcwd()):
if path.endswith('.csv'):
os.remove(path)
def setup_test(self, rest_client_mock):
rest_client_mock.return_value = rest_client_mock
rest_client_mock.login.__name__ = 'login'
atc = ApiToCsv('cluster', 'admin', 'a', os.getcwd())
return atc
def test_timestamp(self, rest_client_mock):
atc = self.setup_test(rest_client_mock)
atc.set_timestamp('2020-02-20 10:10:10')
self.assertEqual(atc.timestamp, '2020-02-20 10:10:10')
self.assertEqual(atc.datestamp, '2020-02-20')
def test_add_data(self, rest_client_mock):
atc = self.setup_test(rest_client_mock)
csv_file_path = 'test.csv'
atc.add_data(csv_file_path, {'k1': 'v1', 'k2': 'v2'})
with open(csv_file_path, 'r') as csv_file:
self.assertEqual(csv_file.read(), 'k1,k2\nv1,v2\n')
def test_qumulo_api_call(self, rest_client_mock):
atc = self.setup_test(rest_client_mock)
# We've logged in once from initializing the ApiToCsv object
self.assertEqual(len(atc.api_call_times['login']), 1)
atc.qumulo_api_call(rest_client_mock.login)
self.assertEqual(len(atc.api_call_times['login']), 2)
rest_client_mock.some_other_api.__name__ = 'some_other_api'
atc.qumulo_api_call(rest_client_mock.some_other_api)
self.assertEqual(len(atc.api_call_times['some_other_api']), 1)
def test_get_api_call_log(self, rest_client_mock):
atc = self.setup_test(rest_client_mock)
rest_client_mock.some_other_api.__name__ = 'some_other_api'
atc.qumulo_api_call(rest_client_mock.some_other_api)
atc.set_timestamp('2020-02-20 10:10:10')
atc.get_api_call_log('test')
with open('2020-02-20-test.csv', 'r') as csv_file:
data = csv_file.read()
self.assertIn(
'timestamp,call_name,call_count,avg_call_time\n'
'2020-02-20 10:10:10,login,1,',
data
)
self.assertIn('2020-02-20 10:10:10,some_other_api,1,', data)
def test_get_latest_date_dashstats_file(self, rest_client_mock):
atc = self.setup_test(rest_client_mock)
atc.set_timestamp('2020-02-20 10:10:10')
atc.get_api_call_log('test')
date = atc.get_latest_date_dashstats_file('2020-02-20-test.csv')
self.assertEqual(date, '2020-02-20 10:10:10')
def test_get_latest_date_dashstats_file_no_get_api_call_log(self, rest_client_mock):
atc = self.setup_test(rest_client_mock)
csv_file_path = 'test.csv'
atc.add_data(csv_file_path, {'k1': 'v1', 'k2': 'v2'})
with open(csv_file_path, 'r') as csv_file:
self.assertEqual(csv_file.read(), 'k1,k2\nv1,v2\n')
date = atc.get_latest_date_dashstats_file('csv_file_path')
self.assertEqual(date, '2000-01-01 00:00:00')
def test_set_dashstats_with_analytics_time_series(self, rest_client_mock):
atc = self.setup_test(rest_client_mock)
def get_time_series_data(**kwargs):
return [
{
'id': 'iops.read.rate',
'times': [ 0, 1500000000],
'values': [ .5, ]
},
{
'id': 'iops.write.rate',
'times': [ 0, 1500000000 ],
'values': [ 8 ]
},
{
'id': 'not_real_metric',
'times': [ 9, 9, 9 ],
'values': [ .1, .1, .1 ]
},
]
rest_client_mock.stats.time_series_get.side_effect = get_time_series_data
rest_client_mock.stats.time_series_get.__name__ = 'time_series_get'
atc.get_dashstats('test')
[csv_path] = [path for path in os.listdir(os.getcwd()) if path.endswith('.csv')]
with open(csv_path, 'r') as csv_file:
data = csv_file.read()
self.assertIn('timestamp,iops_read,iops_write', data)
self.assertIn(',0.5,8\n', data)
def test_get_cluster_status(self, rest_client_mock):
atc = self.setup_test(rest_client_mock)
def get_read_fs_stats(**kwargs):
return {
'block_size_bytes': 4096,
'free_size_bytes': '100',
'snapshot_size_bytes': '200',
'total_size_bytes': '300'
}
rest_client_mock.fs.read_fs_stats.side_effect = get_read_fs_stats
rest_client_mock.fs.read_fs_stats.__name__ = 'read_fs_stats'
def get_list_nodes(**kwargs):
return [
{
'node_status': 'online'
},
{
'node_status': 'offline'
}
]
rest_client_mock.cluster.list_nodes.side_effect = get_list_nodes
rest_client_mock.cluster.list_nodes.__name__ = 'list_nodes'
atc.set_timestamp('2020-02-20 10:10:10')
cluster_status = atc.get_cluster_status('test')
with open('2020-02-20-test.csv', 'r') as csv_file:
self.assertEqual(
csv_file.read(),
'timestamp,total_raw_capacity,total_usable_capacity,total_used_capacity,nodes_status\n'
'2020-02-20 10:10:10,300,300,200,"{1,0}"\n'
)
def test_get_sampled_files(self, rest_client_mock):
atc = self.setup_test(rest_client_mock)
def get_sampled_files(**kwargs):
return [
{
'capacity_usage': 1234,
'id': 1,
'name': 'file1'
},
]
rest_client_mock.fs.get_file_samples.side_effect = get_sampled_files
rest_client_mock.fs.get_file_samples.__name__ = 'file_samples'
def get_attr(**kwargs):
return {
'group': 513,
'owner': 500,
'modification_time': '2021-05-14T21:38:46.59687791Z',
'mode': '0777'
}
rest_client_mock.fs.get_attr.side_effect = get_attr
rest_client_mock.fs.get_attr.__name__ = 'get_attr'
atc.set_timestamp('2020-02-20 10:10:10')
atc.get_sampled_files('test', 'capacity', '/', 2)
with open('2020-02-20-test.csv', 'r') as csv_file:
self.assertEqual(
csv_file.read(),
'timestamp,inode_id,inode_name,extension,group_id,owner_id,last_modified,mode,size\n'
'2020-02-20 10:10:10,1,file1,,-17179868671,-12884901388,2021-05-14 21:38:46,0777,1234\n'
)
def test_get_iops_by_path(self, rest_client_mock):
atc = self.setup_test(rest_client_mock)
def get_iops(**kwargs):
return {
'entries': [
{
'id': 1,
'ip': '1.1.1.1',
'rate': .05,
'type': 'read'
},
{
'id': 2,
'ip': '2.2.2.2',
'rate': .05,
'type': 'write'
}
]
}
rest_client_mock.stats.iops_get.side_effect = get_iops
rest_client_mock.stats.iops_get.__name__ = 'iops_get'
def resolve_path(ids):
paths = []
for file_id in ids:
paths.append({ 'id': file_id, 'path': f'/path/{file_id}/' })
return paths
rest_client_mock.fs.resolve_paths.side_effect = resolve_path
rest_client_mock.fs.resolve_paths.__name__ = 'resolve_paths'
atc.set_timestamp('2020-02-20 10:10:10')
atc.get_iops_by_path('test')
with open('2020-02-20-test.csv', 'r') as csv_file:
self.assertEqual(
csv_file.read(),
'timestamp,level,path,total,file_read,file_write,namespace_read,namespace_write\n'
'2020-02-20 10:10:10,0,/,0.1,0.05,0.05,0,0\n'
'2020-02-20 10:10:10,1,/path,0.1,0.05,0.05,0,0\n'
'2020-02-20 10:10:10,2,/path/1,0.05,0.05,0,0,0\n'
'2020-02-20 10:10:10,2,/path/2,0.05,0,0.05,0,0\n'
)
with open('2020-02-20-iops_by_client_ip.csv', 'r') as csv_file:
self.assertEqual(
csv_file.read(),
'timestamp,ip,total,file_read,file_write,namespace_read,namespace_write\n'
'2020-02-20 10:10:10,1.1.1.1,0.05,0.05,0,0,0\n'
'2020-02-20 10:10:10,2.2.2.2,0.05,0,0.05,0,0\n'
)
def test_get_capacity_by_path(self, rest_client_mock):
atc = self.setup_test(rest_client_mock)
def get_read_dir_aggregates(**kwargs):
return {
'files': [
{
'capacity_usage': '4096',
'data_usage': '0',
'id': '1',
'name': 'one',
'num_directories': '0',
'num_files': '1',
},
{
'capacity_usage': '4096',
'data_usage': '0',
'id': '2',
'name': 'two',
'num_directories': '3',
'num_files': '1',
}
],
'id': '3',
'path': '/',
'total_capacity': '100',
'total_data': '50',
'total_directories': '6',
'total_files': '80',
}
rest_client_mock.fs.read_dir_aggregates.side_effect = get_read_dir_aggregates
rest_client_mock.fs.read_dir_aggregates.__name__ = 'read_dir_aggregates'
atc.set_timestamp('2020-02-20 10:10:10')
atc.get_capacity_by_path('test')
with open('2020-02-20-test.csv', 'r') as csv_file:
self.assertEqual(
csv_file.read(),
'timestamp,levels,inode_path,total_capacity,total_directories,total_files\n'
'2020-02-20 10:10:10,1,/,100,6,80\n'
'2020-02-20 10:10:10,1,/one,4096,0,1\n'
'2020-02-20 10:10:10,1,/two,4096,3,1\n'
)
if __name__ == '__main__':
unittest.main()