def test_add_neighbor_existing_interface(self): systemmac = random_string() peer = Mock() intf = random_string() node = Node(systemmac=systemmac) node.add_neighbor(intf, [dict(device=peer.remote_device, port=peer.remote_interface)]) self.assertRaises(NodeError, node.add_neighbor, intf, [dict(device=peer.remote_device, port=peer.remote_interface)])
def test_add_neighbor_existing_interface(self): systemmac = random_string() peer = Mock() intf = random_string() node = Node(systemmac=systemmac) node.add_neighbor( intf, [dict(device=peer.remote_device, port=peer.remote_interface)]) self.assertRaises( NodeError, node.add_neighbor, intf, [dict(device=peer.remote_device, port=peer.remote_interface)])
def test_create_node_neighbors_valid(self): nodeattrs = create_node() remote_device = random_string() remote_interface = random_string() neighbors = {'Ethernet1': {'device': remote_device, 'port': remote_interface}} nodeattrs.add_neighbors(neighbors) kwargs = nodeattrs.as_dict() node = Node(**kwargs) self.assertIsNotNone(node.neighbors('Ethernet1')) self.assertEqual(node.neighbors('Ethernet1')[0].device, remote_device) self.assertEqual(node.neighbors('Ethernet1')[0].interface, remote_interface)
def load_node(node, content_type=CONTENT_TYPE_YAML): try: if not hasattr(node, 'items'): node = load_file(node, content_type, ID) for symbol in [':', '.']: node['systemmac'] = str(node['systemmac']).replace(symbol, '') return Node(**node) except TypeError: log.error('Failed to load node') except KeyError as err: log.error('Failed to load node - missing attribute: %s' % err)
def test_create_node_neighbors_valid(self): nodeattrs = create_node() remote_device = random_string() remote_interface = random_string() neighbors = { 'Ethernet1': { 'device': remote_device, 'port': remote_interface } } nodeattrs.add_neighbors(neighbors) kwargs = nodeattrs.as_dict() node = Node(**kwargs) self.assertIsNotNone(node.neighbors('Ethernet1')) self.assertEqual(node.neighbors('Ethernet1')[0].device, remote_device) self.assertEqual( node.neighbors('Ethernet1')[0].interface, remote_interface)
def test_create_node_neighbors_remote_interface_missing(self): nodeattrs = create_node() remote_device = random_string() neighbors = {'Ethernet1': {'remote_device': remote_device}} nodeattrs.add_neighbors(neighbors) kwargs = nodeattrs.as_dict() try: node = None node = Node(**kwargs) except NodeError: pass finally: self.assertIsNone(node)
def test_add_neighbor(self): systemmac = random_string() peer = Mock() intf = random_string() node = Node(systemmac=systemmac) node.add_neighbor( intf, [dict(device=peer.remote_device, port=peer.remote_interface)]) self.assertIsNotNone(node.neighbors(intf)) self.assertEqual(node.neighbors(intf)[0].device, peer.remote_device) self.assertEqual( node.neighbors(intf)[0].interface, peer.remote_interface)
def test_add_neighbor(self): systemmac = random_string() peer = Mock() intf = random_string() node = Node(systemmac=systemmac) node.add_neighbor(intf, [dict(device=peer.remote_device, port=peer.remote_interface)]) self.assertIsNotNone(node.neighbors(intf)) self.assertEqual(node.neighbors(intf)[0].device, peer.remote_device) self.assertEqual(node.neighbors(intf)[0].interface, peer.remote_interface)
def test_create_node_failure(cls): try: _ = Node() except TypeError: pass
def test_create_node_systemmac_only(self): systemmac = random_string() node = Node(systemmac=systemmac) self.assertEqual(node.systemmac, systemmac)
def test_create_node_success(self): node = create_node() systemmac = node.systemmac kwargs = node.as_dict() node = Node(**kwargs) self.assertEqual(node.systemmac, systemmac)
def test_serialize_success(self): nodeattrs = create_node() kwargs = nodeattrs.as_dict() node = Node(**kwargs) result = node.serialize() self.assertEqual(result, nodeattrs.as_dict())