Matteo Scandolo | e3c8446 | 2020-03-30 15:26:00 -0700 | [diff] [blame^] | 1 | # Copyright 2017 the original author or authors. |
| 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 | import os |
| 15 | import sys |
| 16 | from unittest import TestCase, main |
| 17 | from unittest.mock import patch |
| 18 | from twisted.internet import defer |
| 19 | from voltha_protos.adapter_pb2 import Adapter |
| 20 | from voltha_protos.device_pb2 import DeviceType |
| 21 | |
| 22 | sys.path.append(os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), "../../../"))) |
| 23 | |
| 24 | def mock_decorator(f): |
| 25 | def real_wrapper(func): |
| 26 | return func |
| 27 | return real_wrapper |
| 28 | |
| 29 | |
| 30 | patch('pyvoltha.adapters.kafka.container_proxy.ContainerProxy.wrap_request', mock_decorator).start() |
| 31 | from pyvoltha.adapters.kafka.core_proxy import CoreProxy |
| 32 | |
| 33 | |
| 34 | class TestCoreProxy(TestCase): |
| 35 | |
| 36 | def setUp(self): |
| 37 | self.core_proxy = CoreProxy( |
| 38 | kafka_proxy=None, |
| 39 | default_core_topic='test_core', |
| 40 | default_event_topic='test.events', |
| 41 | my_listening_topic='test_openonu') |
| 42 | |
| 43 | self.supported_device_types = [ |
| 44 | DeviceType( |
| 45 | id="brmc_openonu", |
| 46 | vendor_ids=['BBSM'], |
| 47 | adapter="openonu", |
| 48 | accepts_bulk_flow_update=False, |
| 49 | accepts_add_remove_flow_updates=True |
| 50 | ) |
| 51 | ] |
| 52 | |
| 53 | @defer.inlineCallbacks |
| 54 | def test_register_defaults(self): |
| 55 | adapter = Adapter( |
| 56 | id="testAdapter", |
| 57 | vendor="ONF", |
| 58 | version="test", |
| 59 | ) |
| 60 | |
| 61 | expected_adapter = Adapter( |
| 62 | id="testAdapter", |
| 63 | vendor="ONF", |
| 64 | version="test", |
| 65 | currentReplica=1, |
| 66 | totalReplicas=1 |
| 67 | ) |
| 68 | |
| 69 | with patch.object(self.core_proxy, "invoke") as mock_invoke: |
| 70 | |
| 71 | mock_invoke.return_value = "success" |
| 72 | |
| 73 | res = yield self.core_proxy.register(adapter, self.supported_device_types) |
| 74 | mock_invoke.assert_called_with( |
| 75 | rpc="Register", |
| 76 | adapter=expected_adapter, |
| 77 | deviceTypes=self.supported_device_types |
| 78 | ) |
| 79 | self.assertTrue(mock_invoke.call_count, 1) |
| 80 | self.assertEqual(res, "success") |
| 81 | |
| 82 | @defer.inlineCallbacks |
| 83 | def test_register_multiple(self): |
| 84 | |
| 85 | adapter = Adapter( |
| 86 | id="testAdapter", |
| 87 | vendor="ONF", |
| 88 | version="test", |
| 89 | currentReplica=4, |
| 90 | totalReplicas=8 |
| 91 | ) |
| 92 | |
| 93 | with patch.object(self.core_proxy, "invoke") as mock_invoke: |
| 94 | mock_invoke.return_value = "success" |
| 95 | |
| 96 | res = yield self.core_proxy.register(adapter, self.supported_device_types) |
| 97 | mock_invoke.assert_called_with( |
| 98 | rpc="Register", |
| 99 | adapter=adapter, |
| 100 | deviceTypes=self.supported_device_types |
| 101 | ) |
| 102 | |
| 103 | @defer.inlineCallbacks |
| 104 | def test_register_misconfigured(self): |
| 105 | """ |
| 106 | In case the operator sets wrong parameter, eg: currentReplica=10, totalReplicas=2 |
| 107 | raise an exception |
| 108 | """ |
| 109 | adapter = Adapter( |
| 110 | id="testAdapter", |
| 111 | vendor="ONF", |
| 112 | version="test", |
| 113 | currentReplica=10, |
| 114 | totalReplicas=8 |
| 115 | ) |
| 116 | |
| 117 | with self.assertRaises(Exception) as e: |
| 118 | res = yield self.core_proxy.register(adapter, self.supported_device_types) |
| 119 | |
| 120 | self.assertEqual(str(e.exception), "currentReplica (10) can't be greater than totalReplicas (8)") |
| 121 | |
| 122 | adapter = Adapter( |
| 123 | id="testAdapter", |
| 124 | vendor="ONF", |
| 125 | version="test", |
| 126 | totalReplicas=0, |
| 127 | currentReplica=1 |
| 128 | ) |
| 129 | |
| 130 | with self.assertRaises(Exception) as e: |
| 131 | res = yield self.core_proxy.register(adapter, self.supported_device_types) |
| 132 | |
| 133 | self.assertEqual(str(e.exception), "totalReplicas can't be 0, since you're here you have at least one") |
| 134 | |
| 135 | adapter = Adapter( |
| 136 | id="testAdapter", |
| 137 | vendor="ONF", |
| 138 | version="test", |
| 139 | totalReplicas=1, |
| 140 | currentReplica=0 |
| 141 | ) |
| 142 | |
| 143 | with self.assertRaises(Exception) as e: |
| 144 | res = yield self.core_proxy.register(adapter, self.supported_device_types) |
| 145 | |
| 146 | self.assertEqual(str(e.exception), "currentReplica can't be 0, it has to start from 1") |
| 147 | |
| 148 | |
| 149 | if __name__ == '__main__': |
| 150 | main() |