blob: 7a41c1d596faa375493d352152bb5c57e12d8568 [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"),
39 ("vsg", "vsg.xproto"),
Matteo Scandoloe04a4882019-02-14 10:22:24 -080040 ("rcord", "rcord.xproto"), ])
Matteo Scandolo19466a02018-05-16 17:43:39 -070041
Scott Baker47b47302019-01-30 16:55:07 -080042 import xossynchronizer.modelaccessor
43 reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
44
45 from xossynchronizer.modelaccessor import model_accessor
46 self.model_accessor = model_accessor
47
48 from xossynchronizer.steps.syncstep import DeferredException
Matteo Scandolof6337eb2018-04-05 15:58:37 -070049 from sync_volt_service_instance import SyncVOLTServiceInstance, model_accessor
50
51 # import all class names to globals
52 for (k, v) in model_accessor.all_model_classes.items():
53 globals()[k] = v
54
55 self.sync_step = SyncVOLTServiceInstance
56
Matteo Scandolof6337eb2018-04-05 15:58:37 -070057 volt_service = Mock()
Luca Preteca974c82018-05-01 18:06:16 -070058 volt_service.onos_voltha_url = "onos_voltha_url"
59 volt_service.onos_voltha_port = 4321
60 volt_service.onos_voltha_user = "onos_voltha_user"
61 volt_service.onos_voltha_pass = "onos_voltha_pass"
Matteo Scandolof6337eb2018-04-05 15:58:37 -070062
Matteo Scandolod8ed60e2018-06-18 17:00:57 -070063 uni_port = Mock()
64 uni_port.port_no = "uni_port_id"
65
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070066 onu_device = Mock()
67 onu_device.name = "BRCM1234"
68 onu_device.pon_port.olt_device.dp_id = None
69 onu_device.pon_port.olt_device.name = "Test OLT Device"
Matteo Scandolod8ed60e2018-06-18 17:00:57 -070070 onu_device.uni_ports.first.return_value = uni_port
71
72 # create a mock service instance
73 o = Mock()
Matteo Scandolo80912942018-07-25 20:51:30 -070074 o.policy_code = 1
Matteo Scandolod8ed60e2018-06-18 17:00:57 -070075 o.id = 1
76 o.owner_id = "volt_service"
77 o.onu_device = onu_device
78 o.tologdict.return_value = {}
Matteo Scandolof6337eb2018-04-05 15:58:37 -070079
80 self.o = o
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070081 self.onu_device = onu_device
Matteo Scandolof6337eb2018-04-05 15:58:37 -070082 self.volt_service = volt_service
83
84 def tearDown(self):
85 self.o = None
86 sys.path = self.sys_path_save
87
Matteo Scandolof6337eb2018-04-05 15:58:37 -070088 @requests_mock.Mocker()
89 def test_do_not_sync(self, m):
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070090 self.onu_device.pon_port.olt_device.dp_id = None
Matteo Scandolof6337eb2018-04-05 15:58:37 -070091
Matteo Scandolo25ad2902018-08-14 17:02:05 -070092 with patch.object(VOLTService.objects, "get") as olt_service_mock:
Matteo Scandolof6337eb2018-04-05 15:58:37 -070093 olt_service_mock.return_value = self.volt_service
94
95 with self.assertRaises(DeferredException) as e:
Scott Baker47b47302019-01-30 16:55:07 -080096 self.sync_step(model_accessor=self.model_accessor).sync_record(self.o)
Matteo Scandolof6337eb2018-04-05 15:58:37 -070097
98 self.assertFalse(m.called)
99 self.assertEqual(e.exception.message, "Waiting for OLTDevice Test OLT Device to be synchronized")
100
101 @requests_mock.Mocker()
102 def test_do_sync(self, m):
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700103
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -0700104 self.onu_device.pon_port.olt_device.dp_id = "of:dp_id"
Matteo Scandolo80912942018-07-25 20:51:30 -0700105
Matteo Scandolo25ad2902018-08-14 17:02:05 -0700106 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 -0700107
Matteo Scandolo25ad2902018-08-14 17:02:05 -0700108 with patch.object(VOLTService.objects, "get") as olt_service_mock:
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700109 olt_service_mock.return_value = self.volt_service
110
Scott Baker47b47302019-01-30 16:55:07 -0800111 self.sync_step(model_accessor=self.model_accessor).sync_record(self.o)
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700112 self.assertTrue(m.called)
Matteo Scandolo18358822018-08-15 17:17:43 -0700113 self.assertEqual(self.o.backend_handle, "of:dp_id/uni_port_id")
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700114
115 @requests_mock.Mocker()
116 def test_do_sync_fail(self, m):
Matteo Scandolo80912942018-07-25 20:51:30 -0700117
Matteo Scandolo25ad2902018-08-14 17:02:05 -0700118 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 -0700119
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -0700120 self.onu_device.pon_port.olt_device.dp_id = "of:dp_id"
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700121
Matteo Scandolo25ad2902018-08-14 17:02:05 -0700122 with patch.object(VOLTService.objects, "get") as olt_service_mock:
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700123 olt_service_mock.return_value = self.volt_service
124
125 with self.assertRaises(Exception) as e:
Scott Baker47b47302019-01-30 16:55:07 -0800126 self.sync_step(model_accessor=self.model_accessor).sync_record(self.o)
Luca Preteca974c82018-05-01 18:06:16 -0700127 self.assertTrue(m.called)
128 self.assertEqual(e.exception.message, "Failed to add subscriber in onos voltha: Mock Error")
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700129
Matteo Scandoloe48a4642018-06-26 10:08:34 -0700130 @requests_mock.Mocker()
131 def test_delete(self, m):
132 m.delete("http://onos_voltha_url:4321/onos/olt/oltapp/of:dp_id/uni_port_id", status_code=204)
133
134 self.onu_device.pon_port.olt_device.dp_id = "of:dp_id"
Matteo Scandolo18358822018-08-15 17:17:43 -0700135 self.o.backend_handle = "of:dp_id/uni_port_id"
Matteo Scandoloe48a4642018-06-26 10:08:34 -0700136
137 with patch.object(VOLTService.objects, "get") as olt_service_mock:
138 olt_service_mock.return_value = self.volt_service
139
Scott Baker47b47302019-01-30 16:55:07 -0800140 self.sync_step(model_accessor=self.model_accessor).delete_record(self.o)
Matteo Scandoloe48a4642018-06-26 10:08:34 -0700141 self.assertTrue(m.called)
142 self.assertEqual(m.call_count, 1)
143
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700144if __name__ == "__main__":
Luca Preteca974c82018-05-01 18:06:16 -0700145 unittest.main()