blob: c6514b7b59995622c437f24eb0c5197357a8f378 [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"),
37 ("vsg", "vsg.xproto"),
38 ("../profiles/rcord", "rcord.xproto"),])
Scott Bakerb404b492017-12-01 13:01:10 -080039
Scott Baker47b47302019-01-30 16:55:07 -080040 import xossynchronizer.modelaccessor
41 import mock_modelaccessor
42 reload(mock_modelaccessor) # in case nose2 loaded it in a previous test
43 reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
44
45 from xossynchronizer.modelaccessor import model_accessor
46 self.model_accessor = model_accessor
Scott Bakerb404b492017-12-01 13:01:10 -080047
48 from mock_modelaccessor import MockObjectList
Scott Baker47b47302019-01-30 16:55:07 -080049 from model_policy_voltserviceinstance import VOLTServiceInstancePolicy
Scott Bakerb404b492017-12-01 13:01:10 -080050
51 # import all class names to globals
52 for (k, v) in model_accessor.all_model_classes.items():
53 globals()[k] = v
54
55 # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to creation of
56 # tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world.
57 model_accessor.reset_all_object_stores()
58
Scott Baker47b47302019-01-30 16:55:07 -080059 self.policy = VOLTServiceInstancePolicy(model_accessor=self.model_accessor)
Matteo Scandolo2360fd92018-05-29 17:27:51 -070060 self.si = Mock()
Scott Bakerb404b492017-12-01 13:01:10 -080061
62 def tearDown(self):
63 sys.path = self.sys_path_save
64
65 def test_handle_create(self):
Matteo Scandolo2360fd92018-05-29 17:27:51 -070066 with patch.object(VOLTServiceInstancePolicy, "create_eastbound_instance") as create_eastbound_instance, \
67 patch.object(VOLTServiceInstancePolicy, "associate_onu_device") as associate_onu_device:
Scott Bakerb404b492017-12-01 13:01:10 -080068
Matteo Scandolo2360fd92018-05-29 17:27:51 -070069 self.policy.handle_create(self.si)
70 create_eastbound_instance.assert_called_with(self.si)
71 associate_onu_device.assert_called_with(self.si)
Scott Bakerb404b492017-12-01 13:01:10 -080072
73 def test_create_vsg(self):
Matteo Scandolo89f16482018-03-12 16:12:53 -070074 with patch.object(ServiceInstanceLink, "save", autospec=True) as save_link, \
Matteo Scandolo2360fd92018-05-29 17:27:51 -070075 patch.object(VSGServiceInstance, "save", autospec=True) as save_vsg:
Matteo Scandolo89f16482018-03-12 16:12:53 -070076
Scott Baker59b1e072018-08-23 15:59:05 -070077 subscriber_si = Mock()
78
Matteo Scandolo89f16482018-03-12 16:12:53 -070079 link = Mock()
80 link.provider_service.get_service_instance_class_name.return_value = "VSGServiceInstance"
Scott Bakerc50a3502018-08-27 11:36:36 -070081 link.provider_service.name = "FabricCrossconnect"
Scott Baker59b1e072018-08-23 15:59:05 -070082 link.provider_service.validate_links = Mock(return_value=[])
83 link.provider_service.acquire_service_instance = Mock(return_value=subscriber_si)
84 link.provider_service.leaf_model = link.provider_service
Matteo Scandolo89f16482018-03-12 16:12:53 -070085
86 si = Mock()
Matteo Scandolo89f16482018-03-12 16:12:53 -070087 si.subscribed_links.all.return_value = []
88 si.owner.subscribed_dependencies.all.return_value = [link]
89
90 self.policy.create_eastbound_instance(si)
Scott Bakerb404b492017-12-01 13:01:10 -080091
Scott Baker59b1e072018-08-23 15:59:05 -070092 link.provider_service.validate_links.assert_called_with(si)
93 link.provider_service.acquire_service_instance.assert_called_with(si)
Scott Bakerb404b492017-12-01 13:01:10 -080094
Scott Baker59b1e072018-08-23 15:59:05 -070095 def test_create_vsg_already_exists(self):
96 with patch.object(ServiceInstanceLink, "save", autospec=True) as save_link, \
97 patch.object(VSGServiceInstance, "save", autospec=True) as save_vsg:
Scott Bakerb404b492017-12-01 13:01:10 -080098
Scott Baker59b1e072018-08-23 15:59:05 -070099 subscriber_si = Mock()
Scott Bakerb404b492017-12-01 13:01:10 -0800100
Scott Baker59b1e072018-08-23 15:59:05 -0700101 link = Mock()
102 link.provider_service.get_service_instance_class_name.return_value = "VSGServiceInstance"
103 link.provider_service.name = "FabricCrossconnect"
104 link.provider_service.validate_links = Mock(return_value=subscriber_si)
105 link.provider_service.acquire_service_instance = Mock()
106 link.provider_service.leaf_model = link.provider_service
107
108 si = Mock()
109 si.subscribed_links.all.return_value = []
110 si.owner.subscribed_dependencies.all.return_value = [link]
111
112 self.policy.create_eastbound_instance(si)
113
114 link.provider_service.validate_links.assert_called_with(si)
115 link.provider_service.acquire_service_instance.assert_not_called()
Scott Bakerb404b492017-12-01 13:01:10 -0800116
Matteo Scandolo2360fd92018-05-29 17:27:51 -0700117 def test_associate_onu(self):
118 with patch.object(ServiceInstance.objects, "get") as get_si, \
119 patch.object(ONUDevice.objects, "get") as get_onu:
120
121 mock_si = Mock()
122 mock_si.get_westbound_service_instance_properties.return_value = "BRCM1234"
123 get_si.return_value = mock_si
124
125 mock_onu = Mock()
126 mock_onu.id = 12
127 get_onu.return_value = mock_onu
128
129 self.policy.associate_onu_device(self.si)
130
131 self.assertEqual(self.si.onu_device_id, mock_onu.id)
132 self.si.save.assert_called()
133
Scott Bakerb404b492017-12-01 13:01:10 -0800134 def test_handle_delete(self):
Matteo Scandolo2360fd92018-05-29 17:27:51 -0700135 self.policy.handle_delete(self.si)
Scott Bakerb404b492017-12-01 13:01:10 -0800136 # handle delete does nothing, and should trivially succeed
137
Scott Bakerb404b492017-12-01 13:01:10 -0800138if __name__ == '__main__':
139 unittest.main()
140