blob: 6de02090b002f42c4c92ef7038ef40dc789de007 [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
Matteo Scandolod2458012018-03-01 13:40:36 -080042class TestModelPolicyVOLTServiceInstance(unittest.TestCase):
Scott Bakerb404b492017-12-01 13:01:10 -080043 def setUp(self):
Matteo Scandolod2458012018-03-01 13:40:36 -080044 global VOLTServiceInstancePolicy, MockObjectList
Scott Bakerb404b492017-12-01 13:01:10 -080045
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
Matteo Scandolod2458012018-03-01 13:40:36 -080061 import model_policy_voltserviceinstance
62 from model_policy_voltserviceinstance import VOLTServiceInstancePolicy, model_accessor
Scott Bakerb404b492017-12-01 13:01:10 -080063
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
Matteo Scandolod2458012018-03-01 13:40:36 -080074 self.policy = VOLTServiceInstancePolicy()
75 self.tenant = VOLTServiceInstance(s_tag=111, c_tag=222, service_specific_id=1234)
Scott Bakerb404b492017-12-01 13:01:10 -080076
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):
Matteo Scandolod2458012018-03-01 13:40:36 -080083 with patch.object(VOLTServiceInstancePolicy, "manage_vsg") as manage_vsg, \
84 patch.object(VOLTServiceInstancePolicy, "cleanup_orphans") as cleanup_orphans:
Scott Bakerb404b492017-12-01 13:01:10 -080085 self.policy.handle_create(self.tenant)
Scott Bakerb404b492017-12-01 13:01:10 -080086 manage_vsg.assert_called_with(self.tenant)
87 cleanup_orphans.assert_called_with(self.tenant)
88
89 def test_manage_vsg(self):
Matteo Scandolod2458012018-03-01 13:40:36 -080090 with patch.object(VOLTServiceInstancePolicy, "get_current_vsg") as get_current_vsg, \
Matteo Scandolo89f16482018-03-12 16:12:53 -070091 patch.object(VOLTServiceInstancePolicy, "create_eastbound_instance") as create_vsg, \
Scott Bakerb404b492017-12-01 13:01:10 -080092 patch.object(VSGService.objects, "get_items") as vsg_items:
93
94 vsg_items.return_value = [self.vsg_service]
95 get_current_vsg.return_value = None
96 self.policy.manage_vsg(self.tenant)
97
98 create_vsg.assert_called()
99
100 def test_get_current_vsg(self):
101 with patch.object(ServiceInstanceLink.objects, "get_items") as link_items:
102 vsg = VSGServiceInstance()
103 link = ServiceInstanceLink(provider_service_instance=vsg, subscriber_service_instance_id=self.tenant.id)
104
105 link_items.return_value = [link]
106
107 vsg = self.policy.get_current_vsg(self.tenant)
108
109 self.assertNotEqual(vsg, None)
110
111 def test_get_current_vsg_noexist(self):
112 vsg = self.policy.get_current_vsg(self.tenant)
113
114 self.assertEqual(vsg, None)
115
116 def test_create_vsg(self):
Matteo Scandolo89f16482018-03-12 16:12:53 -0700117 # with patch.object(model_accessor, "get_model_class") as mock_model_accessor, \
118 with patch.object(ServiceInstanceLink, "save", autospec=True) as save_link, \
Scott Bakerb404b492017-12-01 13:01:10 -0800119 patch.object(VSGServiceInstance, "save", autospec=True) as save_vsg:
120
Matteo Scandolo89f16482018-03-12 16:12:53 -0700121 # mock_model_accessor.return_value = VSGServiceInstance
122
123 link = Mock()
124 link.provider_service.get_service_instance_class_name.return_value = "VSGServiceInstance"
125
126 si = Mock()
Matteo Scandolo89f16482018-03-12 16:12:53 -0700127 si.subscribed_links.all.return_value = []
128 si.owner.subscribed_dependencies.all.return_value = [link]
129
130 self.policy.create_eastbound_instance(si)
Scott Bakerb404b492017-12-01 13:01:10 -0800131
132 # Should have created a vsg
133
134 self.assertEqual(save_vsg.call_count, 1)
135 vsg = save_vsg.call_args[0][0]
Scott Bakerb404b492017-12-01 13:01:10 -0800136
137 # Should have created a link from OLT to vsg
138
139 self.assertEqual(save_link.call_count, 1)
140 link = save_link.call_args[0][0]
141 self.assertEqual(link.provider_service_instance, vsg)
Matteo Scandolo89f16482018-03-12 16:12:53 -0700142 self.assertEqual(link.subscriber_service_instance, si)
Scott Bakerb404b492017-12-01 13:01:10 -0800143
Scott Bakerb404b492017-12-01 13:01:10 -0800144 def test_handle_delete(self):
145 self.policy.handle_delete(self.tenant)
146 # handle delete does nothing, and should trivially succeed
147
148 def test_cleanup_orphans(self):
149 with patch.object(ServiceInstanceLink, "delete", autospec=True) as delete_link, \
150 patch.object(VSGServiceInstance.objects, "get_items") as vsg_si_items, \
151 patch.object(ServiceInstanceLink.objects, "get_items") as link_items:
152
153 vsg1 = VSGServiceInstance(id=123)
154 vsg2 = VSGServiceInstance(id=456)
155 link1 = ServiceInstanceLink(provider_service_instance=vsg1, provider_service_instance_id=vsg1.id,
156 subscriber_service_instance=self.tenant, subscriber_service_instance_id=self.tenant.id)
157 link2 = ServiceInstanceLink(provider_service_instance=vsg2, provider_service_instance_id=vsg2.id,
158 subscriber_service_instance=self.tenant, subscriber_service_instance_id=self.tenant.id)
159
160 self.tenant.subscribed_links=MockObjectList(initial=[link1,link2])
161
162 vsg_si_items.return_value = [vsg1, vsg2]
163 link_items.return_value = [link1, link2]
164
165 self.policy.cleanup_orphans(self.tenant)
166
167 # Since there are two VSGs linked to this VOLT, cleanup_orphans() will have caused one of them to be
168 # deleted.
169
170 self.assertEqual(delete_link.call_count, 1)
171
172if __name__ == '__main__':
173 unittest.main()
174