From a9dfa98ad457863b1a0cdaaef4fe57797f92660f Mon Sep 17 00:00:00 2001 From: Sawyer Bergeron Date: Tue, 5 Mar 2019 15:28:51 -0500 Subject: Add API tests Change-Id: Ic26d0b6de63405d239a9260b862158962c3140ac Signed-off-by: Sawyer Bergeron --- dashboard/src/dashboard/testing_utils.py | 210 +++++++++++++++++++++++++++++-- 1 file changed, 200 insertions(+), 10 deletions(-) (limited to 'dashboard/src/dashboard/testing_utils.py') diff --git a/dashboard/src/dashboard/testing_utils.py b/dashboard/src/dashboard/testing_utils.py index e98b5e6..558031d 100644 --- a/dashboard/src/dashboard/testing_utils.py +++ b/dashboard/src/dashboard/testing_utils.py @@ -8,9 +8,15 @@ ############################################################################## from django.contrib.auth.models import User +from django.core.files.base import ContentFile import json +import re +from dashboard.exceptions import ( + InvalidHostnameException +) +from booking.models import Booking from account.models import UserProfile, Lab, LabStatus, VlanManager, PublicNetwork from resource_inventory.models import ( Host, @@ -24,13 +30,23 @@ from resource_inventory.models import ( Installer, OPNFVRole, RamProfile, + Network, + Vlan, + GenericResourceBundle, + GenericResource, + GenericHost, + ConfigBundle, + GenericInterface, + HostConfiguration, + OPNFVConfig, ) +from resource_inventory.resource_manager import ResourceManager class BookingContextData(object): def prepopulate(self, *args, **kwargs): self.loginuser = instantiate_user(False, username=kwargs.get("login_username", "newtestuser"), password="testpassword") - instantiate_userprofile(self.loginuser, True) + instantiate_userprofile(self.loginuser) lab_user = kwargs.get("lab_user", instantiate_user(True)) self.lab = instantiate_lab(lab_user) @@ -45,6 +61,179 @@ class BookingContextData(object): self.pubnet = instantiate_publicnet(10, self.lab) +""" +Info for instantiate_booking() function: +[topology] argument structure: + the [topology] argument should describe the structure of the pod + the top level should be a dictionary, with each key being a hostname + each value in the top level should be a dictionary with two keys: + "type" should map to a host profile instance + "nets" should map to a list of interfaces each with a list of + dictionaries each defining a network in the format + { "name": "netname", "tagged": True|False, "public": True|False } + each network is defined if a matching name is not found + + sample argument structure: + topology={ + "host1": { + "type": instanceOf HostProfile, + "role": instanceOf OPNFVRole + "image": instanceOf Image + "nets": [ + 0: [ + 0: { "name": "public", "tagged": True, "public": True }, + 1: { "name": "private", "tagged": False, "public": False }, + ] + 1: [] + ] + } + } +""" + + +def instantiate_booking(owner, + start, + end, + booking_identifier, + lab=Lab.objects.first(), + purpose="purposetext", + project="projecttext", + collaborators=[], + topology={}, + installer=None, + scenario=None): + (grb, host_set) = instantiate_grb(topology, owner, lab, booking_identifier) + cb = instantiate_cb(grb, owner, booking_identifier, topology, host_set, installer, scenario) + + resource = ResourceManager.getInstance().convertResourceBundle(grb, lab, cb) + + booking = Booking() + + booking.resource = resource + if not resource: + raise Exception("Resource not created") + booking.config_bundle = cb + booking.start = start + booking.end = end + booking.owner = owner + booking.purpose = purpose + booking.project = project + booking.lab = lab + booking.save() + + return booking + + +def instantiate_cb(grb, + owner, + booking_identifier, + topology={}, + host_set={}, + installer=None, + scenario=None): + cb = ConfigBundle() + cb.owner = owner + cb.name = str(booking_identifier) + "_cb" + cb.description = "cb generated by instantiate_cb() method" + cb.save() + + opnfvconfig = OPNFVConfig() + opnfvconfig.installer = installer + opnfvconfig.scenario = scenario + opnfvconfig.bundle = cb + opnfvconfig.save() + + # generate host configurations based on topology and host set + for hostname, host_info in topology.items(): + hconf = HostConfiguration() + hconf.bundle = cb + hconf.host = host_set[hostname] + hconf.image = host_info["image"] + hconf.opnfvRole = host_info["role"] + hconf.save() + return cb + + +def instantiate_grb(topology, + owner, + lab, + booking_identifier): + + grb = GenericResourceBundle(owner=owner, lab=lab) + grb.name = str(booking_identifier) + "_grb" + grb.description = "grb generated by instantiate_grb() method" + grb.save() + + networks = {} + host_set = {} + + for hostname in topology.keys(): + info = topology[hostname] + host_profile = info["type"] + + # need to construct host from hostname and type + ghost = instantiate_ghost(grb, host_profile, hostname) + host_set[hostname] = ghost + + ghost.save() + + # set up networks + nets = info["nets"] + for interface_index, interface_profile in enumerate(host_profile.interfaceprofile.all()): + generic_interface = GenericInterface() + generic_interface.host = ghost + generic_interface.profile = interface_profile + generic_interface.save() + + netconfig = nets[interface_index] + for network_index, network_info in enumerate(netconfig): + network_name = network_info["name"] + network = None + if network_name in networks: + network = networks[network_name] + else: + network = Network() + network.name = network_name + network.vlan_id = lab.vlan_manager.get_vlan() + network.save() + networks[network_name] = network + if network_info["public"]: + public_net = lab.vlan_manager.get_public_vlan() + if not public_net: + raise Exception("No more public networks available") + lab.vlan_manager.reserve_public_vlan(public_net.vlan) + network.vlan_id = public_net.vlan + else: + private_net = lab.vlan_manager.get_vlan() + if not private_net: + raise Exception("No more generic vlans are available") + lab.vlan_manager.reserve_vlans([private_net]) + network.vlan_id = private_net + + vlan = Vlan() + vlan.vlan_id = network.vlan_id + vlan.public = network_info["public"] + vlan.tagged = network_info["tagged"] + vlan.save() + generic_interface.vlans.add(vlan) + + return (grb, host_set) + + +def instantiate_ghost(grb, host_profile, hostname): + if not re.match(r"(?=^.{1,253}$)(^([A-Za-z0-9-_]{1,62}\.)*[A-Za-z0-9-_]{1,63})$", hostname): + raise InvalidHostnameException("Hostname must comply to RFC 952 and all extensions to it until this point") + gresource = GenericResource(bundle=grb, name=hostname) + gresource.save() + + ghost = GenericHost() + ghost.resource = gresource + ghost.profile = host_profile + ghost.save() + + return ghost + + def instantiate_user(is_superuser, username="testuser", password="testpassword", @@ -58,16 +247,17 @@ def instantiate_user(is_superuser, return user -def instantiate_userprofile(user=None, can_book_multiple=False): - if not user: - user = instantiate_user(True, 'test_user', 'test_pass', 'test_user@test_site.org') - userprofile = UserProfile() - userprofile.user = user - userprofile.booking_privledge = can_book_multiple +def instantiate_userprofile(user, email_addr="email@email.com", company="company", full_name="John Doe", booking_privledge=True, ssh_file=None): + up = UserProfile() + up.email_address = email_addr + up.company = company + up.full_name = full_name + up.booking_privledge = booking_privledge + up.user = user + up.save() + up.ssh_public_key.save("user_ssh_key", ssh_file if ssh_file else ContentFile("public key content string")) - userprofile.save() - - return user + return up def instantiate_vlanmanager(vlans=None, -- cgit 1.2.3-korg