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_gets_all_allocated_ips_without_project_id(self):
        network_id = "network123"
        vif_id = "vif1"
        mock_client = setup_mock_client(self.mox)
        stub_response = ResponseStub({'ip_addresses': [{'id': "123"}]})
        mock_client.get("/v0.1/ipam/networks/network123/"
                         "interfaces/vif1/ip_allocations",
                         headers=json_content_type()).AndReturn(stub_response)

        self.mox.ReplayAll()

        ip_addresses = melange_client.get_allocated_ips(network_id, vif_id,
                                                        project_id=None)
        self.assertEqual(ip_addresses, [{'id': "123"}])
 def get_ips(self, interface):
     project_id = interface['network']['project_id']
     return melange.get_allocated_ips(interface['network_id'],
                                     interface['id'],
                                     project_id=project_id)