Lab as a Service 2.0
[laas.git] / src / api / tests / test_serializers.py
1 ##############################################################################
2 # Copyright (c) 2018 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 from django.test import TestCase
10 from booking.models import Booking
11 from resource_inventory.models import *
12 from account.models import Lab
13 from api.serializers.booking_serializer import *
14 from datetime import timedelta
15 from django.utils import timezone
16 from django.contrib.auth.models import Permission, User
17
18
19 class BookingSerializerTestCase(TestCase):
20
21     count = 0
22
23     def makeHostConfigurations(self, hosts, config):
24         lab_user = User.objects.create(username="asfasdfasdf")
25         owner = User.objects.create(username="asfasdfasdffffff")
26         lab = Lab.objects.create(
27                 lab_user=lab_user,
28                 name="TestLab123123",
29                 contact_email="mail@email.com",
30                 contact_phone=""
31                 )
32         jumphost=True
33         for host in hosts:
34             image = Image.objects.create(
35                     lab_id=12,
36                     from_lab=lab,
37                     name="this is a test image",
38                     owner=owner
39                     )
40             name = "jumphost"
41             if not jumphost:
42                 name = "compute"
43             role = OPNFVRole.objects.create(
44                     name=name,
45                     description="stuff"
46                     )
47
48             HostConfiguration.objects.create(
49                     host=host,
50                     image=image,
51                     bundle=config,
52                     opnfvRole=role
53                     )
54             jumphost=False
55
56
57     def setUp(self):
58         self.serializer = BookingField()
59         lab_user = User.objects.create(username="lab user")
60         lab = Lab.objects.create(name="test lab", lab_user=lab_user)
61         # create hostProfile
62         hostProfile = HostProfile.objects.create(
63             host_type=0,
64             name='Test profile',
65             description='a test profile'
66         )
67         interfaceProfile = InterfaceProfile.objects.create(
68             speed=1000,
69             name='eno3',
70             host=hostProfile
71         )
72         diskProfile = DiskProfile.objects.create(
73             size=1000,
74             media_type="SSD",
75             name='/dev/sda',
76             host=hostProfile
77         )
78         cpuProfile = CpuProfile.objects.create(
79             cores=96,
80             architecture="x86_64",
81             cpus=2,
82             host=hostProfile
83         )
84         ramProfile = RamProfile.objects.create(
85             amount=256,
86             channels=4,
87             host=hostProfile
88         )
89
90         #create GenericResourceBundle
91         genericBundle = GenericResourceBundle.objects.create()
92
93         gres1 = GenericResource.objects.create(
94             bundle=genericBundle,
95             name='generic resource ' + str(self.count)
96         )
97         self.count += 1
98         gHost1 = GenericHost.objects.create(
99             resource=gres1,
100             profile=hostProfile
101         )
102
103         gres2 = GenericResource.objects.create(
104             bundle=genericBundle,
105             name='generic resource ' + str(self.count)
106         )
107         self.count += 1
108         gHost2 = GenericHost.objects.create(
109             resource=gres2,
110             profile=hostProfile
111         )
112         user1 = User.objects.create(username='user1')
113
114         add_booking_perm = Permission.objects.get(codename='add_booking')
115         user1.user_permissions.add(add_booking_perm)
116
117         user1 = User.objects.get(pk=user1.id)
118
119         conf = ConfigBundle.objects.create(owner=user1, name="test conf")
120         self.makeHostConfigurations([gHost1, gHost2], conf)
121
122         #actual resource bundle
123         bundle = ResourceBundle.objects.create(
124             template = genericBundle
125         )
126
127         host1 = Host.objects.create(
128             template=gHost1,
129             booked=True,
130             name='host1',
131             bundle=bundle,
132             profile=hostProfile,
133             lab=lab
134         )
135
136         host2 = Host.objects.create(
137             template=gHost2,
138             booked=True,
139             name='host2',
140             bundle=bundle,
141             profile=hostProfile,
142             lab=lab
143         )
144
145         vlan1 = Vlan.objects.create(vlan_id=300, tagged=False)
146         vlan2 = Vlan.objects.create(vlan_id=300, tagged=False)
147
148         iface1 = Interface.objects.create(
149             mac_address='00:11:22:33:44:55',
150             bus_address='some bus address',
151             switch_name='switch1',
152             port_name='port10',
153             host=host1
154         )
155
156         iface1.config = [vlan1]
157
158         iface2 = Interface.objects.create(
159             mac_address='00:11:22:33:44:56',
160             bus_address='some bus address',
161             switch_name='switch1',
162             port_name='port12',
163             host=host2
164         )
165
166         iface2.config = [vlan2]
167
168         # finally, can create booking
169         self.booking = Booking.objects.create(
170             owner=user1,
171             start = timezone.now(),
172             end = timezone.now() + timedelta(weeks=1),
173             purpose='Testing',
174             resource=bundle,
175             config_bundle=conf
176         )
177
178         serialized_booking = {}
179
180         host1 = {}
181         host1['hostname'] = 'host1'
182         host1['image'] = {}  # TODO: Images
183         host1['deploy_image'] = True
184         host2 = {}
185         host2['hostname'] = 'host2'
186         host2['image'] = {}  # TODO: Images
187         host2['deploy_image'] = True
188
189         serialized_booking['hosts'] = [host1, host2]
190
191         net = {}
192         net['name'] = 'network_name'
193         net['vlan_id'] = 300
194         netHost1 = {}
195         netHost1['hostname'] = 'host1'
196         netHost1['tagged'] = False
197         netHost1['interface'] = 0
198         netHost2 = {}
199         netHost2['hostname'] = 'host2'
200         netHost2['tagged'] = False
201         netHost2['interface'] = 0
202         net['hosts'] = [netHost1, netHost2]
203
204         serialized_booking['networking'] = [net]
205         serialized_booking['jumphost'] = 'host1'
206
207         self.serialized_booking = serialized_booking
208
209     def test_to_representation(self):
210         keys = ['hosts', 'networking', 'jumphost']
211         serialized_form = self.serializer.to_representation(self.booking)
212         for key in keys:
213             self.assertEquals(serialized_form[key], self.serialized_booking)