blob: 85a40214a44f9b6c68d80116119256edbd294929 [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
52 config = os.path.join(test_path, "../model_policies/test_config.yaml")
53 Config.clear()
54 Config.init(config, "synchronizer-config-schema.yaml")
55 # END Setting up the config module
56
57 from synchronizers.new_base.syncstep import DeferredException
58 from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
59 # build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto")])
60
61 # FIXME this is to get jenkins to pass the tests, somehow it is running tests in a different order
62 # and apparently it is not overriding the generated model accessor
63 build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto"),
64 get_models_fn("vsg", "vsg.xproto"),
65 get_models_fn("../profiles/rcord", "rcord.xproto")])
66 import synchronizers.new_base.modelaccessor
67 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()
90 def test_do_nothing(self, m):
91 self.o.admin_state = "ENABLED"
92 self.sync_step().sync_record(self.o)
93 self.assertFalse(m.called)
94
95 @requests_mock.Mocker()
96 def test_disable(self, m):
97 m.post("http://voltha_url:1234/api/v1/devices/test_id/disable")
98
99 self.o.admin_state = "DISABLED"
100 self.sync_step().sync_record(self.o)
101 self.assertTrue(m.called)
102
103 @requests_mock.Mocker()
104 def test_disable_fail(self, m):
105 m.post("http://voltha_url:1234/api/v1/devices/test_id/disable", status_code=500, text="Mock Error")
106
107 self.o.admin_state = "DISABLED"
108
109 with self.assertRaises(Exception) as e:
110 self.sync_step().sync_record(self.o)
111 self.assertTrue(m.called)
112 self.assertEqual(e.exception.message, "Failed to disable ONU device: Mock Error")
113
114if __name__ == "__main__":
115 unittest.main()