blob: f624540de6647fde64707fa07a4bf4c13e0b671d [file] [log] [blame]
Matteo Scandolo53d61192018-06-01 16:11:14 -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
16from mock import patch, call, Mock, PropertyMock
17import requests_mock
18
19import os, sys
20
Matteo Scandolo53d61192018-06-01 16:11:14 -070021test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
Matteo Scandolo53d61192018-06-01 16:11:14 -070022
23class TestSyncVOLTServiceInstance(unittest.TestCase):
24 def setUp(self):
25
26 self.sys_path_save = sys.path
Matteo Scandolo53d61192018-06-01 16:11:14 -070027
28 # Setting up the config module
29 from xosconfig import Config
Matteo Scandolof7ebb112018-09-18 16:17:22 -070030 config = os.path.join(test_path, "../test_config.yaml")
Matteo Scandolo53d61192018-06-01 16:11:14 -070031 Config.clear()
32 Config.init(config, "synchronizer-config-schema.yaml")
33 # END Setting up the config module
34
Scott Baker47b47302019-01-30 16:55:07 -080035 from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
36 mock_modelaccessor_config(test_path, [("olt-service", "volt.xproto"),
37 ("vsg", "vsg.xproto"),
38 ("../profiles/rcord", "rcord.xproto"),])
Matteo Scandolo53d61192018-06-01 16:11:14 -070039
Scott Baker47b47302019-01-30 16:55:07 -080040 import xossynchronizer.modelaccessor
41 reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
42
43 from xossynchronizer.modelaccessor import model_accessor
44 self.model_accessor = model_accessor
45
46 from xossynchronizer.steps.syncstep import DeferredException
Matteo Scandolo53d61192018-06-01 16:11:14 -070047 from sync_onu_device import SyncONUDevice, model_accessor
48
49 # import all class names to globals
50 for (k, v) in model_accessor.all_model_classes.items():
51 globals()[k] = v
52
53 self.sync_step = SyncONUDevice
54
55 volt_service = Mock()
56 volt_service.voltha_url = "voltha_url"
57 volt_service.voltha_port = 1234
58 volt_service.voltha_user = "voltha_user"
59 volt_service.voltha_pass = "voltha_pass"
60
61 self.o = Mock()
62 self.o.device_id = "test_id"
63 self.o.pon_port.olt_device.volt_service = volt_service
64
65 def tearDown(self):
66 self.o = None
67 sys.path = self.sys_path_save
68
69 @requests_mock.Mocker()
Matteo Scandoloe3daaa22018-06-19 14:36:31 -070070 def test_enable(self, m):
71 m.post("http://voltha_url:1234/api/v1/devices/test_id/enable")
72
Matteo Scandolo53d61192018-06-01 16:11:14 -070073 self.o.admin_state = "ENABLED"
Scott Baker47b47302019-01-30 16:55:07 -080074 self.sync_step(model_accessor=self.model_accessor).sync_record(self.o)
Matteo Scandoloe3daaa22018-06-19 14:36:31 -070075 self.assertTrue(m.called)
Matteo Scandolo53d61192018-06-01 16:11:14 -070076
77 @requests_mock.Mocker()
78 def test_disable(self, m):
79 m.post("http://voltha_url:1234/api/v1/devices/test_id/disable")
80
81 self.o.admin_state = "DISABLED"
Scott Baker47b47302019-01-30 16:55:07 -080082 self.sync_step(model_accessor=self.model_accessor).sync_record(self.o)
Matteo Scandolo53d61192018-06-01 16:11:14 -070083 self.assertTrue(m.called)
84
85 @requests_mock.Mocker()
86 def test_disable_fail(self, m):
87 m.post("http://voltha_url:1234/api/v1/devices/test_id/disable", status_code=500, text="Mock Error")
88
89 self.o.admin_state = "DISABLED"
90
91 with self.assertRaises(Exception) as e:
Scott Baker47b47302019-01-30 16:55:07 -080092 self.sync_step(model_accessor=self.model_accessor).sync_record(self.o)
Matteo Scandolo53d61192018-06-01 16:11:14 -070093 self.assertTrue(m.called)
94 self.assertEqual(e.exception.message, "Failed to disable ONU device: Mock Error")
95
96if __name__ == "__main__":
97 unittest.main()