blob: 314a0c17f7908248264a49e166f1a023a778e7e6 [file] [log] [blame]
Matteo Scandolo0b986e22018-06-04 14:07:33 -07001# 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
15import unittest
16import json
17import functools
18from mock import patch, call, Mock, PropertyMock
19import requests_mock
20
21import os, sys
22
23# Hack to load synchronizer framework
24test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
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")
29sys.path.append(xos_dir)
30sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
31# END Hack to load synchronizer framework
32
33# generate model from xproto
34def get_models_fn(service_name, xproto_name):
35 name = os.path.join(service_name, "xos", xproto_name)
36 if os.path.exists(os.path.join(services_dir, name)):
37 return name
38 else:
39 name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name)
40 if os.path.exists(os.path.join(services_dir, name)):
41 return name
42 raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name))
43# END generate model from xproto
44
45def match_json(desired, req):
46 if desired!=req.json():
47 raise Exception("Got request %s, but body is not matching" % req.url)
48 return False
49 return True
50
51class TestSyncOnosService(unittest.TestCase):
52
53 def setUp(self):
54
55 self.sys_path_save = sys.path
56 sys.path.append(xos_dir)
57 sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
58
59 # Setting up the config module
60 from xosconfig import Config
61 config = os.path.join(test_path, "../test_config.yaml")
62 Config.clear()
63 Config.init(config, "synchronizer-config-schema.yaml")
64 # END Setting up the config module
65
66 from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
67 build_mock_modelaccessor(xos_dir, services_dir, [
68 get_models_fn("onos-service", "onos.xproto")
69 ])
70 import synchronizers.new_base.modelaccessor
71
72 from sync_onos_service import SyncONOSService, model_accessor
73
74 # import all class names to globals
75 for (k, v) in model_accessor.all_model_classes.items():
76 globals()[k] = v
77
78
79 self.sync_step = SyncONOSService
80
81 self.onos = Mock(spec=[
82 'id',
83 'name',
84 "rest_hostname",
85 "rest_port",
86 "rest_username",
87 "rest_password",
88 "class_names"
89 ])
90 self.onos.id = 1
91 self.onos.name = "onos"
92 self.onos.rest_hostname = "onos-url"
93 self.onos.rest_port = "8181"
94 self.onos.rest_username = "karaf"
95 self.onos.rest_password = "karaf"
96 self.onos.class_names = "ONOSService"
97
98 self.service = Mock()
99 self.service.id = 1
100 self.service.serviceattribute_dict = {}
101 self.service.leaf_model = self.onos
102
103 self.onos_service_attribute = Mock(spec=[
104 'id',
105 'service',
106 'name',
107 'value'
108 ])
109 self.onos_service_attribute.service = self.service
110 self.onos_service_attribute.name = "/onos/v1/network/configuration/apps/org.opencord.olt"
111 self.onos_service_attribute.value = {
112 "kafka": {
113 "bootstrapServers": "cord-kafka-kafka.default.svc.cluster.local:9092"
114 }
115 }
116
117 def tearDown(self):
118 self.onos = None
119 sys.path = self.sys_path_save
120
121 @requests_mock.Mocker()
122 def test_sync_no_service_attributes(self, m):
123 with patch.object(Service.objects, "get_items") as service_mock:
124 service_mock.return_value = [self.service]
125 self.sync_step().sync_record(self.onos)
126 self.assertFalse(m.called)
127
128 @requests_mock.Mocker()
129 def test_sync_service_attributes_from_service(self, m):
130 expected_conf = '{"foo": "bar"}'
131
132 self.service.serviceattribute_dict = {
133 '/onos/v1/network/configuration/apps/org.onosproject.olt': expected_conf,
134 '/onos/v1/network/configuration/apps/org.onosproject.dhcp': expected_conf
135 }
136
137 m.post("http://onos-url:8181/onos/v1/network/configuration/apps/org.onosproject.olt",
138 status_code=200,
139 additional_matcher=functools.partial(match_json, json.loads(expected_conf)))
140
141 m.post("http://onos-url:8181/onos/v1/network/configuration/apps/org.onosproject.dhcp",
142 status_code=200,
143 additional_matcher=functools.partial(match_json, json.loads(expected_conf)))
144
145 with patch.object(Service.objects, "get_items") as service_mock:
146 service_mock.return_value = [self.service]
147 self.sync_step().sync_record(self.onos)
148 self.assertTrue(m.called)
149 self.assertEqual(m.call_count, 2)
150
151 @requests_mock.Mocker()
152 def test_sync_service_attributes_from_attribute(self, m):
153 expected_conf = '{"foo": "bar"}'
154 self.service.serviceattribute_dict = {
155 '/onos/v1/network/configuration/apps/org.onosproject.olt': expected_conf,
156 }
157 m.post("http://onos-url:8181/onos/v1/network/configuration/apps/org.onosproject.olt",
158 status_code=200,
159 additional_matcher=functools.partial(match_json, json.loads(expected_conf)))
160
161 with patch.object(Service.objects, "get_items") as service_mock:
162 service_mock.return_value = [self.service]
163 self.sync_step().sync_record(self.onos_service_attribute)
164
165 self.assertTrue(m.called)
166 self.assertEqual(m.call_count, 1)
167
168 @requests_mock.Mocker()
169 def test_sync_service_attributes_err(self, m):
170 expected_conf = '{"foo": "bar"}'
171
172 self.service.serviceattribute_dict = {
173 '/onos/v1/network/configuration/apps/org.onosproject.olt': expected_conf,
174 }
175
176 m.post("http://onos-url:8181/onos/v1/network/configuration/apps/org.onosproject.olt",
177 status_code=500,
178 text="Mock Error",
179 additional_matcher=functools.partial(match_json, json.loads(expected_conf)))
180
181 with self.assertRaises(Exception) as e, \
182 patch.object(Service.objects, "get_items") as service_mock:
183
184 service_mock.return_value = [self.service]
185 self.sync_step().sync_record(self.onos)
186
187 self.assertTrue(m.called)
188 self.assertEqual(m.call_count, 1)
189 self.assertEqual(e.exception.message, "Failed to add config http://onos-url:8181/onos/v1/network/configuration/apps/org.onosproject.olt in ONOS")
190
191 @requests_mock.Mocker()
192 def test_delete(self, m):
193 m.delete("http://onos-url:8181%s" % self.onos_service_attribute.name,
194 status_code=204)
195
196 self.sync_step().delete_record(self.onos_service_attribute)
197 self.assertTrue(m.called)
198 self.assertEqual(m.call_count, 1)