def delete_networks(): """Tear down things set up by create_networks again, we do various checks along the way. """ # Query the DB for nodes on this project project = api._must_find(model.Project, 'anvil-nextgen') nodes = project.nodes ports = self.get_all_ports(nodes) # Remove all nodes from their networks. We do this in two different # ways for different ports to test the different mechanisms. For # the first two nodes we explicity remove the attachments. For the # latter two we call port_revert. for node in nodes[:2]: attachment = model.NetworkAttachment.query \ .filter_by(nic=node.nics[0]).one() api.node_detach_network(node.label, node.nics[0].label, attachment.network.label) for node in nodes[2:]: port = node.nics[0].port api.port_revert(port.owner.label, port.label) deferred.apply_networking() # Assert that none of the nodes are on any network port_networks = self.get_port_networks(ports) for node in nodes: assert self.get_network(node.nics[0].port, port_networks) == \ set() # Delete the networks api.network_delete('net-0') api.network_delete('net-1')
def delete_networks(): """Tear down things set up by create_networks again, we do various checks along the way. """ # Query the DB for nodes on this project project = api.get_or_404(model.Project, 'anvil-nextgen') nodes = project.nodes ports = self.get_all_ports(nodes) # Remove all nodes from their networks. We do this in two different # ways for different ports to test the different mechanisms. For # the first two nodes we explicity remove the attachments. For the # latter two we call port_revert. for node in nodes[:2]: attachment = model.NetworkAttachment.query \ .filter_by(nic=node.nics[0]).one() api.node_detach_network(node.label, node.nics[0].label, attachment.network.label) for node in nodes[2:]: port = node.nics[0].port api.port_revert(port.owner.label, port.label) deferred.apply_networking() # Assert that none of the nodes are on any network port_networks = self.get_port_networks(ports) for node in nodes: assert self.get_network(node.nics[0].port, port_networks) == \ set() # Delete the networks api.network_delete('net-0') api.network_delete('net-1')
def teardown(): """Teardown the setup from create_multi_nets. """ # Query the DB for nodes on this project project = api.get_or_404(model.Project, 'anvil-nextgen') nodes = project.nodes ports = self.get_all_ports(nodes) # Remove all nodes from their networks using port_revert. for node in nodes: port = node.nics[0].port api.port_revert(port.owner.label, port.label) deferred.apply_networking() # Assert that none of the nodes are on any network port_networks = self.get_port_networks(ports) for node in nodes: assert self.get_network(node.nics[0].port, port_networks) == \ set() # Delete the networks api.network_delete('net-0') api.network_delete('net-1') api.network_delete('net-2') api.network_delete('net-3')
def delete_networks(): """Tear down things set up by create_networks again, we do various checks along the way. """ # Query the DB for nodes on this project project = api._must_find(model.Project, 'anvil-nextgen') nodes = project.nodes ports = self.get_all_ports(nodes) # Remove all nodes from their networks. We do this in two ways, to # test the different mechanisms. # For the first two nodes, we first build up a list of # the arguments to the API calls, which has no direct references to # database objects, and then make the API calls and invoke # deferred.apply_networking after. This is important -- # The API calls and apply_networking normally run in their own # transaction. We get away with not doing this in the tests because # we serialize everything ourselves, so there's no risk of # interference. If we were to hang on to references to database # objects across such calls however, things could get harry. all_attachments = [] for node in nodes[:2]: attachments = model.NetworkAttachment.query \ .filter_by(nic=node.nics[0]).all() for attachment in attachments: all_attachments.append((node.label, node.nics[0].label, attachment.network.label)) for attachment in all_attachments: api.node_detach_network(*attachment) deferred.apply_networking() # For the second two nodes, we just call port_revert on the nic's # port. for node in nodes[2:]: port = node.nics[0].port api.port_revert(port.owner.label, port.label) deferred.apply_networking() # Assert that none of the nodes are on any network port_networks = self.get_port_networks(ports) for node in nodes: assert self.get_network(node.nics[0].port, port_networks) == \ set() # Delete the networks api.network_delete('net-0') api.network_delete('net-1')
def test_create_network_with_id_from_pool(self): """Test creation of networks with IDs from the pool.""" api.project_create('nuggets') # create a project owned network and get its network_id api.network_create('hammernet', 'nuggets', 'nuggets', '') network = api._must_find(model.Network, 'hammernet') net_id = int(network.network_id) assert network.allocated is True # create an admin owned network with net_id from pool api.network_create('nailnet', 'admin', '', 103) network = api._must_find(model.Network, 'nailnet') assert network.allocated is True # creating a network with the same network id should raise an error with pytest.raises(errors.BlockedError): api.network_create('redbone', 'admin', '', 103) with pytest.raises(errors.BlockedError): api.network_create('starfish', 'admin', '', net_id) # free the network ids by deleting the networks api.network_delete('hammernet') api.network_delete('nailnet') api._assert_absent(model.Network, 'hammernet') api._assert_absent(model.Network, 'nailnet') # after deletion we should be able to create admin networks with those # network_ids api.network_create('redbone', 'admin', '', 103) network = api._must_find(model.Network, 'redbone') assert int(network.network_id) == 103 api.network_create('starfish', 'admin', '', net_id) network = api._must_find(model.Network, 'starfish') assert int(network.network_id) == net_id
def test_create_network_with_id_from_pool(self): """Test creation of networks with IDs from the pool.""" api.project_create('nuggets') # create a project owned network and get its network_id api.network_create('hammernet', 'nuggets', 'nuggets', '') network = api.get_or_404(model.Network, 'hammernet') net_id = int(network.network_id) assert network.allocated is True # create an admin owned network with net_id from pool api.network_create('nailnet', 'admin', '', 103) network = api.get_or_404(model.Network, 'nailnet') assert network.allocated is True # creating a network with the same network id should raise an error with pytest.raises(errors.BlockedError): api.network_create('redbone', 'admin', '', 103) with pytest.raises(errors.BlockedError): api.network_create('starfish', 'admin', '', net_id) # free the network ids by deleting the networks api.network_delete('hammernet') api.network_delete('nailnet') api.absent_or_conflict(model.Network, 'hammernet') api.absent_or_conflict(model.Network, 'nailnet') # after deletion we should be able to create admin networks with those # network_ids api.network_create('redbone', 'admin', '', 103) network = api.get_or_404(model.Network, 'redbone') assert int(network.network_id) == 103 api.network_create('starfish', 'admin', '', net_id) network = api.get_or_404(model.Network, 'starfish') assert int(network.network_id) == net_id
def delete_networks(): """Tear down things set up by create_networks again, we do various checks along the way. """ # Query the DB for nodes on this project project = api.get_or_404(model.Project, 'anvil-nextgen') nodes = project.nodes ports = self.get_all_ports(nodes) # Remove all nodes from their networks. We do this in two ways, to # test the different mechanisms. # For the first two nodes, we first build up a list of # the arguments to the API calls, which has no direct references to # database objects, and then make the API calls and invoke # deferred.apply_networking after. This is important -- # The API calls and apply_networking normally run in their own # transaction. We get away with not doing this in the tests because # we serialize everything ourselves, so there's no risk of # interference. If we were to hang on to references to database # objects across such calls however, things could get harry. all_attachments = [] net = namedtuple('net', 'node nic network channel') for node in nodes[:2]: attachments = model.NetworkAttachment.query \ .filter_by(nic=node.nics[0]).all() for attachment in attachments: all_attachments.append( net(node=node.label, nic=node.nics[0].label, network=attachment.network.label, channel=attachment.channel)) switch = nodes[0].nics[0].port.owner # in some switches, the native network can only be disconnected # after we remove all tagged networks first. The following checks # for that and rearranges the networks (all_attachments) such that # tagged networks are removed first. if 'nativeless-trunk-mode' not in switch.get_capabilities(): # sort by channel; vlan/<integer> comes before vlan/native # because the ASCII for numbers comes before ASCII for letters. all_attachments = sorted(all_attachments, key=lambda net: net.channel) for attachment in all_attachments: api.node_detach_network(attachment.node, attachment.nic, attachment.network) deferred.apply_networking() # For the second two nodes, we just call port_revert on the nic's # port. for node in nodes[2:]: port = node.nics[0].port api.port_revert(port.owner.label, port.label) deferred.apply_networking() # Assert that none of the nodes are on any network port_networks = self.get_port_networks(ports) for node in nodes: assert self.get_network(node.nics[0].port, port_networks) == \ set() # Delete the networks api.network_delete('net-0') api.network_delete('net-1') api.network_delete('net-2') api.network_delete('net-3')