Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 1 | # 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 | |
| 15 | import unittest |
Matteo Scandolo | 8091294 | 2018-07-25 20:51:30 -0700 | [diff] [blame] | 16 | import functools |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 17 | from mock import patch, call, Mock, PropertyMock |
| 18 | import requests_mock |
| 19 | |
| 20 | import os, sys |
| 21 | |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 22 | test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 23 | |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 24 | class TestSyncVOLTServiceInstance(unittest.TestCase): |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 25 | def setUp(self): |
| 26 | global DeferredException |
| 27 | |
| 28 | self.sys_path_save = sys.path |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 29 | |
| 30 | # Setting up the config module |
| 31 | from xosconfig import Config |
Matteo Scandolo | f7ebb11 | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 32 | config = os.path.join(test_path, "../test_config.yaml") |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 33 | Config.clear() |
| 34 | Config.init(config, "synchronizer-config-schema.yaml") |
| 35 | # END Setting up the config module |
| 36 | |
Scott Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 37 | from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config |
| 38 | mock_modelaccessor_config(test_path, [("olt-service", "volt.xproto"), |
Matteo Scandolo | 35207b7 | 2019-05-10 08:46:48 -0700 | [diff] [blame] | 39 | ("rcord", "rcord.xproto")]) |
Matteo Scandolo | 19466a0 | 2018-05-16 17:43:39 -0700 | [diff] [blame] | 40 | |
Scott Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 41 | 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 Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 48 | 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 Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 56 | volt_service = Mock() |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 57 | 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 Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 61 | |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 62 | uni_port = Mock() |
| 63 | uni_port.port_no = "uni_port_id" |
| 64 | |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 65 | 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 Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 69 | onu_device.uni_ports.first.return_value = uni_port |
| 70 | |
| 71 | # create a mock service instance |
| 72 | o = Mock() |
Matteo Scandolo | 8091294 | 2018-07-25 20:51:30 -0700 | [diff] [blame] | 73 | o.policy_code = 1 |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 74 | o.id = 1 |
| 75 | o.owner_id = "volt_service" |
| 76 | o.onu_device = onu_device |
| 77 | o.tologdict.return_value = {} |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 78 | |
| 79 | self.o = o |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 80 | self.onu_device = onu_device |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 81 | self.volt_service = volt_service |
| 82 | |
| 83 | def tearDown(self): |
| 84 | self.o = None |
| 85 | sys.path = self.sys_path_save |
| 86 | |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 87 | @requests_mock.Mocker() |
| 88 | def test_do_not_sync(self, m): |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 89 | self.onu_device.pon_port.olt_device.dp_id = None |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 90 | |
Matteo Scandolo | 25ad290 | 2018-08-14 17:02:05 -0700 | [diff] [blame] | 91 | with patch.object(VOLTService.objects, "get") as olt_service_mock: |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 92 | olt_service_mock.return_value = self.volt_service |
| 93 | |
| 94 | with self.assertRaises(DeferredException) as e: |
Scott Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 95 | self.sync_step(model_accessor=self.model_accessor).sync_record(self.o) |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 96 | |
| 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 Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 102 | |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 103 | self.onu_device.pon_port.olt_device.dp_id = "of:dp_id" |
Matteo Scandolo | 8091294 | 2018-07-25 20:51:30 -0700 | [diff] [blame] | 104 | |
Matteo Scandolo | 25ad290 | 2018-08-14 17:02:05 -0700 | [diff] [blame] | 105 | m.post("http://onos_voltha_url:4321/onos/olt/oltapp/of:dp_id/uni_port_id", status_code=200, json={}) |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 106 | |
Matteo Scandolo | 25ad290 | 2018-08-14 17:02:05 -0700 | [diff] [blame] | 107 | with patch.object(VOLTService.objects, "get") as olt_service_mock: |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 108 | olt_service_mock.return_value = self.volt_service |
| 109 | |
Scott Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 110 | self.sync_step(model_accessor=self.model_accessor).sync_record(self.o) |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 111 | self.assertTrue(m.called) |
Matteo Scandolo | 1835882 | 2018-08-15 17:17:43 -0700 | [diff] [blame] | 112 | self.assertEqual(self.o.backend_handle, "of:dp_id/uni_port_id") |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 113 | |
| 114 | @requests_mock.Mocker() |
| 115 | def test_do_sync_fail(self, m): |
Matteo Scandolo | 8091294 | 2018-07-25 20:51:30 -0700 | [diff] [blame] | 116 | |
Matteo Scandolo | 25ad290 | 2018-08-14 17:02:05 -0700 | [diff] [blame] | 117 | m.post("http://onos_voltha_url:4321/onos/olt/oltapp/of:dp_id/uni_port_id", status_code=500, text="Mock Error") |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 118 | |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 119 | self.onu_device.pon_port.olt_device.dp_id = "of:dp_id" |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 120 | |
Matteo Scandolo | 25ad290 | 2018-08-14 17:02:05 -0700 | [diff] [blame] | 121 | with patch.object(VOLTService.objects, "get") as olt_service_mock: |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 122 | olt_service_mock.return_value = self.volt_service |
| 123 | |
| 124 | with self.assertRaises(Exception) as e: |
Scott Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 125 | self.sync_step(model_accessor=self.model_accessor).sync_record(self.o) |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 126 | self.assertTrue(m.called) |
| 127 | self.assertEqual(e.exception.message, "Failed to add subscriber in onos voltha: Mock Error") |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 128 | |
Matteo Scandolo | e48a464 | 2018-06-26 10:08:34 -0700 | [diff] [blame] | 129 | @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 Scandolo | 1835882 | 2018-08-15 17:17:43 -0700 | [diff] [blame] | 134 | self.o.backend_handle = "of:dp_id/uni_port_id" |
Matteo Scandolo | e48a464 | 2018-06-26 10:08:34 -0700 | [diff] [blame] | 135 | |
| 136 | with patch.object(VOLTService.objects, "get") as olt_service_mock: |
| 137 | olt_service_mock.return_value = self.volt_service |
| 138 | |
Scott Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 139 | self.sync_step(model_accessor=self.model_accessor).delete_record(self.o) |
Matteo Scandolo | e48a464 | 2018-06-26 10:08:34 -0700 | [diff] [blame] | 140 | self.assertTrue(m.called) |
| 141 | self.assertEqual(m.call_count, 1) |
| 142 | |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 143 | if __name__ == "__main__": |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 144 | unittest.main() |