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 ##############################################################################
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 *
23 class BaseStepTestCase(TestCase):
26 def setUpTestData(cls):
27 Populator().populate()
31 repo.el[repo.SESSION_USER] = User.objects.filter(username="user 1").first()
34 def step_test(self, step_type, data):
35 step = WorkflowFactory().make_step(step_type, self.makeRepo())
36 formData = QueryDict(mutable=True)
38 request = HttpRequest()
39 request.POST = formData
40 response = step.post_render(request)
41 context = step.get_context()
42 return response, context
45 class BookingResourceSelectTestCase(BaseStepTestCase):
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)
55 def test_step_with_bad_data(self): # TODO
57 response, context = self.step_test(Booking_Resource_Select, data)
59 def test_step_with_empty_data(self):
61 response, context = self.step_test(SWConfig_Select, data)
63 class SoftwareConfigSelectTestCase(BaseStepTestCase):
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)
72 def test_step_with_bad_data(self): # TODO
74 response, context = self.step_test(SWConfig_Select, data)
76 def test_step_with_empty_data(self):
78 response, context = self.step_test(SWConfig_Select, data)
80 class BookingMetaTestCase(BaseStepTestCase):
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")
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}
90 users = str(users).replace("'", '"')
92 response, context = self.step_test(Booking_Meta, data)
94 def test_step_with_bad_data(self): # TODO
96 response, context = self.step_test(Booking_Meta, data)
98 def test_step_with_empty_data(self):
100 response, context = self.step_test(Booking_Meta, data)
103 class DefineHardwareTestCase(BaseStepTestCase):
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)
111 def test_step_with_bad_data(self): # TODO
113 response, context = self.step_test(Define_Hardware, data)
115 def test_step_with_empty_data(self):
117 response, context = self.step_test(Define_Hardware, data)
120 class HostMetaInfoTestCase(BaseStepTestCase):
124 override to provide step with needed host info
126 repo = super(HostMetaInfoTestCase, self).makeRepo()
129 models['bundle'] = GenericResourceBundle()
131 gres1 = GenericResource(bundle=models['bundle'])
132 prof1 = HostProfile.objects.get(name="Test profile 0")
133 ghost1 = GenericHost(profile=prof1, resource=gres1)
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
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)
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)
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)
163 class DefineNetsTestCase(BaseStepTestCase):
165 def test_step_with_good_data(self):
166 xml = constants.POD_XML
168 response, context = self.step_test(Define_Nets, data)
170 def test_step_with_bad_data(self): # TODO
172 response, context = self.step_test(Define_Nets, data)
174 def test_step_with_empty_data(self):
176 response, context = self.step_test(Define_Nets, data)
179 class ResourceMetaInfoTestCase(BaseStepTestCase):
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)
185 def test_step_with_bad_data(self): # TODO
187 response, context = self.step_test(Resource_Meta_Info, data)
189 def test_step_with_empty_data(self):
191 response, context = self.step_test(Resource_Meta_Info, data)
194 class SWConfResourceSelectTestCase(BaseStepTestCase):
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)
203 def test_step_with_bad_data(self): # TODO
205 response, context = self.step_test(SWConf_Resource_Select, data)
207 def test_step_with_empty_data(self):
209 response, context = self.step_test(SWConf_Resource_Select, data)
212 class DefineSoftwareTestCase(BaseStepTestCase):
216 put selected grb in repo for step
218 repo = super(DefineSoftwareTestCase, self).makeRepo()
219 grb = GenericResourceBundle.objects.filter(owner__username="user 1").first()
220 repo.el[repo.SWCONF_SELECTED_GRB] = grb
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)
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)
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)
254 class ConfigSoftwareTestCase(BaseStepTestCase):
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)
264 def test_step_with_bad_data(self): # TODO
266 response, context = self.step_test(Config_Software, data)
268 def test_step_with_empty_data(self):
270 response, context = self.step_test(Config_Software, data)