Make "Extra Info" step cleaner
[pharos-tools.git] / dashboard / 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
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 (
21     Scenario,
22     Installer,
23     OPNFVRole,
24     Image,
25     GenericResourceBundle,
26     GenericHost,
27     HostProfile,
28     GenericResource,
29     ConfigBundle
30 )
31
32
33 class BaseStepTestCase(TestCase):
34
35     @classmethod
36     def setUpTestData(cls):
37         Populator().populate()
38
39     def makeRepo(self):
40         repo = Repository()
41         repo.el[repo.SESSION_USER] = User.objects.filter(username="user 1").first()
42         return repo
43
44     def step_test(self, step_type, data):
45         step = WorkflowFactory().make_step(step_type, self.makeRepo())
46         formData = QueryDict(mutable=True)
47         formData.update(data)
48         request = HttpRequest()
49         request.POST = formData
50         response = step.post_render(request)
51         context = step.get_context()
52         return response, context
53
54
55 class BookingResourceSelectTestCase(BaseStepTestCase):
56
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)
63         self.assertTrue(True)
64
65     def test_step_with_bad_data(self):  # TODO
66         data = {}
67         response, context = self.step_test(Booking_Resource_Select, data)
68
69     def test_step_with_empty_data(self):
70         data = {}
71         response, context = self.step_test(SWConfig_Select, data)
72
73
74 class SoftwareConfigSelectTestCase(BaseStepTestCase):
75
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)
82
83     def test_step_with_bad_data(self):  # TODO
84         data = {}
85         response, context = self.step_test(SWConfig_Select, data)
86
87     def test_step_with_empty_data(self):
88         data = {}
89         response, context = self.step_test(SWConfig_Select, data)
90
91
92 class BookingMetaTestCase(BaseStepTestCase):
93
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")
98         users = [
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}
101         ]
102         users = str(users).replace("'", '"')
103         data['users'] = users
104         response, context = self.step_test(Booking_Meta, data)
105
106     def test_step_with_bad_data(self):  # TODO
107         data = {}
108         response, context = self.step_test(Booking_Meta, data)
109
110     def test_step_with_empty_data(self):
111         data = {}
112         response, context = self.step_test(Booking_Meta, data)
113
114
115 class DefineHardwareTestCase(BaseStepTestCase):
116
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)
122
123     def test_step_with_bad_data(self):  # TODO
124         data = {}
125         response, context = self.step_test(Define_Hardware, data)
126
127     def test_step_with_empty_data(self):
128         data = {}
129         response, context = self.step_test(Define_Hardware, data)
130
131
132 class HostMetaInfoTestCase(BaseStepTestCase):
133
134     def makeRepo(self):
135         """
136         override to provide step with needed host info
137         """
138         repo = super(HostMetaInfoTestCase, self).makeRepo()
139         # get models
140         models = {}
141         models['bundle'] = GenericResourceBundle()
142         # make generic hosts
143         gres1 = GenericResource(bundle=models['bundle'])
144         prof1 = HostProfile.objects.get(name="Test profile 0")
145         ghost1 = GenericHost(profile=prof1, resource=gres1)
146
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
152         return repo
153
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)
161
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)
167
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)
173
174
175 class DefineNetsTestCase(BaseStepTestCase):
176
177     def test_step_with_good_data(self):
178         xml = constants.POD_XML
179         data = {"xml": xml}
180         response, context = self.step_test(Define_Nets, data)
181
182     def test_step_with_bad_data(self):  # TODO
183         data = {}
184         response, context = self.step_test(Define_Nets, data)
185
186     def test_step_with_empty_data(self):
187         data = {}
188         response, context = self.step_test(Define_Nets, data)
189
190
191 class ResourceMetaInfoTestCase(BaseStepTestCase):
192
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)
196
197     def test_step_with_bad_data(self):  # TODO
198         data = {}
199         response, context = self.step_test(Resource_Meta_Info, data)
200
201     def test_step_with_empty_data(self):
202         data = {}
203         response, context = self.step_test(Resource_Meta_Info, data)
204
205
206 class SWConfResourceSelectTestCase(BaseStepTestCase):
207
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)
214
215     def test_step_with_bad_data(self):  # TODO
216         data = {}
217         response, context = self.step_test(SWConf_Resource_Select, data)
218
219     def test_step_with_empty_data(self):
220         data = {}
221         response, context = self.step_test(SWConf_Resource_Select, data)
222
223
224 class DefineSoftwareTestCase(BaseStepTestCase):
225
226     def makeRepo(self):
227         """
228         put selected grb in repo for step
229         """
230         repo = super(DefineSoftwareTestCase, self).makeRepo()
231         grb = GenericResourceBundle.objects.filter(owner__username="user 1").first()
232         repo.el[repo.SWCONF_SELECTED_GRB] = grb
233         return repo
234
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)
251
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)
257
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)
263
264
265 class ConfigSoftwareTestCase(BaseStepTestCase):
266
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)
274
275     def test_step_with_bad_data(self):  # TODO
276         data = {}
277         response, context = self.step_test(Config_Software, data)
278
279     def test_step_with_empty_data(self):
280         data = {}
281         response, context = self.step_test(Config_Software, data)