Beispiel #1
0
    def test_find_usage(self):
        mock_conn = Mock()
        sn = {
            'sn-1': 'az1',
            'sn-2': 'az2'
        }

        with patch('%s.connect' % self.pb) as mock_connect:
            with patch.multiple(
                    self.pb,
                    _find_usage_vpcs=DEFAULT,
                    _find_usage_subnets=DEFAULT,
                    _find_usage_ACLs=DEFAULT,
                    _find_usage_route_tables=DEFAULT,
                    _find_usage_gateways=DEFAULT,
                    _find_usage_nat_gateways=DEFAULT,
            ) as mocks:
                mocks['_find_usage_subnets'].return_value = sn
                cls = _VpcService(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 == []
        for x in [
                '_find_usage_vpcs',
                '_find_usage_subnets',
                '_find_usage_ACLs',
                '_find_usage_route_tables',
                '_find_usage_gateways'
        ]:
            assert mocks[x].mock_calls == [call()]
        assert mocks['_find_usage_nat_gateways'].mock_calls == [call(sn)]
Beispiel #2
0
    def test_find_usage_route_tables(self):
        response = result_fixtures.VPC.test_find_usage_route_tables

        mock_conn = Mock()
        mock_conn.describe_route_tables.return_value = response

        cls = _VpcService(21, 43, {}, None)
        cls._current_account_id = '0123456789'
        cls.conn = mock_conn

        cls._find_usage_route_tables()

        usage = sorted(cls.limits['Route tables per VPC'].get_current_usage())
        assert len(usage) == 2
        assert usage[0].get_value() == 1
        assert usage[0].resource_id == 'vpc-2'
        assert usage[1].get_value() == 2
        assert usage[1].resource_id == 'vpc-1'
        entries = sorted(cls.limits['Entries per route '
                                    'table'].get_current_usage())
        assert len(entries) == 3
        assert entries[0].resource_id == 'rt-2'
        assert entries[0].get_value() == 1
        assert entries[1].resource_id == 'rt-1'
        assert entries[1].get_value() == 2
        assert entries[2].resource_id == 'rt-3'
        assert entries[2].get_value() == 3
        assert mock_conn.mock_calls == [
            call.describe_route_tables(Filters=[{
                'Name': 'owner-id',
                'Values': ['0123456789']
            }])
        ]
    def test_find_usage(self):
        mock_conn = Mock()

        with patch('%s.connect' % self.pb) as mock_connect:
            with patch.multiple(
                    self.pb,
                    _find_usage_vpcs=DEFAULT,
                    _find_usage_subnets=DEFAULT,
                    _find_usage_ACLs=DEFAULT,
                    _find_usage_route_tables=DEFAULT,
                    _find_usage_gateways=DEFAULT,
            ) as mocks:
                cls = _VpcService(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 == []
        for x in [
                '_find_usage_vpcs',
                '_find_usage_subnets',
                '_find_usage_ACLs',
                '_find_usage_route_tables',
                '_find_usage_gateways',
        ]:
            assert mocks[x].mock_calls == [call()]
 def test_init(self):
     """test __init__()"""
     cls = _VpcService(21, 43)
     assert cls.service_name == 'VPC'
     assert cls.conn is None
     assert cls.warning_threshold == 21
     assert cls.critical_threshold == 43
 def test_get_limits_again(self):
     """test that existing limits dict is returned on subsequent calls"""
     mock_limits = Mock()
     cls = _VpcService(21, 43)
     cls.limits = mock_limits
     res = cls.get_limits()
     assert res == mock_limits
Beispiel #6
0
    def test_find_usage(self):
        mock_conn = Mock()
        sn = {'sn-1': 'az1', 'sn-2': 'az2'}

        with patch('%s.connect' % self.pb) as mock_connect:
            with patch.multiple(
                    self.pb,
                    _find_usage_vpcs=DEFAULT,
                    _find_usage_subnets=DEFAULT,
                    _find_usage_ACLs=DEFAULT,
                    _find_usage_route_tables=DEFAULT,
                    _find_usage_gateways=DEFAULT,
                    _find_usage_nat_gateways=DEFAULT,
                    _find_usages_vpn_gateways=DEFAULT,
                    _find_usage_network_interfaces=DEFAULT,
            ) as mocks:
                mocks['_find_usage_subnets'].return_value = sn
                cls = _VpcService(21, 43, {}, None)
                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 == []
        for x in [
                '_find_usage_vpcs',
                '_find_usage_subnets',
                '_find_usage_ACLs',
                '_find_usage_route_tables',
                '_find_usage_gateways',
                '_find_usages_vpn_gateways',
                '_find_usage_network_interfaces',
        ]:
            assert mocks[x].mock_calls == [call()]
        assert mocks['_find_usage_nat_gateways'].mock_calls == [call(sn)]
Beispiel #7
0
    def test_find_usage_nat_gateways_exception(self):
        subnets = result_fixtures.VPC.test_find_usage_nat_gateways_subnets

        def se_exc(*args, **kwargs):
            raise ClientError({'Error': {}}, 'opname')

        mock_conn = Mock()
        mock_conn.describe_nat_gateways.side_effect = se_exc

        cls = _VpcService(21, 43, {}, None)
        cls._current_account_id = '0123456789'
        cls.conn = mock_conn

        with patch('%s.logger' % self.pbm, autospec=True) as mock_logger:
            cls._find_usage_nat_gateways(subnets)

        assert len(cls.limits['NAT Gateways per AZ'].get_current_usage()) == 0
        assert mock_conn.mock_calls == [
            call.describe_nat_gateways(),
        ]
        assert mock_logger.mock_calls == [
            call.error(
                'Caught exception when trying to list NAT Gateways; '
                'perhaps NAT service does not exist in this region?',
                exc_info=1)
        ]
Beispiel #8
0
    def test_find_usages_vpn_gateways(self):
        response = result_fixtures.VPC.test_find_usages_vpn_gateways

        mock_conn = Mock()
        mock_conn.describe_vpn_gateways.return_value = response

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

        cls._find_usages_vpn_gateways()

        assert len(cls.limits['Virtual private gateways']
                   .get_current_usage()) == 1
        assert cls.limits['Virtual private gateways'].get_current_usage()[
            0].get_value() == 2
        assert mock_conn.mock_calls == [
            call.describe_vpn_gateways(Filters=[
                {
                    'Name': 'state',
                    'Values': [
                        'available',
                        'pending'
                    ]
                }
            ]),
        ]
Beispiel #9
0
    def test_find_usage_nat_gateways(self):
        subnets = result_fixtures.VPC.test_find_usage_nat_gateways_subnets
        response = result_fixtures.VPC.test_find_usage_nat_gateways

        mock_conn = Mock()
        mock_conn.describe_nat_gateways.return_value = response

        with patch('%s.logger' % self.pbm) as mock_logger:
            cls = _VpcService(21, 43)
            cls.conn = mock_conn
            cls._find_usage_nat_gateways(subnets)

        assert len(cls.limits['NAT Gateways per AZ'].get_current_usage()) == 2
        az2 = cls.limits['NAT Gateways per AZ'].get_current_usage()[0]
        assert az2.get_value() == 3
        assert az2.resource_id == 'az2'
        az3 = cls.limits['NAT Gateways per AZ'].get_current_usage()[1]
        assert az3.get_value() == 1
        assert az3.resource_id == 'az3'
        assert mock_conn.mock_calls == [
            call.describe_nat_gateways(),
        ]
        assert mock_logger.mock_calls == [
            call.error(
                'ERROR: NAT Gateway %s in SubnetId %s, but SubnetId not '
                'found in subnet_to_az; Gateway cannot be counted!',
                'nat-124', 'subnet4'
            ),
            call.debug(
                'Skipping NAT Gateway %s in state: %s', 'nat-125', 'deleted'
            ),
            call.debug(
                'Skipping NAT Gateway %s in state: %s', 'nat-127', 'failed'
            )
        ]
Beispiel #10
0
    def test_find_usage_acls(self):
        response = result_fixtures.VPC.test_find_usage_acls
        mock_conn = Mock()

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

        mock_conn.describe_network_acls.return_value = response
        cls._find_usage_ACLs()

        usage = sorted(cls.limits['Network ACLs per VPC'].get_current_usage())
        assert len(usage) == 2
        assert usage[0].get_value() == 1
        assert usage[0].resource_id == 'vpc-2'
        assert usage[1].get_value() == 2
        assert usage[1].resource_id == 'vpc-1'
        entries = sorted(cls.limits['Rules per network '
                                    'ACL'].get_current_usage())
        assert len(entries) == 3
        assert entries[0].resource_id == 'acl-2'
        assert entries[0].get_value() == 1
        assert entries[1].resource_id == 'acl-1'
        assert entries[1].get_value() == 3
        assert entries[2].resource_id == 'acl-3'
        assert entries[2].get_value() == 5
        assert mock_conn.mock_calls == [
            call.describe_network_acls()
        ]
Beispiel #11
0
    def test_update_limits_from_api_low_max_instances(self):
        fixtures = result_fixtures.VPC()
        response = fixtures.test_update_limits_from_api_low_max_instances

        mock_conn = Mock()
        mock_client_conn = Mock()
        mock_client_conn.describe_account_attributes.return_value = response

        cls = _VpcService(21, 43)
        cls.resource_conn = mock_conn
        cls.conn = mock_client_conn
        with patch('awslimitchecker.services.vpc.logger') as mock_logger:
            cls._update_limits_from_api()
        assert mock_conn.mock_calls == []
        assert mock_client_conn.mock_calls == [
            call.describe_account_attributes()
        ]
        assert mock_logger.mock_calls == [
            call.info("Querying EC2 DescribeAccountAttributes for limits"),
            call.debug('Done setting limits from API')
        ]

        limit_name = 'Network interfaces per Region'
        assert cls.limits[limit_name].api_limit is None
        assert cls.limits[limit_name].get_limit() == DEFAULT_ENI_LIMIT
Beispiel #12
0
    def test_find_usage_acls(self):
        response = result_fixtures.VPC.test_find_usage_acls
        mock_conn = Mock()

        cls = _VpcService(21, 43, {}, None)
        cls._current_account_id = '0123456789'
        cls.conn = mock_conn

        mock_conn.describe_network_acls.return_value = response
        cls._find_usage_ACLs()

        usage = sorted(cls.limits['Network ACLs per VPC'].get_current_usage())
        assert len(usage) == 2
        assert usage[0].get_value() == 1
        assert usage[0].resource_id == 'vpc-2'
        assert usage[1].get_value() == 2
        assert usage[1].resource_id == 'vpc-1'
        entries = sorted(cls.limits['Rules per network '
                                    'ACL'].get_current_usage())
        assert len(entries) == 3
        assert entries[0].resource_id == 'acl-2'
        assert entries[0].get_value() == 1
        assert entries[1].resource_id == 'acl-1'
        assert entries[1].get_value() == 3
        assert entries[2].resource_id == 'acl-3'
        assert entries[2].get_value() == 5
        assert mock_conn.mock_calls == [
            call.describe_network_acls(Filters=[{
                'Name': 'owner-id',
                'Values': ['0123456789']
            }])
        ]
Beispiel #13
0
 def test_init(self):
     """test __init__()"""
     cls = _VpcService(21, 43)
     assert cls.service_name == 'VPC'
     assert cls.conn is None
     assert cls.warning_threshold == 21
     assert cls.critical_threshold == 43
Beispiel #14
0
    def test_update_limits_from_api_low_max_instances(self):
        fixtures = result_fixtures.VPC()
        response = fixtures.test_update_limits_from_api_low_max_instances

        mock_conn = Mock()
        mock_client_conn = Mock()
        mock_client_conn.describe_account_attributes.return_value = response

        cls = _VpcService(21, 43)
        cls.resource_conn = mock_conn
        cls.conn = mock_client_conn
        with patch('awslimitchecker.services.vpc.logger') as mock_logger:
            cls._update_limits_from_api()
        assert mock_conn.mock_calls == []
        assert mock_client_conn.mock_calls == [
            call.describe_account_attributes()
        ]
        assert mock_logger.mock_calls == [
            call.info("Querying EC2 DescribeAccountAttributes for limits"),
            call.debug('Done setting limits from API')
        ]

        limit_name = 'Network interfaces per Region'
        assert cls.limits[limit_name].api_limit is None
        assert cls.limits[limit_name].get_limit() == DEFAULT_ENI_LIMIT
Beispiel #15
0
 def test_get_limits_again(self):
     """test that existing limits dict is returned on subsequent calls"""
     mock_limits = Mock()
     cls = _VpcService(21, 43)
     cls.limits = mock_limits
     res = cls.get_limits()
     assert res == mock_limits
Beispiel #16
0
    def test_find_usages_vpn_gateways(self):
        response = result_fixtures.VPC.test_find_usages_vpn_gateways

        mock_conn = Mock()
        mock_conn.describe_vpn_gateways.return_value = response

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

        cls._find_usages_vpn_gateways()

        assert len(cls.limits['Virtual private gateways']
                   .get_current_usage()) == 1
        assert cls.limits['Virtual private gateways'].get_current_usage()[
            0].get_value() == 2
        assert mock_conn.mock_calls == [
            call.describe_vpn_gateways(Filters=[
                {
                    'Name': 'state',
                    'Values': [
                        'available',
                        'pending'
                    ]
                }
            ]),
        ]
    def test_find_usage_acls(self):
        response = result_fixtures.VPC.test_find_usage_acls
        mock_conn = Mock()

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

        mock_conn.describe_network_acls.return_value = response
        cls._find_usage_ACLs()

        usage = sorted(cls.limits['Network ACLs per VPC'].get_current_usage())
        assert len(usage) == 2
        assert usage[0].get_value() == 1
        assert usage[0].resource_id == 'vpc-2'
        assert usage[1].get_value() == 2
        assert usage[1].resource_id == 'vpc-1'
        entries = sorted(cls.limits['Rules per network '
                                    'ACL'].get_current_usage())
        assert len(entries) == 3
        assert entries[0].resource_id == 'acl-2'
        assert entries[0].get_value() == 1
        assert entries[1].resource_id == 'acl-1'
        assert entries[1].get_value() == 3
        assert entries[2].resource_id == 'acl-3'
        assert entries[2].get_value() == 5
        assert mock_conn.mock_calls == [
            call.describe_network_acls()
        ]
    def test_find_usage_route_tables(self):
        response = result_fixtures.VPC.test_find_usage_route_tables

        mock_conn = Mock()
        mock_conn.describe_route_tables.return_value = response

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

        cls._find_usage_route_tables()

        usage = sorted(cls.limits['Route tables per VPC'].get_current_usage())
        assert len(usage) == 2
        assert usage[0].get_value() == 1
        assert usage[0].resource_id == 'vpc-2'
        assert usage[1].get_value() == 2
        assert usage[1].resource_id == 'vpc-1'
        entries = sorted(cls.limits['Entries per route '
                                    'table'].get_current_usage())
        assert len(entries) == 3
        assert entries[0].resource_id == 'rt-2'
        assert entries[0].get_value() == 1
        assert entries[1].resource_id == 'rt-1'
        assert entries[1].get_value() == 3
        assert entries[2].resource_id == 'rt-3'
        assert entries[2].get_value() == 5
        assert mock_conn.mock_calls == [
            call.describe_route_tables()
        ]
Beispiel #19
0
    def test_find_usage_nat_gateways(self):
        subnets = result_fixtures.VPC.test_find_usage_nat_gateways_subnets
        response = result_fixtures.VPC.test_find_usage_nat_gateways

        mock_conn = Mock()
        mock_conn.describe_nat_gateways.return_value = response

        with patch('%s.logger' % self.pbm) as mock_logger:
            cls = _VpcService(21, 43)
            cls.conn = mock_conn
            cls._find_usage_nat_gateways(subnets)

        assert len(cls.limits['NAT Gateways per AZ'].get_current_usage()) == 2
        az2 = cls.limits['NAT Gateways per AZ'].get_current_usage()[0]
        assert az2.get_value() == 3
        assert az2.resource_id == 'az2'
        az3 = cls.limits['NAT Gateways per AZ'].get_current_usage()[1]
        assert az3.get_value() == 1
        assert az3.resource_id == 'az3'
        assert mock_conn.mock_calls == [
            call.describe_nat_gateways(),
        ]
        assert mock_logger.mock_calls == [
            call.error(
                'ERROR: NAT Gateway %s in SubnetId %s, but SubnetId not '
                'found in subnet_to_az; Gateway cannot be counted!',
                'nat-124', 'subnet4'
            ),
            call.debug(
                'Skipping NAT Gateway %s in state: %s', 'nat-125', 'deleted'
            ),
            call.debug(
                'Skipping NAT Gateway %s in state: %s', 'nat-127', 'failed'
            )
        ]
Beispiel #20
0
    def test_find_usage(self):
        mock_conn = Mock()

        with patch('%s.connect' % self.pb) as mock_connect:
            with patch.multiple(
                    self.pb,
                    _find_usage_vpcs=DEFAULT,
                    _find_usage_subnets=DEFAULT,
                    _find_usage_ACLs=DEFAULT,
                    _find_usage_route_tables=DEFAULT,
                    _find_usage_gateways=DEFAULT,
            ) as mocks:
                cls = _VpcService(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 == []
        for x in [
                '_find_usage_vpcs',
                '_find_usage_subnets',
                '_find_usage_ACLs',
                '_find_usage_route_tables',
                '_find_usage_gateways',
        ]:
            assert mocks[x].mock_calls == [call()]
Beispiel #21
0
    def test_find_usage_route_tables(self):
        response = result_fixtures.VPC.test_find_usage_route_tables

        mock_conn = Mock()
        mock_conn.describe_route_tables.return_value = response

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

        cls._find_usage_route_tables()

        usage = sorted(cls.limits['Route tables per VPC'].get_current_usage())
        assert len(usage) == 2
        assert usage[0].get_value() == 1
        assert usage[0].resource_id == 'vpc-2'
        assert usage[1].get_value() == 2
        assert usage[1].resource_id == 'vpc-1'
        entries = sorted(cls.limits['Entries per route '
                                    'table'].get_current_usage())
        assert len(entries) == 3
        assert entries[0].resource_id == 'rt-2'
        assert entries[0].get_value() == 1
        assert entries[1].resource_id == 'rt-1'
        assert entries[1].get_value() == 2
        assert entries[2].resource_id == 'rt-3'
        assert entries[2].get_value() == 3
        assert mock_conn.mock_calls == [
            call.describe_route_tables()
        ]
Beispiel #22
0
    def test_find_usage_subnets(self):
        mock1 = Mock(spec_set=Subnet)
        type(mock1).vpc_id = 'vpc-1'
        mock2 = Mock(spec_set=Subnet)
        type(mock2).vpc_id = 'vpc-1'
        mock3 = Mock(spec_set=Subnet)
        type(mock3).vpc_id = 'vpc-2'

        subnets = [mock1, mock2, mock3]
        mock_conn = Mock(spec_set=VPCConnection)
        cls = _VpcService(21, 43)
        cls.conn = mock_conn

        with patch('%s.boto_query_wrapper' % self.pbm) as mock_wrapper:
            mock_wrapper.return_value = subnets
            cls._find_usage_subnets()

        usage = sorted(cls.limits['Subnets per VPC'].get_current_usage())
        assert len(usage) == 2
        assert usage[0].get_value() == 1
        assert usage[0].resource_id == 'vpc-2'
        assert usage[1].get_value() == 2
        assert usage[1].resource_id == 'vpc-1'
        assert mock_conn.mock_calls == []
        assert mock_wrapper.mock_calls == [call(mock_conn.get_all_subnets)]
 def test_required_iam_permissions(self):
     cls = _VpcService(21, 43)
     assert cls.required_iam_permissions() == [
         'ec2:DescribeNetworkAcls',
         'ec2:DescribeRouteTables',
         'ec2:DescribeSubnets',
         'ec2:DescribeVpcs',
     ]
Beispiel #24
0
 def test_required_iam_permissions(self):
     cls = _VpcService(21, 43)
     assert cls.required_iam_permissions() == [
         'ec2:DescribeNetworkAcls',
         'ec2:DescribeRouteTables',
         'ec2:DescribeSubnets',
         'ec2:DescribeVpcs',
     ]
Beispiel #25
0
 def test_connect(self):
     """test connect()"""
     mock_conn = Mock()
     cls = _VpcService(21, 43)
     with patch('awslimitchecker.services.vpc.boto.connect_vpc') as mock_vpc:
         mock_vpc.return_value = mock_conn
         cls.connect()
     assert mock_vpc.mock_calls == [call()]
     assert mock_conn.mock_calls == []
Beispiel #26
0
 def test_connect_again(self):
     """make sure we re-use the connection"""
     mock_conn = Mock()
     cls = _VpcService(21, 43)
     cls.conn = mock_conn
     with patch('awslimitchecker.services.vpc.boto.connect_vpc') as mock_vpc:
         mock_vpc.return_value = mock_conn
         cls.connect()
     assert mock_vpc.mock_calls == []
     assert mock_conn.mock_calls == []
Beispiel #27
0
 def test_required_iam_permissions(self):
     cls = _VpcService(21, 43, {}, None)
     assert cls.required_iam_permissions() == [
         'ec2:DescribeNatGateways',
         'ec2:DescribeNetworkAcls',
         'ec2:DescribeRouteTables',
         'ec2:DescribeSubnets',
         'ec2:DescribeVpcs',
         'ec2:DescribeVpnGateways',
         'ec2:DescribeNetworkInterfaces',
     ]
Beispiel #28
0
    def test_find_usage_vpcs(self):
        response = result_fixtures.VPC.test_find_usage_vpcs

        mock_conn = Mock()
        mock_conn.describe_vpcs.return_value = response

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

        cls._find_usage_vpcs()

        assert len(cls.limits['VPCs'].get_current_usage()) == 1
        assert cls.limits['VPCs'].get_current_usage()[0].get_value() == 2
        assert mock_conn.mock_calls == [call.describe_vpcs()]
Beispiel #29
0
 def test_connect_region(self):
     """test connect()"""
     mock_conn = Mock()
     mock_conn_via = Mock()
     cls = _VpcService(21, 43, region='foo')
     with patch('%s.boto.connect_vpc' % self.pbm) as mock_vpc:
         with patch('%s.connect_via' % self.pb) as mock_connect_via:
             mock_vpc.return_value = mock_conn
             mock_connect_via.return_value = mock_conn_via
             cls.connect()
     assert mock_vpc.mock_calls == []
     assert mock_conn.mock_calls == []
     assert mock_connect_via.mock_calls == [call(connect_to_region)]
     assert cls.conn == mock_conn_via
Beispiel #30
0
 def test_connect_again(self):
     """test connect()"""
     mock_conn = Mock()
     mock_conn_via = Mock()
     cls = _VpcService(21, 43)
     cls.conn = mock_conn
     with patch('%s.boto.connect_vpc' % self.pbm) as mock_vpc:
         with patch('%s.connect_via' % self.pb) as mock_connect_via:
             mock_vpc.return_value = mock_conn
             mock_connect_via.return_value = mock_conn_via
             cls.connect()
     assert mock_vpc.mock_calls == []
     assert mock_conn.mock_calls == []
     assert mock_connect_via.mock_calls == []
     assert cls.conn == mock_conn
Beispiel #31
0
    def test_find_usage_internet_gateways(self):
        response = result_fixtures.VPC.test_find_usage_internet_gateways

        mock_conn = Mock()
        mock_conn.describe_internet_gateways.return_value = response

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

        cls._find_usage_gateways()

        assert len(cls.limits['Internet gateways'].get_current_usage()) == 1
        assert cls.limits['Internet gateways'].get_current_usage(
        )[0].get_value() == 2
        assert mock_conn.mock_calls == [call.describe_internet_gateways()]
    def test_find_usage_vpcs(self):
        response = result_fixtures.VPC.test_find_usage_vpcs

        mock_conn = Mock()
        mock_conn.describe_vpcs.return_value = response

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

        cls._find_usage_vpcs()

        assert len(cls.limits['VPCs'].get_current_usage()) == 1
        assert cls.limits['VPCs'].get_current_usage()[0].get_value() == 2
        assert mock_conn.mock_calls == [
            call.describe_vpcs()
        ]
 def test_get_limits(self):
     cls = _VpcService(21, 43)
     cls.limits = {}
     res = cls.get_limits()
     assert sorted(res.keys()) == sorted([
         'Entries per route table',
         'Internet gateways',
         'VPCs',
         'Subnets per VPC',
         'Network ACLs per VPC',
         'Rules per network ACL',
         'Route tables per VPC',
     ])
     for name, limit in res.items():
         assert limit.service == cls
         assert limit.def_warning_threshold == 21
         assert limit.def_critical_threshold == 43
Beispiel #34
0
 def test_get_limits(self):
     cls = _VpcService(21, 43)
     cls.limits = {}
     res = cls.get_limits()
     assert sorted(res.keys()) == sorted([
         'Entries per route table',
         'Internet gateways',
         'VPCs',
         'Subnets per VPC',
         'Network ACLs per VPC',
         'Rules per network ACL',
         'Route tables per VPC',
     ])
     for name, limit in res.items():
         assert limit.service == cls
         assert limit.def_warning_threshold == 21
         assert limit.def_critical_threshold == 43
Beispiel #35
0
    def test_find_usage_subnets(self):
        response = result_fixtures.VPC.test_find_usage_subnets

        mock_conn = Mock()
        mock_conn.describe_subnets.return_value = response
        cls = _VpcService(21, 43)
        cls.conn = mock_conn

        cls._find_usage_subnets()

        usage = sorted(cls.limits['Subnets per VPC'].get_current_usage())
        assert len(usage) == 2
        assert usage[0].get_value() == 1
        assert usage[0].resource_id == 'vpc-2'
        assert usage[1].get_value() == 2
        assert usage[1].resource_id == 'vpc-1'
        assert mock_conn.mock_calls == [call.describe_subnets()]
Beispiel #36
0
    def test_find_usage_network_interfaces(self):
        response = result_fixtures.VPC.test_find_usage_network_interfaces

        mock_conn = Mock()
        mock_conn.describe_network_interfaces.return_value = response

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

        cls._find_usage_network_interfaces()

        assert len(cls.limits['Network interfaces per Region']
                   .get_current_usage()) == 1
        assert cls.limits['Network interfaces per Region'].get_current_usage()[
            0].get_value() == 1
        assert mock_conn.mock_calls == [
            call.describe_network_interfaces(),
        ]
Beispiel #37
0
    def test_find_usage_network_interfaces(self):
        response = result_fixtures.VPC.test_find_usage_network_interfaces

        mock_conn = Mock()
        mock_conn.describe_network_interfaces.return_value = response

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

        cls._find_usage_network_interfaces()

        assert len(cls.limits['Network interfaces per Region']
                   .get_current_usage()) == 1
        assert cls.limits['Network interfaces per Region'].get_current_usage()[
            0].get_value() == 1
        assert mock_conn.mock_calls == [
            call.describe_network_interfaces(),
        ]
Beispiel #38
0
    def test_find_usage_route_tables(self):
        mock1 = Mock(spec_set=RouteTable)
        type(mock1).id = 'rt-1'
        type(mock1).vpc_id = 'vpc-1'
        type(mock1).routes = [1, 2, 3]
        mock2 = Mock(spec_set=RouteTable)
        type(mock2).id = 'rt-2'
        type(mock2).vpc_id = 'vpc-1'
        type(mock2).routes = [1]
        mock3 = Mock(spec_set=RouteTable)
        type(mock3).id = 'rt-3'
        type(mock3).vpc_id = 'vpc-2'
        type(mock3).routes = [1, 2, 3, 4, 5]

        tables = [mock1, mock2, mock3]

        mock_conn = Mock(spec_set=VPCConnection)

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

        with patch('%s.boto_query_wrapper' % self.pbm) as mock_wrapper:
            mock_wrapper.return_value = tables
            cls._find_usage_route_tables()

        usage = sorted(cls.limits['Route tables per VPC'].get_current_usage())
        assert len(usage) == 2
        assert usage[0].get_value() == 1
        assert usage[0].resource_id == 'vpc-2'
        assert usage[1].get_value() == 2
        assert usage[1].resource_id == 'vpc-1'
        entries = sorted(cls.limits['Entries per route '
                                    'table'].get_current_usage())
        assert len(entries) == 3
        assert entries[0].resource_id == 'rt-2'
        assert entries[0].get_value() == 1
        assert entries[1].resource_id == 'rt-1'
        assert entries[1].get_value() == 3
        assert entries[2].resource_id == 'rt-3'
        assert entries[2].get_value() == 5
        assert mock_conn.mock_calls == []
        assert mock_wrapper.mock_calls == [
            call(mock_conn.get_all_route_tables)
        ]
Beispiel #39
0
    def test_find_usage_vpcs(self):
        mock1 = Mock(spec_set=VPC)
        type(mock1).id = 'vpc-1'
        mock2 = Mock(spec_set=VPC)
        type(mock2).id = 'vpc-2'

        vpcs = [mock1, mock2]

        mock_conn = Mock(spec_set=VPCConnection)
        mock_conn.get_all_vpcs.return_value = vpcs

        cls = _VpcService(21, 43)
        cls.conn = mock_conn
        cls._find_usage_vpcs()
        assert len(cls.limits['VPCs'].get_current_usage()) == 1
        assert cls.limits['VPCs'].get_current_usage()[0].get_value() == 2
        assert mock_conn.mock_calls == [
            call.get_all_vpcs(),
        ]
    def test_find_usage_subnets(self):
        response = result_fixtures.VPC.test_find_usage_subnets

        mock_conn = Mock()
        mock_conn.describe_subnets.return_value = response
        cls = _VpcService(21, 43)
        cls.conn = mock_conn

        cls._find_usage_subnets()

        usage = sorted(cls.limits['Subnets per VPC'].get_current_usage())
        assert len(usage) == 2
        assert usage[0].get_value() == 1
        assert usage[0].resource_id == 'vpc-2'
        assert usage[1].get_value() == 2
        assert usage[1].resource_id == 'vpc-1'
        assert mock_conn.mock_calls == [
            call.describe_subnets()
        ]
Beispiel #41
0
    def test_find_usage_acls(self):
        mock1 = Mock(spec_set=NetworkAcl)
        type(mock1).id = 'acl-1'
        type(mock1).vpc_id = 'vpc-1'
        type(mock1).network_acl_entries = [1, 2, 3]
        mock2 = Mock(spec_set=NetworkAcl)
        type(mock2).id = 'acl-2'
        type(mock2).vpc_id = 'vpc-1'
        type(mock2).network_acl_entries = [1]
        mock3 = Mock(spec_set=NetworkAcl)
        type(mock3).id = 'acl-3'
        type(mock3).vpc_id = 'vpc-2'
        type(mock3).network_acl_entries = [1, 2, 3, 4, 5]

        acls = [mock1, mock2, mock3]
        mock_conn = Mock(spec_set=VPCConnection)

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

        with patch('%s.boto_query_wrapper' % self.pbm) as mock_wrapper:
            mock_wrapper.return_value = acls
            cls._find_usage_ACLs()

        usage = sorted(cls.limits['Network ACLs per VPC'].get_current_usage())
        assert len(usage) == 2
        assert usage[0].get_value() == 1
        assert usage[0].resource_id == 'vpc-2'
        assert usage[1].get_value() == 2
        assert usage[1].resource_id == 'vpc-1'
        entries = sorted(cls.limits['Rules per network '
                                    'ACL'].get_current_usage())
        assert len(entries) == 3
        assert entries[0].resource_id == 'acl-2'
        assert entries[0].get_value() == 1
        assert entries[1].resource_id == 'acl-1'
        assert entries[1].get_value() == 3
        assert entries[2].resource_id == 'acl-3'
        assert entries[2].get_value() == 5
        assert mock_conn.mock_calls == []
        assert mock_wrapper.mock_calls == [
            call(mock_conn.get_all_network_acls)
        ]
Beispiel #42
0
    def test_find_usage_internet_gateways(self):
        mock1 = Mock(spec_set=InternetGateway)
        type(mock1).id = 'gw-1'
        mock2 = Mock(spec_set=InternetGateway)
        type(mock2).id = 'gw-2'

        gateways = [mock1, mock2]

        mock_conn = Mock(spec_set=VPCConnection)
        mock_conn.get_all_internet_gateways.return_value = gateways

        cls = _VpcService(21, 43)
        cls.conn = mock_conn
        cls._find_usage_gateways()
        assert len(cls.limits['Internet gateways'].get_current_usage()) == 1
        assert cls.limits['Internet gateways'].get_current_usage()[
            0].get_value() == 2
        assert mock_conn.mock_calls == [
            call.get_all_internet_gateways(),
        ]
Beispiel #43
0
    def test_find_usage_vpcs(self):
        response = result_fixtures.VPC.test_find_usage_vpcs

        mock_conn = Mock()
        mock_conn.describe_vpcs.return_value = response

        cls = _VpcService(21, 43, {}, None)
        cls._current_account_id = '0123456789'
        cls.conn = mock_conn

        cls._find_usage_vpcs()

        assert len(cls.limits['VPCs'].get_current_usage()) == 1
        assert cls.limits['VPCs'].get_current_usage()[0].get_value() == 2
        assert mock_conn.mock_calls == [
            call.describe_vpcs(Filters=[{
                'Name': 'owner-id',
                'Values': ['0123456789']
            }])
        ]
Beispiel #44
0
    def test_find_usage_vpcs(self):
        mock1 = Mock(spec_set=VPC)
        type(mock1).id = 'vpc-1'
        mock2 = Mock(spec_set=VPC)
        type(mock2).id = 'vpc-2'

        vpcs = [mock1, mock2]

        mock_conn = Mock(spec_set=VPCConnection)

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

        with patch('%s.boto_query_wrapper' % self.pbm) as mock_wrapper:
            mock_wrapper.return_value = vpcs
            cls._find_usage_vpcs()

        assert len(cls.limits['VPCs'].get_current_usage()) == 1
        assert cls.limits['VPCs'].get_current_usage()[0].get_value() == 2
        assert mock_conn.mock_calls == []
        assert mock_wrapper.mock_calls == [call(mock_conn.get_all_vpcs)]
Beispiel #45
0
    def test_find_usage_route_tables(self):
        mock1 = Mock(spec_set=RouteTable)
        type(mock1).id = 'rt-1'
        type(mock1).vpc_id = 'vpc-1'
        type(mock1).routes = [1, 2, 3]
        mock2 = Mock(spec_set=RouteTable)
        type(mock2).id = 'rt-2'
        type(mock2).vpc_id = 'vpc-1'
        type(mock2).routes = [1]
        mock3 = Mock(spec_set=RouteTable)
        type(mock3).id = 'rt-3'
        type(mock3).vpc_id = 'vpc-2'
        type(mock3).routes = [1, 2, 3, 4, 5]

        tables = [mock1, mock2, mock3]

        mock_conn = Mock(spec_set=VPCConnection)
        mock_conn.get_all_route_tables.return_value = tables

        cls = _VpcService(21, 43)
        cls.conn = mock_conn
        cls._find_usage_route_tables()
        usage = sorted(cls.limits['Route tables per VPC'].get_current_usage())
        assert len(usage) == 2
        assert usage[0].get_value() == 1
        assert usage[0].resource_id == 'vpc-2'
        assert usage[1].get_value() == 2
        assert usage[1].resource_id == 'vpc-1'
        entries = sorted(cls.limits['Entries per route '
                                    'table'].get_current_usage())
        assert len(entries) == 3
        assert entries[0].resource_id == 'rt-2'
        assert entries[0].get_value() == 1
        assert entries[1].resource_id == 'rt-1'
        assert entries[1].get_value() == 3
        assert entries[2].resource_id == 'rt-3'
        assert entries[2].get_value() == 5
        assert mock_conn.mock_calls == [
            call.get_all_route_tables(),
        ]
Beispiel #46
0
    def test_find_usage_acls(self):
        mock1 = Mock(spec_set=NetworkAcl)
        type(mock1).id = 'acl-1'
        type(mock1).vpc_id = 'vpc-1'
        type(mock1).network_acl_entries = [1, 2, 3]
        mock2 = Mock(spec_set=NetworkAcl)
        type(mock2).id = 'acl-2'
        type(mock2).vpc_id = 'vpc-1'
        type(mock2).network_acl_entries = [1]
        mock3 = Mock(spec_set=NetworkAcl)
        type(mock3).id = 'acl-3'
        type(mock3).vpc_id = 'vpc-2'
        type(mock3).network_acl_entries = [1, 2, 3, 4, 5]

        acls = [mock1, mock2, mock3]
        mock_conn = Mock(spec_set=VPCConnection)
        mock_conn.get_all_network_acls.return_value = acls

        cls = _VpcService(21, 43)
        cls.conn = mock_conn
        cls._find_usage_ACLs()
        usage = sorted(cls.limits['Network ACLs per VPC'].get_current_usage())
        assert len(usage) == 2
        assert usage[0].get_value() == 1
        assert usage[0].resource_id == 'vpc-2'
        assert usage[1].get_value() == 2
        assert usage[1].resource_id == 'vpc-1'
        entries = sorted(cls.limits['Rules per network '
                                    'ACL'].get_current_usage())
        assert len(entries) == 3
        assert entries[0].resource_id == 'acl-2'
        assert entries[0].get_value() == 1
        assert entries[1].resource_id == 'acl-1'
        assert entries[1].get_value() == 3
        assert entries[2].resource_id == 'acl-3'
        assert entries[2].get_value() == 5
        assert mock_conn.mock_calls == [
            call.get_all_network_acls(),
        ]
Beispiel #47
0
    def test_find_usage_nat_gateways(self):
        subnets = result_fixtures.VPC.test_find_usage_nat_gateways_subnets
        response = result_fixtures.VPC.test_find_usage_nat_gateways

        mock_conn = Mock()
        mock_conn.describe_nat_gateways.return_value = response

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

        cls._find_usage_nat_gateways(subnets)

        assert len(cls.limits['NAT Gateways per AZ'].get_current_usage()) == 2
        az2 = cls.limits['NAT Gateways per AZ'].get_current_usage()[0]
        assert az2.get_value() == 2
        assert az2.resource_id == 'az2'
        az3 = cls.limits['NAT Gateways per AZ'].get_current_usage()[1]
        assert az3.get_value() == 1
        assert az3.resource_id == 'az3'
        assert mock_conn.mock_calls == [
            call.describe_nat_gateways(),
        ]
Beispiel #48
0
    def test_find_usage_nat_gateways(self):
        subnets = result_fixtures.VPC.test_find_usage_nat_gateways_subnets
        response = result_fixtures.VPC.test_find_usage_nat_gateways

        mock_conn = Mock()
        mock_conn.describe_nat_gateways.return_value = response

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

        cls._find_usage_nat_gateways(subnets)

        assert len(cls.limits['NAT Gateways per AZ'].get_current_usage()) == 2
        az2 = cls.limits['NAT Gateways per AZ'].get_current_usage()[0]
        assert az2.get_value() == 2
        assert az2.resource_id == 'az2'
        az3 = cls.limits['NAT Gateways per AZ'].get_current_usage()[1]
        assert az3.get_value() == 1
        assert az3.resource_id == 'az3'
        assert mock_conn.mock_calls == [
            call.describe_nat_gateways(),
        ]
Beispiel #49
0
    def test_find_usage_subnets(self):
        mock1 = Mock(spec_set=Subnet)
        type(mock1).vpc_id = 'vpc-1'
        mock2 = Mock(spec_set=Subnet)
        type(mock2).vpc_id = 'vpc-1'
        mock3 = Mock(spec_set=Subnet)
        type(mock3).vpc_id = 'vpc-2'

        subnets = [mock1, mock2, mock3]
        mock_conn = Mock(spec_set=VPCConnection)
        mock_conn.get_all_subnets.return_value = subnets
        cls = _VpcService(21, 43)
        cls.conn = mock_conn
        cls._find_usage_subnets()
        usage = sorted(cls.limits['Subnets per VPC'].get_current_usage())
        assert len(usage) == 2
        assert usage[0].get_value() == 1
        assert usage[0].resource_id == 'vpc-2'
        assert usage[1].get_value() == 2
        assert usage[1].resource_id == 'vpc-1'
        assert mock_conn.mock_calls == [
            call.get_all_subnets(),
        ]
Beispiel #50
0
    def test_find_usage_nat_gateways_exception(self):
        subnets = result_fixtures.VPC.test_find_usage_nat_gateways_subnets

        def se_exc(*args, **kwargs):
            raise ClientError({'Error': {}}, 'opname')

        mock_conn = Mock()
        mock_conn.describe_nat_gateways.side_effect = se_exc

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

        with patch('%s.logger' % self.pbm, autospec=True) as mock_logger:
            cls._find_usage_nat_gateways(subnets)

        assert len(cls.limits['NAT Gateways per AZ'].get_current_usage()) == 0
        assert mock_conn.mock_calls == [
            call.describe_nat_gateways(),
        ]
        assert mock_logger.mock_calls == [
            call.error('Caught exception when trying to list NAT Gateways; '
                       'perhaps NAT service does not exist in this region?',
                       exc_info=1)
        ]
Beispiel #51
0
    def test_find_usage_subnets(self):
        response = result_fixtures.VPC.test_find_usage_subnets

        mock_conn = Mock()
        mock_conn.describe_subnets.return_value = response
        cls = _VpcService(21, 43, {}, None)
        cls._current_account_id = '0123456789'
        cls.conn = mock_conn

        res = cls._find_usage_subnets()
        assert res == {'string': 'string', 'subnet2': 'az3', 'subnet3': 'az2'}

        usage = sorted(cls.limits['Subnets per VPC'].get_current_usage())
        assert len(usage) == 2
        assert usage[0].get_value() == 1
        assert usage[0].resource_id == 'vpc-2'
        assert usage[1].get_value() == 2
        assert usage[1].resource_id == 'vpc-1'
        assert mock_conn.mock_calls == [
            call.describe_subnets(Filters=[{
                'Name': 'owner-id',
                'Values': ['0123456789']
            }])
        ]
Beispiel #52
0
    def test_find_usage_internet_gateways(self):
        mock1 = Mock(spec_set=InternetGateway)
        type(mock1).id = 'gw-1'
        mock2 = Mock(spec_set=InternetGateway)
        type(mock2).id = 'gw-2'

        gateways = [mock1, mock2]

        mock_conn = Mock(spec_set=VPCConnection)

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

        with patch('%s.boto_query_wrapper' % self.pbm) as mock_wrapper:
            mock_wrapper.return_value = gateways
            cls._find_usage_gateways()

        assert len(cls.limits['Internet gateways'].get_current_usage()) == 1
        assert cls.limits['Internet gateways'].get_current_usage(
        )[0].get_value() == 2
        assert mock_conn.mock_calls == []
        assert mock_wrapper.mock_calls == [
            call(mock_conn.get_all_internet_gateways)
        ]