forked from RexGibson/ice_pick
/
tests.py
805 lines (724 loc) · 33.3 KB
/
tests.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
import unittest
from mock import patch, call, Mock, MagicMock
import datetime
from ice_pick import utils
from ice_pick.exceptions import APIRequestException
from ice_pick import api
from ice_pick.api import APIRequest, APIFilters
from ice_pick.groups import Groups
import logging
from contextlib import nested
from ice_pick.filters import group_by as _group_by
from freezegun import freeze_time
class TestUtils(unittest.TestCase):
def test_format_datetime(self):
'''Tests that the datetime formatter utils function returns the right
format.
'''
utcnow = datetime.datetime.utcnow()
datetime_format = '%Y-%m-%d %I%p'
self.assertEquals(utcnow.strftime(datetime_format),
utils.format_datetime(utcnow))
class TestAPIFilters(unittest.TestCase):
def test_filter_keys(self):
'''Tests the name of the filter keys.'''
self.assertEquals(APIFilters.ACCOUNTS, 'account')
self.assertEquals(APIFilters.AGGREGATE, 'aggregate')
self.assertEquals(APIFilters.BREAKDOWN, 'breakdown')
self.assertEquals(APIFilters.CONSOLIDATE, 'consolidate')
self.assertEquals(APIFilters.END, 'end')
self.assertEquals(APIFilters.FACTOR_SPS, 'factorsps')
self.assertEquals(APIFilters.GROUP_BY, 'groupBy')
self.assertEquals(APIFilters.IS_COST, 'isCost')
self.assertEquals(APIFilters.OPERATIONS, 'operation')
self.assertEquals(APIFilters.PRODUCTS, 'product')
self.assertEquals(APIFilters.REGIONS, 'region')
self.assertEquals(APIFilters.SHOW_SPS, 'showsps')
self.assertEquals(APIFilters.START, 'start')
self.assertEquals(APIFilters.USAGE_TYPES, 'usageType')
def test_types(self):
'''Tests the types of the filters.'''
types = APIFilters.TYPES
self.assertEquals(types[APIFilters.ACCOUNTS], list)
self.assertEquals(types[APIFilters.AGGREGATE], str)
self.assertEquals(types[APIFilters.BREAKDOWN], bool)
self.assertEquals(types[APIFilters.CONSOLIDATE], str)
self.assertEquals(types[APIFilters.END], datetime.datetime)
self.assertEquals(types[APIFilters.FACTOR_SPS], bool)
self.assertEquals(types[APIFilters.GROUP_BY], str)
self.assertEquals(types[APIFilters.IS_COST], bool)
self.assertEquals(types[APIFilters.OPERATIONS], list)
self.assertEquals(types[APIFilters.PRODUCTS], list)
self.assertEquals(types[APIFilters.REGIONS], list)
self.assertEquals(types[APIFilters.SHOW_SPS], bool)
self.assertEquals(types[APIFilters.START], datetime.datetime)
self.assertEquals(types[APIFilters.USAGE_TYPES], list)
def test_default_filters(self):
'''Tests the default filters key/value.'''
utcnow = datetime.datetime.utcnow()
start = datetime.datetime(utcnow.year, utcnow.month, 1)
default_key_value = {
APIFilters.IS_COST: True,
APIFilters.AGGREGATE: 'data',
APIFilters.GROUP_BY: 'Product',
APIFilters.CONSOLIDATE: 'monthly',
APIFilters.START: utils.format_datetime(start),
APIFilters.END: utils.format_datetime(utcnow),
APIFilters.BREAKDOWN: True,
APIFilters.SHOW_SPS: False,
APIFilters.FACTOR_SPS: False,
}
assert APIFilters.default_filters() == default_key_value
class TestAPIRequest(unittest.TestCase):
api_request = None
dummy_ice_url = None
dummy_list = None
dummy_string = None
dummy_datetime = None
def setUp(self):
self.dummy_ice_url = 'http://foo.com'
self.api_request = APIRequest(self.dummy_ice_url)
self.dummy_list = ['012345678900', '009876543210']
self.dummy_str = 'foo'
self.dummy_datetime = datetime.datetime.utcnow()
def _join_list(self, value_list):
return ','.join(value_list)
def test_api_request_filter(self):
is_cost = False
dummy_filter = 'foo_bar'
dummy_value = ['foo', 'bar']
filters = {
APIFilters.ACCOUNTS: self.dummy_list,
APIFilters.IS_COST: is_cost,
dummy_filter: dummy_value,
}
api_request = APIRequest(self.dummy_ice_url, **filters)
request_filters = api_request.get_filters()
self.assertEquals(request_filters[APIFilters.ACCOUNTS],
self._join_list(self.dummy_list))
self.assertEquals(request_filters[APIFilters.IS_COST], is_cost)
self.assertEquals(request_filters[dummy_filter], dummy_value)
def test_set_http_auth(self):
api_request = APIRequest(self.dummy_ice_url)
self.assertEquals(api_request.auth, None)
api_request.set_http_auth(('foo', 'bar'))
self.assertEquals(api_request.auth, ('foo', 'bar'))
def _test_set_list(self, fn, filter_name):
# Type error exception test
self.assertRaises(TypeError,
fn,
self.dummy_str)
# Filter value test
fn(self.dummy_list)
self.assertEquals(self.api_request.get_filters()[filter_name],
self._join_list(self.dummy_list))
def _test_set_str(self, fn, filter_name):
# Type error exception test
self.assertRaises(TypeError, fn, [])
# Filter value test
fn(self.dummy_str)
self.assertEquals(self.api_request.get_filters()[filter_name],
self.dummy_str)
def _test_set_bool(self, fn, filter_name, value):
# Type error exception test
self.assertRaises(TypeError, fn, self.dummy_str)
# Filter value test
fn(value)
self.assertEquals(self.api_request.get_filters()[filter_name], value)
def _test_set_datetime(self, fn, filter_name):
# Type error exception test
self.assertRaises(TypeError, fn, self.dummy_str)
# Filter value test
fn(self.dummy_datetime)
self.assertEquals(self.api_request.get_filters()[filter_name],
utils.format_datetime(self.dummy_datetime))
def test_set_accounts(self):
'''Tests set_accounts function exception and filter results'''
self._test_set_list(self.api_request.set_accounts, APIFilters.ACCOUNTS)
def test_set_aggregate(self):
'''Tests set_aggregate function exception and filter results'''
self._test_set_str(self.api_request.set_aggregate,
APIFilters.AGGREGATE)
def test_set_breakdown(self):
'''Tests set_breakdown function exception and filter results'''
self._test_set_bool(self.api_request.set_breakdown,
APIFilters.BREAKDOWN, False)
def test_set_consolidate(self):
'''Tests set_consolidate function exception and filter results'''
self._test_set_str(self.api_request.set_consolidate,
APIFilters.CONSOLIDATE)
def test_set_end(self):
'''Tests set_end function exception and filter results'''
self._test_set_datetime(self.api_request.set_end, APIFilters.END)
def test_set_factor_sps(self):
'''Tests set_factor_sps function exception and filter results'''
self._test_set_bool(self.api_request.set_factor_sps,
APIFilters.FACTOR_SPS, True)
def test_set_group_by(self):
'''Tests set_group_by function exception and filter results'''
self._test_set_str(self.api_request.set_group_by,
APIFilters.GROUP_BY)
def test_set_is_cost(self):
'''Tests set_is_cost function exception and filter results'''
self._test_set_bool(self.api_request.set_is_cost,
APIFilters.IS_COST, False)
def test_set_operations(self):
'''Tests set_operations function exception and filter results'''
self._test_set_list(self.api_request.set_operations,
APIFilters.OPERATIONS)
def test_set_products(self):
'''Tests set_products function exception and filter results'''
self._test_set_list(self.api_request.set_products,
APIFilters.PRODUCTS)
def test_set_regions(self):
'''Tests set_regions function exception and filter results'''
self._test_set_list(self.api_request.set_regions,
APIFilters.REGIONS)
def test_set_show_sps(self):
'''Tests set_show_sps function exception and filter results'''
self._test_set_bool(self.api_request.set_show_sps,
APIFilters.SHOW_SPS, True)
def test_set_start(self):
'''Tests set_start function exception and filter results'''
self._test_set_datetime(self.api_request.set_start, APIFilters.START)
def test_set_usage_types(self):
'''Tests set_usage_types function exception and filter results'''
self._test_set_list(self.api_request.set_usage_types,
APIFilters.USAGE_TYPES)
def test_get_data(self):
'''Tests the get_data function by mocking the requests.post'''
class MockResponse():
def __init__(self, status_code):
self.status_code = status_code
@property
def content(self):
return '''{
"data": {},
"groupBy": {},
"interval": 3600000,
"start": 1380585600000,
"stats": {},
"status": 200
}'''
with patch.object(api, '_requests') as mock_request:
# Testing that if the post request comes back with a status_code
# different than 200, the function get_data will raise an
# APIRequestException.
mock_request.post.return_value = MockResponse(500)
self.assertRaises(APIRequestException, self.api_request.get_data)
# Testing that if the post request status_code is 200, the function
# get_data will return a dictionary.
mock_request.post.return_value = MockResponse(200)
self.assertTrue(isinstance(self.api_request.get_data(), dict))
@patch('ice_pick.groups.logger', spec_set=logging.Logger)
@patch('ice_pick.groups._requests')
@patch('ice_pick.groups.Groups._ice_get')
@patch('ice_pick.groups.Groups._ice_post')
class TestGroups(unittest.TestCase):
def setUp(self):
self.dummy_ice_url = 'http://foo.com/'
def test_init(self, mock_post, mock_get, mock_requests, mock_logger):
g = Groups(self.dummy_ice_url)
self.assertEquals(g.ice_url, self.dummy_ice_url)
self.assertEquals(g.dry_run, False)
self.assertEquals(g.auth, None)
self.assertEquals(mock_logger.mock_calls, [])
self.assertEquals(mock_requests.mock_calls, [])
self.assertEquals(mock_get.mock_calls, [])
self.assertEquals(mock_post.mock_calls, [])
def test_init_bad_url(self, mock_post, mock_get, mock_requests, mock_logger):
self.assertRaises(ValueError, Groups, 'foobar')
self.assertEquals(mock_logger.mock_calls, [])
self.assertEquals(mock_requests.mock_calls, [])
self.assertEquals(mock_get.mock_calls, [])
self.assertEquals(mock_post.mock_calls, [])
def test_init_dry_run(self, mock_post, mock_get, mock_requests, mock_logger):
g = Groups(self.dummy_ice_url, dry_run=True)
self.assertEquals(g.dry_run, True)
self.assertEquals(mock_logger.mock_calls,
[call.warning('DRY RUN only - will not make any changes')]
)
self.assertEquals(mock_requests.mock_calls, [])
self.assertEquals(mock_get.mock_calls, [])
self.assertEquals(mock_post.mock_calls, [])
def test_set_auth(self, mock_post, mock_get, mock_requests, mock_logger):
m = Mock()
g = Groups(self.dummy_ice_url)
g.set_http_auth(m)
self.assertEquals(g.ice_url, self.dummy_ice_url)
self.assertEquals(g.dry_run, False)
self.assertEquals(g.auth, m)
self.assertEquals(mock_logger.mock_calls, [])
self.assertEquals(mock_requests.mock_calls, [])
self.assertEquals(mock_get.mock_calls, [])
self.assertEquals(mock_post.mock_calls, [])
def test_get_account_names(self, mock_post, mock_get, mock_requests, mock_logger):
mock_get.return_value = [
{'name': '1234'},
{'name': '5678'}
]
g = Groups(self.dummy_ice_url, dry_run=False)
res = g.get_account_names()
self.assertEquals(mock_logger.mock_calls,
[]
)
self.assertEquals(mock_requests.mock_calls, [])
self.assertEquals(mock_get.mock_calls, [call('getAccounts?')])
self.assertEquals(mock_post.mock_calls, [])
self.assertEquals(res, ['1234', '5678'])
def test_get_regions_for_account(self, mock_post, mock_get, mock_requests, mock_logger):
mock_get.return_value = [
{"name":"ap-northeast-1"},
{"name":"us-east-1"},
{"name":"us-west-2"}
]
g = Groups(self.dummy_ice_url, dry_run=False)
res = g.get_regions_for_account(['123456', '7890'])
self.assertEquals(mock_logger.mock_calls, [])
self.assertEquals(mock_requests.mock_calls, [])
self.assertEquals(mock_get.mock_calls, [call('getRegions?account=123456%2C7890')])
self.assertEquals(mock_post.mock_calls, [])
self.assertEquals(res, ["ap-northeast-1", "us-east-1", "us-west-2"])
def test_get_all_resource_groups(self, mock_post, mock_get, mock_requests, mock_logger):
mock_get.return_value = [
{"name":"tag1_tagA"},
{"name":"tag2_tagA"},
{"name":"tag1_tagB"},
{"name":"tag2_tagB"},
]
url = 'getResourceGroups?account=23456%2C7890&product=ec2%2Cs3%2Crds®ion=us-east-1%2Cus-west-2'
g = Groups(self.dummy_ice_url, dry_run=False)
res = g.get_all_resource_groups(['23456', '7890'], ['us-east-1', 'us-west-2'], ['ec2', 's3', 'rds'])
self.assertEquals(mock_logger.mock_calls, [])
self.assertEquals(mock_requests.mock_calls, [])
self.assertEquals(mock_get.mock_calls, [call(url)])
self.assertEquals(mock_post.mock_calls, [])
self.assertEquals(res, ["tag1_tagA", "tag2_tagA", "tag1_tagB", "tag2_tagB"])
def test_get_products(self, mock_post, mock_get, mock_requests, mock_logger):
mock_get.return_value = [
{"name":"cloudfront"},
{"name":"cloudwatch"},
{"name":"ebs"},
]
url = 'getProducts?account=1234%2C23456®ion=us-east-1%2Cus-west-2&showResourceGroups=true'
g = Groups(self.dummy_ice_url, dry_run=False)
res = g.get_products(['1234', '23456'], ['us-east-1', 'us-west-2'])
self.assertEquals(mock_logger.mock_calls, [])
self.assertEquals(mock_requests.mock_calls, [])
self.assertEquals(mock_get.mock_calls, [call(url)])
self.assertEquals(mock_post.mock_calls, [])
self.assertEquals(res, ["cloudfront", "cloudwatch", "ebs"])
def test_get_resource_group_lists(self, mock_post, mock_get, mock_requests, mock_logger):
mock_get.return_value = [
{
"product": { "name": "vpc" },
"data": []
},
{
"product": { "name":"rds" },
"data":[
{"name":"tag1_tagA"},
{"name":"tag2_tagA"},
{"name":"tag1_tagB"},
{"name":"tag2_tagB"},
]
},
{
"product": { "name":"ebs" },
"data":[
{"name":"tag1_tagA"},
{"name":"tag2_tagA"},
{"name":"tag1_tagB"},
{"name":"tag2_tagB"},
]
},
]
url = 'getResourceGroupLists?'
expected = {
"vpc": [],
"rds": [
"tag1_tagA",
"tag2_tagA",
"tag1_tagB",
"tag2_tagB",
],
"ebs": [
"tag1_tagA",
"tag2_tagA",
"tag1_tagB",
"tag2_tagB",
],
}
g = Groups(self.dummy_ice_url, dry_run=False)
res = g.get_resource_group_lists()
self.assertEquals(mock_logger.mock_calls,
[]
)
self.assertEquals(mock_requests.mock_calls, [])
self.assertEquals(mock_get.mock_calls, [call(url)])
self.assertEquals(mock_post.mock_calls, [])
self.assertEquals(res, expected)
def test_get_application_group(self, mock_post, mock_get, mock_requests, mock_logger):
mock_get.return_value = {
"name": "AppGroup",
"owner": "nobody@example.com",
"data": {
"cloudwatch": [
{"name":"tag1_tagA"},
{"name":"tag2_tagA"},
{"name":"tag1_tagB"},
{"name":"tag2_tagB"},
],
"ec2": [
{"name":"tag1_tagB"},
{"name":"tag2_tagB"},
],
"ebs": [
{"name":"tag1_tagA"},
{"name":"tag2_tagA"},
],
},
}
url = 'getApplicationGroup?name=foobar'
g = Groups(self.dummy_ice_url, dry_run=False)
res = g.get_application_group('foobar')
self.assertEquals(mock_logger.mock_calls,
[]
)
self.assertEquals(mock_requests.mock_calls, [])
self.assertEquals(mock_get.mock_calls, [call(url)])
self.assertEquals(mock_post.mock_calls, [])
self.assertEquals(res, {
'name': 'AppGroup',
'owner': 'nobody@example.com',
'products': {
"cloudwatch": ["tag1_tagA", "tag2_tagA", "tag1_tagB", "tag2_tagB"],
"ec2": ["tag1_tagB", "tag2_tagB"],
"ebs": [ "tag1_tagA", "tag2_tagA"],
}
})
def test_set_application_group(self, mock_post, mock_get, mock_requests, mock_logger):
products = {
"cloudwatch": ["tag1_tagA", "tag2_tagA", "tag1_tagB", "tag2_tagB"],
"ec2": ["tag1_tagB", "tag2_tagB"],
"ebs": [ "tag1_tagA", "tag2_tagA"],
}
params = {'owner': 'foo@example.com', 'name': 'MyName', 'data': products}
g = Groups(self.dummy_ice_url, dry_run=False)
g.set_application_group('MyName', products, 'foo@example.com')
self.assertEquals(mock_logger.mock_calls, [
call.info("POSTing to /saveApplicationGroup with data: {'owner': "
"'foo@example.com', 'data': {'ec2': ['tag1_tagB', 'tag2_tagB'],"
" 'cloudwatch': ['tag1_tagA', 'tag2_tagA', 'tag1_tagB', "
"'tag2_tagB'], 'ebs': ['tag1_tagA', 'tag2_tagA']},"
" 'name': 'MyName'}")
])
self.assertEquals(mock_requests.mock_calls, [])
self.assertEquals(mock_get.mock_calls, [])
self.assertEquals(mock_post.mock_calls, [
call('saveApplicationGroup', params)
])
def test_set_application_group_dry_run(self, mock_post, mock_get, mock_requests, mock_logger):
products = {
"cloudwatch": ["tag1_tagA", "tag2_tagA", "tag1_tagB", "tag2_tagB"],
"ec2": ["tag1_tagB", "tag2_tagB"],
"ebs": [ "tag1_tagA", "tag2_tagA"],
}
params = {'owner': 'foo@example.com', 'name': 'MyName', 'data': products}
g = Groups(self.dummy_ice_url, dry_run=True)
mock_logger.reset_mock()
g.set_application_group('MyName', products, 'foo@example.com')
self.assertEquals(mock_logger.mock_calls, [
call.warning("DRY_RUN: would POST to /saveApplicationGroup with data: {'owner': "
"'foo@example.com', 'data': {'ec2': ['tag1_tagB', 'tag2_tagB'],"
" 'cloudwatch': ['tag1_tagA', 'tag2_tagA', 'tag1_tagB', "
"'tag2_tagB'], 'ebs': ['tag1_tagA', 'tag2_tagA']},"
" 'name': 'MyName'}")
])
self.assertEquals(mock_requests.mock_calls, [])
self.assertEquals(mock_get.mock_calls, [])
self.assertEquals(mock_post.mock_calls, [])
def test_delete_application_group_dry_run(self, mock_post, mock_get, mock_requests, mock_logger):
groups = Groups(self.dummy_ice_url, dry_run=True)
mock_logger.reset_mock()
groups.delete_application_group('foo')
self.assertEquals(mock_logger.mock_calls,
[call.warning('Would GET deleteApplicationGroup?name=foo')]
)
self.assertEquals(mock_get.mock_calls, [])
self.assertEquals(mock_post.mock_calls, [])
def test_delete_application_group(self, mock_post, mock_get, mock_requests, mock_logger):
groups = Groups(self.dummy_ice_url, dry_run=False)
mock_logger.reset_mock()
groups.delete_application_group('foo')
self.assertEquals(mock_logger.mock_calls, [])
self.assertEquals(mock_get.mock_calls, [call('deleteApplicationGroup?name=foo')])
self.assertEquals(mock_post.mock_calls, [])
@patch('ice_pick.groups.logger', spec_set=logging.Logger)
@patch('ice_pick.groups._requests')
class TestGroupsRequests(unittest.TestCase):
def test_ice_get(self, mock_requests, mock_logger):
url = 'http://foo.com/dashboard/foobar'
mock_result = Mock(status_code=200)
mock_result.json.return_value = {"status": 200, "data": ["foo","bar"] }
mock_requests.get.return_value = mock_result
g = Groups('http://foo.com/', dry_run=False)
mock_logger.reset_mock()
res = g._ice_get('foobar')
self.assertEquals(mock_logger.mock_calls,
[call.debug('GETing http://foo.com/dashboard/foobar')]
)
self.assertEquals(mock_requests.mock_calls, [
call.get('http://foo.com/dashboard/foobar', auth=None),
call.get().json()
])
self.assertEquals(res, ['foo', 'bar'])
def test_ice_get_auth(self, mock_requests, mock_logger):
url = 'http://foo.com/dashboard/foobar'
mock_result = Mock(status_code=200)
mock_result.json.return_value = {"status": 200, "data": ["foo","bar"] }
mock_requests.get.return_value = mock_result
g = Groups('http://foo.com/', dry_run=False)
g.set_http_auth(('myuser', 'mypass'))
mock_logger.reset_mock()
res = g._ice_get('foobar')
self.assertEquals(mock_logger.mock_calls,
[call.debug('GETing http://foo.com/dashboard/foobar')]
)
self.assertEquals(mock_requests.mock_calls, [
call.get('http://foo.com/dashboard/foobar', auth=('myuser', 'mypass')),
call.get().json()
])
self.assertEquals(res, ['foo', 'bar'])
def test_ice_get_request_error(self, mock_requests, mock_logger):
url = 'http://foo.com/dashboard/foobar'
mock_result = Mock(status_code=502)
mock_requests.get.return_value = mock_result
g = Groups('http://foo.com/', dry_run=False)
mock_logger.reset_mock()
self.assertRaises(APIRequestException,
g._ice_get,
'foobar')
self.assertEquals(mock_logger.mock_calls, [call.debug('GETing http://foo.com/dashboard/foobar')])
self.assertEquals(mock_requests.mock_calls, [call.get('http://foo.com/dashboard/foobar', auth=None)])
def test_ice_get_response_error(self, mock_requests, mock_logger):
url = 'http://foo.com/dashboard/foobar'
mock_result = Mock(status_code=200)
mock_result.json.return_value = {"status": 503, "data": ["foo","bar"] }
mock_requests.get.return_value = mock_result
g = Groups('http://foo.com/', dry_run=False)
mock_logger.reset_mock()
self.assertRaises(APIRequestException,
g._ice_get,
'foobar')
self.assertEquals(mock_logger.mock_calls, [call.debug('GETing http://foo.com/dashboard/foobar')])
self.assertEquals(mock_requests.mock_calls, [
call.get('http://foo.com/dashboard/foobar', auth=None),
call.get().json()
])
def test_ice_get_no_data(self, mock_requests, mock_logger):
url = 'http://foo.com/dashboard/foobar'
mock_result = Mock(status_code=200)
mock_result.json.return_value = {"status": 200}
mock_requests.get.return_value = mock_result
g = Groups('http://foo.com/', dry_run=False)
mock_logger.reset_mock()
res = g._ice_get('foobar')
self.assertEquals(mock_logger.mock_calls,
[call.debug('GETing http://foo.com/dashboard/foobar')]
)
self.assertEquals(mock_requests.mock_calls, [
call.get('http://foo.com/dashboard/foobar', auth=None),
call.get().json()
])
self.assertEquals(res, {})
def test_ice_post(self, mock_requests, mock_logger):
url = 'http://foo.com/dashboard/foobar'
mock_result = Mock(status_code=200)
mock_result.json.return_value = {"status": 200}
mock_requests.post.return_value = mock_result
g = Groups('http://foo.com/', dry_run=False)
mock_logger.reset_mock()
res = g._ice_post('foobar', {'baz': 'blam'})
self.assertEquals(mock_logger.mock_calls,
[call.debug("POSTing to http://foo.com/dashboard/foobar: {'baz': 'blam'}")]
)
self.assertEquals(mock_requests.mock_calls, [
call.post('http://foo.com/dashboard/foobar', data='{"baz": "blam"}', auth=None),
call.post().json()
])
self.assertEquals(res, {})
def test_ice_post_auth(self, mock_requests, mock_logger):
url = 'http://foo.com/dashboard/foobar'
mock_result = Mock(status_code=200)
mock_result.json.return_value = {"status": 200}
mock_requests.post.return_value = mock_result
g = Groups('http://foo.com/', dry_run=False)
g.set_http_auth(('myuser', 'mypass'))
mock_logger.reset_mock()
res = g._ice_post('foobar', {'baz': 'blam'})
self.assertEquals(mock_logger.mock_calls,
[call.debug("POSTing to http://foo.com/dashboard/foobar: {'baz': 'blam'}")]
)
self.assertEquals(mock_requests.mock_calls, [
call.post('http://foo.com/dashboard/foobar', data='{"baz": "blam"}', auth=('myuser', 'mypass')),
call.post().json()
])
self.assertEquals(res, {})
def test_ice_post_dry_run(self, mock_requests, mock_logger):
url = 'http://foo.com/dashboard/foobar'
g = Groups('http://foo.com/', dry_run=True)
mock_logger.reset_mock()
res = g._ice_post('foobar', {'baz': 'blam'})
self.assertEquals(mock_logger.mock_calls,
[call.warning("DRY RUN: Would POST to http://foo.com/dashboard/foobar: {'baz': 'blam'}")]
)
self.assertEquals(mock_requests.mock_calls, [])
def test_ice_post_request_error(self, mock_requests, mock_logger):
url = 'http://foo.com/dashboard/foobar'
mock_result = Mock(status_code=404)
mock_requests.post.return_value = mock_result
g = Groups('http://foo.com/', dry_run=False)
mock_logger.reset_mock()
self.assertRaises(APIRequestException,
g._ice_post,
'foobar',
{'baz': 'blam'}
)
self.assertEquals(mock_logger.mock_calls,
[call.debug("POSTing to http://foo.com/dashboard/foobar: {'baz': 'blam'}")]
)
self.assertEquals(mock_requests.mock_calls, [
call.post('http://foo.com/dashboard/foobar', data='{"baz": "blam"}', auth=None)
])
def test_ice_post_response_error(self, mock_requests, mock_logger):
url = 'http://foo.com/dashboard/foobar'
mock_result = Mock(status_code=200)
mock_result.json.return_value = {"status": 403}
mock_requests.post.return_value = mock_result
g = Groups('http://foo.com/', dry_run=False)
mock_logger.reset_mock()
self.assertRaises(APIRequestException,
g._ice_post,
'foobar',
{'baz': 'blam'}
)
self.assertEquals(mock_logger.mock_calls,
[call.debug("POSTing to http://foo.com/dashboard/foobar: {'baz': 'blam'}")]
)
self.assertEquals(mock_requests.mock_calls, [
call.post('http://foo.com/dashboard/foobar', data='{"baz": "blam"}', auth=None),
call.post().json()
])
def test_ice_post_data(self, mock_requests, mock_logger):
url = 'http://foo.com/dashboard/foobar'
mock_result = Mock(status_code=200)
mock_result.json.return_value = {"status": 200, "data": {'foo': 'bar'}}
mock_requests.post.return_value = mock_result
g = Groups('http://foo.com/', dry_run=False)
mock_logger.reset_mock()
res = g._ice_post('foobar', {'baz': 'blam'})
self.assertEquals(mock_logger.mock_calls,
[call.debug("POSTing to http://foo.com/dashboard/foobar: {'baz': 'blam'}")]
)
self.assertEquals(mock_requests.mock_calls, [
call.post('http://foo.com/dashboard/foobar', data='{"baz": "blam"}', auth=None),
call.post().json()
])
self.assertEquals(res, {'foo': 'bar'})
@freeze_time('2015-04-10 05:00:00')
def test_get_application_group_names(self, mock_requests, mock_logger):
mock_api = Mock()
mock_api.get_data.return_value = {
u'status': 200,
u'stats': {
u'foo': {
u'max': 21.597462450416614,
u'average': 21.597462450416614,
u'total': 21.597462450416614
},
u'bar': {
u'max': 1.6682055999999998,
u'average': 1.6682055999999998,
u'total': 1.6682055999999998
},
u'baz': {
u'max': 26.88331985366129,
u'average': 26.88331985366129,
u'total': 26.88331985366129
},
},
u'hours': [151],
u'start': 1430438400000,
u'time': [1430438400000],
u'data': {
u'foo': [21.597462450416614],
u'bar': [1.6682055999999998],
u'baz': [26.88331985366129],
},
u'groupBy': u'ApplicationGroup'
}
with patch('ice_pick.groups.APIRequest', spec_set=APIRequest) as mock_api_constr:
mock_api_constr.return_value = mock_api
g = Groups('http://foo.com/', dry_run=False)
res = g.get_application_group_names()
self.assertEqual(mock_api_constr.mock_calls[0], call('http://foo.com/'))
self.assertEqual(mock_api.mock_calls, [
call.set_aggregate('stats'),
call.set_group_by(_group_by.APPLICATION_GROUP),
call.set_end(datetime.datetime(2015, 4, 10, 5, 0)),
call.set_start(datetime.datetime(2015, 3, 13, 5, 0)),
call.get_data(),
])
self.assertEqual(sorted(res), [u'bar', u'baz', u'foo'])
self.assertEqual(mock_logger.mock_calls, [])
@freeze_time('2015-04-10 05:00:00')
def test_get_application_group_names_auth(self, mock_requests, mock_logger):
mock_api = Mock()
mock_api.get_data.return_value = {
u'status': 200,
u'stats': {
u'foo': {
u'max': 21.597462450416614,
u'average': 21.597462450416614,
u'total': 21.597462450416614
},
u'bar': {
u'max': 1.6682055999999998,
u'average': 1.6682055999999998,
u'total': 1.6682055999999998
},
u'baz': {
u'max': 26.88331985366129,
u'average': 26.88331985366129,
u'total': 26.88331985366129
},
},
u'hours': [151],
u'start': 1430438400000,
u'time': [1430438400000],
u'data': {
u'foo': [21.597462450416614],
u'bar': [1.6682055999999998],
u'baz': [26.88331985366129],
},
u'groupBy': u'ApplicationGroup'
}
with patch('ice_pick.groups.APIRequest', spec_set=APIRequest) as mock_api_constr:
mock_api_constr.return_value = mock_api
g = Groups('http://foo.com/', dry_run=False)
g.set_http_auth(('user', 'pass'))
res = g.get_application_group_names()
self.assertEqual(mock_api_constr.mock_calls[0], call('http://foo.com/'))
self.assertEqual(mock_api.mock_calls, [
call.set_http_auth(('user', 'pass')),
call.set_aggregate('stats'),
call.set_group_by(_group_by.APPLICATION_GROUP),
call.set_end(datetime.datetime(2015, 4, 10, 5, 0)),
call.set_start(datetime.datetime(2015, 3, 13, 5, 0)),
call.get_data(),
])
self.assertEqual(sorted(res), [u'bar', u'baz', u'foo'])
self.assertEqual(mock_logger.mock_calls, [])
if __name__ == '__main__':
unittest.main()