def test_allocates_v4_ips_for_public_network(self):
        public_network = db_api.network_create_safe(admin_context,
                                    dict(label="public", project_id=None,
                                         priority=1))
        private_noise_network = db_api.network_create_safe(admin_context,
                        dict(label="private", project_id="another_project",
                             priority=1))

        public_v4block = dict(netmask="255.255.255.0", cidr="10.1.1.0/24",
                               gateway="10.1.1.1", broadcast="10.1.1.255",
                               dns1="1.2.3.4", dns2="2.3.4.5")
        public_v4ip = dict(address="10.1.1.2", version=4,
                            ip_block=public_v4block)
        melange_client.allocate_ip(public_network.id, IgnoreArg(),
                                   project_id=None,
                                   mac_address=IgnoreArg())\
                                   .InAnyOrder().AndReturn([public_v4ip])

        self.mox.ReplayAll()

        net_info = QuantumManager().allocate_for_instance(admin_context,
                                               instance_id=self.instance_id,
                                               host="localhost",
                                               project_id="project1",
                                               instance_type_id=1,
                                               vpn="vpn_address")

        self.assertEqual(len(net_info), 1)
        assert_network_info_has_ip(self, net_info[0], public_v4ip,
                                         public_network)
Ejemplo n.º 2
0
        def allocate_ip(vif):
            network_for_vif = lambda net: net['id'] == vif['network_id']
            project_id = filter(network_for_vif, networks)[0].project_id

            return melange.allocate_ip(vif['network_id'],
                                       vif['id'], project_id=project_id,
                                       mac_address=vif['address'])
    def _setup_network_and_melange_ip(self, address, cidr,
                                      net_label=None, project_id=None):
        ip_block = IPNetwork(cidr)
        network = db_api.network_create_safe(admin_context,
                                  dict(label='private',
                                       project_id="project1", priority=1))

        block = dict(netmask=ip_block.netmask, cidr=cidr, gateway=ip_block[1],
                     broadcast=ip_block.broadcast, dns1="1.2.3.4",
                     dns2="2.3.4.5")
        ip = dict(address=address, version=ip_block.version, ip_block=block)

        melange_client.allocate_ip(network.id, IgnoreArg(),
                                   project_id="project1",
                                   mac_address=IgnoreArg())\
                                   .InAnyOrder().AndReturn([ip])
        return network, ip
    def test_allocates_v6_ips_from_melange(self):
        quantum_mgr = QuantumManager()
        mac_address = "11:22:33:44:55:66"
        self._stub_out_mac_address_generation(mac_address, quantum_mgr)
        network = db_api.network_create_safe(admin_context,
                                             dict(project_id="project1",
                                                  cidr_v6="fe::/96",
                                                  priority=1))

        v4_block = dict(netmask="255.255.255.0", cidr="10.1.1.0/24",
                               gateway="10.1.1.1", broadcast="10.1.1.255",
                               dns1="1.2.3.4", dns2="2.3.4.5")
        allocated_v4ip = dict(address="10.1.1.2", version=4,
                              ip_block=v4_block)

        v6_block = dict(netmask="f:f:f:f::", cidr="fe::/96",
                        gateway="fe::1", broadcast="fe::ffff:ffff")
        allocated_v6ip = dict(address="fe::2", version=6, ip_block=v6_block)
        v6_block_prefix_length = 96

        melange_client.allocate_ip(network.id, IgnoreArg(),
                                   project_id="project1",
                                   mac_address=mac_address)\
                                   .AndReturn([allocated_v4ip, allocated_v6ip])
        self.mox.ReplayAll()

        [net_info] = quantum_mgr.allocate_for_instance(admin_context,
                                               instance_id=self.instance_id,
                                               host="localhost",
                                               project_id="project1",
                                               instance_type_id=1,
                                               vpn="vpn_address")
        vif_config_net_params = net_info[1]

        assert_network_info_has_ip(self, net_info, allocated_v4ip, network)
        self.assertEqual(vif_config_net_params['ip6s'],
                         [{'ip': 'fe::2',
                           'netmask': v6_block_prefix_length,
                           'enabled': '1'}])
        self.assertEqual(vif_config_net_params['gateway6'], "fe::1")
    def test_allocate_ip_without_a_project_id(self):
        network_id = "network333"
        vif_id = "vif1"
        mock_client = setup_mock_client(self.mox)
        stub_response = ResponseStub({'ip_addresses': [{'id': "123"}]})
        mock_client.post("/v0.1/ipam/networks/network333/"
                         "ports/vif1/ip_allocations", body=None,
                         headers=json_content_type()).AndReturn(stub_response)

        self.mox.ReplayAll()

        ip_addresses = melange_client.allocate_ip(network_id, vif_id,
                                                  project_id=None)
        self.assertEqual(ip_addresses, [{'id': "123"}])
    def test_allocate_ip_for_a_given_project_id(self):
        network_id = "network1"
        vif_id = "vif1"
        project_id = "project2"
        mac_address = "11:22:33:44:55:66"
        request_body = json.dumps(dict(network=dict(mac_address=mac_address)))
        mock_client = setup_mock_client(self.mox)
        stub_response = ResponseStub({'ip_addresses': [{'id': "123"}]})
        mock_client.post("/v0.1/ipam/tenants/project2/networks/network1/"
                         "ports/vif1/ip_allocations", body=request_body,
                         headers=json_content_type()).AndReturn(stub_response)

        self.mox.ReplayAll()

        ip_addresses = melange_client.allocate_ip(network_id, vif_id,
                        project_id=project_id, mac_address=mac_address)
        self.assertEqual(ip_addresses, [{'id': "123"}])