blob: a07c68159cf6956bde33ea78ae226c7ef8c8bd72 [file] [log] [blame]
Matteo Scandoloc6230b42018-02-26 15:27:57 -08001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15
16import unittest
Matteo Scandolocc94e902018-05-22 15:25:25 -070017from mock import patch, Mock
18
Matteo Scandoloc6230b42018-02-26 15:27:57 -080019
20import os, sys
21
Matteo Scandolocc94e902018-05-22 15:25:25 -070022test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
Matteo Scandoloc6230b42018-02-26 15:27:57 -080023
24class TestModelPolicyRCORDSubscriber(unittest.TestCase):
25 def setUp(self):
Scott Baker9d9ddf62018-03-20 20:44:27 -070026
Matteo Scandolocc94e902018-05-22 15:25:25 -070027 self.sys_path_save = sys.path
Matteo Scandoloc6230b42018-02-26 15:27:57 -080028
Matteo Scandolocc94e902018-05-22 15:25:25 -070029 config = os.path.join(test_path, "../test_config.yaml")
30 from xosconfig import Config
31 Config.clear()
32 Config.init(config, 'synchronizer-config-schema.yaml')
33
Scott Baker8b1a8852019-02-04 09:39:17 -080034 from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
35 mock_modelaccessor_config(test_path, [
36 ("../profiles/rcord", "rcord.xproto"),
37 ("olt-service", "volt.xproto")])
Scott Baker9d9ddf62018-03-20 20:44:27 -070038
Scott Baker8b1a8852019-02-04 09:39:17 -080039 import xossynchronizer.modelaccessor
40 import mock_modelaccessor
41 reload(mock_modelaccessor) # in case nose2 loaded it in a previous test
42 reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
43
44 from xossynchronizer.modelaccessor import model_accessor
45 self.model_accessor = model_accessor
46
47 from model_policy_rcordsubscriber import RCORDSubscriberPolicy
Matteo Scandolocc94e902018-05-22 15:25:25 -070048
49 from mock_modelaccessor import MockObjectList
Matteo Scandoloc6230b42018-02-26 15:27:57 -080050
51 # import all class names to globals
52 for (k, v) in model_accessor.all_model_classes.items():
53 globals()[k] = v
54
Matteo Scandolocc94e902018-05-22 15:25:25 -070055 # 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()
Matteo Scandoloc6230b42018-02-26 15:27:57 -080058
Scott Baker8b1a8852019-02-04 09:39:17 -080059 self.policy = RCORDSubscriberPolicy(model_accessor=self.model_accessor)
Matteo Scandolocc94e902018-05-22 15:25:25 -070060 self.si = Mock(name="myTestSubscriber")
Matteo Scandoloc6230b42018-02-26 15:27:57 -080061
62 def tearDown(self):
Matteo Scandolocc94e902018-05-22 15:25:25 -070063 sys.path = self.sys_path_save
Matteo Scandoloc6230b42018-02-26 15:27:57 -080064
Matteo Scandoloc322df52018-06-22 14:43:59 -070065 def test_update_pre_provisione(self):
66 si = self.si
67 si.status = "pre-provisioned"
68 self.policy.handle_create(si)
69
70 with patch.object(VOLTServiceInstance, "save", autospec=True) as save_volt, \
71 patch.object(ServiceInstanceLink, "save", autospec=True) as save_link:
72
73 self.policy.handle_create(si)
74 self.assertEqual(save_link.call_count, 0)
75 self.assertEqual(save_volt.call_count, 0)
76
Matteo Scandoloc6230b42018-02-26 15:27:57 -080077 def test_update_and_do_nothing(self):
Matteo Scandolocc94e902018-05-22 15:25:25 -070078 si = self.si
Matteo Scandoloc6230b42018-02-26 15:27:57 -080079 si.is_new = False
Matteo Scandolocc94e902018-05-22 15:25:25 -070080 si.subscribed_links.all.return_value = ["already", "have", "a", "chain"]
Matteo Scandoloc322df52018-06-22 14:43:59 -070081
82 with patch.object(VOLTServiceInstance, "save", autospec=True) as save_volt, \
83 patch.object(ServiceInstanceLink, "save", autospec=True) as save_link:
84
85 self.policy.handle_create(si)
86 self.assertEqual(save_link.call_count, 0)
87 self.assertEqual(save_volt.call_count, 0)
Matteo Scandoloc6230b42018-02-26 15:27:57 -080088
Matteo Scandoloace9dd72018-08-16 16:12:43 -070089 def test_create_chain(self):
Matteo Scandolocc94e902018-05-22 15:25:25 -070090 volt = Mock()
91 volt.get_service_instance_class_name.return_value = "VOLTServiceInstance"
Matteo Scandoloc6230b42018-02-26 15:27:57 -080092
Matteo Scandolocc94e902018-05-22 15:25:25 -070093 service_dependency = Mock()
94 service_dependency.provider_service = volt
Matteo Scandoloc6230b42018-02-26 15:27:57 -080095
Matteo Scandolocc94e902018-05-22 15:25:25 -070096 si = self.si
97 si.is_new = True
Matteo Scandolofab45ef2018-08-17 16:33:29 -070098 si.status = "enabled"
Matteo Scandolocc94e902018-05-22 15:25:25 -070099 si.subscribed_links.all.return_value = []
100 si.owner.subscribed_dependencies.all.return_value = [service_dependency]
101
102 with patch.object(VOLTServiceInstance, "save", autospec=True) as save_volt, \
Matteo Scandoloc6230b42018-02-26 15:27:57 -0800103 patch.object(ServiceInstanceLink, "save", autospec=True) as save_link:
Matteo Scandolocc94e902018-05-22 15:25:25 -0700104
Matteo Scandoloc6230b42018-02-26 15:27:57 -0800105 self.policy.handle_create(si)
106 self.assertEqual(save_link.call_count, 1)
107 self.assertEqual(save_volt.call_count, 1)
108
Matteo Scandoloace9dd72018-08-16 16:12:43 -0700109 def test_remove_chain(self):
110 volt = VOLTServiceInstance()
111 volt.name = "volt"
112
113 link = ServiceInstanceLink()
114 link.subscriber_service_instance= self.si
115 link.provider_service_instance = volt
116 link.provider_service_instance.leaf_model = volt
117
118
119 si = self.si
120 si.is_new = False
121 si.status = "awaiting-auth"
122 si.subscribed_links.all.return_value = [link]
123
124 with patch.object(VOLTServiceInstance, "delete", autospec=True) as delete_volt, \
125 patch.object(ServiceInstanceLink, "delete", autospec=True) as delete_link:
126
127 self.policy.handle_create(si)
128 self.assertEqual(delete_link.call_count, 1)
129 self.assertEqual(delete_volt.call_count, 1)
130
Matteo Scandoloc6230b42018-02-26 15:27:57 -0800131
132if __name__ == '__main__':
Scott Baker9d9ddf62018-03-20 20:44:27 -0700133 unittest.main()