blob: efc946d22cbb52b849e49c7fc1a07ece9c346d5c [file] [log] [blame]
Matteo Scandolof6337eb2018-04-05 15:58:37 -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
Matteo Scandolo80912942018-07-25 20:51:30 -070016import functools
Matteo Scandolof6337eb2018-04-05 15:58:37 -070017from mock import patch, call, Mock, PropertyMock
18import requests_mock
19
20import os, sys
21
22# Hack to load synchronizer framework
23test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
24xos_dir=os.path.join(test_path, "../../..")
25if not os.path.exists(os.path.join(test_path, "new_base")):
26 xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos")
27 services_dir = os.path.join(xos_dir, "../../xos_services")
28sys.path.append(xos_dir)
29sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
30# END Hack to load synchronizer framework
31
32# generate model from xproto
33def get_models_fn(service_name, xproto_name):
34 name = os.path.join(service_name, "xos", xproto_name)
35 if os.path.exists(os.path.join(services_dir, name)):
36 return name
37 else:
38 name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name)
39 if os.path.exists(os.path.join(services_dir, name)):
40 return name
41 raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name))
42# END generate model from xproto
43
Matteo Scandolod44ca992018-05-17 15:02:10 -070044class TestSyncVOLTServiceInstance(unittest.TestCase):
Matteo Scandolof6337eb2018-04-05 15:58:37 -070045 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
Matteo Scandolof6337eb2018-04-05 15:58:37 -070059 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 Scandolof6337eb2018-04-05 15:58:37 -070067 import synchronizers.new_base.modelaccessor
Scott Baker8699e7b2018-07-05 13:11:14 -070068 from synchronizers.new_base.syncstep import DeferredException
Matteo Scandolof6337eb2018-04-05 15:58:37 -070069 from sync_volt_service_instance import SyncVOLTServiceInstance, model_accessor
70
71 # import all class names to globals
72 for (k, v) in model_accessor.all_model_classes.items():
73 globals()[k] = v
74
75 self.sync_step = SyncVOLTServiceInstance
76
Matteo Scandolof6337eb2018-04-05 15:58:37 -070077 volt_service = Mock()
Luca Preteca974c82018-05-01 18:06:16 -070078 volt_service.onos_voltha_url = "onos_voltha_url"
79 volt_service.onos_voltha_port = 4321
80 volt_service.onos_voltha_user = "onos_voltha_user"
81 volt_service.onos_voltha_pass = "onos_voltha_pass"
Matteo Scandolof6337eb2018-04-05 15:58:37 -070082
Matteo Scandolod8ed60e2018-06-18 17:00:57 -070083 uni_port = Mock()
84 uni_port.port_no = "uni_port_id"
85
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070086 onu_device = Mock()
87 onu_device.name = "BRCM1234"
88 onu_device.pon_port.olt_device.dp_id = None
89 onu_device.pon_port.olt_device.name = "Test OLT Device"
Matteo Scandolod8ed60e2018-06-18 17:00:57 -070090 onu_device.uni_ports.first.return_value = uni_port
91
92 # create a mock service instance
93 o = Mock()
Matteo Scandolo80912942018-07-25 20:51:30 -070094 o.policy_code = 1
Matteo Scandolod8ed60e2018-06-18 17:00:57 -070095 o.id = 1
96 o.owner_id = "volt_service"
97 o.onu_device = onu_device
98 o.tologdict.return_value = {}
Matteo Scandolof6337eb2018-04-05 15:58:37 -070099
100 self.o = o
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -0700101 self.onu_device = onu_device
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700102 self.volt_service = volt_service
103
104 def tearDown(self):
105 self.o = None
106 sys.path = self.sys_path_save
107
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700108 @requests_mock.Mocker()
109 def test_do_not_sync(self, m):
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -0700110 self.onu_device.pon_port.olt_device.dp_id = None
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700111
Matteo Scandolo25ad2902018-08-14 17:02:05 -0700112 with patch.object(VOLTService.objects, "get") as olt_service_mock:
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700113 olt_service_mock.return_value = self.volt_service
114
115 with self.assertRaises(DeferredException) as e:
116 self.sync_step().sync_record(self.o)
117
118 self.assertFalse(m.called)
119 self.assertEqual(e.exception.message, "Waiting for OLTDevice Test OLT Device to be synchronized")
120
121 @requests_mock.Mocker()
122 def test_do_sync(self, m):
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700123
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -0700124 self.onu_device.pon_port.olt_device.dp_id = "of:dp_id"
Matteo Scandolo80912942018-07-25 20:51:30 -0700125
Matteo Scandolo25ad2902018-08-14 17:02:05 -0700126 m.post("http://onos_voltha_url:4321/onos/olt/oltapp/of:dp_id/uni_port_id", status_code=200, json={})
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700127
Matteo Scandolo25ad2902018-08-14 17:02:05 -0700128 with patch.object(VOLTService.objects, "get") as olt_service_mock:
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700129 olt_service_mock.return_value = self.volt_service
130
131 self.sync_step().sync_record(self.o)
132 self.assertTrue(m.called)
Matteo Scandolo18358822018-08-15 17:17:43 -0700133 self.assertEqual(self.o.backend_handle, "of:dp_id/uni_port_id")
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700134
135 @requests_mock.Mocker()
136 def test_do_sync_fail(self, m):
Matteo Scandolo80912942018-07-25 20:51:30 -0700137
Matteo Scandolo25ad2902018-08-14 17:02:05 -0700138 m.post("http://onos_voltha_url:4321/onos/olt/oltapp/of:dp_id/uni_port_id", status_code=500, text="Mock Error")
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700139
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -0700140 self.onu_device.pon_port.olt_device.dp_id = "of:dp_id"
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700141
Matteo Scandolo25ad2902018-08-14 17:02:05 -0700142 with patch.object(VOLTService.objects, "get") as olt_service_mock:
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700143 olt_service_mock.return_value = self.volt_service
144
145 with self.assertRaises(Exception) as e:
146 self.sync_step().sync_record(self.o)
Luca Preteca974c82018-05-01 18:06:16 -0700147 self.assertTrue(m.called)
148 self.assertEqual(e.exception.message, "Failed to add subscriber in onos voltha: Mock Error")
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700149
Matteo Scandoloe48a4642018-06-26 10:08:34 -0700150 @requests_mock.Mocker()
151 def test_delete(self, m):
152 m.delete("http://onos_voltha_url:4321/onos/olt/oltapp/of:dp_id/uni_port_id", status_code=204)
153
154 self.onu_device.pon_port.olt_device.dp_id = "of:dp_id"
Matteo Scandolo18358822018-08-15 17:17:43 -0700155 self.o.backend_handle = "of:dp_id/uni_port_id"
Matteo Scandoloe48a4642018-06-26 10:08:34 -0700156
157 with patch.object(VOLTService.objects, "get") as olt_service_mock:
158 olt_service_mock.return_value = self.volt_service
159
160 self.sync_step().delete_record(self.o)
161 self.assertTrue(m.called)
162 self.assertEqual(m.call_count, 1)
163
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700164if __name__ == "__main__":
Luca Preteca974c82018-05-01 18:06:16 -0700165 unittest.main()