Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 1 | |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 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. |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 15 | |
| 16 | |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 17 | import unittest |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 18 | from mock import patch, call, Mock, PropertyMock |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 19 | import mock |
| 20 | |
| 21 | import os, sys |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 22 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -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") |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 29 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 30 | # --------------------------------------------------------------------------------------------------------------------- |
| 31 | # End Model Policy Testing Framework |
| 32 | # --------------------------------------------------------------------------------------------------------------------- |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 33 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 34 | class TestModelPolicyVsgTenant(unittest.TestCase): |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 35 | def setUp(self): |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 36 | global VEGTenantPolicy, 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, ["vEG/xos/veg.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_vegtenant |
| 53 | from model_policy_vegtenant import VEGTenantPolicy, 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 | |
| 62 | # Some of the functions we call have side-effects. For example, creating a VEGTenant may lead to creation of |
| 63 | # tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world. |
| 64 | model_accessor.reset_all_object_stores() |
| 65 | |
| 66 | # attic functions that are not present in the mock model accessor |
| 67 | VEGTenant.volt = PropertyMock(return_value = None) |
| 68 | AddressManagerServiceInstance.set_attribute = Mock() |
| 69 | |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 70 | self.policy = VEGTenantPolicy() |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 71 | self.tenant = VEGTenant() |
| 72 | self.user = User(email="testadmin@test.org") |
| 73 | self.tenant = VEGTenant(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 = [] |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 85 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 86 | def tearDownb(self): |
| 87 | sys.path = self.sys_path_save |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 88 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 89 | def test_handle_create(self): |
| 90 | with patch.object(VEGTenantPolicy, "manage_container") as manage_container, \ |
| 91 | patch.object(VEGTenantPolicy, "manage_address_service_instance") as manage_address_service_instance, \ |
| 92 | patch.object(VEGTenantPolicy, "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) |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 97 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 98 | def test_handle_update(self): |
| 99 | with patch.object(VEGTenantPolicy, "manage_container") as manage_container, \ |
| 100 | patch.object(VEGTenantPolicy, "manage_address_service_instance") as manage_address_service_instance, \ |
| 101 | patch.object(VEGTenantPolicy, "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) |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 106 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -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 should be called. |
| 113 | # NOTE: This will change when vEG is brought up to compliance with R-CORD vSG. See comments in VSG |
| 114 | # model policy test case. |
| 115 | amsi_delete.assert_called() |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 116 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 117 | def test_handle_delete_asi_noexist(self): |
| 118 | with patch.object(AddressManagerServiceInstance, "delete") as amsi_delete: |
| 119 | self.tenant.address_service_instance = None |
| 120 | self.policy.handle_delete(self.tenant) |
| 121 | amsi_delete.assert_not_called() |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 122 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 123 | # TODO: vEG doesn't currently implement this functionality |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 124 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 125 | # def test_handle_delete_cleaup_instance(self): |
| 126 | # with patch.object(VEGTenant.objects, "get_items") as vegtenant_objects, \ |
| 127 | # patch.object(Instance.objects, "get_items") as instance_objects, \ |
| 128 | # patch.object(Instance, "delete") as instance_delete: |
| 129 | # veg_service = VEGService() |
| 130 | # instance = Instance(id=1) |
| 131 | # instance_objects.return_value = [instance] |
| 132 | # self.tenant.address_service_instance = None |
| 133 | # self.tenant.instance = instance |
| 134 | # self.tenant.instance_id = instance.id |
| 135 | # self.tenant.owner = veg_service |
| 136 | # vegtenant_objects.return_value = [self.tenant] |
| 137 | # self.policy.handle_delete(self.tenant) |
| 138 | # instance_delete.assert_called() |
| 139 | |
| 140 | # TODO: vEG doesn't currently implement this functionality |
| 141 | |
| 142 | # def test_handle_delete_cleanup_instance_live(self): |
| 143 | # with patch.object(VEGTenant.objects, "get_items") as vegtenant_objects, \ |
| 144 | # patch.object(Instance.objects, "get_items") as instance_objects, \ |
| 145 | # patch.object(Instance, "delete") as instance_delete: |
| 146 | # # Make sure if an Instance still has active VSG Tenants, that we don't clean it up |
| 147 | # veg_service = VEGService() |
| 148 | # instance = Instance(id=1) |
| 149 | # instance_objects.return_value = [instance] |
| 150 | # self.tenant.address_service_instance = None |
| 151 | # self.tenant.instance = instance |
| 152 | # self.tenant.instance_id = instance.id |
| 153 | # self.tenant.owner = veg_service |
| 154 | # |
| 155 | # other_tenant = VEGTenant() |
| 156 | # other_tenant.address_service_instance = None |
| 157 | # other_tenant.instance = instance |
| 158 | # other_tenant.instance_id = instance.id |
| 159 | # other_tenant.owner = veg_service |
| 160 | # |
| 161 | # vegtenant_objects.return_value = [self.tenant, other_tenant] |
| 162 | # |
| 163 | # self.policy.handle_delete(self.tenant) |
| 164 | # instance_delete.assert_not_called() |
| 165 | |
| 166 | |
| 167 | # TODO: vEG doesn't currently implement this functionality |
| 168 | |
| 169 | # def test_handle_delete_cleanup_instance_and_tags_and_stuff(self): |
| 170 | # with patch.object(ServiceInstance.objects, "get_items") as si_objects, \ |
| 171 | # patch.object(AddressManagerServiceInstance.objects, "get_items") as amsi_objects, \ |
| 172 | # patch.object(Tag.objects, "get_items") as tag_objects, \ |
| 173 | # patch.object(VEGTenant.objects, "get_items") as vegtenant_objects, \ |
| 174 | # patch.object(Instance.objects, "get_items") as instance_objects, \ |
| 175 | # patch.object(AddressManagerServiceInstance, "delete") as amsi_delete, \ |
| 176 | # patch.object(Tag, "delete") as tag_delete, \ |
| 177 | # patch.object(Instance, "delete") as instance_delete: |
| 178 | # veg_service = VEGService() |
| 179 | # am_instance = AddressManagerServiceInstance() |
| 180 | # amsi_objects.return_value = [am_instance] |
| 181 | # si_objects.return_value = [am_instance] # AddressManagerServiceInstance is a subclass of ServiceInstance |
| 182 | # instance = Instance(id=1) |
| 183 | # instance_objects.return_value = [instance] |
| 184 | # self.tenant.address_service_instance = None |
| 185 | # self.tenant.instance = instance |
| 186 | # self.tenant.instance_id = instance.id |
| 187 | # self.tenant.owner = veg_service |
| 188 | # vegtenant_objects.return_value = [self.tenant] |
| 189 | # stag_tag = Tag(service_id=self.tenant.owner.id, content_type=instance.self_content_type_id, |
| 190 | # object_id=instance.id, name="s_tag") |
| 191 | # vrouter_tag = Tag(service_id=self.tenant.owner.id, content_type=instance.self_content_type_id, |
| 192 | # object_id=instance.id, name="vm_vrouter_tenant", value=am_instance.id) |
| 193 | # tag_objects.return_value = [stag_tag, vrouter_tag] |
| 194 | # self.policy.handle_delete(self.tenant) |
| 195 | # instance_delete.assert_called() |
| 196 | # assert stag_tag.delete.called |
| 197 | # assert vrouter_tag.delete.called |
| 198 | # assert am_instance.delete.called |
| 199 | |
| 200 | def test_cleanup_orphans(self): |
| 201 | with patch.object(AddressManagerServiceInstance.objects, "get_items") as amsi_objects, \ |
| 202 | patch.object(AddressManagerServiceInstance, "delete") as amsi_delete: |
| 203 | vrtenant = AddressManagerServiceInstance(id=1) |
| 204 | self.tenant.address_service_instance = vrtenant |
| 205 | some_other_vrtenant = AddressManagerServiceInstance(id=2) |
| 206 | link = ServiceInstanceLink(subscriber_service_instance=self.tenant, provider_service_instance=some_other_vrtenant) |
| 207 | self.tenant.subscribed_links = MockObjectList(initial=[link]) |
| 208 | amsi_objects.return_value = [some_other_vrtenant] |
| 209 | self.policy.cleanup_orphans(self.tenant) |
| 210 | amsi_delete.assert_called() |
| 211 | |
| 212 | def test_find_instance_for_s_tag_noexist(self): |
| 213 | with patch.object(Tag.objects, "get_items") as tag_objects: |
| 214 | tag_objects.filter.return_value = [] |
| 215 | instance = self.policy.find_instance_for_s_tag(3) |
| 216 | self.assertEqual(instance, None) |
| 217 | |
| 218 | def test_find_instance_for_s_tag(self): |
| 219 | with patch.object(Tag, "objects") as tag_objects: |
| 220 | tagged_instance = Instance() |
| 221 | tag = Tag(content_object = tagged_instance) |
| 222 | tag_objects.filter.return_value = [tag] |
| 223 | instance = self.policy.find_instance_for_s_tag(3) |
| 224 | self.assertEqual(instance, tagged_instance) |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 225 | |
| 226 | def test_manage_container_no_volt(self): |
| 227 | with self.assertRaises(Exception) as e: |
| 228 | self.policy.manage_container(self.tenant) |
| 229 | self.assertEqual(e.exception.message, "This VEG container has no volt") |
| 230 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 231 | def test_manage_container_noinstance(self): |
| 232 | with patch.object(VEGTenantPolicy, "find_or_make_instance_for_s_tag") as find_or_make_instance_for_s_tag, \ |
| 233 | patch.object(VEGTenant, "save") as tenant_save, \ |
| 234 | patch.object(VEGTenant, "volt") as volt: |
| 235 | instance = Instance() |
| 236 | volt.s_tag=222 |
| 237 | volt.c_tag=111 |
| 238 | find_or_make_instance_for_s_tag.return_value = instance |
| 239 | self.policy.manage_container(self.tenant) |
| 240 | self.assertEqual(self.tenant.instance, instance) |
| 241 | tenant_save.assert_called() |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 242 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 243 | def test_manage_container_hasinstance(self): |
| 244 | with patch.object(VEGTenantPolicy, "find_or_make_instance_for_s_tag") as find_or_make_instance_for_s_tag, \ |
| 245 | patch.object(VEGTenant, "save") as tenant_save, \ |
| 246 | patch.object(VEGTenant, "volt") as volt: |
| 247 | instance = Instance() |
| 248 | volt.s_tag=222 |
| 249 | volt.c_tag=111 |
| 250 | self.tenant.instance = instance |
| 251 | self.policy.manage_container(self.tenant) |
| 252 | find_or_make_instance_for_s_tag.assert_not_called() |
| 253 | self.assertEqual(self.tenant.instance, instance) |
| 254 | tenant_save.assert_not_called() |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 255 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 256 | def test_manage_container_deleted(self): |
| 257 | with patch.object(VEGTenantPolicy, "find_or_make_instance_for_s_tag") as find_or_make_instance_for_s_tag, \ |
| 258 | patch.object(VEGTenant, "save") as tenant_save, \ |
| 259 | patch.object(VEGTenant, "volt") as volt: |
| 260 | self.tenant.deleted = True |
| 261 | self.policy.manage_container(self.tenant) |
| 262 | find_or_make_instance_for_s_tag.assert_not_called() |
| 263 | tenant_save.assert_not_called() |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 264 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 265 | def test_find_or_make_port_noexist(self): |
| 266 | with patch.object(Port, "save") as port_save, \ |
| 267 | patch.object(Port, "objects") as port_objects: |
| 268 | instance = Instance(id=123) |
| 269 | network = Instance(id=456) |
| 270 | port_objects.filter.return_value = [] |
| 271 | port=self.policy.find_or_make_port(instance, network) |
| 272 | self.assertNotEqual(port, None) |
| 273 | port_save.assert_called() |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 274 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 275 | def test_find_or_make_port_exists(self): |
| 276 | with patch.object(Port, "save") as port_save, \ |
| 277 | patch.object(Port, "objects") as port_objects: |
| 278 | someport = Port() |
| 279 | def mock_port_filter(network_id, instance_id): |
| 280 | if (network_id==456) and (instance_id==123): |
| 281 | return [someport] |
| 282 | return None |
| 283 | instance = Instance(id=123) |
| 284 | network = Instance(id=456) |
| 285 | port_objects.filter.side_effect = mock_port_filter |
| 286 | port=self.policy.find_or_make_port(instance, network) |
| 287 | self.assertEqual(port, someport) |
| 288 | port_save.assert_not_called() |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 289 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 290 | def test_get_lan_network_noexist(self): |
| 291 | with patch.object(VEGService.objects, "get_items") as vegservice_objects: |
| 292 | vegservice=VEGService(name="myvegservice", id=1, slices=MockObjectList(initial=[self.slice])) |
| 293 | vegservice_objects.return_value = [vegservice] |
| 294 | self.tenant.owner = vegservice |
| 295 | self.slice.networks = MockObjectList() |
| 296 | with self.assertRaises(Exception) as e: |
| 297 | self.policy.get_lan_network(self.tenant, None) |
| 298 | self.assertEqual(e.exception.message, "No lan_network") |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 299 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 300 | def test_get_lan_network(self): |
| 301 | with patch.object(VEGService.objects, "get_items") as vegservice_objects: |
| 302 | vegservice=VEGService(name="myvegservice", id=1, slices=MockObjectList(initial=[self.slice])) |
| 303 | vegservice_objects.return_value = [vegservice] |
| 304 | self.tenant.owner = vegservice |
| 305 | self.slice.networks = MockObjectList([self.priv_network]) |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 306 | lan_network = self.policy.get_lan_network(self.tenant, None) |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 307 | self.assertEqual(lan_network, self.priv_network) |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 308 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 309 | def test_get_lan_network_toomany(self): |
| 310 | with patch.object(VEGService.objects, "get_items") as vegservice_objects: |
| 311 | some_other_network = Network(name="mysite_test1_private", template=self.priv_template) |
| 312 | vegservice=VEGService(name="myvegservice", id=1, slices=MockObjectList(initial=[self.slice])) |
| 313 | vegservice_objects.return_value = [vegservice] |
| 314 | self.tenant.owner = vegservice |
| 315 | self.slice.networks = MockObjectList([self.priv_network, some_other_network]) |
| 316 | with self.assertRaises(Exception) as e: |
| 317 | lan_network = self.policy.get_lan_network(self.tenant, None) |
| 318 | self.assertEqual(e.exception.message, "The vEG slice should only have one non-management private network") |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 319 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 320 | def test_port_set_parameter_noparamexist(self): |
| 321 | with patch.object(NetworkParameterType.objects, "get_items") as npt_objects: |
| 322 | npt_objects.return_value = [self.npt_stag] |
| 323 | port = Port() |
| 324 | self.policy.port_set_parameter(port, "s_tag", "123") |
| 325 | self.assertNotEqual(NetworkParameter.objects.all(), []) |
| 326 | param = NetworkParameter.objects.first() |
| 327 | self.assertEqual(param.value, "123") |
| 328 | self.assertEqual(param.parameter, self.npt_stag) |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 329 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 330 | def test_port_set_parameter_paramexist(self): |
| 331 | with patch.object(NetworkParameterType.objects, "get_items") as npt_objects, \ |
| 332 | patch.object(NetworkParameter.objects, "get_items") as np_objects: |
| 333 | port = Port(id=1) |
| 334 | np_orig = NetworkParameter(parameter_id=self.npt_stag.id, value="456", object_id=port.id, content_type=port.self_content_type_id) |
| 335 | np_objects.return_value = [np_orig] |
| 336 | npt_objects.return_value = [self.npt_stag] |
| 337 | self.policy.port_set_parameter(port, "s_tag", "123") |
| 338 | self.assertEqual(NetworkParameter.objects.count(), 1) |
| 339 | param = NetworkParameter.objects.first() |
| 340 | self.assertEqual(param.value, "123") |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 341 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 342 | def test_find_or_make_instance_for_s_tag(self): |
| 343 | with patch.object(NetworkParameterType.objects, "get_items") as npt_objects, \ |
| 344 | patch.object(Node.objects, "get_items") as node_objects, \ |
| 345 | patch.object(Flavor.objects, "get_items") as flavor_objects, \ |
| 346 | patch.object(VEGService.objects, "get_items") as vegservice_objects, \ |
| 347 | patch.object(VEGTenant, "volt") as volt, \ |
| 348 | patch.object(VEGTenant, "save") as tenant_save, \ |
| 349 | patch.object(VEGTenantPolicy, "get_image") as get_image, \ |
| 350 | patch.object(VEGTenantPolicy, "allocate_public_service_instance") as get_psi, \ |
| 351 | patch.object(LeastLoadedNodeScheduler, "pick") as pick, \ |
| 352 | patch.object(Node, "site_deployment") as site_deployment, \ |
| 353 | patch.object(Instance, "save") as instance_save, \ |
| 354 | patch.object(Instance, "delete") as instance_delete, \ |
| 355 | patch.object(VEGTenantPolicy, "port_set_parameter") as port_set_parameter: |
| 356 | # setup mocks |
| 357 | vrtenant = AddressManagerServiceInstance(public_ip="1.2.3.4", public_mac="01:02:03:04:05:06") |
| 358 | vegservice=VEGService(name="myvegservice", id=1, slices=MockObjectList(initial=[self.slice])) |
| 359 | vegservice_objects.return_value = [vegservice] |
| 360 | self.tenant.owner = vegservice |
| 361 | volt.s_tag=222 |
| 362 | volt.c_tag=111 |
| 363 | get_image.return_value = self.image |
| 364 | get_psi.return_value = vrtenant |
| 365 | pick.return_value = (self.node, None) |
| 366 | site_deployment.deployment = self.deployment |
| 367 | flavor_objects.return_value=[self.flavor] |
| 368 | node_objects.return_value=[self.node] |
| 369 | npt_objects.return_value=[self.npt_stag, self.npt_ctag, self.npt_neutron_port_name] |
| 370 | self.slice.networks = MockObjectList([self.priv_network]) |
| 371 | # done setup mocks |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 372 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 373 | # call the function under test |
| 374 | instance = self.policy.find_or_make_instance_for_s_tag(self.tenant, volt.s_tag) |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 375 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 376 | # make sure Instance was created |
| 377 | self.assertNotEqual(instance, None) |
| 378 | self.assertEqual(instance.creator.email, "testadmin@test.org") |
| 379 | self.assertEqual(instance.image.name, "trusty-server-multi-nic") |
| 380 | self.assertEqual(instance.flavor.name, "m1.small") |
| 381 | self.assertEqual(instance.isolation, "vm") |
| 382 | self.assertEqual(instance.node.hostname, "my.node.com") |
| 383 | self.assertEqual(instance.slice.name, "mysite_test1") |
| 384 | self.assertEqual(instance.parent, None) |
| 385 | instance_save.assert_called() |
| 386 | instance_delete.assert_not_called() |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 387 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 388 | # Access Network Port should have tags to c-tag and s-tag |
| 389 | port = Port.objects.first() |
| 390 | self.assertEqual(port.instance, instance) |
| 391 | self.assertEqual(port.network, self.priv_network) |
| 392 | port_set_parameter.assert_has_calls([mock.call(port, "c_tag", 111), |
| 393 | mock.call(port, "s_tag", 222), |
| 394 | mock.call(port, "neutron_port_name", "stag-222")]) |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 395 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 396 | # The instance should be tagged with the s-tag |
| 397 | tag = Tag.objects.get(name="s_tag") |
| 398 | self.assertEqual(tag.value, "222") |
| 399 | self.assertEqual(tag.object_id, instance.id) |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 400 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 401 | # The instance should have a tag pointing to its address_service_instance |
| 402 | tag = Tag.objects.get(name="vm_vrouter_tenant") |
| 403 | self.assertNotEqual(tag.value, vrtenant.id) |
| 404 | self.assertEqual(tag.object_id, instance.id) |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 405 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 406 | # Allocate_public_service_instance should have been called |
| 407 | get_psi.assert_called() |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 408 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 409 | def test_manage_address_service_instance(self): |
| 410 | with patch.object(VEGTenantPolicy, "allocate_public_service_instance") as get_psi: |
| 411 | vrtenant = AddressManagerServiceInstance(public_ip="1.2.3.4", public_mac="01:02:03:04:05:06") |
| 412 | get_psi.return_value = vrtenant |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 413 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 414 | self.tenant.vrouter = None |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 415 | |
Scott Baker | 021d4b5 | 2017-11-22 10:22:45 -0800 | [diff] [blame^] | 416 | self.policy.manage_address_service_instance(self.tenant) |
| 417 | |
| 418 | get_psi.assert_called_with(address_pool_name="addresses_veg", subscriber_tenant=self.tenant) |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 419 | |
| 420 | if __name__ == '__main__': |
| 421 | unittest.main() |
| 422 | |