Removed obsolete url
[ta/config-manager.git] / cmframework / test / cmcsn_test.py
1 # Copyright 2019 Nokia
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
15 import unittest
16 import mock
17 from mock import call
18 import json
19
20 from cmframework.server.cmcsn import CMCSN
21 from cmframework.apis.cmerror import CMError
22
23
24 class CMCSNTest(unittest.TestCase):
25     @mock.patch('cmframework.server.cmcsn.logging')
26     def test_initial_config(self, mock_logging):
27         mock_backend = mock.MagicMock()
28         mock_backend.get_property = mock.MagicMock()
29         mock_backend.get_property.side_effect = CMError('Test error')
30
31         csn = CMCSN(mock_backend)
32
33         mock_backend.assert_has_calls([call.get_property('cloud.cmframework')])
34         self.assertEqual(csn.get(), 0)
35
36     @mock.patch('cmframework.server.cmcsn.logging')
37     def test_bad_config(self, mock_logging):
38         mock_backend = mock.MagicMock()
39         mock_backend.get_property = mock.MagicMock()
40         mock_backend.get_property.return_value = 'bad json'
41
42         csn = CMCSN(mock_backend)
43
44         mock_backend.assert_has_calls([call.get_property('cloud.cmframework')])
45         self.assertEqual(csn.get(), 0)
46
47     @mock.patch('cmframework.server.cmcsn.logging')
48     def test_old_config(self, mock_logging):
49         mock_backend = mock.MagicMock()
50         mock_backend.get_property = mock.MagicMock()
51         mock_backend.get_property.return_value = ('{"csn": {"global": 101, "nodes": '
52                                                   '{"node-a": 99, "node-b": 100, "node-c": 101}}}')
53
54         csn = CMCSN(mock_backend)
55
56         self.assertEqual(csn.get(), 101)
57         self.assertEqual(csn.get_node_csn('node-a'), 99)
58         self.assertEqual(csn.get_node_csn('node-b'), 100)
59         self.assertEqual(csn.get_node_csn('node-c'), 101)
60
61     @mock.patch('cmframework.server.cmcsn.logging')
62     def test_increment(self, mock_logging):
63         mock_backend = mock.MagicMock()
64         mock_backend.get_property = mock.MagicMock()
65         mock_backend.set_property = mock.MagicMock()
66         mock_backend.get_property.return_value = ('{"csn": {"global": 101, "nodes": '
67                                                   '{"node-a": 99, "node-b": 100, "node-c": 101}}}')
68
69         csn = CMCSN(mock_backend)
70         csn.increment()
71
72         mock_backend.set_property.assert_called_once()
73         mock_backend_set_property_arg_value = json.loads(mock_backend.set_property.call_args[0][1])
74         self.assertEqual(mock_backend_set_property_arg_value['csn']['global'], 102)
75         self.assertEqual(mock_backend_set_property_arg_value['csn']['nodes']['node-a'], 99)
76         self.assertEqual(mock_backend_set_property_arg_value['csn']['nodes']['node-b'], 100)
77         self.assertEqual(mock_backend_set_property_arg_value['csn']['nodes']['node-c'], 101)
78
79         self.assertEqual(csn.get(), 102)
80
81     @mock.patch('cmframework.server.cmcsn.logging')
82     def test_sync_node(self, mock_logging):
83         mock_backend = mock.MagicMock()
84         mock_backend.get_property = mock.MagicMock()
85         mock_backend.set_property = mock.MagicMock()
86         mock_backend.get_property.return_value = ('{"csn": {"global": 101, "nodes": '
87                                                   '{"node-a": 99, "node-b": 100, "node-c": 101}}}')
88
89         csn = CMCSN(mock_backend)
90         csn.sync_node_csn('node-a')
91
92         mock_backend.set_property.assert_called_once()
93         mock_backend_set_property_arg_value = json.loads(mock_backend.set_property.call_args[0][1])
94         self.assertEqual(mock_backend_set_property_arg_value['csn']['global'], 101)
95         self.assertEqual(mock_backend_set_property_arg_value['csn']['nodes']['node-a'], 101)
96         self.assertEqual(mock_backend_set_property_arg_value['csn']['nodes']['node-b'], 100)
97         self.assertEqual(mock_backend_set_property_arg_value['csn']['nodes']['node-c'], 101)
98
99         self.assertEqual(csn.get_node_csn('node-a'), 101)
100
101
102 if __name__ == '__main__':
103     unittest.main()