def test_get_limits_again(self): """test that existing limits dict is returned on subsequent calls""" mock_limits = Mock() cls = _EcsService(21, 43) cls.limits = mock_limits res = cls.get_limits() assert res == mock_limits
def test_find_usage_one_cluster(self): def se_cluster(*_, **kwargs): if kwargs['services'] == ['s1arn']: return { 'services': [{ 'launchType': 'EC2', 'serviceName': 's1', 'desiredCount': 4 }] } elif kwargs['services'] == ['s2arn']: return { 'services': [{ 'launchType': 'Fargate', 'serviceName': 's2', 'desiredCount': 26 }] } elif kwargs['services'] == ['s3arn']: return { 'services': [{ 'launchType': 'EC2', 'serviceName': 's3', 'desiredCount': 8 }] } else: return {} mock_conn = Mock() mock_conn.describe_services.side_effect = se_cluster mock_paginator = Mock() mock_paginator.paginate.return_value = [{ 'serviceArns': ['s1arn', 's2arn', 's3arn'], 'nextToken': 'string' }] mock_conn.get_paginator.return_value = mock_paginator cls = _EcsService(21, 43, {}, None) cls.conn = mock_conn cls._find_usage_one_cluster('cName') assert mock_conn.mock_calls == [ call.get_paginator('list_services'), call.get_paginator().paginate(cluster='cName', launchType='EC2'), call.describe_services(cluster='cName', services=['s1arn']), call.describe_services(cluster='cName', services=['s2arn']), call.describe_services(cluster='cName', services=['s3arn']) ] u = cls.limits[ 'EC2 Tasks per Service (desired count)'].get_current_usage() assert len(u) == 2 assert u[0].get_value() == 4 assert u[0].resource_id == 'cluster=cName; service=s1' assert u[0].aws_type == 'AWS::ECS::Service' assert u[1].get_value() == 8 assert u[1].resource_id == 'cluster=cName; service=s3' assert u[1].aws_type == 'AWS::ECS::Service'
def test_init(self): """test __init__()""" cls = _EcsService(21, 43) assert cls.service_name == 'ECS' assert cls.api_name == 'ecs' assert cls.conn is None assert cls.warning_threshold == 21 assert cls.critical_threshold == 43
def test_required_iam_permissions(self): cls = _EcsService(21, 43) assert sorted(cls.required_iam_permissions()) == [ 'ecs:DescribeClusters', 'ecs:DescribeServices', 'ecs:ListClusters', 'ecs:ListServices' ]
def test_find_usage(self): with patch.multiple(pb, autospec=True, connect=DEFAULT, _find_usage_clusters=DEFAULT) as mocks: cls = _EcsService(21, 43, {}, None) assert cls._have_usage is False cls.find_usage() assert mocks['connect'].mock_calls == [call(cls)] assert cls._have_usage is True assert mocks['connect'].return_value.mock_calls == []
def test_find_usage(self): with patch.multiple( pb, autospec=True, connect=DEFAULT, _find_usage_clusters=DEFAULT ) as mocks: cls = _EcsService(21, 43) assert cls._have_usage is False cls.find_usage() assert mocks['connect'].mock_calls == [call(cls)] assert cls._have_usage is True assert mocks['connect'].return_value.mock_calls == []
def test_get_limits(self): cls = _EcsService(21, 43) cls.limits = {} res = cls.get_limits() assert sorted(res.keys()) == sorted([ 'Clusters', 'Container Instances per Cluster', 'EC2 Tasks per Service (desired count)', 'Fargate Tasks', 'Services per Cluster', ]) for name, limit in res.items(): assert limit.service == cls assert limit.def_warning_threshold == 21 assert limit.def_critical_threshold == 43
def test_get_limits(self): cls = _EcsService(21, 43, {}, None) cls.limits = {} res = cls.get_limits() assert sorted(res.keys()) == sorted([ 'Clusters', 'Container Instances per Cluster', 'Tasks per service', 'Fargate On-Demand resource count', 'Fargate Spot resource count', 'Services per Cluster', ]) for name, limit in res.items(): assert limit.service == cls assert limit.def_warning_threshold == 21 assert limit.def_critical_threshold == 43
def test_find_usage_fargate(self): def se_gcul(klass, dims, metric_name='ResourceCount', period=60): dim_dict = {x['Name']: x['Value'] for x in dims} if dim_dict['Resource'] == 'OnDemand': return 6.0 if dim_dict['Resource'] == 'Spot': return 2.0 return 0 with patch('%s._get_cloudwatch_usage_latest' % pb, autospec=True) as m_gcul: m_gcul.side_effect = se_gcul cls = _EcsService(21, 43, {}, None) cls._find_usage_fargate() ondemand = cls.limits[ 'Fargate On-Demand resource count'].get_current_usage() assert len(ondemand) == 1 assert ondemand[0].get_value() == 6.0 assert ondemand[0].resource_id is None spot = cls.limits['Fargate Spot resource count'].get_current_usage() assert len(spot) == 1 assert spot[0].get_value() == 2.0 assert spot[0].resource_id is None
def test_find_usage_one_cluster(self): def se_cluster(*_, **kwargs): if kwargs['services'] == ['s1arn']: return { 'services': [ { 'launchType': 'EC2', 'serviceName': 's1', 'desiredCount': 4 } ] } elif kwargs['services'] == ['s2arn']: return { 'services': [ { 'launchType': 'Fargate', 'serviceName': 's2', 'desiredCount': 26 } ] } elif kwargs['services'] == ['s3arn']: return { 'services': [ { 'launchType': 'EC2', 'serviceName': 's3', 'desiredCount': 8 } ] } else: return {} mock_conn = Mock() mock_conn.describe_services.side_effect = se_cluster mock_paginator = Mock() mock_paginator.paginate.return_value = [{ 'serviceArns': [ 's1arn', 's2arn', 's3arn' ], 'nextToken': 'string' }] mock_conn.get_paginator.return_value = mock_paginator cls = _EcsService(21, 43) cls.conn = mock_conn cls._find_usage_one_cluster('cName') assert mock_conn.mock_calls == [ call.get_paginator('list_services'), call.get_paginator().paginate( cluster='cName', launchType='EC2' ), call.describe_services(cluster='cName', services=['s1arn']), call.describe_services(cluster='cName', services=['s2arn']), call.describe_services(cluster='cName', services=['s3arn']) ] u = cls.limits[ 'EC2 Tasks per Service (desired count)' ].get_current_usage() assert len(u) == 2 assert u[0].get_value() == 4 assert u[0].resource_id == 'cluster=cName; service=s1' assert u[0].aws_type == 'AWS::ECS::Service' assert u[1].get_value() == 8 assert u[1].resource_id == 'cluster=cName; service=s3' assert u[1].aws_type == 'AWS::ECS::Service'
def test_find_usage_clusters(self): def se_clusters(*_, **kwargs): if kwargs['clusters'] == ['c1arn']: return { 'clusters': [ { 'clusterArn': 'c1arn', 'clusterName': 'c1name', 'status': 'string', 'registeredContainerInstancesCount': 11, 'runningTasksCount': 6, 'pendingTasksCount': 45, 'activeServicesCount': 23, 'statistics': [ {'name': 'runningEC2TasksCount', 'value': '0'}, { 'name': 'runningFargateTasksCount', 'value': '4' }, {'name': 'pendingEC2TasksCount', 'value': '0'}, { 'name': 'pendingFargateTasksCount', 'value': '2' } ] } ] } elif kwargs['clusters'] == ['c2arn']: return { 'clusters': [ { 'clusterArn': 'c2arn', 'clusterName': 'c2name', 'status': 'string', 'registeredContainerInstancesCount': 3, 'runningTasksCount': 8, 'pendingTasksCount': 22, 'activeServicesCount': 2 } ] } return {} mock_conn = Mock() mock_conn.describe_clusters.side_effect = se_clusters mock_paginator = Mock() mock_paginator.paginate.return_value = [{ 'clusterArns': [ 'c1arn', 'c2arn' ], 'nextToken': 'string' }] mock_conn.get_paginator.return_value = mock_paginator cls = _EcsService(21, 43) cls.conn = mock_conn with patch('%s._find_usage_one_cluster' % pb, autospec=True) as m_fuoc: cls._find_usage_clusters() assert mock_conn.mock_calls == [ call.get_paginator('list_clusters'), call.get_paginator().paginate(), call.describe_clusters( clusters=['c1arn'], include=['STATISTICS'] ), call.describe_clusters( clusters=['c2arn'], include=['STATISTICS'] ) ] c = cls.limits['Container Instances per Cluster'].get_current_usage() assert len(c) == 2 assert c[0].get_value() == 11 assert c[0].resource_id == 'c1name' assert c[1].get_value() == 3 assert c[1].resource_id == 'c2name' s = cls.limits['Services per Cluster'].get_current_usage() assert len(s) == 2 assert s[0].get_value() == 23 assert s[0].resource_id == 'c1name' assert s[1].get_value() == 2 assert s[1].resource_id == 'c2name' u = cls.limits['Clusters'].get_current_usage() assert len(u) == 1 assert u[0].get_value() == 2 assert u[0].resource_id is None f = cls.limits['Fargate Tasks'].get_current_usage() assert len(f) == 1 assert f[0].get_value() == 4 assert f[0].resource_id is None assert m_fuoc.mock_calls == [ call(cls, 'c1name'), call(cls, 'c2name') ]