blob: 14a9c05caf1fb91f5daf5a5079ff06f9f6792aec [file] [log] [blame]
Matteo Scandolo5a0eed22018-06-01 14:42:43 -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 TestSyncHippieOssServiceInstance(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, [
Matteo Scandolo3c842ef2018-06-19 15:46:06 -070064 get_models_fn("hippie-oss", "hippie-oss.xproto"),
65 get_models_fn("olt-service", "volt.xproto"),
Matteo Scandolobbbdb1a2019-02-14 14:05:04 -080066 get_models_fn("rcord", "rcord.xproto")
Matteo Scandolo5a0eed22018-06-01 14:42:43 -070067 ])
68 import synchronizers.new_base.modelaccessor
69
70 from sync_hippie_oss_service_instance import SyncOSSServiceInstance, 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 = SyncOSSServiceInstance
78
79 self.oss = Mock()
80 self.oss.name = "oss"
Scott Baker26437eb2018-07-31 16:46:31 -070081 self.oss.id = 5367
Matteo Scandolo5a0eed22018-06-01 14:42:43 -070082
Matteo Scandolob8dec872018-07-17 16:56:23 -040083 # create a mock HippieOssServiceInstance instance
Matteo Scandolo5a0eed22018-06-01 14:42:43 -070084 self.o = Mock()
85 self.o.serial_number = "BRCM1234"
Matteo Scandolo5a0eed22018-06-01 14:42:43 -070086 self.o.of_dpid = "of:109299321"
87 self.o.owner.leaf_model = self.oss
88 self.o.tologdict.return_value = {}
89
90 def tearDown(self):
91 self.o = None
92 sys.path = self.sys_path_save
93
94 def test_sync_valid(self):
Scott Baker26437eb2018-07-31 16:46:31 -070095 with patch.object(HippieOSSWhiteListEntry.objects, "get_items") as whitelist_items:
96 # Create a whitelist entry for self.o's serial number
97 whitelist_entry = HippieOSSWhiteListEntry(owner_id=self.oss.id, serial_number=self.o.serial_number)
98 whitelist_items.return_value = [whitelist_entry]
Matteo Scandolo5a0eed22018-06-01 14:42:43 -070099
Scott Baker26437eb2018-07-31 16:46:31 -0700100 self.sync_step().sync_record(self.o)
Matteo Scandolo5a0eed22018-06-01 14:42:43 -0700101
Scott Baker26437eb2018-07-31 16:46:31 -0700102 self.assertEqual(self.o.valid, "valid")
103 self.assertTrue(self.o.no_sync)
104 self.o.save.assert_called()
Matteo Scandolo5a0eed22018-06-01 14:42:43 -0700105
106 def test_sync_rejected(self):
Matteo Scandolo5a0eed22018-06-01 14:42:43 -0700107 self.sync_step().sync_record(self.o)
108
109 self.assertEqual(self.o.valid, "invalid")
110 self.assertTrue(self.o.no_sync)
Scott Baker3685b042018-07-09 12:43:46 -0700111 self.o.save.assert_called()
112
113if __name__ == '__main__':
114 unittest.main()