blob: abd72f7c4dca41fb74ec164107bff70073f0ca9d [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
21# Hack to load synchronizer framework
22test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
23xos_dir=os.path.join(test_path, "../../..")
24if not os.path.exists(os.path.join(test_path, "new_base")):
25 xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos")
26 services_dir = os.path.join(xos_dir, "../../xos_services")
27sys.path.append(xos_dir)
28sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
29# END Hack to load synchronizer framework
30
31# generate model from xproto
32def get_models_fn(service_name, xproto_name):
33 name = os.path.join(service_name, "xos", xproto_name)
34 if os.path.exists(os.path.join(services_dir, name)):
35 return name
36 else:
37 name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name)
38 if os.path.exists(os.path.join(services_dir, name)):
39 return name
40 raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name))
41# END generate model from xproto
42
43class TestSyncVOLTServiceInstance(unittest.TestCase):
44 def setUp(self):
45
46 self.sys_path_save = sys.path
47 sys.path.append(xos_dir)
48 sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
49
50 # Setting up the config module
51 from xosconfig import Config
Matteo Scandolof7ebb112018-09-18 16:17:22 -070052 config = os.path.join(test_path, "../test_config.yaml")
Matteo Scandolo53d61192018-06-01 16:11:14 -070053 Config.clear()
54 Config.init(config, "synchronizer-config-schema.yaml")
55 # END Setting up the config module
56
Matteo Scandolo53d61192018-06-01 16:11:14 -070057 from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
58 # build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto")])
59
60 # FIXME this is to get jenkins to pass the tests, somehow it is running tests in a different order
61 # and apparently it is not overriding the generated model accessor
62 build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto"),
63 get_models_fn("vsg", "vsg.xproto"),
64 get_models_fn("../profiles/rcord", "rcord.xproto")])
65 import synchronizers.new_base.modelaccessor
Scott Baker8699e7b2018-07-05 13:11:14 -070066 from synchronizers.new_base.syncstep import DeferredException
Matteo Scandolo53d61192018-06-01 16:11:14 -070067 from sync_onu_device import SyncONUDevice, model_accessor
68
69 # import all class names to globals
70 for (k, v) in model_accessor.all_model_classes.items():
71 globals()[k] = v
72
73 self.sync_step = SyncONUDevice
74
75 volt_service = Mock()
76 volt_service.voltha_url = "voltha_url"
77 volt_service.voltha_port = 1234
78 volt_service.voltha_user = "voltha_user"
79 volt_service.voltha_pass = "voltha_pass"
80
81 self.o = Mock()
82 self.o.device_id = "test_id"
83 self.o.pon_port.olt_device.volt_service = volt_service
84
85 def tearDown(self):
86 self.o = None
87 sys.path = self.sys_path_save
88
89 @requests_mock.Mocker()
Matteo Scandoloe3daaa22018-06-19 14:36:31 -070090 def test_enable(self, m):
91 m.post("http://voltha_url:1234/api/v1/devices/test_id/enable")
92
Matteo Scandolo53d61192018-06-01 16:11:14 -070093 self.o.admin_state = "ENABLED"
94 self.sync_step().sync_record(self.o)
Matteo Scandoloe3daaa22018-06-19 14:36:31 -070095 self.assertTrue(m.called)
Matteo Scandolo53d61192018-06-01 16:11:14 -070096
97 @requests_mock.Mocker()
98 def test_disable(self, m):
99 m.post("http://voltha_url:1234/api/v1/devices/test_id/disable")
100
101 self.o.admin_state = "DISABLED"
102 self.sync_step().sync_record(self.o)
103 self.assertTrue(m.called)
104
105 @requests_mock.Mocker()
106 def test_disable_fail(self, m):
107 m.post("http://voltha_url:1234/api/v1/devices/test_id/disable", status_code=500, text="Mock Error")
108
109 self.o.admin_state = "DISABLED"
110
111 with self.assertRaises(Exception) as e:
112 self.sync_step().sync_record(self.o)
113 self.assertTrue(m.called)
114 self.assertEqual(e.exception.message, "Failed to disable ONU device: Mock Error")
115
116if __name__ == "__main__":
117 unittest.main()