blob: 5488b156c2a8c24b0ace6e97bdabe4832b19a48e [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__)))
23service_dir=os.path.join(test_path, "../../../..")
24xos_dir=os.path.join(test_path, "../../..")
25if not os.path.exists(os.path.join(test_path, "new_base")):
26 xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos")
27 services_dir=os.path.join(xos_dir, "../../xos_services")
28
Scott Baker27e4fcb2018-01-09 10:24:52 -080029# While transitioning from static to dynamic load, the path to find neighboring xproto files has changed. So check
30# both possible locations...
31def get_models_fn(service_name, xproto_name):
32 name = os.path.join(service_name, "xos", xproto_name)
33 if os.path.exists(os.path.join(services_dir, name)):
34 return name
35 else:
36 name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name)
37 if os.path.exists(os.path.join(services_dir, name)):
38 return name
39 raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name))
40
Matteo Scandolod2458012018-03-01 13:40:36 -080041class TestModelPolicyVOLTServiceInstance(unittest.TestCase):
Scott Bakerb404b492017-12-01 13:01:10 -080042 def setUp(self):
Matteo Scandolod2458012018-03-01 13:40:36 -080043 global VOLTServiceInstancePolicy, MockObjectList
Scott Bakerb404b492017-12-01 13:01:10 -080044
45 self.sys_path_save = sys.path
46 sys.path.append(xos_dir)
47 sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
48
49 config = os.path.join(test_path, "test_config.yaml")
50 from xosconfig import Config
51 Config.clear()
52 Config.init(config, 'synchronizer-config-schema.yaml')
53
54 from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
Scott Baker27e4fcb2018-01-09 10:24:52 -080055 build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto"),
56 get_models_fn("vsg", "vsg.xproto"),
57 get_models_fn("../profiles/rcord", "rcord.xproto")])
Scott Bakerb404b492017-12-01 13:01:10 -080058
59 import synchronizers.new_base.modelaccessor
Matteo Scandolod2458012018-03-01 13:40:36 -080060 import model_policy_voltserviceinstance
61 from model_policy_voltserviceinstance import VOLTServiceInstancePolicy, model_accessor
Scott Bakerb404b492017-12-01 13:01:10 -080062
63 from mock_modelaccessor import MockObjectList
64
65 # import all class names to globals
66 for (k, v) in model_accessor.all_model_classes.items():
67 globals()[k] = v
68
69 # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to creation of
70 # tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world.
71 model_accessor.reset_all_object_stores()
72
Matteo Scandolod2458012018-03-01 13:40:36 -080073 self.policy = VOLTServiceInstancePolicy()
74 self.tenant = VOLTServiceInstance(s_tag=111, c_tag=222, service_specific_id=1234)
Scott Bakerb404b492017-12-01 13:01:10 -080075
76 self.vsg_service = VSGService(name="the vsg service")
77
78 def tearDown(self):
79 sys.path = self.sys_path_save
80
81 def test_handle_create(self):
Matteo Scandolod2458012018-03-01 13:40:36 -080082 with patch.object(VOLTServiceInstancePolicy, "manage_vsg") as manage_vsg, \
83 patch.object(VOLTServiceInstancePolicy, "cleanup_orphans") as cleanup_orphans:
Scott Bakerb404b492017-12-01 13:01:10 -080084 self.policy.handle_create(self.tenant)
Scott Bakerb404b492017-12-01 13:01:10 -080085 manage_vsg.assert_called_with(self.tenant)
86 cleanup_orphans.assert_called_with(self.tenant)
87
88 def test_manage_vsg(self):
Matteo Scandolod2458012018-03-01 13:40:36 -080089 with patch.object(VOLTServiceInstancePolicy, "get_current_vsg") as get_current_vsg, \
Matteo Scandolo89f16482018-03-12 16:12:53 -070090 patch.object(VOLTServiceInstancePolicy, "create_eastbound_instance") as create_vsg, \
Scott Bakerb404b492017-12-01 13:01:10 -080091 patch.object(VSGService.objects, "get_items") as vsg_items:
92
93 vsg_items.return_value = [self.vsg_service]
94 get_current_vsg.return_value = None
95 self.policy.manage_vsg(self.tenant)
96
97 create_vsg.assert_called()
98
99 def test_get_current_vsg(self):
100 with patch.object(ServiceInstanceLink.objects, "get_items") as link_items:
101 vsg = VSGServiceInstance()
102 link = ServiceInstanceLink(provider_service_instance=vsg, subscriber_service_instance_id=self.tenant.id)
103
104 link_items.return_value = [link]
105
106 vsg = self.policy.get_current_vsg(self.tenant)
107
108 self.assertNotEqual(vsg, None)
109
110 def test_get_current_vsg_noexist(self):
111 vsg = self.policy.get_current_vsg(self.tenant)
112
113 self.assertEqual(vsg, None)
114
115 def test_create_vsg(self):
Matteo Scandolo89f16482018-03-12 16:12:53 -0700116 # with patch.object(model_accessor, "get_model_class") as mock_model_accessor, \
117 with patch.object(ServiceInstanceLink, "save", autospec=True) as save_link, \
Scott Bakerb404b492017-12-01 13:01:10 -0800118 patch.object(VSGServiceInstance, "save", autospec=True) as save_vsg:
119
Matteo Scandolo89f16482018-03-12 16:12:53 -0700120 # mock_model_accessor.return_value = VSGServiceInstance
121
122 link = Mock()
123 link.provider_service.get_service_instance_class_name.return_value = "VSGServiceInstance"
124
125 si = Mock()
Matteo Scandolo89f16482018-03-12 16:12:53 -0700126 si.subscribed_links.all.return_value = []
127 si.owner.subscribed_dependencies.all.return_value = [link]
128
129 self.policy.create_eastbound_instance(si)
Scott Bakerb404b492017-12-01 13:01:10 -0800130
131 # Should have created a vsg
132
133 self.assertEqual(save_vsg.call_count, 1)
134 vsg = save_vsg.call_args[0][0]
Scott Bakerb404b492017-12-01 13:01:10 -0800135
136 # Should have created a link from OLT to vsg
137
138 self.assertEqual(save_link.call_count, 1)
139 link = save_link.call_args[0][0]
140 self.assertEqual(link.provider_service_instance, vsg)
Matteo Scandolo89f16482018-03-12 16:12:53 -0700141 self.assertEqual(link.subscriber_service_instance, si)
Scott Bakerb404b492017-12-01 13:01:10 -0800142
Scott Bakerb404b492017-12-01 13:01:10 -0800143 def test_handle_delete(self):
144 self.policy.handle_delete(self.tenant)
145 # handle delete does nothing, and should trivially succeed
146
147 def test_cleanup_orphans(self):
148 with patch.object(ServiceInstanceLink, "delete", autospec=True) as delete_link, \
149 patch.object(VSGServiceInstance.objects, "get_items") as vsg_si_items, \
150 patch.object(ServiceInstanceLink.objects, "get_items") as link_items:
151
152 vsg1 = VSGServiceInstance(id=123)
153 vsg2 = VSGServiceInstance(id=456)
154 link1 = ServiceInstanceLink(provider_service_instance=vsg1, provider_service_instance_id=vsg1.id,
155 subscriber_service_instance=self.tenant, subscriber_service_instance_id=self.tenant.id)
156 link2 = ServiceInstanceLink(provider_service_instance=vsg2, provider_service_instance_id=vsg2.id,
157 subscriber_service_instance=self.tenant, subscriber_service_instance_id=self.tenant.id)
158
159 self.tenant.subscribed_links=MockObjectList(initial=[link1,link2])
160
161 vsg_si_items.return_value = [vsg1, vsg2]
162 link_items.return_value = [link1, link2]
163
164 self.policy.cleanup_orphans(self.tenant)
165
166 # Since there are two VSGs linked to this VOLT, cleanup_orphans() will have caused one of them to be
167 # deleted.
168
169 self.assertEqual(delete_link.call_count, 1)
170
171if __name__ == '__main__':
172 unittest.main()
173