Matteo Scandolo | aca8665 | 2017-08-08 13:05:27 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 17 | import unittest |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 18 | from mock import patch, call, Mock, PropertyMock |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 19 | import mock |
| 20 | |
| 21 | import os, sys |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 22 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 23 | test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 24 | service_dir=os.path.join(test_path, "../../../..") |
| 25 | xos_dir=os.path.join(test_path, "../../..") |
| 26 | if not os.path.exists(os.path.join(test_path, "new_base")): |
| 27 | xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos") |
| 28 | services_dir=os.path.join(xos_dir, "../../xos_services") |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 29 | |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame] | 30 | # --------------------------------------------------------------------------------------------------------------------- |
| 31 | # End Model Policy Testing Framework |
| 32 | # --------------------------------------------------------------------------------------------------------------------- |
| 33 | |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 34 | class TestModelPolicyVsgTenant(unittest.TestCase): |
| 35 | def setUp(self): |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 36 | global VSGServiceInstancePolicy, LeastLoadedNodeScheduler, MockObjectList |
| 37 | |
| 38 | self.sys_path_save = sys.path |
| 39 | sys.path.append(xos_dir) |
| 40 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
| 41 | |
| 42 | config = os.path.join(test_path, "test_config.yaml") |
| 43 | from xosconfig import Config |
| 44 | Config.clear() |
| 45 | Config.init(config, 'synchronizer-config-schema.yaml') |
| 46 | |
| 47 | from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor |
| 48 | build_mock_modelaccessor(xos_dir, services_dir, ["vsg/xos/vsg.xproto", "addressmanager/xos/addressmanager.xproto"]) |
| 49 | |
| 50 | import synchronizers.new_base.modelaccessor |
| 51 | import synchronizers.new_base.model_policies.model_policy_tenantwithcontainer |
| 52 | import model_policy_vsgserviceinstance |
| 53 | from model_policy_vsgserviceinstance import VSGServiceInstancePolicy, model_accessor |
| 54 | from synchronizers.new_base.model_policies.model_policy_tenantwithcontainer import LeastLoadedNodeScheduler |
| 55 | |
| 56 | from mock_modelaccessor import MockObjectList |
| 57 | |
| 58 | # import all class names to globals |
| 59 | for (k, v) in model_accessor.all_model_classes.items(): |
| 60 | globals()[k] = v |
| 61 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 62 | # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to creation of |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame] | 63 | # tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world. |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 64 | model_accessor.reset_all_object_stores() |
| 65 | |
| 66 | # attic functions that are not present in the mock model accessor |
| 67 | VSGServiceInstance.volt = PropertyMock(return_value = None) |
| 68 | AddressManagerServiceInstance.set_attribute = Mock() |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame] | 69 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 70 | self.policy = VSGServiceInstancePolicy() |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 71 | self.tenant = VSGServiceInstance() |
| 72 | self.user = User(email="testadmin@test.org") |
| 73 | self.tenant = VSGServiceInstance(creator=self.user, id=1) |
| 74 | self.flavor = Flavor(name="m1.small") |
| 75 | self.npt_ctag = NetworkParameterType(name="c_tag", id=1) |
| 76 | self.npt_stag = NetworkParameterType(name="s_tag", id=2) |
| 77 | self.npt_neutron_port_name = NetworkParameterType(name="neutron_port_name", id=3) |
| 78 | self.node = Node(hostname="my.node.com") |
| 79 | self.slice = Slice(name="mysite_test1", default_flavor=self.flavor, default_isolation="vm") |
| 80 | self.priv_template = NetworkTemplate(name="access_network", visibility="private") |
| 81 | self.priv_network = Network(name="mysite_test1_private", template=self.priv_template) |
| 82 | self.image = Image(name="trusty-server-multi-nic") |
| 83 | self.deployment = Deployment(name="testdeployment") |
| 84 | Tag.objects.item_list = [] |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame] | 85 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 86 | def tearDownb(self): |
| 87 | sys.path = self.sys_path_save |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 88 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 89 | def test_handle_create(self): |
| 90 | with patch.object(VSGServiceInstancePolicy, "manage_container") as manage_container, \ |
| 91 | patch.object(VSGServiceInstancePolicy, "manage_address_service_instance") as manage_address_service_instance, \ |
| 92 | patch.object(VSGServiceInstancePolicy, "cleanup_orphans") as cleanup_orphans: |
| 93 | self.policy.handle_create(self.tenant) |
| 94 | manage_container.assert_called_with(self.tenant) |
| 95 | manage_address_service_instance.assert_called_with(self.tenant) |
| 96 | cleanup_orphans.assert_called_with(self.tenant) |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 97 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 98 | def test_handle_update(self): |
| 99 | with patch.object(VSGServiceInstancePolicy, "manage_container") as manage_container, \ |
| 100 | patch.object(VSGServiceInstancePolicy, "manage_address_service_instance") as manage_address_service_instance, \ |
| 101 | patch.object(VSGServiceInstancePolicy, "cleanup_orphans") as cleanup_orphans: |
| 102 | self.policy.handle_create(self.tenant) |
| 103 | manage_container.assert_called_with(self.tenant) |
| 104 | manage_address_service_instance.assert_called_with(self.tenant) |
| 105 | cleanup_orphans.assert_called_with(self.tenant) |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 106 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 107 | def test_handle_delete_asi_exist(self): |
| 108 | with patch.object(AddressManagerServiceInstance, "delete") as amsi_delete: |
| 109 | vrtenant = AddressManagerServiceInstance() |
| 110 | self.tenant.address_service_instance = vrtenant |
| 111 | self.policy.handle_delete(self.tenant) |
| 112 | # The delete model_policy no longer deletes the AddressManagerServiceInstance. It's now handled by logic in |
| 113 | # ServiceInstanceLink, together with model_policies in the target object. |
| 114 | amsi_delete.assert_not_called() |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 115 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 116 | def test_handle_delete_asi_noexist(self): |
| 117 | with patch.object(AddressManagerServiceInstance, "delete") as amsi_delete: |
| 118 | self.tenant.address_service_instance = None |
| 119 | self.policy.handle_delete(self.tenant) |
| 120 | amsi_delete.assert_not_called() |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 121 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 122 | def test_handle_delete_cleanup_instance(self): |
| 123 | with patch.object(VSGServiceInstance.objects, "get_items") as vsgserviceinstance_objects, \ |
| 124 | patch.object(Instance.objects, "get_items") as instance_objects, \ |
| 125 | patch.object(Instance, "delete") as instance_delete: |
| 126 | vsg_service = VSGService() |
| 127 | instance = Instance(id=1) |
| 128 | instance_objects.return_value = [instance] |
| 129 | self.tenant.address_service_instance = None |
| 130 | self.tenant.instance = instance |
| 131 | self.tenant.instance_id = instance.id |
| 132 | self.tenant.owner = vsg_service |
| 133 | vsgserviceinstance_objects.return_value = [self.tenant] |
| 134 | self.policy.handle_delete(self.tenant) |
| 135 | instance_delete.assert_called() |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame] | 136 | |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame] | 137 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 138 | def test_handle_delete_cleanup_instance_live(self): |
| 139 | with patch.object(VSGServiceInstance.objects, "get_items") as vsgserviceinstance_objects, \ |
| 140 | patch.object(Instance.objects, "get_items") as instance_objects, \ |
| 141 | patch.object(Instance, "delete") as instance_delete: |
| 142 | # Make sure if an Instance still has active VSG Tenants, that we don't clean it up |
| 143 | vsg_service = VSGService() |
| 144 | instance = Instance(id=1) |
| 145 | instance_objects.return_value = [instance] |
| 146 | self.tenant.address_service_instance = None |
| 147 | self.tenant.instance = instance |
| 148 | self.tenant.instance_id = instance.id |
| 149 | self.tenant.owner = vsg_service |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame] | 150 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 151 | other_tenant = VSGServiceInstance() |
| 152 | other_tenant.address_service_instance = None |
| 153 | other_tenant.instance = instance |
| 154 | other_tenant.instance_id = instance.id |
| 155 | other_tenant.owner = vsg_service |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame] | 156 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 157 | vsgserviceinstance_objects.return_value = [self.tenant, other_tenant] |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame] | 158 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 159 | self.policy.handle_delete(self.tenant) |
| 160 | instance_delete.assert_not_called() |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame] | 161 | |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 162 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 163 | def test_handle_delete_cleanup_instance_and_tags_and_stuff(self): |
| 164 | with patch.object(ServiceInstance.objects, "get_items") as si_objects, \ |
| 165 | patch.object(AddressManagerServiceInstance.objects, "get_items") as amsi_objects, \ |
| 166 | patch.object(Tag.objects, "get_items") as tag_objects, \ |
| 167 | patch.object(VSGServiceInstance.objects, "get_items") as vsgserviceinstance_objects, \ |
| 168 | patch.object(Instance.objects, "get_items") as instance_objects, \ |
| 169 | patch.object(AddressManagerServiceInstance, "delete") as amsi_delete, \ |
| 170 | patch.object(Tag, "delete") as tag_delete, \ |
| 171 | patch.object(Instance, "delete") as instance_delete: |
| 172 | vsg_service = VSGService() |
| 173 | am_instance = AddressManagerServiceInstance() |
| 174 | amsi_objects.return_value = [am_instance] |
| 175 | si_objects.return_value = [am_instance] # AddressManagerServiceInstance is a subclass of ServiceInstance |
| 176 | instance = Instance(id=1) |
| 177 | instance_objects.return_value = [instance] |
| 178 | self.tenant.address_service_instance = None |
| 179 | self.tenant.instance = instance |
| 180 | self.tenant.instance_id = instance.id |
| 181 | self.tenant.owner = vsg_service |
| 182 | vsgserviceinstance_objects.return_value = [self.tenant] |
| 183 | stag_tag = Tag(service_id=self.tenant.owner.id, content_type=instance.self_content_type_id, |
| 184 | object_id=instance.id, name="s_tag") |
| 185 | vrouter_tag = Tag(service_id=self.tenant.owner.id, content_type=instance.self_content_type_id, |
| 186 | object_id=instance.id, name="vm_vrouter_tenant", value=am_instance.id) |
| 187 | tag_objects.return_value = [stag_tag, vrouter_tag] |
| 188 | self.policy.handle_delete(self.tenant) |
| 189 | instance_delete.assert_called() |
| 190 | assert stag_tag.delete.called |
| 191 | assert vrouter_tag.delete.called |
| 192 | assert am_instance.delete.called |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 193 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 194 | def test_cleanup_orphans(self): |
| 195 | with patch.object(AddressManagerServiceInstance.objects, "get_items") as amsi_objects, \ |
| 196 | patch.object(AddressManagerServiceInstance, "delete") as amsi_delete: |
| 197 | vrtenant = AddressManagerServiceInstance(id=1) |
| 198 | self.tenant.address_service_instance = vrtenant |
| 199 | some_other_vrtenant = AddressManagerServiceInstance(id=2) |
| 200 | link = ServiceInstanceLink(subscriber_service_instance=self.tenant, provider_service_instance=some_other_vrtenant) |
| 201 | self.tenant.subscribed_links = MockObjectList(initial=[link]) |
| 202 | amsi_objects.return_value = [some_other_vrtenant] |
| 203 | self.policy.cleanup_orphans(self.tenant) |
| 204 | amsi_delete.assert_called() |
| 205 | |
| 206 | def test_find_instance_for_s_tag_noexist(self): |
| 207 | with patch.object(Tag.objects, "get_items") as tag_objects: |
| 208 | tag_objects.filter.return_value = [] |
| 209 | instance = self.policy.find_instance_for_s_tag(3) |
| 210 | self.assertEqual(instance, None) |
| 211 | |
| 212 | def test_find_instance_for_s_tag(self): |
| 213 | with patch.object(Tag, "objects") as tag_objects: |
| 214 | tagged_instance = Instance() |
| 215 | tag = Tag(content_object = tagged_instance) |
| 216 | tag_objects.filter.return_value = [tag] |
| 217 | instance = self.policy.find_instance_for_s_tag(3) |
| 218 | self.assertEqual(instance, tagged_instance) |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 219 | |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 220 | def test_manage_container_no_volt(self): |
| 221 | with self.assertRaises(Exception) as e: |
| 222 | self.policy.manage_container(self.tenant) |
| 223 | self.assertEqual(e.exception.message, "This VSG container has no volt") |
| 224 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 225 | def test_manage_container_noinstance(self): |
| 226 | with patch.object(VSGServiceInstancePolicy, "find_or_make_instance_for_s_tag") as find_or_make_instance_for_s_tag, \ |
| 227 | patch.object(VSGServiceInstance, "save") as tenant_save, \ |
| 228 | patch.object(VSGServiceInstance, "volt") as volt: |
| 229 | instance = Instance() |
| 230 | volt.s_tag=222 |
| 231 | volt.c_tag=111 |
| 232 | find_or_make_instance_for_s_tag.return_value = instance |
| 233 | self.policy.manage_container(self.tenant) |
| 234 | self.assertEqual(self.tenant.instance, instance) |
| 235 | tenant_save.assert_called() |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 236 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 237 | def test_manage_container_hasinstance(self): |
| 238 | with patch.object(VSGServiceInstancePolicy, "find_or_make_instance_for_s_tag") as find_or_make_instance_for_s_tag, \ |
| 239 | patch.object(VSGServiceInstance, "save") as tenant_save, \ |
| 240 | patch.object(VSGServiceInstance, "volt") as volt: |
| 241 | instance = Instance() |
| 242 | volt.s_tag=222 |
| 243 | volt.c_tag=111 |
| 244 | self.tenant.instance = instance |
| 245 | self.policy.manage_container(self.tenant) |
| 246 | find_or_make_instance_for_s_tag.assert_not_called() |
| 247 | self.assertEqual(self.tenant.instance, instance) |
| 248 | tenant_save.assert_not_called() |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 249 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 250 | def test_manage_container_deleted(self): |
| 251 | with patch.object(VSGServiceInstancePolicy, "find_or_make_instance_for_s_tag") as find_or_make_instance_for_s_tag, \ |
| 252 | patch.object(VSGServiceInstance, "save") as tenant_save, \ |
| 253 | patch.object(VSGServiceInstance, "volt") as volt: |
| 254 | self.tenant.deleted = True |
| 255 | self.policy.manage_container(self.tenant) |
| 256 | find_or_make_instance_for_s_tag.assert_not_called() |
| 257 | tenant_save.assert_not_called() |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 258 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 259 | def test_find_or_make_port_noexist(self): |
| 260 | with patch.object(Port, "save") as port_save, \ |
| 261 | patch.object(Port, "objects") as port_objects: |
| 262 | instance = Instance(id=123) |
| 263 | network = Instance(id=456) |
| 264 | port_objects.filter.return_value = [] |
| 265 | port=self.policy.find_or_make_port(instance, network) |
| 266 | self.assertNotEqual(port, None) |
| 267 | port_save.assert_called() |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 268 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 269 | def test_find_or_make_port_exists(self): |
| 270 | with patch.object(Port, "save") as port_save, \ |
| 271 | patch.object(Port, "objects") as port_objects: |
| 272 | someport = Port() |
| 273 | def mock_port_filter(network_id, instance_id): |
| 274 | if (network_id==456) and (instance_id==123): |
| 275 | return [someport] |
| 276 | return None |
| 277 | instance = Instance(id=123) |
| 278 | network = Instance(id=456) |
| 279 | port_objects.filter.side_effect = mock_port_filter |
| 280 | port=self.policy.find_or_make_port(instance, network) |
| 281 | self.assertEqual(port, someport) |
| 282 | port_save.assert_not_called() |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 283 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 284 | def test_get_lan_network_noexist(self): |
| 285 | with patch.object(VSGService.objects, "get_items") as vsgservice_objects: |
| 286 | vsgservice=VSGService(name="myvsgservice", id=1, slices=MockObjectList(initial=[self.slice])) |
| 287 | vsgservice_objects.return_value = [vsgservice] |
| 288 | self.tenant.owner = vsgservice |
| 289 | self.slice.networks = MockObjectList() |
| 290 | with self.assertRaises(Exception) as e: |
| 291 | self.policy.get_lan_network(self.tenant, None) |
| 292 | self.assertEqual(e.exception.message, "No lan_network") |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 293 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 294 | def test_get_lan_network(self): |
| 295 | with patch.object(VSGService.objects, "get_items") as vsgservice_objects: |
| 296 | vsgservice=VSGService(name="myvsgservice", id=1, slices=MockObjectList(initial=[self.slice])) |
| 297 | vsgservice_objects.return_value = [vsgservice] |
| 298 | self.tenant.owner = vsgservice |
| 299 | self.slice.networks = MockObjectList([self.priv_network]) |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 300 | lan_network = self.policy.get_lan_network(self.tenant, None) |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 301 | self.assertEqual(lan_network, self.priv_network) |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 302 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 303 | def test_get_lan_network_toomany(self): |
| 304 | with patch.object(VSGService.objects, "get_items") as vsgservice_objects: |
| 305 | some_other_network = Network(name="mysite_test1_private", template=self.priv_template) |
| 306 | vsgservice=VSGService(name="myvsgservice", id=1, slices=MockObjectList(initial=[self.slice])) |
| 307 | vsgservice_objects.return_value = [vsgservice] |
| 308 | self.tenant.owner = vsgservice |
| 309 | self.slice.networks = MockObjectList([self.priv_network, some_other_network]) |
| 310 | with self.assertRaises(Exception) as e: |
| 311 | lan_network = self.policy.get_lan_network(self.tenant, None) |
| 312 | self.assertEqual(e.exception.message, "The vSG slice should only have one non-management private network") |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 313 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 314 | def test_port_set_parameter_noparamexist(self): |
| 315 | with patch.object(NetworkParameterType.objects, "get_items") as npt_objects: |
| 316 | npt_objects.return_value = [self.npt_stag] |
| 317 | port = Port() |
| 318 | self.policy.port_set_parameter(port, "s_tag", "123") |
| 319 | self.assertNotEqual(NetworkParameter.objects.all(), []) |
| 320 | param = NetworkParameter.objects.first() |
| 321 | self.assertEqual(param.value, "123") |
| 322 | self.assertEqual(param.parameter, self.npt_stag) |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 323 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 324 | def test_port_set_parameter_paramexist(self): |
| 325 | with patch.object(NetworkParameterType.objects, "get_items") as npt_objects, \ |
| 326 | patch.object(NetworkParameter.objects, "get_items") as np_objects: |
| 327 | port = Port(id=1) |
| 328 | np_orig = NetworkParameter(parameter_id=self.npt_stag.id, value="456", object_id=port.id, content_type=port.self_content_type_id) |
| 329 | np_objects.return_value = [np_orig] |
| 330 | npt_objects.return_value = [self.npt_stag] |
| 331 | self.policy.port_set_parameter(port, "s_tag", "123") |
| 332 | self.assertEqual(NetworkParameter.objects.count(), 1) |
| 333 | param = NetworkParameter.objects.first() |
| 334 | self.assertEqual(param.value, "123") |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 335 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 336 | def test_find_or_make_instance_for_s_tag(self): |
| 337 | with patch.object(NetworkParameterType.objects, "get_items") as npt_objects, \ |
| 338 | patch.object(Node.objects, "get_items") as node_objects, \ |
| 339 | patch.object(Flavor.objects, "get_items") as flavor_objects, \ |
| 340 | patch.object(VSGService.objects, "get_items") as vsgservice_objects, \ |
| 341 | patch.object(VSGServiceInstance, "volt") as volt, \ |
| 342 | patch.object(VSGServiceInstance, "save") as tenant_save, \ |
| 343 | patch.object(VSGServiceInstancePolicy, "get_image") as get_image, \ |
| 344 | patch.object(VSGServiceInstancePolicy, "allocate_public_service_instance") as get_psi, \ |
| 345 | patch.object(LeastLoadedNodeScheduler, "pick") as pick, \ |
| 346 | patch.object(Node, "site_deployment") as site_deployment, \ |
| 347 | patch.object(Instance, "save") as instance_save, \ |
| 348 | patch.object(Instance, "delete") as instance_delete, \ |
| 349 | patch.object(VSGServiceInstancePolicy, "port_set_parameter") as port_set_parameter: |
| 350 | # setup mocks |
| 351 | vrtenant = AddressManagerServiceInstance(public_ip="1.2.3.4", public_mac="01:02:03:04:05:06") |
| 352 | vsgservice=VSGService(name="myvsgservice", id=1, slices=MockObjectList(initial=[self.slice])) |
| 353 | vsgservice_objects.return_value = [vsgservice] |
| 354 | self.tenant.owner = vsgservice |
| 355 | volt.s_tag=222 |
| 356 | volt.c_tag=111 |
| 357 | get_image.return_value = self.image |
| 358 | get_psi.return_value = vrtenant |
| 359 | pick.return_value = (self.node, None) |
| 360 | site_deployment.deployment = self.deployment |
| 361 | flavor_objects.return_value=[self.flavor] |
| 362 | node_objects.return_value=[self.node] |
| 363 | npt_objects.return_value=[self.npt_stag, self.npt_ctag, self.npt_neutron_port_name] |
| 364 | self.slice.networks = MockObjectList([self.priv_network]) |
| 365 | # done setup mocks |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 366 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 367 | # call the function under test |
| 368 | instance = self.policy.find_or_make_instance_for_s_tag(self.tenant) |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 369 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 370 | # make sure Instance was created |
| 371 | self.assertNotEqual(instance, None) |
| 372 | self.assertEqual(instance.creator.email, "testadmin@test.org") |
| 373 | self.assertEqual(instance.image.name, "trusty-server-multi-nic") |
| 374 | self.assertEqual(instance.flavor.name, "m1.small") |
| 375 | self.assertEqual(instance.isolation, "vm") |
| 376 | self.assertEqual(instance.node.hostname, "my.node.com") |
| 377 | self.assertEqual(instance.slice.name, "mysite_test1") |
| 378 | self.assertEqual(instance.parent, None) |
| 379 | instance_save.assert_called() |
| 380 | instance_delete.assert_not_called() |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 381 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 382 | # Access Network Port should have tags to c-tag and s-tag |
| 383 | port = Port.objects.first() |
| 384 | self.assertEqual(port.instance, instance) |
| 385 | self.assertEqual(port.network, self.priv_network) |
| 386 | port_set_parameter.assert_has_calls([mock.call(port, "c_tag", 111), |
| 387 | mock.call(port, "s_tag", 222), |
| 388 | mock.call(port, "neutron_port_name", "stag-222")]) |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 389 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 390 | # The instance should be tagged with the s-tag |
| 391 | tag = Tag.objects.get(name="s_tag") |
| 392 | self.assertEqual(tag.value, "222") |
| 393 | self.assertEqual(tag.object_id, instance.id) |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 394 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 395 | # The instance should have a tag pointing to its address_service_instance |
| 396 | tag = Tag.objects.get(name="vm_vrouter_tenant") |
| 397 | self.assertNotEqual(tag.value, vrtenant.id) |
| 398 | self.assertEqual(tag.object_id, instance.id) |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 399 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 400 | # Allocate_public_service_instance should have been called |
| 401 | get_psi.assert_called() |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 402 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 403 | def test_manage_address_service_instance(self): |
| 404 | with patch.object(VSGServiceInstancePolicy, "allocate_public_service_instance") as get_psi: |
| 405 | vrtenant = AddressManagerServiceInstance(public_ip="1.2.3.4", public_mac="01:02:03:04:05:06") |
| 406 | get_psi.return_value = vrtenant |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 407 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 408 | self.tenant.address_service_instance = None |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 409 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame^] | 410 | self.policy.manage_address_service_instance(self.tenant) |
| 411 | |
| 412 | get_psi.assert_called_with(address_pool_name="addresses_vsg", subscriber_tenant=self.tenant) |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 413 | |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 414 | if __name__ == '__main__': |
| 415 | unittest.main() |
| 416 | |