Add test utils and tests for quick booking
[pharos-tools.git] / dashboard / src / dashboard / testing_utils.py
1 ##############################################################################
2 # Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 from django.contrib.auth.models import User
11
12 import json
13
14 from account.models import UserProfile, Lab, LabStatus, VlanManager, PublicNetwork
15 from resource_inventory.models import (
16     Host,
17     HostProfile,
18     InterfaceProfile,
19     DiskProfile,
20     CpuProfile,
21     Opsys,
22     Image,
23     Scenario,
24     Installer,
25     OPNFVRole,
26     RamProfile,
27 )
28
29
30 class BookingContextData(object):
31     def prepopulate(self, *args, **kwargs):
32         self.loginuser = instantiate_user(False, username=kwargs.get("login_username", "newtestuser"), password="testpassword")
33         instantiate_userprofile(self.loginuser, True)
34
35         lab_user = kwargs.get("lab_user", instantiate_user(True))
36         self.lab = instantiate_lab(lab_user)
37
38         self.host_profile = make_hostprofile_set(self.lab)
39         self.scenario = instantiate_scenario()
40         self.installer = instantiate_installer([self.scenario])
41         os = instantiate_os([self.installer])
42         self.image = instantiate_image(self.lab, 1, self.loginuser, os, self.host_profile)
43         self.host = instantiate_host(self.host_profile, self.lab)
44         self.role = instantiate_opnfvrole()
45         self.pubnet = instantiate_publicnet(10, self.lab)
46
47
48 def instantiate_user(is_superuser,
49                      username="testuser",
50                      password="testpassword",
51                      email="default_email@user.com"
52                      ):
53     user = User.objects.create_user(username=username, email=email, password=password)
54     user.is_superuser = is_superuser
55
56     user.save()
57
58     return user
59
60
61 def instantiate_userprofile(user=None, can_book_multiple=False):
62     if not user:
63         user = instantiate_user(True, 'test_user', 'test_pass', 'test_user@test_site.org')
64     userprofile = UserProfile()
65     userprofile.user = user
66     userprofile.booking_privledge = can_book_multiple
67
68     userprofile.save()
69
70     return user
71
72
73 def instantiate_vlanmanager(vlans=None,
74                             block_size=20,
75                             allow_overlapping=False,
76                             reserved_vlans=None
77                             ):
78     vlanmanager = VlanManager()
79     if not vlans:
80         vlans = []
81         for vlan in range(0, 4095):
82             vlans.append(vlan % 2)
83     vlanmanager.vlans = json.dumps(vlans)
84     if not reserved_vlans:
85         reserved_vlans = []
86         for vlan in range(0, 4095):
87             reserved_vlans.append(0)
88     vlanmanager.reserved_vlans = json.dumps(vlans)
89     vlanmanager.block_size = block_size
90     vlanmanager.allow_overlapping = allow_overlapping
91
92     vlanmanager.save()
93
94     return vlanmanager
95
96
97 def instantiate_lab(user=None,
98                     name="Test Lab Instance",
99                     status=LabStatus.UP,
100                     vlan_manager=None
101                     ):
102     if not vlan_manager:
103         vlan_manager = instantiate_vlanmanager()
104
105     if not user:
106         user = instantiate_user(True, 'test_user', 'test_pass', 'test_user@test_site.org')
107
108     lab = Lab()
109     lab.lab_user = user
110     lab.name = name
111     lab.contact_email = 'test_lab@test_site.org'
112     lab.contact_phone = '603 123 4567'
113     lab.status = status
114     lab.vlan_manager = vlan_manager
115     lab.description = 'test lab instantiation'
116     lab.api_token = '12345678'
117
118     lab.save()
119
120     return lab
121
122
123 """
124 resource_inventory instantiation section for permenant resources
125 """
126
127
128 def make_hostprofile_set(lab, name="test_hostprofile"):
129     hostprof = instantiate_hostprofile(lab, name=name)
130     instantiate_diskprofile(hostprof, 500, name=name)
131     instantiate_cpuprofile(hostprof)
132     instantiate_interfaceprofile(hostprof, name=name)
133     instantiate_ramprofile(hostprof)
134
135     return hostprof
136
137
138 def instantiate_hostprofile(lab,
139                             host_type=0,
140                             name="test hostprofile instance"
141                             ):
142     hostprof = HostProfile()
143     hostprof.host_type = host_type
144     hostprof.name = name
145     hostprof.description = 'test hostprofile instance'
146     hostprof.save()
147     hostprof.labs.add(lab)
148
149     hostprof.save()
150
151     return hostprof
152
153
154 def instantiate_ramprofile(host,
155                            channels=4,
156                            amount=256):
157     ramprof = RamProfile()
158     ramprof.host = host
159     ramprof.amount = amount
160     ramprof.channels = channels
161     ramprof.save()
162
163     return ramprof
164
165
166 def instantiate_diskprofile(hostprofile,
167                             size=0,
168                             media_type="SSD",
169                             name="test diskprofile",
170                             rotation=0,
171                             interface="sata"):
172
173     diskprof = DiskProfile()
174     diskprof.name = name
175     diskprof.size = size
176     diskprof.media_type = media_type
177     diskprof.host = hostprofile
178     diskprof.rotation = rotation
179     diskprof.interface = interface
180
181     diskprof.save()
182
183     return diskprof
184
185
186 def instantiate_cpuprofile(hostprofile,
187                            cores=4,
188                            architecture="x86_64",
189                            cpus=4,
190                            ):
191     cpuprof = CpuProfile()
192     cpuprof.cores = cores
193     cpuprof.architecture = architecture
194     cpuprof.cpus = cpus
195     cpuprof.host = hostprofile
196     cpuprof.cflags = ''
197
198     cpuprof.save()
199
200     return cpuprof
201
202
203 def instantiate_interfaceprofile(hostprofile,
204                                  speed=1000,
205                                  name="test interface profile",
206                                  nic_type="pcie"
207                                  ):
208     intprof = InterfaceProfile()
209     intprof.host = hostprofile
210     intprof.name = name
211     intprof.speed = speed
212     intprof.nic_type = nic_type
213
214     intprof.save()
215
216     return intprof
217
218
219 def instantiate_image(lab,
220                       lab_id,
221                       owner,
222                       os,
223                       host_profile,
224                       public=True,
225                       name="default image",
226                       description="default image"
227                       ):
228     image = Image()
229     image.from_lab = lab
230     image.lab_id = lab_id
231     image.os = os
232     image.host_type = host_profile
233     image.public = public
234     image.name = name
235     image.description = description
236
237     image.save()
238
239     return image
240
241
242 def instantiate_scenario(name="test scenario"):
243     scenario = Scenario()
244     scenario.name = name
245     scenario.save()
246     return scenario
247
248
249 def instantiate_installer(supported_scenarios,
250                           name="test installer"
251                           ):
252     installer = Installer()
253     installer.name = name
254     installer.save()
255     for scenario in supported_scenarios:
256         installer.sup_scenarios.add(scenario)
257
258     installer.save()
259     return installer
260
261
262 def instantiate_os(supported_installers,
263                    name="test operating system",
264                    ):
265     os = Opsys()
266     os.name = name
267     os.save()
268     for installer in supported_installers:
269         os.sup_installers.add(installer)
270     os.save()
271     return os
272
273
274 def instantiate_host(host_profile,
275                      lab,
276                      labid="test_host",
277                      name="test_host",
278                      booked=False,
279                      working=True,
280                      config=None,
281                      template=None,
282                      bundle=None,
283                      model="Model 1",
284                      vendor="ACME"):
285     host = Host()
286     host.lab = lab
287     host.profile = host_profile
288     host.name = name
289     host.booked = booked
290     host.working = working
291     host.config = config
292     host.template = template
293     host.bundle = bundle
294     host.model = model
295     host.vendor = vendor
296
297     host.save()
298
299     return host
300
301
302 def instantiate_opnfvrole(name="Jumphost",
303                           description="test opnfvrole"):
304     role = OPNFVRole()
305     role.name = name
306     role.description = description
307     role.save()
308
309     return role
310
311
312 def instantiate_publicnet(vlan,
313                           lab,
314                           in_use=False,
315                           cidr="0.0.0.0/0",
316                           gateway="0.0.0.0"):
317     pubnet = PublicNetwork()
318     pubnet.lab = lab
319     pubnet.vlan = vlan
320     pubnet.cidr = cidr
321     pubnet.gateway = gateway
322     pubnet.save()
323
324     return pubnet