1 ##############################################################################
2 # Copyright (c) 2018 Parker Berberian, 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 ##############################################################################
10 from django.test import TestCase
11 from dashboard.populate_db import Populator
12 from workflow.tests import constants
13 from workflow.workflow_factory import WorkflowFactory
14 from workflow.models import Repository
15 from workflow.resource_bundle_workflow import Define_Hardware, Define_Nets, Resource_Meta_Info, Host_Meta_Info
16 from workflow.sw_bundle_workflow import SWConf_Resource_Select, Define_Software, Config_Software
17 from workflow.booking_workflow import Booking_Resource_Select, SWConfig_Select, Booking_Meta
18 from django.http import QueryDict, HttpRequest
19 from django.contrib.auth.models import User
20 from resource_inventory.models import (
25 GenericResourceBundle,
33 class BaseStepTestCase(TestCase):
36 def setUpTestData(cls):
37 Populator().populate()
41 repo.el[repo.SESSION_USER] = User.objects.filter(username="user 1").first()
44 def step_test(self, step_type, data):
45 step = WorkflowFactory().make_step(step_type, self.makeRepo())
46 formData = QueryDict(mutable=True)
48 request = HttpRequest()
49 request.POST = formData
50 response = step.post_render(request)
51 context = step.get_context()
52 return response, context
55 class BookingResourceSelectTestCase(BaseStepTestCase):
57 def test_step_with_good_data(self):
58 grb_model = GenericResourceBundle.objects.filter(owner__username="user 1").first()
59 grb = [{"small_name": grb_model.name, "expanded_name": "user 1", "id": grb_model.id, "string": ""}]
60 grb = str(grb).replace("'", '"')
61 data = {"generic_resource_bundle": grb}
62 response, context = self.step_test(Booking_Resource_Select, data)
65 def test_step_with_bad_data(self): # TODO
67 response, context = self.step_test(Booking_Resource_Select, data)
69 def test_step_with_empty_data(self):
71 response, context = self.step_test(SWConfig_Select, data)
74 class SoftwareConfigSelectTestCase(BaseStepTestCase):
76 def test_step_with_good_data(self):
77 config_model = ConfigBundle.objects.filter(owner__username="user 1").first()
78 config = [{"expanded_name": "user 1", "small_name": config_model.name, "id": config_model.id, "string": ""}]
79 config = str(config).replace("'", '"')
80 data = {"software_bundle": config}
81 response, context = self.step_test(SWConfig_Select, data)
83 def test_step_with_bad_data(self): # TODO
85 response, context = self.step_test(SWConfig_Select, data)
87 def test_step_with_empty_data(self):
89 response, context = self.step_test(SWConfig_Select, data)
92 class BookingMetaTestCase(BaseStepTestCase):
94 def test_step_with_good_data(self):
95 data = {"length": 7, "project": "LaaS", "purpose": "testing"}
96 user2 = User.objects.get(username="user 2")
97 john = User.objects.get(username="johnsmith")
99 {"expanded_name": "", "id": user2.id, "small_name": user2.username, "string": user2.email},
100 {"expanded_name": "", "id": john.id, "small_name": john.username, "string": john.email}
102 users = str(users).replace("'", '"')
103 data['users'] = users
104 response, context = self.step_test(Booking_Meta, data)
106 def test_step_with_bad_data(self): # TODO
108 response, context = self.step_test(Booking_Meta, data)
110 def test_step_with_empty_data(self):
112 response, context = self.step_test(Booking_Meta, data)
115 class DefineHardwareTestCase(BaseStepTestCase):
117 def test_step_with_good_data(self):
118 hosts = {"host_4": 1, "host_1": 1}
119 labs = {"lab_1": "true"}
120 data = {"hosts": hosts, "labs": labs}
121 response, context = self.step_test(Define_Hardware, data)
123 def test_step_with_bad_data(self): # TODO
125 response, context = self.step_test(Define_Hardware, data)
127 def test_step_with_empty_data(self):
129 response, context = self.step_test(Define_Hardware, data)
132 class HostMetaInfoTestCase(BaseStepTestCase):
136 override to provide step with needed host info
138 repo = super(HostMetaInfoTestCase, self).makeRepo()
141 models['bundle'] = GenericResourceBundle()
143 gres1 = GenericResource(bundle=models['bundle'])
144 prof1 = HostProfile.objects.get(name="Test profile 0")
145 ghost1 = GenericHost(profile=prof1, resource=gres1)
147 gres2 = GenericResource(bundle=models['bundle'])
148 prof2 = HostProfile.objects.get(name="Test profile 3")
149 ghost2 = GenericHost(profile=prof2, resource=gres2)
150 models['hosts'] = [ghost1, ghost2]
151 repo.el[repo.GRESOURCE_BUNDLE_MODELS] = models
154 def test_step_with_good_data(self):
155 data = {"form-INITIAL_FORMS": 2, "form-MAX_NUM_FORMS": 1000}
156 data["form-MIN_NUM_FORMS"] = 0
157 data["form-TOTAL_FORMS"] = 2
158 data['form-0-host_name'] = "first host"
159 data['form-1-host_name'] = "second host"
160 response, context = self.step_test(Host_Meta_Info, data)
162 def test_step_with_bad_data(self): # TODO
163 data = {"form-INITIAL_FORMS": 0, "form-MAX_NUM_FORMS": 1000}
164 data["form-MIN_NUM_FORMS"] = 0
165 data["form-TOTAL_FORMS"] = 0
166 response, context = self.step_test(Host_Meta_Info, data)
168 def test_step_with_empty_data(self):
169 data = {"form-INITIAL_FORMS": 0, "form-MAX_NUM_FORMS": 1000}
170 data["form-MIN_NUM_FORMS"] = 0
171 data["form-TOTAL_FORMS"] = 0
172 response, context = self.step_test(Host_Meta_Info, data)
175 class DefineNetsTestCase(BaseStepTestCase):
177 def test_step_with_good_data(self):
178 xml = constants.POD_XML
180 response, context = self.step_test(Define_Nets, data)
182 def test_step_with_bad_data(self): # TODO
184 response, context = self.step_test(Define_Nets, data)
186 def test_step_with_empty_data(self):
188 response, context = self.step_test(Define_Nets, data)
191 class ResourceMetaInfoTestCase(BaseStepTestCase):
193 def test_step_with_good_data(self):
194 data = {"bundle_description": "description", "bundle_name": "my testing bundle"}
195 response, context = self.step_test(Resource_Meta_Info, data)
197 def test_step_with_bad_data(self): # TODO
199 response, context = self.step_test(Resource_Meta_Info, data)
201 def test_step_with_empty_data(self):
203 response, context = self.step_test(Resource_Meta_Info, data)
206 class SWConfResourceSelectTestCase(BaseStepTestCase):
208 def test_step_with_good_data(self):
209 grb_model = GenericResourceBundle.objects.filter(owner__username="user 1").first()
210 grb = [{"small_name": grb_model.name, "expanded_name": "user 1", "id": grb_model.id, "string": ""}]
211 grb = str(grb).replace("'", '"')
212 data = {"generic_resource_bundle": grb}
213 response, context = self.step_test(SWConf_Resource_Select, data)
215 def test_step_with_bad_data(self): # TODO
217 response, context = self.step_test(SWConf_Resource_Select, data)
219 def test_step_with_empty_data(self):
221 response, context = self.step_test(SWConf_Resource_Select, data)
224 class DefineSoftwareTestCase(BaseStepTestCase):
228 put selected grb in repo for step
230 repo = super(DefineSoftwareTestCase, self).makeRepo()
231 grb = GenericResourceBundle.objects.filter(owner__username="user 1").first()
232 repo.el[repo.SWCONF_SELECTED_GRB] = grb
235 def test_step_with_good_data(self):
236 data = {"form-INITIAL_FORMS": 3, "form-MAX_NUM_FORMS": 1000}
237 data["form-MIN_NUM_FORMS"] = 0
238 data["form-TOTAL_FORMS"] = 3
239 an_image_id = Image.objects.get(name="a host image").id
240 another_image_id = Image.objects.get(name="another host image").id
241 control = OPNFVRole.objects.get(name="Controller")
242 compute = OPNFVRole.objects.get(name="Compute")
243 jumphost = OPNFVRole.objects.get(name="Jumphost")
244 data['form-0-image'] = an_image_id
245 data['form-1-image'] = an_image_id
246 data['form-2-image'] = another_image_id
247 data['form-0-role'] = compute.id
248 data['form-1-role'] = control.id
249 data['form-2-role'] = jumphost.id
250 response, context = self.step_test(Define_Software, data)
252 def test_step_with_bad_data(self): # TODO
253 data = {"form-INITIAL_FORMS": 0, "form-MAX_NUM_FORMS": 1000}
254 data["form-MIN_NUM_FORMS"] = 0
255 data["form-TOTAL_FORMS"] = 0
256 response, context = self.step_test(Define_Software, data)
258 def test_step_with_empty_data(self):
259 data = {"form-INITIAL_FORMS": 0, "form-MAX_NUM_FORMS": 1000}
260 data["form-MIN_NUM_FORMS"] = 0
261 data["form-TOTAL_FORMS"] = 0
262 response, context = self.step_test(Define_Software, data)
265 class ConfigSoftwareTestCase(BaseStepTestCase):
267 def test_step_with_good_data(self):
268 data = {"description": "description", "name": "namey"}
269 installer = Installer.objects.get(name="Fuel")
270 scenario = Scenario.objects.get(name="os-nosdn-nofeature-noha")
271 data['installer'] = installer.id
272 data['scenario'] = scenario.id
273 response, context = self.step_test(Config_Software, data)
275 def test_step_with_bad_data(self): # TODO
277 response, context = self.step_test(Config_Software, data)
279 def test_step_with_empty_data(self):
281 response, context = self.step_test(Config_Software, data)