blob: 895a2549bcef45a3c88df580b222b201a2a46860 [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__)))
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")
Matteo Scandoloc6230b42018-02-26 15:27:57 -080028
Matteo Scandolocc94e902018-05-22 15:25:25 -070029# 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))
Matteo Scandoloc6230b42018-02-26 15:27:57 -080040
41class TestModelPolicyRCORDSubscriber(unittest.TestCase):
42 def setUp(self):
Scott Baker9d9ddf62018-03-20 20:44:27 -070043
Matteo Scandolocc94e902018-05-22 15:25:25 -070044 self.sys_path_save = sys.path
45 sys.path.append(xos_dir)
46 sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
Matteo Scandoloc6230b42018-02-26 15:27:57 -080047
Matteo Scandolocc94e902018-05-22 15:25:25 -070048 config = os.path.join(test_path, "../test_config.yaml")
49 from xosconfig import Config
50 Config.clear()
51 Config.init(config, 'synchronizer-config-schema.yaml')
52
53 from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
54 build_mock_modelaccessor(xos_dir, services_dir, [
55 get_models_fn("../profiles/rcord", "rcord.xproto"),
56 get_models_fn("olt-service", "volt.xproto") # in test create we spy on VOLTServiceInstance
57 ])
Scott Baker9d9ddf62018-03-20 20:44:27 -070058
59 import synchronizers.new_base.modelaccessor
Matteo Scandolocc94e902018-05-22 15:25:25 -070060 from model_policy_rcordsubscriber import RCORDSubscriberPolicy, model_accessor
61
62 from mock_modelaccessor import MockObjectList
Matteo Scandoloc6230b42018-02-26 15:27:57 -080063
64 # import all class names to globals
65 for (k, v) in model_accessor.all_model_classes.items():
66 globals()[k] = v
67
Matteo Scandolocc94e902018-05-22 15:25:25 -070068 # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to creation of
69 # tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world.
70 model_accessor.reset_all_object_stores()
Matteo Scandoloc6230b42018-02-26 15:27:57 -080071
Matteo Scandoloc6230b42018-02-26 15:27:57 -080072 self.policy = RCORDSubscriberPolicy()
Matteo Scandolocc94e902018-05-22 15:25:25 -070073 self.si = Mock(name="myTestSubscriber")
Matteo Scandoloc6230b42018-02-26 15:27:57 -080074
75 def tearDown(self):
Matteo Scandolocc94e902018-05-22 15:25:25 -070076 sys.path = self.sys_path_save
Matteo Scandoloc6230b42018-02-26 15:27:57 -080077
Matteo Scandoloc322df52018-06-22 14:43:59 -070078 def test_update_pre_provisione(self):
79 si = self.si
80 si.status = "pre-provisioned"
81 self.policy.handle_create(si)
82
83 with patch.object(VOLTServiceInstance, "save", autospec=True) as save_volt, \
84 patch.object(ServiceInstanceLink, "save", autospec=True) as save_link:
85
86 self.policy.handle_create(si)
87 self.assertEqual(save_link.call_count, 0)
88 self.assertEqual(save_volt.call_count, 0)
89
Matteo Scandoloc6230b42018-02-26 15:27:57 -080090 def test_update_and_do_nothing(self):
Matteo Scandolocc94e902018-05-22 15:25:25 -070091 si = self.si
Matteo Scandoloc6230b42018-02-26 15:27:57 -080092 si.is_new = False
Matteo Scandolocc94e902018-05-22 15:25:25 -070093 si.subscribed_links.all.return_value = ["already", "have", "a", "chain"]
Matteo Scandoloc322df52018-06-22 14:43:59 -070094
95 with patch.object(VOLTServiceInstance, "save", autospec=True) as save_volt, \
96 patch.object(ServiceInstanceLink, "save", autospec=True) as save_link:
97
98 self.policy.handle_create(si)
99 self.assertEqual(save_link.call_count, 0)
100 self.assertEqual(save_volt.call_count, 0)
Matteo Scandoloc6230b42018-02-26 15:27:57 -0800101
Matteo Scandoloc6230b42018-02-26 15:27:57 -0800102 def test_create(self):
Matteo Scandolocc94e902018-05-22 15:25:25 -0700103 volt = Mock()
104 volt.get_service_instance_class_name.return_value = "VOLTServiceInstance"
Matteo Scandoloc6230b42018-02-26 15:27:57 -0800105
Matteo Scandolocc94e902018-05-22 15:25:25 -0700106 service_dependency = Mock()
107 service_dependency.provider_service = volt
Matteo Scandoloc6230b42018-02-26 15:27:57 -0800108
Matteo Scandolocc94e902018-05-22 15:25:25 -0700109 si = self.si
110 si.is_new = True
111 si.subscribed_links.all.return_value = []
112 si.owner.subscribed_dependencies.all.return_value = [service_dependency]
113
114 with patch.object(VOLTServiceInstance, "save", autospec=True) as save_volt, \
Matteo Scandoloc6230b42018-02-26 15:27:57 -0800115 patch.object(ServiceInstanceLink, "save", autospec=True) as save_link:
Matteo Scandolocc94e902018-05-22 15:25:25 -0700116
Matteo Scandoloc6230b42018-02-26 15:27:57 -0800117 self.policy.handle_create(si)
118 self.assertEqual(save_link.call_count, 1)
119 self.assertEqual(save_volt.call_count, 1)
120
121
122if __name__ == '__main__':
Scott Baker9d9ddf62018-03-20 20:44:27 -0700123 unittest.main()