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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
20 from cmframework.server.cmcsn import CMCSN
21 from cmframework.apis.cmerror import CMError
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')
31 csn = CMCSN(mock_backend)
33 mock_backend.assert_has_calls([call.get_property('cloud.cmframework')])
34 self.assertEqual(csn.get(), 0)
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'
42 csn = CMCSN(mock_backend)
44 mock_backend.assert_has_calls([call.get_property('cloud.cmframework')])
45 self.assertEqual(csn.get(), 0)
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}}}')
54 csn = CMCSN(mock_backend)
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)
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}}}')
69 csn = CMCSN(mock_backend)
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)
79 self.assertEqual(csn.get(), 102)
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}}}')
89 csn = CMCSN(mock_backend)
90 csn.sync_node_csn('node-a')
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)
99 self.assertEqual(csn.get_node_csn('node-a'), 101)
102 if __name__ == '__main__':