1 ##############################################################################
2 # Copyright (c) 2018 Sawyer Bergeron and others.
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
19 class BookingSerializerTestCase(TestCase):
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(
29 contact_email="mail@email.com",
34 image = Image.objects.create(
37 name="this is a test image",
43 role = OPNFVRole.objects.create(
48 HostConfiguration.objects.create(
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)
62 hostProfile = HostProfile.objects.create(
65 description='a test profile'
67 interfaceProfile = InterfaceProfile.objects.create(
72 diskProfile = DiskProfile.objects.create(
78 cpuProfile = CpuProfile.objects.create(
80 architecture="x86_64",
84 ramProfile = RamProfile.objects.create(
90 #create GenericResourceBundle
91 genericBundle = GenericResourceBundle.objects.create()
93 gres1 = GenericResource.objects.create(
95 name='generic resource ' + str(self.count)
98 gHost1 = GenericHost.objects.create(
103 gres2 = GenericResource.objects.create(
104 bundle=genericBundle,
105 name='generic resource ' + str(self.count)
108 gHost2 = GenericHost.objects.create(
112 user1 = User.objects.create(username='user1')
114 add_booking_perm = Permission.objects.get(codename='add_booking')
115 user1.user_permissions.add(add_booking_perm)
117 user1 = User.objects.get(pk=user1.id)
119 conf = ConfigBundle.objects.create(owner=user1, name="test conf")
120 self.makeHostConfigurations([gHost1, gHost2], conf)
122 #actual resource bundle
123 bundle = ResourceBundle.objects.create(
124 template = genericBundle
127 host1 = Host.objects.create(
136 host2 = Host.objects.create(
145 vlan1 = Vlan.objects.create(vlan_id=300, tagged=False)
146 vlan2 = Vlan.objects.create(vlan_id=300, tagged=False)
148 iface1 = Interface.objects.create(
149 mac_address='00:11:22:33:44:55',
150 bus_address='some bus address',
151 switch_name='switch1',
156 iface1.config = [vlan1]
158 iface2 = Interface.objects.create(
159 mac_address='00:11:22:33:44:56',
160 bus_address='some bus address',
161 switch_name='switch1',
166 iface2.config = [vlan2]
168 # finally, can create booking
169 self.booking = Booking.objects.create(
171 start = timezone.now(),
172 end = timezone.now() + timedelta(weeks=1),
178 serialized_booking = {}
181 host1['hostname'] = 'host1'
182 host1['image'] = {} # TODO: Images
183 host1['deploy_image'] = True
185 host2['hostname'] = 'host2'
186 host2['image'] = {} # TODO: Images
187 host2['deploy_image'] = True
189 serialized_booking['hosts'] = [host1, host2]
192 net['name'] = 'network_name'
195 netHost1['hostname'] = 'host1'
196 netHost1['tagged'] = False
197 netHost1['interface'] = 0
199 netHost2['hostname'] = 'host2'
200 netHost2['tagged'] = False
201 netHost2['interface'] = 0
202 net['hosts'] = [netHost1, netHost2]
204 serialized_booking['networking'] = [net]
205 serialized_booking['jumphost'] = 'host1'
207 self.serialized_booking = serialized_booking
209 def test_to_representation(self):
210 keys = ['hosts', 'networking', 'jumphost']
211 serialized_form = self.serializer.to_representation(self.booking)
213 self.assertEquals(serialized_form[key], self.serialized_booking)