blob: e81f87685ba1b43aef272f61d0802a20c9c1157c [file] [log] [blame]
Matteo Scandoloccef5782018-08-13 13:25:17 -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 json
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 TestSyncOLTDevice(unittest.TestCase):
44
45 def setUp(self):
46
47 self.sys_path_save = sys.path
48 sys.path.append(xos_dir)
49 sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
50
51 # Setting up the config module
52 from xosconfig import Config
53 config = os.path.join(test_path, "../test_config.yaml")
54 Config.clear()
55 Config.init(config, "synchronizer-config-schema.yaml")
56 # END Setting up the config module
57
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, [
64 get_models_fn("att-workflow-driver", "att-workflow-driver.xproto"),
65 get_models_fn("olt-service", "volt.xproto"),
66 get_models_fn("../profiles/rcord", "rcord.xproto")
67 ])
68 import synchronizers.new_base.modelaccessor
69 from onu_event import ONUEventStep, model_accessor
70
71 # import all class names to globals
72 for (k, v) in model_accessor.all_model_classes.items():
73 globals()[k] = v
74
75 self.log = Mock()
76
77 self.event_step = ONUEventStep(self.log)
78
79 self.event = Mock()
80 self.event_dict = {
81 'status': 'activated',
82 'serial_number': 'BRCM1234',
Matteo Scandolo53da44c2018-08-14 16:04:18 -070083 'of_dpid': 'of:109299321',
84 'uni_port_id': 16
Matteo Scandoloccef5782018-08-13 13:25:17 -070085 }
86 self.event.value = json.dumps(self.event_dict)
87
88 def tearDown(self):
89 sys.path = self.sys_path_save
90
91
92 def test_create_instance(self):
93
94 with patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as att_si_mock , \
95 patch.object(AttWorkflowDriverServiceInstance, "save", autospec=True) as mock_save:
96
97 att_si_mock.side_effect = IndexError
98
99 self.event_step.process_event(self.event)
100
101 att_si = mock_save.call_args[0][0]
102
103 self.assertEqual(mock_save.call_count, 1)
104
105 self.assertEqual(att_si.serial_number, self.event_dict['serial_number'])
106 self.assertEqual(att_si.of_dpid, self.event_dict['of_dpid'])
Matteo Scandolo53da44c2018-08-14 16:04:18 -0700107 self.assertEqual(att_si.uni_port_id, self.event_dict['uni_port_id'])