summaryrefslogtreecommitdiffstats
path: root/networking_sfc/tests/unit/db/test_flowclassifier_db.py
blob: 36c9af8b2dbbac043afe9496b7237d67514b66bc (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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
# Copyright 2015 Futurewei. All rights reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import contextlib
import mock
import six
import webob.exc

from oslo_config import cfg
from oslo_utils import importutils
from oslo_utils import uuidutils

from neutron.api import extensions as api_ext
from neutron.common import config
from neutron.common import constants as const
import neutron.extensions as nextensions

from networking_sfc.db import flowclassifier_db as fdb
from networking_sfc import extensions
from networking_sfc.extensions import flowclassifier as fc_ext
from networking_sfc.tests import base


DB_FLOWCLASSIFIER_PLUGIN_CLASS = (
    "networking_sfc.db.flowclassifier_db.FlowClassifierDbPlugin"
)
extensions_path = ':'.join(extensions.__path__ + nextensions.__path__)


class FlowClassifierDbPluginTestCaseBase(base.BaseTestCase):
    def _create_flow_classifier(
        self, fmt, flow_classifier=None, expected_res_status=None, **kwargs
    ):
        ctx = kwargs.get('context', None)
        tenant_id = kwargs.get('tenant_id', self._tenant_id)
        data = {'flow_classifier': flow_classifier or {}}
        if ctx is None:
            data['flow_classifier'].update({'tenant_id': tenant_id})
        req = self.new_create_request(
            'flow_classifiers', data, fmt, context=ctx
        )
        res = req.get_response(self.ext_api)
        if expected_res_status:
            self.assertEqual(res.status_int, expected_res_status)
        return res

    @contextlib.contextmanager
    def flow_classifier(
        self, fmt=None, flow_classifier=None, do_delete=True, **kwargs
    ):
        if not fmt:
            fmt = self.fmt
        res = self._create_flow_classifier(fmt, flow_classifier, **kwargs)
        if res.status_int >= 400:
            raise webob.exc.HTTPClientError(code=res.status_int)
        flow_classifier = self.deserialize(fmt or self.fmt, res)
        yield flow_classifier
        if do_delete:
            self._delete('flow_classifiers',
                         flow_classifier['flow_classifier']['id'])

    def _get_expected_flow_classifier(self, flow_classifier):
        expected_flow_classifier = {
            'name': flow_classifier.get('name') or '',
            'description': flow_classifier.get('description') or '',
            'source_port_range_min': flow_classifier.get(
                'source_port_range_min'),
            'source_port_range_max': flow_classifier.get(
                'source_port_range_max'),
            'destination_port_range_min': flow_classifier.get(
                'destination_port_range_min'),
            'destination_port_range_max': flow_classifier.get(
                'destination_port_range_max'),
            'ethertype': flow_classifier.get(
                'ethertype') or 'IPv4',
            'protocol': flow_classifier.get(
                'protocol'),
            'l7_parameters': flow_classifier.get(
                'l7_parameters') or {}
        }
        if (
            'source_ip_prefix' in flow_classifier and
            flow_classifier['source_ip_prefix']
        ):
            expected_flow_classifier['source_ip_prefix'] = (
                flow_classifier['source_ip_prefix'])
        if (
            'destination_ip_prefix' in flow_classifier and
            flow_classifier['destination_ip_prefix']
        ):
            expected_flow_classifier['destination_ip_prefix'] = (
                flow_classifier['destination_ip_prefix'])
        return expected_flow_classifier

    def _test_create_flow_classifier(
        self, flow_classifier, expected_flow_classifier=None
    ):
        if expected_flow_classifier is None:
            expected_flow_classifier = self._get_expected_flow_classifier(
                flow_classifier)
        with self.flow_classifier(flow_classifier=flow_classifier) as fc:
            for k, v in six.iteritems(expected_flow_classifier):
                self.assertIn(k, fc['flow_classifier'])
                self.assertEqual(fc['flow_classifier'][k], v)


class FlowClassifierDbPluginTestCase(
    base.NeutronDbPluginV2TestCase,
    FlowClassifierDbPluginTestCaseBase
):
    resource_prefix_map = dict(
        (k, fc_ext.FLOW_CLASSIFIER_PREFIX)
        for k in fc_ext.RESOURCE_ATTRIBUTE_MAP.keys()
    )

    def setUp(self, core_plugin=None, flowclassifier_plugin=None,
              ext_mgr=None):
        mock_log_p = mock.patch.object(fdb, 'LOG')
        self.mock_log = mock_log_p.start()
        cfg.CONF.register_opts(fc_ext.flow_classifier_quota_opts, 'QUOTAS')
        if not flowclassifier_plugin:
            flowclassifier_plugin = DB_FLOWCLASSIFIER_PLUGIN_CLASS
        service_plugins = {
            fc_ext.FLOW_CLASSIFIER_EXT: flowclassifier_plugin
        }
        fdb.FlowClassifierDbPlugin.supported_extension_aliases = [
            fc_ext.FLOW_CLASSIFIER_EXT]
        fdb.FlowClassifierDbPlugin.path_prefix = (
            fc_ext.FLOW_CLASSIFIER_PREFIX
        )
        super(FlowClassifierDbPluginTestCase, self).setUp(
            ext_mgr=ext_mgr,
            plugin=core_plugin,
            service_plugins=service_plugins
        )
        if not ext_mgr:
            self.flowclassifier_plugin = importutils.import_object(
                flowclassifier_plugin)
            ext_mgr = api_ext.PluginAwareExtensionManager(
                extensions_path,
                {fc_ext.FLOW_CLASSIFIER_EXT: self.flowclassifier_plugin}
            )
            app = config.load_paste_app('extensions_test_app')
            self.ext_api = api_ext.ExtensionMiddleware(app, ext_mgr=ext_mgr)

    def test_create_flow_classifier(self):
        self._test_create_flow_classifier({})

    def test_quota_create_flow_classifier(self):
        cfg.CONF.set_override('quota_flow_classifier', 3, group='QUOTAS')
        self._create_flow_classifier(self.fmt, {}, expected_res_status=201)
        self._create_flow_classifier(self.fmt, {}, expected_res_status=201)
        self._create_flow_classifier(self.fmt, {}, expected_res_status=201)
        self._create_flow_classifier(self.fmt, {}, expected_res_status=409)

    def test_create_flow_classifier_with_all_fields(self):
        self._test_create_flow_classifier({
            'name': 'test1',
            'ethertype': const.IPv4,
            'protocol': const.PROTO_NAME_TCP,
            'source_port_range_min': 100,
            'source_port_range_max': 200,
            'destination_port_range_min': 101,
            'destination_port_range_max': 201,
            'source_ip_prefix': '10.100.0.0/16',
            'destination_ip_prefix': '10.200.0.0/16',
            'logical_source_port': None,
            'logical_destination_port': None,
            'l7_parameters': {}
        })

    def test_create_flow_classifier_with_all_supported_ethertype(self):
        self._test_create_flow_classifier({
            'ethertype': None
        })
        self._test_create_flow_classifier({
            'ethertype': 'IPv4'
        })
        self._test_create_flow_classifier({
            'ethertype': 'IPv6'
        })

    def test_create_flow_classifier_with_invalid_ethertype(self):
        self._create_flow_classifier(
            self.fmt, {
                'ethertype': 'unsupported',
            },
            expected_res_status=400
        )

    def test_create_flow_classifier_with_all_supported_protocol(self):
        self._test_create_flow_classifier({
            'protocol': None
        })
        self._test_create_flow_classifier({
            'protocol': const.PROTO_NAME_TCP
        })
        self._test_create_flow_classifier({
            'protocol': const.PROTO_NAME_UDP
        })
        self._test_create_flow_classifier({
            'protocol': const.PROTO_NAME_ICMP
        })

    def test_create_flow_classifier_with_invalid_protocol(self):
        self._create_flow_classifier(
            self.fmt, {
                'protocol': 'unsupported',
            },
            expected_res_status=400
        )

    def test_create_flow_classifier_with_all_supported_port_protocol(self):
        self._test_create_flow_classifier({
            'source_port_range_min': None,
            'source_port_range_max': None,
            'destination_port_range_min': None,
            'destination_port_range_max': None
        })
        self._test_create_flow_classifier({
            'source_port_range_min': 100,
            'source_port_range_max': 200,
            'destination_port_range_min': 100,
            'destination_port_range_max': 200,
            'protocol': const.PROTO_NAME_TCP
        })
        self._test_create_flow_classifier({
            'source_port_range_min': 100,
            'source_port_range_max': 100,
            'destination_port_range_min': 100,
            'destination_port_range_max': 100,
            'protocol': const.PROTO_NAME_TCP
        })
        self._test_create_flow_classifier({
            'source_port_range_min': '100',
            'source_port_range_max': '200',
            'destination_port_range_min': '100',
            'destination_port_range_max': '200',
            'protocol': const.PROTO_NAME_UDP
        }, {
            'source_port_range_min': 100,
            'source_port_range_max': 200,
            'destination_port_range_min': 100,
            'destination_port_range_max': 200,
            'protocol': const.PROTO_NAME_UDP
        })

    def test_create_flow_classifier_with_invalid__port_protocol(self):
        self._create_flow_classifier(
            self.fmt, {
                'source_port_range_min': 'abc',
                'protocol': const.PROTO_NAME_TCP
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'source_port_range_max': 'abc',
                'protocol': const.PROTO_NAME_TCP
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'source_port_range_min': 100,
                'source_port_range_max': 99,
                'protocol': const.PROTO_NAME_TCP
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'source_port_range_min': 65536,
                'protocol': const.PROTO_NAME_TCP
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'source_port_range_max': 65536,
                'protocol': const.PROTO_NAME_TCP
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'source_port_range_min': -1,
                'protocol': const.PROTO_NAME_TCP
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'source_port_range_max': -1,
                'protocol': const.PROTO_NAME_TCP
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'destination_port_range_min': 'abc',
                'protocol': const.PROTO_NAME_TCP
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'destination_port_range_max': 'abc',
                'protocol': const.PROTO_NAME_TCP
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'destination_port_range_min': 100,
                'destination_port_range_max': 99,
                'protocol': const.PROTO_NAME_TCP
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'destination_port_range_min': 65536,
                'protocol': const.PROTO_NAME_TCP
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'destination_port_range_max': 65536,
                'protocol': const.PROTO_NAME_TCP
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'destination_port_range_min': -1,
                'protocol': const.PROTO_NAME_TCP
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'destination_port_range_max': -1,
                'protocol': const.PROTO_NAME_TCP
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'source_port_range_min': 100
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'source_port_range_max': 100
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'source_port_range_min': 100,
                'source_port_range_max': 200
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'source_port_range_min': 100,
                'source_port_range_max': 200,
                'protocol': const.PROTO_NAME_ICMP
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'destination_port_range_min': 100
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'destination_port_range_max': 100
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'destination_port_range_min': 100,
                'destination_port_range_max': 200
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'destination_port_range_min': 100,
                'destination_port_range_max': 200,
                'protocol': const.PROTO_NAME_ICMP
            },
            expected_res_status=400
        )

    def test_create_flow_classifier_with_all_supported_ip_prefix(self):
        self._test_create_flow_classifier({
            'source_ip_prefix': None,
            'destination_ip_prefix': None
        })
        self._test_create_flow_classifier({
            'source_ip_prefix': '10.0.0.0/8',
            'destination_ip_prefix': '10.0.0.0/8'
        })

    def test_create_flow_classifier_with_invalid_ip_prefix(self):
        self._create_flow_classifier(
            self.fmt, {
                'source_ip_prefix': '10.0.0.0/34'
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'source_ip_prefix': '10.0.0.0.0/8'
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'source_ip_prefix': '256.0.0.0/8'
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'source_ip_prefix': '10.0.0.0'
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'destination_ip_prefix': '10.0.0.0/34'
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'destination_ip_prefix': '10.0.0.0.0/8'
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'destination_ip_prefix': '256.0.0.0/8'
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'destination_ip_prefix': '10.0.0.0'
            },
            expected_res_status=400
        )

    def test_create_flow_classifier_with_all_supported_l7_parameters(self):
        self._test_create_flow_classifier({
            'l7_parameters': None
        })
        self._test_create_flow_classifier({
            'l7_parameters': {}
        })

    def test_create_flow_classifier_with_invalid_l7_parameters(self):
        self._create_flow_classifier(
            self.fmt, {
                'l7_parameters': {'abc': 'def'}
            },
            expected_res_status=400
        )

    def test_create_flow_classifier_with_port_id(self):
        self._test_create_flow_classifier({
            'logical_source_port': None,
            'logical_destination_port': None,
        })
        with self.port(
            name='test1'
        ) as port:
            self._test_create_flow_classifier({
                'logical_source_port': port['port']['id'],
                'logical_destination_port': port['port']['id'],
            })

    def test_create_flow_classifier_with_nouuid_port_id(self):
        self._create_flow_classifier(
            self.fmt, {
                'logical_source_port': 'abc'
            },
            expected_res_status=400
        )
        self._create_flow_classifier(
            self.fmt, {
                'logical_destination_port': 'abc'
            },
            expected_res_status=400
        )

    def test_create_flow_classifier_with_unknown_port_id(self):
        self._create_flow_classifier(
            self.fmt, {
                'logical_source_port': uuidutils.generate_uuid()
            },
            expected_res_status=404
        )
        self._create_flow_classifier(
            self.fmt, {
                'logical_destination_port': uuidutils.generate_uuid()
            },
            expected_res_status=404
        )

    def test_list_flow_classifiers(self):
        with self.flow_classifier(flow_classifier={
            'name': 'test1'
        }) as fc1, self.flow_classifier(flow_classifier={
            'name': 'test2',
        }) as fc2:
            fcs = [fc1, fc2]
            self._test_list_resources(
                'flow_classifier', fcs
            )

    def test_list_flow_classifiers_with_params(self):
        with self.flow_classifier(flow_classifier={
            'name': 'test1'
        }) as fc1, self.flow_classifier(flow_classifier={
            'name': 'test2',
        }) as fc2:
            self._test_list_resources(
                'flow_classifier', [fc1],
                query_params='name=test1'
            )
            self._test_list_resources(
                'flow_classifier', [fc2],
                query_params='name=test2'
            )
            self._test_list_resources(
                'flow_classifier', [],
                query_params='name=test3'
            )

    def test_list_flow_classifiers_with_unknown_params(self):
        with self.flow_classifier(flow_classifier={
            'name': 'test1'
        }) as fc1, self.flow_classifier(flow_classifier={
            'name': 'test2',
        }) as fc2:
            self._test_list_resources(
                'flow_classifier', [fc1, fc2],
                query_params='hello=test3'
            )

    def test_show_flow_classifier(self):
        with self.flow_classifier(flow_classifier={
            'name': 'test1'
        }) as fc:
            req = self.new_show_request(
                'flow_classifiers', fc['flow_classifier']['id']
            )
            res = self.deserialize(
                self.fmt, req.get_response(self.ext_api)
            )
            for k, v in six.iteritems(fc['flow_classifier']):
                self.assertEqual(res['flow_classifier'][k], v)

    def test_show_flow_classifier_noexist(self):
        req = self.new_show_request(
            'flow_classifiers', '1'
        )
        res = req.get_response(self.ext_api)
        self.assertEqual(res.status_int, 404)

    def test_update_flow_classifier(self):
        with self.flow_classifier(flow_classifier={
            'name': 'test1',
            'description': 'desc1'
        }) as fc:
            updates = {
                'name': 'test2',
                'description': 'desc2',
            }
            req = self.new_update_request(
                'flow_classifiers', {'flow_classifier': updates},
                fc['flow_classifier']['id']
            )
            res = self.deserialize(
                self.fmt,
                req.get_response(self.ext_api)
            )
            expected = fc['flow_classifier']
            expected.update(updates)
            for k, v in six.iteritems(expected):
                self.assertEqual(res['flow_classifier'][k], v)
            req = self.new_show_request(
                'flow_classifiers', fc['flow_classifier']['id']
            )
            res = self.deserialize(
                self.fmt, req.get_response(self.ext_api)
            )
            for k, v in six.iteritems(expected):
                self.assertEqual(res['flow_classifier'][k], v)

    def _test_update_with_field(
        self, fc, updates, expected_status_code
    ):
        req = self.new_update_request(
            'flow_classifiers', {'flow_classifier': updates},
            fc['flow_classifier']['id']
        )
        res = req.get_response(self.ext_api)
        self.assertEqual(res.status_int, expected_status_code)

    def test_update_flow_classifer_unsupported_fields(self):
        with self.flow_classifier(flow_classifier={
            'name': 'test1',
            'description': 'desc1'
        }) as fc:
            self._test_update_with_field(
                fc, {'ethertype': None}, 400)
            self._test_update_with_field(
                fc, {'protocol': None}, 400)
            self._test_update_with_field(
                fc, {'source_port_range_min': None}, 400)
            self._test_update_with_field(
                fc, {'source_port_range_max': None}, 400)
            self._test_update_with_field(
                fc, {'destination_port_range_min': None}, 400)
            self._test_update_with_field(
                fc, {'destination_port_range_max': None}, 400)
            self._test_update_with_field(
                fc, {'source_ip_prefix': None}, 400)
            self._test_update_with_field(
                fc, {'destination_ip_prefix': None}, 400)
            self._test_update_with_field(
                fc, {'l7_parameters': None}, 400)

    def test_delete_flow_classifier(self):
        with self.flow_classifier(flow_classifier={
            'name': 'test1'
        }, do_delete=False) as fc:
            req = self.new_delete_request(
                'flow_classifiers', fc['flow_classifier']['id']
            )
            res = req.get_response(self.ext_api)
            self.assertEqual(res.status_int, 204)
            req = self.new_show_request(
                'flow_classifiers', fc['flow_classifier']['id']
            )
            res = req.get_response(self.ext_api)
            self.assertEqual(res.status_int, 404)

    def test_delete_flow_classifier_noexist(self):
        req = self.new_delete_request(
            'flow_classifiers', '1'
        )
        res = req.get_response(self.ext_api)
        self.assertEqual(res.status_int, 404)