Matteo Scandolo | 2044cec | 2019-06-27 15:30:34 -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 |
| 16 | from mock import patch, call, Mock, PropertyMock |
| 17 | import requests_mock |
| 18 | |
| 19 | import os, sys |
| 20 | |
| 21 | test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 22 | |
| 23 | class TestSyncTechProfile(unittest.TestCase): |
| 24 | def setUp(self): |
| 25 | |
| 26 | self.mock_etcd = Mock(name="etcd-client") |
| 27 | etcd = Mock(name="etcd-mocked-lib") |
| 28 | etcd.client.return_value = self.mock_etcd |
| 29 | modules = { |
| 30 | 'etcd3': etcd |
| 31 | } |
| 32 | self.module_patcher = patch.dict('sys.modules', modules) |
| 33 | self.module_patcher.start() |
| 34 | |
| 35 | self.sys_path_save = sys.path |
| 36 | |
| 37 | # Setting up the config module |
| 38 | from xosconfig import Config |
| 39 | config = os.path.join(test_path, "../test_config.yaml") |
| 40 | Config.clear() |
| 41 | Config.init(config, "synchronizer-config-schema.yaml") |
| 42 | # END Setting up the config module |
| 43 | |
| 44 | from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config |
| 45 | mock_modelaccessor_config(test_path, [("olt-service", "volt.xproto"), |
| 46 | ("rcord", "rcord.xproto")]) |
| 47 | |
| 48 | import xossynchronizer.modelaccessor |
| 49 | reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test |
| 50 | |
| 51 | from xossynchronizer.modelaccessor import model_accessor |
| 52 | self.model_accessor = model_accessor |
| 53 | |
| 54 | from xossynchronizer.steps.syncstep import DeferredException |
| 55 | from sync_tech_profile import SyncTechnologyProfile, model_accessor |
| 56 | |
| 57 | # import all class names to globals |
| 58 | for (k, v) in model_accessor.all_model_classes.items(): |
| 59 | globals()[k] = v |
| 60 | |
| 61 | self.sync_step = SyncTechnologyProfile |
| 62 | |
| 63 | self.o = Mock() |
| 64 | self.o.technology = "test_technology" |
| 65 | self.o.profile_id = 64 |
| 66 | self.o.profile_value = '{"test":"profile"}' |
| 67 | |
| 68 | self.o.tologdict.return_value = {'name': "mock-tp"} |
| 69 | |
| 70 | def tearDown(self): |
| 71 | self.o = None |
| 72 | sys.path = self.sys_path_save |
| 73 | self.module_patcher.stop() |
| 74 | |
| 75 | def test_sync(self): |
| 76 | |
| 77 | self.sync_step(model_accessor=self.model_accessor).sync_record(self.o) |
| 78 | self.mock_etcd.put.assert_called_with('service/voltha/technology_profiles/test_technology/64', |
| 79 | '{"test":"profile"}') |
| 80 | |
| 81 | def test_delete(self): |
| 82 | |
| 83 | self.mock_etcd.get.return_value = [self.o.profile_value, "response from mock-etcd"] |
| 84 | |
| 85 | self.sync_step(model_accessor=self.model_accessor).delete_record(self.o) |
| 86 | self.mock_etcd.get.assert_called_with('service/voltha/technology_profiles/test_technology/64') |
| 87 | self.mock_etcd.delete.assert_called_with('service/voltha/technology_profiles/test_technology/64') |
| 88 | |
| 89 | def test_delete_missing_object(self): |
| 90 | |
| 91 | self.mock_etcd.get.return_value = [None, "response from mock-etcd"] |
| 92 | |
| 93 | self.sync_step(model_accessor=self.model_accessor).delete_record(self.o) |
| 94 | self.mock_etcd.get.assert_called_with('service/voltha/technology_profiles/test_technology/64') |
| 95 | self.mock_etcd.delete.assert_not_called() |
| 96 | |
| 97 | |
| 98 | if __name__ == "__main__": |
| 99 | unittest.main() |