Exemplo n.º 1
0
        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')
Exemplo n.º 2
0
 def test_no_nic(self):
     """port_revert on a port with no nic should raise not found."""
     with pytest.raises(errors.NotFoundError):
         # free_port_0 is not attached to a nic.
         api.port_revert('stock_switch_0', 'free_port_0')
     deferred.apply_networking()
     assert self.LOCAL_STATE['stock_switch_0']['free_port_0'] == {}
Exemplo n.º 3
0
    def test_one_network(self):
        """Test port_revert on a port attached to one network."""
        api.node_connect_network('runway_node_0',
                                 'nic-with-port',
                                 'runway_pxe',
                                 'vlan/native')
        deferred.apply_networking()

        net_id = model.Network.query.filter_by(label='runway_pxe')\
            .one().network_id
        assert self.LOCAL_STATE['stock_switch_0']['runway_node_0_port'] == {
            'vlan/native': net_id,
        }

        api.port_revert('stock_switch_0', 'runway_node_0_port')
        deferred.apply_networking()

        assert self.LOCAL_STATE['stock_switch_0']['runway_node_0_port'] == {},\
            "port_revert did not detach the port from the networks!"

        network = model.Network.query.filter_by(label='runway_pxe').one()
        assert model.NetworkAttachment.query.filter_by(
            network_id=network.id,
        ).first() is None, (
            "port_revert did not remove the network attachment object in "
            "the database!"
        )
Exemplo n.º 4
0
 def test_node_detach_network(self):
     """(successful) call to node_detach_network"""
     C.node.connect_network('node-04', 'eth0', 'net-04', 'vlan/native')
     deferred.apply_networking()
     response = C.node.detach_network('node-04', 'eth0', 'net-04')
     assert uuid_pattern.match(response['status_id'])
     deferred.apply_networking()
Exemplo n.º 5
0
 def test_node_detach_network_error(self):
     C.node.connect_network('node-04', 'eth0', 'net-04', 'vlan/native')
     deferred.apply_networking()
     C.node.detach_network('node-04', 'eth0', 'net-04')
     deferred.apply_networking()
     with pytest.raises(FailedAPICallException):
         C.node.detach_network('node-04', 'eth0', 'net-04')
Exemplo n.º 6
0
 def test_no_nic(self):
     """port_revert on a port with no nic should raise not found."""
     with pytest.raises(errors.NotFoundError):
         # free_port_0 is not attached to a nic.
         api.port_revert('stock_switch_0', 'free_port_0')
     deferred.apply_networking()
     assert self.LOCAL_STATE['stock_switch_0']['free_port_0'] == {}
Exemplo n.º 7
0
        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')
Exemplo n.º 8
0
        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')
Exemplo n.º 9
0
        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')
Exemplo n.º 10
0
 def test_node_connect_network_error(self):
     """Duplicate call to node_connect_network should fail."""
     C.node.connect_network('node-02', 'eth0', 'net-04', 'vlan/native')
     deferred.apply_networking()
     with pytest.raises(FailedAPICallException):
         C.node.connect_network('node-02', 'eth0', 'net-04', 'vlan/native')
     deferred.apply_networking()
Exemplo n.º 11
0
 def test_node_detach_network(self):
     """(successful) call to node_detach_network"""
     C.node.connect_network('node-04', 'eth0', 'net-04', 'vlan/native')
     deferred.apply_networking()
     response = C.node.detach_network('node-04', 'eth0', 'net-04')
     assert uuid_pattern.match(response['status_id'])
     deferred.apply_networking()
Exemplo n.º 12
0
    def test_saving_config_file(self):
        """Test saving the switch config to flash."""
        api.project_create('anvil-nextgen')
        nodes = self.collect_nodes()

        # Create two networks
        network_create_simple('net-0', 'anvil-nextgen')
        network_create_simple('net-1', 'anvil-nextgen')

        # save the old startup config before performing a networking action
        old_startup_config = self.get_config('startup')
        # Connect n0 and n1 to net-0 and net-1 respectively
        api.node_connect_network(nodes[0].label, nodes[0].nics[0].label,
                                 'net-0')

        api.node_connect_network(nodes[1].label, nodes[1].nics[0].label,
                                 'net-1')

        deferred.apply_networking()

        # get the running config, and the new startup config
        running_config = self.get_config('running')
        new_startup_config = self.get_config('startup')

        assert new_startup_config == running_config
        assert new_startup_config != old_startup_config

        # cleanup
        api.node_detach_network(nodes[0].label, nodes[0].nics[0].label,
                                'net-0')

        api.node_detach_network(nodes[1].label, nodes[1].nics[0].label,
                                'net-1')

        deferred.apply_networking()
Exemplo n.º 13
0
 def test_node_connect_network_error(self):
     """Duplicate call to node_connect_network should fail."""
     C.node.connect_network('node-02', 'eth0', 'net-04', 'vlan/native')
     deferred.apply_networking()
     with pytest.raises(FailedAPICallException):
         C.node.connect_network('node-02', 'eth0', 'net-04', 'vlan/native')
     deferred.apply_networking()
Exemplo n.º 14
0
    def test_node_connect_network(self):
        """(successful) call to node_connect_network"""
        response = C.node.connect_network('node-01', 'eth0', 'net-01',
                                          'vlan/native')

        # check that the reponse contains a valid UUID.
        assert uuid_pattern.match(response['status_id'])
        deferred.apply_networking()
Exemplo n.º 15
0
def test_apply_networking(switch, network, fresh_database):
    '''Test to validate apply_networking commits actions incrementally

    This test verifies that the apply_networking() function in hil/deferred.py
    incrementally commits actions, which ensures that any error on an action
    will not require a complete rerun of the prior actions (e.g. if an error
    is thrown on the 3rd action, the 1st and 2nd action will have already been
    committed)

    The test also verifies that if a new networking action fails, then the
    old networking actions in the queue were commited.
    '''
    nic = []
    actions = []
    # initialize 3 nics and networking actions
    for i in range(0, 3):
        interface = 'gi1/0/%d' % (i)
        nic.append(new_nic(str(i)))
        nic[i].port = model.Port(label=interface, switch=switch)
        actions.append(
            model.NetworkingAction(nic=nic[i],
                                   new_network=network,
                                   channel='vlan/native',
                                   type='modify_port'))

    # this makes the last action invalid for the test switch because the switch
    # raises an error when the networking action is of type revert port.
    actions[2] = model.NetworkingAction(nic=nic[2],
                                        new_network=None,
                                        channel='',
                                        type='revert_port')
    for action in actions:
        db.session.add(action)
    db.session.commit()

    with pytest.raises(RevertPortError):
        deferred.apply_networking()

    # close the session opened by `apply_networking` when `handle_actions`
    # fails; without this the tests would just stall (when using postgres)
    db.session.close()

    local_db = new_db()

    pending_action = local_db.session \
        .query(model.NetworkingAction) \
        .order_by(model.NetworkingAction.id).one_or_none()
    current_count = local_db.session \
        .query(model.NetworkingAction).count()

    local_db.session.delete(pending_action)
    local_db.session.commit()
    local_db.session.close()

    # test that there's only pending action in the queue and it is of type
    # revert_port
    assert current_count == 1
    assert pending_action.type == 'revert_port'
Exemplo n.º 16
0
Arquivo: auth.py Projeto: shwsun/haas
 def setUp(self):
     self.auth_backend = get_auth_backend()
     self.runway = model.Project.query.filter_by(label='runway').one()
     self.manhattan = model.Project.query.filter_by(label='manhattan').one()
     self.auth_backend.set_project(self.manhattan)
     api.node_connect_network('manhattan_node_0',
                              'boot-nic',
                              'stock_int_pub')
     deferred.apply_networking()
Exemplo n.º 17
0
Arquivo: auth.py Projeto: djfinn14/hil
 def setUp(self):
     self.auth_backend = get_auth_backend()
     self.runway = model.Project.query.filter_by(label='runway').one()
     self.manhattan = model.Project.query.filter_by(label='manhattan').one()
     self.auth_backend.set_project(self.manhattan)
     api.node_connect_network('manhattan_node_0',
                              'boot-nic',
                              'stock_int_pub')
     deferred.apply_networking()
Exemplo n.º 18
0
 def test_node_detach_network(self):
     """(successful) call to node_detach_network"""
     C.node.connect_network('manhattan_node_0', 'nic-with-port',
                            'manhattan_pxe', 'vlan/native')
     deferred.apply_networking()
     response = C.node.detach_network('manhattan_node_0', 'nic-with-port',
                                      'manhattan_pxe')
     assert uuid_pattern.match(response['status_id'])
     deferred.apply_networking()
Exemplo n.º 19
0
 def test_node_connect_network_error(self):
     """Duplicate call to node_connect_network should fail."""
     C.node.connect_network('manhattan_node_1', 'nic-with-port',
                            'manhattan_pxe', 'vlan/native')
     deferred.apply_networking()
     with pytest.raises(FailedAPICallException):
         C.node.connect_network('manhattan_node_1', 'nic-with-port',
                                'manhattan_pxe', 'vlan/native')
     deferred.apply_networking()
Exemplo n.º 20
0
    def test_node_connect_network(self):
        """(successful) call to node_connect_network"""
        response = C.node.connect_network(
                'manhattan_node_1', 'nic-with-port', 'manhattan_pxe',
                'vlan/native')

        # check that the reponse contains a valid UUID.
        assert uuid_pattern.match(response['status_id'])
        deferred.apply_networking()
Exemplo n.º 21
0
 def test_node_detach_network(self):
     """(successful) call to node_detach_network"""
     C.node.connect_network(
         'manhattan_node_0', 'nic-with-port', 'manhattan_pxe',
         'vlan/native')
     deferred.apply_networking()
     response = C.node.detach_network(
         'manhattan_node_0', 'nic-with-port', 'manhattan_pxe')
     assert uuid_pattern.match(response['status_id'])
     deferred.apply_networking()
Exemplo n.º 22
0
 def test_list_network_attachments(self):
     """ Test list of network attachments """
     assert C.network.list_network_attachments("net-01", "all") == {}
     assert C.network.list_network_attachments("net-02", "proj-01") == {}
     C.node.connect_network('node-01', 'eth0', 'net-03', 'vlan/native')
     deferred.apply_networking()
     assert C.network.list_network_attachments("net-03", "all") == {
             'node-01': {'project': 'proj-01',
                         'nic': 'eth0',
                         'channel': 'vlan/native'}
             }
Exemplo n.º 23
0
 def test_list_network_attachments(self):
     """ Test list of network attachments """
     assert C.network.list_network_attachments("net-01", "all") == {}
     assert C.network.list_network_attachments("net-02", "proj-01") == {}
     C.node.connect_network('node-01', 'eth0', 'net-03', 'vlan/native')
     deferred.apply_networking()
     assert C.network.list_network_attachments("net-03", "all") == {
             'node-01': {'project': 'proj-01',
                         'nic': 'eth0',
                         'channel': 'vlan/native'}
             }
Exemplo n.º 24
0
 def test_node_connect_network_error(self):
     """Duplicate call to node_connect_network should fail."""
     C.node.connect_network(
         'manhattan_node_1', 'nic-with-port', 'manhattan_pxe',
         'vlan/native')
     deferred.apply_networking()
     with pytest.raises(FailedAPICallException):
         C.node.connect_network(
             'manhattan_node_1', 'nic-with-port', 'manhattan_pxe',
             'vlan/native')
     deferred.apply_networking()
Exemplo n.º 25
0
        def create_networks():
            """Create networks and connect things to them.

            Test various things along the way.
            """
            nodes = self.collect_nodes()

            # Create two networks
            network_create_simple('net-0', 'anvil-nextgen')
            network_create_simple('net-1', 'anvil-nextgen')

            ports = self.get_all_ports(nodes)

            # Assert that n0 and n1 are not on any network
            port_networks = self.get_port_networks(ports)

            assert self.get_network(nodes[0].nics[0].port, port_networks) == \
                set()
            assert self.get_network(nodes[1].nics[0].port, port_networks) == \
                set()

            # Connect n0 and n1 to net-0 and net-1 respectively
            api.node_connect_network(nodes[0].label,
                                     nodes[0].nics[0].label,
                                     'net-0')
            api.node_connect_network(nodes[1].label,
                                     nodes[1].nics[0].label,
                                     'net-1')
            deferred.apply_networking()

            # Assert that n0 and n1 are on isolated networks
            port_networks = self.get_port_networks(ports)
            assert self.get_network(nodes[0].nics[0].port, port_networks) == \
                set([nodes[0].nics[0].port])
            assert self.get_network(nodes[1].nics[0].port, port_networks) == \
                set([nodes[1].nics[0].port])

            # Add n2 and n3 to the same networks as n0 and n1 respectively
            api.node_connect_network(nodes[2].label,
                                     nodes[2].nics[0].label,
                                     'net-0')
            api.node_connect_network(nodes[3].label,
                                     nodes[3].nics[0].label,
                                     'net-1')
            deferred.apply_networking()

            # Assert that n2 and n3 have been added to n0 and n1's networks
            # respectively
            port_networks = self.get_port_networks(ports)
            assert self.get_network(nodes[0].nics[0].port, port_networks) == \
                set([nodes[0].nics[0].port, nodes[2].nics[0].port])
            assert self.get_network(nodes[1].nics[0].port, port_networks) == \
                set([nodes[1].nics[0].port, nodes[3].nics[0].port])
Exemplo n.º 26
0
 def test_port_revert(self):
     """Revert port should run without error and remove all networks"""
     C.node.connect_network('node-01', 'eth0', 'net-01', 'vlan/native')
     deferred.apply_networking()
     assert C.port.show('mock-01', 'gi1/0/1') == {
             'node': 'node-01',
             'nic': 'eth0',
             'networks': {'vlan/native': 'net-01'}}
     assert C.port.port_revert('mock-01', 'gi1/0/1') is None
     deferred.apply_networking()
     assert C.port.show('mock-01', 'gi1/0/1') == {
             'node': 'node-01',
             'nic': 'eth0',
             'networks': {}}
Exemplo n.º 27
0
        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')
Exemplo n.º 28
0
        def create_networks():
            """Create networks and connect things to them.

            Test various things along the way.
            """
            nodes = self.collect_nodes()

            # Create two networks
            network_create_simple('net-0', 'anvil-nextgen')
            network_create_simple('net-1', 'anvil-nextgen')

            ports = self.get_all_ports(nodes)

            # Assert that n0 and n1 are not on any network
            port_networks = self.get_port_networks(ports)

            assert self.get_network(nodes[0].nics[0].port, port_networks) == \
                set()
            assert self.get_network(nodes[1].nics[0].port, port_networks) == \
                set()

            # Connect n0 and n1 to net-0 and net-1 respectively
            api.node_connect_network(nodes[0].label, nodes[0].nics[0].label,
                                     'net-0')
            api.node_connect_network(nodes[1].label, nodes[1].nics[0].label,
                                     'net-1')
            deferred.apply_networking()

            # Assert that n0 and n1 are on isolated networks
            port_networks = self.get_port_networks(ports)
            assert self.get_network(nodes[0].nics[0].port, port_networks) == \
                set([nodes[0].nics[0].port])
            assert self.get_network(nodes[1].nics[0].port, port_networks) == \
                set([nodes[1].nics[0].port])

            # Add n2 and n3 to the same networks as n0 and n1 respectively
            api.node_connect_network(nodes[2].label, nodes[2].nics[0].label,
                                     'net-0')
            api.node_connect_network(nodes[3].label, nodes[3].nics[0].label,
                                     'net-1')
            deferred.apply_networking()

            # Assert that n2 and n3 have been added to n0 and n1's networks
            # respectively
            port_networks = self.get_port_networks(ports)
            assert self.get_network(nodes[0].nics[0].port, port_networks) == \
                set([nodes[0].nics[0].port, nodes[2].nics[0].port])
            assert self.get_network(nodes[1].nics[0].port, port_networks) == \
                set([nodes[1].nics[0].port, nodes[3].nics[0].port])
Exemplo n.º 29
0
        def create_multi_nets():
            """Create multiple networks and connect them all to one port.

            Test that each network can be successfully added and discovered
            on the port.
            """

            nodes = self.collect_nodes()

            # create 5 networks
            for i in range(5):
                network_create_simple('net-%d' % i, 'anvil-nextgen')

            ports = self.get_all_ports(nodes)

            # assert that node 0 is not on any network
            port_networks = self.get_port_networks(ports)
            assert self.get_network(nodes[0].nics[0].port, port_networks) == \
                set()

            # get channel IDs for tagged versions of networks
            net_tag = {}
            for i in range(4):
                net_tag[i] = get_legal_channels('net-%d' % i)[1]

            # connect node 0 to net-0 in native mode
            api.node_connect_network(nodes[0].label, nodes[0].nics[0].label,
                                     'net-0')
            deferred.apply_networking()
            # connect node 0 to net-1 in tagged mode
            api.node_connect_network(nodes[0].label,
                                     nodes[0].nics[0].label,
                                     'net-1',
                                     channel=net_tag[1])
            deferred.apply_networking()
            # connect node 0 to net-2 in tagged mode
            api.node_connect_network(nodes[0].label,
                                     nodes[0].nics[0].label,
                                     'net-2',
                                     channel=net_tag[2])
            deferred.apply_networking()
            # connect node 0 to net-3 in tagged mode
            api.node_connect_network(nodes[0].label,
                                     nodes[0].nics[0].label,
                                     'net-3',
                                     channel=net_tag[3])
            deferred.apply_networking()

            # assert that all networks show up on the port
            port_networks = self.get_port_networks(ports)
            networks = \
                set([net for net,
                    _channel in port_networks.get(nodes[0].nics[0].port)])
            # create a list of networks with native net-0 included
            networks_added = set([
                get_legal_channels('net-0')[0], net_tag[1], net_tag[2],
                net_tag[3]
            ])
            assert networks == networks_added
Exemplo n.º 30
0
        def create_multi_nets():
            """Create multiple networks and connect them all to one port.

            Test that each network can be successfully added and discovered
            on the port.
            """

            nodes = self.collect_nodes()

            # create 5 networks
            for i in range(5):
                network_create_simple('net-%d' % i, 'anvil-nextgen')

            ports = self.get_all_ports(nodes)

            # assert that node 0 is not on any network
            port_networks = self.get_port_networks(ports)
            assert self.get_network(nodes[0].nics[0].port, port_networks) == \
                set()

            # get channel IDs for tagged versions of networks
            net_tag = {}
            for i in range(4):
                net_tag[i] = get_legal_channels('net-%d' % i)[1]

            # connect node 0 to net-0 in native mode
            api.node_connect_network(nodes[0].label,
                                     nodes[0].nics[0].label,
                                     'net-0')
            deferred.apply_networking()
            # connect node 0 to net-1 in tagged mode
            api.node_connect_network(nodes[0].label,
                                     nodes[0].nics[0].label,
                                     'net-1',
                                     channel=net_tag[1])
            deferred.apply_networking()
            # connect node 0 to net-2 in tagged mode
            api.node_connect_network(nodes[0].label,
                                     nodes[0].nics[0].label,
                                     'net-2',
                                     channel=net_tag[2])
            deferred.apply_networking()
            # connect node 0 to net-3 in tagged mode
            api.node_connect_network(nodes[0].label,
                                     nodes[0].nics[0].label,
                                     'net-3',
                                     channel=net_tag[3])
            deferred.apply_networking()

            # assert that all networks show up on the port
            port_networks = self.get_port_networks(ports)
            networks = \
                set([net for net,
                    _channel in port_networks.get(nodes[0].nics[0].port)])
            # create a list of networks with native net-0 included
            networks_added = set([get_legal_channels('net-0')[0],
                                 net_tag[1],
                                 net_tag[2], net_tag[3]])
            assert networks == networks_added
Exemplo n.º 31
0
 def test_list_network_attachments(self):
     """ Test list of network attachments """
     assert C.network.list_network_attachments(
         "manhattan_provider", "all") == {}
     assert C.network.list_network_attachments(
         "runway_provider", "runway") == {}
     C.node.connect_network('manhattan_node_0', 'nic-with-port',
                            'manhattan_provider', 'vlan/native')
     deferred.apply_networking()
     assert C.network.list_network_attachments(
         "manhattan_provider", "all") == {
             'manhattan_node_0': {'project': 'manhattan',
                                  'nic': 'nic-with-port',
                                  'channel': 'vlan/native'}
             }
Exemplo n.º 32
0
 def test_port_revert(self):
     """Revert port should run without error and remove all networks"""
     C.node.connect_network('node-01', 'eth0', 'net-01', 'vlan/native')
     deferred.apply_networking()
     assert C.port.show('mock-01', 'gi1/0/1') == {
             'node': 'node-01',
             'nic': 'eth0',
             'networks': {'vlan/native': 'net-01'}}
     response = C.port.port_revert('mock-01', 'gi1/0/1')
     assert uuid_pattern.match(response['status_id'])
     deferred.apply_networking()
     assert C.port.show('mock-01', 'gi1/0/1') == {
             'node': 'node-01',
             'nic': 'eth0',
             'networks': {}}
Exemplo n.º 33
0
 def test_port_revert(self):
     """Revert port should run without error and remove all networks"""
     C.node.connect_network('node-01', 'eth0', 'net-01', 'vlan/native')
     deferred.apply_networking()
     assert C.port.show('mock-01', 'gi1/0/1') == {
             'node': 'node-01',
             'nic': 'eth0',
             'networks': {'vlan/native': 'net-01'}}
     response = C.port.port_revert('mock-01', 'gi1/0/1')
     assert uuid_pattern.match(response['status_id'])
     deferred.apply_networking()
     assert C.port.show('mock-01', 'gi1/0/1') == {
             'node': 'node-01',
             'nic': 'eth0',
             'networks': {}}
Exemplo n.º 34
0
 def test_port_revert(self):
     """Revert port should run without error and remove all networks"""
     C.node.connect_network(
         'runway_node_0', 'nic-with-port', 'runway_pxe', 'vlan/native')
     deferred.apply_networking()
     assert C.port.show('stock_switch_0', 'runway_node_0_port') == {
             'node': 'runway_node_0',
             'nic': 'nic-with-port',
             'networks': {'vlan/native': 'runway_pxe'}}
     response = C.port.port_revert('stock_switch_0', 'runway_node_0_port')
     assert uuid_pattern.match(response['status_id'])
     deferred.apply_networking()
     assert C.port.show('stock_switch_0', 'runway_node_0_port') == {
             'node': 'runway_node_0',
             'nic': 'nic-with-port',
             'networks': {}}
Exemplo n.º 35
0
    def run(self):
        logger = logging.getLogger(__name__)
        server.init()
        server.register_drivers()
        server.validate_state()
        migrations.check_db_schema()

        # Check if config contains usable sleep_time
        if (config.cfg.has_section('network-daemon')
                and config.cfg.has_option('network-daemon', 'sleep_time')):
            try:
                sleep_time = config.cfg.getfloat('network-daemon',
                                                 'sleep_time')
            except (ValueError):
                sys.exit("Error: sleep_time set to non-float value")
            if sleep_time <= 0 or sleep_time >= 3600:
                sys.exit("Error: sleep_time not within bounds "
                         "0 < sleep_time < 3600")
            if sleep_time > 60:
                logger.warn('sleep_time greater than 1 minute.')
        else:
            sleep_time = 2

        while True:
            # Empty the journal until it's empty; then delay so we don't tight
            # loop.
            while deferred.apply_networking():
                pass
            sleep(sleep_time)
Exemplo n.º 36
0
def serve_networks():
    """Start the HIL networking server"""
    from hil import model, deferred
    from time import sleep
    config.setup()
    server.init()
    server.register_drivers()
    server.validate_state()
    model.init_db()
    migrations.check_db_schema()

    # Check if config contains usable sleep_time
    if (cfg.has_section('network-daemon')
            and cfg.has_option('network-daemon', 'sleep_time')):
        try:
            sleep_time = cfg.getfloat('network-daemon', 'sleep_time')
        except (ValueError):
            sys.exit("Error: sleep_time set to non-float value")
        if sleep_time <= 0 or sleep_time >= 3600:
            sys.exit("Error: sleep_time not within bounds "
                     "0 < sleep_time < 3600")
        if sleep_time > 60:
            logger.warn('sleep_time greater than 1 minute.')
    else:
        sleep_time = 2

    while True:
        # Empty the journal until it's empty; then delay so we don't tight
        # loop.
        while deferred.apply_networking():
            pass
        sleep(sleep_time)
Exemplo n.º 37
0
Arquivo: admin.py Projeto: CCI-MOC/hil
    def run(self):
        logger = logging.getLogger(__name__)
        server.init()
        server.register_drivers()
        server.validate_state()
        migrations.check_db_schema()

        # Check if config contains usable sleep_time
        if (config.cfg.has_section('network-daemon') and
                config.cfg.has_option('network-daemon', 'sleep_time')):
            try:
                sleep_time = config.cfg.getfloat(
                    'network-daemon', 'sleep_time')
            except (ValueError):
                sys.exit("Error: sleep_time set to non-float value")
            if sleep_time <= 0 or sleep_time >= 3600:
                sys.exit("Error: sleep_time not within bounds "
                         "0 < sleep_time < 3600")
            if sleep_time > 60:
                logger.warn('sleep_time greater than 1 minute.')
        else:
            sleep_time = 2

        while True:
            # Empty the journal until it's empty; then delay so we don't tight
            # loop.
            while deferred.apply_networking():
                pass
            sleep(sleep_time)
Exemplo n.º 38
0
    def test_show_networking_action(self):
        """(successful) call to show_networking_action"""
        response = C.node.connect_network(
                'node-01', 'eth0', 'net-01', 'vlan/native'
                )
        status_id = response['status_id']

        response = C.node.show_networking_action(status_id)
        assert response == {'status': 'PENDING',
                            'node': 'node-01',
                            'nic': 'eth0',
                            'type': 'modify_port',
                            'channel': 'vlan/native',
                            'new_network': 'net-01'}

        deferred.apply_networking()
        response = C.node.show_networking_action(status_id)
        assert response['status'] == 'DONE'
Exemplo n.º 39
0
    def test_show_networking_action(self):
        """(successful) call to show_networking_action"""
        response = C.node.connect_network(
                'manhattan_node_0', 'nic-with-port',
                'manhattan_provider', 'vlan/native')
        status_id = response['status_id']

        response = C.node.show_networking_action(status_id)
        assert response == {'status': 'PENDING',
                            'node': 'manhattan_node_0',
                            'nic': 'nic-with-port',
                            'type': 'modify_port',
                            'channel': 'vlan/native',
                            'new_network': 'manhattan_provider'}

        deferred.apply_networking()
        response = C.node.show_networking_action(status_id)
        assert response['status'] == 'DONE'
Exemplo n.º 40
0
    def setUp(self):
        """Common setup for the tests.

        * node 'manhattan_node_0' is attached to network 'stock_int_pub', via
          'boot-nic'.

        This also sets some properties for easy access to the projects.
        """
        self.auth_backend = get_auth_backend()
        self.runway = model.Project.query.filter_by(label='runway').one()
        self.manhattan = model.Project.query.filter_by(label='manhattan').one()

        # The individual tests set the right project, but we need this to
        # connect the network during setup:
        self.auth_backend.set_project(self.manhattan)

        api.node_connect_network('manhattan_node_0', 'boot-nic',
                                 'stock_int_pub')
        deferred.apply_networking()
Exemplo n.º 41
0
 def test_port_revert(self):
     """Revert port should run without error and remove all networks"""
     C.node.connect_network('runway_node_0', 'nic-with-port', 'runway_pxe',
                            'vlan/native')
     deferred.apply_networking()
     assert C.port.show('stock_switch_0', 'runway_node_0_port') == {
         'node': 'runway_node_0',
         'nic': 'nic-with-port',
         'networks': {
             'vlan/native': 'runway_pxe'
         }
     }
     response = C.port.port_revert('stock_switch_0', 'runway_node_0_port')
     assert uuid_pattern.match(response['status_id'])
     deferred.apply_networking()
     assert C.port.show('stock_switch_0', 'runway_node_0_port') == {
         'node': 'runway_node_0',
         'nic': 'nic-with-port',
         'networks': {}
     }
Exemplo n.º 42
0
 def test_list_network_attachments(self):
     """ Test list of network attachments """
     assert C.network.list_network_attachments("manhattan_provider",
                                               "all") == {}
     assert C.network.list_network_attachments("runway_provider",
                                               "runway") == {}
     C.node.connect_network('manhattan_node_0', 'nic-with-port',
                            'manhattan_provider', 'vlan/native')
     deferred.apply_networking()
     assert C.network.list_network_attachments("manhattan_provider",
                                               "all") == {
                                                   'manhattan_node_0': {
                                                       'project':
                                                       'manhattan',
                                                       'nic':
                                                       'nic-with-port',
                                                       'channel':
                                                       'vlan/native'
                                                   }
                                               }
Exemplo n.º 43
0
    def setUp(self):
        """Common setup for the tests.

        * node 'manhattan_node_0' is attached to network 'stock_int_pub', via
          'boot-nic'.

        This also sets some properties for easy access to the projects.
        """
        self.auth_backend = get_auth_backend()
        self.runway = model.Project.query.filter_by(label='runway').one()
        self.manhattan = model.Project.query.filter_by(label='manhattan').one()

        # The individual tests set the right project, but we need this to
        # connect the network during setup:
        self.auth_backend.set_project(self.manhattan)

        api.node_connect_network('manhattan_node_0',
                                 'boot-nic',
                                 'stock_int_pub')
        deferred.apply_networking()
Exemplo n.º 44
0
 def test_two_networks(self):
     """Test port_revert on a port attached to two networks."""
     pxe_net_id = model.Network.query.filter_by(label='runway_pxe')\
         .one().network_id
     pub_net_id = model.Network.query.filter_by(label='stock_int_pub')\
         .one().network_id
     api.node_connect_network('runway_node_0',
                              'nic-with-port',
                              'runway_pxe',
                              'vlan/native')
     deferred.apply_networking()
     assert self.LOCAL_STATE['stock_switch_0']['runway_node_0_port'] == {
         'vlan/native': pxe_net_id,
     }
     api.node_connect_network('runway_node_0',
                              'nic-with-port',
                              'stock_int_pub',
                              'vlan/' + pub_net_id)
     deferred.apply_networking()
     assert self.LOCAL_STATE['stock_switch_0']['runway_node_0_port'] == {
         'vlan/native': pxe_net_id,
         'vlan/' + pub_net_id: pub_net_id,
     }
     api.port_revert('stock_switch_0', 'runway_node_0_port')
     deferred.apply_networking()
     assert self.LOCAL_STATE['stock_switch_0']['runway_node_0_port'] == {}
Exemplo n.º 45
0
    def test_saving_config_file(self):

        api.project_create('anvil-nextgen')
        nodes = self.collect_nodes()

        # Create two networks
        network_create_simple('net-0', 'anvil-nextgen')
        network_create_simple('net-1', 'anvil-nextgen')

        # save the old startup config before performing a networking action
        old_startup_config = self.get_config('startup')
        # Connect n0 and n1 to net-0 and net-1 respectively
        api.node_connect_network(nodes[0].label,
                                 nodes[0].nics[0].label,
                                 'net-0')

        api.node_connect_network(nodes[1].label,
                                 nodes[1].nics[0].label,
                                 'net-1')

        deferred.apply_networking()

        # get the running config, and the new startup config
        running_config = self.get_config('running')
        new_startup_config = self.get_config('startup')

        assert new_startup_config == running_config
        assert new_startup_config != old_startup_config

        # cleanup
        api.node_detach_network(nodes[0].label,
                                nodes[0].nics[0].label,
                                'net-0')

        api.node_detach_network(nodes[1].label,
                                nodes[1].nics[0].label,
                                'net-1')

        deferred.apply_networking()
Exemplo n.º 46
0
        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')
Exemplo n.º 47
0
def test_apply_networking(switch, network, fresh_database):
    '''Test to validate apply_networking commits actions incrementally

    This test verifies that the apply_networking() function in hil/deferred.py
    incrementally commits actions, which ensures that any error on an action
    will not require a complete rerun of the prior actions (e.g. if an error
    is thrown on the 3rd action, the 1st and 2nd action will have already been
    committed)

    The test also verifies that if a new networking action fails, then the
    old networking actions in the queue were commited.
    '''
    nic = []
    actions = []
    # initialize 3 nics and networking actions
    for i in range(0, 2):
        interface = 'gi1/0/%d' % (i)
        nic.append(new_nic(str(i)))
        nic[i].port = model.Port(label=interface, switch=switch)
        unique_id = str(uuid.uuid4())
        actions.append(
            model.NetworkingAction(nic=nic[i],
                                   new_network=network,
                                   channel='vlan/native',
                                   type='modify_port',
                                   uuid=unique_id,
                                   status='PENDING'))

    # Create another aciton of type revert_port. This action is invalid for the
    # test switch because the switch raises an error when the networking action
    # is of type revert port.
    unique_id = str(uuid.uuid4())
    nic.append(new_nic('2'))
    nic[2].port = model.Port(label=interface, switch=switch)
    actions.append(
        model.NetworkingAction(nic=nic[2],
                               new_network=None,
                               uuid=unique_id,
                               channel='',
                               status='PENDING',
                               type='revert_port'))

    # get some nic attributes before we close this db session.
    nic2_label = nic[2].label
    nic2_node = nic[2].owner.label

    for action in actions:
        db.session.add(action)
    db.session.commit()

    # simple check to ensure that right number of actions are added.
    total_count = db.session.query(model.NetworkingAction).count()
    assert total_count == 3

    deferred.apply_networking()

    # close the session opened by `apply_networking` when `handle_actions`
    # fails; without this the tests would just stall (when using postgres)
    db.session.close()

    local_db = new_db()

    errored_action = local_db.session \
        .query(model.NetworkingAction) \
        .order_by(model.NetworkingAction.id).filter_by(status='ERROR') \
        .one_or_none()

    # Count the number of actions with different statuses
    error_count = local_db.session \
        .query(model.NetworkingAction).filter_by(status='ERROR').count()

    pending_count = local_db.session \
        .query(model.NetworkingAction).filter_by(status='PENDING').count()

    done_count = local_db.session \
        .query(model.NetworkingAction).filter_by(status='DONE').count()

    # test that there's only 1 action that errored out in the queue and that it
    # is of type revert_port
    assert error_count == 1
    assert errored_action.type == 'revert_port'

    assert pending_count == 0
    assert done_count == 2
    local_db.session.commit()
    local_db.session.close()

    # add another action on a nic with a previously failed action.
    api.network_create('corsair', 'admin', '', '105')
    api.node_connect_network(nic2_node, nic2_label, 'corsair')

    # the api call should delete the errored action on that nic, and a new
    # pending action should appear.
    local_db = new_db()
    errored_action = local_db.session \
        .query(model.NetworkingAction) \
        .order_by(model.NetworkingAction.id).filter_by(status='ERROR') \
        .one_or_none()

    pending_action = local_db.session \
        .query(model.NetworkingAction) \
        .order_by(model.NetworkingAction.id).filter_by(status='PENDING') \
        .one_or_none()

    assert errored_action is None
    assert pending_action is not None

    local_db.session.commit()
    local_db.session.close()
Exemplo n.º 48
0
def test_apply_networking(switch, network, fresh_database):
    '''Test to validate apply_networking commits actions incrementally

    This test verifies that the apply_networking() function in hil/deferred.py
    incrementally commits actions, which ensures that any error on an action
    will not require a complete rerun of the prior actions (e.g. if an error
    is thrown on the 3rd action, the 1st and 2nd action will have already been
    committed)

    The test also verifies that if a new networking action fails, then the
    old networking actions in the queue were commited.
    '''
    nic = []
    actions = []
    # initialize 3 nics and networking actions
    for i in range(0, 2):
        interface = 'gi1/0/%d' % (i)
        nic.append(new_nic(str(i)))
        nic[i].port = model.Port(label=interface, switch=switch)
        unique_id = str(uuid.uuid4())
        actions.append(model.NetworkingAction(nic=nic[i],
                                              new_network=network,
                                              channel='vlan/native',
                                              type='modify_port',
                                              uuid=unique_id,
                                              status='PENDING'))

    # Create another aciton of type revert_port. This action is invalid for the
    # test switch because the switch raises an error when the networking action
    # is of type revert port.
    unique_id = str(uuid.uuid4())
    nic.append(new_nic('2'))
    nic[2].port = model.Port(label=interface, switch=switch)
    actions.append(model.NetworkingAction(nic=nic[2],
                                          new_network=None,
                                          uuid=unique_id,
                                          channel='',
                                          status='PENDING',
                                          type='revert_port'))

    # get some nic attributes before we close this db session.
    nic2_label = nic[2].label
    nic2_node = nic[2].owner.label

    for action in actions:
        db.session.add(action)
    db.session.commit()

    # simple check to ensure that right number of actions are added.
    total_count = db.session.query(model.NetworkingAction).count()
    assert total_count == 3

    deferred.apply_networking()

    # close the session opened by `apply_networking` when `handle_actions`
    # fails; without this the tests would just stall (when using postgres)
    db.session.close()

    local_db = new_db()

    errored_action = local_db.session \
        .query(model.NetworkingAction) \
        .order_by(model.NetworkingAction.id).filter_by(status='ERROR') \
        .one_or_none()

    # Count the number of actions with different statuses
    error_count = local_db.session \
        .query(model.NetworkingAction).filter_by(status='ERROR').count()

    pending_count = local_db.session \
        .query(model.NetworkingAction).filter_by(status='PENDING').count()

    done_count = local_db.session \
        .query(model.NetworkingAction).filter_by(status='DONE').count()

    # test that there's only 1 action that errored out in the queue and that it
    # is of type revert_port
    assert error_count == 1
    assert errored_action.type == 'revert_port'

    assert pending_count == 0
    assert done_count == 2
    local_db.session.commit()
    local_db.session.close()

    # add another action on a nic with a previously failed action.
    api.network_create('corsair', 'admin', '', '105')
    api.node_connect_network(nic2_node, nic2_label, 'corsair')

    # the api call should delete the errored action on that nic, and a new
    # pending action should appear.
    local_db = new_db()
    errored_action = local_db.session \
        .query(model.NetworkingAction) \
        .order_by(model.NetworkingAction.id).filter_by(status='ERROR') \
        .one_or_none()

    pending_action = local_db.session \
        .query(model.NetworkingAction) \
        .order_by(model.NetworkingAction.id).filter_by(status='PENDING') \
        .one_or_none()

    assert errored_action is None
    assert pending_action is not None

    local_db.session.commit()
    local_db.session.close()
Exemplo n.º 49
0
        def create_networks():
            """Create networks and connect things to them.

            Test various things along the way.
            """
            nodes = self.collect_nodes()

            # Create four networks
            network_create_simple('net-0', 'anvil-nextgen')
            network_create_simple('net-1', 'anvil-nextgen')
            network_create_simple('net-2', 'anvil-nextgen')
            network_create_simple('net-3', 'anvil-nextgen')

            ports = self.get_all_ports(nodes)
            # get the switch name from any of the nics
            switch = nodes[0].nics[0].port.owner

            # Assert that n0 and n1 are not on any network
            port_networks = self.get_port_networks(ports)

            assert self.get_network(nodes[0].nics[0].port, port_networks) == \
                set()
            assert self.get_network(nodes[1].nics[0].port, port_networks) == \
                set()

            # Get the channel ids for the tagged versions of the networks:
            net_tag = {}
            net_tag[0] = get_legal_channels('net-0')[1]
            net_tag[1] = get_legal_channels('net-1')[1]
            net_tag[2] = get_legal_channels('net-2')[1]

            # Connect node 0 to net-0 (native mode)
            api.node_connect_network(nodes[0].label,
                                     nodes[0].nics[0].label,
                                     'net-0')

            # before connecting node 1 to net-1 in tagged mode, we must check
            # if the switch supports nativeless trunk mode; if not, then we
            # add some native network and perform additional checks before
            # proceeding.
            if 'nativeless-trunk-mode' not in switch.get_capabilities():
                # connecting the first network as tagged should raise an error
                with pytest.raises(errors.BlockedError):
                    api.node_connect_network(nodes[1].label,
                                             nodes[1].nics[0].label,
                                             'net-2',
                                             channel=net_tag[2])
                api.node_connect_network(nodes[1].label,
                                         nodes[1].nics[0].label,
                                         'net-2')
                deferred.apply_networking()

            # Connect node 1 to net-1 (tagged mode)
            api.node_connect_network(nodes[1].label,
                                     nodes[1].nics[0].label,
                                     'net-1',
                                     channel=net_tag[1])
            deferred.apply_networking()
            # Assert that n0 and n1 are on isolated networks
            port_networks = self.get_port_networks(ports)
            assert self.get_network(nodes[0].nics[0].port, port_networks) == \
                set([nodes[0].nics[0].port])
            assert self.get_network(nodes[1].nics[0].port, port_networks) == \
                set([nodes[1].nics[0].port])

            # Add n2 and n3 to the same networks as n0 and n1 respectively, but
            # with different channels (native vs. tagged)
            if 'nativeless-trunk-mode' not in switch.get_capabilities():
                api.node_connect_network(nodes[2].label,
                                         nodes[2].nics[0].label,
                                         'net-3')
                deferred.apply_networking()
            api.node_connect_network(nodes[2].label,
                                     nodes[2].nics[0].label,
                                     'net-0',
                                     channel=net_tag[0])

            api.node_connect_network(nodes[3].label,
                                     nodes[3].nics[0].label,
                                     'net-1')
            deferred.apply_networking()

            # Assert that n2 and n3 have been added to n0 and n1's networks
            # respectively
            port_networks = self.get_port_networks(ports)
            assert self.get_network(nodes[0].nics[0].port, port_networks) == \
                set([nodes[0].nics[0].port, nodes[2].nics[0].port])
            assert self.get_network(nodes[1].nics[0].port, port_networks) == \
                set([nodes[1].nics[0].port, nodes[3].nics[0].port])

            # Verify that we can put nodes on more than one network, with
            # different channels:

            # at this point, node-2 is connected to net-0 (tagged)
            # and depending on the switch, to net-3 (native). Let's connect it
            # to net-1 (tagged) (which node-1 is connected to)
            api.node_connect_network(nodes[2].label,
                                     nodes[2].nics[0].label,
                                     'net-1',
                                     channel=net_tag[1])
            deferred.apply_networking()
            port_networks = self.get_port_networks(ports)
            # assert that node-2 was added to node-1's network correctly.
            assert self.get_network(nodes[1].nics[0].port, port_networks) == \
                set([nodes[1].nics[0].port,
                     nodes[2].nics[0].port,
                     nodes[3].nics[0].port])
Exemplo n.º 50
0
Arquivo: client.py Projeto: vsemp/hil
 def test_node_detach_network(self):
     """(successful) call to node_detach_network"""
     C.node.connect_network('node-04', 'eth0', 'net-04', 'vlan/native')
     deferred.apply_networking()
     assert C.node.detach_network('node-04', 'eth0', 'net-04') is None
     deferred.apply_networking()
Exemplo n.º 51
0
 def test_no_nic(self):
     with pytest.raises(api.NotFoundError):
         # free_port_0 is not attached to a nic.
         api.port_revert('stock_switch_0', 'free_port_0')
     deferred.apply_networking()
     assert self.LOCAL_STATE['stock_switch_0']['free_port_0'] == {}
Exemplo n.º 52
0
        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')
Exemplo n.º 53
0
        def create_networks():
            """Create networks and connect things to them.

            Test various things along the way.
            """
            nodes = self.collect_nodes()

            # Create four networks
            network_create_simple('net-0', 'anvil-nextgen')
            network_create_simple('net-1', 'anvil-nextgen')
            network_create_simple('net-2', 'anvil-nextgen')
            network_create_simple('net-3', 'anvil-nextgen')

            ports = self.get_all_ports(nodes)
            # get the switch name from any of the nics
            switch = nodes[0].nics[0].port.owner

            # Assert that n0 and n1 are not on any network
            port_networks = self.get_port_networks(ports)

            assert self.get_network(nodes[0].nics[0].port, port_networks) == \
                set()
            assert self.get_network(nodes[1].nics[0].port, port_networks) == \
                set()

            # Get the channel ids for the tagged versions of the networks:
            net_tag = {}
            net_tag[0] = get_legal_channels('net-0')[1]
            net_tag[1] = get_legal_channels('net-1')[1]
            net_tag[2] = get_legal_channels('net-2')[1]

            # Connect node 0 to net-0 (native mode)
            api.node_connect_network(nodes[0].label, nodes[0].nics[0].label,
                                     'net-0')

            # before connecting node 1 to net-1 in tagged mode, we must check
            # if the switch supports nativeless trunk mode; if not, then we
            # add some native network and perform additional checks before
            # proceeding.
            if 'nativeless-trunk-mode' not in switch.get_capabilities():
                # connecting the first network as tagged should raise an error
                with pytest.raises(errors.BlockedError):
                    api.node_connect_network(nodes[1].label,
                                             nodes[1].nics[0].label,
                                             'net-2',
                                             channel=net_tag[2])
                api.node_connect_network(nodes[1].label,
                                         nodes[1].nics[0].label, 'net-2')
                deferred.apply_networking()

            # Connect node 1 to net-1 (tagged mode)
            api.node_connect_network(nodes[1].label,
                                     nodes[1].nics[0].label,
                                     'net-1',
                                     channel=net_tag[1])
            deferred.apply_networking()
            # Assert that n0 and n1 are on isolated networks
            port_networks = self.get_port_networks(ports)
            assert self.get_network(nodes[0].nics[0].port, port_networks) == \
                set([nodes[0].nics[0].port])
            assert self.get_network(nodes[1].nics[0].port, port_networks) == \
                set([nodes[1].nics[0].port])

            # Add n2 and n3 to the same networks as n0 and n1 respectively, but
            # with different channels (native vs. tagged)
            if 'nativeless-trunk-mode' not in switch.get_capabilities():
                api.node_connect_network(nodes[2].label,
                                         nodes[2].nics[0].label, 'net-3')
                deferred.apply_networking()
            api.node_connect_network(nodes[2].label,
                                     nodes[2].nics[0].label,
                                     'net-0',
                                     channel=net_tag[0])

            api.node_connect_network(nodes[3].label, nodes[3].nics[0].label,
                                     'net-1')
            deferred.apply_networking()

            # Assert that n2 and n3 have been added to n0 and n1's networks
            # respectively
            port_networks = self.get_port_networks(ports)
            assert self.get_network(nodes[0].nics[0].port, port_networks) == \
                set([nodes[0].nics[0].port, nodes[2].nics[0].port])
            assert self.get_network(nodes[1].nics[0].port, port_networks) == \
                set([nodes[1].nics[0].port, nodes[3].nics[0].port])

            # Verify that we can put nodes on more than one network, with
            # different channels:

            # at this point, node-2 is connected to net-0 (tagged)
            # and depending on the switch, to net-3 (native). Let's connect it
            # to net-1 (tagged) (which node-1 is connected to)
            api.node_connect_network(nodes[2].label,
                                     nodes[2].nics[0].label,
                                     'net-1',
                                     channel=net_tag[1])
            deferred.apply_networking()
            port_networks = self.get_port_networks(ports)
            # assert that node-2 was added to node-1's network correctly.
            assert self.get_network(nodes[1].nics[0].port, port_networks) == \
                set([nodes[1].nics[0].port,
                     nodes[2].nics[0].port,
                     nodes[3].nics[0].port])