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