blob: f301f6de11c26b225f3032d715edc73e5337aa8b [file] [log] [blame]
Matteo Scandolob517fb22018-07-29 09:57:17 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
17import unittest
18from mock import patch, call, Mock, PropertyMock
19
20import os, sys
21
22test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
23service_dir=os.path.join(test_path, "../../../..")
24xos_dir=os.path.join(test_path, "../../..")
25if not os.path.exists(os.path.join(test_path, "new_base")):
26 xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos")
27 services_dir=os.path.join(xos_dir, "../../xos_services")
28
29def get_models_fn(service_name, xproto_name):
30 name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name)
31 if os.path.exists(os.path.join(services_dir, name)):
32 return name
33 raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name))
34
35class TestModelPolicyHippieOssService(unittest.TestCase):
36 def setUp(self):
Matteo Scandolob517fb22018-07-29 09:57:17 -070037 self.sys_path_save = sys.path
38 sys.path.append(xos_dir)
39 sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
40
41 config = os.path.join(test_path, "../test_config.yaml")
42 from xosconfig import Config
43 Config.clear()
44 Config.init(config, 'synchronizer-config-schema.yaml')
45
46 from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
47 build_mock_modelaccessor(xos_dir, services_dir, [
48 get_models_fn("hippie-oss", "hippie-oss.xproto"),
49 get_models_fn("olt-service", "volt.xproto"),
Matteo Scandolobbbdb1a2019-02-14 14:05:04 -080050 get_models_fn("rcord", "rcord.xproto")
Matteo Scandolob517fb22018-07-29 09:57:17 -070051 ])
52
53 import synchronizers.new_base.modelaccessor
54 from model_policy_hippieossservice import OSSServicePolicy, model_accessor
55
56 from mock_modelaccessor import MockObjectList
Scott Baker26437eb2018-07-31 16:46:31 -070057 self.MockObjectList = MockObjectList
Matteo Scandolob517fb22018-07-29 09:57:17 -070058
59 # import all class names to globals
60 for (k, v) in model_accessor.all_model_classes.items():
61 globals()[k] = v
62
63 # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to creation of
64 # tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world.
65 model_accessor.reset_all_object_stores()
66
67 self.policy = OSSServicePolicy()
68
69 self.service = HippieOSSService(
Scott Baker26437eb2018-07-31 16:46:31 -070070 id = 5367,
71 whitelist_entries = [],
Matteo Scandolob517fb22018-07-29 09:57:17 -070072 )
73
74 # needs to be enabled
75 self.si1 = HippieOSSServiceInstance(
76 valid="awaiting",
77 serial_number="BRCM111"
78 )
79
80 # needs to be enabled
81 self.si2 = HippieOSSServiceInstance(
82 valid="invalid",
83 serial_number="BRCM222"
84 )
85
86 # remains disabled
87 self.si3 = HippieOSSServiceInstance(
88 valid="invalid",
89 serial_number="BRCM333"
90 )
91
92 # needs to be disabled
93 self.si4 = HippieOSSServiceInstance(
94 valid="valid",
95 serial_number="BRCM444"
96 )
97
98 def tearDown(self):
99 sys.path = self.sys_path_save
100 self.service = None
101
102 def test_whitelist_update(self):
103 """
104 When the whitelist is updated, check for added ONU to be enabled and for removed ONU to be disabled
105 """
106 with patch.object(HippieOSSServiceInstance.objects, "get_items") as oss_si, \
107 patch.object(self.si1, "save") as si1_save, \
108 patch.object(self.si2, "save") as si2_save, \
109 patch.object(self.si3, "save") as si3_save, \
110 patch.object(self.si4, "save") as si4_save:
111 oss_si.return_value = [self.si1, self.si2, self.si3, self.si4]
112
Scott Baker26437eb2018-07-31 16:46:31 -0700113 wle1 = HippieOSSWhiteListEntry(owner_id=self.service.id, serial_number="BRCM111")
114 wle2 = HippieOSSWhiteListEntry(owner_id=self.service.id, serial_number="BRCM222")
115 self.service.whitelist_entries = self.MockObjectList([wle1, wle2])
116
Matteo Scandolob517fb22018-07-29 09:57:17 -0700117 self.policy.handle_update(self.service)
118
119 self.si1.save.assert_called_with(always_update_timestamp=True, update_fields=['valid', 'no_sync', 'updated'])
120 self.assertEqual(self.si1.valid, "valid")
121 self.si2.save.assert_called_with(always_update_timestamp=True, update_fields=['valid', 'no_sync', 'updated'])
122 self.assertEqual(self.si2.valid, "valid")
123 self.si3.save.assert_not_called()
124 self.assertEqual(self.si3.valid, "invalid")
125 self.si4.save.assert_called_with(always_update_timestamp=True, update_fields=['valid', 'no_sync', 'updated'])
126 self.assertEqual(self.si4.valid, "invalid")
127
128if __name__ == '__main__':
129 unittest.main()
130