Lab as a Service 2.0
[laas.git] / src / workflow / tests / test_steps.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 from django.test import TestCase, client
10 from dashboard.populate_db import Populator
11 from workflow.tests import constants
12 from workflow.workflow_factory import WorkflowFactory
13 from workflow.models import Repository
14 from workflow.resource_bundle_workflow import *
15 from workflow.sw_bundle_workflow import *
16 from workflow.booking_workflow import *
17 from django.http import QueryDict, HttpRequest
18 from django.contrib.auth.models import User
19 from django.core.management import call_command
20 from resource_inventory.models import *
21
22
23 class BaseStepTestCase(TestCase):
24
25     @classmethod
26     def setUpTestData(cls):
27         Populator().populate()
28
29     def makeRepo(self):
30         repo = Repository()
31         repo.el[repo.SESSION_USER] = User.objects.filter(username="user 1").first()
32         return repo
33
34     def step_test(self, step_type, data):
35         step = WorkflowFactory().make_step(step_type, self.makeRepo())
36         formData = QueryDict(mutable=True)
37         formData.update(data)
38         request = HttpRequest()
39         request.POST = formData
40         response = step.post_render(request)
41         context = step.get_context()
42         return response, context
43
44
45 class BookingResourceSelectTestCase(BaseStepTestCase):
46
47     def test_step_with_good_data(self):
48         grb_model = GenericResourceBundle.objects.filter(owner__username="user 1").first()
49         grb = [{"small_name": grb_model.name, "expanded_name": "user 1", "id": grb_model.id, "string": ""}]
50         grb = str(grb).replace("'", '"')
51         data = {"generic_resource_bundle": grb }
52         response, context = self.step_test(Booking_Resource_Select, data)
53         self.assertTrue(True)
54
55     def test_step_with_bad_data(self):  # TODO
56         data = {}
57         response, context = self.step_test(Booking_Resource_Select, data)
58
59     def test_step_with_empty_data(self):
60         data = {}
61         response, context = self.step_test(SWConfig_Select, data)
62
63 class SoftwareConfigSelectTestCase(BaseStepTestCase):
64
65     def test_step_with_good_data(self):
66         config_model = ConfigBundle.objects.filter(owner__username="user 1").first()
67         config = [{"expanded_name":"user 1", "small_name":config_model.name, "id":config_model.id, "string":""}]
68         config = str(config).replace("'", '"')
69         data = {"software_bundle": config}
70         response, context = self.step_test(SWConfig_Select, data)
71
72     def test_step_with_bad_data(self):  # TODO
73         data = {}
74         response, context = self.step_test(SWConfig_Select, data)
75
76     def test_step_with_empty_data(self):
77         data = {}
78         response, context = self.step_test(SWConfig_Select, data)
79
80 class BookingMetaTestCase(BaseStepTestCase):
81
82     def test_step_with_good_data(self):
83         data = {"length": 7, "project": "LaaS", "purpose": "testing"}
84         user2 = User.objects.get(username="user 2")
85         john = User.objects.get(username="johnsmith")
86         users = [
87                 {"expanded_name":"", "id":user2.id, "small_name":user2.username, "string":user2.email},
88                 {"expanded_name":"", "id":john.id, "small_name":john.username, "string":john.email}
89                 ]
90         users = str(users).replace("'", '"')
91         data['users'] = users
92         response, context = self.step_test(Booking_Meta, data)
93
94     def test_step_with_bad_data(self):  # TODO
95         data = {}
96         response, context = self.step_test(Booking_Meta, data)
97
98     def test_step_with_empty_data(self):
99         data = {}
100         response, context = self.step_test(Booking_Meta, data)
101
102
103 class DefineHardwareTestCase(BaseStepTestCase):
104
105     def test_step_with_good_data(self):
106         hosts = {"host_4": 1, "host_1": 1}
107         labs = {"lab_1":"true"}
108         data = {"hosts": hosts, "labs": labs}
109         response, context = self.step_test(Define_Hardware, data)
110
111     def test_step_with_bad_data(self):  # TODO
112         data = {}
113         response, context = self.step_test(Define_Hardware, data)
114
115     def test_step_with_empty_data(self):
116         data = {}
117         response, context = self.step_test(Define_Hardware, data)
118
119
120 class HostMetaInfoTestCase(BaseStepTestCase):
121
122     def makeRepo(self):
123         """
124         override to provide step with needed host info
125         """
126         repo = super(HostMetaInfoTestCase, self).makeRepo()
127         # get models
128         models = {}
129         models['bundle'] = GenericResourceBundle()
130         # make generic hosts
131         gres1 = GenericResource(bundle=models['bundle'])
132         prof1 = HostProfile.objects.get(name="Test profile 0")
133         ghost1 = GenericHost(profile=prof1, resource=gres1)
134
135         gres2 = GenericResource(bundle=models['bundle'])
136         prof2 = HostProfile.objects.get(name="Test profile 3")
137         ghost2 = GenericHost(profile=prof2, resource=gres2)
138         models['hosts'] = [ghost1, ghost2]
139         repo.el[repo.GRESOURCE_BUNDLE_MODELS] = models
140         return repo
141
142     def test_step_with_good_data(self):
143         data = {"form-INITIAL_FORMS": 2, "form-MAX_NUM_FORMS": 1000}
144         data["form-MIN_NUM_FORMS"] = 0
145         data["form-TOTAL_FORMS"] = 2
146         data['form-0-host_name'] = "first host"
147         data['form-1-host_name'] = "second host"
148         response, context = self.step_test(Host_Meta_Info, data)
149
150     def test_step_with_bad_data(self):  # TODO
151         data = {"form-INITIAL_FORMS": 0, "form-MAX_NUM_FORMS": 1000}
152         data["form-MIN_NUM_FORMS"] = 0
153         data["form-TOTAL_FORMS"] = 0
154         response, context = self.step_test(Host_Meta_Info, data)
155
156     def test_step_with_empty_data(self):
157         data = {"form-INITIAL_FORMS": 0, "form-MAX_NUM_FORMS": 1000}
158         data["form-MIN_NUM_FORMS"] = 0
159         data["form-TOTAL_FORMS"] = 0
160         response, context = self.step_test(Host_Meta_Info, data)
161
162
163 class DefineNetsTestCase(BaseStepTestCase):
164
165     def test_step_with_good_data(self):
166         xml = constants.POD_XML
167         data = {"xml": xml}
168         response, context = self.step_test(Define_Nets, data)
169
170     def test_step_with_bad_data(self):  # TODO
171         data = {}
172         response, context = self.step_test(Define_Nets, data)
173
174     def test_step_with_empty_data(self):
175         data = {}
176         response, context = self.step_test(Define_Nets, data)
177
178
179 class ResourceMetaInfoTestCase(BaseStepTestCase):
180
181     def test_step_with_good_data(self):
182         data = {"bundle_description": "description", "bundle_name": "my testing bundle"}
183         response, context = self.step_test(Resource_Meta_Info, data)
184
185     def test_step_with_bad_data(self):  # TODO
186         data = {}
187         response, context = self.step_test(Resource_Meta_Info, data)
188
189     def test_step_with_empty_data(self):
190         data = {}
191         response, context = self.step_test(Resource_Meta_Info, data)
192
193
194 class SWConfResourceSelectTestCase(BaseStepTestCase):
195
196     def test_step_with_good_data(self):
197         grb_model = GenericResourceBundle.objects.filter(owner__username="user 1").first()
198         grb = [{"small_name": grb_model.name, "expanded_name": "user 1", "id": grb_model.id, "string": ""}]
199         grb = str(grb).replace("'", '"')
200         data = {"generic_resource_bundle": grb }
201         response, context = self.step_test(SWConf_Resource_Select, data)
202
203     def test_step_with_bad_data(self):  # TODO
204         data = {}
205         response, context = self.step_test(SWConf_Resource_Select, data)
206
207     def test_step_with_empty_data(self):
208         data = {}
209         response, context = self.step_test(SWConf_Resource_Select, data)
210
211
212 class DefineSoftwareTestCase(BaseStepTestCase):
213
214     def makeRepo(self):
215         """
216         put selected grb in repo for step
217         """
218         repo = super(DefineSoftwareTestCase, self).makeRepo()
219         grb = GenericResourceBundle.objects.filter(owner__username="user 1").first()
220         repo.el[repo.SWCONF_SELECTED_GRB] = grb
221         return repo
222
223
224     def test_step_with_good_data(self):
225         data = {"form-INITIAL_FORMS": 3, "form-MAX_NUM_FORMS": 1000}
226         data["form-MIN_NUM_FORMS"] = 0
227         data["form-TOTAL_FORMS"] = 3
228         an_image_id = Image.objects.get(name="a host image").id
229         another_image_id = Image.objects.get(name="another host image").id
230         control = OPNFVRole.objects.get(name="Controller")
231         compute = OPNFVRole.objects.get(name="Compute")
232         jumphost = OPNFVRole.objects.get(name="Jumphost")
233         data['form-0-image'] = an_image_id
234         data['form-1-image'] = an_image_id
235         data['form-2-image'] = another_image_id
236         data['form-0-role'] = compute.id
237         data['form-1-role'] = control.id
238         data['form-2-role'] = jumphost.id
239         response, context = self.step_test(Define_Software, data)
240
241     def test_step_with_bad_data(self):  # TODO
242         data = {"form-INITIAL_FORMS": 0, "form-MAX_NUM_FORMS": 1000}
243         data["form-MIN_NUM_FORMS"] = 0
244         data["form-TOTAL_FORMS"] = 0
245         response, context = self.step_test(Define_Software, data)
246
247     def test_step_with_empty_data(self):
248         data = {"form-INITIAL_FORMS": 0, "form-MAX_NUM_FORMS": 1000}
249         data["form-MIN_NUM_FORMS"] = 0
250         data["form-TOTAL_FORMS"] = 0
251         response, context = self.step_test(Define_Software, data)
252
253
254 class ConfigSoftwareTestCase(BaseStepTestCase):
255
256     def test_step_with_good_data(self):
257         data = {"description": "description", "name": "namey"}
258         installer = Installer.objects.get(name="Fuel")
259         scenario = Scenario.objects.get(name="os-nosdn-nofeature-noha")
260         data['installer'] = installer.id
261         data['scenario'] = scenario.id
262         response, context = self.step_test(Config_Software, data)
263
264     def test_step_with_bad_data(self):  # TODO
265         data = {}
266         response, context = self.step_test(Config_Software, data)
267
268     def test_step_with_empty_data(self):
269         data = {}
270         response, context = self.step_test(Config_Software, data)
271