Browse Source

Handle unsupported network types

Raise exception in case of unsupported network types such as vxlan,
gre, etc. Add negative test cases for this change

Include method name as argument while raising MechanismDriverError

Change-Id: Ief051367d83e8048e9538334c23a339b38ea9630
Closes-Bug: #1441999
Pradeep Sathasivam 4 years ago
parent
commit
8d4b725849

+ 33
- 3
networking_brocade/mlx/ml2/fi_ni/mechanism_brocade_fi_ni.py View File

@@ -72,6 +72,19 @@ class BrocadeFiNiMechanism(driver_api.MechanismDriver):
72 72
         network_type = segments[0]['network_type']
73 73
         vlan_id = segments[0]['segmentation_id']
74 74
         physical_network = segments[0]['physical_network']
75
+        if physical_network not in self._physical_networks:
76
+            LOG.exception(_LE("BrocadeFiNiMechanism: Failed to create network."
77
+                              " Network cannot be created in the configured "
78
+                              "physical network %(physnet)s"),
79
+                          {'physnet': physical_network})
80
+            raise ml2_exc.MechanismDriverError(method='create_network_postcomm'
81
+                                               'it')
82
+        if network_type != 'vlan':
83
+            LOG.exception(_LE("BrocadeFiNiMechanism: Failed to create network "
84
+                              "for network type %(nw_type)s. Only network type"
85
+                              " vlan is supported"), {'nw_type': network_type})
86
+            raise ml2_exc.MechanismDriverError(method='create_network_postcomm'
87
+                                               'it')
75 88
         try:
76 89
             devices = self._physical_networks.get(physical_network)
77 90
             for device in devices:
@@ -86,7 +99,8 @@ class BrocadeFiNiMechanism(driver_api.MechanismDriver):
86 99
                                       "device %(host)s exception=%(error)s"),
87 100
                                   {'host': address,
88 101
                                    'error': e.args})
89
-                    raise ml2_exc.MechanismDriverError()
102
+                    raise ml2_exc.MechanismDriverError(method='create_network_'
103
+                                                       'postcommit')
90 104
                 # Proceed only if the driver is not None
91 105
                 if driver is not None:
92 106
                     driver.create_network(
@@ -96,7 +110,8 @@ class BrocadeFiNiMechanism(driver_api.MechanismDriver):
96 110
             LOG.exception(
97 111
                 _LE("Brocade FI/NI driver: create_network_postcommit failed"
98 112
                     "Error = %(error)s"), {'error': e.args})
99
-            raise ml2_exc.MechanismDriverError()
113
+            raise ml2_exc.MechanismDriverError(method='create_network_postcomm'
114
+                                               'it')
100 115
         LOG.info(_LI("BrocadeFiNiMechanism:created_network_postcommit: "
101 116
                      "%(network_id)s of network type = %(network_type)s with "
102 117
                      "vlan = %(vlan_id)s for tenant %(tenant_id)s"),
@@ -124,7 +139,21 @@ class BrocadeFiNiMechanism(driver_api.MechanismDriver):
124 139
         tenant_id = network['tenant_id']
125 140
         segments = mech_context.network_segments
126 141
         segment = segments[0]
142
+        network_type = segment['network_type']
127 143
         physical_network = segment['physical_network']
144
+        if physical_network not in self._physical_networks:
145
+            LOG.exception(_LE("BrocadeFiNiMechanism: Failed to delete network."
146
+                              " Network cannot be deleted in the configured "
147
+                              "physical network %(physnet)s"),
148
+                          {'physnet': physical_network})
149
+            raise ml2_exc.MechanismDriverError(method='delete_network_postcomm'
150
+                                               'it')
151
+        if network_type != 'vlan':
152
+            LOG.exception(_LE("BrocadeFiNiMechanism: Failed to delete network "
153
+                              "for network type %(nw_type)s. Only network type"
154
+                              " vlan is supported"), {'nw_type': network_type})
155
+            raise ml2_exc.MechanismDriverError(method='delete_network_postcomm'
156
+                                               'it')
128 157
         try:
129 158
             devices = self._physical_networks.get(physical_network)
130 159
             for device in devices:
@@ -133,7 +162,8 @@ class BrocadeFiNiMechanism(driver_api.MechanismDriver):
133 162
         except Exception:
134 163
             LOG.exception(
135 164
                 _LE("BrocadeFiNiMechanism: failed to delete network"))
136
-            raise ml2_exc.MechanismDriverError()
165
+            raise ml2_exc.MechanismDriverError(method='delete_network_postcomm'
166
+                                               'it')
137 167
 
138 168
         LOG.info(_LI("BrocadeFiNiMechanism: delete network (postcommit): "
139 169
                      "%(network_id)s with vlan = %(vlan_id)s for tenant "

+ 54
- 10
networking_brocade/mlx/tests/unit/ml2/drivers/brocade/test_brocade_fi_ni_mechanism_driver.py View File

@@ -17,6 +17,7 @@ import mock
17 17
 from networking_brocade.mlx.ml2.fi_ni import (
18 18
     mechanism_brocade_fi_ni as brocadefinimechanism)
19 19
 from neutron import context
20
+from neutron.plugins.ml2.common import exceptions as ml2_exc
20 21
 from neutron.tests import base
21 22
 from oslo_log import log as logging
22 23
 from oslo_utils import importutils
@@ -82,6 +83,31 @@ class TestBrocadeFiNiMechDriver(base.BaseTestCase):
82 83
             super(TestBrocadeFiNiMechDriver, self).setUp()
83 84
             self.mechanism_driver = importutils.import_object(_mechanism_name)
84 85
 
86
+    def test_create_network_postcommit_wrong_physnet(self):
87
+        """
88
+        Test create network with wrong value for physical network.
89
+        Physical network to which the devices belong is 'physnet1' but
90
+        we make a call to create network with physical network 'physnet2'.
91
+        In this case we raise an exception with error message -
92
+        "Brocade Mechanism: failed to create network, network cannot be
93
+        created in the configured physical network."
94
+        """
95
+        ctx = self._get_network_context('physnet2', 'vlan')
96
+        self.assertRaises(ml2_exc.MechanismDriverError,
97
+                          self.mechanism_driver.create_network_postcommit, ctx)
98
+
99
+    def test_create_network_postcommit_wrong_network_type(self):
100
+        """
101
+        Test create network with wrong value for network type. The plugin
102
+        allows to create network only if the request is to create a VLAN
103
+        network. For any other network type following exception is raised -
104
+        'Brocade Mechanism failed to create network, only network type vlan
105
+        is supported"
106
+        """
107
+        ctx = self._get_network_context('physnet1', 'vxlan')
108
+        self.assertRaises(ml2_exc.MechanismDriverError,
109
+                          self.mechanism_driver.create_network_postcommit, ctx)
110
+
85 111
     @mock.patch.object(brocadefinimechanism.BrocadeFiNiMechanism,
86 112
                        '_get_driver')
87 113
     def test_create_network_postcommit(self, mock_driver):
@@ -91,19 +117,42 @@ class TestBrocadeFiNiMechDriver(base.BaseTestCase):
91 117
         mock_driver.side_effect = self.side_effect
92 118
         ctx = self._get_network_context('physnet1', 'vlan')
93 119
         self.mechanism_driver.create_network_postcommit(ctx)
94
-        global config_map
95 120
         self.assertDictSupersetOf(config_map, actual_config_map)
96 121
 
122
+    @mock.patch.object(brocadefinimechanism.BrocadeFiNiMechanism,
123
+                       '_get_driver')
124
+    def test_create_network_postcommit_driver_exception(self, mock_driver):
125
+        """
126
+        Expect an exception while trying to get the driver
127
+        """
128
+        mock_driver.side_effect = self.side_effect_error
129
+        ctx = self._get_network_context('physnet1', 'vlan')
130
+        self.assertRaises(ml2_exc.MechanismDriverError,
131
+                          self.mechanism_driver.create_network_postcommit, ctx)
132
+
133
+    @mock.patch.object(brocadefinimechanism.BrocadeFiNiMechanism,
134
+                       '_get_driver')
135
+    def test_create_network_postcommit_exception(self, mock_driver):
136
+        """
137
+        Exception is raised when the input values are incorrect.
138
+        """
139
+        mock_driver.side_effect = self.side_effect_error
140
+        ctx = self._get_network_context('physnet1', 'vlan')
141
+        self.assertRaises(ml2_exc.MechanismDriverError,
142
+                          self.mechanism_driver.create_network_postcommit, ctx)
143
+
97 144
     @mock.patch.object(brocadefinimechanism.BrocadeFiNiMechanism,
98 145
                        '_get_driver')
99 146
     def test_delete_network_postcommit(self, mock_driver):
100 147
         """
101 148
         Test delete network
102 149
         """
103
-        global config_map
104
-        config_map = actual_config_map
105 150
         mock_driver.side_effect = self.side_effect
106 151
         ctx = self._get_network_context('physnet1', 'vlan')
152
+        vlan_map = {200: ['1/1/1', '1/1/2']}
153
+        config_map.update({'1.1.1.1': vlan_map})
154
+        vlan_map = {200: ['2/1', '2/2']}
155
+        config_map.update({'2.2.2.2': vlan_map})
107 156
         self.mechanism_driver.delete_network_postcommit(ctx)
108 157
         self.assertDictSupersetOf(config_map, empty_config_map)
109 158
 
@@ -184,19 +233,14 @@ class FakeDriver(object):
184 233
 
185 234
     def create_network(self, vlan_id, ports):
186 235
         if self.error:
187
-            raise Exception("Invalid Input - Create network failed")
236
+            raise ml2_exc.MechanismDriverError(method='create_network')
188 237
         vlan_map = {}
189 238
         vlan_map.update({vlan_id: ports})
190
-        global config_map
191 239
         config_map.update({self.address: vlan_map})
192 240
 
193 241
     def delete_network(self, vlan_id):
194
-        global config_map
195 242
         vlan_map = config_map.pop(self.address, None)
196 243
         if vlan_map is None:
197
-            raise Exception("No Address")
244
+            raise ml2_exc.MechanismDriverError(method='delete_network')
198 245
         else:
199 246
             vlan_map.pop(vlan_id, None)
200
-
201
-    def _get_config_map(self):
202
-        return self.config_map

Loading…
Cancel
Save