def test_set_attributes(self): intf1 = Interface('eth', '1', '2', '3', '4') intf2 = Interface('eth', '6', '7', '8', '9') self.assertTrue(intf1.attributes['interface_type'] == 'eth') self.assertTrue(intf1.attributes['pod'] == '1') self.assertTrue(intf1.attributes['node'] == '2') self.assertTrue(intf1.attributes['module'] == '3') self.assertTrue(intf1.attributes['port'] == '4') self.assertTrue(intf2.attributes['interface_type'] == 'eth') self.assertTrue(intf2.attributes['pod'] == '6') self.assertTrue(intf2.attributes['node'] == '7') self.assertTrue(intf2.attributes['module'] == '8') self.assertTrue(intf2.attributes['port'] == '9')
def test_preexisting_endpoints_consume_imported_contract(self): self.setup_export_contract() self.consume_exported_contract() session2 = self._login_session(SITE2_URL, SITE2_LOGIN, SITE2_PASSWORD) tenant = Tenant('multisite') app = AppProfile('my-demo-app', tenant) new_epg_name = 'another-epg' web_epg = EPG(new_epg_name, app) # Create the Endpoint mac = '00:77:55:44:33:22' ip = '8.3.2.1' ep = Endpoint(mac, web_epg) ep.mac = mac ep.ip = ip intf = Interface('eth', '1', '101', '1', '38') # Create a VLAN interface and attach to the physical interface vlan_intf = L2Interface('vlan-5', 'vlan', '5') vlan_intf.attach(intf) # Attach the EPG to the VLAN interface web_epg.attach(vlan_intf) # Assign Endpoint to the L2Interface ep.attach(vlan_intf) resp = tenant.push_to_apic(session2) self.assertTrue(resp.ok) self.consume_exported_contract(epg_name=new_epg_name) tenants = Tenant.get_deep(session2, names=['multisite']) multisite_tenant = tenants[0] app = multisite_tenant.get_child(AppProfile, 'my-demo-app') self.assertIsNotNone(app) epg = app.get_child(EPG, new_epg_name) self.assertIsNotNone(epg) multisite_ep = epg.get_child(Endpoint, mac) self.assertIsNotNone(multisite_ep) session1 = self._login_session(SITE1_URL, SITE1_LOGIN, SITE1_PASSWORD) self._assert_l3extsubnet_exists(session1, tenant_name='multisite', mac=mac, ip=ip) self.unconsume_exported_contract(new_epg_name) time.sleep(2) self._assert_l3extsubnet_does_not_exist(session1, tenant_name='multisite', mac=mac, ip=ip) web_epg.mark_as_deleted() tenant.push_to_apic(session2) time.sleep(1) self.teardown_export_contract()
def test_create_valid_phydomain(self): intf = Interface('eth', '1', '1', '1', '1') (phydomain_json, fabric_json, infra_json) = intf.get_json() expected_json = ("{'physDomP': {'attributes': {'name': 'allvlans'}, 'c" "hildren': [{'infraRsVlanNs': {'attributes': {'tDn': " "'uni/infra/vlanns-allvlans-static'}, 'children': []}" "}]}}") self.assertEqual(str(phydomain_json), expected_json)
def test_adminstate_down(self): intf = Interface('eth', '1', '1', '1', '1') intf.adminstatus = 'down' phys_domain_json, fabric_json, infra_json = intf.get_json() expected_json = ("{'fabricOOServicePol': {'children': [{'fabricRsOosPa" "th': {'attributes': {'tDn': 'topology/pod-1/paths-1/" "pathep-[eth1/1]', 'lc': 'blacklist'}, 'children': []" "}}]}}") self.assertEqual(str(fabric_json), expected_json)
def test_adminstate_up(self): intf = Interface('eth', '1', '1', '1', '1') intf.adminstatus = 'up' phys_domain_json, fabric_json, infra_json = intf.get_json() expected_json = ("{'fabricOOServicePol': {'children': [{'fabricRsOosPa" "th': {'attributes': {'dn': 'uni/fabric/outofsvc/rsoo" "sPath-[topology/pod-1/paths-1/pathep-[eth1/1]]', 'st" "atus': 'deleted', 'tDn': 'topology/pod-1/paths-1/pat" "hep-[eth1/1]'}, 'children': []}}]}}") self.assertEqual(str(fabric_json), expected_json)
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 add_consuming_static_endpoint(self, mac, ip, site1=False, epg_name='web-frontend'): if site1: session = self._login_session(SITE1_URL, SITE1_LOGIN, SITE1_PASSWORD) else: session = self._login_session(SITE2_URL, SITE2_LOGIN, SITE2_PASSWORD) tenant = Tenant('multisite') app = AppProfile('my-demo-app', tenant) web_epg = EPG(epg_name, app) # Create the Endpoint ep = Endpoint(mac, web_epg) ep.mac = mac ep.ip = ip intf = Interface('eth', '1', '101', '1', '38') # Create a VLAN interface and attach to the physical interface vlan_intf = L2Interface('vlan-5', 'vlan', '5') vlan_intf.attach(intf) # Attach the EPG to the VLAN interface web_epg.attach(vlan_intf) # Assign Endpoint to the L2Interface ep.attach(vlan_intf) resp = tenant.push_to_apic(session) self.assertTrue(resp.ok) tenants = Tenant.get_deep(session, names=['multisite']) multisite_tenant = tenants[0] app = multisite_tenant.get_child(AppProfile, 'my-demo-app') self.assertIsNotNone(app) epg = app.get_child(EPG, epg_name) self.assertIsNotNone(epg) ep = epg.get_child(Endpoint, mac) self.assertIsNotNone(ep) if site1: session = self._login_session(SITE2_URL, SITE2_LOGIN, SITE2_PASSWORD) else: session = self._login_session(SITE1_URL, SITE1_LOGIN, SITE1_PASSWORD) self._assert_l3extsubnet_exists(session, tenant_name='multisite', mac=mac, ip=ip)
def add_providing_static_endpoint(self, mac, ip): session = self._login_session(SITE1_URL, SITE1_LOGIN, SITE1_PASSWORD) tenant = Tenant('multisite') app = AppProfile('my-demo-app', tenant) web_epg = EPG('database-backend', app) # Create the Endpoint ep = Endpoint(mac, web_epg) ep.mac = mac ep.ip = ip intf = Interface('eth', '1', '101', '1', '38') # Create a VLAN interface and attach to the physical interface vlan_intf = L2Interface('vlan-5', 'vlan', '5') vlan_intf.attach(intf) # Attach the EPG to the VLAN interface web_epg.attach(vlan_intf) # Assign Endpoint to the L2Interface ep.attach(vlan_intf) resp = tenant.push_to_apic(session) if not resp.ok: self.assertTrue(resp.ok) print resp, resp.text time.sleep(1) # Verify that the Endpoint was pushed successfully tenants = Tenant.get_deep(session, names=['multisite']) multisite_tenant = tenants[0] app = multisite_tenant.get_child(AppProfile, 'my-demo-app') self.assertIsNotNone(app) epg = app.get_child(EPG, 'database-backend') self.assertIsNotNone(epg) ep = epg.get_child(Endpoint, mac) self.assertIsNotNone(ep) # Verify that the entry was pushed to the other site session = self._login_session(SITE2_URL, SITE2_LOGIN, SITE2_PASSWORD) self._assert_l3extsubnet_exists(session, tenant_name='multisite', mac=mac, ip=ip)
def test_set_speed_1G(self): intf = Interface('eth', '1', '1', '1', '1') intf.speed = '1G' (phys_domain_json, fabric_json, infra_json) = intf.get_json() expected_json = ( "{'infraInfra': {'attributes': {}, 'children': [{'infraNodeP': {'attrib" "utes': {'name': '1-1-1-1'}, 'children': [{'infraLeaf" "S': {'attributes': {'type': 'range', 'name': '1-1-1-" "1'}, 'children': [{'infraNodeBlk': {'attributes': {'" "from_': '1', 'name': '1-1-1-1', 'to_': '1'}, 'childr" "en': []}}]}}, {'infraRsAccPortP': {'attributes': {'t" "Dn': 'uni/infra/accportprof-1-1-1-1'}, 'children': [" "]}}]}}, {'infraAccPortP': {'attributes': {'name': '1" "-1-1-1'}, 'children': [{'infraHPortS': {'attributes'" ": {'type': 'range', 'name': '1-1-1-1'}, 'children': " "[{'infraPortBlk': {'attributes': {'toPort': '1', 'fr" "omPort': '1', 'fromCard': '1', 'name': '1-1-1-1', 't" "oCard': '1'}, 'children': []}}, {'infraRsAccBaseGrp'" ": {'attributes': {'tDn': 'uni/infra/funcprof/accport" "grp-1-1-1-1'}, 'children': []}}]}}]}}, {'fabricHIfPo" "l': {'attributes': {'dn': 'uni/infra/hintfpol-speed1" "G', 'autoNeg': 'on', 'speed': '1G', 'name': 'speed1G" "'}, 'children': []}}, {'infraFuncP': {'attributes': " "{}, 'children': [{'infraAccPortGrp': {'attributes': " "{'dn': 'uni/infra/funcprof/accportgrp-1-1-1-1', 'nam" "e': '1-1-1-1'}, 'children': [{'infraRsHIfPol': {'att" "ributes': {'tnFabricHIfPolName': 'speed1G'}, 'childr" "en': []}}, {'infraRsAttEntP': {'attributes': {'tDn':" " 'uni/infra/attentp-allvlans'}, 'children': []}}]}}]" "}}, {'infraAttEntityP': {'attributes': {'name': 'all" "vlans'}, 'children': [{'infraRsDomP': {'attributes':" " {'tDn': 'uni/phys-allvlans'}}}]}}, {'fvnsVlanInstP'" ": {'attributes': {'name': 'allvlans', 'allocMode': '" "static'}, 'children': [{'fvnsEncapBlk': {'attributes" "': {'to': 'vlan-4092', 'from': 'vlan-1', 'name': 'en" "cap'}}}]}}]}}") self.assertEqual(str(infra_json), expected_json)
def remove_providing_static_endpoint(self, mac, ip): session = self._login_session(SITE1_URL, SITE1_LOGIN, SITE1_PASSWORD) tenant = Tenant('multisite') app = AppProfile('my-demo-app', tenant) web_epg = EPG('database-backend', app) # Create the Endpoint ep = Endpoint(mac, web_epg) ep.mac = mac ep.ip = ip intf = Interface('eth', '1', '101', '1', '38') # Create a VLAN interface and attach to the physical interface vlan_intf = L2Interface('vlan-5', 'vlan', '5') vlan_intf.attach(intf) # Attach the EPG to the VLAN interface web_epg.attach(vlan_intf) # Assign Endpoint to the L2Interface ep.attach(vlan_intf) # Mark the Endpoint as deleted ep.mark_as_deleted() resp = tenant.push_to_apic(session) self.assertTrue(resp.ok) # Verify that the Endpoint has been removed time.sleep(1) tenants = Tenant.get_deep(session, names=['multisite']) multisite_tenant = tenants[0] app = multisite_tenant.get_child(AppProfile, 'my-demo-app') self.assertIsNotNone(app) epg = app.get_child(EPG, 'web-frontend') self.assertIsNotNone(epg) ep = epg.get_child(Endpoint, mac) self.assertIsNone(ep)
def test_set_speed_10G(self): intf = Interface('eth', '1', '101', '1', '5') intf.speed = '10G' (phys_domain_json, fabric_json, infra_json) = intf.get_json() expected_json = ( "{'infraInfra': {'attributes': {}, 'children': [{'infraNodeP': {'attrib" "utes': {'name': '1-101-1-5'}, 'children': [{'infraLe" "afS': {'attributes': {'type': 'range', 'name': '1-10" "1-1-5'}, 'children': [{'infraNodeBlk': {'attributes'" ": {'from_': '101', 'name': '1-101-1-5', 'to_': '101'" "}, 'children': []}}]}}, {'infraRsAccPortP': {'attrib" "utes': {'tDn': 'uni/infra/accportprof-1-101-1-5'}, '" "children': []}}]}}, {'infraAccPortP': {'attributes':" " {'name': '1-101-1-5'}, 'children': [{'infraHPortS':" " {'attributes': {'type': 'range', 'name': '1-101-1-5" "'}, 'children': [{'infraPortBlk': {'attributes': {'t" "oPort': '5', 'fromPort': '5', 'fromCard': '1', 'name" "': '1-101-1-5', 'toCard': '1'}, 'children': []}}, {'" "infraRsAccBaseGrp': {'attributes': {'tDn': 'uni/infr" "a/funcprof/accportgrp-1-101-1-5'}, 'children': []}}]" "}}]}}, {'fabricHIfPol': {'attributes': {'dn': 'uni/i" "nfra/hintfpol-speed10G', 'autoNeg': 'on', 'speed': '" "10G', 'name': 'speed10G'}, 'children': []}}, {'infra" "FuncP': {'attributes': {}, 'children': [{'infraAccPo" "rtGrp': {'attributes': {'dn': 'uni/infra/funcprof/ac" "cportgrp-1-101-1-5', 'name': '1-101-1-5'}, 'children" "': [{'infraRsHIfPol': {'attributes': {'tnFabricHIfPo" "lName': 'speed10G'}, 'children': []}}, {'infraRsAttE" "ntP': {'attributes': {'tDn': 'uni/infra/attentp-allv" "lans'}, 'children': []}}]}}]}}, {'infraAttEntityP': " "{'attributes': {'name': 'allvlans'}, 'children': [{'" "infraRsDomP': {'attributes': {'tDn': 'uni/phys-allvl" "ans'}}}]}}, {'fvnsVlanInstP': {'attributes': {'name'" ": 'allvlans', 'allocMode': 'static'}, 'children': [{" "'fvnsEncapBlk': {'attributes': {'to': 'vlan-4092', '" "from': 'vlan-1', 'name': 'encap'}}}]}}]}}") self.assertEqual(str(infra_json), expected_json)
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 setup_multisite_test(printonly=False, delete=False): # Create the Tenant tenant1 = Tenant('multisite') # Create the Application Profile app = AppProfile('my-demo-app', tenant1) # Create the EPGs web_epg = EPG('web-frontend', app) db_epg = EPG('database-backend', app) # Create a Context and BridgeDomain # Place both EPGs in the Context and in the same BD context = Context('VRF-1', tenant1) bd = BridgeDomain('BD-1', tenant1) bd.add_context(context) web_epg.add_bd(bd) db_epg.add_bd(bd) # Define a contract with a single entry contract = Contract('multisite_mysqlcontract', tenant1) entry1 = FilterEntry('entry1', applyToFrag='no', arpOpc='unspecified', dFromPort='3306', dToPort='3306', etherT='ip', prot='tcp', sFromPort='1', sToPort='65535', tcpRules='unspecified', parent=contract) # Provide the contract from 1 EPG and consume from the other db_epg.provide(contract) web_epg.consume(contract) context = Context('ctx0', tenant1) #contract = Contract('contract', tenant) phyif = Interface('eth', '1', '102', '1', '25') l2if = L2Interface('eth 1/102/1/25', 'vlan', '500') l2if.attach(phyif) l3if = L3Interface('l3if') l3if.set_l3if_type('ext-svi') l3if.set_addr('20.0.0.1/16') l3if.add_context(context) l3if.attach(l2if) #l3if.networks.append('1.1.1.1/32') #outside.provide(contract) l3if.attach(l2if) rtr = OSPFRouter('rtr-1') rtr.set_router_id('101.101.101.101') rtr.set_node_id('102') # net1 = OutsideNetwork('1.1.1.1/32') # net1.network = '1.1.1.1/32' # net1.provide(contract) ospfif = OSPFInterface('ospfif-1', router=rtr, area_id='0.0.0.1') ospfif.attach(l3if) # ospfif.networks.append(net1) outside = OutsideEPG('multisite-l3out', tenant1) outside.attach(ospfif) #outside.add_context(context) # Create the Tenant tenant2 = Tenant('multisite') # Create the Application Profile app = AppProfile('my-demo-app', tenant2) # Create the EPGs web_epg = EPG('web-frontend', app) # Create a Context and BridgeDomain # Place both EPGs in the Context and in the same BD context = Context('VRF-1', tenant2) bd = BridgeDomain('BD-1', tenant2) bd.add_context(context) web_epg.add_bd(bd) context = Context('ctx0', tenant2) #contract = Contract('contract', tenant) phyif = Interface('eth', '1', '102', '1', '25') l2if = L2Interface('eth 1/102/1/25', 'vlan', '500') l2if.attach(phyif) l3if = L3Interface('l3if') l3if.set_l3if_type('ext-svi') l3if.set_addr('20.0.0.2/16') l3if.add_context(context) l3if.attach(l2if) #outside.provide(contract) l3if.attach(l2if) rtr = OSPFRouter('rtr-1') rtr.set_router_id('102.102.102.102') rtr.set_node_id('102') ospfif = OSPFInterface('ospfif-1', router=rtr, area_id='0.0.0.1') ospfif.attach(l3if) #ospfif.networks.append('1.1.1.1/32') #ospfif.networks.append('1.1.1.2/32') outside = OutsideEPG('multisite-l3out', tenant2) outside.attach(ospfif) if not printonly: # Login to APIC and push the config session = Session(SITE1_URL, SITE1_LOGIN, SITE1_PASSWORD) session.login() # Cleanup (uncomment the next line to delete the config) if delete: print 'Deleting...' tenant1.mark_as_deleted() resp = tenant1.push_to_apic(session) if resp.ok: # Print what was sent print('Pushed the following JSON to the APIC', resp.text) else: print resp, resp.text print('URL: ' + str(tenant1.get_url())) print('JSON:') print json.dumps(tenant1.get_json(), indent=4, separators=(',',':')) if not printonly: # Login to APIC and push the config session = Session(SITE2_URL, SITE2_LOGIN, SITE2_PASSWORD) session.login() # Cleanup (uncomment the next line to delete the config) if delete: tenant2.mark_as_deleted() resp = tenant2.push_to_apic(session) if resp.ok: # Print what was sent print('Pushed the following JSON to the APIC', resp.text) else: print resp, resp.text print('URL: ' + str(tenant2.get_url())) print('JSON:') print json.dumps(tenant2.get_json(), indent=4, separators=(',',':'))
def test_lldp_disabled(self): intf1 = Interface('eth', '1', '2', '3', '4') self.assertFalse(intf1.is_lldp_disabled())
def test_get_type(self): intf1 = Interface('eth', '1', '2', '3', '4') self.assertEqual(intf1.get_type(), 'interface')
def test_get_serial(self): intf1 = Interface('eth', '1', '2', '3', '4') self.assertEqual(intf1.get_serial(), None)
def test_lldp_is_disabled(self): intf = Interface('eth', '1', '1', '1', '1') intf.disable_lldp() self.assertFalse(intf.is_lldp_enabled())
def test_lldp_is_enabled(self): intf = Interface('eth', '1', '1', '1', '1') intf.enable_lldp() self.assertTrue(intf.is_lldp_enabled())
def test_lldp_not_enabled(self): intf = Interface('eth', '1', '1', '1', '1') self.assertFalse(intf.is_lldp_enabled())
def test_adminstate_not_set(self): intf = Interface('eth', '1', '1', '1', '1') intf.adminstate = '' # phys_domain_url, fabric_url, infra_url = intf.get_url() phys_domain_json, fabric_json, infra_json = intf.get_json() self.assertIsNone(fabric_json)