예제 #1
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()
예제 #2
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')
예제 #3
0
파일: stress.py 프로젝트: kylehogan/haas
def test_many_http_queries():
    """Put a few objects in the db, then bombard the api with queries.

    This is intended to shake out problems like the resource leak discussed
    in issue #454.
    """
    # NOTE: Now that the session is managed by Flask-SQLAlchemy, failures here
    # are unlikely to be regressions of the issue that #454 fixed; we're no
    # longer managing the lifecycle of the session ourselves. It's not obvious
    # that this is more than clutter now, but let's not be too trigger happy
    # about deleting tests.
    with rest.app.test_request_context():
        rest.init_auth()
        api.node_register('node-99', obm={
            "type": "http://schema.massopencloud.org/haas/v0/obm/ipmi",
            "host": "ipmihost",
            "user": "******",
            "password": "******"})
        api.node_register('node-98', obm={
            "type": "http://schema.massopencloud.org/haas/v0/obm/ipmi",
            "host": "ipmihost",
            "user": "******",
            "password": "******"})
        api.node_register('node-97', obm={
            "type": "http://schema.massopencloud.org/haas/v0/obm/ipmi",
            "host": "ipmihost",
            "user": "******",
            "password": "******"})
        api.node_register_nic('node-99', 'eth0', 'DE:AD:BE:EF:20:14')
        api.node_register_nic('node-98', 'eth0', 'DE:AD:BE:EF:20:15')
        api.node_register_nic('node-97', 'eth0', 'DE:AD:BE:EF:20:16')
        api.project_create('anvil-nextgen')
        api.project_create('anvil-legacy')
        api.project_connect_node('anvil-nextgen', 'node-99')
        api.project_connect_node('anvil-legacy', 'node-98')

    client = rest.app.test_client()

    def _show_nodes(path):
        """Helper for the loop below.

        This does a GET on path, which must return a json list of names of
        nodes. It will then query the state of each node. If any request does
        not return 200 or has a body which is not valid json, the test will
        fail.
        """
        resp = client.get(path)
        assert resp.status_code == 200
        for node in json.loads(resp.get_data()):
            resp = client.get('/nodes/%s' % node)
            assert resp.status_code == 200
            # At least make sure the body parses:
            json.loads(resp.get_data())

    for i in range(100):
        _show_nodes('/nodes/free')
        resp = client.get('/projects')
        assert resp.status_code == 200
        for project in json.loads(resp.get_data()):
            _show_nodes('/project/%s/nodes' % project)
예제 #4
0
파일: database.py 프로젝트: gsilvis/haas
 def test_user_add_project(self):
     user_create('charlie', 'secret')
     api.project_create('acme-corp')
     user_add_project('charlie', 'acme-corp')
     user = api._must_find(User, 'charlie')
     project = api._must_find(model.Project, 'acme-corp')
     assert project in user.projects
     assert user in project.users
예제 #5
0
 def test_user_add_project(self):
     user_create('charlie', 'secret')
     api.project_create('acme-corp')
     user_add_project('charlie', 'acme-corp')
     user = api._must_find(User, 'charlie')
     project = api._must_find(model.Project, 'acme-corp')
     assert project in user.projects
     assert user in project.users
예제 #6
0
 def test_user_remove_project(self):
     self.dbauth.user_create('charlie', 'secret')
     api.project_create('acme-corp')
     self.dbauth.user_add_project('charlie', 'acme-corp')
     self.dbauth.user_remove_project('charlie', 'acme-corp')
     user = api._must_find(self.dbauth.User, 'charlie')
     project = api._must_find(model.Project, 'acme-corp')
     assert project not in user.projects
     assert user not in project.users
예제 #7
0
 def test_headnode(self, db):
     api.project_create('anvil-nextgen')
     network_create_simple('spider-web', 'anvil-nextgen')
     api.headnode_create('hn-0', 'anvil-nextgen', 'base-headnode')
     api.headnode_create_hnic('hn-0', 'hnic-0')
     api.headnode_connect_network('hn-0', 'hnic-0', 'spider-web')
     assert json.loads(api.show_headnode('hn-0'))['vncport'] is None
     api.headnode_start('hn-0')
     assert json.loads(api.show_headnode('hn-0'))['vncport'] is not None
     api.headnode_stop('hn-0')
     api.headnode_delete('hn-0')
예제 #8
0
 def test_headnode(self, db):
     api.project_create('anvil-nextgen')
     network_create_simple('spider-web', 'anvil-nextgen')
     api.headnode_create('hn-0', 'anvil-nextgen', 'base-headnode')
     api.headnode_create_hnic('hn-0', 'hnic-0')
     api.headnode_connect_network('hn-0', 'hnic-0', 'spider-web')
     assert json.loads(api.show_headnode('hn-0'))['vncport'] is None
     api.headnode_start('hn-0')
     assert json.loads(api.show_headnode('hn-0'))['vncport'] is not None
     api.headnode_stop('hn-0')
     api.headnode_delete('hn-0')
예제 #9
0
파일: stress.py 프로젝트: starbops/haas
def test_many_http_queries():
    """Put a few objects in the db, then bombard the api with queries.

    This is intended to shake out problems like the resource leak discussed
    in issue #454.
    """
    with rest.RequestContext():
        api.node_register('node-99', obm={
                  "type": "http://schema.massopencloud.org/haas/v0/obm/ipmi",
                  "host": "ipmihost",
                  "user": "******",
                  "password": "******"})
        api.node_register('node-98', obm={
                  "type": "http://schema.massopencloud.org/haas/v0/obm/ipmi",
                  "host": "ipmihost",
                  "user": "******",
                  "password": "******"})
        api.node_register('node-97', obm={
                  "type": "http://schema.massopencloud.org/haas/v0/obm/ipmi",
                  "host": "ipmihost",
                  "user": "******",
                  "password": "******"})
        api.node_register_nic('node-99', 'eth0', 'DE:AD:BE:EF:20:14')
        api.node_register_nic('node-98', 'eth0', 'DE:AD:BE:EF:20:15')
        api.node_register_nic('node-97', 'eth0', 'DE:AD:BE:EF:20:16')
        api.project_create('anvil-nextgen')
        api.project_create('anvil-legacy')
        api.project_connect_node('anvil-nextgen', 'node-99')
        api.project_connect_node('anvil-legacy', 'node-98')

    def _show_nodes(path):
        """Helper for the loop below.

        This does a GET on path, which must return a json list of names of
        nodes. It will then query the state of each node. If any request does
        not return 200 or has a body which is not valid json, the test will
        fail.
        """
        resp = do_request('GET', path)
        assert resp.status_code == 200
        for node in json.loads(resp.get_data()):
            resp = do_request('GET', '/node/%s' % node)
            assert resp.status_code == 200
            # At least make sure the body parses:
            json.loads(resp.get_data())

    for i in range(100):
        _show_nodes('/free_nodes')
        resp = do_request('GET', '/projects')
        assert resp.status_code == 200
        for project in json.loads(resp.get_data()):
            _show_nodes('/project/%s/nodes' % project)
예제 #10
0
def test_test():

    api.node_register('node-99',
                      obm={
                          "type":
                          "http://schema.massopencloud.org/haas/v0/obm/ipmi",
                          "host": "ipmihost",
                          "user": "******",
                          "password": "******"
                      })
    api.project_create('anvil-nextgen')
    api.project_connect_node('anvil-nextgen', 'node-99')
    api.project_detach_node('anvil-nextgen', 'node-99')
    api.project_delete('anvil-nextgen')
예제 #11
0
    def test_isolated_networks(self):
        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()

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

        def delete_networks():
            # 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
            for node in nodes:
                attachment = model.NetworkAttachment.query \
                    .filter_by(nic=node.nics[0]).one()
                api.node_detach_network(node.label, node.nics[0].label,
                                        attachment.network.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')

        # Create a project
        api.project_create('anvil-nextgen')

        create_networks()
        delete_networks()
예제 #12
0
    def test_isolated_networks(self):

        def get_legal_channels(network):
            response_body = api.show_network(network)
            response_body = json.loads(response_body)
            return response_body['channels']

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

        def delete_networks():
            # 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 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:
                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()

            # 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')

        # Create a project
        api.project_create('anvil-nextgen')

        create_networks()
        delete_networks()
예제 #13
0
def test_many_http_queries():
    """Put a few objects in the db, then bombard the api with queries.

    This is intended to shake out problems like the resource leak discussed
    in issue #454.
    """
    # NOTE: Now that the session is managed by Flask-SQLAlchemy, failures here
    # are unlikely to be regressions of the issue that #454 fixed; we're no
    # longer managing the lifecycle of the session ourselves. It's not obvious
    # that this is more than clutter now, but let's not be too trigger happy
    # about deleting tests.
    with rest.app.test_request_context():
        rest.init_auth()
        api.node_register(
            'node-99',
            obm={
                "type": "http://schema.massopencloud.org/haas/v0/obm/ipmi",
                "host": "ipmihost",
                "user": "******",
                "password": "******"
            })
        api.node_register(
            'node-98',
            obm={
                "type": "http://schema.massopencloud.org/haas/v0/obm/ipmi",
                "host": "ipmihost",
                "user": "******",
                "password": "******"
            })
        api.node_register(
            'node-97',
            obm={
                "type": "http://schema.massopencloud.org/haas/v0/obm/ipmi",
                "host": "ipmihost",
                "user": "******",
                "password": "******"
            })
        api.node_register_nic('node-99', 'eth0', 'DE:AD:BE:EF:20:14')
        api.node_register_nic('node-98', 'eth0', 'DE:AD:BE:EF:20:15')
        api.node_register_nic('node-97', 'eth0', 'DE:AD:BE:EF:20:16')
        api.project_create('anvil-nextgen')
        api.project_create('anvil-legacy')
        api.project_connect_node('anvil-nextgen', 'node-99')
        api.project_connect_node('anvil-legacy', 'node-98')

    client = rest.app.test_client()

    def _show_nodes(path):
        """Helper for the loop below.

        This does a GET on path, which must return a json list of names of
        nodes. It will then query the state of each node. If any request does
        not return 200 or has a body which is not valid json, the test will
        fail.
        """
        resp = client.get(path)
        assert resp.status_code == 200
        for node in json.loads(resp.get_data()):
            resp = client.get('/nodes/%s' % node)
            assert resp.status_code == 200
            # At least make sure the body parses:
            json.loads(resp.get_data())

    for i in range(100):
        _show_nodes('/nodes/free')
        resp = client.get('/projects')
        assert resp.status_code == 200
        for project in json.loads(resp.get_data()):
            _show_nodes('/project/%s/nodes' % project)
예제 #14
0
파일: database.py 프로젝트: gsilvis/haas
 def test_bad_user_remove_project(self):
     """Tests that removing a user from a project they're not in fails."""
     user_create('charlie', 'secret')
     api.project_create('acme-corp')
     with pytest.raises(api.NotFoundError):
         user_remove_project('charlie', 'acme-corp')
예제 #15
0
파일: database.py 프로젝트: gsilvis/haas
 def test_duplicate_user_add_project(self):
     user_create('charlie', 'secret')
     api.project_create('acme-corp')
     user_add_project('charlie', 'acme-corp')
     with pytest.raises(api.DuplicateError):
         user_add_project('charlie', 'acme-corp')
예제 #16
0
 def test_bad_user_remove_project(self):
     """Tests that removing a user from a project they're not in fails."""
     user_create('charlie', 'secret')
     api.project_create('acme-corp')
     with pytest.raises(api.NotFoundError):
         user_remove_project('charlie', 'acme-corp')
예제 #17
0
 def test_duplicate_user_add_project(self):
     user_create('charlie', 'secret')
     api.project_create('acme-corp')
     user_add_project('charlie', 'acme-corp')
     with pytest.raises(api.DuplicateError):
         user_add_project('charlie', 'acme-corp')
예제 #18
0
    def test_isolated_networks(self, db):

        driver_name = cfg.get('general', 'driver')
        driver = importlib.import_module('haas.drivers.' + driver_name)

        def get_switch_vlans():
            return driver.get_switch_vlans(get_vlan_list())

        def get_network(port, vlan_cfg):
            """Returns all interfaces on the same network as a given port"""
            for vlan in vlan_cfg:
                if port in vlan_cfg[vlan]:
                    return vlan_cfg[vlan]
            return []

        def create_networks():
            # Add up to 4 available nodes with nics to the project
            free_nodes = db.query(model.Node).filter_by(project_id=None).all()
            nodes = []
            for node in free_nodes:
                if len(node.nics) > 0:
                    api.project_connect_node('anvil-nextgen', node.label)
                    nodes.append(node)
                    if len(nodes) >= 4:
                        break

            # If there are not enough nodes with nics, raise an exception
            if len(nodes) < 4:
                raise api.AllocationError(('At least 4 nodes with at least ' +
                    '1 NIC are required for this test. Only %d node(s) were ' +
                    'provided.') % len(nodes))

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

            # Convert each node to a dict for ease of access
            nodes = [{'label': n.label,
                      'nic': n.nics[0].label,
                      'port': n.nics[0].port.label}
                     for n in nodes]

            # Assert that n0 and n1 are not on any network
            vlan_cfgs = get_switch_vlans()

            assert get_network(nodes[0]['port'], vlan_cfgs) == []
            assert get_network(nodes[1]['port'], vlan_cfgs) == []

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

            # Assert that n0 and n1 are on isolated networks
            vlan_cfgs = get_switch_vlans()
            assert get_network(nodes[0]['port'], vlan_cfgs) == [nodes[0]['port']]
            assert get_network(nodes[1]['port'], vlan_cfgs) == [nodes[1]['port']]

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

            # Assert that n2 and n3 have been added to n0 and n1's networks
            # respectively
            vlan_cfgs = get_switch_vlans()
            assert sorted(get_network(nodes[0]['port'], vlan_cfgs)) == sorted([nodes[0]['port'], nodes[2]['port']])
            assert sorted(get_network(nodes[1]['port'], vlan_cfgs)) == sorted([nodes[1]['port'], nodes[3]['port']])


        def delete_networks():
            # Query the DB for nodes on this project
            project = api._must_find(db, model.Project, 'anvil-nextgen')
            nodes = project.nodes

            # Remove all nodes from their networks
            for node in nodes:
                if node.nics[0].network is not None:
                    api.node_detach_network(node.label, node.nics[0].label)
            deferred.apply_networking()

            # Assert that none of the nodes are on any network
            vlan_cfgs = get_switch_vlans()
            for node in nodes:
                assert get_network(node.nics[0].label, vlan_cfgs) == []

            # Delete the networks
            api.network_delete('net-0')
            api.network_delete('net-1')

        # Create a project
        api.project_create('anvil-nextgen')

        create_networks()
        delete_networks()
예제 #19
0
    def test_isolated_networks(self):
        def get_legal_channels(network):
            response_body = api.show_network(network)
            response_body = json.loads(response_body)
            return response_body['channels']

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

        def delete_networks():
            # 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 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:
                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()

            # 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')

        # Create a project
        api.project_create('anvil-nextgen')

        create_networks()
        delete_networks()
예제 #20
0
    def test_isolated_networks(self, db):

        driver_name = cfg.get('general', 'driver')
        driver = importlib.import_module('haas.drivers.' + driver_name)

        def get_switch_vlans():
            return driver.get_switch_vlans(get_vlan_list())

        def get_network(port, vlan_cfg):
            """Returns all interfaces on the same network as a given port"""
            for vlan in vlan_cfg:
                if port in vlan_cfg[vlan]:
                    return vlan_cfg[vlan]
            return []

        def create_networks():
            # Add up to 4 available nodes with nics to the project
            free_nodes = db.query(model.Node).filter_by(project_id=None).all()
            nodes = []
            for node in free_nodes:
                if len(node.nics) > 0:
                    api.project_connect_node('anvil-nextgen', node.label)
                    nodes.append(node)
                    if len(nodes) >= 4:
                        break

            # If there are not enough nodes with nics, raise an exception
            if len(nodes) < 4:
                raise api.AllocationError(
                    ('At least 4 nodes with at least ' +
                     '1 NIC are required for this test. Only %d node(s) were '
                     + 'provided.') % len(nodes))

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

            # Convert each node to a dict for ease of access
            nodes = [{
                'label': n.label,
                'nic': n.nics[0].label,
                'port': n.nics[0].port.label
            } for n in nodes]

            # Assert that n0 and n1 are not on any network
            vlan_cfgs = get_switch_vlans()

            assert get_network(nodes[0]['port'], vlan_cfgs) == []
            assert get_network(nodes[1]['port'], vlan_cfgs) == []

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

            # Assert that n0 and n1 are on isolated networks
            vlan_cfgs = get_switch_vlans()
            assert get_network(nodes[0]['port'],
                               vlan_cfgs) == [nodes[0]['port']]
            assert get_network(nodes[1]['port'],
                               vlan_cfgs) == [nodes[1]['port']]

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

            # Assert that n2 and n3 have been added to n0 and n1's networks
            # respectively
            vlan_cfgs = get_switch_vlans()
            assert sorted(get_network(nodes[0]['port'], vlan_cfgs)) == sorted(
                [nodes[0]['port'], nodes[2]['port']])
            assert sorted(get_network(nodes[1]['port'], vlan_cfgs)) == sorted(
                [nodes[1]['port'], nodes[3]['port']])

        def delete_networks():
            # Query the DB for nodes on this project
            project = api._must_find(db, model.Project, 'anvil-nextgen')
            nodes = project.nodes

            # Remove all nodes from their networks
            for node in nodes:
                if node.nics[0].network is not None:
                    api.node_detach_network(node.label, node.nics[0].label)
            deferred.apply_networking()

            # Assert that none of the nodes are on any network
            vlan_cfgs = get_switch_vlans()
            for node in nodes:
                assert get_network(node.nics[0].label, vlan_cfgs) == []

            # Delete the networks
            api.network_delete('net-0')
            api.network_delete('net-1')

        # Create a project
        api.project_create('anvil-nextgen')

        create_networks()
        delete_networks()
예제 #21
0
 def test_headnode_deletion_while_running(self, db):
     api.project_create('anvil-nextgen')
     api.headnode_create('hn-0', 'anvil-nextgen', 'base-headnode-2')
     api.headnode_start('hn-0')
     api.headnode_delete('hn-0')
예제 #22
0
    def test_isolated_networks(self, db):

        def get_legal_channels(network):
            response_body = api.show_network(network)
            response_body = json.loads(response_body)
            return response_body['channels']

        def create_networks():
            nodes = self.collect_nodes(db)

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


        def delete_networks():
            # Query the DB for nodes on this project
            project = api._must_find(db, model.Project, 'anvil-nextgen')
            nodes = project.nodes
            ports = self.get_all_ports(nodes)

            # Remove all nodes from their networks
            for node in nodes:
                attachments = db.query(model.NetworkAttachment)\
                    .filter_by(nic=node.nics[0]).all()
                for attachment in attachments:
                    api.node_detach_network(node.label,
                                            node.nics[0].label,
                                            attachment.network.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')

        # Create a project
        api.project_create('anvil-nextgen')

        create_networks()
        delete_networks()
예제 #23
0
    def test_isolated_networks(self, db):
        def create_networks():
            nodes = self.collect_nodes(db)

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

        def delete_networks():
            # Query the DB for nodes on this project
            project = api._must_find(db, model.Project, "anvil-nextgen")
            nodes = project.nodes
            ports = self.get_all_ports(nodes)

            # Remove all nodes from their networks
            for node in nodes:
                attachment = db.query(model.NetworkAttachment).filter_by(nic=node.nics[0]).one()
                api.node_detach_network(node.label, node.nics[0].label, attachment.network.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")

        # Create a project
        api.project_create("anvil-nextgen")

        create_networks()
        delete_networks()
예제 #24
0
 def test_headnode_deletion_while_running(self, db):
     api.project_create('anvil-nextgen')
     api.headnode_create('hn-0', 'anvil-nextgen', 'base-headnode-2')
     api.headnode_start('hn-0')
     api.headnode_delete('hn-0')