summaryrefslogtreecommitdiffstats
path: root/src/api/tests/test_serializers.py
blob: c49010cde66373d7dc782d68e721a3df500e13e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
##############################################################################
# Copyright (c) 2018 Sawyer Bergeron and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
from django.test import TestCase
from booking.models import Booking
from resource_inventory.models import *
from account.models import Lab
from api.serializers.booking_serializer import *
from datetime import timedelta
from django.utils import timezone
from django.contrib.auth.models import Permission, User


class BookingSerializerTestCase(TestCase):

    count = 0

    def makeHostConfigurations(self, hosts, config):
        lab_user = User.objects.create(username="asfasdfasdf")
        owner = User.objects.create(username="asfasdfasdffffff")
        lab = Lab.objects.create(
                lab_user=lab_user,
                name="TestLab123123",
                contact_email="mail@email.com",
                contact_phone=""
                )
        jumphost=True
        for host in hosts:
            image = Image.objects.create(
                    lab_id=12,
                    from_lab=lab,
                    name="this is a test image",
                    owner=owner
                    )
            name = "jumphost"
            if not jumphost:
                name = "compute"
            role = OPNFVRole.objects.create(
                    name=name,
                    description="stuff"
                    )

            HostConfiguration.objects.create(
                    host=host,
                    image=image,
                    bundle=config,
                    opnfvRole=role
                    )
            jumphost=False


    def setUp(self):
        self.serializer = BookingField()
        lab_user = User.objects.create(username="lab user")
        lab = Lab.objects.create(name="test lab", lab_user=lab_user)
        # create hostProfile
        hostProfile = HostProfile.objects.create(
            host_type=0,
            name='Test profile',
            description='a test profile'
        )
        interfaceProfile = InterfaceProfile.objects.create(
            speed=1000,
            name='eno3',
            host=hostProfile
        )
        diskProfile = DiskProfile.objects.create(
            size=1000,
            media_type="SSD",
            name='/dev/sda',
            host=hostProfile
        )
        cpuProfile = CpuProfile.objects.create(
            cores=96,
            architecture="x86_64",
            cpus=2,
            host=hostProfile
        )
        ramProfile = RamProfile.objects.create(
            amount=256,
            channels=4,
            host=hostProfile
        )

        #create GenericResourceBundle
        genericBundle = GenericResourceBundle.objects.create()

        gres1 = GenericResource.objects.create(
            bundle=genericBundle,
            name='generic resource ' + str(self.count)
        )
        self.count += 1
        gHost1 = GenericHost.objects.create(
            resource=gres1,
            profile=hostProfile
        )

        gres2 = GenericResource.objects.create(
            bundle=genericBundle,
            name='generic resource ' + str(self.count)
        )
        self.count += 1
        gHost2 = GenericHost.objects.create(
            resource=gres2,
            profile=hostProfile
        )
        user1 = User.objects.create(username='user1')

        add_booking_perm = Permission.objects.get(codename='add_booking')
        user1.user_permissions.add(add_booking_perm)

        user1 = User.objects.get(pk=user1.id)

        conf = ConfigBundle.objects.create(owner=user1, name="test conf")
        self.makeHostConfigurations([gHost1, gHost2], conf)

        #actual resource bundle
        bundle = ResourceBundle.objects.create(
            template = genericBundle
        )

        host1 = Host.objects.create(
            template=gHost1,
            booked=True,
            name='host1',
            bundle=bundle,
            profile=hostProfile,
            lab=lab
        )

        host2 = Host.objects.create(
            template=gHost2,
            booked=True,
            name='host2',
            bundle=bundle,
            profile=hostProfile,
            lab=lab
        )

        vlan1 = Vlan.objects.create(vlan_id=300, tagged=False)
        vlan2 = Vlan.objects.create(vlan_id=300, tagged=False)

        iface1 = Interface.objects.create(
            mac_address='00:11:22:33:44:55',
            bus_address='some bus address',
            switch_name='switch1',
            port_name='port10',
            host=host1
        )

        iface1.config = [vlan1]

        iface2 = Interface.objects.create(
            mac_address='00:11:22:33:44:56',
            bus_address='some bus address',
            switch_name='switch1',
            port_name='port12',
            host=host2
        )

        iface2.config = [vlan2]

        # finally, can create booking
        self.booking = Booking.objects.create(
            owner=user1,
            start = timezone.now(),
            end = timezone.now() + timedelta(weeks=1),
            purpose='Testing',
            resource=bundle,
            config_bundle=conf
        )

        serialized_booking = {}

        host1 = {}
        host1['hostname'] = 'host1'
        host1['image'] = {}  # TODO: Images
        host1['deploy_image'] = True
        host2 = {}
        host2['hostname'] = 'host2'
        host2['image'] = {}  # TODO: Images
        host2['deploy_image'] = True

        serialized_booking['hosts'] = [host1, host2]

        net = {}
        net['name'] = 'network_name'
        net['vlan_id'] = 300
        netHost1 = {}
        netHost1['hostname'] = 'host1'
        netHost1['tagged'] = False
        netHost1['interface'] = 0
        netHost2 = {}
        netHost2['hostname'] = 'host2'
        netHost2['tagged'] = False
        netHost2['interface'] = 0
        net['hosts'] = [netHost1, netHost2]

        serialized_booking['networking'] = [net]
        serialized_booking['jumphost'] = 'host1'

        self.serialized_booking = serialized_booking

    def test_to_representation(self):
        keys = ['hosts', 'networking', 'jumphost']
        serialized_form = self.serializer.to_representation(self.booking)
        for key in keys:
            self.assertEquals(serialized_form[key], self.serialized_booking)