예제 #1
0
파일: test_common.py 프로젝트: henn/hil
def additional_db():
    """ Populated database with additional objects needed for testing.

    The database setup in initial_db is required to remain static as
    a starting point for database migrations so any changes needed for
    testing should be made in additional_db.
    """

    initial_db()

    switch = db.session.query(Switch).filter_by(label="stock_switch_0").one()

    with app.app_context():
        for node_label in ["runway_node_0", "runway_node_1", "manhattan_node_0", "manhattan_node_1"]:

            node = db.session.query(Node).filter_by(label=node_label).one()
            nic = db.session.query(Nic).filter_by(owner=node, label="boot-nic").one()

            port = Port("connected_port_0", switch)
            port.nic = nic
            nic.port = port

            db.session.add(node)

        db.session.add(switch)
        db.session.commit()
예제 #2
0
def additional_db():
    """ Populated database with additional objects needed for testing.

    The database setup in initial_db is required to remain static as
    a starting point for database migrations so any changes needed for
    testing should be made in additional_db.
    """

    initial_db()

    switch = db.session.query(Switch).filter_by(label="stock_switch_0").one()
    manhattan = db.session.query(Project).filter_by(label="manhattan").one()
    runway = db.session.query(Project).filter_by(label="runway").one()

    with app.app_context():
        for node_label in ['runway_node_0', 'runway_node_1',
                           'manhattan_node_0', 'manhattan_node_1']:

            node = db.session.query(Node).filter_by(label=node_label).one()
            nic = db.session.query(Nic).filter_by(owner=node,
                                                  label='boot-nic').one()

            port = Port('connected_port_0', switch)
            port.nic = nic
            nic.port = port

        networks = [
            {
                'owner': None,
                'access': [manhattan, runway],
                'allocated': False,
                'network_id': 'manhattan_runway_provider_chan',
                'label': 'manhattan_runway_provider',
            },
            {
                'owner': manhattan,
                'access': [manhattan, runway],
                'allocated': True,
                'label': 'manhattan_runway_pxe',
            },
        ]

        for net in networks:
            if net['allocated']:
                net['network_id'] = \
                    get_network_allocator().get_new_network_id()
            db.session.add(Network(**net))
            db.session.add(node)

        db.session.add(switch)
        node = db.session.query(Node).filter_by(label='runway_node_0').one()
        db.session.add(Metadata('EK', 'pk', node))
        db.session.add(Metadata('SHA256', 'b5962d8173c14e60259211bcf25d1263c36'
                                'e0ad7da32ba9d07b224eac1834813', node))
        db.session.commit()
예제 #3
0
def additional_db():
    """ Populated database with additional objects needed for testing.

    The database setup in initial_db is required to remain static as
    a starting point for database migrations so any changes needed for
    testing should be made in additional_db.
    """

    initial_db()

    switch = db.session.query(Switch).filter_by(label="stock_switch_0").one()
    manhattan = db.session.query(Project).filter_by(label="manhattan").one()
    runway = db.session.query(Project).filter_by(label="runway").one()

    with app.app_context():
        for node_label in ['runway_node_0', 'runway_node_1',
                           'manhattan_node_0', 'manhattan_node_1']:

            node = db.session.query(Node).filter_by(label=node_label).one()
            nic = db.session.query(Nic).filter_by(owner=node,
                                                  label='boot-nic').one()

            port = Port('connected_port_0', switch)
            port.nic = nic
            nic.port = port

        networks = [
            {
                'owner': None,
                'access': [manhattan, runway],
                'allocated': False,
                'network_id': 'manhattan_runway_provider_chan',
                'label': 'manhattan_runway_provider',
            },
            {
                'owner': manhattan,
                'access': [manhattan, runway],
                'allocated': True,
                'label': 'manhattan_runway_pxe',
            },
        ]

        for net in networks:
            if net['allocated']:
                net['network_id'] = \
                    get_network_allocator().get_new_network_id()
            db.session.add(Network(**net))
            db.session.add(node)

        db.session.add(switch)
        db.session.commit()
예제 #4
0
def initial_db():
    """Populates the database with a useful set of objects.

    This allows us to avoid some boilerplate in tests which need a few objects
    in the database in order to work.

    Note that this fixture requires the use of the following extensions:

        - haas.ext.switches.mock
        - haas.ext.obm.mock
    """
    for required_extension in 'haas.ext.switches.mock', 'haas.ext.obm.mock':
        assert required_extension in sys.modules, \
            "The 'initial_db' fixture requires the extension %r" % \
            required_extension

    from haas.ext.switches.mock import MockSwitch
    from haas.ext.obm.mock import MockObm

    with app.app_context():
        # Create a couple projects:
        runway = Project("runway")
        manhattan = Project("manhattan")
        for proj in [runway, manhattan]:
            db.session.add(proj)

        # ...including at least one with nothing in it:
        db.session.add(Project('empty-project'))

        # ...A variety of networks:

        networks = [
            {
                'creator': None,
                'access': None,
                'allocated': True,
                'label': 'stock_int_pub',
            },
            {
                'creator': None,
                'access': None,
                'allocated': False,
                'network_id': 'ext_pub_chan',
                'label': 'stock_ext_pub',
            },
            {
                # For some tests, we want things to initial be attached to a
                # network. This one serves that purpose; using the others would
                # interfere with some of the network_delete tests.
                'creator': None,
                'access': None,
                'allocated': True,
                'label': 'pub_default',
            },
            {
                'creator': runway,
                'access': runway,
                'allocated': True,
                'label': 'runway_pxe'
            },
            {
                'creator': None,
                'access': runway,
                'allocated': False,
                'network_id': 'runway_provider_chan',
                'label': 'runway_provider',
            },
            {
                'creator': manhattan,
                'access': manhattan,
                'allocated': True,
                'label': 'manhattan_pxe'
            },
            {
                'creator': None,
                'access': manhattan,
                'allocated': False,
                'network_id': 'manhattan_provider_chan',
                'label': 'manhattan_provider',
            },
        ]

        for net in networks:
            if net['allocated']:
                net['network_id'] = \
                    get_network_allocator().get_new_network_id()
            db.session.add(Network(**net))

        # ... Two switches. One of these is just empty, for testing deletion:
        db.session.add(
            MockSwitch(label='empty-switch',
                       hostname='empty',
                       username='******',
                       password='******',
                       type=MockSwitch.api_name))

        # ... The other we'll actually attach stuff to for other tests:
        switch = MockSwitch(label="stock_switch_0",
                            hostname='stock',
                            username='******',
                            password='******',
                            type=MockSwitch.api_name)

        # ... Some free ports:
        db.session.add(Port('free_port_0', switch))
        db.session.add(Port('free_port_1', switch))

        # ... Some nodes (with projets):
        nodes = [
            {
                'label': 'runway_node_0',
                'project': runway
            },
            {
                'label': 'runway_node_1',
                'project': runway
            },
            {
                'label': 'manhattan_node_0',
                'project': manhattan
            },
            {
                'label': 'manhattan_node_1',
                'project': manhattan
            },
            {
                'label': 'free_node_0',
                'project': None
            },
            {
                'label': 'free_node_1',
                'project': None
            },
        ]
        for node_dict in nodes:
            obm = MockObm(type=MockObm.api_name,
                          host=node_dict['label'],
                          user='******',
                          password='******')
            node = Node(label=node_dict['label'], obm=obm)
            node.project = node_dict['project']
            db.session.add(Nic(node, label='boot-nic', mac_addr='Unknown'))

            # give it a nic that's attached to a port:
            port_nic = Nic(node, label='nic-with-port', mac_addr='Unknown')
            port = Port(node_dict['label'] + '_port', switch)
            port.nic = port_nic

        # ... Some headnodes:
        headnodes = [
            {
                'label': 'runway_headnode_on',
                'project': runway,
                'on': True
            },
            {
                'label': 'runway_headnode_off',
                'project': runway,
                'on': False
            },
            {
                'label': 'runway_manhattan_on',
                'project': manhattan,
                'on': True
            },
            {
                'label': 'runway_manhattan_off',
                'project': manhattan,
                'on': False
            },
        ]
        for hn_dict in headnodes:
            headnode = Headnode(hn_dict['project'], hn_dict['label'],
                                'base-headnode')
            headnode.dirty = not hn_dict['on']
            hnic = Hnic(headnode, 'pxe')
            db.session.add(hnic)

            # Connect them to a network, so we can test detaching.
            hnic = Hnic(headnode, 'public')
            hnic.network = Network.query \
                .filter_by(label='pub_default').one()

        # ... and at least one node with no nics (useful for testing delete):
        obm = MockObm(type=MockObm.api_name,
                      host='hostname',
                      user='******',
                      password='******')
        db.session.add(Node(label='no_nic_node', obm=obm))

        db.session.commit()
예제 #5
0
def releaseDB():
    """Do we need to do anything here to release resources?"""
    with app.app_context():
        db.drop_all()
예제 #6
0
def newDB():
    """Configures and returns a connection to a freshly initialized DB."""
    with app.app_context():
        init_db()
        create_db()
예제 #7
0
def initial_db():
    """Populates the database with a useful set of objects.

    This allows us to avoid some boilerplate in tests which need a few objects
    in the database in order to work.

    Is static to allow database migrations to be tested, if new objects need
    to be added they should be included in additional_db not initial_db.

    Note that this fixture requires the use of the following extensions:

        - haas.ext.switches.mock
        - haas.ext.obm.mock
    """
    for required_extension in 'haas.ext.switches.mock', 'haas.ext.obm.mock':
        assert required_extension in sys.modules, \
            "The 'initial_db' fixture requires the extension %r" % \
            required_extension

    from haas.ext.switches.mock import MockSwitch
    from haas.ext.obm.mock import MockObm

    with app.app_context():
        # Create a couple projects:
        runway = Project("runway")
        manhattan = Project("manhattan")
        for proj in [runway, manhattan]:
            db.session.add(proj)

        # ...including at least one with nothing in it:
        db.session.add(Project('empty-project'))

        # ...A variety of networks:

        networks = [
            {
                'owner': None,
                'access': [],
                'allocated': True,
                'label': 'stock_int_pub',
            },
            {
                'owner': None,
                'access': [],
                'allocated': False,
                'network_id': 'ext_pub_chan',
                'label': 'stock_ext_pub',
            },
            {
                # For some tests, we want things to initially be attached to a
                # network. This one serves that purpose; using the others would
                # interfere with some of the network_delete tests.
                'owner': None,
                'access': [],
                'allocated': True,
                'label': 'pub_default',
            },
            {
                'owner': runway,
                'access': [runway],
                'allocated': True,
                'label': 'runway_pxe'
            },
            {
                'owner': None,
                'access': [runway],
                'allocated': False,
                'network_id': 'runway_provider_chan',
                'label': 'runway_provider',
            },
            {
                'owner': manhattan,
                'access': [manhattan],
                'allocated': True,
                'label': 'manhattan_pxe'
            },
            {
                'owner': None,
                'access': [manhattan],
                'allocated': False,
                'network_id': 'manhattan_provider_chan',
                'label': 'manhattan_provider',
            },
        ]

        for net in networks:
            if net['allocated']:
                net['network_id'] = \
                    get_network_allocator().get_new_network_id()
            db.session.add(Network(**net))

        # ... Two switches. One of these is just empty, for testing deletion:
        db.session.add(MockSwitch(label='empty-switch',
                                  hostname='empty',
                                  username='******',
                                  password='******',
                                  type=MockSwitch.api_name))

        # ... The other we'll actually attach stuff to for other tests:
        switch = MockSwitch(label="stock_switch_0",
                            hostname='stock',
                            username='******',
                            password='******',
                            type=MockSwitch.api_name)

        # ... Some free ports:
        db.session.add(Port('free_port_0', switch))
        db.session.add(Port('free_port_1', switch))

        # ... Some nodes (with projets):
        nodes = [
            {'label': 'runway_node_0', 'project': runway},
            {'label': 'runway_node_1', 'project': runway},
            {'label': 'manhattan_node_0', 'project': manhattan},
            {'label': 'manhattan_node_1', 'project': manhattan},
            {'label': 'free_node_0', 'project': None},
            {'label': 'free_node_1', 'project': None},
        ]
        for node_dict in nodes:
            obm = MockObm(type=MockObm.api_name,
                          host=node_dict['label'],
                          user='******',
                          password='******')
            node = Node(label=node_dict['label'], obm=obm)
            node.project = node_dict['project']
            db.session.add(Nic(node, label='boot-nic', mac_addr='Unknown'))

            # give it a nic that's attached to a port:
            port_nic = Nic(node, label='nic-with-port', mac_addr='Unknown')
            port = Port(node_dict['label'] + '_port', switch)
            port.nic = port_nic

        # ... Some headnodes:
        headnodes = [
            {'label': 'runway_headnode_on',
             'project': runway,
             'on': True},
            {'label': 'runway_headnode_off',
             'project': runway,
             'on': False},
            {'label': 'runway_manhattan_on',
             'project': manhattan,
             'on': True},
            {'label': 'runway_manhattan_off',
             'project': manhattan,
             'on': False},
        ]
        for hn_dict in headnodes:
            headnode = Headnode(hn_dict['project'],
                                hn_dict['label'],
                                'base-headnode')
            headnode.dirty = not hn_dict['on']
            hnic = Hnic(headnode, 'pxe')
            db.session.add(hnic)

            # Connect them to a network, so we can test detaching.
            hnic = Hnic(headnode, 'public')
            hnic.network = Network.query \
                .filter_by(label='pub_default').one()

        # ... and at least one node with no nics (useful for testing delete):
        obm = MockObm(type=MockObm.api_name,
                      host='hostname',
                      user='******',
                      password='******')
        db.session.add(Node(label='no_nic_node', obm=obm))

        db.session.commit()
예제 #8
0
def releaseDB():
    """Do we need to do anything here to release resources?"""
    with app.app_context():
        db.drop_all()
예제 #9
0
def newDB():
    """Configures and returns a connection to a freshly initialized DB."""
    with app.app_context():
        init_db()
        create_db()
예제 #10
0
파일: test_common.py 프로젝트: henn/hil
def initial_db():
    """Populates the database with a useful set of objects.

    This allows us to avoid some boilerplate in tests which need a few objects
    in the database in order to work.

    Note that this fixture requires the use of the following extensions:

        - haas.ext.switches.mock
        - haas.ext.obm.mock
    """
    for required_extension in "haas.ext.switches.mock", "haas.ext.obm.mock":
        assert required_extension in sys.modules, (
            "The 'initial_db' fixture requires the extension %r" % required_extension
        )

    from haas.ext.switches.mock import MockSwitch
    from haas.ext.obm.mock import MockObm

    with app.app_context():
        # Create a couple projects:
        runway = Project("runway")
        manhattan = Project("manhattan")
        for proj in [runway, manhattan]:
            db.session.add(proj)

        # ...including at least one with nothing in it:
        db.session.add(Project("empty-project"))

        # ...A variety of networks:

        networks = [
            {"creator": None, "access": None, "allocated": True, "label": "stock_int_pub"},
            {
                "creator": None,
                "access": None,
                "allocated": False,
                "network_id": "ext_pub_chan",
                "label": "stock_ext_pub",
            },
            {
                # For some tests, we want things to initial be attached to a
                # network. This one serves that purpose; using the others would
                # interfere with some of the network_delete tests.
                "creator": None,
                "access": None,
                "allocated": True,
                "label": "pub_default",
            },
            {"creator": runway, "access": runway, "allocated": True, "label": "runway_pxe"},
            {
                "creator": None,
                "access": runway,
                "allocated": False,
                "network_id": "runway_provider_chan",
                "label": "runway_provider",
            },
            {"creator": manhattan, "access": manhattan, "allocated": True, "label": "manhattan_pxe"},
            {
                "creator": None,
                "access": manhattan,
                "allocated": False,
                "network_id": "manhattan_provider_chan",
                "label": "manhattan_provider",
            },
        ]

        for net in networks:
            if net["allocated"]:
                net["network_id"] = get_network_allocator().get_new_network_id()
            db.session.add(Network(**net))

        # ... Two switches. One of these is just empty, for testing deletion:
        db.session.add(
            MockSwitch(
                label="empty-switch", hostname="empty", username="******", password="******", type=MockSwitch.api_name
            )
        )

        # ... The other we'll actually attach stuff to for other tests:
        switch = MockSwitch(
            label="stock_switch_0", hostname="stock", username="******", password="******", type=MockSwitch.api_name
        )

        # ... Some free ports:
        db.session.add(Port("free_port_0", switch))
        db.session.add(Port("free_port_1", switch))

        # ... Some nodes (with projets):
        nodes = [
            {"label": "runway_node_0", "project": runway},
            {"label": "runway_node_1", "project": runway},
            {"label": "manhattan_node_0", "project": manhattan},
            {"label": "manhattan_node_1", "project": manhattan},
            {"label": "free_node_0", "project": None},
            {"label": "free_node_1", "project": None},
        ]
        for node_dict in nodes:
            obm = MockObm(type=MockObm.api_name, host=node_dict["label"], user="******", password="******")
            node = Node(label=node_dict["label"], obm=obm)
            node.project = node_dict["project"]
            db.session.add(Nic(node, label="boot-nic", mac_addr="Unknown"))

            # give it a nic that's attached to a port:
            port_nic = Nic(node, label="nic-with-port", mac_addr="Unknown")
            port = Port(node_dict["label"] + "_port", switch)
            port.nic = port_nic

        # ... Some headnodes:
        headnodes = [
            {"label": "runway_headnode_on", "project": runway, "on": True},
            {"label": "runway_headnode_off", "project": runway, "on": False},
            {"label": "runway_manhattan_on", "project": manhattan, "on": True},
            {"label": "runway_manhattan_off", "project": manhattan, "on": False},
        ]
        for hn_dict in headnodes:
            headnode = Headnode(hn_dict["project"], hn_dict["label"], "base-headnode")
            headnode.dirty = not hn_dict["on"]
            hnic = Hnic(headnode, "pxe")
            db.session.add(hnic)

            # Connect them to a network, so we can test detaching.
            hnic = Hnic(headnode, "public")
            hnic.network = Network.query.filter_by(label="pub_default").one()

        # ... and at least one node with no nics (useful for testing delete):
        obm = MockObm(type=MockObm.api_name, host="hostname", user="******", password="******")
        db.session.add(Node(label="no_nic_node", obm=obm))

        db.session.commit()