def test_get_all_allocated_ips_for_an_interface(self):
        quantum_mgr = QuantumManager()
        interface = dict(network_id="network123", id="vif_id",
                         network=dict(project_id="project1"))
        self.mox.StubOutWithMock(melange_client, 'get_allocated_ips')
        allocated_v4ip = dict(address="10.1.1.2", version=4)
        allocated_v6ip = dict(address="fe::2", version=6)

        melange_client.get_allocated_ips("network123", "vif_id",
                                         project_id="project1").AndReturn([
            allocated_v4ip, allocated_v6ip])
        self.mox.ReplayAll()

        ips = quantum_mgr.get_ips(interface)
        self.assertEqual(ips, [allocated_v4ip, allocated_v6ip])
    def test_get_network_info(self):
        quantum_mgr = QuantumManager()
        admin_context = context.get_admin_context()
        instance = db_api.instance_create(admin_context, {})

        network1 = db_api.network_create_safe(admin_context,
                                              dict(label="private1",
                                                   project_id="project1",
                                                   priority=1))
        network2 = db_api.network_create_safe(admin_context,
                                              dict(label="private2",
                                                   project_id="project1",
                                                   priority=1))

        vif1 = db_api.virtual_interface_create(admin_context,
                                         dict(address="11:22:33:44:55:66",
                                              instance_id=instance['id'],
                                              network_id=network1['id']))
        vif2 = db_api.virtual_interface_create(admin_context,
                                         dict(address="66:22:33:44:55:66",
                                              instance_id=instance['id'],
                                              network_id=network2['id']))

        self.mox.StubOutWithMock(melange_client, 'get_allocated_ips')
        block1 = 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")
        ip1 = dict(address="10.1.1.2", version=4, ip_block=block1)
        block2 = dict(netmask="255.255.255.0", cidr="77.1.1.0/24",
                      gateway="77.1.1.1", broadcast="77.1.1.255",
                      dns1="1.2.3.4", dns2=None)
        ip2 = dict(address="77.1.1.2", version=4, ip_block=block2)

        melange_client.get_allocated_ips(network1['id'], vif1['id'],
                                         project_id="project1").\
                                         AndReturn([ip1])

        melange_client.get_allocated_ips(network2['id'], vif2['id'],
                                         project_id="project1").\
                                         AndReturn([ip2])

        self.mox.ReplayAll()

        net_info = quantum_mgr.get_instance_nw_info(admin_context,
                                                    instance['id'], 1, None)
        assert_network_info_has_ip(self, net_info[0], ip1, network1)
        assert_network_info_has_ip(self, net_info[1], ip2, network2)
    def test_deallocates_ips_from_melange(self):
        quantum_mgr = QuantumManager()
        admin_context = context.get_admin_context()
        project_id = "project1"

        instance_id = db_api.instance_create(admin_context, dict())['id']
        network1 = db_api.network_create_safe(admin_context,
                                             dict(instance_id=instance_id,
                                                  priority=1,
                                                  project_id=project_id))
        network2 = db_api.network_create_safe(admin_context,
                                             dict(instance_id=instance_id,
                                                  priority=2,
                                                  project_id=project_id))

        vif1 = db_api.virtual_interface_create(admin_context,
                                              dict(instance_id=instance_id,
                                              network_id=network1['id'],
                                              project_id=project_id))
        vif2 = db_api.virtual_interface_create(admin_context,
                                              dict(instance_id=instance_id,
                                              network_id=network2['id'],
                                              project_id=project_id))
        self._setup_quantum_mocks()

        self.mox.StubOutWithMock(melange_client, "deallocate_ips")
        melange_client.deallocate_ips(network1['id'], vif1['id'],
                                      project_id=project_id)
        melange_client.deallocate_ips(network2['id'], vif2['id'],
                                      project_id=project_id)

        self.mox.ReplayAll()

        quantum_mgr.deallocate_for_instance(admin_context,
                                            instance_id=instance_id,
                                            project_id=project_id)

        vifs_left = db_api.virtual_interface_get_by_instance(admin_context,
                                                             instance_id)
        self.assertEqual(len(vifs_left), 0)
    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_private_net_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")