Beispiel #1
0
    def test_find_port(self):
        hostname = 'testhostname'
        ifname = 'testname'
        ifaddress = 'testinterface'
        node = create_node_by_address(hostname)

        port1 = create_port(node, self.EXAMPLE_IP[0])
        port1_find = find_port(self.EXAMPLE_IP[0])
        self.assertEqual(port1, port1_find)

        port2 = create_port(node, ifaddress='testinterface')
        port2_find = find_port(hostname=hostname, ifaddress=ifaddress)
        self.assertEqual(port2, port2_find)

        port3 = create_port(node, self.EXAMPLE_IP[1], ifname=ifname)
        port3_find = find_port(self.EXAMPLE_IP[1])
        self.assertEqual(port3, port3_find)

        port3_find = find_port(hostname=hostname, ifname=ifname)
        self.assertEqual(port3, port3_find)

        self.assertIsNone(find_port(address='www.exmaple.com'))
        self.assertIsNone(find_port(hostname='tmp', ifaddress='address'))
        self.assertIsNone(find_port(hostname='tmp', ifname='name'))

        self.assertRaises(ValueError, find_port, None)
        self.assertRaises(ValueError, find_port, ifaddress='address')
        self.assertRaises(ValueError, find_port, ifname='address')
Beispiel #2
0
 def test_find_port(self):
     hostname = 'testhostname'
     ifname = 'testname'
     ifaddress = 'testinterface'
     node = create_node_by_address(hostname)
     
     port1 = create_port(node, self.EXAMPLE_IP[0])
     port1_find = find_port(self.EXAMPLE_IP[0])
     self.assertEqual(port1, port1_find)
     
     port2 = create_port(node, ifaddress='testinterface')
     port2_find = find_port(hostname=hostname, ifaddress=ifaddress)
     self.assertEqual(port2, port2_find)
     
     port3 = create_port(node, self.EXAMPLE_IP[1], ifname=ifname)
     port3_find = find_port(self.EXAMPLE_IP[1])
     self.assertEqual(port3, port3_find)
     
     port3_find = find_port(hostname=hostname, ifname=ifname)
     self.assertEqual(port3, port3_find)
     
     self.assertIsNone(find_port(address='www.exmaple.com'))
     self.assertIsNone(find_port(hostname='tmp', ifaddress='address'))
     self.assertIsNone(find_port(hostname='tmp', ifname='name'))
     
     self.assertRaises(ValueError, find_port, None)
     self.assertRaises(ValueError, find_port, ifaddress='address')
     self.assertRaises(ValueError, find_port, ifname='address')
Beispiel #3
0
def interface_to_port(interface, create_new=True):
    """
    Creates a UNIS port from L{psapi.protocol.Interface} object.

    if the a Port already exists for the Interface, it just going to
    return the port instance without creating new Port object.

    see L{port_to_interface}.

    @param interface: L{psapi.protocol.Interface} object   

    @returns: L{topology.models.Port}
    """
    if not isinstance(interface, Interface):
        raise ValueError("interface must be of type psapi.protocol.Interface, "
                        "not %s" % type(interface))

    ipaddress = interface.ipAddress
    ifaddress = interface.ifAddress
    hostname =  interface.hostName
    ifname =  interface.ifName
    # TODO read URN from interface (need updating psapi)

    if hostname is None:
        hostname = interface.ifHostName

    port = find_port(ipaddress, hostname, ifaddress, ifname)

    if not port and create_new:
        try:
            node = Node.objects.get(names__value=hostname)
        except MultipleObjectsReturned:
            raise ValueError("Hostname is not unique")
        except ObjectDoesNotExist:
            node = find_node_by_address(hostname)
            if not node:
                node = create_node_by_address(hostname)

        port = create_port(node, ifaddress=ifaddress, ifname=ifname)

    return port