def new_nic(name): """Create a new nic named ``name``, and an associated Node + Obm. The new nic is attached to a new node each time, and the node is added to the project named 'anvil-nextgen-####' """ from hil.ext.obm.mock import MockObm unique_id = str(uuid.uuid4()) project = model.Project('anvil-nextgen-' + unique_id) node = model.Node( label=str(uuid.uuid4()), obm=MockObm(type="http://schema.massopencloud.org/haas/v0/obm/mock", host="ipmihost", user="******", password="******")) if node.project is None: project.nodes.append(node) return model.Nic(node, name, '00:11:22:33:44:55')
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: - hil.ext.switches.mock - hil.ext.obm.mock """ for required_extension in 'hil.ext.switches.mock', 'hil.ext.obm.mock': assert required_extension in sys.modules, \ "The 'initial_db' fixture requires the extension %r" % \ required_extension from hil.ext.switches.mock import MockSwitch from hil.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, obmd_uri='http://obmd.example.com/nodes/' + node_dict['label'], obmd_admin_token='secret', ) 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, obmd_uri='http://obmd.example.com/nodes/no_nic_node', obmd_admin_token='secret', )) db.session.commit()
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()