blob: 2b82b1bde921afd9d9fe9d4d7b2e6927e37d2ca6 [file] [log] [blame]
Matteo Scandoload0c1752018-08-09 15:47:16 -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
16
17import functools
18from mock import patch, call, Mock, PropertyMock
19import requests_mock
20import multistructlog
21from multistructlog import create_logger
22
23import os, sys
24
25# Hack to load synchronizer framework
26test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
27xos_dir=os.path.join(test_path, "../../..")
28if not os.path.exists(os.path.join(test_path, "new_base")):
29 xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos")
30 services_dir = os.path.join(xos_dir, "../../xos_services")
31sys.path.append(xos_dir)
32sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
33# END Hack to load synchronizer framework
34
35# generate model from xproto
36def get_models_fn(service_name, xproto_name):
37 name = os.path.join(service_name, "xos", xproto_name)
38 if os.path.exists(os.path.join(services_dir, name)):
39 return name
40 else:
41 name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name)
42 if os.path.exists(os.path.join(services_dir, name)):
43 return name
44 raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name))
45# END generate model from xproto
46
47class TestSyncAttWorkflowDriverServiceInstance(unittest.TestCase):
48
49 def setUp(self):
50
51 self.sys_path_save = sys.path
52 sys.path.append(xos_dir)
53 sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
54
55 # Setting up the config module
56 from xosconfig import Config
57 config = os.path.join(test_path, "../test_config.yaml")
58 Config.clear()
59 Config.init(config, "synchronizer-config-schema.yaml")
60 # END Setting up the config module
61
62 from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
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
70 from sync_att_workflow_driver_service_instance import SyncAttWorkflowDriverServiceInstance, model_accessor
71
72 # import all class names to globals
73 for (k, v) in model_accessor.all_model_classes.items():
74 globals()[k] = v
75
76
77 self.sync_step = SyncAttWorkflowDriverServiceInstance
78
Matteo Scandolo53da44c2018-08-14 16:04:18 -070079 self.oss = AttWorkflowDriverService()
Matteo Scandoload0c1752018-08-09 15:47:16 -070080 self.oss.name = "oss"
81 self.oss.id = 5367
82
83 # create a mock AttWorkflowDriverServiceInstance instance
84 self.o = Mock()
85 self.o.serial_number = "BRCM1234"
86 self.o.of_dpid = "of:109299321"
Matteo Scandolo53da44c2018-08-14 16:04:18 -070087 self.o.pon_port_id = 1
Matteo Scandoload0c1752018-08-09 15:47:16 -070088 self.o.owner.leaf_model = self.oss
89 self.o.tologdict.return_value = {}
90
Matteo Scandolo53da44c2018-08-14 16:04:18 -070091 self.pon_port = PONPort(
92 port_no=1
93 )
94 self.onu = ONUDevice(
95 serial_number=self.o.serial_number,
96 pon_port=self.pon_port
97 )
98
Matteo Scandoload0c1752018-08-09 15:47:16 -070099 def tearDown(self):
100 self.o = None
101 sys.path = self.sys_path_save
102
103 def test_sync_valid(self):
Matteo Scandolo53da44c2018-08-14 16:04:18 -0700104 with patch.object(AttWorkflowDriverWhiteListEntry.objects, "get_items") as whitelist_items, \
105 patch.object(ONUDevice.objects, "get_items") as onu_items:
Matteo Scandoload0c1752018-08-09 15:47:16 -0700106 # Create a whitelist entry for self.o's serial number
Matteo Scandolo53da44c2018-08-14 16:04:18 -0700107 whitelist_entry = AttWorkflowDriverWhiteListEntry(
108 owner_id=self.oss.id,
109 serial_number=self.o.serial_number,
110 device_id=self.o.of_dpid,
111 pon_port_id=1,
112 )
Matteo Scandoload0c1752018-08-09 15:47:16 -0700113 whitelist_items.return_value = [whitelist_entry]
Matteo Scandolo53da44c2018-08-14 16:04:18 -0700114 onu_items.return_value = [self.onu]
Matteo Scandoload0c1752018-08-09 15:47:16 -0700115
116 self.sync_step().sync_record(self.o)
117
118 self.assertEqual(self.o.valid, "valid")
Matteo Scandoload0c1752018-08-09 15:47:16 -0700119 self.o.save.assert_called()
120
Matteo Scandolo53da44c2018-08-14 16:04:18 -0700121 def test_sync_bad_location(self):
122 with patch.object(AttWorkflowDriverWhiteListEntry.objects, "get_items") as whitelist_items, \
123 patch.object(ONUDevice.objects, "get_items") as onu_items:
124 # Create a whitelist entry for self.o's serial number
125 whitelist_entry = AttWorkflowDriverWhiteListEntry(
126 owner_id=self.oss.id,
127 serial_number=self.o.serial_number,
128 device_id="foo",
129 pon_port_id=666
130 )
131 whitelist_items.return_value = [whitelist_entry]
132 onu_items.return_value = [self.onu]
133
134 self.sync_step().sync_record(self.o)
135
136 self.assertEqual(self.o.valid, "invalid")
137 self.o.save.assert_called()
138
139 def test_sync_no_whitelist(self):
Matteo Scandoload0c1752018-08-09 15:47:16 -0700140 self.sync_step().sync_record(self.o)
141
142 self.assertEqual(self.o.valid, "invalid")
Matteo Scandoload0c1752018-08-09 15:47:16 -0700143 self.o.save.assert_called()
144
145if __name__ == '__main__':
146 unittest.main()