summaryrefslogtreecommitdiffstats
path: root/snaps/openstack/tests/create_security_group_tests.py
blob: 6f0fdecb77d0cbfc5371fba6f1066b62135227ed (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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
# Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
#                    and others.  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 unittest
import uuid

from snaps.config.security_group import (
    SecurityGroupConfig,  SecurityGroupRuleConfig,
    SecurityGroupRuleConfigError, SecurityGroupConfigError)
from snaps.openstack import create_security_group
from snaps.openstack.create_security_group import (
    SecurityGroupSettings, SecurityGroupRuleSettings, Direction, Ethertype,
    Protocol, OpenStackSecurityGroup)
from snaps.openstack.tests import validation_utils
from snaps.openstack.tests.os_source_file_test import OSIntegrationTestCase
from snaps.openstack.utils import neutron_utils

__author__ = 'spisarski'


class SecurityGroupRuleSettingsUnitTests(unittest.TestCase):
    """
    Tests the construction of the SecurityGroupRuleSettings class
    """

    def test_no_params(self):
        with self.assertRaises(SecurityGroupRuleConfigError):
            SecurityGroupRuleSettings()

    def test_empty_config(self):
        with self.assertRaises(SecurityGroupRuleConfigError):
            SecurityGroupRuleSettings(**dict())

    def test_name_only(self):
        with self.assertRaises(SecurityGroupRuleConfigError):
            SecurityGroupRuleSettings(sec_grp_name='foo')

    def test_config_with_name_only(self):
        with self.assertRaises(SecurityGroupRuleConfigError):
            SecurityGroupRuleSettings(**{'sec_grp_name': 'foo'})

    def test_name_and_direction(self):
        settings = SecurityGroupRuleSettings(sec_grp_name='foo',
                                             direction=Direction.ingress)
        self.assertEqual('foo', settings.sec_grp_name)
        self.assertEqual(Direction.ingress.value, settings.direction.value)

    def test_config_name_and_direction(self):
        settings = SecurityGroupRuleSettings(
            **{'sec_grp_name': 'foo', 'direction': 'ingress'})
        self.assertEqual('foo', settings.sec_grp_name)
        self.assertEqual(Direction.ingress.value, settings.direction.value)

    def test_proto_ah_str(self):
        settings = SecurityGroupRuleSettings(
            **{'sec_grp_name': 'foo', 'direction': 'ingress',
               'protocol': 'ah'})
        self.assertEqual('foo', settings.sec_grp_name)
        self.assertEqual(Direction.ingress.value, settings.direction.value)
        self.assertEqual(Protocol.ah.value, settings.protocol.value)

    def test_proto_ah_value(self):
        settings = SecurityGroupRuleSettings(
            **{'sec_grp_name': 'foo', 'direction': 'ingress',
               'protocol': 51})
        self.assertEqual('foo', settings.sec_grp_name)
        self.assertEqual(Direction.ingress.value, settings.direction.value)
        self.assertEqual(Protocol.ah.value, settings.protocol.value)

    def test_proto_any(self):
        settings = SecurityGroupRuleSettings(
            **{'sec_grp_name': 'foo', 'direction': 'ingress',
               'protocol': 'any'})
        self.assertEqual('foo', settings.sec_grp_name)
        self.assertEqual(Direction.ingress.value, settings.direction.value)
        self.assertEqual(Protocol.null.value, settings.protocol.value)

    def test_proto_null(self):
        settings = SecurityGroupRuleSettings(
            **{'sec_grp_name': 'foo', 'direction': 'ingress',
               'protocol': 'null'})
        self.assertEqual('foo', settings.sec_grp_name)
        self.assertEqual(Direction.ingress.value, settings.direction.value)
        self.assertEqual(Protocol.null.value, settings.protocol.value)

    def test_all(self):
        settings = SecurityGroupRuleSettings(
            sec_grp_name='foo', description='fubar',
            direction=Direction.egress, remote_group_id='rgi',
            protocol=Protocol.icmp, ethertype=Ethertype.IPv6, port_range_min=1,
            port_range_max=2,
            remote_ip_prefix='prfx')
        self.assertEqual('foo', settings.sec_grp_name)
        self.assertEqual('fubar', settings.description)
        self.assertEqual(Direction.egress.value, settings.direction.value)
        self.assertEqual('rgi', settings.remote_group_id)
        self.assertEqual(Protocol.icmp.value, settings.protocol.value)
        self.assertEqual(Ethertype.IPv6.value, settings.ethertype.value)
        self.assertEqual(1, settings.port_range_min)
        self.assertEqual(2, settings.port_range_max)
        self.assertEqual('prfx', settings.remote_ip_prefix)

    def test_config_all(self):
        settings = SecurityGroupRuleSettings(
            **{'sec_grp_name': 'foo',
               'description': 'fubar',
               'direction': 'egress',
               'remote_group_id': 'rgi',
               'protocol': 'tcp',
               'ethertype': 'IPv6',
               'port_range_min': 1,
               'port_range_max': 2,
               'remote_ip_prefix': 'prfx'})
        self.assertEqual('foo', settings.sec_grp_name)
        self.assertEqual('fubar', settings.description)
        self.assertEqual(Direction.egress.value, settings.direction.value)
        self.assertEqual('rgi', settings.remote_group_id)
        self.assertEqual(Protocol.tcp.value, settings.protocol.value)
        self.assertEqual(Ethertype.IPv6.value, settings.ethertype.value)
        self.assertEqual(1, settings.port_range_min)
        self.assertEqual(2, settings.port_range_max)
        self.assertEqual('prfx', settings.remote_ip_prefix)


class SecurityGroupSettingsUnitTests(unittest.TestCase):
    """
    Tests the construction of the SecurityGroupSettings class
    """

    def test_no_params(self):
        with self.assertRaises(SecurityGroupConfigError):
            SecurityGroupSettings()

    def test_empty_config(self):
        with self.assertRaises(SecurityGroupConfigError):
            SecurityGroupSettings(**dict())

    def test_name_only(self):
        settings = SecurityGroupSettings(name='foo')
        self.assertEqual('foo', settings.name)

    def test_config_with_name_only(self):
        settings = SecurityGroupSettings(**{'name': 'foo'})
        self.assertEqual('foo', settings.name)

    def test_invalid_rule(self):
        rule_setting = SecurityGroupRuleSettings(
            sec_grp_name='bar', direction=Direction.ingress,
            description='test_rule_1')
        with self.assertRaises(SecurityGroupConfigError):
            SecurityGroupSettings(name='foo', rule_settings=[rule_setting])

    def test_all(self):
        rule_settings = list()
        rule_settings.append(SecurityGroupRuleSettings(
            sec_grp_name='bar', direction=Direction.egress,
            description='test_rule_1'))
        rule_settings.append(SecurityGroupRuleSettings(
            sec_grp_name='bar', direction=Direction.ingress,
            description='test_rule_2'))
        settings = SecurityGroupSettings(
            name='bar', description='fubar', project_name='foo',
            rule_settings=rule_settings)

        self.assertEqual('bar', settings.name)
        self.assertEqual('fubar', settings.description)
        self.assertEqual('foo', settings.project_name)
        self.assertEqual(rule_settings[0], settings.rule_settings[0])
        self.assertEqual(rule_settings[1], settings.rule_settings[1])

    def test_config_all(self):
        settings = SecurityGroupSettings(
            **{'name': 'bar',
               'description': 'fubar',
               'project_name': 'foo',
               'rules': [
                   {'sec_grp_name': 'bar', 'direction': 'ingress'}]})

        self.assertEqual('bar', settings.name)
        self.assertEqual('fubar', settings.description)
        self.assertEqual('foo', settings.project_name)
        self.assertEqual(1, len(settings.rule_settings))
        self.assertEqual('bar', settings.rule_settings[0].sec_grp_name)
        self.assertEqual(Direction.ingress.value,
                         settings.rule_settings[0].direction.value)


class CreateSecurityGroupTests(OSIntegrationTestCase):
    """
    Test for the CreateSecurityGroup class defined in create_security_group.py
    """

    def setUp(self):
        """
        Instantiates the CreateSecurityGroup object that is responsible for
        downloading and creating an OS image file within OpenStack
        """
        super(self.__class__, self).__start__()

        guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
        self.sec_grp_name = guid + 'name'
        self.neutron = neutron_utils.neutron_client(self.os_creds)

        # Initialize for cleanup
        self.sec_grp_creator = None

    def tearDown(self):
        """
        Cleans the image and downloaded image file
        """
        if self.sec_grp_creator:
            self.sec_grp_creator.clean()

        super(self.__class__, self).__clean__()

    def test_create_group_without_rules(self):
        """
        Tests the creation of an OpenStack Security Group without custom rules.
        """
        # Create Security Group
        sec_grp_settings = SecurityGroupConfig(name=self.sec_grp_name,
                                               description='hello group')
        self.sec_grp_creator = create_security_group.OpenStackSecurityGroup(
            self.os_creds, sec_grp_settings)
        self.sec_grp_creator.create()

        sec_grp = neutron_utils.get_security_group(
            self.neutron, self.keystone, sec_grp_settings=sec_grp_settings)
        self.assertIsNotNone(sec_grp)

        validation_utils.objects_equivalent(
            self.sec_grp_creator.get_security_group(), sec_grp)
        rules = neutron_utils.get_rules_by_security_group(
            self.neutron, self.sec_grp_creator.get_security_group())
        self.assertEqual(len(self.sec_grp_creator.get_rules()), len(rules))
        validation_utils.objects_equivalent(self.sec_grp_creator.get_rules(),
                                            rules)

        self.assertTrue(
            validate_sec_grp(
                self.neutron, self.keystone,
                self.sec_grp_creator.sec_grp_settings,
                self.sec_grp_creator.get_security_group()))

    def test_create_group_admin_user_to_new_project(self):
        """
        Tests the creation of an OpenStack Security Group without custom rules.
        """
        # Create Security Group
        sec_grp_settings = SecurityGroupConfig(
            name=self.sec_grp_name, description='hello group',
            project_name=self.os_creds.project_name)
        self.sec_grp_creator = OpenStackSecurityGroup(
            self.admin_os_creds, sec_grp_settings)
        self.sec_grp_creator.create()

        sec_grp = neutron_utils.get_security_group(
            self.neutron, self.keystone, sec_grp_settings=sec_grp_settings)
        self.assertIsNotNone(sec_grp)

        validation_utils.objects_equivalent(
            self.sec_grp_creator.get_security_group(), sec_grp)
        rules = neutron_utils.get_rules_by_security_group(
            self.neutron, self.sec_grp_creator.get_security_group())
        self.assertEqual(len(self.sec_grp_creator.get_rules()), len(rules))
        validation_utils.objects_equivalent(self.sec_grp_creator.get_rules(),
                                            rules)

        self.assertTrue(
            validate_sec_grp(
                self.neutron, self.keystone,
                self.sec_grp_creator.sec_grp_settings,
                self.sec_grp_creator.get_security_group(), rules))

        self.assertEqual(self.sec_grp_creator.get_security_group().id,
                         sec_grp.id)

        proj_creator = OpenStackSecurityGroup(
            self.os_creds, SecurityGroupConfig(name=self.sec_grp_name))
        proj_creator.create()

        self.assertEqual(self.sec_grp_creator.get_security_group().id,
                         proj_creator.get_security_group().id)

    def test_create_group_new_user_to_admin_project(self):
        """
        Tests the creation of an OpenStack Security Group without custom rules.
        """
        # Create Security Group
        sec_grp_settings = SecurityGroupConfig(
            name=self.sec_grp_name, description='hello group',
            project_name=self.os_creds.project_name)
        self.sec_grp_creator = create_security_group.OpenStackSecurityGroup(
            self.admin_os_creds, sec_grp_settings)
        self.sec_grp_creator.create()

        sec_grp = neutron_utils.get_security_group(
            self.neutron, self.keystone, sec_grp_settings=sec_grp_settings)
        self.assertIsNotNone(sec_grp)

        validation_utils.objects_equivalent(
            self.sec_grp_creator.get_security_group(), sec_grp)
        rules = neutron_utils.get_rules_by_security_group(
            self.neutron, self.sec_grp_creator.get_security_group())
        self.assertEqual(len(self.sec_grp_creator.get_rules()), len(rules))
        validation_utils.objects_equivalent(self.sec_grp_creator.get_rules(),
                                            rules)

        self.assertTrue(
            validate_sec_grp(
                self.neutron, self.keystone,
                self.sec_grp_creator.sec_grp_settings,
                self.sec_grp_creator.get_security_group(), rules))

    def test_create_delete_group(self):
        """
        Tests the creation of an OpenStack Security Group without custom rules.
        """
        # Create Security Group
        sec_grp_settings = SecurityGroupConfig(name=self.sec_grp_name,
                                               description='hello group')
        self.sec_grp_creator = create_security_group.OpenStackSecurityGroup(
            self.os_creds, sec_grp_settings)
        created_sec_grp = self.sec_grp_creator.create()
        self.assertIsNotNone(created_sec_grp)

        self.assertTrue(
            validate_sec_grp(
                self.neutron, self.keystone,
                self.sec_grp_creator.sec_grp_settings,
                self.sec_grp_creator.get_security_group()))

        neutron_utils.delete_security_group(self.neutron, created_sec_grp)
        self.assertIsNone(neutron_utils.get_security_group(
            self.neutron, self.keystone,
            sec_grp_settings=self.sec_grp_creator.sec_grp_settings))

        self.sec_grp_creator.clean()

    def test_create_group_with_one_simple_rule(self):
        """
        Tests the creation of an OpenStack Security Group with one simple
        custom rule.
        """
        # Create Security Group
        sec_grp_rule_settings = list()
        sec_grp_rule_settings.append(
            SecurityGroupRuleConfig(
                sec_grp_name=self.sec_grp_name, direction=Direction.ingress,
                description='test_rule_1'))
        sec_grp_settings = SecurityGroupConfig(
            name=self.sec_grp_name, description='hello group',
            rule_settings=sec_grp_rule_settings)
        self.sec_grp_creator = create_security_group.OpenStackSecurityGroup(
            self.os_creds, sec_grp_settings)
        self.sec_grp_creator.create()

        sec_grp = neutron_utils.get_security_group(
            self.neutron, self.keystone, sec_grp_settings=sec_grp_settings)
        validation_utils.objects_equivalent(
            self.sec_grp_creator.get_security_group(), sec_grp)
        rules = neutron_utils.get_rules_by_security_group(
            self.neutron, self.sec_grp_creator.get_security_group())
        self.assertEqual(len(self.sec_grp_creator.get_rules()), len(rules))
        validation_utils.objects_equivalent(self.sec_grp_creator.get_rules(),
                                            rules)

        self.assertTrue(
            validate_sec_grp(
                self.neutron, self.keystone,
                self.sec_grp_creator.sec_grp_settings,
                self.sec_grp_creator.get_security_group(), rules))

    def test_create_group_with_one_complex_rule(self):
        """
        Tests the creation of an OpenStack Security Group with one simple
        custom rule.
        """
        # Create Security Group
        sec_grp_rule_settings = list()
        sec_grp_rule_settings.append(
            SecurityGroupRuleConfig(
                sec_grp_name=self.sec_grp_name, direction=Direction.egress,
                protocol=Protocol.udp, ethertype=Ethertype.IPv4,
                port_range_min=10, port_range_max=20,
                description='test_rule_1'))
        sec_grp_settings = SecurityGroupConfig(
            name=self.sec_grp_name, description='hello group',
            rule_settings=sec_grp_rule_settings)
        self.sec_grp_creator = create_security_group.OpenStackSecurityGroup(
            self.os_creds, sec_grp_settings)
        self.sec_grp_creator.create()

        sec_grp = neutron_utils.get_security_group(
            self.neutron, self.keystone, sec_grp_settings=sec_grp_settings)
        validation_utils.objects_equivalent(
            self.sec_grp_creator.get_security_group(), sec_grp)
        rules = neutron_utils.get_rules_by_security_group(
            self.neutron, self.sec_grp_creator.get_security_group())
        self.assertEqual(len(self.sec_grp_creator.get_rules()), len(rules))
        validation_utils.objects_equivalent(self.sec_grp_creator.get_rules(),
                                            rules)

        self.assertTrue(
            validate_sec_grp(
                self.neutron, self.keystone,
                self.sec_grp_creator.sec_grp_settings,
                self.sec_grp_creator.get_security_group(), rules))

    def test_create_group_with_several_rules(self):
        """
        Tests the creation of an OpenStack Security Group with one simple
        custom rule.
        """
        # Create Security Group
        sec_grp_rule_settings = list()
        sec_grp_rule_settings.append(
            SecurityGroupRuleConfig(
                sec_grp_name=self.sec_grp_name, direction=Direction.ingress,
                description='test_rule_1'))
        sec_grp_rule_settings.append(
            SecurityGroupRuleConfig(
                sec_grp_name=self.sec_grp_name, direction=Direction.egress,
                protocol=Protocol.udp, ethertype=Ethertype.IPv6,
                description='test_rule_2'))
        sec_grp_rule_settings.append(
            SecurityGroupRuleConfig(
                sec_grp_name=self.sec_grp_name, direction=Direction.egress,
                protocol=Protocol.udp, ethertype=Ethertype.IPv4,
                port_range_min=10, port_range_max=20,
                description='test_rule_3'))
        sec_grp_settings = SecurityGroupConfig(
            name=self.sec_grp_name, description='hello group',
            rule_settings=sec_grp_rule_settings)
        self.sec_grp_creator = create_security_group.OpenStackSecurityGroup(
            self.os_creds, sec_grp_settings)
        self.sec_grp_creator.create()

        sec_grp = neutron_utils.get_security_group(
            self.neutron, self.keystone, sec_grp_settings=sec_grp_settings)
        validation_utils.objects_equivalent(
            self.sec_grp_creator.get_security_group(), sec_grp)
        rules = neutron_utils.get_rules_by_security_group(
            self.neutron, self.sec_grp_creator.get_security_group())
        self.assertEqual(len(self.sec_grp_creator.get_rules()), len(rules))
        validation_utils.objects_equivalent(self.sec_grp_creator.get_rules(),
                                            rules)

        self.assertTrue(
            validate_sec_grp(
                self.neutron, self.keystone,
                self.sec_grp_creator.sec_grp_settings,
                self.sec_grp_creator.get_security_group(), rules))

    def test_add_rule(self):
        """
        Tests the creation of an OpenStack Security Group with one simple
        custom rule then adds one after creation.
        """
        # Create Security Group
        sec_grp_rule_settings = list()
        sec_grp_rule_settings.append(
            SecurityGroupRuleConfig(
                sec_grp_name=self.sec_grp_name, direction=Direction.ingress,
                description='test_rule_1'))
        sec_grp_settings = SecurityGroupConfig(
            name=self.sec_grp_name, description='hello group',
            rule_settings=sec_grp_rule_settings)
        self.sec_grp_creator = create_security_group.OpenStackSecurityGroup(
            self.os_creds, sec_grp_settings)
        self.sec_grp_creator.create()

        sec_grp = neutron_utils.get_security_group(
            self.neutron, self.keystone, sec_grp_settings=sec_grp_settings)
        validation_utils.objects_equivalent(
            self.sec_grp_creator.get_security_group(), sec_grp)

        rules = neutron_utils.get_rules_by_security_group(
            self.neutron, self.sec_grp_creator.get_security_group())

        self.assertTrue(
            validate_sec_grp(
                self.neutron, self.keystone,
                self.sec_grp_creator.sec_grp_settings,
                self.sec_grp_creator.get_security_group(), rules))

        rules = neutron_utils.get_rules_by_security_group(
            self.neutron, self.sec_grp_creator.get_security_group())
        self.assertEqual(len(self.sec_grp_creator.get_rules()), len(rules))
        validation_utils.objects_equivalent(self.sec_grp_creator.get_rules(),
                                            rules)

        self.sec_grp_creator.add_rule(SecurityGroupRuleConfig(
            sec_grp_name=self.sec_grp_creator.sec_grp_settings.name,
            direction=Direction.egress, protocol=Protocol.icmp,
            description='test_rule_2'))
        rules2 = neutron_utils.get_rules_by_security_group(
            self.neutron, self.sec_grp_creator.get_security_group())
        self.assertEqual(len(rules) + 1, len(rules2))

    def test_remove_rule_by_id(self):
        """
        Tests the creation of an OpenStack Security Group with two simple
        custom rules then removes one by the rule ID.
        """
        # Create Security Group
        sec_grp_rule_settings = list()
        sec_grp_rule_settings.append(
            SecurityGroupRuleConfig(
                sec_grp_name=self.sec_grp_name, direction=Direction.ingress,
                description='test_rule_1'))
        sec_grp_rule_settings.append(
            SecurityGroupRuleConfig(
                sec_grp_name=self.sec_grp_name, direction=Direction.egress,
                protocol=Protocol.udp, ethertype=Ethertype.IPv6,
                description='test_rule_2'))
        sec_grp_rule_settings.append(
            SecurityGroupRuleConfig(
                sec_grp_name=self.sec_grp_name, direction=Direction.egress,
                protocol=Protocol.udp, ethertype=Ethertype.IPv4,
                port_range_min=10, port_range_max=20,
                description='test_rule_3'))
        sec_grp_settings = SecurityGroupConfig(
            name=self.sec_grp_name, description='hello group',
            rule_settings=sec_grp_rule_settings)
        self.sec_grp_creator = create_security_group.OpenStackSecurityGroup(
            self.os_creds, sec_grp_settings)
        self.sec_grp_creator.create()

        sec_grp = neutron_utils.get_security_group(
            self.neutron, self.keystone, sec_grp_settings=sec_grp_settings)
        validation_utils.objects_equivalent(
            self.sec_grp_creator.get_security_group(), sec_grp)
        rules = neutron_utils.get_rules_by_security_group(
            self.neutron, self.sec_grp_creator.get_security_group())
        self.assertEqual(len(self.sec_grp_creator.get_rules()), len(rules))
        validation_utils.objects_equivalent(self.sec_grp_creator.get_rules(),
                                            rules)

        self.assertTrue(
            validate_sec_grp(
                self.neutron, self.keystone,
                self.sec_grp_creator.sec_grp_settings,
                self.sec_grp_creator.get_security_group(), rules))

        self.sec_grp_creator.remove_rule(
            rule_id=rules[0].id)
        rules_after_del = neutron_utils.get_rules_by_security_group(
            self.neutron,
            self.sec_grp_creator.get_security_group())
        self.assertEqual(len(rules) - 1, len(rules_after_del))

    def test_remove_rule_by_setting(self):
        """
        Tests the creation of an OpenStack Security Group with two simple
        custom rules then removes one by the rule setting object
        """
        # Create Security Group
        sec_grp_rule_settings = list()
        sec_grp_rule_settings.append(
            SecurityGroupRuleConfig(
                sec_grp_name=self.sec_grp_name, direction=Direction.ingress,
                description='test_rule_1'))
        sec_grp_rule_settings.append(
            SecurityGroupRuleConfig(
                sec_grp_name=self.sec_grp_name, direction=Direction.egress,
                protocol=Protocol.udp, ethertype=Ethertype.IPv6,
                description='test_rule_2'))
        sec_grp_rule_settings.append(
            SecurityGroupRuleConfig(
                sec_grp_name=self.sec_grp_name, direction=Direction.egress,
                protocol=Protocol.udp, ethertype=Ethertype.IPv4,
                port_range_min=10, port_range_max=20,
                description='test_rule_3'))
        sec_grp_settings = SecurityGroupConfig(
            name=self.sec_grp_name, description='hello group',
            rule_settings=sec_grp_rule_settings)
        self.sec_grp_creator = create_security_group.OpenStackSecurityGroup(
            self.os_creds, sec_grp_settings)
        self.sec_grp_creator.create()

        sec_grp = neutron_utils.get_security_group(
            self.neutron, self.keystone, sec_grp_settings=sec_grp_settings)
        validation_utils.objects_equivalent(
            self.sec_grp_creator.get_security_group(), sec_grp)

        rules = neutron_utils.get_rules_by_security_group(
            self.neutron, self.sec_grp_creator.get_security_group())
        self.assertEqual(len(self.sec_grp_creator.get_rules()), len(rules))
        validation_utils.objects_equivalent(self.sec_grp_creator.get_rules(),
                                            rules)

        self.assertTrue(
            validate_sec_grp(
                self.neutron, self.keystone,
                self.sec_grp_creator.sec_grp_settings,
                self.sec_grp_creator.get_security_group(), rules))

        self.sec_grp_creator.remove_rule(rule_setting=sec_grp_rule_settings[0])
        rules_after_del = neutron_utils.get_rules_by_security_group(
            self.neutron,
            self.sec_grp_creator.get_security_group())
        self.assertEqual(len(rules) - 1, len(rules_after_del))


def validate_sec_grp(neutron, keystone, sec_grp_settings, sec_grp,
                     rules=list()):
    """
    Returns True is the settings on a security group are properly contained
    on the SNAPS SecurityGroup domain object
    :param neutron: the neutron client
    :param keystone: the keystone client
    :param sec_grp_settings: the security group configuration
    :param sec_grp: the SNAPS-OO security group object
    :param rules: collection of SNAPS-OO security group rule objects
    :return: T/F
    """
    return (sec_grp.description == sec_grp_settings.description and
            sec_grp.name == sec_grp_settings.name and
            validate_sec_grp_rules(
                neutron, keystone, sec_grp_settings.rule_settings, rules))


def validate_sec_grp_rules(neutron, keystone, rule_settings, rules):
    """
    Returns True is the settings on a security group rule are properly
    contained on the SNAPS SecurityGroupRule domain object.
    This function will only operate on rules that contain a description as
    this is the only means to tell if the rule is custom or defaulted by
    OpenStack
    :param neutron: the neutron client
    :param keystone: the keystone client
    :param rule_settings: collection of SecurityGroupRuleConfig objects
    :param rules: a collection of SecurityGroupRule domain objects
    :return: T/F
    """

    for rule_setting in rule_settings:
        if rule_setting.description:
            match = False
            for rule in rules:
                sec_grp = neutron_utils.get_security_group(
                    neutron, keystone, sec_grp_name=rule_setting.sec_grp_name)

                setting_eth_type = create_security_group.Ethertype.IPv4
                if rule_setting.ethertype:
                    setting_eth_type = rule_setting.ethertype

                if not sec_grp:
                    return False

                proto_str = 'null'
                if rule.protocol:
                    proto_str = rule.protocol

                if (rule.description == rule_setting.description and
                    rule.direction == rule_setting.direction.name and
                    rule.ethertype == setting_eth_type.name and
                    rule.port_range_max == rule_setting.port_range_max and
                    rule.port_range_min == rule_setting.port_range_min and
                    proto_str == str(rule_setting.protocol.value) and
                    rule.remote_group_id == rule_setting.remote_group_id and
                    rule.remote_ip_prefix == rule_setting.remote_ip_prefix and
                        rule.security_group_id == sec_grp.id):
                    match = True
                    break

            if not match:
                return False

    return True


class CreateMultipleSecurityGroupTests(OSIntegrationTestCase):
    """
    Test for the CreateSecurityGroup class and how it interacts with security
    groups within other projects with the same name
    """

    def setUp(self):
        """
        Instantiates the CreateSecurityGroup object that is responsible for
        downloading and creating an OS image file within OpenStack
        """
        super(self.__class__, self).__start__()

        guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
        self.sec_grp_name = guid + 'name'
        self.neutron = neutron_utils.neutron_client(self.os_creds)

        # Initialize for cleanup
        self.admin_sec_grp_config = SecurityGroupConfig(
            name=self.sec_grp_name, description='hello group')
        self.sec_grp_creator_admin = OpenStackSecurityGroup(
            self.admin_os_creds, self.admin_sec_grp_config)
        self.sec_grp_creator_admin.create()
        self.sec_grp_creator_proj = None

    def tearDown(self):
        """
        Cleans the image and downloaded image file
        """
        if self.sec_grp_creator_admin:
            self.sec_grp_creator_admin.clean()
        if self.sec_grp_creator_proj:
            self.sec_grp_creator_proj.clean()

        super(self.__class__, self).__clean__()

    def test_sec_grp_same_name_diff_proj(self):
        """
        Tests the creation of an OpenStack Security Group with the same name
        within a different project/tenant.
        """
        # Create Security Group
        sec_grp_config = SecurityGroupConfig(
            name=self.sec_grp_name, description='hello group')
        self.sec_grp_creator_proj = OpenStackSecurityGroup(
            self.os_creds, sec_grp_config)
        self.sec_grp_creator_proj.create()

        self.assertNotEqual(
            self.sec_grp_creator_admin.get_security_group().id,
            self.sec_grp_creator_proj.get_security_group().id)

        admin_sec_grp_creator = OpenStackSecurityGroup(
            self.admin_os_creds, self.admin_sec_grp_config)
        admin_sec_grp_creator.create()
        self.assertEqual(self.sec_grp_creator_admin.get_security_group().id,
                         admin_sec_grp_creator.get_security_group().id)

        proj_sec_grp_creator = OpenStackSecurityGroup(
            self.os_creds, sec_grp_config)
        proj_sec_grp_creator.create()
        self.assertEqual(self.sec_grp_creator_proj.get_security_group().id,
                         proj_sec_grp_creator.get_security_group().id)