Exemplo n.º 1
0
def test_obmd_migrate(tmpdir):
    """The test proper.

    Create some nodes, run the script, and verify that it has done the right
    thing.
    """
    from hil.ext.obm.ipmi import Ipmi

    # Add some objects to the hil database:
    with app.app_context():
        for i in range(4):
            node = model.Node(label='node-%d' % i,
                              obm=Ipmi(user='******',
                                       host='10.0.0.%d' % (100 + i),
                                       password='******'))
            model.db.session.add(node)
        model.db.session.commit()

    check_call([
        'hil-admin', 'migrate-ipmi-info',
        '--obmd-base-url', OBMD_BASE_URL,
        '--obmd-admin-token', ADMIN_TOKEN,
    ])

    with app.app_context():
        for node in model.Node.query.all():

            # Check that the db info was updated correctly:
            assert node.obmd_admin_token == ADMIN_TOKEN, (
                "Node %s{label}'s admin token was incorrect: %s{token}"
                .format(
                    label=node.label,
                    token=node.obmd_admin_token,
                )
            )
            assert node.obmd_uri == OBMD_BASE_URL + '/node/' + node.label, (
                "Node %s{label}'s obmd_uri was incorrect: %s{uri}"
                .format(
                    label=node.label,
                    uri=node.obmd_uri,
                )
            )

            # Make sure obmd thinks the nodes are there; if so it should be
            # possible to get a token:
            sess = requests.Session()
            sess.auth = ('admin', ADMIN_TOKEN)
            resp = sess.post(node.obmd_uri + '/token')
            assert resp.ok, (
                "Failure getting token for node %s{label} from obmd; "
                "response: %s{resp}".format(
                    label=node.label,
                    resp=resp,
                )
            )
Exemplo n.º 2
0
def initial_admin():
    """Inserts an admin user into the database.

    This fixture is used by Test_user tests"""
    with app.app_context():
        from hil.ext.auth.database import User
        db.session.add(User(username, password, is_admin=True))
        db.session.commit()
Exemplo n.º 3
0
def initial_admin():
    """Inserts an admin user into the database.

    This fixture is used by Test_user tests
    """
    with app.app_context():
        from hil.ext.auth.database import User
        db.session.add(User(username, password, is_admin=True))
        db.session.commit()
Exemplo n.º 4
0
    def run_fn(f):
        """Run the function f and return a representation of its db."""

        # We can't just do db.drop_all(), since db's metadata won't
        # necessarily reflect the contents of the database. If there are
        # tables it doesn't know about, it may raise an exception.
        with app.app_context():
            drop_tables()
            f()

        return get_db_state()
Exemplo n.º 5
0
    def run_fn(f):
        """Run the function f and return a representation of its db."""

        # We can't just do db.drop_all(), since db's metadata won't
        # necessarily reflect the contents of the database. If there are
        # tables it doesn't know about, it may raise an exception.
        with app.app_context():
            drop_tables()
            f()

        return get_db_state()
Exemplo n.º 6
0
def initial_db(request, dbauth):
    fresh_database(request)
    with app.app_context():
        alice = dbauth.User(label='alice', password='******', is_admin=True)
        bob = dbauth.User(label='bob', password='******', is_admin=False)

        db.session.add(alice)
        db.session.add(bob)

        runway = model.Project('runway')
        runway.users.append(alice)
        db.session.add(runway)
        db.session.commit()
Exemplo n.º 7
0
def load_dump(filename):
    """Load a database dump and upgrades it to the latest schema.

    `filename` is path to the SQL dump to load, relative to `pg_dump_dir`.

    The SQL in that file will be executed, and then migration scripts will
    be run to bring it up to date.
    """
    with open(path.join(pg_dump_dir, filename)) as f:
        sql = f.read()
    with app.app_context():
        db.session.execute(sql)
        db.session.commit()
    upgrade(revision='heads')
Exemplo n.º 8
0
def load_dump(filename):
    """Load a database dump and upgrades it to the latest schema.

    `filename` is path to the SQL dump to load, relative to `pg_dump_dir`.

    The SQL in that file will be executed, and then migration scripts will
    be run to bring it up to date.
    """
    with open(path.join(pg_dump_dir, filename)) as f:
        sql = f.read()
    with app.app_context():
        db.session.execute(sql)
        db.session.commit()
    upgrade(revision='heads')
Exemplo n.º 9
0
def create_db():
    """Create and populate the initial database.

    The database connection must have been previously initialzed via
    `hil.model.init_db`.
    """
    with app.app_context():
        db.create_all()
        for head in _expected_heads():
            # Record the version of each branch. Each extension which uses the
            # database will have its own branch.
            db.session.execute(
                AlembicVersion.insert().values(version_num=head))
        get_network_allocator().populate()
        db.session.commit()
Exemplo n.º 10
0
def create_db():
    """Create and populate the initial database.

    The database connection must have been previously initialzed via
    `hil.model.init_db`.
    """
    with app.app_context():
        db.create_all()
        for head in _expected_heads():
            # Record the version of each branch. Each extension which uses the
            # database will have its own branch.
            db.session.execute(
                AlembicVersion.insert().values(version_num=head)
            )
        get_network_allocator().populate()
        db.session.commit()
Exemplo n.º 11
0
def initial_db(request, dbauth):
    fresh_database(request)
    with app.app_context():
        alice = dbauth.User(label='alice',
                            password='******',
                            is_admin=True)
        bob = dbauth.User(label='bob',
                          password='******',
                          is_admin=False)

        db.session.add(alice)
        db.session.add(bob)

        runway = model.Project('runway')
        runway.users.append(alice)
        db.session.add(runway)
        db.session.commit()
Exemplo n.º 12
0
def test_populate_dirty_db():
    """running the allocator's populate() on an existing db should be ok.

    This includes the case where modifications have been made to the vlans
    in the database.

    Note that we only check that this doesn't raise an exception.
    """
    # The fresh_database fixture will have created a clean database for us. We
    # just tweak it and then re-run create_db
    from hil.ext.network_allocators.vlan_pool import Vlan
    with app.app_context():
        # flag vlan 100 as in-use, just so the db isn't quite pristine.
        vlan100 = Vlan.query.filter_by(vlan_no=100).one()
        vlan100.available = False
        db.session.commit()
    # Okay, now try re-initializing:
    create_db()
Exemplo n.º 13
0
def test_populate_dirty_db():
    """running the allocator's populate() on an existing db should be ok.

    This includes the case where modifications have been made to the vlans
    in the database.

    Note that we only check that this doesn't raise an exception.
    """
    # The fresh_database fixture will have created a clean database for us. We
    # just tweak it and then re-run create_db
    from hil.ext.network_allocators.vlan_pool import Vlan
    with app.app_context():
        # flag vlan 100 as in-use, just so the db isn't quite pristine.
        vlan100 = Vlan.query.filter_by(vlan_no=100).one()
        vlan100.available = False
        db.session.commit()
    # Okay, now try re-initializing:
    create_db()
Exemplo n.º 14
0
def populate_server():
    """
    this function will populate some mock objects to faciliate testing of the
    client library
    """
    # create our initial admin user:
    with app.app_context():
        from hil.ext.auth.database import User
        db.session.add(User(username, password, is_admin=True))
        db.session.commit()

    # Adding nodes, node-01 - node-09
    url_node = 'http://127.0.0.1:8000/node/'
    ipmi = 'http://schema.massopencloud.org/haas/v0/obm/ipmi'

    for i in range(1, 10):
        obminfo = {
            "type": ipmi,
            "host": "10.10.0.0" + repr(i),
            "user": "******",
            "password": "******"
        }
        http_client.request('PUT',
                            url_node + 'node-0' + repr(i),
                            data=json.dumps({"obm": obminfo}))
        http_client.request('PUT',
                            url_node + 'node-0' + repr(i) + '/nic/eth0',
                            data=json.dumps(
                                {"macaddr": "aa:bb:cc:dd:ee:0" + repr(i)}))

    # Adding Projects proj-01 - proj-03
    for i in ["proj-01", "proj-02", "proj-03"]:
        http_client.request('PUT', 'http://127.0.0.1:8000/project/' + i)

    # Adding switches one for each driver
    url_switch = 'http://127.0.0.1:8000/switch/'
    api_name = 'http://schema.massopencloud.org/haas/v0/switches/'

    dell_param = {
        'type': api_name + 'powerconnect55xx',
        'hostname': 'dell-01',
        'username': '******',
        'password': '******'
    }
    nexus_param = {
        'type': api_name + 'nexus',
        'hostname': 'nexus-01',
        'username': '******',
        'password': '******',
        'dummy_vlan': '333'
    }
    mock_param = {
        'type': api_name + 'mock',
        'hostname': 'mockSwitch-01',
        'username': '******',
        'password': '******'
    }
    brocade_param = {
        'type': api_name + 'brocade',
        'hostname': 'brocade-01',
        'username': '******',
        'password': '******',
        'interface_type': 'TenGigabitEthernet'
    }

    http_client.request('PUT',
                        url_switch + 'dell-01',
                        data=json.dumps(dell_param))
    http_client.request('PUT',
                        url_switch + 'nexus-01',
                        data=json.dumps(nexus_param))
    http_client.request('PUT',
                        url_switch + 'mock-01',
                        data=json.dumps(mock_param))
    http_client.request('PUT',
                        url_switch + 'brocade-01',
                        data=json.dumps(brocade_param))

    # Adding ports to the mock switch, Connect nics to ports:
    for i in range(1, 8):
        http_client.request('PUT',
                            url_switch + 'mock-01/port/gi1/0/' + repr(i))
        http_client.request('POST',
                            url_switch + 'mock-01/port/gi1/0/' + repr(i) +
                            '/connect_nic',
                            data=json.dumps({
                                'node': 'node-0' + repr(i),
                                'nic': 'eth0'
                            }))

    # Adding port gi1/0/8 to switch mock-01 without connecting it to any node.
    http_client.request('PUT', url_switch + 'mock-01/port/gi1/0/8')

    # Adding Projects proj-01 - proj-03
    for i in ["proj-01", "proj-02", "proj-03"]:
        http_client.request('PUT', 'http://127.0.0.1:8000/project/' + i)

    # Allocating nodes to projects
    assign_nodes2project('proj-01', 'node-01')
    assign_nodes2project('proj-02', 'node-02', 'node-04')
    assign_nodes2project('proj-03', 'node-03', 'node-05')

    # Assigning networks to projects
    url_network = 'http://127.0.0.1:8000/network/'
    for i in ['net-01', 'net-02', 'net-03']:
        http_client.request('PUT',
                            url_network + i,
                            data=json.dumps({
                                "owner": "proj-01",
                                "access": "proj-01",
                                "net_id": ""
                            }))

    for i in ['net-04', 'net-05']:
        http_client.request('PUT',
                            url_network + i,
                            data=json.dumps({
                                "owner": "proj-02",
                                "access": "proj-02",
                                "net_id": ""
                            }))
Exemplo n.º 15
0
def populate_server():
    """
    this function will populate some mock objects to faciliate testing of the
    client library
    """
    # FIXME: we're not checking responses for any of these requests, so
    # failures here will show up as stranger errors later on.

    # create our initial admin user:
    with app.app_context():
        from hil.ext.auth.database import User
        db.session.add(User(username, password, is_admin=True))
        db.session.commit()

    # Adding nodes, node-01 - node-09
    url_node = 'http://127.0.0.1:8000/v0/node/'
    mock = 'http://schema.massopencloud.org/haas/v0/obm/mock'

    for i in range(1, 10):
        obminfo = {
                "type": mock, "host": "10.10.0.0"+repr(i),
                "user": "******", "password": "******"
                }
        http_client.request(
                'PUT',
                url_node + 'node-0'+repr(i), data=json.dumps({
                    "obm": obminfo,
                    "obmd": {
                        'uri': 'https://obmd.example.org/nodes/node-0' +
                               repr(i),
                        'admin_token': 'secret',
                    },
                })
        )
        http_client.request(
                'PUT',
                url_node + 'node-0' + repr(i) + '/nic/eth0', data=json.dumps(
                            {"macaddr": "aa:bb:cc:dd:ee:0" + repr(i)}
                            )
                     )

    # Adding Projects proj-01 - proj-03
    for i in ["proj-01", "proj-02", "proj-03"]:
        http_client.request('PUT', 'http://127.0.0.1:8000/v0/project/' + i)

    # Adding switches one for each driver
    url_switch = 'http://127.0.0.1:8000/v0/switch/'
    api_name = 'http://schema.massopencloud.org/haas/v0/switches/'

    dell_param = {
            'type': api_name + 'powerconnect55xx', 'hostname': 'dell-01',
            'username': '******', 'password': '******'
            }
    nexus_param = {
            'type': api_name + 'nexus', 'hostname': 'nexus-01',
            'username': '******', 'password': '******', 'dummy_vlan': '333'
            }
    mock_param = {
            'type': api_name + 'mock', 'hostname': 'mockSwitch-01',
            'username': '******', 'password': '******'
            }
    brocade_param = {
            'type': api_name + 'brocade', 'hostname': 'brocade-01',
            'username': '******', 'password': '******',
            'interface_type': 'TenGigabitEthernet'
            }

    http_client.request('PUT', url_switch + 'dell-01',
                        data=json.dumps(dell_param))
    http_client.request('PUT', url_switch + 'nexus-01',
                        data=json.dumps(nexus_param))
    http_client.request('PUT', url_switch + 'mock-01',
                        data=json.dumps(mock_param))
    http_client.request('PUT', url_switch + 'brocade-01',
                        data=json.dumps(brocade_param))

    # Adding ports to the mock switch, Connect nics to ports:
    for i in range(1, 8):
        http_client.request(
            'PUT',
            url_switch + 'mock-01/port/gi1/0/' + repr(i)
        )
        http_client.request(
                'POST',
                url_switch + 'mock-01/port/gi1/0/' + repr(i) + '/connect_nic',
                data=json.dumps(
                    {'node': 'node-0' + repr(i), 'nic': 'eth0'}
                    )
                )

    # Adding port gi1/0/8 to switch mock-01 without connecting it to any node.
    http_client.request('PUT', url_switch + 'mock-01/port/gi1/0/8')

    # Adding Projects proj-01 - proj-03
    for i in ["proj-01", "proj-02", "proj-03"]:
        http_client.request('PUT', 'http://127.0.0.1:8000/v0/project/' + i)

    # Allocating nodes to projects
    assign_nodes2project('proj-01', 'node-01')
    assign_nodes2project('proj-02', 'node-02', 'node-04')
    assign_nodes2project('proj-03', 'node-03', 'node-05')

    # Assigning networks to projects
    url_network = 'http://127.0.0.1:8000/v0/network/'
    for i in ['net-01', 'net-02', 'net-03']:
        http_client.request(
                'PUT',
                url_network + i,
                data=json.dumps(
                    {"owner": "proj-01", "access": "proj-01", "net_id": ""}
                    )
                )

    for i in ['net-04', 'net-05']:
        http_client.request(
                'PUT',
                url_network + i,
                data=json.dumps(
                    {"owner": "proj-02", "access": "proj-02", "net_id": ""}
                    )
                )
Exemplo n.º 16
0
 def run(self, obmd_base_url, obmd_admin_token):
     server.init()
     with app.app_context():
         info = db_extract_ipmi_info()
         obmd_upload_ipmi_info(obmd_base_url, obmd_admin_token, info)
         db_add_obmd_info(obmd_base_url, obmd_admin_token)
Exemplo n.º 17
0
def create_bigint_db():
    """Create database objects used in 'after-PK-bigint.sql'"""
    from hil.ext.switches.n3000 import DellN3000
    from hil.ext.switches.dell import PowerConnect55xx
    from hil.ext.switches.brocade import Brocade
    from hil.ext.switches.nexus import Nexus
    from hil.ext.switches.mock import MockSwitch
    from hil.ext.obm.ipmi import Ipmi
    from hil.ext.obm.mock import MockObm
    from hil.ext.auth.database import User
    from hil.ext.auth import database as dbauth
    with app.app_context():
        db.session.add(
            DellN3000(label='sw-n3000',
                      hostname='host',
                      username='******',
                      password='******',
                      dummy_vlan='5',
                      type=DellN3000.api_name))
        dell1 = PowerConnect55xx(label='sw-dell',
                                 hostname='host',
                                 username='******',
                                 password='******',
                                 type=PowerConnect55xx.api_name)
        db.session.add(dell1)
        db.session.add(
            Nexus(label='sw-nexus',
                  hostname='host',
                  username='******',
                  password='******',
                  dummy_vlan='5',
                  type=Nexus.api_name))
        db.session.add(
            Brocade(label='sw-brocade',
                    hostname='host',
                    username='******',
                    password='******',
                    interface_type='4',
                    type=Brocade.api_name))
        db.session.add(
            MockSwitch(label='sw0',
                       hostname='host',
                       username='******',
                       password='******',
                       type=MockSwitch.api_name))
        proj = model.Project(label='runway')
        db.session.add(proj)
        headnode1 = model.Headnode(label='runway_headnode',
                                   project=proj,
                                   base_img='image1')
        db.session.add(headnode1)
        db.session.add(model.Hnic(label='hnic1', headnode=headnode1))
        ipmi = Ipmi(host='host', user='******', password='******')
        db.session.add(ipmi)
        mock_obm = MockObm(host='host', user='******', password='******')
        db.session.add(mock_obm)
        node1 = model.Node(label='node-1', obm=ipmi)
        db.session.add(node1)

        db.session.add(
            model.Metadata(label='meta',
                           value="it is a true value",
                           node=node1))
        network1 = model.Network(owner=None,
                                 access=[proj],
                                 allocated=False,
                                 network_id="networking network",
                                 label='hil wireless')
        db.session.add(network1)
        nic1 = model.Nic(node=node1, label='pxe', mac_addr='ff:ff:ff:ff:ff:fe')
        model.Port(label='A fine port', switch=dell1)
        db.session.add(nic1)
        db.session.add(
            model.NetworkAttachment(nic=nic1, network_id=1,
                                    channel='vlan/100'))
        db.session.add(
            model.NetworkingAction(type='modify_port',
                                   nic=nic1,
                                   new_network=network1,
                                   channel='vlan/100'))
        jim = User(label='jim', password='******', is_admin=True)
        db.session.add(jim)

        local.auth = dbauth.User.query.filter_by(label='jim').one()

        dbauth.user_add_project('jim', 'runway')

        db.session.commit()

        # Original password is "pass"
        db.session.query(User).get(1).hashed_password = \
            ('$6$rounds=656000$iTyrApYTUhMx4b4g$YcaMExV'
             'YtS0ut2yXWrT64OggFpE4lLg12QsAuyMA3YKX6Czth'
             'XeisA47dJZW9GwU2q2CTIVrsbpxAVT64Pih2/')
        db.session.commit()