blob: ea88667159eff7b0eecdd31f11034057f56a401d [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
17from mock import patch, MagicMock
18import mock
19from xosconfig import Config
20
21import os, sys
22
23cwd=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
24xos_dir=os.path.abspath(os.path.join(cwd, "../../../../../../orchestration/xos/xos"))
25# services_dir=os.path.join(xos_dir, "../../xos_services")
26config_file = os.path.join(cwd, "test_config.yaml")
27
28# NOTE this have to start for xos_services
29# RCORD_XPROTO = "../profiles/rcord/xos/synchronizer/models/rcord.xproto"
30# OLT_XPROTO = "olt-service/xos/synchronizer/models/volt.xproto"
31
32Config.clear()
33Config.init(config_file, 'synchronizer-config-schema.yaml')
34
35# FIXME move the synchronizer framework into a library
36sys.path.append(xos_dir)
37# from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
38from synchronizers.new_base.modelaccessor import model_accessor
39
40from model_policy_rcordsubscriber import RCORDSubscriberPolicy
41
42class MockSubscriber:
43
44 def __init__(self, *args, **kwargs):
45 self.name = kwargs['name']
46 self.is_new = True
47 self.id = 1
48
49 def subscribed_links(self):
50 pass
51
52class MockAll:
53 @staticmethod
54 def all():
55 pass
56
57class MockService:
58
59 def __init__(self):
60 self.subscribed_dependencies = MockAll
61
62
63class MockModel:
64 def __init__(self):
65 self.model_name = "VOLTService"
66
67class MockLeaf:
68 def __init__(self):
69 self.leaf_model = MockModel()
70
71class MockLink:
72 def __init__(self):
73 self.provider_service = MockLeaf()
74
75class TestModelPolicyRCORDSubscriber(unittest.TestCase):
76 def setUp(self):
77 self.original_sys_path = sys.path
78
79 # Generate a fake model accessor (emulate the client library)
80 # build_mock_modelaccessor(xos_dir, services_dir, [RCORD_XPROTO, OLT_XPROTO])
81
82 # import all class names to globals
83 for (k, v) in model_accessor.all_model_classes.items():
84 globals()[k] = v
85
86 # model_accessor.reset_all_object_stores()
87
88 # create base objects for testing
89 self.policy = RCORDSubscriberPolicy()
90
91 def tearDown(self):
92 sys.path = self.original_sys_path
93
94 @patch.object(MockSubscriber, 'subscribed_links', MockAll)
95 @patch.object(MockAll, 'all', MagicMock(return_value=['foo']))
96 def test_update_and_do_nothing(self):
97 si = MockSubscriber(name="myTestSubscriber")
98 si.is_new = False
99 self.policy.handle_create(si)
100 # NOTE assert that no models are created
101
102 @patch.object(MockSubscriber, 'subscribed_links', MockAll)
103 @patch.object(MockAll, 'all', MagicMock(return_value=[MockLink]))
104 def test_create(self):
105 si = MockSubscriber(name="myTestSubscriber")
106 owner = MockService()
107
108 si.owner = owner
109
110 with patch.object(owner.subscribed_dependencies, 'all', MagicMock(return_value=[MockLink()])), \
111 patch.object(VOLTTenant, "save", autospec=True) as save_volt, \
112 patch.object(ServiceInstanceLink, "save", autospec=True) as save_link:
113 self.policy.handle_create(si)
114 self.assertEqual(save_link.call_count, 1)
115 self.assertEqual(save_volt.call_count, 1)
116
117
118if __name__ == '__main__':
119 unittest.main()