def test_find(self): pod_id = '1' node1_id = '2' node2_id = '3' slot1_id = '4' slot2_id = '5' port2_id = '7' pod = Pod(pod_id) node1 = Node(pod_id, node1_id, 'Spine', 'spine', pod) node2 = Node(pod_id, node2_id, 'Leaf', 'leaf', pod) linecard1 = Linecard(slot1_id, node1) linecard2 = Linecard(slot2_id, node2) linecard1.serial = 'SerialNumber1' linecard2.serial = 'SerialNumber2' interface2 = Interface(interface_type='eth', pod=pod_id, node=node2_id, module=slot2_id, port=port2_id, parent=linecard2) so = Search() so.node = node2_id results = pod.find(so) self.assertIn(node2, results) self.assertIn(linecard2, results) self.assertIn(interface2, results) self.assertEqual(len(results), 3) for result in results: self.assertEqual(result.node, node2_id) so = Search() so.serial = 'SerialNumber1' results = pod.find(so) self.assertIn(linecard1, results) self.assertEqual(len(results), 1) self.assertEqual(results[0].serial, 'SerialNumber1')
def test_find(self): pod_id = "1" node1_id = "2" node2_id = "3" slot1_id = "4" slot2_id = "5" port2_id = "7" pod = Pod(pod_id) node1 = Node(pod_id, node1_id, "Spine", "spine", pod) node2 = Node(pod_id, node2_id, "Leaf", "leaf", pod) linecard1 = Linecard(slot1_id, node1) linecard2 = Linecard(slot2_id, node2) linecard1.serial = "SerialNumber1" linecard2.serial = "SerialNumber2" interface2 = Interface( interface_type="eth", pod=pod_id, node=node2_id, module=slot2_id, port=port2_id, parent=linecard2 ) so = Search() so.node = node2_id results = pod.find(so) self.assertIn(node2, results) self.assertIn(linecard2, results) self.assertIn(interface2, results) self.assertEqual(len(results), 3) for result in results: self.assertEqual(result.node, node2_id) so = Search() so.serial = "SerialNumber1" results = pod.find(so) self.assertIn(linecard1, results) self.assertEqual(len(results), 1) self.assertEqual(results[0].serial, "SerialNumber1")
def test_pod_type(self): """ Test the pod type :return: None """ pod = Pod('1') self.assertEqual(pod.get_type(), 'pod')
def test_pod_get_json(self): """ Test pod get_json :return: None """ pod_id = '3' pod = Pod(pod_id) self.assertEqual(pod.get_json(), None)
def test_pod_get_url(self): """ Test pod get_url :return: None """ pod_id = "3" pod = Pod(pod_id) self.assertEqual(pod.get_url(), None)
def test_pod_name(self): """ Test the pod name :return: None """ pod_id = '1' pod = Pod(pod_id) self.assertEqual(pod.get_name(), 'pod-' + pod_id) self.assertEqual(pod.get_pod(), '1')
def test_pod_name(self): """ Test the pod name :return: None """ pod_id = "1" pod = Pod(pod_id) self.assertEqual(pod.get_name(), "pod-" + pod_id) self.assertEqual(pod.get_pod(), "1")
def test_eNode_parent(self): pod = Pod('1') node = ExternalSwitch(parent=pod) self.assertEqual(node.role, 'external_switch') self.assertEqual(node._parent, pod) children = pod.get_children() self.assertEqual(len(children), 1) for child in children: self.assertEqual(child, node) pod = Node('1', '101', 'Spine2', 'leaf') self.assertRaises(TypeError, ExternalSwitch, pod)
def test_eNode_parent(self): pod = Pod("1") node = ExternalSwitch(parent=pod) self.assertEqual(node.role, "external_switch") self.assertEqual(node._parent, pod) children = pod.get_children() self.assertEqual(len(children), 1) for child in children: self.assertEqual(child, node) pod = Node("1", "101", "Spine2", "leaf") self.assertRaises(TypeError, ExternalSwitch, pod)
def test_link_get_bad_parent(self): pod = Link session = Session(URL, LOGIN, PASSWORD) self.assertRaises(TypeError, Link.get, session, pod) pod = Pod('1') self.assertRaises(TypeError, Link.get, 'bad_session', pod)
def test_node_get_parent_pod(self): session = self.login_to_apic() pods = Pod.get(session) pod = pods[0] nodes = Node.get(session, parent=pod) self.assertEqual(len(nodes), len(pod.get_children())) self.assertEqual(nodes[0].get_parent(), pod)
def test_node_equal(self): """ Test node equal :return: None """ node_name = 'Leaf1' node_pod = '1' node_node = '3' node_role = 'leaf' node1 = Node(node_pod, node_node, node_name, node_role) node_name = 'Leaf2' node2 = Node(node_pod, node_node, node_name, node_role) self.assertNotEqual(node1, node2) node_name = 'Leaf1' node_pod = '2' node2 = Node(node_pod, node_node, node_name, node_role) self.assertNotEqual(node1, node2) node_pod = '1' node_node = '4' node2 = Node(node_pod, node_node, node_name, node_role) self.assertNotEqual(node1, node2) node_node = '3' node_role = 'controller' node2 = Node(node_pod, node_node, node_name, node_role) self.assertNotEqual(node1, node2) node_role = 'leaf' node2 = Node(node_pod, node_node, node_name, node_role) self.assertEqual(node1, node2) node2 = Pod(node_pod) self.assertNotEqual(node1, node2)
def test_parameters(self): pod = Pod('1') node1 = '2' node2 = '3' slot1 = '4' slot2 = '5' port1 = '6' port2 = '7' link = '101' link1 = Link(pod) link1.pod = pod.pod link1.link = link link1.node1 = node1 link1.slot1 = slot1 link1.port1 = port1 link1.node2 = node2 link1.slot2 = slot2 link1.port2 = port2 self.assertEqual(link1.pod, pod.pod) self.assertEqual(link1.link, link) self.assertEqual(link1.node1, node1) self.assertEqual(link1.slot1, slot1) self.assertEqual(link1.port1, port1) self.assertEqual(link1.node2, node2) self.assertEqual(link1.slot2, slot2) self.assertEqual(link1.port2, port2) self.assertEqual(link1.get_parent(), pod) self.assertEqual(link1.get_port_id1(), '1/2/4/6') self.assertEqual(link1.get_port_id2(), '1/3/5/7')
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables description = ('Simple application that logs on to the APIC and displays' ' the physical inventory.') creds = Credentials('apic', description) args = creds.get() # Login to APIC session = Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') sys.exit(0) # Print the inventory of each Pod pods = Pod.get(session) for pod in pods: pod.populate_children(deep=True) pod_name = 'Pod: %s' % pod.name print(pod_name) print('=' * len(pod_name)) print_inventory(pod)
def main(): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables description = "Simple application that logs on to the APIC and displays" " the physical inventory." creds = Credentials("apic", description) args = creds.get() # Login to APIC session = Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print("%% Could not login to APIC") sys.exit(0) # Print the inventory of each Pod pods = Pod.get(session) for pod in pods: pod.populate_children(deep=True) pod_name = "Pod: %s" % pod.name print(pod_name) print("=" * len(pod_name)) print_inventory(pod)
def test_pod_id(self): """ Test the pod id :return: None """ pod = Pod('1') self.assertEqual(pod.pod, '1')
def test_invalid_session_populate_children(self): """ Test node invalid populate_children :return: None """ pod1 = Pod('1') node = Node('1', '2', 'Spine1', 'spine', pod1) self.assertRaises(TypeError, node.populate_children)
def test_pod_str_name(self): """ Test pod string :return: None """ pod_id = '2' pod = Pod(pod_id) self.assertEqual(str(pod), 'pod-' + pod_id)
def test_get_systemcontroller_parent_exception(self): """ Checks that an exception is raised when a Linecard.get is called with the wrong parent type :return: """ session = self.login_to_apic() node = Pod('1') self.assertRaises(TypeError, Systemcontroller.get, session, node)
def test_populate_deep(self): session = self.login_to_apic() pods = Pod.get(session) pod = pods[0] pod.populate_children(deep=True) nodes = pod.get_children(Node) original_num_nodes = len(nodes) node_roles = set() for node in nodes: node_roles.add(node.get_role()) if node.get_role() == 'spine' and node.fabricSt == 'active': spine = node if node.get_role() == 'controller' and node.fabricSt != 'inactive': controller = node self.assertEqual(len(node_roles ^ {'controller', 'spine', 'leaf'}), 0) modules = spine.get_children() module_types = set() for module in modules: module_types.add(module.get_type()) if module.get_type() == 'linecard': linecard = module self.assertEqual( len(module_types ^ {'linecard', 'supervisor', 'powersupply', 'fantray'}), 0) interfaces = linecard.get_children() for interface in interfaces: self.assertIsInstance(interface, Interface) if linecard.model == 'N9K-X9736PQ': self.assertEqual(len(interfaces), 36) modules = controller.get_children() module_types = set() for module in modules: module_types.add(module.get_type()) self.assertEqual( len(module_types ^ {'systemctrlcard', 'powersupply', 'fantray'}), 0) links = pod.get_children(Link) for link in links: self.assertIsInstance(link, Link) self.assertIsInstance(link.node1, str) self.assertIsInstance(link.node2, str) self.assertIsInstance(link.slot1, str) self.assertIsInstance(link.slot2, str) self.assertIsInstance(link.port1, str) self.assertIsInstance(link.port2, str) self.assertIsInstance(link.link, str) # check that duplicate children are not populated pod.populate_children(deep=True) nodes = pod.get_children(Node) self.assertTrue(len(nodes) == original_num_nodes)
def test_node_parent(self): """ Test node parent :return: None """ pod_id = '1' pod1 = Pod(pod_id) node = Node('1', '2', 'Spine1', role='leaf', parent=pod1) self.assertEqual(pod1, node.get_parent())
def test_pod_valid_parent(self): """ Tests that the parent class of the pod :return: """ session = self.login_to_apic() parent = PhysicalModel() pod = Pod.get(session, parent) children = parent.get_children() self.assertEqual(pod, children)
def test_populate_deep(self): session = self.login_to_apic() pods = Pod.get(session) pod = pods[0] pod.populate_children(deep=True) nodes = pod.get_children(Node) original_num_nodes = len(nodes) node_roles = set() for node in nodes: node_roles.add(node.get_role()) if node.get_role() == 'spine' and node.fabricSt == 'active': spine = node if node.get_role() == 'controller' and node.fabricSt != 'inactive': controller = node self.assertEqual(len(node_roles ^ {'controller', 'spine', 'leaf'}), 0) modules = spine.get_children() module_types = set() for module in modules: module_types.add(module.get_type()) if module.get_type() == 'linecard': linecard = module self.assertEqual(len(module_types ^ {'linecard', 'supervisor', 'powersupply', 'fantray'}), 0) interfaces = linecard.get_children() for interface in interfaces: self.assertIsInstance(interface, Interface) if linecard.model == 'N9K-X9736PQ': self.assertEqual(len(interfaces), 36) modules = controller.get_children() module_types = set() for module in modules: module_types.add(module.get_type()) self.assertEqual(len(module_types ^ {'systemctrlcard', 'powersupply', 'fantray'}), 0) links = pod.get_children(Link) for link in links: self.assertIsInstance(link, Link) self.assertIsInstance(link.node1, str) self.assertIsInstance(link.node2, str) self.assertIsInstance(link.slot1, str) self.assertIsInstance(link.slot2, str) self.assertIsInstance(link.port1, str) self.assertIsInstance(link.port2, str) self.assertIsInstance(link.link, str) # check that duplicate children are not populated pod.populate_children(deep=True) nodes = pod.get_children(Node) self.assertTrue(len(nodes) == original_num_nodes)
def test_pod_equal(self): """ Test pods are equal :return: None """ pod_id = '1' pod1 = Pod(pod_id) # check different IDs pod_id = '2' pod2 = Pod(pod_id) self.assertNotEqual(pod1, pod2) # check same pod_id = '1' pod2 = Pod(pod_id) self.assertEqual(pod1, pod2) # check different types pod2 = Node(pod_id, '2', 'Leaf1', role='leaf') self.assertNotEqual(pod1, pod2)
def test_link_get_for_node(self): session = self.login_to_apic() pod = Pod.get(session)[0] links = Link.get(session) total_links = len(links) self.assertTrue(total_links > 0) self.assertRaises(TypeError, links[0].get_node1) self.assertRaises(TypeError, links[0].get_node2) self.assertRaises(TypeError, links[0].get_slot1) self.assertRaises(TypeError, links[0].get_slot2) self.assertRaises(TypeError, links[0].get_port1) self.assertRaises(TypeError, links[0].get_port2) links = Link.get(session, pod) self.assertEqual(len(links), total_links) switches = [] for link in links: self.assertEqual(link.get_node1(), None) self.assertEqual(link.get_slot1(), None) self.assertEqual(link.get_port1(), None) self.assertEqual(link.get_node2(), None) self.assertEqual(link.get_slot2(), None) self.assertEqual(link.get_port2(), None) if link.node1 not in switches: switches.append(link.node1) self.assertTrue(len(switches) > 1) nodes = Node.get(session) spine = None for node in nodes: if node.get_role() == 'spine' and node.fabricSt == 'active': spine = node break if spine: links = Link.get(session, pod, spine.node) spine_links = len(links) self.assertTrue(spine_links > 0) self.assertTrue(spine_links < total_links) for link in links: self.assertEqual(link.node1, spine.node) self.assertIsInstance(str(link), str) links = Link.get(session, '1', spine.node) self.assertEqual(len(links), spine_links) self.assertNotEqual(links[0], links[1])
def test_get_endpoint_objects(self): pod_id = '1' node1_id = '2' node2_id = '3' slot1_id = '4' slot2_id = '5' port1_id = '6' port2_id = '7' link_id = '101' pod = Pod(pod_id) node1 = Node(pod_id, node1_id, 'Spine', 'spine', pod) node2 = Node(pod_id, node2_id, 'Leaf', 'leaf', pod) linecard1 = Linecard(slot1_id, node1) linecard2 = Linecard(slot2_id, node2) interface1 = Interface(interface_type='eth', pod=pod_id, node=node1_id, module=slot1_id, port=port1_id, parent=linecard1) interface2 = Interface(interface_type='eth', pod=pod_id, node=node2_id, module=slot2_id, port=port2_id, parent=linecard2) link1 = Link(pod) link1.pod = pod_id link1.link = link_id link1.node1 = node1_id link1.slot1 = slot1_id link1.port1 = port1_id link1.node2 = node2_id link1.slot2 = slot2_id link1.port2 = port2_id self.assertEqual(node1, link1.get_node1()) self.assertEqual(node2, link1.get_node2()) self.assertEqual(linecard1, link1.get_slot1()) self.assertEqual(linecard2, link1.get_slot2()) self.assertEqual(interface1, link1.get_port1()) self.assertEqual(interface2, link1.get_port2())
def main(): """ Main execution routine :return: None """ description = ('Simple application that logs on to the APIC and displays' ' the physical inventory.') # Login to APIC session = aci.Session(URL, LOGIN, PASSWORD) session.login() # Print the inventory of each Pod pods = Pod.get(session) for pod in pods: pod.populate_children(deep=True) pod_name = 'Pod: %s' % pod.name print(pod_name) print('=' * len(pod_name)) print_inventory(pod)
def main(login, password, url): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables description = ('Simple application that logs on to the APIC and displays' ' the physical inventory.') #creds = Credentials('apic', description) #args1 = creds.get() #print type(args1) #print "args url ", args1.url #print "args user ", args1.login #print "args password", args1.password #url = 'http://172.31.216.24' #login = '******' #password = '******' # Login to APIC session = Session(url, login, password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') sys.exit(0) # Print the inventory of each Pod pods = Pod.get(session) for pod in pods: pod.populate_children(deep=True) pod_name = 'Pod: %s' % pod.name print(pod_name) print('=' * len(pod_name)) print_inventory(pod)
def main(login,password,url): """ Main execution routine :return: None """ # Take login credentials from the command line if provided # Otherwise, take them from your environment variables description = ('Simple application that logs on to the APIC and displays' ' the physical inventory.') #creds = Credentials('apic', description) #args1 = creds.get() #print type(args1) #print "args url ", args1.url #print "args user ", args1.login #print "args password", args1.password #url = 'http://172.31.216.24' #login = '******' #password = '******' # Login to APIC session = Session(url, login, password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') sys.exit(0) # Print the inventory of each Pod pods = Pod.get(session) for pod in pods: pod.populate_children(deep=True) pod_name = 'Pod: %s' % pod.name print(pod_name) print('=' * len(pod_name)) print_inventory(pod)
def get_all_pods(self): session = self.login_to_apic() pods = Pod.get(session) self.assertTrue(len(pods) > 0) return pods, session