summaryrefslogtreecommitdiffstats
path: root/testapi/opnfv_testapi/tests/unit/handlers/test_scenario.py
blob: 481078d630686d65cbe10727c017193558bbb6e6 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
from datetime import datetime
import functools
import httplib

from opnfv_testapi.common import message
import opnfv_testapi.models.scenario_models as sm
from opnfv_testapi.tests.unit import executor
from opnfv_testapi.tests.unit.handlers import test_base as base


def _none_default(check, default):
    return check if check else default


class TestScenarioBase(base.TestBase):
    def setUp(self):
        super(TestScenarioBase, self).setUp()
        self.get_res = sm.Scenario
        self.list_res = sm.Scenarios
        self.basePath = '/api/v1/scenarios'
        self.req_d = self.load_json('scenario-c1')
        self.req_2 = self.load_json('scenario-c2')

    @executor.mock_valid_lfid()
    def create_return_name(self, req):
        _, res = self.create(req)
        return res.href.split('/')[-1]

    def assert_res(self, code, scenario, req=None):
        self.assertEqual(code, httplib.OK)
        if req is None:
            req = self.req_d
        self.assertIsNotNone(scenario._id)
        self.assertIsNotNone(scenario.creation_date)
        self.assertEqual(scenario, sm.Scenario.from_dict(req))

    @staticmethod
    def set_query(*args):
        uri = ''
        for arg in args:
            uri += arg + '&'
        return uri[0: -1]

    def get_and_assert(self, name):
        code, body = self.get(name)
        self.assert_res(code, body, self.req_d)


class TestScenarioCreate(TestScenarioBase):
    @executor.mock_valid_lfid()
    def test_withoutBody(self):
        (code, body) = self.create()
        self.assertEqual(code, httplib.BAD_REQUEST)

    @executor.mock_valid_lfid()
    def test_emptyName(self):
        req_empty = sm.ScenarioCreateRequest('')
        (code, body) = self.create(req_empty)
        self.assertEqual(code, httplib.BAD_REQUEST)
        self.assertIn(message.missing('name'), body)

    @executor.mock_valid_lfid()
    def test_noneName(self):
        req_none = sm.ScenarioCreateRequest(None)
        (code, body) = self.create(req_none)
        self.assertEqual(code, httplib.BAD_REQUEST)
        self.assertIn(message.missing('name'), body)

    @executor.mock_valid_lfid()
    def test_success(self):
        (code, body) = self.create_d()
        self.assertEqual(code, httplib.OK)
        self.assert_create_body(body)

    @executor.mock_valid_lfid()
    def test_alreadyExist(self):
        self.create_d()
        (code, body) = self.create_d()
        self.assertEqual(code, httplib.FORBIDDEN)
        self.assertIn(message.exist_base, body)


class TestScenarioGet(TestScenarioBase):
    def setUp(self):
        super(TestScenarioGet, self).setUp()
        self.scenario_1 = self.create_return_name(self.req_d)
        self.scenario_2 = self.create_return_name(self.req_2)

    def test_getByName(self):
        self.get_and_assert(self.scenario_1)

    def test_getAll(self):
        self._query_and_assert(query=None, reqs=[self.req_d, self.req_2])

    def test_queryName(self):
        query = self.set_query('name=nosdn-nofeature-ha')
        self._query_and_assert(query, reqs=[self.req_d])

    def test_queryInstaller(self):
        query = self.set_query('installer=apex')
        self._query_and_assert(query, reqs=[self.req_d])

    def test_queryVersion(self):
        query = self.set_query('version=master')
        self._query_and_assert(query, reqs=[self.req_d])

    def test_queryProject(self):
        query = self.set_query('project=functest')
        self._query_and_assert(query, reqs=[self.req_d, self.req_2])

    # close due to random fail, open again after solve it in another patch
    # def test_queryCombination(self):
    #     query = self._set_query('name=nosdn-nofeature-ha',
    #                             'installer=apex',
    #                             'version=master',
    #                             'project=functest')
    #
    #     self._query_and_assert(query, reqs=[self.req_d])

    def _query_and_assert(self, query, found=True, reqs=None):
        code, body = self.query(query)
        if not found:
            self.assertEqual(code, httplib.OK)
            self.assertEqual(0, len(body.scenarios))
        else:
            self.assertEqual(len(reqs), len(body.scenarios))
            for req in reqs:
                for scenario in body.scenarios:
                    if req['name'] == scenario.name:
                        self.assert_res(code, scenario, req)


class TestScenarioDelete(TestScenarioBase):
    def test_notFound(self):
        code, body = self.delete('notFound')
        self.assertEqual(code, httplib.NOT_FOUND)

    @executor.mock_valid_lfid()
    def test_success(self):
        scenario = self.create_return_name(self.req_d)
        code, _ = self.delete(scenario)
        self.assertEqual(code, httplib.OK)
        code, _ = self.get(scenario)
        self.assertEqual(code, httplib.NOT_FOUND)


class TestScenarioUpdate(TestScenarioBase):
    def setUp(self):
        super(TestScenarioUpdate, self).setUp()
        self.scenario = self.create_return_name(self.req_d)
        self.scenario_2 = self.create_return_name(self.req_2)
        self.update_url = ''
        self.scenario_url = '/api/v1/scenarios/{}'.format(self.scenario)
        self.installer = self.req_d['installers'][0]['installer']
        self.version = self.req_d['installers'][0]['versions'][0]['version']
        self.locate_project = 'installer={}&version={}&project={}'.format(
            self.installer,
            self.version,
            'functest')

    def update_url_fixture(item):
        def _update_url_fixture(xstep):
            def wrapper(self, *args, **kwargs):
                self.update_url = '{}/{}'.format(self.scenario_url, item)
                locator = None
                if item in ['projects', 'owner']:
                    locator = 'installer={}&version={}'.format(
                        self.installer,
                        self.version)
                elif item in ['versions']:
                    locator = 'installer={}'.format(
                        self.installer)
                elif item in ['rename']:
                    self.update_url = self.scenario_url

                if locator:
                    self.update_url = '{}?{}'.format(self.update_url, locator)

                xstep(self, *args, **kwargs)
            return wrapper
        return _update_url_fixture

    def update_partial(operate, expected):
        def _update_partial(set_update):
            @functools.wraps(set_update)
            def wrapper(self):
                update = set_update(self)
                code, body = getattr(self, operate)(update)
                getattr(self, expected)(code)
            return wrapper
        return _update_partial

    @executor.mock_valid_lfid()
    @update_partial('_add', '_success')
    def test_addScore(self):
        add = sm.ScenarioScore(date=str(datetime.now()), score='11/12')
        projects = self.req_d['installers'][0]['versions'][0]['projects']
        functest = filter(lambda f: f['project'] == 'functest', projects)[0]
        functest['scores'].append(add.format())
        self.update_url = '{}/scores?{}'.format(self.scenario_url,
                                                self.locate_project)

        return add

    @executor.mock_valid_lfid()
    @update_partial('_add', '_success')
    def test_addTrustIndicator(self):
        add = sm.ScenarioTI(date=str(datetime.now()), status='gold')
        projects = self.req_d['installers'][0]['versions'][0]['projects']
        functest = filter(lambda f: f['project'] == 'functest', projects)[0]
        functest['trust_indicators'].append(add.format())
        self.update_url = '{}/trust_indicators?{}'.format(self.scenario_url,
                                                          self.locate_project)

        return add

    @executor.mock_valid_lfid()
    @update_partial('_add', '_success')
    def test_addCustoms(self):
        adds = ['odl', 'parser', 'vping_ssh']
        projects = self.req_d['installers'][0]['versions'][0]['projects']
        functest = filter(lambda f: f['project'] == 'functest', projects)[0]
        functest['customs'] = list(set(functest['customs'] + adds))
        self.update_url = '{}/customs?{}'.format(self.scenario_url,
                                                 self.locate_project)
        return adds

    @executor.mock_valid_lfid()
    @update_partial('_update', '_success')
    def test_updateCustoms(self):
        updates = ['odl', 'parser', 'vping_ssh']
        projects = self.req_d['installers'][0]['versions'][0]['projects']
        functest = filter(lambda f: f['project'] == 'functest', projects)[0]
        functest['customs'] = updates
        self.update_url = '{}/customs?{}'.format(self.scenario_url,
                                                 self.locate_project)

        return updates

    @executor.mock_valid_lfid()
    @update_partial('_delete', '_success')
    def test_deleteCustoms(self):
        deletes = ['vping_ssh']
        projects = self.req_d['installers'][0]['versions'][0]['projects']
        functest = filter(lambda f: f['project'] == 'functest', projects)[0]
        functest['customs'] = ['healthcheck']
        self.update_url = '{}/customs?{}'.format(self.scenario_url,
                                                 self.locate_project)

        return deletes

    @executor.mock_valid_lfid()
    @update_url_fixture('projects')
    @update_partial('_add', '_success')
    def test_addProjects_succ(self):
        add = sm.ScenarioProject(project='qtip').format()
        self.req_d['installers'][0]['versions'][0]['projects'].append(add)
        return [add]

    @executor.mock_valid_lfid()
    @update_url_fixture('projects')
    @update_partial('_add', '_conflict')
    def test_addProjects_already_exist(self):
        add = sm.ScenarioProject(project='functest').format()
        return [add]

    @executor.mock_valid_lfid()
    @update_url_fixture('projects')
    @update_partial('_add', '_bad_request')
    def test_addProjects_bad_schema(self):
        add = sm.ScenarioProject(project='functest').format()
        add['score'] = None
        return [add]

    @executor.mock_valid_lfid()
    @update_url_fixture('projects')
    @update_partial('_update', '_success')
    def test_updateProjects_succ(self):
        update = sm.ScenarioProject(project='qtip').format()
        self.req_d['installers'][0]['versions'][0]['projects'] = [update]
        return [update]

    @executor.mock_valid_lfid()
    @update_url_fixture('projects')
    @update_partial('_update', '_conflict')
    def test_updateProjects_duplicated(self):
        update = sm.ScenarioProject(project='qtip').format()
        return [update, update]

    @executor.mock_valid_lfid()
    @update_url_fixture('projects')
    @update_partial('_update', '_bad_request')
    def test_updateProjects_bad_schema(self):
        update = sm.ScenarioProject(project='functest').format()
        update['score'] = None
        return [update]

    @executor.mock_valid_lfid()
    @update_url_fixture('projects')
    @update_partial('_delete', '_success')
    def test_deleteProjects(self):
        deletes = ['functest']
        projects = self.req_d['installers'][0]['versions'][0]['projects']
        self.req_d['installers'][0]['versions'][0]['projects'] = filter(
            lambda f: f['project'] != 'functest',
            projects)
        return deletes

    @executor.mock_valid_lfid()
    @update_url_fixture('owner')
    @update_partial('_update', '_success')
    def test_changeOwner(self):
        new_owner = 'new_owner'
        update = sm.ScenarioChangeOwnerRequest(new_owner).format()
        self.req_d['installers'][0]['versions'][0]['owner'] = new_owner
        return update

    @executor.mock_valid_lfid()
    @update_url_fixture('versions')
    @update_partial('_add', '_success')
    def test_addVersions_succ(self):
        add = sm.ScenarioVersion(version='Euphrates').format()
        self.req_d['installers'][0]['versions'].append(add)
        return [add]

    @executor.mock_valid_lfid()
    @update_url_fixture('versions')
    @update_partial('_add', '_conflict')
    def test_addVersions_already_exist(self):
        add = sm.ScenarioVersion(version='master').format()
        return [add]

    @executor.mock_valid_lfid()
    @update_url_fixture('versions')
    @update_partial('_add', '_bad_request')
    def test_addVersions_bad_schema(self):
        add = sm.ScenarioVersion(version='euphrates').format()
        add['notexist'] = None
        return [add]

    @executor.mock_valid_lfid()
    @update_url_fixture('versions')
    @update_partial('_update', '_success')
    def test_updateVersions_succ(self):
        update = sm.ScenarioVersion(version='euphrates').format()
        self.req_d['installers'][0]['versions'] = [update]
        return [update]

    @executor.mock_valid_lfid()
    @update_url_fixture('versions')
    @update_partial('_update', '_conflict')
    def test_updateVersions_duplicated(self):
        update = sm.ScenarioVersion(version='euphrates').format()
        return [update, update]

    @executor.mock_valid_lfid()
    @update_url_fixture('versions')
    @update_partial('_update', '_bad_request')
    def test_updateVersions_bad_schema(self):
        update = sm.ScenarioVersion(version='euphrates').format()
        update['not_owner'] = 'Iam'
        return [update]

    @executor.mock_valid_lfid()
    @update_url_fixture('versions')
    @update_partial('_delete', '_success')
    def test_deleteVersions(self):
        deletes = ['master']
        versions = self.req_d['installers'][0]['versions']
        self.req_d['installers'][0]['versions'] = filter(
            lambda f: f['version'] != 'master',
            versions)
        return deletes

    @executor.mock_valid_lfid()
    @update_url_fixture('installers')
    @update_partial('_add', '_success')
    def test_addInstallers_succ(self):
        add = sm.ScenarioInstaller(installer='daisy').format()
        self.req_d['installers'].append(add)
        return [add]

    @executor.mock_valid_lfid()
    @update_url_fixture('installers')
    @update_partial('_add', '_conflict')
    def test_addInstallers_already_exist(self):
        add = sm.ScenarioInstaller(installer='apex').format()
        return [add]

    @executor.mock_valid_lfid()
    @update_url_fixture('installers')
    @update_partial('_add', '_bad_request')
    def test_addInstallers_bad_schema(self):
        add = sm.ScenarioInstaller(installer='daisy').format()
        add['not_exist'] = 'not_exist'
        return [add]

    @executor.mock_valid_lfid()
    @update_url_fixture('installers')
    @update_partial('_update', '_success')
    def test_updateInstallers_succ(self):
        update = sm.ScenarioInstaller(installer='daisy').format()
        self.req_d['installers'] = [update]
        return [update]

    @executor.mock_valid_lfid()
    @update_url_fixture('installers')
    @update_partial('_update', '_conflict')
    def test_updateInstallers_duplicated(self):
        update = sm.ScenarioInstaller(installer='daisy').format()
        return [update, update]

    @executor.mock_valid_lfid()
    @update_url_fixture('installers')
    @update_partial('_update', '_bad_request')
    def test_updateInstallers_bad_schema(self):
        update = sm.ScenarioInstaller(installer='daisy').format()
        update['not_exist'] = 'not_exist'
        return [update]

    @executor.mock_valid_lfid()
    @update_url_fixture('installers')
    @update_partial('_delete', '_success')
    def test_deleteInstallers(self):
        deletes = ['apex']
        installers = self.req_d['installers']
        self.req_d['installers'] = filter(
            lambda f: f['installer'] != 'apex',
            installers)
        return deletes

    @executor.mock_valid_lfid()
    @update_url_fixture('rename')
    @update_partial('_update', '_success')
    def test_renameScenario(self):
        new_name = 'new_scenario_name'
        update = sm.ScenarioUpdateRequest(name=new_name)
        self.req_d['name'] = new_name
        return update

    @executor.mock_valid_lfid()
    @update_url_fixture('rename')
    @update_partial('_update', '_forbidden')
    def test_renameScenario_exist(self):
        new_name = self.req_d['name']
        update = sm.ScenarioUpdateRequest(name=new_name)
        return update

    def _add(self, update_req):
        return self.post_direct_url(self.update_url, update_req)

    def _update(self, update_req):
        return self.update_direct_url(self.update_url, update_req)

    def _delete(self, update_req):
        return self.delete_direct_url(self.update_url, update_req)

    def _success(self, status):
        self.assertEqual(status, httplib.OK)
        self.get_and_assert(self.req_d['name'])

    def _forbidden(self, status):
        self.assertEqual(status, httplib.FORBIDDEN)

    def _bad_request(self, status):
        self.assertEqual(status, httplib.BAD_REQUEST)

    def _conflict(self, status):
        self.assertEqual(status, httplib.CONFLICT)