blob: f377f8c17132c592049b5b087fbae8bcf03483d9 [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
60 build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto")])
61 import synchronizers.new_base.modelaccessor
62 from pull_olts import OLTDevicePullStep, model_accessor
63
64 # import all class names to globals
65 for (k, v) in model_accessor.all_model_classes.items():
66 globals()[k] = v
67
68 self.sync_step = OLTDevicePullStep
69
70 # mock volt service
71 self.volt_service = Mock()
72 self.volt_service.id = "volt_service_id"
73 self.volt_service.voltha_url = "voltha_url"
74 self.volt_service.voltha_user = "voltha_user"
75 self.volt_service.voltha_pass = "voltha_pass"
76
77 # mock voltha responses
78 self.devices = {
79 "items": [
80 {
81 "id": "test_id",
82 "type": "simulated_olt",
83 "host_and_port": "172.17.0.1:50060",
84 "admin_state": "ENABLED",
85 "oper_status": "ACTIVE"
86 }
87 ]
88 }
89
90 self.logical_devices = {
91 "items": [
92 {
93 "root_device_id": "test_id",
94 "id": "of_id",
95 "datapath_id": "55334486016"
96 }
97 ]
98 }
99
100 def tearDown(self):
101 sys.path = self.sys_path_save
102
103 @requests_mock.Mocker()
104 def test_missing_volt_service(self, m):
105 self.assertFalse(m.called)
106
107 @requests_mock.Mocker()
108 def test_pull(self, m):
109
110 with patch.object(VOLTService.objects, "all") as olt_service_mock, \
111 patch.object(OLTDevice, "save") as mock_save:
112 olt_service_mock.return_value = [self.volt_service]
113
114 m.get("http://voltha_url/api/v1/devices", status_code=200, json=self.devices)
115 m.get("http://voltha_url/api/v1/logical_devices", status_code=200, json=self.logical_devices)
116
117 self.sync_step().pull_records()
118
119 # TODO how to asster this?
120 # self.assertEqual(existing_olt.admin_state, "ENABLED")
121 # self.assertEqual(existing_olt.oper_status, "ACTIVE")
122 # self.assertEqual(existing_olt.volt_service_id, "volt_service_id")
123 # self.assertEqual(existing_olt.device_id, "test_id")
124 # self.assertEqual(existing_olt.of_id, "of_id")
125 # self.assertEqual(existing_olt.dp_id, "of:0000000ce2314000")
126
127 mock_save.assert_called()
128
129 @requests_mock.Mocker()
130 def test_pull_existing(self, m):
131
132 existing_olt = Mock()
133 existing_olt.enacted = 2
134 existing_olt.updated = 1
135
136 with patch.object(VOLTService.objects, "all") as olt_service_mock, \
137 patch.object(OLTDevice.objects, "filter") as mock_get, \
138 patch.object(existing_olt, "save") as mock_save:
139 olt_service_mock.return_value = [self.volt_service]
140 mock_get.return_value = [existing_olt]
141
142 m.get("http://voltha_url/api/v1/devices", status_code=200, json=self.devices)
143 m.get("http://voltha_url/api/v1/logical_devices", status_code=200, json=self.logical_devices)
144
145 self.sync_step().pull_records()
146
147 self.assertEqual(existing_olt.admin_state, "ENABLED")
148 self.assertEqual(existing_olt.oper_status, "ACTIVE")
149 self.assertEqual(existing_olt.volt_service_id, "volt_service_id")
150 self.assertEqual(existing_olt.device_id, "test_id")
151 self.assertEqual(existing_olt.of_id, "of_id")
152 self.assertEqual(existing_olt.dp_id, "of:0000000ce2314000")
153
154 mock_save.assert_called()
155
156 @requests_mock.Mocker()
157 def test_pull_existing_do_not_sync(self, m):
158 existing_olt = Mock()
159 existing_olt.enacted = 1
160 existing_olt.updated = 2
161
162 with patch.object(VOLTService.objects, "all") as olt_service_mock, \
163 patch.object(OLTDevice.objects, "get") as mock_get, \
164 patch.object(existing_olt, "save") as mock_save:
165 olt_service_mock.return_value = [self.volt_service]
166 mock_get.return_value = existing_olt
167
168 m.get("http://voltha_url/api/v1/devices", status_code=200, json=self.devices)
169 m.get("http://voltha_url/api/v1/logical_devices", status_code=200, json=self.logical_devices)
170
171 self.sync_step().pull_records()
172
173 mock_save.assert_not_called()
174
175if __name__ == "__main__":
176 unittest.main()