blob: 3e27dee3c5c98e5c8fcd2b1d3d76e023fb6635d9 [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, \
Scott Bakerb404b492017-12-01 13:01:10 -080084 patch.object(VOLTTenantPolicy, "cleanup_orphans") as cleanup_orphans:
85 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):
90 with patch.object(VOLTTenantPolicy, "get_current_vsg") as get_current_vsg, \
91 patch.object(VOLTTenantPolicy, "create_vsg") as create_vsg, \
92 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):
117 with patch.object(VSGService.objects, "get_items") as vsg_items, \
118 patch.object(ServiceInstanceLink, "save", autospec=True) as save_link, \
119 patch.object(VSGServiceInstance, "save", autospec=True) as save_vsg:
120
121 vsg_items.return_value = [self.vsg_service]
122 self.policy.create_vsg(self.tenant)
123
124 # Should have created a vsg
125
126 self.assertEqual(save_vsg.call_count, 1)
127 vsg = save_vsg.call_args[0][0]
128 self.assertEqual(vsg.creator, self.tenant.creator)
129
130 # Should have created a link from OLT to vsg
131
132 self.assertEqual(save_link.call_count, 1)
133 link = save_link.call_args[0][0]
134 self.assertEqual(link.provider_service_instance, vsg)
135 self.assertEqual(link.subscriber_service_instance, self.tenant)
136
Scott Bakerb404b492017-12-01 13:01:10 -0800137 def test_handle_delete(self):
138 self.policy.handle_delete(self.tenant)
139 # handle delete does nothing, and should trivially succeed
140
141 def test_cleanup_orphans(self):
142 with patch.object(ServiceInstanceLink, "delete", autospec=True) as delete_link, \
143 patch.object(VSGServiceInstance.objects, "get_items") as vsg_si_items, \
144 patch.object(ServiceInstanceLink.objects, "get_items") as link_items:
145
146 vsg1 = VSGServiceInstance(id=123)
147 vsg2 = VSGServiceInstance(id=456)
148 link1 = ServiceInstanceLink(provider_service_instance=vsg1, provider_service_instance_id=vsg1.id,
149 subscriber_service_instance=self.tenant, subscriber_service_instance_id=self.tenant.id)
150 link2 = ServiceInstanceLink(provider_service_instance=vsg2, provider_service_instance_id=vsg2.id,
151 subscriber_service_instance=self.tenant, subscriber_service_instance_id=self.tenant.id)
152
153 self.tenant.subscribed_links=MockObjectList(initial=[link1,link2])
154
155 vsg_si_items.return_value = [vsg1, vsg2]
156 link_items.return_value = [link1, link2]
157
158 self.policy.cleanup_orphans(self.tenant)
159
160 # Since there are two VSGs linked to this VOLT, cleanup_orphans() will have caused one of them to be
161 # deleted.
162
163 self.assertEqual(delete_link.call_count, 1)
164
165if __name__ == '__main__':
166 unittest.main()
167