blob: 8bbdf6f632ab80c810b4f14408a08487a5d62c1c [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
Matteo Scandolof6337eb2018-04-05 15:58:37 -070022test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
Matteo Scandolof6337eb2018-04-05 15:58:37 -070023
Matteo Scandolod44ca992018-05-17 15:02:10 -070024class TestSyncVOLTServiceInstance(unittest.TestCase):
Matteo Scandolof6337eb2018-04-05 15:58:37 -070025 def setUp(self):
26 global DeferredException
27
28 self.sys_path_save = sys.path
Matteo Scandolof6337eb2018-04-05 15:58:37 -070029
30 # Setting up the config module
31 from xosconfig import Config
Matteo Scandolof7ebb112018-09-18 16:17:22 -070032 config = os.path.join(test_path, "../test_config.yaml")
Matteo Scandolof6337eb2018-04-05 15:58:37 -070033 Config.clear()
34 Config.init(config, "synchronizer-config-schema.yaml")
35 # END Setting up the config module
36
Scott Baker47b47302019-01-30 16:55:07 -080037 from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
38 mock_modelaccessor_config(test_path, [("olt-service", "volt.xproto"),
Matteo Scandolo35207b72019-05-10 08:46:48 -070039 ("rcord", "rcord.xproto")])
Matteo Scandolo19466a02018-05-16 17:43:39 -070040
Scott Baker47b47302019-01-30 16:55:07 -080041 import xossynchronizer.modelaccessor
42 reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
43
44 from xossynchronizer.modelaccessor import model_accessor
45 self.model_accessor = model_accessor
46
47 from xossynchronizer.steps.syncstep import DeferredException
Matteo Scandolof6337eb2018-04-05 15:58:37 -070048 from sync_volt_service_instance import SyncVOLTServiceInstance, model_accessor
49
50 # import all class names to globals
51 for (k, v) in model_accessor.all_model_classes.items():
52 globals()[k] = v
53
54 self.sync_step = SyncVOLTServiceInstance
55
Matteo Scandolo0d756f22019-06-10 14:55:55 -070056 # create a mock ONOS Service
57 onos = Mock()
58 onos.name = "ONOS"
59 onos.leaf_model.rest_hostname = "onos_voltha_url"
60 onos.leaf_model.rest_port = 4321
61 onos.leaf_model.rest_username = "onos_voltha_user"
62 onos.leaf_model.rest_password = "onos_voltha_pass"
63
Matteo Scandolof6337eb2018-04-05 15:58:37 -070064 volt_service = Mock()
Matteo Scandolo0d756f22019-06-10 14:55:55 -070065 volt_service.provider_services = [onos]
Matteo Scandolof6337eb2018-04-05 15:58:37 -070066
Matteo Scandolod8ed60e2018-06-18 17:00:57 -070067 uni_port = Mock()
68 uni_port.port_no = "uni_port_id"
69
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070070 onu_device = Mock()
71 onu_device.name = "BRCM1234"
72 onu_device.pon_port.olt_device.dp_id = None
73 onu_device.pon_port.olt_device.name = "Test OLT Device"
Matteo Scandolod8ed60e2018-06-18 17:00:57 -070074 onu_device.uni_ports.first.return_value = uni_port
75
76 # create a mock service instance
77 o = Mock()
Matteo Scandolo80912942018-07-25 20:51:30 -070078 o.policy_code = 1
Matteo Scandolod8ed60e2018-06-18 17:00:57 -070079 o.id = 1
80 o.owner_id = "volt_service"
81 o.onu_device = onu_device
82 o.tologdict.return_value = {}
Matteo Scandolof6337eb2018-04-05 15:58:37 -070083
84 self.o = o
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070085 self.onu_device = onu_device
Matteo Scandolof6337eb2018-04-05 15:58:37 -070086 self.volt_service = volt_service
87
88 def tearDown(self):
89 self.o = None
90 sys.path = self.sys_path_save
91
Matteo Scandolof6337eb2018-04-05 15:58:37 -070092 @requests_mock.Mocker()
93 def test_do_not_sync(self, m):
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070094 self.onu_device.pon_port.olt_device.dp_id = None
Matteo Scandolof6337eb2018-04-05 15:58:37 -070095
Matteo Scandolo25ad2902018-08-14 17:02:05 -070096 with patch.object(VOLTService.objects, "get") as olt_service_mock:
Matteo Scandolof6337eb2018-04-05 15:58:37 -070097 olt_service_mock.return_value = self.volt_service
98
99 with self.assertRaises(DeferredException) as e:
Scott Baker47b47302019-01-30 16:55:07 -0800100 self.sync_step(model_accessor=self.model_accessor).sync_record(self.o)
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700101
102 self.assertFalse(m.called)
103 self.assertEqual(e.exception.message, "Waiting for OLTDevice Test OLT Device to be synchronized")
104
105 @requests_mock.Mocker()
106 def test_do_sync(self, m):
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700107
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -0700108 self.onu_device.pon_port.olt_device.dp_id = "of:dp_id"
Matteo Scandolo80912942018-07-25 20:51:30 -0700109
Matteo Scandolo25ad2902018-08-14 17:02:05 -0700110 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 -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
Scott Baker47b47302019-01-30 16:55:07 -0800115 self.sync_step(model_accessor=self.model_accessor).sync_record(self.o)
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700116 self.assertTrue(m.called)
Matteo Scandolo18358822018-08-15 17:17:43 -0700117 self.assertEqual(self.o.backend_handle, "of:dp_id/uni_port_id")
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700118
119 @requests_mock.Mocker()
120 def test_do_sync_fail(self, m):
Matteo Scandolo80912942018-07-25 20:51:30 -0700121
Matteo Scandolo25ad2902018-08-14 17:02:05 -0700122 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 -0700123
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -0700124 self.onu_device.pon_port.olt_device.dp_id = "of:dp_id"
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700125
Matteo Scandolo25ad2902018-08-14 17:02:05 -0700126 with patch.object(VOLTService.objects, "get") as olt_service_mock:
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700127 olt_service_mock.return_value = self.volt_service
128
129 with self.assertRaises(Exception) as e:
Scott Baker47b47302019-01-30 16:55:07 -0800130 self.sync_step(model_accessor=self.model_accessor).sync_record(self.o)
Luca Preteca974c82018-05-01 18:06:16 -0700131 self.assertTrue(m.called)
132 self.assertEqual(e.exception.message, "Failed to add subscriber in onos voltha: Mock Error")
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700133
Matteo Scandoloe48a4642018-06-26 10:08:34 -0700134 @requests_mock.Mocker()
135 def test_delete(self, m):
136 m.delete("http://onos_voltha_url:4321/onos/olt/oltapp/of:dp_id/uni_port_id", status_code=204)
137
138 self.onu_device.pon_port.olt_device.dp_id = "of:dp_id"
Matteo Scandolo18358822018-08-15 17:17:43 -0700139 self.o.backend_handle = "of:dp_id/uni_port_id"
Matteo Scandoloe48a4642018-06-26 10:08:34 -0700140
141 with patch.object(VOLTService.objects, "get") as olt_service_mock:
142 olt_service_mock.return_value = self.volt_service
143
Scott Baker47b47302019-01-30 16:55:07 -0800144 self.sync_step(model_accessor=self.model_accessor).delete_record(self.o)
Matteo Scandoloe48a4642018-06-26 10:08:34 -0700145 self.assertTrue(m.called)
146 self.assertEqual(m.call_count, 1)
147
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700148if __name__ == "__main__":
Luca Preteca974c82018-05-01 18:06:16 -0700149 unittest.main()