blob: b3edf3fe6586f354dc2e0e0df0539ccd4593e1d3 [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
19import mock
20
21import os, sys
22
23test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
24service_dir=os.path.join(test_path, "../../../..")
25xos_dir=os.path.join(test_path, "../../..")
26if 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 Baker27e4fcb2018-01-09 10:24:52 -080030# While transitioning from static to dynamic load, the path to find neighboring xproto files has changed. So check
31# both possible locations...
32def 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 Bakerb404b492017-12-01 13:01:10 -080042class TestModelPolicyVOLTTenant(unittest.TestCase):
43 def setUp(self):
44 global VOLTTenantPolicy, 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 Baker27e4fcb2018-01-09 10:24:52 -080056 build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto"),
57 get_models_fn("vsg", "vsg.xproto"),
58 get_models_fn("../profiles/rcord", "rcord.xproto")])
Scott Bakerb404b492017-12-01 13:01:10 -080059
60 import synchronizers.new_base.modelaccessor
61 import model_policy_volttenant
62 from model_policy_volttenant import VOLTTenantPolicy, 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 self.policy = VOLTTenantPolicy()
75 self.tenant = VOLTTenant(s_tag=111, c_tag=222, service_specific_id=1234)
76
77 self.vsg_service = VSGService(name="the vsg service")
78
79 def tearDown(self):
80 sys.path = self.sys_path_save
81
82 def test_handle_create(self):
83 with patch.object(VOLTTenantPolicy, "manage_vsg") as manage_vsg, \
84 patch.object(VOLTTenantPolicy, "manage_subscriber") as manage_subscriber, \
85 patch.object(VOLTTenantPolicy, "cleanup_orphans") as cleanup_orphans:
86 self.policy.handle_create(self.tenant)
87 manage_subscriber.assert_called_with(self.tenant)
88 manage_vsg.assert_called_with(self.tenant)
89 cleanup_orphans.assert_called_with(self.tenant)
90
91 def test_manage_vsg(self):
92 with patch.object(VOLTTenantPolicy, "get_current_vsg") as get_current_vsg, \
93 patch.object(VOLTTenantPolicy, "create_vsg") as create_vsg, \
94 patch.object(VSGService.objects, "get_items") as vsg_items:
95
96 vsg_items.return_value = [self.vsg_service]
97 get_current_vsg.return_value = None
98 self.policy.manage_vsg(self.tenant)
99
100 create_vsg.assert_called()
101
102 def test_get_current_vsg(self):
103 with patch.object(ServiceInstanceLink.objects, "get_items") as link_items:
104 vsg = VSGServiceInstance()
105 link = ServiceInstanceLink(provider_service_instance=vsg, subscriber_service_instance_id=self.tenant.id)
106
107 link_items.return_value = [link]
108
109 vsg = self.policy.get_current_vsg(self.tenant)
110
111 self.assertNotEqual(vsg, None)
112
113 def test_get_current_vsg_noexist(self):
114 vsg = self.policy.get_current_vsg(self.tenant)
115
116 self.assertEqual(vsg, None)
117
118 def test_create_vsg(self):
119 with patch.object(VSGService.objects, "get_items") as vsg_items, \
120 patch.object(ServiceInstanceLink, "save", autospec=True) as save_link, \
121 patch.object(VSGServiceInstance, "save", autospec=True) as save_vsg:
122
123 vsg_items.return_value = [self.vsg_service]
124 self.policy.create_vsg(self.tenant)
125
126 # Should have created a vsg
127
128 self.assertEqual(save_vsg.call_count, 1)
129 vsg = save_vsg.call_args[0][0]
130 self.assertEqual(vsg.creator, self.tenant.creator)
131
132 # Should have created a link from OLT to vsg
133
134 self.assertEqual(save_link.call_count, 1)
135 link = save_link.call_args[0][0]
136 self.assertEqual(link.provider_service_instance, vsg)
137 self.assertEqual(link.subscriber_service_instance, self.tenant)
138
139 def test_manage_subscriber(self):
140 with patch.object(ServiceInstanceLink, "save", autospec=True) as save_link, \
141 patch.object(CordSubscriberRoot, "save", autospec=True) as save_csr:
142
143 self.tenant.provided_links = MockObjectList()
144
145 self.policy.manage_subscriber(self.tenant)
146
147 self.assertEqual(save_csr.call_count, 1)
148 csr = save_csr.call_args[0][0]
149
150 self.assertEqual(save_link.call_count, 1)
151 link = save_link.call_args[0][0]
152 self.assertEqual(link.provider_service_instance, self.tenant)
153 self.assertEqual(link.subscriber_service_instance, csr)
154
155 def test_manage_subscriber_exists(self):
156 with patch.object(ServiceInstanceLink, "save", autospec=True) as save_link, \
157 patch.object(CordSubscriberRoot, "save", autospec=True) as save_csr, \
158 patch.object(CordSubscriberRoot.objects, "get_items") as csr_items, \
159 patch.object(ServiceInstanceLink.objects, "get_items") as link_items:
160 self.tenant.provided_links = MockObjectList()
161
162 subscriber = CordSubscriberRoot(service_specific_id=1234)
163 csr_items.return_value = [subscriber]
164
165 link = ServiceInstanceLink(provider_service_instance= self.tenant, subscriber_service_instance = subscriber)
166 link_items.return_value = [link]
167
168 self.tenant.provided_links = MockObjectList(initial=[link])
169
170 self.policy.manage_subscriber(self.tenant)
171
172 self.assertEqual(save_csr.call_count, 0)
173 self.assertEqual(save_link.call_count, 0)
174
175 def test_manage_subscriber_exists_nolink(self):
176 with patch.object(ServiceInstanceLink, "save", autospec=True) as save_link, \
177 patch.object(CordSubscriberRoot, "save", autospec=True) as save_csr, \
178 patch.object(CordSubscriberRoot.objects, "get_items") as csr_items, \
179 patch.object(ServiceInstanceLink.objects, "get_items") as link_items:
180 self.tenant.provided_links = MockObjectList()
181
182 subscriber = CordSubscriberRoot(service_specific_id=1234)
183 csr_items.return_value = [subscriber]
184
185 self.tenant.provided_links = MockObjectList()
186
187 self.policy.manage_subscriber(self.tenant)
188
189 self.assertEqual(save_csr.call_count, 0)
190
191 self.assertEqual(save_link.call_count, 1)
192 link = save_link.call_args[0][0]
193 self.assertEqual(link.provider_service_instance, self.tenant)
194 self.assertEqual(link.subscriber_service_instance, subscriber)
195
196 def test_handle_delete(self):
197 self.policy.handle_delete(self.tenant)
198 # handle delete does nothing, and should trivially succeed
199
200 def test_cleanup_orphans(self):
201 with patch.object(ServiceInstanceLink, "delete", autospec=True) as delete_link, \
202 patch.object(VSGServiceInstance.objects, "get_items") as vsg_si_items, \
203 patch.object(ServiceInstanceLink.objects, "get_items") as link_items:
204
205 vsg1 = VSGServiceInstance(id=123)
206 vsg2 = VSGServiceInstance(id=456)
207 link1 = ServiceInstanceLink(provider_service_instance=vsg1, provider_service_instance_id=vsg1.id,
208 subscriber_service_instance=self.tenant, subscriber_service_instance_id=self.tenant.id)
209 link2 = ServiceInstanceLink(provider_service_instance=vsg2, provider_service_instance_id=vsg2.id,
210 subscriber_service_instance=self.tenant, subscriber_service_instance_id=self.tenant.id)
211
212 self.tenant.subscribed_links=MockObjectList(initial=[link1,link2])
213
214 vsg_si_items.return_value = [vsg1, vsg2]
215 link_items.return_value = [link1, link2]
216
217 self.policy.cleanup_orphans(self.tenant)
218
219 # Since there are two VSGs linked to this VOLT, cleanup_orphans() will have caused one of them to be
220 # deleted.
221
222 self.assertEqual(delete_link.call_count, 1)
223
224if __name__ == '__main__':
225 unittest.main()
226