blob: c880e2f02cdfd11989e7dc214b55fecf41e2651e [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"))
Scott Baker9d9ddf62018-03-20 20:44:27 -070025services_dir=os.path.join(xos_dir, "../../xos_services")
Matteo Scandoloc6230b42018-02-26 15:27:57 -080026config_file = os.path.join(cwd, "test_config.yaml")
27
28# NOTE this have to start for xos_services
Scott Baker9d9ddf62018-03-20 20:44:27 -070029RCORD_XPROTO = "../profiles/rcord/xos/synchronizer/models/rcord.xproto"
Matteo Scandoloc6230b42018-02-26 15:27:57 -080030
31Config.clear()
32Config.init(config_file, 'synchronizer-config-schema.yaml')
33
34# FIXME move the synchronizer framework into a library
35sys.path.append(xos_dir)
Scott Baker9d9ddf62018-03-20 20:44:27 -070036from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
Matteo Scandoloc6230b42018-02-26 15:27:57 -080037
38class MockSubscriber:
39
40 def __init__(self, *args, **kwargs):
41 self.name = kwargs['name']
42 self.is_new = True
43 self.id = 1
44
45 def subscribed_links(self):
46 pass
47
48class MockAll:
49 @staticmethod
50 def all():
51 pass
52
53class MockService:
54
55 def __init__(self):
56 self.subscribed_dependencies = MockAll
57
58
59class MockModel:
60 def __init__(self):
61 self.model_name = "VOLTService"
62
63class MockLeaf:
64 def __init__(self):
65 self.leaf_model = MockModel()
66
67class MockLink:
68 def __init__(self):
69 self.provider_service = MockLeaf()
70
71class TestModelPolicyRCORDSubscriber(unittest.TestCase):
72 def setUp(self):
Scott Baker9d9ddf62018-03-20 20:44:27 -070073 global model_accessor
74
Matteo Scandoloc6230b42018-02-26 15:27:57 -080075 self.original_sys_path = sys.path
76
77 # Generate a fake model accessor (emulate the client library)
Matteo Scandolo5f894a12018-03-21 12:47:24 -070078 build_mock_modelaccessor(xos_dir, services_dir, [RCORD_XPROTO])
Scott Baker9d9ddf62018-03-20 20:44:27 -070079
80 import synchronizers.new_base.modelaccessor
81 from synchronizers.new_base.modelaccessor import model_accessor
82 from model_policy_rcordsubscriber import RCORDSubscriberPolicy
Matteo Scandoloc6230b42018-02-26 15:27:57 -080083
84 # import all class names to globals
85 for (k, v) in model_accessor.all_model_classes.items():
86 globals()[k] = v
87
88 # model_accessor.reset_all_object_stores()
89
90 # create base objects for testing
91 self.policy = RCORDSubscriberPolicy()
92
93 def tearDown(self):
94 sys.path = self.original_sys_path
95
96 @patch.object(MockSubscriber, 'subscribed_links', MockAll)
97 @patch.object(MockAll, 'all', MagicMock(return_value=['foo']))
98 def test_update_and_do_nothing(self):
99 si = MockSubscriber(name="myTestSubscriber")
100 si.is_new = False
101 self.policy.handle_create(si)
102 # NOTE assert that no models are created
103
104 @patch.object(MockSubscriber, 'subscribed_links', MockAll)
105 @patch.object(MockAll, 'all', MagicMock(return_value=[MockLink]))
106 def test_create(self):
107 si = MockSubscriber(name="myTestSubscriber")
108 owner = MockService()
109
110 si.owner = owner
111
112 with patch.object(owner.subscribed_dependencies, 'all', MagicMock(return_value=[MockLink()])), \
Matteo Scandolo6f5c8962018-03-01 13:40:49 -0800113 patch.object(VOLTServiceInstance, "save", autospec=True) as save_volt, \
Matteo Scandoloc6230b42018-02-26 15:27:57 -0800114 patch.object(ServiceInstanceLink, "save", autospec=True) as save_link:
115 self.policy.handle_create(si)
116 self.assertEqual(save_link.call_count, 1)
117 self.assertEqual(save_volt.call_count, 1)
118
119
120if __name__ == '__main__':
Scott Baker9d9ddf62018-03-20 20:44:27 -0700121 unittest.main()