Exemple #1
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!"
        )
Exemple #2
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'] == {}
Exemple #3
0
def create_pending_actions_db():
    """Create database objects including a pending NetworkingAction.

    The first version of this function was used to create the dump
    'pending-networking-actions.sql'.
    """
    # At a minimum we need a project, node, nic, switch, port, and network:
    api.project_create('runway')
    api.node_register(
        'node-1',
        obm={
            'type': MOCK_OBM_TYPE,
            'user': '******',
            'host': 'host',
            'password': '******',
        },
    )
    api.node_register_nic('node-1', 'pxe', 'de:ad:be:ef:20:16')
    api.switch_register('sw0',
                        type=MOCK_SWITCH_TYPE,
                        username='******',
                        hostname='host',
                        password='******',
                        )
    api.switch_register_port('sw0', 'gi1/0/4')
    api.port_connect_nic('sw0', 'gi1/0/4', 'node-1', 'pxe')
    api.project_connect_node('runway', 'node-1')
    api.network_create('runway_pxe', 'runway', 'runway', '')

    # Queue up a networking action. Importantly, we do *not* call
    # deferred.apply_networking, as that would flush the action and
    # remove it from the database.
    api.node_connect_network('node-1', 'pxe', 'runway_pxe')
Exemple #4
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()
Exemple #5
0
def create_pending_actions_db():
    """Create database objects including a pending NetworkingAction.

    The first version of this function was used to create the dump
    'pending-networking-actions.sql'.
    """
    # At a minimum we need a project, node, nic, switch, port, and network:
    api.project_create('runway')
    api.node_register(
        'node-1',
        obm={
            'type': MOCK_OBM_TYPE,
            'user': '******',
            'host': 'host',
            'password': '******',
        },
    )
    api.node_register_nic('node-1', 'pxe', 'de:ad:be:ef:20:16')
    api.switch_register(
        'sw0',
        type=MOCK_SWITCH_TYPE,
        username='******',
        hostname='host',
        password='******',
    )
    api.switch_register_port('sw0', 'gi1/0/4')
    api.port_connect_nic('sw0', 'gi1/0/4', 'node-1', 'pxe')
    api.project_connect_node('runway', 'node-1')
    api.network_create('runway_pxe', 'runway', 'runway', '')

    # Queue up a networking action. Importantly, we do *not* call
    # deferred.apply_networking, as that would flush the action and
    # remove it from the database.
    api.node_connect_network('node-1', 'pxe', 'runway_pxe')
Exemple #6
0
 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()
Exemple #7
0
 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()
Exemple #8
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
Exemple #9
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
Exemple #10
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()
Exemple #11
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])
Exemple #12
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()
Exemple #13
0
def test_ensure_legal_operations():
    """Test to ensure that ensure_legal_operations works as expected"""

    # create a project and a network
    api.project_create('anvil-nextgen')
    network_create_simple('hammernet', 'anvil-nextgen')
    network_create_simple('pineapple', 'anvil-nextgen')

    # register a switch of type dellnos9 and add a port to it
    api.switch_register('s3048',
                        type=SWITCH_TYPE,
                        username="******",
                        password="******",
                        hostname="switchname",
                        interface_type="GigabitEthernet")
    api.switch_register_port('s3048', '1/3')
    switch = api.get_or_404(model.Switch, 's3048')

    # register a ndoe and a nic
    api.node_register(
        node='compute-01',
        obmd={
            'uri': 'http://obmd.example.com',
            'admin_token': 'secret',
        },
    )
    api.project_connect_node('anvil-nextgen', 'compute-01')
    api.node_register_nic('compute-01', 'eth0', 'DE:AD:BE:EF:20:14')
    nic = api.get_or_404(model.Nic, 'eth0')

    api.port_connect_nic('s3048', '1/3', 'compute-01', 'eth0')

    # connecting a trunked network wihtout having a native should fail.
    # call the method directly and test the API too.
    with pytest.raises(BlockedError):
        switch.ensure_legal_operation(nic, 'connect', 'vlan/1212')

    with pytest.raises(BlockedError):
        api.node_connect_network('compute-01', 'eth0', 'hammernet', 'vlan/40')

    # doing these operations in the correct order, that is native network first
    # and then trunked, should work.
    api.node_connect_network('compute-01', 'eth0', 'hammernet', 'vlan/native')
    mock_networking_action()
    api.node_connect_network('compute-01', 'eth0', 'pineapple', 'vlan/41')
    mock_networking_action()

    # removing these networks in the wrong order should not work.
    with pytest.raises(BlockedError):
        switch.ensure_legal_operation(nic, 'detach', 'vlan/native')

    with pytest.raises(BlockedError):
        api.node_detach_network('compute-01', 'eth0', 'hammernet')

    # removing networks in the right order should work
    api.node_detach_network('compute-01', 'eth0', 'pineapple')
    mock_networking_action()
    api.node_detach_network('compute-01', 'eth0', 'hammernet')
    mock_networking_action()
    db.session.close()
Exemple #14
0
def test_ensure_legal_operations():
    """Test to ensure that ensure_legal_operations works as expected"""

    # create a project and a network
    api.project_create('anvil-nextgen')
    network_create_simple('hammernet', 'anvil-nextgen')
    network_create_simple('pineapple', 'anvil-nextgen')

    # register a switch of type dellnos9 and add a port to it
    api.switch_register('s3048',
                        type=SWITCH_TYPE,
                        username="******",
                        password="******",
                        hostname="switchname",
                        interface_type="GigabitEthernet")
    api.switch_register_port('s3048', '1/3')
    switch = api.get_or_404(model.Switch, 's3048')

    # register a ndoe and a nic
    api.node_register(
        node='compute-01',
        obmd={
            'uri': 'http://obmd.example.com',
            'admin_token': 'secret',
        },
    )
    api.project_connect_node('anvil-nextgen', 'compute-01')
    api.node_register_nic('compute-01', 'eth0', 'DE:AD:BE:EF:20:14')
    nic = api.get_or_404(model.Nic, 'eth0')

    api.port_connect_nic('s3048', '1/3', 'compute-01', 'eth0')

    # connecting a trunked network wihtout having a native should fail.
    # call the method directly and test the API too.
    with pytest.raises(BlockedError):
        switch.ensure_legal_operation(nic, 'connect', 'vlan/1212')

    with pytest.raises(BlockedError):
        api.node_connect_network('compute-01', 'eth0', 'hammernet', 'vlan/40')

    # doing these operations in the correct order, that is native network first
    # and then trunked, should work.
    api.node_connect_network('compute-01', 'eth0', 'hammernet', 'vlan/native')
    mock_networking_action()
    api.node_connect_network('compute-01', 'eth0', 'pineapple', 'vlan/41')
    mock_networking_action()

    # removing these networks in the wrong order should not work.
    with pytest.raises(BlockedError):
        switch.ensure_legal_operation(nic, 'detach', 'vlan/native')

    with pytest.raises(BlockedError):
        api.node_detach_network('compute-01', 'eth0', 'hammernet')

    # removing networks in the right order should work
    api.node_detach_network('compute-01', 'eth0', 'pineapple')
    mock_networking_action()
    api.node_detach_network('compute-01', 'eth0', 'hammernet')
    mock_networking_action()
    db.session.close()
Exemple #15
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])
Exemple #16
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()
Exemple #17
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()

        self.auth_backend.set_project(self.manhattan)
        response = api.node_connect_network('manhattan_node_0', 'boot-nic',
                                            'stock_int_pub')
        self.status_id = json.loads(response[0])
        self.status_id = self.status_id['status_id']
Exemple #18
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()

        self.auth_backend.set_project(self.manhattan)
        response = api.node_connect_network('manhattan_node_0',
                                            'boot-nic',
                                            'stock_int_pub')
        self.status_id = json.loads(response[0])
        self.status_id = self.status_id['status_id']
Exemple #19
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])
Exemple #20
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()
Exemple #21
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])
Exemple #22
0
        def create_networks():
            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()

            # 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]

            # Connect node 0 to net-0 (native mode)
            api.node_connect_network(nodes[0].label,
                                     nodes[0].nics[0].label,
                                     'net-0')
            # 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)
            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:
            api.node_connect_network(nodes[2].label,
                                     nodes[2].nics[0].label,
                                     'net-1')
            deferred.apply_networking()
            port_networks = self.get_port_networks(ports)
            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])
Exemple #23
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()