def test_modify_network_interface_attribute(self):
        self.set_mock_db_items(fakes.DB_NETWORK_INTERFACE_1,
                               fakes.DB_NETWORK_INTERFACE_2)

        self.execute(
            'ModifyNetworkInterfaceAttribute', {
                'NetworkInterfaceId': fakes.ID_EC2_NETWORK_INTERFACE_1,
                'Description.Value': 'New description'
            })
        self.db_api.update_item.assert_called_once_with(
            mock.ANY,
            tools.update_dict(fakes.DB_NETWORK_INTERFACE_1,
                              {'description': 'New description'}))

        self.db_api.reset_mock()
        self.execute(
            'ModifyNetworkInterfaceAttribute', {
                'NetworkInterfaceId':
                fakes.ID_EC2_NETWORK_INTERFACE_2,
                'Attachment.AttachmentId':
                (fakes.ID_EC2_NETWORK_INTERFACE_2_ATTACH),
                'Attachment.DeleteOnTermination':
                'True'
            })
        self.db_api.update_item.assert_called_once_with(
            mock.ANY,
            tools.update_dict(fakes.DB_NETWORK_INTERFACE_2,
                              {'delete_on_termination': True}))
    def test_stop_gateway_vpn_connections(self, stop_vpn_connection):
        context = base.create_context()
        cleaner = common.OnCrashCleaner()
        vpn_connection_3 = tools.update_dict(
            fakes.DB_VPN_CONNECTION_1, {"id": fakes.random_ec2_id("vpn"), "os_ipsec_site_connections": {}}
        )

        self.set_mock_db_items(fakes.DB_VPN_CONNECTION_1, vpn_connection_3, fakes.DB_VPN_CONNECTION_2)
        vpn_connection_api._stop_gateway_vpn_connections(context, self.neutron, cleaner, fakes.DB_VPN_GATEWAY_1)
        self.assertEqual(2, stop_vpn_connection.call_count)
        stop_vpn_connection.assert_any_call(self.neutron, fakes.DB_VPN_CONNECTION_1)
        stop_vpn_connection.assert_any_call(self.neutron, vpn_connection_3)
        self.assertEqual(2, self.db_api.update_item.call_count)
        self.db_api.update_item.assert_any_call(
            mock.ANY, tools.update_dict(fakes.DB_VPN_CONNECTION_1, {"os_ipsec_site_connections": {}})
        )
        self.db_api.update_item.assert_any_call(mock.ANY, vpn_connection_3)

        self.db_api.reset_mock()
        self.neutron.reset_mock()
        stop_vpn_connection.reset_mock()
        self.set_mock_db_items(fakes.DB_VPN_CONNECTION_1)
        try:
            with common.OnCrashCleaner() as cleaner:
                vpn_connection_api._stop_gateway_vpn_connections(context, self.neutron, cleaner, fakes.DB_VPN_GATEWAY_1)
                raise Exception("fake-exception")
        except Exception as ex:
            if ex.message != "fake-exception":
                raise
        self.db_api.update_item.assert_called_with(mock.ANY, fakes.DB_VPN_CONNECTION_1)
Exemple #3
0
    def test_attach_vpn_gateway_invalid_parameters(self):
        def do_check(error_code):
            self.assert_execution_error(
                error_code, 'AttachVpnGateway',
                {'VpcId': fakes.ID_EC2_VPC_2,
                 'VpnGatewayId': fakes.ID_EC2_VPN_GATEWAY_2})

            self.assertFalse(self.db_api.update_item.called)
            self.db_api.reset_mock()

        self.set_mock_db_items(fakes.DB_VPC_2)
        do_check('InvalidVpnGatewayID.NotFound')

        self.set_mock_db_items(fakes.DB_VPN_GATEWAY_2)
        do_check('InvalidVpcID.NotFound')

        self.set_mock_db_items(
            tools.update_dict(fakes.DB_VPN_GATEWAY_2,
                              {'vpc_id': fakes.ID_EC2_VPC_1}),
            fakes.DB_VPC_2)
        do_check('VpnGatewayAttachmentLimitExceeded')

        self.set_mock_db_items(
            fakes.DB_VPN_GATEWAY_2, fakes.DB_VPC_2,
            tools.update_dict(fakes.DB_VPN_GATEWAY_1,
                              {'vpc_id': fakes.ID_EC2_VPC_2}))
        do_check('InvalidVpcState')
Exemple #4
0
 def setUp(self):
     super(VpnGatewayTestCase, self).setUp()
     self.DB_VPN_GATEWAY_2_ATTACHED = tools.update_dict(
         fakes.DB_VPN_GATEWAY_2, {'vpc_id': fakes.ID_EC2_VPC_2})
     self.DB_VPN_GATEWAY_1_DETACHED = tools.update_dict(
         fakes.DB_VPN_GATEWAY_1, {'vpc_id': None})
     self.DB_SUBNET_1_NO_VPN = tools.purge_dict(
         fakes.DB_SUBNET_1, ('os_vpnservice_id',))
Exemple #5
0
 def test_update_dict(self):
     d1 = {'a': 1, 'b': 2}
     d2 = {'b': 22, 'c': 33}
     res = tools.update_dict(d1, {})
     self.assertEqual({'a': 1, 'b': 2}, res)
     res = tools.update_dict(d1, d2)
     self.assertEqual({'a': 1, 'b': 22, 'c': 33}, res)
     self.assertEqual({'a': 1, 'b': 2}, d1)
    def test_associate_address_vpc(self):
        address.address_engine = (
            address.AddressEngineNeutron())

        def do_check(params, fixed_ip):
            resp = self.execute('AssociateAddress', params)
            self.assertEqual(True, resp['return'])
            self.assertEqual(fakes.ID_EC2_ASSOCIATION_1, resp['associationId'])

            self.neutron.update_floatingip.assert_called_once_with(
                fakes.ID_OS_FLOATING_IP_1,
                {'floatingip': {'port_id': fakes.ID_OS_PORT_2,
                                'fixed_ip_address': fixed_ip}})
            self.db_api.update_item.assert_called_once_with(
                mock.ANY,
                tools.update_dict(
                    fakes.DB_ADDRESS_1,
                    {'network_interface_id':
                     fakes.ID_EC2_NETWORK_INTERFACE_2,
                     'private_ip_address': fixed_ip}))

            self.neutron.update_floatingip.reset_mock()
            self.db_api.update_item.reset_mock()

        self.set_mock_db_items(
            fakes.DB_ADDRESS_1, fakes.DB_IGW_1, fakes.DB_IGW_2,
            fakes.DB_NETWORK_INTERFACE_1, fakes.DB_NETWORK_INTERFACE_2)
        self.neutron.show_floatingip.return_value = (
            {'floatingip': fakes.OS_FLOATING_IP_1})
        do_check({'AllocationId': fakes.ID_EC2_ADDRESS_1,
                  'InstanceId': fakes.ID_EC2_INSTANCE_1},
                 fakes.IP_NETWORK_INTERFACE_2)

        do_check({'AllocationId': fakes.ID_EC2_ADDRESS_1,
                  'NetworkInterfaceId': fakes.ID_EC2_NETWORK_INTERFACE_2},
                 fakes.IP_NETWORK_INTERFACE_2)

        do_check({'AllocationId': fakes.ID_EC2_ADDRESS_1,
                  'NetworkInterfaceId': fakes.ID_EC2_NETWORK_INTERFACE_2,
                  'PrivateIpAddress': fakes.IP_NETWORK_INTERFACE_2_EXT_1},
                 fakes.IP_NETWORK_INTERFACE_2_EXT_1)

        assigned_db_address_1 = tools.update_dict(
            fakes.DB_ADDRESS_1,
            {'network_interface_id': fakes.ID_EC2_NETWORK_INTERFACE_1,
             'private_ip_address': fakes.IP_NETWORK_INTERFACE_1})
        self.add_mock_db_items(assigned_db_address_1)
        assigned_floating_ip_1 = tools.update_dict(
            fakes.OS_FLOATING_IP_1,
            {'fixed_port_id': fakes.ID_OS_PORT_1,
             'fixed_ip_address': fakes.IP_NETWORK_INTERFACE_1})
        self.neutron.show_floatingip.return_value = (
            {'floatingip': assigned_floating_ip_1})
        do_check({'AllocationId': fakes.ID_EC2_ADDRESS_1,
                  'InstanceId': fakes.ID_EC2_INSTANCE_1,
                  'AllowReassociation': 'True'},
                 fakes.IP_NETWORK_INTERFACE_2)
    def test_associate_address_vpc(self):
        address.address_engine = (
            address.AddressEngineNeutron())

        def do_check(params, fixed_ip):
            resp = self.execute('AssociateAddress', params)
            self.assertEqual(True, resp['return'])
            self.assertEqual(fakes.ID_EC2_ASSOCIATION_1, resp['associationId'])

            self.neutron.update_floatingip.assert_called_once_with(
                fakes.ID_OS_FLOATING_IP_1,
                {'floatingip': {'port_id': fakes.ID_OS_PORT_2,
                                'fixed_ip_address': fixed_ip}})
            self.db_api.update_item.assert_called_once_with(
                mock.ANY,
                tools.update_dict(
                    fakes.DB_ADDRESS_1,
                    {'network_interface_id':
                     fakes.ID_EC2_NETWORK_INTERFACE_2,
                     'private_ip_address': fixed_ip}))

            self.neutron.update_floatingip.reset_mock()
            self.db_api.update_item.reset_mock()

        self.set_mock_db_items(
            fakes.DB_ADDRESS_1, fakes.DB_IGW_1, fakes.DB_IGW_2,
            fakes.DB_NETWORK_INTERFACE_1, fakes.DB_NETWORK_INTERFACE_2)
        self.neutron.show_floatingip.return_value = (
            {'floatingip': fakes.OS_FLOATING_IP_1})
        do_check({'AllocationId': fakes.ID_EC2_ADDRESS_1,
                  'InstanceId': fakes.ID_EC2_INSTANCE_1},
                 fakes.IP_NETWORK_INTERFACE_2)

        do_check({'AllocationId': fakes.ID_EC2_ADDRESS_1,
                  'NetworkInterfaceId': fakes.ID_EC2_NETWORK_INTERFACE_2},
                 fakes.IP_NETWORK_INTERFACE_2)

        do_check({'AllocationId': fakes.ID_EC2_ADDRESS_1,
                  'NetworkInterfaceId': fakes.ID_EC2_NETWORK_INTERFACE_2,
                  'PrivateIpAddress': fakes.IP_NETWORK_INTERFACE_2_EXT_1},
                 fakes.IP_NETWORK_INTERFACE_2_EXT_1)

        assigned_db_address_1 = tools.update_dict(
            fakes.DB_ADDRESS_1,
            {'network_interface_id': fakes.ID_EC2_NETWORK_INTERFACE_1,
             'private_ip_address': fakes.IP_NETWORK_INTERFACE_1})
        self.add_mock_db_items(assigned_db_address_1)
        assigned_floating_ip_1 = tools.update_dict(
            fakes.OS_FLOATING_IP_1,
            {'fixed_port_id': fakes.ID_OS_PORT_1,
             'fixed_ip_address': fakes.IP_NETWORK_INTERFACE_1})
        self.neutron.show_floatingip.return_value = (
            {'floatingip': assigned_floating_ip_1})
        do_check({'AllocationId': fakes.ID_EC2_ADDRESS_1,
                  'InstanceId': fakes.ID_EC2_INSTANCE_1,
                  'AllowReassociation': 'True'},
                 fakes.IP_NETWORK_INTERFACE_2)
    def test_attach_vpn_gateway(self):
        self.configure(external_network=fakes.NAME_OS_PUBLIC_NETWORK)
        subnet_2 = tools.patch_dict(fakes.DB_SUBNET_2,
                                    {'vpc_id': fakes.ID_EC2_VPC_2},
                                    ('os_vpnservice_id',))
        self.set_mock_db_items(
            fakes.DB_VPN_GATEWAY_1, fakes.DB_VPN_GATEWAY_2,
            fakes.DB_VPC_2, fakes.DB_IGW_1, fakes.DB_IGW_2,
            fakes.DB_SUBNET_1, subnet_2)
        subnet_2_updated = tools.update_dict(
            subnet_2, {'os_vpnservice_id': fakes.ID_OS_VPNSERVICE_2})
        os_vpnservice_2 = tools.patch_dict(fakes.OS_VPNSERVICE_2,
                                           {'router_id': fakes.ID_OS_ROUTER_2},
                                           ('id',))
        self.neutron.list_networks.return_value = (
            {'networks': [{'id': fakes.ID_OS_PUBLIC_NETWORK}]})
        self.neutron.create_vpnservice.side_effect = tools.get_neutron_create(
            'vpnservice', fakes.ID_OS_VPNSERVICE_2)

        def do_check():
            resp = self.execute('AttachVpnGateway',
                                {'VpcId': fakes.ID_EC2_VPC_2,
                                 'VpnGatewayId': fakes.ID_EC2_VPN_GATEWAY_2})
            self.assertEqual({'attachment': {'state': 'attached',
                                             'vpcId': fakes.ID_EC2_VPC_2}},
                             resp)
            self.assertEqual(2, self.db_api.update_item.call_count)
            self.db_api.update_item.assert_has_calls(
                [mock.call(mock.ANY, self.DB_VPN_GATEWAY_2_ATTACHED),
                 mock.call(mock.ANY, subnet_2_updated)])
            self.neutron.create_vpnservice.assert_called_once_with(
                {'vpnservice': os_vpnservice_2})

        do_check()
        self.neutron.add_gateway_router.assert_called_once_with(
            fakes.ID_OS_ROUTER_2,
            {'network_id': fakes.ID_OS_PUBLIC_NETWORK})
        self.neutron.list_networks.assert_called_once_with(
            **{'router:external': True,
               'name': fakes.NAME_OS_PUBLIC_NETWORK})

        # Internet gateway is already attached
        self.db_api.reset_mock()
        self.neutron.reset_mock()
        igw_2 = tools.update_dict(fakes.DB_IGW_2,
                                  {'vpc_id': fakes.ID_EC2_VPC_2})
        self.add_mock_db_items(igw_2)

        do_check()
        self.neutron.add_gateway_router.assert_not_called()
Exemple #9
0
    def test_associate_dhcp_options_rollback(self):
        vpc = tools.update_dict(
            fakes.DB_VPC_1, {'dhcp_options_id': fakes.ID_EC2_DHCP_OPTIONS_1})
        self.set_mock_db_items(vpc, fakes.DB_DHCP_OPTIONS_1,
                               fakes.DB_DHCP_OPTIONS_2,
                               fakes.DB_NETWORK_INTERFACE_1,
                               fakes.DB_NETWORK_INTERFACE_2)
        self.neutron.list_ports.return_value = ({
            'ports': [fakes.OS_PORT_1, fakes.OS_PORT_2]
        })

        def update_port_func(port_id, _port_data):
            if port_id == fakes.ID_OS_PORT_2:
                raise Exception()

        self.neutron.update_port.side_effect = update_port_func

        self.assert_execution_error(
            self.ANY_EXECUTE_ERROR, 'AssociateDhcpOptions', {
                'dhcpOptionsId': fakes.ID_EC2_DHCP_OPTIONS_2,
                'vpcId': fakes.ID_EC2_VPC_1
            })

        self.assert_any_call(self.neutron.update_port, fakes.ID_OS_PORT_1,
                             {'port': fakes.OS_DHCP_OPTIONS_1})
        self.db_api.update_item.assert_any_call(mock.ANY, vpc)
    def test_associate_dhcp_options_rollback(self):
        vpc = tools.update_dict(
                fakes.DB_VPC_1,
                {'dhcp_options_id': fakes.ID_EC2_DHCP_OPTIONS_1})
        self.set_mock_db_items(
            vpc, fakes.DB_DHCP_OPTIONS_1, fakes.DB_DHCP_OPTIONS_2,
            fakes.DB_NETWORK_INTERFACE_1, fakes.DB_NETWORK_INTERFACE_2)
        self.neutron.list_ports.return_value = (
                {'ports': [fakes.OS_PORT_1, fakes.OS_PORT_2]})

        def update_port_func(port_id, _port_data):
            if port_id == fakes.ID_OS_PORT_2:
                raise Exception()

        self.neutron.update_port.side_effect = update_port_func

        self.assert_execution_error(
            self.ANY_EXECUTE_ERROR, 'AssociateDhcpOptions',
            {'dhcpOptionsId': fakes.ID_EC2_DHCP_OPTIONS_2,
             'vpcId': fakes.ID_EC2_VPC_1})

        self.assert_any_call(self.neutron.update_port,
                             fakes.ID_OS_PORT_1,
                             {'port': fakes.OS_DHCP_OPTIONS_1})
        self.db_api.update_item.assert_any_call(
                mock.ANY, vpc)
    def test_attach_igw(self):
        self.configure(external_network=fakes.NAME_OS_PUBLIC_NETWORK)
        self.set_mock_db_items(fakes.DB_IGW_1, fakes.DB_IGW_2, fakes.DB_VPC_2,
                               fakes.DB_VPN_GATEWAY_1, fakes.DB_VPN_GATEWAY_2)
        self.neutron.list_networks.return_value = (
                {'networks': [{'id': fakes.ID_OS_PUBLIC_NETWORK}]})

        def do_check():
            resp = self.execute(
                    'AttachInternetGateway',
                    {'VpcId': fakes.ID_EC2_VPC_2,
                     'InternetGatewayId': fakes.ID_EC2_IGW_2})

            self.assertEqual(True, resp['return'])
            self.db_api.update_item.assert_called_once_with(
                    mock.ANY, self.DB_IGW_2_ATTACHED)

        do_check()
        self.neutron.add_gateway_router.assert_called_once_with(
                fakes.ID_OS_ROUTER_2,
                {'network_id': fakes.ID_OS_PUBLIC_NETWORK})
        self.neutron.list_networks.assert_called_once_with(
                **{'router:external': True,
                   'name': fakes.NAME_OS_PUBLIC_NETWORK})

        # VPN gateway is already attached
        self.db_api.reset_mock()
        self.neutron.reset_mock()
        vgw_2 = tools.update_dict(fakes.DB_VPN_GATEWAY_2,
                                  {'vpc_id': fakes.ID_EC2_VPC_2})
        self.add_mock_db_items(vgw_2)
        do_check()
        self.assertFalse(self.neutron.add_gateway_router.called)
Exemple #12
0
 def do_check():
     resp = self.execute(
         'DetachVpnGateway',
         {'VpcId': fakes.ID_EC2_VPC_1,
          'VpnGatewayId': fakes.ID_EC2_VPN_GATEWAY_1})
     self.assertEqual({'return': True}, resp)
     self.assertEqual(3, self.db_api.update_item.call_count)
     self.db_api.update_item.assert_has_calls(
         [mock.call(mock.ANY, self.DB_VPN_GATEWAY_1_DETACHED),
          mock.call(mock.ANY,
                    tools.update_dict(
                        fakes.DB_VPN_CONNECTION_1,
                        {'os_ipsec_site_connections': {}})),
          mock.call(mock.ANY, self.DB_SUBNET_1_NO_VPN)])
     self.neutron.delete_vpnservice.assert_called_once_with(
         fakes.ID_OS_VPNSERVICE_1)
     self.assertEqual(1, len(delete_vpnservice_calls))
     self.assertEqual(
         mock.call(mock.ANY, self.neutron, mock.ANY, fakes.DB_SUBNET_1),
         delete_vpnservice_calls[0])
     self.assertIsInstance(delete_vpnservice_calls[0][1][2],
                           common.OnCrashCleaner)
     stop_gateway_vpn_connections.assert_called_once_with(
         mock.ANY, self.neutron, mock.ANY,
         self.DB_VPN_GATEWAY_1_DETACHED)
     self.assertIsInstance(stop_gateway_vpn_connections.call_args[0][2],
                           common.OnCrashCleaner)
     self.neutron.delete_ipsec_site_connection.assert_called_once_with(
         fakes.ID_OS_IPSEC_SITE_CONNECTION_2)
     mock_manager.assert_has_calls([
         mock.call.stop_gateway_vpn_connections(
             *(mock.ANY for _x in range(4))),
         mock.call.delete_vpnservice(
             *(mock.ANY for _x in range(4)))])
    def test_describe_vpn_connections(self):
        self.set_mock_db_items(
            fakes.DB_VPN_CONNECTION_1,
            fakes.DB_VPN_CONNECTION_2,
            fakes.DB_CUSTOMER_GATEWAY_1,
            fakes.DB_CUSTOMER_GATEWAY_2,
            fakes.DB_VPN_GATEWAY_1,
            fakes.DB_VPN_GATEWAY_2,
            fakes.DB_VPC_1,
            fakes.DB_VPC_2,
        )
        self.neutron.list_ikepolicies.return_value = {"ikepolicies": [fakes.OS_IKEPOLICY_1, fakes.OS_IKEPOLICY_2]}
        self.neutron.list_ipsecpolicies.return_value = {
            "ipsecpolicies": [fakes.OS_IPSECPOLICY_1, fakes.OS_IPSECPOLICY_2]
        }
        self.neutron.list_ipsec_site_connections.return_value = {"ipsec_site_connections": []}
        self.neutron.list_routers.return_value = {"routers": [fakes.OS_ROUTER_1, fakes.OS_ROUTER_2]}

        resp = self.execute("DescribeVpnConnections", {})
        vpns = [
            tools.update_dict(vpn, {"customerGatewayConfiguration": "DONTCARE"})
            for vpn in (fakes.EC2_VPN_CONNECTION_1, fakes.EC2_VPN_CONNECTION_2)
        ]
        self.assertThat(resp, matchers.DictMatches({"vpnConnectionSet": vpns}, orderless_lists=True))
        for vpn in (fakes.EC2_VPN_CONNECTION_1, fakes.EC2_VPN_CONNECTION_2):
            config = next(
                v["customerGatewayConfiguration"]
                for v in resp["vpnConnectionSet"]
                if v["vpnConnectionId"] == vpn["vpnConnectionId"]
            )
            self.assertThat(config, matchers.XMLMatches(vpn["customerGatewayConfiguration"], orderless_sequence=True))
            self.assertTrue(config.startswith("<?xml version='1.0' encoding='UTF-8'?>"))
        self.neutron.list_ikepolicies.assert_called_once_with(tenant_id=fakes.ID_OS_PROJECT)
        self.neutron.list_ipsecpolicies.assert_called_once_with(tenant_id=fakes.ID_OS_PROJECT)
        self.neutron.list_ipsec_site_connections.assert_called_once_with(tenant_id=fakes.ID_OS_PROJECT)
        self.neutron.list_routers.assert_called_once_with(tenant_id=fakes.ID_OS_PROJECT)

        resp = self.execute("DescribeVpnConnections", {"VpnConnectionId.1": fakes.ID_EC2_VPN_CONNECTION_1})
        self.assertThat(resp, matchers.DictMatches({"vpnConnectionSet": [vpns[0]]}, orderless_lists=True))

        self.check_filtering(
            "DescribeVpnConnections",
            "vpnConnectionSet",
            [
                ("customer-gateway-configuration", "*" + fakes.PRE_SHARED_KEY_1 + "*"),
                ("customer-gateway-id", fakes.ID_EC2_CUSTOMER_GATEWAY_1),
                ("state", "available"),
                ("option.static-routes-only", True),
                ("route.destination-cidr-block", fakes.CIDR_VPN_2_PROPAGATED_1),
                ("type", "ipsec.1"),
                ("vpn-connection-id", fakes.ID_EC2_VPN_CONNECTION_1),
                ("vpn-gateway-id", fakes.ID_EC2_VPN_GATEWAY_1),
            ],
        )

        self.check_tag_support(
            "DescribeVpnConnections", "vpnConnectionSet", fakes.ID_EC2_VPN_CONNECTION_1, "vpnConnectionId"
        )
Exemple #14
0
 def test_delete_dhcp_options_with_dependencies(self):
     self.set_mock_db_items(
         fakes.DB_DHCP_OPTIONS_1,
         tools.update_dict(
             fakes.DB_VPC_1,
             {'dhcp_options_id': fakes.ID_EC2_DHCP_OPTIONS_1}))
     self.assert_execution_error(
         'DependencyViolation', 'DeleteDhcpOptions',
         {'dhcpOptionsId': fakes.ID_EC2_DHCP_OPTIONS_1})
 def test_delete_dhcp_options_with_dependencies(self):
     self.set_mock_db_items(
         fakes.DB_DHCP_OPTIONS_1,
         tools.update_dict(
             fakes.DB_VPC_1,
             {'dhcp_options_id': fakes.ID_EC2_DHCP_OPTIONS_1}))
     self.assert_execution_error(
         'DependencyViolation', 'DeleteDhcpOptions',
         {'dhcpOptionsId': fakes.ID_EC2_DHCP_OPTIONS_1})
 def test_format_vpn_connection(self):
     db_vpn_connection_1 = tools.update_dict(fakes.DB_VPN_CONNECTION_1, {"cidrs": []})
     ec2_vpn_connection_1 = tools.patch_dict(
         fakes.EC2_VPN_CONNECTION_1, {"routes": [], "vgwTelemetry": []}, ("customerGatewayConfiguration",)
     )
     formatted = vpn_connection_api._format_vpn_connection(
         db_vpn_connection_1, {fakes.ID_EC2_CUSTOMER_GATEWAY_1: fakes.DB_CUSTOMER_GATEWAY_1}, {}, {}, {}, {}
     )
     formatted.pop("customerGatewayConfiguration")
     self.assertThat(ec2_vpn_connection_1, matchers.DictMatches(formatted))
 def _compare_aws_xml(self, root_tag, xmlns, request_id, dict_data,
                      observed):
     # NOTE(ft): we cann't use matchers.XMLMatches since it makes comparison
     # based on the order of tags
     xml = etree.fromstring(observed)
     self.assertEqual(xmlns, xml.nsmap.get(None))
     observed_data = tools.parse_xml(observed)
     expected = {root_tag: tools.update_dict(dict_data,
                                             {'requestId': request_id})}
     self.assertThat(observed_data, matchers.DictMatches(expected))
 def _compare_aws_xml(self, root_tag, xmlns, request_id, dict_data,
                      observed):
     # NOTE(ft): we cann't use matchers.XMLMatches since it makes comparison
     # based on the order of tags
     xml = etree.fromstring(observed)
     self.assertEqual(xmlns, xml.nsmap.get(None))
     observed_data = tools.parse_xml(observed)
     expected = {root_tag: tools.update_dict(dict_data,
                                             {'requestId': request_id})}
     self.assertThat(observed_data, matchers.DictMatches(expected))
    def test_detach_network_interface_rollback(self):
        network_interface = tools.update_dict(fakes.DB_NETWORK_INTERFACE_2,
                                              {'device_index': 1})
        self.set_mock_db_items(network_interface)
        self.neutron.show_port.return_value = ({'port': fakes.OS_PORT_2})
        self.neutron.update_port.side_effect = Exception()

        self.assert_execution_error(
            self.ANY_EXECUTE_ERROR, 'DetachNetworkInterface',
            {'AttachmentId': fakes.ID_EC2_NETWORK_INTERFACE_2_ATTACH})

        self.db_api.update_item.assert_any_call(mock.ANY, network_interface)
Exemple #20
0
    def test_delete_subnet_vpn(self):
        context = base.create_context()
        cleaner = common.OnCrashCleaner()

        # subnet is not connected to the vpn
        vpn_connection_api._delete_subnet_vpn(context, self.neutron, cleaner,
                                              fakes.DB_SUBNET_1,
                                              fakes.DB_VPN_CONNECTION_1)
        self.assertFalse(self.db_api.update_item.called)
        self.assertFalse(self.neutron.delete_ipsec_site_connection.called)

        # delete subnet vpn connection
        vpn_connection_api._delete_subnet_vpn(
            context, self.neutron, cleaner, fakes.DB_SUBNET_2,
            copy.deepcopy(fakes.DB_VPN_CONNECTION_1))
        self.db_api.update_item.assert_called_once_with(
            mock.ANY,
            tools.update_dict(fakes.DB_VPN_CONNECTION_1,
                              {'os_ipsec_site_connections': {}}))
        self.neutron.delete_ipsec_site_connection.assert_called_once_with(
            fakes.ID_OS_IPSEC_SITE_CONNECTION_2)

        # delete subnet vpn connection, leave connections of other subnets
        self.db_api.reset_mock()
        self.neutron.reset_mock()
        id_os_connection = fakes.random_os_id()
        vpn_connection_1 = copy.deepcopy(fakes.DB_VPN_CONNECTION_1)
        (vpn_connection_1['os_ipsec_site_connections'][fakes.ID_EC2_SUBNET_1]
         ) = id_os_connection
        vpn_connection_api._delete_subnet_vpn(context, self.neutron, cleaner,
                                              fakes.DB_SUBNET_1,
                                              vpn_connection_1)
        self.db_api.update_item.assert_called_once_with(
            mock.ANY, fakes.DB_VPN_CONNECTION_1)
        self.neutron.delete_ipsec_site_connection.assert_called_once_with(
            id_os_connection)

        # rollback of deleting subnet vpn connection
        self.db_api.reset_mock()
        self.neutron.reset_mock()
        try:
            with common.OnCrashCleaner() as cleaner:
                vpn_connection_api._delete_subnet_vpn(
                    context, self.neutron, cleaner, fakes.DB_SUBNET_2,
                    copy.deepcopy(fakes.DB_VPN_CONNECTION_1))
                raise Exception('fake-exception')
        except Exception as ex:
            if ex.message != 'fake-exception':
                raise
        self.db_api.update_item.assert_called_with(mock.ANY,
                                                   fakes.DB_VPN_CONNECTION_1)
        self.assertFalse(self.neutron.create_ipsec_site_connection.called)
    def test_delete_vpn_connection_route(self, reset_vpn_connections):
        self.set_mock_db_items(fakes.DB_VPN_CONNECTION_2, fakes.DB_VPN_GATEWAY_2)

        resp = self.execute(
            "DeleteVpnConnectionRoute",
            {"VpnConnectionId": fakes.ID_EC2_VPN_CONNECTION_2, "DestinationCidrBlock": fakes.CIDR_VPN_2_PROPAGATED_1},
        )
        self.assertEqual({"return": True}, resp)
        vpn = tools.update_dict(fakes.DB_VPN_CONNECTION_2, {"cidrs": [fakes.CIDR_VPN_2_PROPAGATED_2]})
        self.db_api.update_item.assert_called_once_with(mock.ANY, vpn)
        reset_vpn_connections.assert_called_once_with(
            mock.ANY, self.neutron, mock.ANY, fakes.DB_VPN_GATEWAY_2, vpn_connections=[vpn]
        )
Exemple #22
0
 def test_format_vpn_connection(self):
     db_vpn_connection_1 = tools.update_dict(fakes.DB_VPN_CONNECTION_1,
                                             {'cidrs': []})
     ec2_vpn_connection_1 = tools.patch_dict(fakes.EC2_VPN_CONNECTION_1, {
         'routes': [],
         'vgwTelemetry': []
     }, ('customerGatewayConfiguration', ))
     formatted = vpn_connection_api._format_vpn_connection(
         db_vpn_connection_1,
         {fakes.ID_EC2_CUSTOMER_GATEWAY_1: fakes.DB_CUSTOMER_GATEWAY_1}, {},
         {}, {}, {})
     formatted.pop('customerGatewayConfiguration')
     self.assertThat(ec2_vpn_connection_1, matchers.DictMatches(formatted))
Exemple #23
0
 def check(ec2_dhcp_options_id, db_dhcp_options_id, os_dhcp_options):
     resp = self.execute('AssociateDhcpOptions', {
         'dhcpOptionsId': ec2_dhcp_options_id,
         'vpcId': fakes.ID_EC2_VPC_1
     })
     self.assertEqual(True, resp['return'])
     self.db_api.update_item.assert_any_call(
         mock.ANY,
         tools.update_dict(fakes.DB_VPC_1,
                           {'dhcp_options_id': db_dhcp_options_id}))
     self.assert_any_call(
         self.neutron.update_port, fakes.ID_OS_PORT_1,
         {'port': self._effective_os_dhcp_options(os_dhcp_options)})
    def test_delete_subnet_vpn(self):
        context = base.create_context()
        cleaner = common.OnCrashCleaner()

        # subnet is not connected to the vpn
        vpn_connection_api._delete_subnet_vpn(
            context, self.neutron, cleaner, fakes.DB_SUBNET_1,
            fakes.DB_VPN_CONNECTION_1)
        self.assertFalse(self.db_api.update_item.called)
        self.assertFalse(self.neutron.delete_ipsec_site_connection.called)

        # delete subnet vpn connection
        vpn_connection_api._delete_subnet_vpn(
            context, self.neutron, cleaner, fakes.DB_SUBNET_2,
            copy.deepcopy(fakes.DB_VPN_CONNECTION_1))
        self.db_api.update_item.assert_called_once_with(
            mock.ANY, tools.update_dict(fakes.DB_VPN_CONNECTION_1,
                                        {'os_ipsec_site_connections': {}}))
        self.neutron.delete_ipsec_site_connection.assert_called_once_with(
            fakes.ID_OS_IPSEC_SITE_CONNECTION_2)

        # delete subnet vpn connection, leave connections of other subnets
        self.db_api.reset_mock()
        self.neutron.reset_mock()
        id_os_connection = fakes.random_os_id()
        vpn_connection_1 = copy.deepcopy(fakes.DB_VPN_CONNECTION_1)
        (vpn_connection_1['os_ipsec_site_connections']
         [fakes.ID_EC2_SUBNET_1]) = id_os_connection
        vpn_connection_api._delete_subnet_vpn(
            context, self.neutron, cleaner, fakes.DB_SUBNET_1,
            vpn_connection_1)
        self.db_api.update_item.assert_called_once_with(
            mock.ANY, fakes.DB_VPN_CONNECTION_1)
        self.neutron.delete_ipsec_site_connection.assert_called_once_with(
            id_os_connection)

        # rollback of deleting subnet vpn connection
        self.db_api.reset_mock()
        self.neutron.reset_mock()
        try:
            with common.OnCrashCleaner() as cleaner:
                vpn_connection_api._delete_subnet_vpn(
                    context, self.neutron, cleaner, fakes.DB_SUBNET_2,
                    copy.deepcopy(fakes.DB_VPN_CONNECTION_1))
                raise Exception('fake-exception')
        except Exception as ex:
            if str(ex) != 'fake-exception':
                raise
        self.db_api.update_item.assert_called_with(
            mock.ANY, fakes.DB_VPN_CONNECTION_1)
        self.assertFalse(self.neutron.create_ipsec_site_connection.called)
    def test_create_vpn_connection(self, random_choice, reset_vpn_connections,
                                   describe_vpn_connections):
        self.set_mock_db_items(
            fakes.DB_VPN_GATEWAY_1, fakes.DB_VPN_GATEWAY_2,
            fakes.DB_CUSTOMER_GATEWAY_1, fakes.DB_CUSTOMER_GATEWAY_2,
            fakes.DB_VPC_1)
        self.neutron.create_ikepolicy.side_effect = (
            tools.get_neutron_create('ikepolicy', fakes.ID_OS_IKEPOLICY_1))
        self.neutron.create_ipsecpolicy.side_effect = (
            tools.get_neutron_create('ipsecpolicy', fakes.ID_OS_IPSECPOLICY_1))
        self.db_api.add_item.side_effect = (
            tools.get_db_api_add_item(fakes.ID_EC2_VPN_CONNECTION_1))
        random_choice.side_effect = iter(fakes.PRE_SHARED_KEY_1)
        describe_vpn_connections.return_value = {
            'vpnConnectionSet': [fakes.EC2_VPN_CONNECTION_1]}

        resp = self.execute(
            'CreateVpnConnection',
            {'VpnGatewayId': fakes.ID_EC2_VPN_GATEWAY_1,
             'CustomerGatewayId': fakes.ID_EC2_CUSTOMER_GATEWAY_1,
             'Type': 'ipsec.1',
             'Options.StaticRoutesOnly': 'True'})
        self.assertThat(
            resp,
            matchers.DictMatches(
                {'vpnConnection': fakes.EC2_VPN_CONNECTION_1}))

        self.neutron.create_ikepolicy.assert_called_once_with(
            {'ikepolicy': tools.purge_dict(fakes.OS_IKEPOLICY_1, ('id',))})
        self.neutron.create_ipsecpolicy.assert_called_once_with(
            {'ipsecpolicy': tools.purge_dict(fakes.OS_IPSECPOLICY_1, ('id',))})
        random_choice.assert_called_with(vpn_connection_api.SHARED_KEY_CHARS)
        new_vpn_connection_1 = tools.update_dict(
            fakes.DB_VPN_CONNECTION_1, {'cidrs': [],
                                        'os_ipsec_site_connections': {}})
        self.db_api.add_item.assert_called_once_with(
            mock.ANY, 'vpn',
            tools.purge_dict(new_vpn_connection_1, ('id', 'vpc_id', 'os_id')))
        self.neutron.update_ikepolicy.assert_called_once_with(
            fakes.ID_OS_IKEPOLICY_1,
            {'ikepolicy': {'name': fakes.ID_EC2_VPN_CONNECTION_1}})
        self.neutron.update_ipsecpolicy.assert_called_once_with(
            fakes.ID_OS_IPSECPOLICY_1,
            {'ipsecpolicy': {'name': fakes.ID_EC2_VPN_CONNECTION_1}})
        reset_vpn_connections.assert_called_once_with(
            mock.ANY, self.neutron, mock.ANY, fakes.DB_VPN_GATEWAY_1,
            vpn_connections=[new_vpn_connection_1])
        self.assertIsInstance(reset_vpn_connections.call_args[0][2],
                              common.OnCrashCleaner)
        describe_vpn_connections.assert_called_once_with(
            mock.ANY, vpn_connection_id=[fakes.ID_EC2_VPN_CONNECTION_1])
    def test_detach_network_interface_rollback(self):
        network_interface = tools.update_dict(fakes.DB_NETWORK_INTERFACE_2,
                                              {'device_index': 1})
        self.set_mock_db_items(network_interface)
        self.neutron.show_port.return_value = (
            {'port': fakes.OS_PORT_2})
        self.neutron.update_port.side_effect = Exception()

        self.assert_execution_error(
            self.ANY_EXECUTE_ERROR, 'DetachNetworkInterface',
            {'AttachmentId': fakes.ID_EC2_NETWORK_INTERFACE_2_ATTACH})

        self.db_api.update_item.assert_any_call(
            mock.ANY, network_interface)
 def check(ec2_dhcp_options_id, db_dhcp_options_id, os_dhcp_options):
     resp = self.execute('AssociateDhcpOptions',
                         {'dhcpOptionsId': ec2_dhcp_options_id,
                          'vpcId': fakes.ID_EC2_VPC_1})
     self.assertEqual(True, resp['return'])
     self.db_api.update_item.assert_any_call(
             mock.ANY,
             tools.update_dict(
                     fakes.DB_VPC_1,
                     {'dhcp_options_id': db_dhcp_options_id}))
     self.assert_any_call(
         self.neutron.update_port,
         fakes.ID_OS_PORT_1,
         {'port': self._effective_os_dhcp_options(os_dhcp_options)})
Exemple #28
0
    def test_stop_gateway_vpn_connections(self, stop_vpn_connection):
        context = base.create_context()
        cleaner = common.OnCrashCleaner()
        vpn_connection_3 = tools.update_dict(fakes.DB_VPN_CONNECTION_1, {
            'id': fakes.random_ec2_id('vpn'),
            'os_ipsec_site_connections': {}
        })

        self.set_mock_db_items(fakes.DB_VPN_CONNECTION_1, vpn_connection_3,
                               fakes.DB_VPN_CONNECTION_2)
        vpn_connection_api._stop_gateway_vpn_connections(
            context, self.neutron, cleaner, fakes.DB_VPN_GATEWAY_1)
        self.assertEqual(2, stop_vpn_connection.call_count)
        stop_vpn_connection.assert_any_call(self.neutron,
                                            fakes.DB_VPN_CONNECTION_1)
        stop_vpn_connection.assert_any_call(self.neutron, vpn_connection_3)
        self.assertEqual(2, self.db_api.update_item.call_count)
        self.db_api.update_item.assert_any_call(
            mock.ANY,
            tools.update_dict(fakes.DB_VPN_CONNECTION_1,
                              {'os_ipsec_site_connections': {}}))
        self.db_api.update_item.assert_any_call(mock.ANY, vpn_connection_3)

        self.db_api.reset_mock()
        self.neutron.reset_mock()
        stop_vpn_connection.reset_mock()
        self.set_mock_db_items(fakes.DB_VPN_CONNECTION_1)
        try:
            with common.OnCrashCleaner() as cleaner:
                vpn_connection_api._stop_gateway_vpn_connections(
                    context, self.neutron, cleaner, fakes.DB_VPN_GATEWAY_1)
                raise Exception('fake-exception')
        except Exception as ex:
            if ex.message != 'fake-exception':
                raise
        self.db_api.update_item.assert_called_with(mock.ANY,
                                                   fakes.DB_VPN_CONNECTION_1)
    def test_create_vpn_connection(self, random_choice, reset_vpn_connections, describe_vpn_connections):
        self.set_mock_db_items(
            fakes.DB_VPN_GATEWAY_1,
            fakes.DB_VPN_GATEWAY_2,
            fakes.DB_CUSTOMER_GATEWAY_1,
            fakes.DB_CUSTOMER_GATEWAY_2,
            fakes.DB_VPC_1,
        )
        self.neutron.create_ikepolicy.side_effect = tools.get_neutron_create("ikepolicy", fakes.ID_OS_IKEPOLICY_1)
        self.neutron.create_ipsecpolicy.side_effect = tools.get_neutron_create("ipsecpolicy", fakes.ID_OS_IPSECPOLICY_1)
        self.db_api.add_item.side_effect = tools.get_db_api_add_item(fakes.ID_EC2_VPN_CONNECTION_1)
        random_choice.side_effect = iter(fakes.PRE_SHARED_KEY_1)
        describe_vpn_connections.return_value = {"vpnConnectionSet": [fakes.EC2_VPN_CONNECTION_1]}

        resp = self.execute(
            "CreateVpnConnection",
            {
                "VpnGatewayId": fakes.ID_EC2_VPN_GATEWAY_1,
                "CustomerGatewayId": fakes.ID_EC2_CUSTOMER_GATEWAY_1,
                "Type": "ipsec.1",
                "Options.StaticRoutesOnly": "True",
            },
        )
        self.assertThat(resp, matchers.DictMatches({"vpnConnection": fakes.EC2_VPN_CONNECTION_1}))

        self.neutron.create_ikepolicy.assert_called_once_with(
            {"ikepolicy": tools.purge_dict(fakes.OS_IKEPOLICY_1, ("id",))}
        )
        self.neutron.create_ipsecpolicy.assert_called_once_with(
            {"ipsecpolicy": tools.purge_dict(fakes.OS_IPSECPOLICY_1, ("id",))}
        )
        random_choice.assert_called_with(vpn_connection_api.SHARED_KEY_CHARS)
        new_vpn_connection_1 = tools.update_dict(
            fakes.DB_VPN_CONNECTION_1, {"cidrs": [], "os_ipsec_site_connections": {}}
        )
        self.db_api.add_item.assert_called_once_with(
            mock.ANY, "vpn", tools.purge_dict(new_vpn_connection_1, ("id", "vpc_id", "os_id"))
        )
        self.neutron.update_ikepolicy.assert_called_once_with(
            fakes.ID_OS_IKEPOLICY_1, {"ikepolicy": {"name": fakes.ID_EC2_VPN_CONNECTION_1}}
        )
        self.neutron.update_ipsecpolicy.assert_called_once_with(
            fakes.ID_OS_IPSECPOLICY_1, {"ipsecpolicy": {"name": fakes.ID_EC2_VPN_CONNECTION_1}}
        )
        reset_vpn_connections.assert_called_once_with(
            mock.ANY, self.neutron, mock.ANY, fakes.DB_VPN_GATEWAY_1, vpn_connections=[new_vpn_connection_1]
        )
        self.assertIsInstance(reset_vpn_connections.call_args[0][2], common.OnCrashCleaner)
        describe_vpn_connections.assert_called_once_with(mock.ANY, vpn_connection_id=[fakes.ID_EC2_VPN_CONNECTION_1])
    def test_modify_network_interface_attribute(self):
        self.set_mock_db_items(fakes.DB_NETWORK_INTERFACE_1,
                               fakes.DB_NETWORK_INTERFACE_2)

        self.execute(
            'ModifyNetworkInterfaceAttribute',
            {'NetworkInterfaceId': fakes.ID_EC2_NETWORK_INTERFACE_1,
             'Description.Value': 'New description'})
        self.db_api.update_item.assert_called_once_with(
            mock.ANY,
            tools.update_dict(fakes.DB_NETWORK_INTERFACE_1,
                              {'description': 'New description'}))

        self.db_api.reset_mock()
        self.execute(
            'ModifyNetworkInterfaceAttribute',
            {'NetworkInterfaceId': fakes.ID_EC2_NETWORK_INTERFACE_2,
             'Attachment.AttachmentId': (
                 fakes.ID_EC2_NETWORK_INTERFACE_2_ATTACH),
             'Attachment.DeleteOnTermination': 'True'})
        self.db_api.update_item.assert_called_once_with(
            mock.ANY,
            tools.update_dict(fakes.DB_NETWORK_INTERFACE_2,
                              {'delete_on_termination': True}))
    def test_delete_route_table_invalid_parameters(self):
        self.set_mock_db_items()
        self.assert_execution_error(
            'InvalidRouteTableID.NotFound', 'DeleteRouteTable',
            {'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_1})

        self.set_mock_db_items(fakes.DB_ROUTE_TABLE_1, fakes.DB_VPC_1)
        self.assert_execution_error(
            'DependencyViolation', 'DeleteRouteTable',
            {'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_1})

        subnet = tools.update_dict(
            fakes.DB_SUBNET_2, {'route_table_id': fakes.ID_EC2_ROUTE_TABLE_2})
        self.set_mock_db_items(fakes.DB_ROUTE_TABLE_2, fakes.DB_VPC_1, subnet)
        self.assert_execution_error(
            'DependencyViolation', 'DeleteRouteTable',
            {'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_2})
    def test_create_network_interface_rollback(self, _add_dhcp_opts_to_port):
        self.set_mock_db_items(
            tools.update_dict(
                fakes.DB_VPC_1,
                {'dhcp_options_id': fakes.ID_EC2_DHCP_OPTIONS_1}),
            fakes.DB_SUBNET_1, fakes.DB_DHCP_OPTIONS_1)
        self.db_api.add_item.return_value = fakes.DB_NETWORK_INTERFACE_1
        self.neutron.show_subnet.return_value = {'subnet': fakes.OS_SUBNET_1}
        self.neutron.create_port.return_value = {'port': fakes.OS_PORT_1}
        _add_dhcp_opts_to_port.side_effect = Exception()

        self.assert_execution_error(self.ANY_EXECUTE_ERROR,
                                    'CreateNetworkInterface',
                                    {'SubnetId': fakes.ID_EC2_SUBNET_1})

        self.neutron.delete_port.assert_called_once_with(fakes.ID_OS_PORT_1)
        self.db_api.delete_item.assert_called_once_with(
            mock.ANY, fakes.ID_EC2_NETWORK_INTERFACE_1)
Exemple #33
0
    def test_delete_vpn_connection_route(self, reset_vpn_connections):
        self.set_mock_db_items(fakes.DB_VPN_CONNECTION_2,
                               fakes.DB_VPN_GATEWAY_2)

        resp = self.execute(
            'DeleteVpnConnectionRoute', {
                'VpnConnectionId': fakes.ID_EC2_VPN_CONNECTION_2,
                'DestinationCidrBlock': fakes.CIDR_VPN_2_PROPAGATED_1
            })
        self.assertEqual({'return': True}, resp)
        vpn = tools.update_dict(fakes.DB_VPN_CONNECTION_2,
                                {'cidrs': [fakes.CIDR_VPN_2_PROPAGATED_2]})
        self.db_api.update_item.assert_called_once_with(mock.ANY, vpn)
        reset_vpn_connections.assert_called_once_with(mock.ANY,
                                                      self.neutron,
                                                      mock.ANY,
                                                      fakes.DB_VPN_GATEWAY_2,
                                                      vpn_connections=[vpn])
 def do_check():
     resp = self.execute(
         'DetachVpnGateway',
         {'VpcId': fakes.ID_EC2_VPC_1,
          'VpnGatewayId': fakes.ID_EC2_VPN_GATEWAY_1})
     self.assertEqual({'return': True}, resp)
     self.assertEqual(3, self.db_api.update_item.call_count)
     self.db_api.update_item.assert_has_calls(
         [mock.call(mock.ANY, self.DB_VPN_GATEWAY_1_DETACHED),
          mock.call(mock.ANY,
                    tools.update_dict(
                        fakes.DB_VPN_CONNECTION_1,
                        {'os_ipsec_site_connections': {}})),
          mock.call(mock.ANY, self.DB_SUBNET_1_NO_VPN)])
     self.neutron.delete_vpnservice.assert_called_once_with(
         fakes.ID_OS_VPNSERVICE_1)
     self.neutron.delete_ipsec_site_connection.assert_called_once_with(
         fakes.ID_OS_IPSEC_SITE_CONNECTION_2)
 def test_replace_route_table_association_main(self, routes_updater):
     self.set_mock_db_items(fakes.DB_ROUTE_TABLE_1, fakes.DB_ROUTE_TABLE_2,
                            fakes.DB_VPC_1)
     resp = self.execute(
         'ReplaceRouteTableAssociation', {
             'AssociationId': fakes.ID_EC2_ROUTE_TABLE_ASSOCIATION_1,
             'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_2
         })
     self.assertEqual(fakes.ID_EC2_ROUTE_TABLE_ASSOCIATION_1,
                      resp['newAssociationId'])
     vpc = tools.update_dict(fakes.DB_VPC_1,
                             {'route_table_id': fakes.ID_EC2_ROUTE_TABLE_2})
     self.db_api.update_item.assert_called_once_with(mock.ANY, vpc)
     routes_updater.assert_called_once_with(mock.ANY,
                                            fakes.DB_ROUTE_TABLE_2,
                                            mock.ANY,
                                            fakes.DB_ROUTE_TABLE_1,
                                            is_main=True)
    def test_create_network_interface_rollback(self, _add_dhcp_opts_to_port):
        self.set_mock_db_items(
            tools.update_dict(
                fakes.DB_VPC_1,
                {'dhcp_options_id': fakes.ID_EC2_DHCP_OPTIONS_1}),
            fakes.DB_SUBNET_1, fakes.DB_DHCP_OPTIONS_1)
        self.db_api.add_item.return_value = fakes.DB_NETWORK_INTERFACE_1
        self.neutron.show_subnet.return_value = {'subnet': fakes.OS_SUBNET_1}
        self.neutron.create_port.return_value = {'port': fakes.OS_PORT_1}
        _add_dhcp_opts_to_port.side_effect = Exception()

        self.assert_execution_error(self.ANY_EXECUTE_ERROR,
                                    'CreateNetworkInterface',
                                    {'SubnetId': fakes.ID_EC2_SUBNET_1})

        self.neutron.delete_port.assert_called_once_with(fakes.ID_OS_PORT_1)
        self.db_api.delete_item.assert_called_once_with(
            mock.ANY, fakes.ID_EC2_NETWORK_INTERFACE_1)
        def do_check(params, fixed_ip):
            resp = self.execute('AssociateAddress', params)
            self.assertEqual(True, resp['return'])
            self.assertEqual(fakes.ID_EC2_ASSOCIATION_1, resp['associationId'])

            self.neutron.update_floatingip.assert_called_once_with(
                fakes.ID_OS_FLOATING_IP_1,
                {'floatingip': {'port_id': fakes.ID_OS_PORT_2,
                                'fixed_ip_address': fixed_ip}})
            self.db_api.update_item.assert_called_once_with(
                mock.ANY,
                tools.update_dict(
                    fakes.DB_ADDRESS_1,
                    {'network_interface_id':
                     fakes.ID_EC2_NETWORK_INTERFACE_2,
                     'private_ip_address': fixed_ip}))

            self.neutron.update_floatingip.reset_mock()
            self.db_api.update_item.reset_mock()
 def test_replace_route_table_association(self, routes_updater):
     self.set_mock_db_items(fakes.DB_ROUTE_TABLE_2, fakes.DB_ROUTE_TABLE_3,
                            fakes.DB_SUBNET_2)
     resp = self.execute(
         'ReplaceRouteTableAssociation', {
             'AssociationId': fakes.ID_EC2_ROUTE_TABLE_ASSOCIATION_3,
             'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_2
         })
     self.assertEqual(fakes.ID_EC2_ROUTE_TABLE_ASSOCIATION_2,
                      resp['newAssociationId'])
     subnet = tools.update_dict(
         fakes.DB_SUBNET_2, {'route_table_id': fakes.ID_EC2_ROUTE_TABLE_2})
     self.db_api.update_item.assert_called_once_with(mock.ANY, subnet)
     routes_updater.assert_called_once_with(
         mock.ANY,
         subnet,
         fakes.DB_ROUTE_TABLE_2,
         cleaner=mock.ANY,
         rollback_route_table_object=fakes.DB_ROUTE_TABLE_3)
 def test_associate_route_table(self, routes_updater):
     self.set_mock_db_items(fakes.DB_VPC_1, fakes.DB_ROUTE_TABLE_1,
                            fakes.DB_SUBNET_1)
     resp = self.execute(
         'AssociateRouteTable', {
             'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_1,
             'SubnetId': fakes.ID_EC2_SUBNET_1
         })
     self.assertEqual(fakes.ID_EC2_SUBNET_1.replace('subnet', 'rtbassoc'),
                      resp['associationId'])
     subnet = tools.update_dict(
         fakes.DB_SUBNET_1, {'route_table_id': fakes.ID_EC2_ROUTE_TABLE_1})
     self.db_api.update_item.assert_called_once_with(mock.ANY, subnet)
     routes_updater.assert_called_once_with(
         mock.ANY,
         subnet,
         fakes.DB_ROUTE_TABLE_1,
         cleaner=mock.ANY,
         rollback_route_table_object=fakes.DB_ROUTE_TABLE_1)
        def do_check(params, fixed_ip):
            resp = self.execute('AssociateAddress', params)
            self.assertEqual(True, resp['return'])
            self.assertEqual(fakes.ID_EC2_ASSOCIATION_1, resp['associationId'])

            self.neutron.update_floatingip.assert_called_once_with(
                fakes.ID_OS_FLOATING_IP_1,
                {'floatingip': {'port_id': fakes.ID_OS_PORT_2,
                                'fixed_ip_address': fixed_ip}})
            self.db_api.update_item.assert_called_once_with(
                mock.ANY,
                tools.update_dict(
                    fakes.DB_ADDRESS_1,
                    {'network_interface_id':
                     fakes.ID_EC2_NETWORK_INTERFACE_2,
                     'private_ip_address': fixed_ip}))

            self.neutron.update_floatingip.reset_mock()
            self.db_api.update_item.reset_mock()
 def test_attach_network_interface(self):
     self.set_mock_db_items(fakes.DB_NETWORK_INTERFACE_1,
                            fakes.DB_INSTANCE_1)
     self.neutron.show_port.return_value = (
         {'port': fakes.OS_PORT_1})
     self.isotime.return_value = fakes.TIME_ATTACH_NETWORK_INTERFACE
     self.execute(
         'AttachNetworkInterface',
         {'NetworkInterfaceId': fakes.ID_EC2_NETWORK_INTERFACE_1,
          'InstanceId': fakes.ID_EC2_INSTANCE_1,
          'DeviceIndex': '1'})
     self.nova.servers.interface_attach.assert_called_once_with(
         fakes.ID_OS_INSTANCE_1, fakes.ID_OS_PORT_1, None, None)
     self.db_api.update_item.assert_called_once_with(
         mock.ANY,
         tools.update_dict(
             fakes.DB_NETWORK_INTERFACE_1,
             {'device_index': 1,
              'instance_id': fakes.ID_EC2_INSTANCE_1,
              'delete_on_termination': False,
              'attach_time': fakes.TIME_ATTACH_NETWORK_INTERFACE}))
    def test_associate_route_table_invalid_parameters(self):
        def do_check(params, error_code):
            self.assert_execution_error(error_code, 'AssociateRouteTable',
                                        params)

        self.set_mock_db_items()
        do_check(
            {
                'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_1,
                'SubnetId': fakes.ID_EC2_SUBNET_1
            }, 'InvalidRouteTableID.NotFound')

        self.set_mock_db_items(fakes.DB_ROUTE_TABLE_1)
        do_check(
            {
                'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_1,
                'SubnetId': fakes.ID_EC2_SUBNET_1
            }, 'InvalidSubnetID.NotFound')

        id_ec2_subnet_vpc_2 = fakes.random_ec2_id('subnet')
        db_subnet_vpc_2 = {
            'id': id_ec2_subnet_vpc_2,
            'os_id': fakes.random_os_id(),
            'vpc_id': fakes.ID_EC2_VPC_2
        }
        self.set_mock_db_items(fakes.DB_ROUTE_TABLE_1, db_subnet_vpc_2)
        do_check(
            {
                'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_1,
                'SubnetId': id_ec2_subnet_vpc_2
            }, 'InvalidParameterValue')

        subnet_2 = tools.update_dict(
            fakes.DB_SUBNET_2, {'route_table_id': fakes.ID_EC2_ROUTE_TABLE_2})
        self.set_mock_db_items(fakes.DB_ROUTE_TABLE_1, subnet_2)
        do_check(
            {
                'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_1,
                'SubnetId': fakes.ID_EC2_SUBNET_2
            }, 'Resource.AlreadyAssociated')
 def test_detach_network_interface(self):
     network_interface = tools.update_dict(fakes.DB_NETWORK_INTERFACE_2,
                                           {'device_index': 1})
     self.set_mock_db_items(network_interface)
     self.neutron.show_port.return_value = (
         {'port': fakes.OS_PORT_2})
     self.execute(
         'DetachNetworkInterface',
         {'AttachmentId': ec2utils.change_ec2_id_kind(
                 fakes.ID_EC2_NETWORK_INTERFACE_2, 'eni-attach')})
     self.neutron.update_port.assert_called_once_with(
         fakes.ID_OS_PORT_2,
         {'port': {'device_id': '',
                   'device_owner': ''}}
     )
     self.db_api.update_item.assert_called_once_with(
         mock.ANY,
         tools.purge_dict(fakes.DB_NETWORK_INTERFACE_2,
                          {'device_index',
                           'instance_id',
                           'delete_on_termination',
                           'attach_time'}))
 def test_attach_network_interface(self):
     self.set_mock_db_items(fakes.DB_NETWORK_INTERFACE_1,
                            fakes.DB_INSTANCE_1)
     self.neutron.show_port.return_value = ({'port': fakes.OS_PORT_1})
     self.isotime.return_value = fakes.TIME_ATTACH_NETWORK_INTERFACE
     self.execute(
         'AttachNetworkInterface', {
             'NetworkInterfaceId': fakes.ID_EC2_NETWORK_INTERFACE_1,
             'InstanceId': fakes.ID_EC2_INSTANCE_1,
             'DeviceIndex': '1'
         })
     self.nova.servers.interface_attach.assert_called_once_with(
         fakes.ID_OS_INSTANCE_1, fakes.ID_OS_PORT_1, None, None)
     self.db_api.update_item.assert_called_once_with(
         mock.ANY,
         tools.update_dict(
             fakes.DB_NETWORK_INTERFACE_1, {
                 'device_index': 1,
                 'instance_id': fakes.ID_EC2_INSTANCE_1,
                 'delete_on_termination': False,
                 'attach_time': fakes.TIME_ATTACH_NETWORK_INTERFACE
             }))
Exemple #45
0
    def test_attach_igw(self):
        self.configure(external_network=fakes.NAME_OS_PUBLIC_NETWORK)
        self.set_mock_db_items(fakes.DB_IGW_1, fakes.DB_IGW_2, fakes.DB_VPC_2,
                               fakes.DB_VPN_GATEWAY_1, fakes.DB_VPN_GATEWAY_2)
        self.neutron.list_networks.return_value = ({
            'networks': [{
                'id': fakes.ID_OS_PUBLIC_NETWORK
            }]
        })

        def do_check():
            resp = self.execute(
                'AttachInternetGateway', {
                    'VpcId': fakes.ID_EC2_VPC_2,
                    'InternetGatewayId': fakes.ID_EC2_IGW_2
                })

            self.assertEqual(True, resp['return'])
            self.db_api.update_item.assert_called_once_with(
                mock.ANY, self.DB_IGW_2_ATTACHED)

        do_check()
        self.neutron.add_gateway_router.assert_called_once_with(
            fakes.ID_OS_ROUTER_2, {'network_id': fakes.ID_OS_PUBLIC_NETWORK})
        self.neutron.list_networks.assert_called_once_with(
            **{
                'router:external': True,
                'name': fakes.NAME_OS_PUBLIC_NETWORK
            })

        # VPN gateway is already attached
        self.db_api.reset_mock()
        self.neutron.reset_mock()
        vgw_2 = tools.update_dict(fakes.DB_VPN_GATEWAY_2,
                                  {'vpc_id': fakes.ID_EC2_VPC_2})
        self.add_mock_db_items(vgw_2)
        do_check()
        self.assertFalse(self.neutron.add_gateway_router.called)
    def test_detach_vpn_gateway(self):
        self.set_mock_db_items(
            fakes.DB_VPN_GATEWAY_1, fakes.DB_VPC_1, fakes.DB_VPN_CONNECTION_1,
            fakes.DB_SUBNET_1,
            tools.update_dict(fakes.DB_SUBNET_2,
                              {'vpc_id': fakes.ID_EC2_VPC_2}))

        def do_check():
            resp = self.execute(
                'DetachVpnGateway',
                {'VpcId': fakes.ID_EC2_VPC_1,
                 'VpnGatewayId': fakes.ID_EC2_VPN_GATEWAY_1})
            self.assertEqual({'return': True}, resp)
            self.assertEqual(3, self.db_api.update_item.call_count)
            self.db_api.update_item.assert_has_calls(
                [mock.call(mock.ANY, self.DB_VPN_GATEWAY_1_DETACHED),
                 mock.call(mock.ANY,
                           tools.update_dict(
                               fakes.DB_VPN_CONNECTION_1,
                               {'os_ipsec_site_connections': {}})),
                 mock.call(mock.ANY, self.DB_SUBNET_1_NO_VPN)])
            self.neutron.delete_vpnservice.assert_called_once_with(
                fakes.ID_OS_VPNSERVICE_1)
            self.neutron.delete_ipsec_site_connection.assert_called_once_with(
                fakes.ID_OS_IPSEC_SITE_CONNECTION_2)

        do_check()
        self.neutron.remove_gateway_router.assert_called_once_with(
            fakes.ID_OS_ROUTER_1)

        # Internet gateway is still attached
        self.db_api.reset_mock()
        self.neutron.reset_mock()
        self.add_mock_db_items(fakes.DB_IGW_1)

        do_check()
        self.neutron.remove_gateway_router.assert_not_called()
 def test_detach_network_interface(self):
     network_interface = tools.update_dict(fakes.DB_NETWORK_INTERFACE_2,
                                           {'device_index': 1})
     self.set_mock_db_items(network_interface)
     self.neutron.show_port.return_value = ({'port': fakes.OS_PORT_2})
     self.execute(
         'DetachNetworkInterface', {
             'AttachmentId':
             ec2utils.change_ec2_id_kind(fakes.ID_EC2_NETWORK_INTERFACE_2,
                                         'eni-attach')
         })
     self.neutron.update_port.assert_called_once_with(
         fakes.ID_OS_PORT_2,
         {'port': {
             'device_id': '',
             'device_owner': ''
         }})
     self.db_api.update_item.assert_called_once_with(
         mock.ANY,
         tools.purge_dict(
             fakes.DB_NETWORK_INTERFACE_2, {
                 'device_index', 'instance_id', 'delete_on_termination',
                 'attach_time'
             }))
    def test_get_route_table_vpn_cidrs(self):
        route_table_1 = copy.deepcopy(fakes.DB_ROUTE_TABLE_1)
        vpn_connection_1 = tools.update_dict(
            fakes.DB_VPN_CONNECTION_1, {'cidrs': []})
        vpn_connection_2 = tools.update_dict(
            vpn_connection_1, {'id': fakes.ID_EC2_VPN_CONNECTION_2})

        self.assertThat(
            vpn_connection_api._get_route_table_vpn_cidrs(
                route_table_1, fakes.DB_VPN_GATEWAY_1, []),
            matchers.DictMatches({}))

        self.assertThat(
            vpn_connection_api._get_route_table_vpn_cidrs(
                route_table_1, fakes.DB_VPN_GATEWAY_1,
                [vpn_connection_1, vpn_connection_2]),
            matchers.DictMatches({}))

        route_table_1['propagating_gateways'] = [fakes.ID_EC2_VPN_GATEWAY_1,
                                                 fakes.ID_EC2_VPN_GATEWAY_2]
        self.assertThat(
            vpn_connection_api._get_route_table_vpn_cidrs(
                route_table_1, fakes.DB_VPN_GATEWAY_1,
                [vpn_connection_1, vpn_connection_2]),
            matchers.DictMatches({}))

        vpn_connection_1['cidrs'] = ['cidr_1']
        self.assertThat(
            vpn_connection_api._get_route_table_vpn_cidrs(
                route_table_1, fakes.DB_VPN_GATEWAY_1,
                [vpn_connection_1, vpn_connection_2]),
            matchers.DictMatches({fakes.ID_EC2_VPN_CONNECTION_1: ['cidr_1']}))

        vpn_connection_2['cidrs'] = ['cidr_1', 'cidr_2']
        self.assertThat(
            vpn_connection_api._get_route_table_vpn_cidrs(
                route_table_1, fakes.DB_VPN_GATEWAY_1,
                [vpn_connection_1, vpn_connection_2]),
            matchers.DictMatches(
                {fakes.ID_EC2_VPN_CONNECTION_1: ['cidr_1'],
                 fakes.ID_EC2_VPN_CONNECTION_2: ['cidr_1', 'cidr_2']},
                orderless_lists=True))

        route_table_1['routes'] = [
            {'destination_cidr_block': 'fake_1',
             'network_interface_id': fakes.ID_EC2_NETWORK_INTERFACE_1},
            {'destination_cidr_block': 'fake_2',
             'gateway_id': None},
            {'destination_cidr_block': 'fake_3',
             'gateway_id': fakes.ID_EC2_IGW_1},
            {'destination_cidr_block': 'cidr_3',
             'gateway_id': fakes.ID_EC2_VPN_GATEWAY_1},
            {'destination_cidr_block': 'cidr_4',
             'gateway_id': fakes.ID_EC2_VPN_GATEWAY_1},
            {'destination_cidr_block': 'fake_4',
             'gateway_id': fakes.ID_EC2_VPN_GATEWAY_2}]

        self.assertThat(
            vpn_connection_api._get_route_table_vpn_cidrs(
                route_table_1, fakes.DB_VPN_GATEWAY_1,
                [vpn_connection_1, vpn_connection_2]),
            matchers.DictMatches(
                {fakes.ID_EC2_VPN_CONNECTION_1: ['cidr_1', 'cidr_3', 'cidr_4'],
                 fakes.ID_EC2_VPN_CONNECTION_2: ['cidr_1', 'cidr_2',
                                                 'cidr_3', 'cidr_4']},
                orderless_lists=True))

        route_table_1['propagating_gateways'] = [fakes.ID_EC2_VPN_GATEWAY_2]
        self.assertThat(
            vpn_connection_api._get_route_table_vpn_cidrs(
                route_table_1, fakes.DB_VPN_GATEWAY_1,
                [vpn_connection_1, vpn_connection_2]),
            matchers.DictMatches(
                {fakes.ID_EC2_VPN_CONNECTION_1: ['cidr_3', 'cidr_4'],
                 fakes.ID_EC2_VPN_CONNECTION_2: ['cidr_3', 'cidr_4']},
                orderless_lists=True))
    def test_detach_vpn_gateway(self, delete_vpnservice,
                                stop_gateway_vpn_connections):
        delete_vpnservice_calls = []
        delete_vpnservice.side_effect = (
            tools.deepcopy_call_args_saver(delete_vpnservice_calls))
        mock_manager = mock.Mock()
        mock_manager.attach_mock(delete_vpnservice, 'delete_vpnservice')
        mock_manager.attach_mock(stop_gateway_vpn_connections,
                                 'stop_gateway_vpn_connections')
        self.set_mock_db_items(
            fakes.DB_VPN_GATEWAY_1, fakes.DB_VPC_1, fakes.DB_VPN_CONNECTION_1,
            fakes.DB_SUBNET_1,
            tools.update_dict(fakes.DB_SUBNET_2,
                              {'vpc_id': fakes.ID_EC2_VPC_2}))

        def do_check():
            resp = self.execute(
                'DetachVpnGateway',
                {'VpcId': fakes.ID_EC2_VPC_1,
                 'VpnGatewayId': fakes.ID_EC2_VPN_GATEWAY_1})
            self.assertEqual({'return': True}, resp)
            self.assertEqual(3, self.db_api.update_item.call_count)
            self.db_api.update_item.assert_has_calls(
                [mock.call(mock.ANY, self.DB_VPN_GATEWAY_1_DETACHED),
                 mock.call(mock.ANY,
                           tools.update_dict(
                               fakes.DB_VPN_CONNECTION_1,
                               {'os_ipsec_site_connections': {}})),
                 mock.call(mock.ANY, self.DB_SUBNET_1_NO_VPN)])
            self.neutron.delete_vpnservice.assert_called_once_with(
                fakes.ID_OS_VPNSERVICE_1)
            self.assertEqual(1, len(delete_vpnservice_calls))
            self.assertEqual(
                mock.call(mock.ANY, self.neutron, mock.ANY, fakes.DB_SUBNET_1),
                delete_vpnservice_calls[0])
            self.assertIsInstance(delete_vpnservice_calls[0][1][2],
                                  common.OnCrashCleaner)
            stop_gateway_vpn_connections.assert_called_once_with(
                mock.ANY, self.neutron, mock.ANY,
                self.DB_VPN_GATEWAY_1_DETACHED)
            self.assertIsInstance(stop_gateway_vpn_connections.call_args[0][2],
                                  common.OnCrashCleaner)
            self.neutron.delete_ipsec_site_connection.assert_called_once_with(
                fakes.ID_OS_IPSEC_SITE_CONNECTION_2)
            mock_manager.assert_has_calls([
                mock.call.stop_gateway_vpn_connections(
                    *(mock.ANY for _x in range(4))),
                mock.call.delete_vpnservice(
                    *(mock.ANY for _x in range(4)))])

        do_check()
        self.neutron.remove_gateway_router.assert_called_once_with(
            fakes.ID_OS_ROUTER_1)

        # Internet gateway is still attached
        self.db_api.reset_mock()
        self.neutron.reset_mock()
        del delete_vpnservice_calls[:]
        stop_gateway_vpn_connections.reset_mock()
        mock_manager.reset_mock()
        self.add_mock_db_items(fakes.DB_IGW_1)
        do_check()
        self.assertFalse(self.neutron.remove_gateway_router.called)
    def test_attach_vpn_gateway(self, create_vpnservice,
                                reset_vpn_connections):
        create_vpnservice_calls = []
        create_vpnservice.side_effect = (
            tools.deepcopy_call_args_saver(create_vpnservice_calls))
        mock_manager = mock.Mock()
        mock_manager.attach_mock(create_vpnservice, 'create_vpnservice')
        mock_manager.attach_mock(reset_vpn_connections,
                                 'reset_vpn_connections')
        self.configure(external_network=fakes.NAME_OS_PUBLIC_NETWORK)
        subnet_2 = tools.patch_dict(fakes.DB_SUBNET_2,
                                    {'vpc_id': fakes.ID_EC2_VPC_2},
                                    ('os_vpnservice_id',))
        self.set_mock_db_items(
            fakes.DB_VPN_GATEWAY_1, fakes.DB_VPN_GATEWAY_2,
            fakes.DB_VPC_2, fakes.DB_IGW_1, fakes.DB_IGW_2,
            fakes.DB_SUBNET_1, subnet_2)
        subnet_2_updated = tools.update_dict(
            subnet_2, {'os_vpnservice_id': fakes.ID_OS_VPNSERVICE_2})
        os_vpnservice_2 = tools.patch_dict(fakes.OS_VPNSERVICE_2,
                                           {'router_id': fakes.ID_OS_ROUTER_2},
                                           ('id',))
        self.neutron.list_networks.return_value = (
            {'networks': [{'id': fakes.ID_OS_PUBLIC_NETWORK}]})
        self.neutron.create_vpnservice.side_effect = tools.get_neutron_create(
            'vpnservice', fakes.ID_OS_VPNSERVICE_2)

        def do_check():
            resp = self.execute('AttachVpnGateway',
                                {'VpcId': fakes.ID_EC2_VPC_2,
                                 'VpnGatewayId': fakes.ID_EC2_VPN_GATEWAY_2})
            self.assertEqual({'attachment': {'state': 'attached',
                                             'vpcId': fakes.ID_EC2_VPC_2}},
                             resp)
            self.assertEqual(2, self.db_api.update_item.call_count)
            self.db_api.update_item.assert_has_calls(
                [mock.call(mock.ANY, self.DB_VPN_GATEWAY_2_ATTACHED),
                 mock.call(mock.ANY, subnet_2_updated)])
            self.neutron.create_vpnservice.assert_called_once_with(
                {'vpnservice': os_vpnservice_2})
            self.assertEqual(1, len(create_vpnservice_calls))
            self.assertEqual(
                mock.call(mock.ANY, self.neutron, mock.ANY, subnet_2,
                          fakes.DB_VPC_2),
                create_vpnservice_calls[0])
            self.assertIsInstance(create_vpnservice_calls[0][1][2],
                                  common.OnCrashCleaner)
            reset_vpn_connections.assert_called_once_with(
                mock.ANY, self.neutron, mock.ANY,
                self.DB_VPN_GATEWAY_2_ATTACHED, subnets=[subnet_2_updated])
            self.assertIsInstance(reset_vpn_connections.call_args[0][2],
                                  common.OnCrashCleaner)
            mock_manager.assert_has_calls([
                mock.call.create_vpnservice(
                    *(mock.ANY for _x in range(5))),
                mock.call.reset_vpn_connections(
                    subnets=mock.ANY, *(mock.ANY for _x in range(4)))])

        do_check()
        self.neutron.add_gateway_router.assert_called_once_with(
            fakes.ID_OS_ROUTER_2,
            {'network_id': fakes.ID_OS_PUBLIC_NETWORK})
        self.neutron.list_networks.assert_called_once_with(
            **{'router:external': True,
               'name': fakes.NAME_OS_PUBLIC_NETWORK})

        # Internet gateway is already attached
        self.db_api.reset_mock()
        self.neutron.reset_mock()
        del create_vpnservice_calls[:]
        reset_vpn_connections.reset_mock()
        mock_manager.reset_mock()
        igw_2 = tools.update_dict(fakes.DB_IGW_2,
                                  {'vpc_id': fakes.ID_EC2_VPC_2})
        self.add_mock_db_items(igw_2)
        do_check()
        self.assertFalse(self.neutron.add_gateway_router.called)
    def test_describe_vpn_connections(self):
        self.set_mock_db_items(
            fakes.DB_VPN_CONNECTION_1, fakes.DB_VPN_CONNECTION_2,
            fakes.DB_CUSTOMER_GATEWAY_1, fakes.DB_CUSTOMER_GATEWAY_2,
            fakes.DB_VPN_GATEWAY_1, fakes.DB_VPN_GATEWAY_2,
            fakes.DB_VPC_1, fakes.DB_VPC_2)
        self.neutron.list_ikepolicies.return_value = {
            'ikepolicies': [fakes.OS_IKEPOLICY_1, fakes.OS_IKEPOLICY_2]}
        self.neutron.list_ipsecpolicies.return_value = {
            'ipsecpolicies': [fakes.OS_IPSECPOLICY_1, fakes.OS_IPSECPOLICY_2]}
        self.neutron.list_ipsec_site_connections.return_value = {
            'ipsec_site_connections': []}
        self.neutron.list_routers.return_value = {
            'routers': [fakes.OS_ROUTER_1, fakes.OS_ROUTER_2]}

        resp = self.execute('DescribeVpnConnections', {})
        vpns = [tools.update_dict(
                    vpn, {'customerGatewayConfiguration': 'DONTCARE'})
                for vpn in (fakes.EC2_VPN_CONNECTION_1,
                            fakes.EC2_VPN_CONNECTION_2)]
        self.assertThat(
            resp,
            matchers.DictMatches(
                {'vpnConnectionSet': vpns},
                orderless_lists=True))
        for vpn in (fakes.EC2_VPN_CONNECTION_1, fakes.EC2_VPN_CONNECTION_2):
            config = next(v['customerGatewayConfiguration']
                          for v in resp['vpnConnectionSet']
                          if v['vpnConnectionId'] == vpn['vpnConnectionId'])
            self.assertThat(
                config.encode(),
                matchers.XMLMatches(
                    vpn['customerGatewayConfiguration'].encode(),
                    orderless_sequence=True))
            self.assertTrue(config.startswith(
                '<?xml version=\'1.0\' encoding=\'UTF-8\'?>'))
        self.neutron.list_ikepolicies.assert_called_once_with(
            tenant_id=fakes.ID_OS_PROJECT)
        self.neutron.list_ipsecpolicies.assert_called_once_with(
            tenant_id=fakes.ID_OS_PROJECT)
        self.neutron.list_ipsec_site_connections.assert_called_once_with(
            tenant_id=fakes.ID_OS_PROJECT)
        self.neutron.list_routers.assert_called_once_with(
            tenant_id=fakes.ID_OS_PROJECT)

        resp = self.execute(
            'DescribeVpnConnections',
            {'VpnConnectionId.1': fakes.ID_EC2_VPN_CONNECTION_1})
        self.assertThat(
            resp,
            matchers.DictMatches(
                {'vpnConnectionSet': [vpns[0]]},
                orderless_lists=True))

        self.check_filtering(
            'DescribeVpnConnections', 'vpnConnectionSet',
            [('customer-gateway-configuration',
              '*' + fakes.PRE_SHARED_KEY_1 + '*'),
             ('customer-gateway-id', fakes.ID_EC2_CUSTOMER_GATEWAY_1),
             ('state', 'available'),
             ('option.static-routes-only', True),
             ('route.destination-cidr-block', fakes.CIDR_VPN_2_PROPAGATED_1),
             ('type', 'ipsec.1'),
             ('vpn-connection-id', fakes.ID_EC2_VPN_CONNECTION_1),
             ('vpn-gateway-id', fakes.ID_EC2_VPN_GATEWAY_1)])

        self.check_tag_support(
            'DescribeVpnConnections', 'vpnConnectionSet',
            fakes.ID_EC2_VPN_CONNECTION_1, 'vpnConnectionId')
    def test_get_route_table_vpn_cidrs(self):
        route_table_1 = copy.deepcopy(fakes.DB_ROUTE_TABLE_1)
        vpn_connection_1 = tools.update_dict(fakes.DB_VPN_CONNECTION_1, {"cidrs": []})
        vpn_connection_2 = tools.update_dict(vpn_connection_1, {"id": fakes.ID_EC2_VPN_CONNECTION_2})

        self.assertThat(
            vpn_connection_api._get_route_table_vpn_cidrs(route_table_1, fakes.DB_VPN_GATEWAY_1, []),
            matchers.DictMatches({}),
        )

        self.assertThat(
            vpn_connection_api._get_route_table_vpn_cidrs(
                route_table_1, fakes.DB_VPN_GATEWAY_1, [vpn_connection_1, vpn_connection_2]
            ),
            matchers.DictMatches({}),
        )

        route_table_1["propagating_gateways"] = [fakes.ID_EC2_VPN_GATEWAY_1, fakes.ID_EC2_VPN_GATEWAY_2]
        self.assertThat(
            vpn_connection_api._get_route_table_vpn_cidrs(
                route_table_1, fakes.DB_VPN_GATEWAY_1, [vpn_connection_1, vpn_connection_2]
            ),
            matchers.DictMatches({}),
        )

        vpn_connection_1["cidrs"] = ["cidr_1"]
        self.assertThat(
            vpn_connection_api._get_route_table_vpn_cidrs(
                route_table_1, fakes.DB_VPN_GATEWAY_1, [vpn_connection_1, vpn_connection_2]
            ),
            matchers.DictMatches({fakes.ID_EC2_VPN_CONNECTION_1: ["cidr_1"]}),
        )

        vpn_connection_2["cidrs"] = ["cidr_1", "cidr_2"]
        self.assertThat(
            vpn_connection_api._get_route_table_vpn_cidrs(
                route_table_1, fakes.DB_VPN_GATEWAY_1, [vpn_connection_1, vpn_connection_2]
            ),
            matchers.DictMatches(
                {fakes.ID_EC2_VPN_CONNECTION_1: ["cidr_1"], fakes.ID_EC2_VPN_CONNECTION_2: ["cidr_1", "cidr_2"]},
                orderless_lists=True,
            ),
        )

        route_table_1["routes"] = [
            {"destination_cidr_block": "fake_1", "network_interface_id": fakes.ID_EC2_NETWORK_INTERFACE_1},
            {"destination_cidr_block": "fake_2", "gateway_id": None},
            {"destination_cidr_block": "fake_3", "gateway_id": fakes.ID_EC2_IGW_1},
            {"destination_cidr_block": "cidr_3", "gateway_id": fakes.ID_EC2_VPN_GATEWAY_1},
            {"destination_cidr_block": "cidr_4", "gateway_id": fakes.ID_EC2_VPN_GATEWAY_1},
            {"destination_cidr_block": "fake_4", "gateway_id": fakes.ID_EC2_VPN_GATEWAY_2},
        ]

        self.assertThat(
            vpn_connection_api._get_route_table_vpn_cidrs(
                route_table_1, fakes.DB_VPN_GATEWAY_1, [vpn_connection_1, vpn_connection_2]
            ),
            matchers.DictMatches(
                {
                    fakes.ID_EC2_VPN_CONNECTION_1: ["cidr_1", "cidr_3", "cidr_4"],
                    fakes.ID_EC2_VPN_CONNECTION_2: ["cidr_1", "cidr_2", "cidr_3", "cidr_4"],
                },
                orderless_lists=True,
            ),
        )

        route_table_1["propagating_gateways"] = [fakes.ID_EC2_VPN_GATEWAY_2]
        self.assertThat(
            vpn_connection_api._get_route_table_vpn_cidrs(
                route_table_1, fakes.DB_VPN_GATEWAY_1, [vpn_connection_1, vpn_connection_2]
            ),
            matchers.DictMatches(
                {
                    fakes.ID_EC2_VPN_CONNECTION_1: ["cidr_3", "cidr_4"],
                    fakes.ID_EC2_VPN_CONNECTION_2: ["cidr_3", "cidr_4"],
                },
                orderless_lists=True,
            ),
        )
Exemple #53
0
    def test_describe_images_being_created(self):
        db_api = self.mock_db()
        glance = self.mock_glance()
        context = base.create_context()
        image_id = fakes.random_ec2_id('ami')
        image = {'id': image_id,
                 'os_id': None,
                 'is_public': False,
                 'description': 'fake desc'}
        db_api.set_mock_items(image)
        db_api.get_public_items.return_value = []

        # describe cases when no glance image exists
        glance.images.list.return_value = []
        expected = {'imagesSet': [{'imageId': image_id,
                                   'description': 'fake desc',
                                   'imageOwnerId': fakes.ID_OS_PROJECT,
                                   'imageState': 'pending',
                                   'imageType': 'machine',
                                   'isPublic': False}]}

        # describe all images
        result = image_api.describe_images(context)
        self.assertEqual(expected, result)

        # describe the image
        result = image_api.describe_images(context, image_id=[image_id])
        self.assertEqual(expected, result)

        # describe with filter
        result = image_api.describe_images(
            context, filter=[{'name': 'name', 'value': 'noname'}])
        self.assertEqual({'imagesSet': []}, result)

        # describe failed image
        image['state'] = 'failed'
        expected['imagesSet'][0]['imageState'] = 'failed'
        result = image_api.describe_images(base.create_context())
        self.assertEqual(expected, result)

        # describe cases when glance image exists, db item is yet not updated
        del image['state']
        os_image_id = fakes.random_os_id()
        os_image = {'id': os_image_id,
                    'owner': fakes.ID_OS_PROJECT,
                    'status': 'active',
                    'visibility': 'private',
                    'ec2_id': image_id}
        glance.images.list.return_value = [fakes.OSImage(os_image)]
        expected['imagesSet'] = [{
            'architecture': None,
            'creationDate': None,
            'description': 'fake desc',
            'imageId': image_id,
            'imageLocation': 'None (None)',
            'imageOwnerId': fakes.ID_OS_PROJECT,
            'imageState': 'available',
            'imageType': 'machine',
            'isPublic': False,
            'name': None,
            'rootDeviceType': 'instance-store'}]

        # describe all images
        result = image_api.describe_images(context)
        self.assertEqual(expected, result)
        db_api.update_item.assert_called_once_with(
            context, tools.update_dict(image, {'os_id': os_image_id}))

        # describe the image
        db_api.reset_mock()
        result = image_api.describe_images(context, image_id=[image_id])
        self.assertEqual(expected, result)
        db_api.update_item.assert_called_once_with(
            context, tools.update_dict(image, {'os_id': os_image_id}))
Exemple #54
0
    def test_describe_images_being_created(self):
        db_api = self.mock_db()
        glance = self.mock_glance()
        context = base.create_context()
        image_id = fakes.random_ec2_id('ami')
        image = {
            'id': image_id,
            'os_id': None,
            'is_public': False,
            'description': 'fake desc'
        }
        db_api.set_mock_items(image)
        db_api.get_public_items.return_value = []

        # describe cases when no glance image exists
        glance.images.list.return_value = []
        expected = {
            'imagesSet': [{
                'imageId': image_id,
                'description': 'fake desc',
                'imageOwnerId': fakes.ID_OS_PROJECT,
                'imageState': 'pending',
                'imageType': 'machine',
                'isPublic': False
            }]
        }

        # describe all images
        result = image_api.describe_images(context)
        self.assertEqual(expected, result)

        # describe the image
        result = image_api.describe_images(context, image_id=[image_id])
        self.assertEqual(expected, result)

        # describe failed image
        image['state'] = 'failed'
        expected['imagesSet'][0]['imageState'] = 'failed'
        result = image_api.describe_images(base.create_context())
        self.assertEqual(expected, result)

        # describe cases when glance image exists, db item is yet not updated
        del image['state']
        os_image_id = fakes.random_os_id()
        os_image = {
            'id': os_image_id,
            'owner': fakes.ID_OS_PROJECT,
            'status': 'active',
            'is_public': False,
            'properties': {
                'ec2_id': image_id
            }
        }
        glance.images.list.return_value = [fakes.OSImage(os_image)]
        expected['imagesSet'] = [{
            'architecture': None,
            'creationDate': None,
            'description': 'fake desc',
            'imageId': image_id,
            'imageLocation': 'None (None)',
            'imageOwnerId': fakes.ID_OS_PROJECT,
            'imageState': 'available',
            'imageType': 'machine',
            'isPublic': False,
            'name': None,
            'rootDeviceType': 'instance-store'
        }]

        # describe all images
        result = image_api.describe_images(context)
        self.assertEqual(expected, result)
        db_api.update_item.assert_called_once_with(
            context, tools.update_dict(image, {'os_id': os_image_id}))

        # describe the image
        db_api.reset_mock()
        result = image_api.describe_images(context, image_id=[image_id])
        self.assertEqual(expected, result)
        db_api.update_item.assert_called_once_with(
            context, tools.update_dict(image, {'os_id': os_image_id}))
Exemple #55
0
        'fakeInt': 1234,
        'fakeStr': 'fake',
        'fakeSet': [{'fakeData': 'fake'},
                    {'fakeData': 'fake'}],
    },
    'fakeEmptySet': [],
    'fakeComplexSet': [
        {'fakeSubSet': [{'fakeData': 'fake'},
                        {'fakeData': None}]},
        {'fakeSubSet': [{'fakeData': 'fake'},
                        {'fakeData': 'fake'}]},
    ],
}
DICT_FAKE_RESULT = {
    'FakeActionResponse': tools.update_dict(
        DICT_FAKE_RESULT_DATA,
        {'requestId': None})
}

XML_SINGLE_RESULT = '''
<CreateSnapshotResponse xmlns="http://ec2.amazonaws.com/doc/2009-11-30/">
  <requestId>req-8a80bb71-1e1d-49be-819f-fba429b0ddf1</requestId>
  <status>pending</status>
  <description/>
  <volumeId>vol-00000001</volumeId>
  <volumeSize>1</volumeSize>
  <progress/>
  <startTime>2014-06-04T19:55:55.448117</startTime>
  <ownerId/>
  <snapshotId>snap-00000001</snapshotId>
</CreateSnapshotResponse>