blob: 73c077aea71e69600045c8adc2f3ea4e42489b07 [file] [log] [blame]
Matteo Scandoload0c1752018-08-09 15:47:16 -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 TestModelPolicyAttWorkflowDriverWhiteListEntry(unittest.TestCase):
36 def setUp(self):
37 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("att-workflow-driver", "att-workflow-driver.xproto"),
49 get_models_fn("olt-service", "volt.xproto"),
50 get_models_fn("../profiles/rcord", "rcord.xproto")
51 ])
52
53 import synchronizers.new_base.modelaccessor
54 from model_policy_att_workflow_driver_whitelistentry import AttWorkflowDriverWhiteListEntryPolicy, model_accessor
55
56 from mock_modelaccessor import MockObjectList
57 self.MockObjectList = 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 = AttWorkflowDriverWhiteListEntryPolicy()
68
69 self.service = AttWorkflowDriverService()
70
71
72 def tearDown(self):
73 sys.path = self.sys_path_save
74 self.service = None
75
76 def test_whitelist_update(self):
77 """
78 When a whitelist entry is added, see that the AttWorkflowDriverIServicenstance was set to valid
79 """
80 with patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as oss_si_items:
81 si = AttWorkflowDriverServiceInstance(serial_number="BRCM333", owner_id=self.service.id, valid="invalid")
82 oss_si_items.return_value = [si]
83
84 wle = AttWorkflowDriverWhiteListEntry(serial_number="BRCM333", owner_id=self.service.id, owner=self.service)
85
86 self.policy.handle_update(wle)
87
88 self.assertEqual(si.valid, "valid")
89
90 def test_whitelist_delete(self):
91 """
92 When a whitelist entry is deleted, see that the AttWorkflowDriverIServicenstance was set to invalid
93 """
94 with patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as oss_si_items:
95 si = AttWorkflowDriverServiceInstance(serial_number="BRCM333", owner_id=self.service.id, valid="valid")
96 oss_si_items.return_value = [si]
97
98 wle = AttWorkflowDriverWhiteListEntry(serial_number="BRCM333", owner_id=self.service.id, owner=self.service)
99
100 self.policy.handle_delete(wle)
101
102 self.assertEqual(si.valid, "invalid")
103
104if __name__ == '__main__':
105 unittest.main()
106