blob: 86e20cdcb75e32d6fd0e49a03b3c83cde5cb59a7 [file] [log] [blame]
Takahiro Suzuki2b66b942020-12-17 11:58:14 +09001
2# Copyright 2020-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
19
20import os
21import sys
22
23test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
24
25
26class TestModelPolicyNttWorkflowDriverServiceInstance(unittest.TestCase):
27 def setUp(self):
28
29 self.sys_path_save = sys.path
30
31 config = os.path.join(test_path, "../test_config.yaml")
32 from xosconfig import Config
33 Config.clear()
34 Config.init(config, 'synchronizer-config-schema.yaml')
35
36 from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
37 mock_modelaccessor_config(test_path, [("ntt-workflow-driver", "ntt-workflow-driver.xproto"),
38 ("olt-service", "volt.xproto"),
39 ("rcord", "rcord.xproto")])
40
41 import xossynchronizer.modelaccessor
42 import mock_modelaccessor
43 reload(mock_modelaccessor) # in case nose2 loaded it in a previous test
44 reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
45
46 from xossynchronizer.modelaccessor import model_accessor
47 from model_policy_ntt_workflow_driver_serviceinstance import NttWorkflowDriverServiceInstancePolicy, NttHelpers
48 self.NttHelpers = NttHelpers
49
50 # import all class names to globals
51 for (k, v) in model_accessor.all_model_classes.items():
52 globals()[k] = v
53
54 # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to
55 # creation of tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world.
56 model_accessor.reset_all_object_stores()
57
58 self.policy = NttWorkflowDriverServiceInstancePolicy(model_accessor=model_accessor)
59 self.si = NttWorkflowDriverServiceInstance()
60 self.si.owner = NttWorkflowDriverService()
61 self.si.serial_number = "BRCM1234"
62
63 def tearDown(self):
64 sys.path = self.sys_path_save
65
66 def test_update_onu(self):
67
68 onu = ONUDevice(
69 serial_number="BRCM1234",
70 admin_state="ENABLED"
71 )
72 with patch.object(ONUDevice.objects, "get_items") as get_onu, \
73 patch.object(onu, "save") as onu_save:
74 get_onu.return_value = [onu]
75
76 self.policy.update_onu("brcm1234", "ENABLED")
77 onu_save.assert_not_called()
78
79 self.policy.update_onu("brcm1234", "DISABLED")
80 self.assertEqual(onu.admin_state, "DISABLED")
81 onu_save.assert_called_with(
82 always_update_timestamp=True, update_fields=[
83 'admin_state', 'serial_number', 'updated'])
84
85 def test_enable_onu(self):
86 with patch.object(self.NttHelpers, "validate_onu") as validate_onu, \
87 patch.object(self.policy, "update_onu") as update_onu:
88 validate_onu.return_value = [True, "valid onu"]
89
90 self.policy.process_onu_state(self.si)
91
92 update_onu.assert_called_once()
93 update_onu.assert_called_with("BRCM1234", "ENABLED")
94
95 self.assertIn("valid onu", self.si.status_message)
96
97 def test_disable_onu(self):
98 with patch.object(self.NttHelpers, "validate_onu") as validate_onu, \
99 patch.object(self.policy, "update_onu") as update_onu:
100 validate_onu.return_value = [False, "invalid onu"]
101
102 self.policy.process_onu_state(self.si)
103
104 update_onu.assert_called_once()
105 update_onu.assert_called_with("BRCM1234", "DISABLED")
106
107 self.assertIn("invalid onu", self.si.status_message)
108
109 def test_handle_update_validate_onu(self):
110 """
111 Testing that handle_update calls validate_onu with the correct parameters
112 when necessary
113 """
114 with patch.object(self.policy, "process_onu_state") as process_onu_state, \
115 patch.object(self.policy, "update_onu") as update_onu:
116 update_onu.return_value = None
117
118 self.si.admin_onu_state = "AWAITING"
119 self.si.oper_onu_status = "AWAITING"
120 self.policy.handle_update(self.si)
121 process_onu_state.assert_called_with(self.si)
122
123 self.si.admin_onu_state = "ENABLED"
124 self.si.oper_onu_status = "ENABLED"
125 self.policy.handle_update(self.si)
126 process_onu_state.assert_called_with(self.si)
127
128 self.si.admin_onu_state = "DISABLED"
129 self.si.oper_onu_status = "DISABLED"
130 self.policy.handle_update(self.si)
131 process_onu_state.assert_called_with(self.si)
132
133if __name__ == '__main__':
134 sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))
135 unittest.main()