blob: 7c5a598592722385ace328c13af2ba2cf513ad38 [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):
37
38 self.sys_path_save = sys.path
39 sys.path.append(xos_dir)
40 sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
41
42 config = os.path.join(test_path, "../test_config.yaml")
43 from xosconfig import Config
44 Config.clear()
45 Config.init(config, 'synchronizer-config-schema.yaml')
46
47 from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
48 build_mock_modelaccessor(xos_dir, services_dir, [
49 get_models_fn("hippie-oss", "hippie-oss.xproto"),
50 get_models_fn("olt-service", "volt.xproto"),
51 get_models_fn("../profiles/rcord", "rcord.xproto")
52 ])
53
54 import synchronizers.new_base.modelaccessor
55 from model_policy_hippieossservice import OSSServicePolicy, model_accessor
56
57 from mock_modelaccessor import MockObjectList
58
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(
70 whitelist="BRCM111, BRCM222"
71 )
72
73 # needs to be enabled
74 self.si1 = HippieOSSServiceInstance(
75 valid="awaiting",
76 serial_number="BRCM111"
77 )
78
79 # needs to be enabled
80 self.si2 = HippieOSSServiceInstance(
81 valid="invalid",
82 serial_number="BRCM222"
83 )
84
85 # remains disabled
86 self.si3 = HippieOSSServiceInstance(
87 valid="invalid",
88 serial_number="BRCM333"
89 )
90
91 # needs to be disabled
92 self.si4 = HippieOSSServiceInstance(
93 valid="valid",
94 serial_number="BRCM444"
95 )
96
97 def tearDown(self):
98 sys.path = self.sys_path_save
99 self.service = None
100
101 def test_whitelist_update(self):
102 """
103 When the whitelist is updated, check for added ONU to be enabled and for removed ONU to be disabled
104 """
105 with patch.object(HippieOSSServiceInstance.objects, "get_items") as oss_si, \
106 patch.object(self.si1, "save") as si1_save, \
107 patch.object(self.si2, "save") as si2_save, \
108 patch.object(self.si3, "save") as si3_save, \
109 patch.object(self.si4, "save") as si4_save:
110 oss_si.return_value = [self.si1, self.si2, self.si3, self.si4]
111
112 self.policy.handle_update(self.service)
113
114 self.si1.save.assert_called_with(always_update_timestamp=True, update_fields=['valid', 'no_sync', 'updated'])
115 self.assertEqual(self.si1.valid, "valid")
116 self.si2.save.assert_called_with(always_update_timestamp=True, update_fields=['valid', 'no_sync', 'updated'])
117 self.assertEqual(self.si2.valid, "valid")
118 self.si3.save.assert_not_called()
119 self.assertEqual(self.si3.valid, "invalid")
120 self.si4.save.assert_called_with(always_update_timestamp=True, update_fields=['valid', 'no_sync', 'updated'])
121 self.assertEqual(self.si4.valid, "invalid")
122
123if __name__ == '__main__':
124 unittest.main()
125