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, [])
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_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_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_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_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_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_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_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, [])
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'})
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_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_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': '*****@*****.**', 'name': 'MyName', 'data': products} g = Groups(self.dummy_ice_url, dry_run=True) mock_logger.reset_mock() g.set_application_group('MyName', products, '*****@*****.**') self.assertEquals(mock_logger.mock_calls, [ call.warning("DRY_RUN: would POST to /saveApplicationGroup with data: {'owner': " "'*****@*****.**', '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_get_application_group(self, mock_post, mock_get, mock_requests, mock_logger): mock_get.return_value = { "name": "AppGroup", "owner": "*****@*****.**", "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': '*****@*****.**', 'products': { "cloudwatch": ["tag1_tagA", "tag2_tagA", "tag1_tagB", "tag2_tagB"], "ec2": ["tag1_tagB", "tag2_tagB"], "ebs": [ "tag1_tagA", "tag2_tagA"], } })