blob: 492b4fcb981ef7edcff219be89cfb6dd7d1398cb [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 Scandolof6337eb2018-04-05 15:58:37 -070056 volt_service = Mock()
Luca Preteca974c82018-05-01 18:06:16 -070057 volt_service.onos_voltha_url = "onos_voltha_url"
58 volt_service.onos_voltha_port = 4321
59 volt_service.onos_voltha_user = "onos_voltha_user"
60 volt_service.onos_voltha_pass = "onos_voltha_pass"
Matteo Scandolof6337eb2018-04-05 15:58:37 -070061
Matteo Scandolod8ed60e2018-06-18 17:00:57 -070062 uni_port = Mock()
63 uni_port.port_no = "uni_port_id"
64
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070065 onu_device = Mock()
66 onu_device.name = "BRCM1234"
67 onu_device.pon_port.olt_device.dp_id = None
68 onu_device.pon_port.olt_device.name = "Test OLT Device"
Matteo Scandolod8ed60e2018-06-18 17:00:57 -070069 onu_device.uni_ports.first.return_value = uni_port
70
71 # create a mock service instance
72 o = Mock()
Matteo Scandolo80912942018-07-25 20:51:30 -070073 o.policy_code = 1
Matteo Scandolod8ed60e2018-06-18 17:00:57 -070074 o.id = 1
75 o.owner_id = "volt_service"
76 o.onu_device = onu_device
77 o.tologdict.return_value = {}
Matteo Scandolof6337eb2018-04-05 15:58:37 -070078
79 self.o = o
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070080 self.onu_device = onu_device
Matteo Scandolof6337eb2018-04-05 15:58:37 -070081 self.volt_service = volt_service
82
83 def tearDown(self):
84 self.o = None
85 sys.path = self.sys_path_save
86
Matteo Scandolof6337eb2018-04-05 15:58:37 -070087 @requests_mock.Mocker()
88 def test_do_not_sync(self, m):
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070089 self.onu_device.pon_port.olt_device.dp_id = None
Matteo Scandolof6337eb2018-04-05 15:58:37 -070090
Matteo Scandolo25ad2902018-08-14 17:02:05 -070091 with patch.object(VOLTService.objects, "get") as olt_service_mock:
Matteo Scandolof6337eb2018-04-05 15:58:37 -070092 olt_service_mock.return_value = self.volt_service
93
94 with self.assertRaises(DeferredException) as e:
Scott Baker47b47302019-01-30 16:55:07 -080095 self.sync_step(model_accessor=self.model_accessor).sync_record(self.o)
Matteo Scandolof6337eb2018-04-05 15:58:37 -070096
97 self.assertFalse(m.called)
98 self.assertEqual(e.exception.message, "Waiting for OLTDevice Test OLT Device to be synchronized")
99
100 @requests_mock.Mocker()
101 def test_do_sync(self, m):
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700102
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -0700103 self.onu_device.pon_port.olt_device.dp_id = "of:dp_id"
Matteo Scandolo80912942018-07-25 20:51:30 -0700104
Matteo Scandolo25ad2902018-08-14 17:02:05 -0700105 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 -0700106
Matteo Scandolo25ad2902018-08-14 17:02:05 -0700107 with patch.object(VOLTService.objects, "get") as olt_service_mock:
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700108 olt_service_mock.return_value = self.volt_service
109
Scott Baker47b47302019-01-30 16:55:07 -0800110 self.sync_step(model_accessor=self.model_accessor).sync_record(self.o)
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700111 self.assertTrue(m.called)
Matteo Scandolo18358822018-08-15 17:17:43 -0700112 self.assertEqual(self.o.backend_handle, "of:dp_id/uni_port_id")
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700113
114 @requests_mock.Mocker()
115 def test_do_sync_fail(self, m):
Matteo Scandolo80912942018-07-25 20:51:30 -0700116
Matteo Scandolo25ad2902018-08-14 17:02:05 -0700117 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 -0700118
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -0700119 self.onu_device.pon_port.olt_device.dp_id = "of:dp_id"
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700120
Matteo Scandolo25ad2902018-08-14 17:02:05 -0700121 with patch.object(VOLTService.objects, "get") as olt_service_mock:
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700122 olt_service_mock.return_value = self.volt_service
123
124 with self.assertRaises(Exception) as e:
Scott Baker47b47302019-01-30 16:55:07 -0800125 self.sync_step(model_accessor=self.model_accessor).sync_record(self.o)
Luca Preteca974c82018-05-01 18:06:16 -0700126 self.assertTrue(m.called)
127 self.assertEqual(e.exception.message, "Failed to add subscriber in onos voltha: Mock Error")
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700128
Matteo Scandoloe48a4642018-06-26 10:08:34 -0700129 @requests_mock.Mocker()
130 def test_delete(self, m):
131 m.delete("http://onos_voltha_url:4321/onos/olt/oltapp/of:dp_id/uni_port_id", status_code=204)
132
133 self.onu_device.pon_port.olt_device.dp_id = "of:dp_id"
Matteo Scandolo18358822018-08-15 17:17:43 -0700134 self.o.backend_handle = "of:dp_id/uni_port_id"
Matteo Scandoloe48a4642018-06-26 10:08:34 -0700135
136 with patch.object(VOLTService.objects, "get") as olt_service_mock:
137 olt_service_mock.return_value = self.volt_service
138
Scott Baker47b47302019-01-30 16:55:07 -0800139 self.sync_step(model_accessor=self.model_accessor).delete_record(self.o)
Matteo Scandoloe48a4642018-06-26 10:08:34 -0700140 self.assertTrue(m.called)
141 self.assertEqual(m.call_count, 1)
142
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700143if __name__ == "__main__":
Luca Preteca974c82018-05-01 18:06:16 -0700144 unittest.main()