示例#1
0
    def test_find_usage_snapshots(self):
        response = result_fixtures.RDS.test_find_usage_snapshots

        mock_conn = Mock()
        mock_paginator = Mock()
        mock_paginator.paginate.return_value = response
        mock_conn.get_paginator.return_value = mock_paginator

        cls = _RDSService(21, 43)
        cls.conn = mock_conn

        cls._find_usage_snapshots()

        assert mock_conn.mock_calls == [
            call.get_paginator('describe_db_snapshots'),
            call.get_paginator().paginate()
        ]
        assert mock_paginator.mock_calls == [
            call.paginate()
        ]

        usage = sorted(cls.limits['DB snapshots per user'].get_current_usage())
        assert len(usage) == 1
        assert usage[0].get_value() == 1
        assert usage[0].aws_type == 'AWS::RDS::DBSnapshot'
示例#2
0
    def test_find_usage_subnet_groups(self):
        data = result_fixtures.RDS.test_find_usage_subnet_groups

        mock_conn = Mock()
        mock_paginator = Mock()
        mock_paginator.paginate.return_value = data
        mock_conn.get_paginator.return_value = mock_paginator

        cls = _RDSService(21, 43, {}, None)
        cls.conn = mock_conn

        cls._find_usage_subnet_groups()

        assert mock_conn.mock_calls == [
            call.get_paginator('describe_db_subnet_groups'),
            call.get_paginator().paginate()
        ]
        assert mock_paginator.mock_calls == [call.paginate()]

        usage = sorted(
            cls.limits['Subnets per Subnet Group'].get_current_usage())
        assert len(usage) == 3
        assert usage[0].get_value() == 1
        assert usage[0].aws_type == 'AWS::RDS::DBSubnetGroup'
        assert usage[0].resource_id == "SubnetGroup2"
        assert usage[1].get_value() == 2
        assert usage[1].aws_type == 'AWS::RDS::DBSubnetGroup'
        assert usage[1].resource_id == "SubnetGroup1"
        assert usage[2].get_value() == 3
        assert usage[2].aws_type == 'AWS::RDS::DBSubnetGroup'
        assert usage[2].resource_id == "default"
    def test_find_usage_security_groups(self):
        """test find usage for security groups"""
        # this also tests pagination
        responses = result_fixtures.ElastiCache.test_find_usage_security_groups

        mock_conn = Mock()
        mock_paginator = Mock()
        mock_paginator.paginate.return_value = responses
        mock_conn.get_paginator.return_value = mock_paginator
        cls = _ElastiCacheService(21, 43)
        cls.conn = mock_conn

        cls._find_usage_security_groups()

        assert mock_conn.mock_calls == [
            call.get_paginator('describe_cache_security_groups'),
            call.get_paginator().paginate()
        ]
        assert mock_paginator.mock_calls == [
            call.paginate()
        ]

        usage = cls.limits['Security Groups'].get_current_usage()
        assert len(usage) == 1
        assert usage[0].get_value() == 2
示例#4
0
 def test_find_usage(self):
     mock_paginator = Mock()
     mock_paginator.paginate.return_value = [
         {
             'Stacks': [
                 {'StackStatus': 'CREATE_IN_PROGRESS'},
                 {'StackStatus': 'DELETE_COMPLETE'},
                 {'StackStatus': 'DELETE_IN_PROGRESS'},
                 {'StackStatus': 'CREATE_FAILED'},
             ]
         },
         {
             'Stacks': [
                 {'StackStatus': 'UPDATE_COMPLETE_CLEANUP_IN_PROGRESS'},
                 {'StackStatus': 'ROLLBACK_COMPLETE'},
                 {'StackStatus': 'DELETE_FAILED'},
             ]
         },
     ]
     mock_conn = Mock()
     mock_conn.get_paginator.return_value = mock_paginator
     with patch('%s.connect' % pb) as mock_connect:
         cls = _CloudformationService(21, 43)
         cls.conn = mock_conn
         assert cls._have_usage is False
         cls.find_usage()
     assert mock_connect.mock_calls == [call()]
     assert cls._have_usage is True
     assert mock_conn.mock_calls == [
         call.get_paginator('describe_stacks'),
         call.get_paginator().paginate()
     ]
     assert mock_paginator.mock_calls == [call.paginate()]
     assert len(cls.limits['Stacks'].get_current_usage()) == 1
     assert cls.limits['Stacks'].get_current_usage()[0].get_value() == 6
示例#5
0
    def test_find_usage_subnet_groups(self):
        """test find usage for subnet groups"""
        # this also tests pagination
        responses = result_fixtures.ElastiCache.test_find_usage_subnet_groups

        mock_conn = Mock()
        mock_paginator = Mock()
        mock_paginator.paginate.return_value = responses
        mock_conn.get_paginator.return_value = mock_paginator
        cls = _ElastiCacheService(21, 43)
        cls.conn = mock_conn

        cls._find_usage_subnet_groups()

        assert mock_conn.mock_calls == [
            call.get_paginator('describe_cache_subnet_groups'),
            call.get_paginator().paginate()
        ]
        assert mock_paginator.mock_calls == [call.paginate()]

        usage = cls.limits['Subnet Groups'].get_current_usage()
        assert len(usage) == 1
        assert usage[0].get_value() == 3
        usage2 = cls.limits['Subnets per subnet group'].get_current_usage()
        assert len(usage2) == 3
        assert usage2[0].get_value() == 2
        assert usage2[1].get_value() == 1
        assert usage2[2].get_value() == 3
示例#6
0
    def test_find_usage_nodes(self):
        """test find usage for nodes"""
        # this also tests pagination
        responses = result_fixtures.ElastiCache.test_find_usage_nodes

        mock_conn = Mock()
        mock_paginator = Mock()
        mock_paginator.paginate.return_value = responses
        mock_conn.get_paginator.return_value = mock_paginator
        cls = _ElastiCacheService(21, 43)
        cls.conn = mock_conn
        cls._find_usage_nodes()

        usage = cls.limits['Nodes'].get_current_usage()
        assert len(usage) == 1
        assert usage[0].get_value() == 11

        usage = sorted(cls.limits['Nodes per Cluster'].get_current_usage())
        assert len(usage) == 1
        assert usage[0].get_value() == 1
        assert usage[0].resource_id == 'memcached1'

        assert mock_conn.mock_calls == [
            call.get_paginator('describe_cache_clusters'),
            call.get_paginator().paginate(ShowCacheNodeInfo=True)
        ]
        assert mock_paginator.mock_calls == [
            call.paginate(ShowCacheNodeInfo=True)
        ]
示例#7
0
    def test_find_usage_instances(self):
        instances = result_fixtures.RDS.test_find_usage_instances

        mock_conn = Mock()
        mock_paginator = Mock()
        mock_paginator.paginate.return_value = instances
        mock_conn.get_paginator.return_value = mock_paginator

        cls = _RDSService(21, 43, {}, None)
        cls.conn = mock_conn

        cls._find_usage_instances()

        assert mock_conn.mock_calls == [
            call.get_paginator('describe_db_instances'),
            call.get_paginator().paginate()
        ]
        assert mock_paginator.mock_calls == [call.paginate()]

        usage = sorted(
            cls.limits['Read replicas per master'].get_current_usage())
        assert len(usage) == 2
        assert usage[0].get_value() == 0
        assert usage[0].resource_id == 'foo'
        assert usage[1].get_value() == 2
        assert usage[1].resource_id == 'baz'
示例#8
0
    def test_find_usage_security_groups_exception(self):
        """test find usage for security groups"""
        err_resp = {
            'ResponseMetadata': {
                'HTTPStatusCode': 400,
                'RequestId': '7d74c6f0-c789-11e5-82fe-a96cdaa6d564'
            },
            'Error': {
                'Message': 'other message',
                'Code': 'OtherCode',
                'Type': 'Sender'
            }
        }
        exc = ClientError(err_resp, 'operation')

        def se_exc(*args, **kwargs):
            raise exc

        mock_conn = Mock()
        mock_paginator = Mock()
        mock_paginator.paginate.side_effect = se_exc
        mock_conn.get_paginator.return_value = mock_paginator

        cls = _ElastiCacheService(21, 43)
        cls.conn = mock_conn

        with pytest.raises(Exception) as raised:
            cls._find_usage_security_groups()

        assert mock_conn.mock_calls == [
            call.get_paginator('describe_cache_security_groups'),
            call.get_paginator().paginate()
        ]
        assert mock_paginator.mock_calls == [call.paginate()]
        assert raised.value == exc
    def test_find_usage_subnet_groups(self):
        data = result_fixtures.RDS.test_find_usage_subnet_groups

        mock_conn = Mock()
        mock_paginator = Mock()
        mock_paginator.paginate.return_value = data
        mock_conn.get_paginator.return_value = mock_paginator

        cls = _RDSService(21, 43)
        cls.conn = mock_conn

        cls._find_usage_subnet_groups()

        assert mock_conn.mock_calls == [
            call.get_paginator('describe_db_subnet_groups'),
            call.get_paginator().paginate()
        ]
        assert mock_paginator.mock_calls == [
            call.paginate()
        ]

        usage = sorted(
            cls.limits['Subnets per Subnet Group'].get_current_usage()
        )
        assert len(usage) == 3
        assert usage[0].get_value() == 1
        assert usage[0].aws_type == 'AWS::RDS::DBSubnetGroup'
        assert usage[0].resource_id == "SubnetGroup2"
        assert usage[1].get_value() == 2
        assert usage[1].aws_type == 'AWS::RDS::DBSubnetGroup'
        assert usage[1].resource_id == "SubnetGroup1"
        assert usage[2].get_value() == 3
        assert usage[2].aws_type == 'AWS::RDS::DBSubnetGroup'
        assert usage[2].resource_id == "default"
示例#10
0
    def test_find_usage_instances(self):
        instances = result_fixtures.RDS.test_find_usage_instances

        mock_conn = Mock()
        mock_paginator = Mock()
        mock_paginator.paginate.return_value = instances
        mock_conn.get_paginator.return_value = mock_paginator

        cls = _RDSService(21, 43)
        cls.conn = mock_conn

        cls._find_usage_instances()

        assert mock_conn.mock_calls == [
            call.get_paginator('describe_db_instances'),
            call.get_paginator().paginate()
        ]
        assert mock_paginator.mock_calls == [
            call.paginate()
        ]

        usage = sorted(
            cls.limits['Read replicas per master'].get_current_usage()
        )
        assert len(usage) == 2
        assert usage[0].get_value() == 0
        assert usage[0].resource_id == 'foo'
        assert usage[1].get_value() == 2
        assert usage[1].resource_id == 'baz'
示例#11
0
    def test_find_usage_reserved_instances(self):
        data = result_fixtures.RDS.test_find_usage_reserved_instances

        mock_conn = Mock()
        mock_paginator = Mock()
        mock_paginator.paginate.return_value = data
        mock_conn.get_paginator.return_value = mock_paginator

        cls = _RDSService(21, 43)
        cls.conn = mock_conn

        cls._find_usage_reserved_instances()

        assert mock_conn.mock_calls == [
            call.get_paginator('describe_reserved_db_instances'),
            call.get_paginator().paginate()
        ]
        assert mock_paginator.mock_calls == [
            call.paginate()
        ]

        usage = sorted(cls.limits['Reserved Instances'].get_current_usage())
        assert len(usage) == 1
        assert usage[0].get_value() == 2
        assert usage[0].aws_type == 'AWS::RDS::DBInstance'
示例#12
0
    def test_no_such_resource(self):
        mock_paginator = Mock()
        mock_paginator.paginate.side_effect = ClientError(
            {
                'Error': {
                    'Code':
                    'NoSuchResourceException',
                    'Message':
                    'The request failed because the specified '
                    'service does not exist.'
                }
            }, 'ListServiceQuotas')
        mock_conn = Mock()
        mock_conn.get_paginator.return_value = mock_paginator

        def se_connect(cls):
            cls.conn = mock_conn

        with patch('%s.connect' % pb, autospec=True) as m_connect:
            m_connect.side_effect = se_connect
            res = self.cls.quotas_for_service('scode')
        assert res == {}
        assert self.cls._cache == {'scode': {}}
        assert m_connect.mock_calls == [call(self.cls)]
        assert mock_conn.mock_calls == [
            call.get_paginator('list_service_quotas'),
            call.get_paginator().paginate(ServiceCode='scode')
        ]
示例#13
0
    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'
示例#14
0
    def test_find_usage_security_groups(self):
        data = result_fixtures.RDS.test_find_usage_security_groups

        mock_conn = Mock()
        mock_paginator = Mock()
        mock_paginator.paginate.return_value = data
        mock_conn.get_paginator.return_value = mock_paginator

        cls = _RDSService(21, 43)
        cls.conn = mock_conn

        cls._find_usage_security_groups()

        assert mock_conn.mock_calls == [
            call.get_paginator('describe_db_security_groups'),
            call.get_paginator().paginate()
        ]
        assert mock_paginator.mock_calls == [
            call.paginate()
        ]

        usage = sorted(cls.limits['DB security groups'].get_current_usage())
        assert len(usage) == 1
        assert usage[0].get_value() == 2
        assert usage[0].aws_type == 'AWS::RDS::DBSecurityGroup'

        usage = sorted(cls.limits['VPC Security Groups'].get_current_usage())
        assert len(usage) == 1
        assert usage[0].get_value() == 2
        assert usage[0].aws_type == 'AWS::RDS::DBSecurityGroup'

        usage = sorted(cls.limits[
                           'Max auths per security group'].get_current_usage())
        assert len(usage) == 4
        assert usage[0].get_value() == 0
        assert usage[0].resource_id == 'default:vpc-a926c2cc'
        assert usage[0].aws_type == 'AWS::RDS::DBSecurityGroup'
        assert usage[1].get_value() == 1
        assert usage[1].resource_id == 'SecurityGroup1'
        assert usage[1].aws_type == 'AWS::RDS::DBSecurityGroup'
        assert usage[2].get_value() == 2
        assert usage[2].resource_id == 'alctest'
        assert usage[2].aws_type == 'AWS::RDS::DBSecurityGroup'
        assert usage[3].get_value() == 3
        assert usage[3].resource_id == 'SecurityGroup2'
        assert usage[3].aws_type == 'AWS::RDS::DBSecurityGroup'
示例#15
0
    def test_find_usage_security_groups(self):
        data = result_fixtures.RDS.test_find_usage_security_groups

        mock_conn = Mock()
        mock_paginator = Mock()
        mock_paginator.paginate.return_value = data
        mock_conn.get_paginator.return_value = mock_paginator

        cls = _RDSService(21, 43)
        cls.conn = mock_conn

        cls._find_usage_security_groups()

        assert mock_conn.mock_calls == [
            call.get_paginator('describe_db_security_groups'),
            call.get_paginator().paginate()
        ]
        assert mock_paginator.mock_calls == [call.paginate()]

        usage = sorted(cls.limits['DB security groups'].get_current_usage())
        assert len(usage) == 1
        assert usage[0].get_value() == 2
        assert usage[0].aws_type == 'AWS::RDS::DBSecurityGroup'

        usage = sorted(cls.limits['VPC Security Groups'].get_current_usage())
        assert len(usage) == 1
        assert usage[0].get_value() == 2
        assert usage[0].aws_type == 'AWS::RDS::DBSecurityGroup'

        usage = sorted(
            cls.limits['Max auths per security group'].get_current_usage())
        assert len(usage) == 4
        assert usage[0].get_value() == 0
        assert usage[0].resource_id == 'default:vpc-a926c2cc'
        assert usage[0].aws_type == 'AWS::RDS::DBSecurityGroup'
        assert usage[1].get_value() == 1
        assert usage[1].resource_id == 'SecurityGroup1'
        assert usage[1].aws_type == 'AWS::RDS::DBSecurityGroup'
        assert usage[2].get_value() == 2
        assert usage[2].resource_id == 'alctest'
        assert usage[2].aws_type == 'AWS::RDS::DBSecurityGroup'
        assert usage[3].get_value() == 3
        assert usage[3].resource_id == 'SecurityGroup2'
        assert usage[3].aws_type == 'AWS::RDS::DBSecurityGroup'
    def test_find_usage_security_groups_no_ec2_classic(self):
        """test find usage for security groups"""
        def se_exc(*args, **kwargs):
            resp = {
                'ResponseMetadata': {
                    'HTTPStatusCode': 400,
                    'RequestId': '7d74c6f0-c789-11e5-82fe-a96cdaa6d564'
                },
                'Error': {
                    'Message': 'Use of cache security groups is not permitted'
                               ' in this API version for your account.',
                    'Code': 'InvalidParameterValue',
                    'Type': 'Sender'
                }
            }
            raise ClientError(resp, 'operation')

        mock_conn = Mock()
        mock_paginator = Mock()
        mock_paginator.paginate.side_effect = se_exc
        mock_conn.get_paginator.return_value = mock_paginator

        cls = _ElastiCacheService(21, 43)
        cls.conn = mock_conn

        with patch('%s.logger' % self.pbm) as mock_logger:
            cls._find_usage_security_groups()

        assert mock_conn.mock_calls == [
            call.get_paginator('describe_cache_security_groups'),
            call.get_paginator().paginate()
        ]
        assert mock_paginator.mock_calls == [
            call.paginate()
        ]
        assert mock_logger.mock_calls == [
            call.debug("caught ClientError checking ElastiCache security "
                       "groups (account without EC2-Classic?)")
        ]

        usage = cls.limits['Security Groups'].get_current_usage()
        assert len(usage) == 1
        assert usage[0].get_value() == 0
示例#17
0
    def test_find_usage_security_groups_no_ec2_classic(self):
        """test find usage for security groups"""
        def se_exc(*args, **kwargs):
            resp = {
                'ResponseMetadata': {
                    'HTTPStatusCode': 400,
                    'RequestId': '7d74c6f0-c789-11e5-82fe-a96cdaa6d564'
                },
                'Error': {
                    'Message': 'Use of cache security groups is not permitted'
                               ' in this API version for your account.',
                    'Code': 'InvalidParameterValue',
                    'Type': 'Sender'
                }
            }
            raise ClientError(resp, 'operation')

        mock_conn = Mock()
        mock_paginator = Mock()
        mock_paginator.paginate.side_effect = se_exc
        mock_conn.get_paginator.return_value = mock_paginator

        cls = _ElastiCacheService(21, 43)
        cls.conn = mock_conn

        with patch('%s.logger' % self.pbm) as mock_logger:
            cls._find_usage_security_groups()

        assert mock_conn.mock_calls == [
            call.get_paginator('describe_cache_security_groups'),
            call.get_paginator().paginate()
        ]
        assert mock_paginator.mock_calls == [
            call.paginate()
        ]
        assert mock_logger.mock_calls == [
            call.debug("caught ClientError checking ElastiCache security "
                       "groups (account without EC2-Classic?)")
        ]

        usage = cls.limits['Security Groups'].get_current_usage()
        assert len(usage) == 1
        assert usage[0].get_value() == 0
示例#18
0
    def test_not_cached(self):
        resp, expected = quotas_response()
        mock_paginator = Mock()
        mock_paginator.paginate.return_value = resp
        mock_conn = Mock()
        mock_conn.get_paginator.return_value = mock_paginator

        def se_connect(cls):
            cls.conn = mock_conn

        with patch('%s.connect' % pb, autospec=True) as m_connect:
            m_connect.side_effect = se_connect
            res = self.cls.quotas_for_service('scode')
        assert res == expected
        assert self.cls._cache == {'scode': expected}
        assert m_connect.mock_calls == [call(self.cls)]
        assert mock_conn.mock_calls == [
            call.get_paginator('list_service_quotas'),
            call.get_paginator().paginate(ServiceCode='scode')
        ]
示例#19
0
    def test_find_usage_parameter_groups(self):
        """test find usage for parameter groups"""
        # this also tests pagination
        responses = result_fixtures.ElastiCache.test_find_usage_parameter_groups

        mock_conn = Mock()
        mock_paginator = Mock()
        mock_paginator.paginate.return_value = responses
        mock_conn.get_paginator.return_value = mock_paginator
        cls = _ElastiCacheService(21, 43, {}, None)
        cls.conn = mock_conn
        cls._find_usage_parameter_groups()

        assert mock_conn.mock_calls == [
            call.get_paginator('describe_cache_parameter_groups'),
            call.get_paginator().paginate()
        ]
        assert mock_paginator.mock_calls == [call.paginate()]

        usage = cls.limits['Parameter Groups'].get_current_usage()
        assert len(usage) == 1
        assert usage[0].get_value() == 3
示例#20
0
    def test_find_usage_reserved_instances(self):
        data = result_fixtures.RDS.test_find_usage_reserved_instances

        mock_conn = Mock()
        mock_paginator = Mock()
        mock_paginator.paginate.return_value = data
        mock_conn.get_paginator.return_value = mock_paginator

        cls = _RDSService(21, 43)
        cls.conn = mock_conn

        cls._find_usage_reserved_instances()

        assert mock_conn.mock_calls == [
            call.get_paginator('describe_reserved_db_instances'),
            call.get_paginator().paginate()
        ]
        assert mock_paginator.mock_calls == [call.paginate()]

        usage = sorted(cls.limits['Reserved Instances'].get_current_usage())
        assert len(usage) == 1
        assert usage[0].get_value() == 2
        assert usage[0].aws_type == 'AWS::RDS::DBInstance'
示例#21
0
    def test_find_usage_snapshots(self):
        response = result_fixtures.RDS.test_find_usage_snapshots

        mock_conn = Mock()
        mock_paginator = Mock()
        mock_paginator.paginate.return_value = response
        mock_conn.get_paginator.return_value = mock_paginator

        cls = _RDSService(21, 43)
        cls.conn = mock_conn

        cls._find_usage_snapshots()

        assert mock_conn.mock_calls == [
            call.get_paginator('describe_db_snapshots'),
            call.get_paginator().paginate()
        ]
        assert mock_paginator.mock_calls == [call.paginate()]

        usage = sorted(cls.limits['DB snapshots per user'].get_current_usage())
        assert len(usage) == 1
        assert usage[0].get_value() == 1
        assert usage[0].aws_type == 'AWS::RDS::DBSnapshot'
示例#22
0
    def test_find_usage_api_keys(self):
        mock_conn = Mock()
        res = result_fixtures.ApiGateway.api_keys
        mock_paginator = Mock()
        mock_paginator.paginate.return_value = res

        mock_conn.get_paginator.return_value = mock_paginator
        cls = _ApigatewayService(21, 43)
        cls.conn = mock_conn
        with patch('%s.logger' % pbm) as mock_logger:
            cls._find_usage_api_keys()
        # API Keys usage
        usage = cls.limits['API keys per account'].get_current_usage()
        assert len(usage) == 1
        assert usage[0].get_value() == 4
        assert mock_conn.mock_calls == [
            call.get_paginator('get_api_keys'),
            call.get_paginator().paginate()
        ]
        assert mock_paginator.mock_calls == [call.paginate()]
        assert mock_logger.mock_calls == [
            call.debug('Finding usage for API Keys')
        ]
示例#23
0
    def test_find_usage_plans(self):
        mock_conn = Mock()
        res = result_fixtures.ApiGateway.plans
        mock_paginator = Mock()
        mock_paginator.paginate.return_value = res

        mock_conn.get_paginator.return_value = mock_paginator
        cls = _ApigatewayService(21, 43)
        cls.conn = mock_conn
        with patch('%s.logger' % pbm) as mock_logger:
            cls._find_usage_plans()
        # APIs usage
        usage = cls.limits['Usage plans per account'].get_current_usage()
        assert len(usage) == 1
        assert usage[0].get_value() == 4
        assert mock_conn.mock_calls == [
            call.get_paginator('get_usage_plans'),
            call.get_paginator().paginate()
        ]
        assert mock_paginator.mock_calls == [call.paginate()]
        assert mock_logger.mock_calls == [
            call.debug('Finding usage for Usage Plans')
        ]
示例#24
0
    def test_other_exception(self):
        mock_paginator = Mock()
        mock_paginator.paginate.side_effect = ClientError(
            {'Error': {
                'Code': 'SomeOtherError',
                'Message': 'My message.'
            }}, 'ListServiceQuotas')
        mock_conn = Mock()
        mock_conn.get_paginator.return_value = mock_paginator

        def se_connect(cls):
            cls.conn = mock_conn

        with patch('%s.connect' % pb, autospec=True) as m_connect:
            m_connect.side_effect = se_connect
            with pytest.raises(ClientError):
                self.cls.quotas_for_service('scode')
        assert self.cls._cache == {'scode': {}}
        assert m_connect.mock_calls == [call(self.cls)]
        assert mock_conn.mock_calls == [
            call.get_paginator('list_service_quotas'),
            call.get_paginator().paginate(ServiceCode='scode')
        ]
    def test_find_usage_security_groups_exception(self):
        """test find usage for security groups"""
        err_resp = {
            'ResponseMetadata': {
                'HTTPStatusCode': 400,
                'RequestId': '7d74c6f0-c789-11e5-82fe-a96cdaa6d564'
            },
            'Error': {
                'Message': 'other message',
                'Code': 'OtherCode',
                'Type': 'Sender'
            }
        }
        exc = ClientError(err_resp, 'operation')

        def se_exc(*args, **kwargs):
            raise exc

        mock_conn = Mock()
        mock_paginator = Mock()
        mock_paginator.paginate.side_effect = se_exc
        mock_conn.get_paginator.return_value = mock_paginator

        cls = _ElastiCacheService(21, 43)
        cls.conn = mock_conn

        with pytest.raises(Exception) as raised:
            cls._find_usage_security_groups()

        assert mock_conn.mock_calls == [
            call.get_paginator('describe_cache_security_groups'),
            call.get_paginator().paginate()
        ]
        assert mock_paginator.mock_calls == [
            call.paginate()
        ]
        assert raised.value == exc
    def test_find_usage_nodes(self):
        """test find usage for nodes"""
        # this also tests pagination
        responses = result_fixtures.ElastiCache.test_find_usage_nodes

        mock_conn = Mock()
        mock_paginator = Mock()
        mock_paginator.paginate.return_value = responses
        mock_conn.get_paginator.return_value = mock_paginator
        cls = _ElastiCacheService(21, 43)
        cls.conn = mock_conn
        cls._find_usage_nodes()

        usage = cls.limits['Nodes'].get_current_usage()
        assert len(usage) == 1
        assert usage[0].get_value() == 7

        usage = cls.limits['Clusters'].get_current_usage()
        assert len(usage) == 1
        assert usage[0].get_value() == 3

        usage = sorted(cls.limits['Nodes per Cluster'].get_current_usage())
        assert len(usage) == 3
        assert usage[0].get_value() == 1
        assert usage[0].resource_id == 'memcached1'
        assert usage[1].get_value() == 2
        assert usage[1].resource_id == 'redis1'
        assert usage[2].get_value() == 4
        assert usage[2].resource_id == 'redis2'

        assert mock_conn.mock_calls == [
            call.get_paginator('describe_cache_clusters'),
            call.get_paginator().paginate(ShowCacheNodeInfo=True)
        ]
        assert mock_paginator.mock_calls == [
            call.paginate(ShowCacheNodeInfo=True)
        ]
示例#27
0
    def test_find_usage_certs(self):
        mock_conn = Mock()
        res = result_fixtures.ApiGateway.certs
        mock_paginator = Mock()
        mock_paginator.paginate.return_value = res

        mock_conn.get_paginator.return_value = mock_paginator
        cls = _ApigatewayService(21, 43, {}, None)
        cls.conn = mock_conn
        with patch('%s.logger' % pbm) as mock_logger:
            cls._find_usage_certs()
        # APIs usage
        usage = cls.limits[
            'Client certificates per account'].get_current_usage()
        assert len(usage) == 1
        assert usage[0].get_value() == 2
        assert mock_conn.mock_calls == [
            call.get_paginator('get_client_certificates'),
            call.get_paginator().paginate()
        ]
        assert mock_paginator.mock_calls == [call.paginate()]
        assert mock_logger.mock_calls == [
            call.debug('Finding usage for Client Certificates')
        ]
示例#28
0
    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'
示例#29
0
    def test_find_usage_apis(self):
        mock_conn = Mock()
        res = result_fixtures.ApiGateway.get_rest_apis
        mock_paginator = Mock()
        mock_paginator.paginate.return_value = res

        def se_res_paginate(restApiId=None):
            return result_fixtures.ApiGateway.get_resources[restApiId]

        mock_res_paginator = Mock()
        mock_res_paginator.paginate.side_effect = se_res_paginate

        def se_get_paginator(api_name):
            if api_name == 'get_rest_apis':
                return mock_paginator
            elif api_name == 'get_resources':
                return mock_res_paginator

        def se_paginate_dict(*args, **kwargs):
            if args[0] == mock_conn.get_documentation_parts:
                return result_fixtures.ApiGateway.doc_parts[
                    kwargs['restApiId']]
            if args[0] == mock_conn.get_authorizers:
                return result_fixtures.ApiGateway.authorizers[
                    kwargs['restApiId']]

        def se_get_stages(restApiId=None):
            return result_fixtures.ApiGateway.stages[restApiId]

        mock_conn.get_paginator.side_effect = se_get_paginator
        mock_conn.get_stages.side_effect = se_get_stages
        cls = _ApigatewayService(21, 43)
        cls.conn = mock_conn
        with patch('%s.paginate_dict' % pbm, autospec=True) as mock_pd:
            with patch('%s.logger' % pbm) as mock_logger:
                mock_pd.side_effect = se_paginate_dict
                cls._find_usage_apis()
        # APIs usage
        usage = cls.limits['APIs per account'].get_current_usage()
        assert len(usage) == 1
        assert usage[0].get_value() == 3
        # Resources usage
        usage = cls.limits['Resources per API'].get_current_usage()
        assert len(usage) == 3
        assert usage[0].resource_id == 'api3'
        assert usage[0].get_value() == 0
        assert usage[1].resource_id == 'api2'
        assert usage[1].get_value() == 2
        assert usage[2].resource_id == 'api1'
        assert usage[2].get_value() == 3
        usage = cls.limits['Documentation parts per API'].get_current_usage()
        assert len(usage) == 3
        assert usage[0].resource_id == 'api3'
        assert usage[0].get_value() == 2
        assert usage[1].resource_id == 'api2'
        assert usage[1].get_value() == 1
        assert usage[2].resource_id == 'api1'
        assert usage[2].get_value() == 4
        usage = cls.limits['Stages per API'].get_current_usage()
        assert len(usage) == 3
        assert usage[0].resource_id == 'api3'
        assert usage[0].get_value() == 2
        assert usage[1].resource_id == 'api2'
        assert usage[1].get_value() == 1
        assert usage[2].resource_id == 'api1'
        assert usage[2].get_value() == 3
        usage = cls.limits['Custom authorizers per API'].get_current_usage()
        assert len(usage) == 3
        assert usage[0].resource_id == 'api3'
        assert usage[0].get_value() == 0
        assert usage[1].resource_id == 'api2'
        assert usage[1].get_value() == 2
        assert usage[2].resource_id == 'api1'
        assert usage[2].get_value() == 1
        assert mock_conn.mock_calls == [
            call.get_paginator('get_rest_apis'),
            call.get_paginator('get_resources'),
            call.get_stages(restApiId='api3'),
            call.get_paginator('get_resources'),
            call.get_stages(restApiId='api2'),
            call.get_paginator('get_resources'),
            call.get_stages(restApiId='api1')
        ]
        assert mock_paginator.mock_calls == [call.paginate()]
        assert mock_res_paginator.mock_calls == [
            call.paginate(restApiId='api3'),
            call.paginate(restApiId='api2'),
            call.paginate(restApiId='api1')
        ]
        assert mock_pd.mock_calls == [
            call(mock_conn.get_documentation_parts,
                 restApiId='api3',
                 alc_marker_path=['position'],
                 alc_data_path=['items'],
                 alc_marker_param='position'),
            call(mock_conn.get_authorizers,
                 restApiId='api3',
                 alc_marker_path=['position'],
                 alc_data_path=['items'],
                 alc_marker_param='position'),
            call(mock_conn.get_documentation_parts,
                 restApiId='api2',
                 alc_marker_path=['position'],
                 alc_data_path=['items'],
                 alc_marker_param='position'),
            call(mock_conn.get_authorizers,
                 restApiId='api2',
                 alc_marker_path=['position'],
                 alc_data_path=['items'],
                 alc_marker_param='position'),
            call(mock_conn.get_documentation_parts,
                 restApiId='api1',
                 alc_marker_path=['position'],
                 alc_data_path=['items'],
                 alc_marker_param='position'),
            call(mock_conn.get_authorizers,
                 restApiId='api1',
                 alc_marker_path=['position'],
                 alc_data_path=['items'],
                 alc_marker_param='position'),
        ]
        assert mock_logger.mock_calls == [
            call.debug('Finding usage for APIs'),
            call.debug('Found %d APIs', 3),
            call.debug('Finding usage for per-API limits')
        ]
示例#30
0
    def test_find_usage_apis(self):
        mock_conn = Mock()
        res = result_fixtures.ApiGateway.get_rest_apis
        mock_paginator = Mock()
        mock_paginator.paginate.return_value = res

        def se_res_paginate(restApiId=None):
            return result_fixtures.ApiGateway.get_resources[restApiId]

        mock_res_paginator = Mock()
        mock_res_paginator.paginate.side_effect = se_res_paginate

        def se_get_paginator(api_name):
            if api_name == 'get_rest_apis':
                return mock_paginator
            elif api_name == 'get_resources':
                return mock_res_paginator

        def se_paginate_dict(*args, **kwargs):
            if args[0] == mock_conn.get_documentation_parts:
                return result_fixtures.ApiGateway.doc_parts[kwargs['restApiId']]
            if args[0] == mock_conn.get_authorizers:
                return result_fixtures.ApiGateway.authorizers[
                    kwargs['restApiId']
                ]

        def se_get_stages(restApiId=None):
            return result_fixtures.ApiGateway.stages[restApiId]

        mock_conn.get_paginator.side_effect = se_get_paginator
        mock_conn.get_stages.side_effect = se_get_stages
        cls = _ApigatewayService(21, 43)
        cls.conn = mock_conn
        with patch('%s.paginate_dict' % pbm, autospec=True) as mock_pd:
            with patch('%s.logger' % pbm) as mock_logger:
                mock_pd.side_effect = se_paginate_dict
                cls._find_usage_apis()
        # APIs usage
        usage = cls.limits['Regional APIs per account'].get_current_usage()
        assert len(usage) == 1
        assert usage[0].get_value() == 2
        usage = cls.limits['Edge APIs per account'].get_current_usage()
        assert len(usage) == 1
        assert usage[0].get_value() == 2
        usage = cls.limits['Private APIs per account'].get_current_usage()
        assert len(usage) == 1
        assert usage[0].get_value() == 1
        # Resources usage
        usage = cls.limits['Resources per API'].get_current_usage()
        assert len(usage) == 5
        assert usage[0].resource_id == 'api3'
        assert usage[0].get_value() == 0
        assert usage[1].resource_id == 'api2'
        assert usage[1].get_value() == 2
        assert usage[2].resource_id == 'api1'
        assert usage[2].get_value() == 3
        assert usage[3].resource_id == 'api4'
        assert usage[3].get_value() == 0
        assert usage[4].resource_id == 'api5'
        assert usage[4].get_value() == 0
        usage = cls.limits['Documentation parts per API'].get_current_usage()
        assert len(usage) == 5
        assert usage[0].resource_id == 'api3'
        assert usage[0].get_value() == 2
        assert usage[1].resource_id == 'api2'
        assert usage[1].get_value() == 1
        assert usage[2].resource_id == 'api1'
        assert usage[2].get_value() == 4
        assert usage[3].resource_id == 'api4'
        assert usage[3].get_value() == 1
        assert usage[4].resource_id == 'api5'
        assert usage[4].get_value() == 1
        usage = cls.limits['Stages per API'].get_current_usage()
        assert len(usage) == 5
        assert usage[0].resource_id == 'api3'
        assert usage[0].get_value() == 2
        assert usage[1].resource_id == 'api2'
        assert usage[1].get_value() == 1
        assert usage[2].resource_id == 'api1'
        assert usage[2].get_value() == 3
        assert usage[3].resource_id == 'api4'
        assert usage[3].get_value() == 1
        assert usage[4].resource_id == 'api5'
        assert usage[4].get_value() == 1
        usage = cls.limits['Custom authorizers per API'].get_current_usage()
        assert len(usage) == 5
        assert usage[0].resource_id == 'api3'
        assert usage[0].get_value() == 0
        assert usage[1].resource_id == 'api2'
        assert usage[1].get_value() == 2
        assert usage[2].resource_id == 'api1'
        assert usage[2].get_value() == 1
        assert usage[3].resource_id == 'api4'
        assert usage[3].get_value() == 0
        assert usage[4].resource_id == 'api5'
        assert usage[4].get_value() == 0
        assert mock_conn.mock_calls == [
            call.get_paginator('get_rest_apis'),
            call.get_paginator('get_resources'),
            call.get_stages(restApiId='api3'),
            call.get_paginator('get_resources'),
            call.get_stages(restApiId='api2'),
            call.get_paginator('get_resources'),
            call.get_stages(restApiId='api1'),
            call.get_paginator('get_resources'),
            call.get_stages(restApiId='api4'),
            call.get_paginator('get_resources'),
            call.get_stages(restApiId='api5')
        ]
        assert mock_paginator.mock_calls == [call.paginate()]
        assert mock_res_paginator.mock_calls == [
            call.paginate(restApiId='api3'),
            call.paginate(restApiId='api2'),
            call.paginate(restApiId='api1'),
            call.paginate(restApiId='api4'),
            call.paginate(restApiId='api5')
        ]
        assert mock_pd.mock_calls == [
            call(
                mock_conn.get_documentation_parts,
                restApiId='api3',
                alc_marker_path=['position'],
                alc_data_path=['items'],
                alc_marker_param='position'
            ),
            call(
                mock_conn.get_authorizers,
                restApiId='api3',
                alc_marker_path=['position'],
                alc_data_path=['items'],
                alc_marker_param='position'
            ),
            call(
                mock_conn.get_documentation_parts,
                restApiId='api2',
                alc_marker_path=['position'],
                alc_data_path=['items'],
                alc_marker_param='position'
            ),
            call(
                mock_conn.get_authorizers,
                restApiId='api2',
                alc_marker_path=['position'],
                alc_data_path=['items'],
                alc_marker_param='position'
            ),
            call(
                mock_conn.get_documentation_parts,
                restApiId='api1',
                alc_marker_path=['position'],
                alc_data_path=['items'],
                alc_marker_param='position'
            ),
            call(
                mock_conn.get_authorizers,
                restApiId='api1',
                alc_marker_path=['position'],
                alc_data_path=['items'],
                alc_marker_param='position'
            ),
            call(
                mock_conn.get_documentation_parts,
                restApiId='api4',
                alc_marker_path=['position'],
                alc_data_path=['items'],
                alc_marker_param='position'
            ),
            call(
                mock_conn.get_authorizers,
                restApiId='api4',
                alc_marker_path=['position'],
                alc_data_path=['items'],
                alc_marker_param='position'
            ),
            call(
                mock_conn.get_documentation_parts,
                restApiId='api5',
                alc_marker_path=['position'],
                alc_data_path=['items'],
                alc_marker_param='position'
            ),
            call(
                mock_conn.get_authorizers,
                restApiId='api5',
                alc_marker_path=['position'],
                alc_data_path=['items'],
                alc_marker_param='position'
            )
        ]
        assert mock_logger.mock_calls == [
            call.debug('Finding usage for APIs'),
            call.debug('Found %d APIs', 5),
            call.debug('Finding usage for per-API limits')
        ]
示例#31
0
    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')
        ]