blob: a0ef017120f8a96e0d96f709b4513d1c8e7d73d8 [file] [log] [blame]
Matteo Scandolo33523412018-04-12 15:21:13 -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
16from mock import patch, call, Mock, PropertyMock
17import requests_mock
18
19import os, sys
20
21# Hack to load synchronizer framework
22test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
23xos_dir=os.path.join(test_path, "../../..")
24if not os.path.exists(os.path.join(test_path, "new_base")):
25 xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos")
26 services_dir = os.path.join(xos_dir, "../../xos_services")
27sys.path.append(xos_dir)
28sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
29# END Hack to load synchronizer framework
30
31# generate model from xproto
32def get_models_fn(service_name, xproto_name):
33 name = os.path.join(service_name, "xos", xproto_name)
34 if os.path.exists(os.path.join(services_dir, name)):
35 return name
36 else:
37 name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name)
38 if os.path.exists(os.path.join(services_dir, name)):
39 return name
40 raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name))
41# END generate model from xproto
42
43class TestSyncOLTDevice(unittest.TestCase):
44
45 def setUp(self):
46 global DeferredException
47
48 self.sys_path_save = sys.path
49 sys.path.append(xos_dir)
50 sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
51
52 # Setting up the config module
53 from xosconfig import Config
54 config = os.path.join(test_path, "../model_policies/test_config.yaml")
55 Config.clear()
56 Config.init(config, "synchronizer-config-schema.yaml")
57 # END Setting up the config module
58
59 from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
Matteo Scandolo19466a02018-05-16 17:43:39 -070060 # build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto")])
61
62 # FIXME this is to get jenkins to pass the tests, somehow it is running tests in a different order
63 # and apparently it is not overriding the generated model accessor
64 build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto"),
65 get_models_fn("vsg", "vsg.xproto"),
66 get_models_fn("../profiles/rcord", "rcord.xproto")])
Matteo Scandolo33523412018-04-12 15:21:13 -070067 import synchronizers.new_base.modelaccessor
68 from pull_olts import OLTDevicePullStep, model_accessor
69
70 # import all class names to globals
71 for (k, v) in model_accessor.all_model_classes.items():
72 globals()[k] = v
73
74 self.sync_step = OLTDevicePullStep
75
76 # mock volt service
77 self.volt_service = Mock()
78 self.volt_service.id = "volt_service_id"
79 self.volt_service.voltha_url = "voltha_url"
80 self.volt_service.voltha_user = "voltha_user"
81 self.volt_service.voltha_pass = "voltha_pass"
Matteo Scandolo19466a02018-05-16 17:43:39 -070082 self.volt_service.voltha_port = 1234
Matteo Scandolo33523412018-04-12 15:21:13 -070083
84 # mock voltha responses
85 self.devices = {
86 "items": [
87 {
88 "id": "test_id",
89 "type": "simulated_olt",
90 "host_and_port": "172.17.0.1:50060",
91 "admin_state": "ENABLED",
92 "oper_status": "ACTIVE"
93 }
94 ]
95 }
96
97 self.logical_devices = {
98 "items": [
99 {
100 "root_device_id": "test_id",
101 "id": "of_id",
102 "datapath_id": "55334486016"
103 }
104 ]
105 }
106
107 def tearDown(self):
108 sys.path = self.sys_path_save
109
110 @requests_mock.Mocker()
111 def test_missing_volt_service(self, m):
112 self.assertFalse(m.called)
113
114 @requests_mock.Mocker()
115 def test_pull(self, m):
116
117 with patch.object(VOLTService.objects, "all") as olt_service_mock, \
118 patch.object(OLTDevice, "save") as mock_save:
119 olt_service_mock.return_value = [self.volt_service]
120
Matteo Scandolo19466a02018-05-16 17:43:39 -0700121 m.get("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.devices)
122 m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=self.logical_devices)
Matteo Scandolo33523412018-04-12 15:21:13 -0700123
124 self.sync_step().pull_records()
125
126 # TODO how to asster this?
127 # self.assertEqual(existing_olt.admin_state, "ENABLED")
128 # self.assertEqual(existing_olt.oper_status, "ACTIVE")
129 # self.assertEqual(existing_olt.volt_service_id, "volt_service_id")
130 # self.assertEqual(existing_olt.device_id, "test_id")
131 # self.assertEqual(existing_olt.of_id, "of_id")
132 # self.assertEqual(existing_olt.dp_id, "of:0000000ce2314000")
133
134 mock_save.assert_called()
135
136 @requests_mock.Mocker()
137 def test_pull_existing(self, m):
138
139 existing_olt = Mock()
140 existing_olt.enacted = 2
141 existing_olt.updated = 1
142
143 with patch.object(VOLTService.objects, "all") as olt_service_mock, \
144 patch.object(OLTDevice.objects, "filter") as mock_get, \
145 patch.object(existing_olt, "save") as mock_save:
146 olt_service_mock.return_value = [self.volt_service]
147 mock_get.return_value = [existing_olt]
148
Matteo Scandolo19466a02018-05-16 17:43:39 -0700149 m.get("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.devices)
150 m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=self.logical_devices)
Matteo Scandolo33523412018-04-12 15:21:13 -0700151
152 self.sync_step().pull_records()
153
154 self.assertEqual(existing_olt.admin_state, "ENABLED")
155 self.assertEqual(existing_olt.oper_status, "ACTIVE")
156 self.assertEqual(existing_olt.volt_service_id, "volt_service_id")
157 self.assertEqual(existing_olt.device_id, "test_id")
158 self.assertEqual(existing_olt.of_id, "of_id")
159 self.assertEqual(existing_olt.dp_id, "of:0000000ce2314000")
160
161 mock_save.assert_called()
162
163 @requests_mock.Mocker()
164 def test_pull_existing_do_not_sync(self, m):
165 existing_olt = Mock()
166 existing_olt.enacted = 1
167 existing_olt.updated = 2
168
169 with patch.object(VOLTService.objects, "all") as olt_service_mock, \
170 patch.object(OLTDevice.objects, "get") as mock_get, \
171 patch.object(existing_olt, "save") as mock_save:
172 olt_service_mock.return_value = [self.volt_service]
173 mock_get.return_value = existing_olt
174
Matteo Scandolo19466a02018-05-16 17:43:39 -0700175 m.get("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.devices)
176 m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=self.logical_devices)
Matteo Scandolo33523412018-04-12 15:21:13 -0700177
178 self.sync_step().pull_records()
179
180 mock_save.assert_not_called()
181
182if __name__ == "__main__":
183 unittest.main()