Scott Baker | e27de6a | 2017-11-28 17:10:08 -0800 | [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 | |
| 17 | import unittest |
| 18 | from mock import patch, call, Mock, MagicMock, PropertyMock |
| 19 | import mock |
| 20 | |
| 21 | import os, sys |
| 22 | |
| 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") |
| 29 | |
Scott Baker | e96314e | 2018-01-07 08:28:46 -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)) |
| 41 | |
Scott Baker | e27de6a | 2017-11-28 17:10:08 -0800 | [diff] [blame] | 42 | class TestSyncVSGServiceInstance(unittest.TestCase): |
| 43 | def setUp(self): |
| 44 | global SyncVSGServiceInstance, 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 | e96314e | 2018-01-07 08:28:46 -0800 | [diff] [blame^] | 56 | build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("vsg", "vsg.xproto"), |
| 57 | get_models_fn("addressmanager", "addressmanager.xproto")]) |
Scott Baker | e27de6a | 2017-11-28 17:10:08 -0800 | [diff] [blame] | 58 | |
| 59 | import synchronizers.new_base.modelaccessor |
| 60 | import synchronizers.new_base.model_policies.model_policy_tenantwithcontainer |
| 61 | import sync_vsgserviceinstance |
| 62 | from sync_vsgserviceinstance import SyncVSGServiceInstance, model_accessor |
| 63 | |
| 64 | from mock_modelaccessor import MockObjectList |
| 65 | |
| 66 | # import all class names to globals |
| 67 | for (k, v) in model_accessor.all_model_classes.items(): |
| 68 | globals()[k] = v |
| 69 | |
| 70 | # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to creation of |
| 71 | # tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world. |
| 72 | model_accessor.reset_all_object_stores() |
| 73 | |
| 74 | # attic functions that are not present in the mock model accessor |
| 75 | AddressManagerServiceInstance.set_attribute = Mock() |
| 76 | |
| 77 | self.syncstep = SyncVSGServiceInstance() |
| 78 | |
| 79 | # set up an object hierarchy that represents a Service and ServiceInstance |
| 80 | |
| 81 | self.user = User(email="testadmin@test.org") |
| 82 | self.service = VSGService(name="the_vsg_service", |
| 83 | id=1, |
| 84 | docker_image_name="reg/vsg_docker", |
| 85 | docker_insecure_registry=True, |
| 86 | dns_servers="dnsone,dnstwo", |
| 87 | url_filter_kind=None, |
| 88 | private_key_fn=os.path.join(test_path, "test_private_key")) |
| 89 | self.subscriber = MagicMock(firewall_rules = "rule1", |
| 90 | firewall_enable = True, |
| 91 | url_filter_enable = True, |
| 92 | url_filter_level="R", |
| 93 | cdn_enable=True, |
| 94 | uplink_speed=1234, |
| 95 | downlink_speed=5678, |
| 96 | enable_uverse=False, |
| 97 | status="suspended", |
| 98 | sync_attributes=["firewall_rules", "firewall_enable", "url_filter_enable", |
| 99 | "url_filter_level", "cdn_enable", "uplink_speed", |
| 100 | "downlink_speed", "enable_uverse", "status"]) |
| 101 | self.volt = MagicMock(s_tag=111, c_tag=222, subscriber=self.subscriber) |
| 102 | self.tenant = VSGServiceInstance(creator=self.user, |
| 103 | id=401, |
| 104 | volt=self.volt, |
| 105 | owner=self.service, |
| 106 | wan_container_ip="10.7.1.3", |
| 107 | wan_container_netbits="24", |
| 108 | wan_container_mac="02:42:0a:07:01:03", |
| 109 | wan_container_gateway_ip="10.7.1.1", |
| 110 | wan_vm_ip="10.7.1.2", |
| 111 | wan_vm_mac="02:42:0a:07:01:02", |
| 112 | sync_attributes = ["wan_container_ip", "wan_container_netbits", "wan_container_mac", |
| 113 | "wan_container_gateway_ip", "wan_vm_ip", "wan_vm_mac"]) |
| 114 | self.flavor = Flavor(name="m1.small") |
| 115 | self.npt_ctag = NetworkParameterType(name="c_tag", id=1) |
| 116 | self.npt_stag = NetworkParameterType(name="s_tag", id=2) |
| 117 | self.npt_neutron_port_name = NetworkParameterType(name="neutron_port_name", id=501) |
| 118 | self.priv_template = NetworkTemplate(name="access_network", visibility="private") |
| 119 | self.priv_network = Network(name="mysite_test1_private", template=self.priv_template) |
| 120 | self.image = Image(name="trusty-server-multi-nic") |
| 121 | self.deployment = Deployment(name="testdeployment") |
| 122 | self.user = User(email="smbaker", id=701) |
| 123 | self.controller = Controller(id=101) |
| 124 | self.node = Node(name="testnode") |
| 125 | self.slice = Slice(name="mysite_test1", default_flavor=self.flavor, default_isolation="vm", service=self.service, id=301) |
| 126 | self.instance = Instance(slice=self.slice, |
| 127 | instance_name="testinstance1_instance_name", |
| 128 | instance_id="testinstance1_instance_id", |
| 129 | name="testinstance1_name", |
| 130 | node=self.node, |
| 131 | creator=self.user, |
| 132 | controller=self.controller) |
| 133 | self.tenant.instance = self.instance |
| 134 | self.instance.get_ssh_ip = Mock(return_value="1.2.3.4") |
| 135 | self.controllerslice = ControllerSlice(slice_id=self.slice.id, controller_id=self.controller.id, id=201) |
| 136 | self.controlleruser = ControllerUser(user_id=self.user.id, controller_id=self.controller.id, id=601) |
| 137 | |
| 138 | def tearDown(self): |
| 139 | sys.path = self.sys_path_save |
| 140 | |
| 141 | def test_get_vsg_service(self): |
| 142 | with patch.object(VSGService.objects, "get_items") as vsgservice_objects: |
| 143 | vsgservice_objects.return_value = [self.service] |
| 144 | |
| 145 | self.tenant.owner = self.service |
| 146 | |
| 147 | self.assertEqual(self.syncstep.get_vsg_service(self.tenant), self.service) |
| 148 | |
| 149 | def test_get_extra_attributes(self): |
| 150 | with patch.object(VSGService.objects, "get_items") as vsgservice_objects: |
| 151 | vsgservice_objects.return_value = [self.service] |
| 152 | |
| 153 | attrs = self.syncstep.get_extra_attributes(self.tenant) |
| 154 | |
| 155 | desired_attrs = {"s_tags": [111], |
| 156 | "c_tags": [222], |
| 157 | "docker_remote_image_name": "reg/vsg_docker", |
| 158 | "docker_local_image_name": "reg/vsg_docker", |
| 159 | "docker_opts": "--insecure-registry reg", |
| 160 | "dnsdemux_ip": "none", |
| 161 | "cdn_prefixes": [], |
| 162 | "full_setup": True, |
| 163 | "isolation": "vm", |
| 164 | "safe_browsing_macs": [], |
| 165 | "container_name": "vsg-111-222", |
| 166 | "dns_servers": ["dnsone", "dnstwo"], |
| 167 | "url_filter_kind": None, |
| 168 | |
| 169 | "firewall_rules": "rule1", |
| 170 | "firewall_enable": True, |
| 171 | "url_filter_enable": True, |
| 172 | "url_filter_level": "R", |
| 173 | "cdn_enable": True, |
| 174 | "uplink_speed": 1234, |
| 175 | "downlink_speed": 5678, |
| 176 | "enable_uverse": False, |
| 177 | "status": "suspended"} |
| 178 | |
| 179 | self.assertDictContainsSubset(desired_attrs, attrs) |
| 180 | |
| 181 | |
| 182 | def test_sync_record(self): |
| 183 | with patch.object(VSGService.objects, "get_items") as vsgservice_objects, \ |
| 184 | patch.object(Slice.objects, "get_items") as slice_objects, \ |
| 185 | patch.object(User.objects, "get_items") as user_objects, \ |
| 186 | patch.object(ControllerSlice.objects, "get_items") as controllerslice_objects, \ |
| 187 | patch.object(ControllerUser.objects, "get_items") as controlleruser_objects, \ |
| 188 | patch.object(SyncVSGServiceInstance, "run_playbook") as run_playbook: |
| 189 | slice_objects.return_value = [self.slice] |
| 190 | vsgservice_objects.return_value = [self.service] |
| 191 | controllerslice_objects.return_value = [self.controllerslice] |
| 192 | controlleruser_objects.return_value = [self.controlleruser] |
| 193 | user_objects.return_value = [self.user] |
| 194 | |
| 195 | self.tenant.updated = 10 |
| 196 | self.tenant.policed = 20 |
| 197 | self.tenant.enacted = None |
| 198 | |
| 199 | run_playbook.return_value = True |
| 200 | |
| 201 | self.syncstep.sync_record(self.tenant) |
| 202 | |
| 203 | run_playbook.assert_called() |
| 204 | |
| 205 | attrs = run_playbook.call_args[0][1] |
| 206 | |
| 207 | desired_attrs = {"username": "ubuntu", |
| 208 | "ansible_tag": "VSGServiceInstance_401", |
| 209 | "instance_name": "testinstance1_name", |
| 210 | "hostname": "testnode", |
| 211 | "private_key": "some_key\n", |
| 212 | "ssh_ip": "1.2.3.4", |
| 213 | "instance_id": "testinstance1_instance_id", |
| 214 | |
| 215 | "wan_container_ip": "10.7.1.3", |
| 216 | "wan_container_netbits": "24", |
| 217 | "wan_container_mac": "02:42:0a:07:01:03", |
| 218 | "wan_container_gateway_ip": "10.7.1.1", |
| 219 | "wan_vm_ip": "10.7.1.2", |
| 220 | "wan_vm_mac": "02:42:0a:07:01:02", |
| 221 | |
| 222 | "s_tags": [111], |
| 223 | "c_tags": [222], |
| 224 | "docker_remote_image_name": "reg/vsg_docker", |
| 225 | "docker_local_image_name": "reg/vsg_docker", |
| 226 | "docker_opts": "--insecure-registry reg", |
| 227 | "dnsdemux_ip": "none", |
| 228 | "cdn_prefixes": [], |
| 229 | "full_setup": True, |
| 230 | "isolation": "vm", |
| 231 | "safe_browsing_macs": [], |
| 232 | "container_name": "vsg-111-222", |
| 233 | "dns_servers": ["dnsone", "dnstwo"], |
| 234 | "url_filter_kind": None, |
| 235 | |
| 236 | "firewall_rules": "rule1", |
| 237 | "firewall_enable": True, |
| 238 | "url_filter_enable": True, |
| 239 | "url_filter_level": "R", |
| 240 | "cdn_enable": True, |
| 241 | "uplink_speed": 1234, |
| 242 | "downlink_speed": 5678, |
| 243 | "enable_uverse": False, |
| 244 | "status": "suspended"} |
| 245 | |
| 246 | self.assertDictContainsSubset(desired_attrs, attrs) |
| 247 | |
| 248 | def test_sync_record_emptysubscriber(self): |
| 249 | with patch.object(VSGService.objects, "get_items") as vsgservice_objects, \ |
| 250 | patch.object(Slice.objects, "get_items") as slice_objects, \ |
| 251 | patch.object(User.objects, "get_items") as user_objects, \ |
| 252 | patch.object(ControllerSlice.objects, "get_items") as controllerslice_objects, \ |
| 253 | patch.object(ControllerUser.objects, "get_items") as controlleruser_objects, \ |
| 254 | patch.object(SyncVSGServiceInstance, "run_playbook") as run_playbook: |
| 255 | slice_objects.return_value = [self.slice] |
| 256 | vsgservice_objects.return_value = [self.service] |
| 257 | controllerslice_objects.return_value = [self.controllerslice] |
| 258 | controlleruser_objects.return_value = [self.controlleruser] |
| 259 | user_objects.return_value = [self.user] |
| 260 | |
| 261 | self.tenant.updated = 10 |
| 262 | self.tenant.policed = 20 |
| 263 | self.tenant.enacted = None |
| 264 | |
| 265 | self.volt.subscriber = MagicMock() |
| 266 | |
| 267 | run_playbook.return_value = True |
| 268 | |
| 269 | self.syncstep.sync_record(self.tenant) |
| 270 | |
| 271 | run_playbook.assert_called() |
| 272 | |
| 273 | attrs = run_playbook.call_args[0][1] |
| 274 | |
| 275 | desired_attrs = {"firewall_rules": "", |
| 276 | "firewall_enable": False, |
| 277 | "url_filter_enable": False, |
| 278 | "url_filter_level": "PG", |
| 279 | "cdn_enable": False, |
| 280 | "uplink_speed": 1000000000, |
| 281 | "downlink_speed": 1000000000, |
| 282 | "enable_uverse": True, |
| 283 | "status": "enabled"} |
| 284 | |
| 285 | self.assertDictContainsSubset(desired_attrs, attrs) |
| 286 | |
| 287 | def test_sync_record_no_policy(self): |
| 288 | with patch.object(SyncVSGServiceInstance, "run_playbook") as run_playbook: |
| 289 | |
| 290 | self.tenant.updated = 10 |
| 291 | self.tenant.policed = 5 # policies need to be run |
| 292 | self.tenant.enacted = None |
| 293 | |
| 294 | with self.assertRaises(Exception) as e: |
| 295 | self.syncstep.sync_record(self.tenant) |
| 296 | self.assertIn("due to waiting on model policy", e.exception.message) |
| 297 | |
| 298 | run_playbook.assert_not_called() |
| 299 | |
| 300 | def test_sync_record_instance_not_ready(self): |
| 301 | with patch.object(SyncVSGServiceInstance, "run_playbook") as run_playbook: |
| 302 | |
| 303 | self.tenant.updated = 10 |
| 304 | self.tenant.policed = 20 |
| 305 | self.tenant.enacted = None |
| 306 | |
| 307 | self.instance.instance_name = None # no instance_name means instance is not ready |
| 308 | |
| 309 | with self.assertRaises(Exception) as e: |
| 310 | self.syncstep.sync_record(self.tenant) |
| 311 | self.assertIn("due to waiting on instance.instance_name", e.exception.message) |
| 312 | |
| 313 | run_playbook.assert_not_called() |
| 314 | |
| 315 | def test_delete_record_no_policy(self): |
| 316 | self.tenant.updated = 10 |
| 317 | self.tenant.policed = 20 |
| 318 | self.tenant.enacted = None |
| 319 | |
| 320 | self.syncstep.delete_record(self.tenant) |
| 321 | |
| 322 | # delete doesn't actually do anything, so nothing to further test. |
| 323 | |
| 324 | def test_delete_record_no_policy(self): |
| 325 | self.tenant.updated = 10 |
| 326 | self.tenant.policed = 5 # policies need to be run |
| 327 | self.tenant.enacted = None |
| 328 | |
| 329 | with self.assertRaises(Exception) as e: |
| 330 | self.syncstep.delete_record(self.tenant) |
| 331 | self.assertIn("due to waiting on model policy", e.exception.message) |
| 332 | |
| 333 | if __name__ == '__main__': |
| 334 | unittest.main() |
| 335 | |
| 336 | |