blob: 06101d62de347f3e66887f7d3740d6bd6bbbd186 [file] [log] [blame]
Scott Bakerb404b492017-12-01 13:01:10 -08001
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
17import unittest
18from mock import patch, call, Mock, PropertyMock
Scott Bakerb404b492017-12-01 13:01:10 -080019
20import os, sys
21
22test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
Scott Baker27e4fcb2018-01-09 10:24:52 -080023
Matteo Scandolod2458012018-03-01 13:40:36 -080024class TestModelPolicyVOLTServiceInstance(unittest.TestCase):
Scott Bakerb404b492017-12-01 13:01:10 -080025 def setUp(self):
Matteo Scandolod2458012018-03-01 13:40:36 -080026 global VOLTServiceInstancePolicy, MockObjectList
Scott Bakerb404b492017-12-01 13:01:10 -080027
28 self.sys_path_save = sys.path
Scott Bakerb404b492017-12-01 13:01:10 -080029
Matteo Scandolof7ebb112018-09-18 16:17:22 -070030 config = os.path.join(test_path, "../test_config.yaml")
Scott Bakerb404b492017-12-01 13:01:10 -080031 from xosconfig import Config
32 Config.clear()
33 Config.init(config, 'synchronizer-config-schema.yaml')
34
Scott Baker47b47302019-01-30 16:55:07 -080035 from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
36 mock_modelaccessor_config(test_path, [("olt-service", "volt.xproto"),
Matteo Scandolo35207b72019-05-10 08:46:48 -070037 ("rcord", "rcord.xproto")])
Scott Bakerb404b492017-12-01 13:01:10 -080038
Scott Baker47b47302019-01-30 16:55:07 -080039 import xossynchronizer.modelaccessor
40 import mock_modelaccessor
41 reload(mock_modelaccessor) # in case nose2 loaded it in a previous test
42 reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
43
44 from xossynchronizer.modelaccessor import model_accessor
45 self.model_accessor = model_accessor
Scott Bakerb404b492017-12-01 13:01:10 -080046
47 from mock_modelaccessor import MockObjectList
Scott Baker47b47302019-01-30 16:55:07 -080048 from model_policy_voltserviceinstance import VOLTServiceInstancePolicy
Scott Bakerb404b492017-12-01 13:01:10 -080049
50 # import all class names to globals
51 for (k, v) in model_accessor.all_model_classes.items():
52 globals()[k] = v
53
Matteo Scandolo35207b72019-05-10 08:46:48 -070054 # Some of the functions we call have side-effects. For example, creating a ServiceInstance may lead to creation of
Scott Bakerb404b492017-12-01 13:01:10 -080055 # tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world.
56 model_accessor.reset_all_object_stores()
57
Scott Baker47b47302019-01-30 16:55:07 -080058 self.policy = VOLTServiceInstancePolicy(model_accessor=self.model_accessor)
Matteo Scandolo2360fd92018-05-29 17:27:51 -070059 self.si = Mock()
Scott Bakerb404b492017-12-01 13:01:10 -080060
61 def tearDown(self):
62 sys.path = self.sys_path_save
63
64 def test_handle_create(self):
Matteo Scandolo2360fd92018-05-29 17:27:51 -070065 with patch.object(VOLTServiceInstancePolicy, "create_eastbound_instance") as create_eastbound_instance, \
66 patch.object(VOLTServiceInstancePolicy, "associate_onu_device") as associate_onu_device:
Scott Bakerb404b492017-12-01 13:01:10 -080067
Matteo Scandolo2360fd92018-05-29 17:27:51 -070068 self.policy.handle_create(self.si)
69 create_eastbound_instance.assert_called_with(self.si)
70 associate_onu_device.assert_called_with(self.si)
Scott Bakerb404b492017-12-01 13:01:10 -080071
72 def test_create_vsg(self):
Matteo Scandolo89f16482018-03-12 16:12:53 -070073 with patch.object(ServiceInstanceLink, "save", autospec=True) as save_link, \
Matteo Scandolo35207b72019-05-10 08:46:48 -070074 patch.object(ServiceInstance, "save", autospec=True) as save_vsg:
Matteo Scandolo89f16482018-03-12 16:12:53 -070075
Scott Baker59b1e072018-08-23 15:59:05 -070076 subscriber_si = Mock()
77
Matteo Scandolo89f16482018-03-12 16:12:53 -070078 link = Mock()
Matteo Scandolo35207b72019-05-10 08:46:48 -070079 link.provider_service.get_service_instance_class_name.return_value = "ServiceInstance"
Scott Bakerc50a3502018-08-27 11:36:36 -070080 link.provider_service.name = "FabricCrossconnect"
Scott Baker59b1e072018-08-23 15:59:05 -070081 link.provider_service.validate_links = Mock(return_value=[])
82 link.provider_service.acquire_service_instance = Mock(return_value=subscriber_si)
83 link.provider_service.leaf_model = link.provider_service
Matteo Scandolo89f16482018-03-12 16:12:53 -070084
85 si = Mock()
Matteo Scandolo89f16482018-03-12 16:12:53 -070086 si.subscribed_links.all.return_value = []
87 si.owner.subscribed_dependencies.all.return_value = [link]
88
89 self.policy.create_eastbound_instance(si)
Scott Bakerb404b492017-12-01 13:01:10 -080090
Scott Baker59b1e072018-08-23 15:59:05 -070091 link.provider_service.validate_links.assert_called_with(si)
92 link.provider_service.acquire_service_instance.assert_called_with(si)
Scott Bakerb404b492017-12-01 13:01:10 -080093
Scott Baker59b1e072018-08-23 15:59:05 -070094 def test_create_vsg_already_exists(self):
95 with patch.object(ServiceInstanceLink, "save", autospec=True) as save_link, \
Matteo Scandolo35207b72019-05-10 08:46:48 -070096 patch.object(ServiceInstance, "save", autospec=True) as save_vsg:
Scott Bakerb404b492017-12-01 13:01:10 -080097
Scott Baker59b1e072018-08-23 15:59:05 -070098 subscriber_si = Mock()
Scott Bakerb404b492017-12-01 13:01:10 -080099
Scott Baker59b1e072018-08-23 15:59:05 -0700100 link = Mock()
Matteo Scandolo35207b72019-05-10 08:46:48 -0700101 link.provider_service.get_service_instance_class_name.return_value = "ServiceInstance"
Scott Baker59b1e072018-08-23 15:59:05 -0700102 link.provider_service.name = "FabricCrossconnect"
103 link.provider_service.validate_links = Mock(return_value=subscriber_si)
104 link.provider_service.acquire_service_instance = Mock()
105 link.provider_service.leaf_model = link.provider_service
106
107 si = Mock()
108 si.subscribed_links.all.return_value = []
109 si.owner.subscribed_dependencies.all.return_value = [link]
110
111 self.policy.create_eastbound_instance(si)
112
113 link.provider_service.validate_links.assert_called_with(si)
114 link.provider_service.acquire_service_instance.assert_not_called()
Scott Bakerb404b492017-12-01 13:01:10 -0800115
Matteo Scandolo2360fd92018-05-29 17:27:51 -0700116 def test_associate_onu(self):
117 with patch.object(ServiceInstance.objects, "get") as get_si, \
118 patch.object(ONUDevice.objects, "get") as get_onu:
119
120 mock_si = Mock()
121 mock_si.get_westbound_service_instance_properties.return_value = "BRCM1234"
122 get_si.return_value = mock_si
123
124 mock_onu = Mock()
125 mock_onu.id = 12
126 get_onu.return_value = mock_onu
127
128 self.policy.associate_onu_device(self.si)
129
130 self.assertEqual(self.si.onu_device_id, mock_onu.id)
Andy Bavier00c573c2019-02-08 16:19:11 -0700131 self.si.save_changed_fields.assert_called()
Matteo Scandolo2360fd92018-05-29 17:27:51 -0700132
Scott Bakerb404b492017-12-01 13:01:10 -0800133 def test_handle_delete(self):
Matteo Scandolo2360fd92018-05-29 17:27:51 -0700134 self.policy.handle_delete(self.si)
Scott Bakerb404b492017-12-01 13:01:10 -0800135 # handle delete does nothing, and should trivially succeed
136
Scott Bakerb404b492017-12-01 13:01:10 -0800137if __name__ == '__main__':
138 unittest.main()
139