Initial commit
[ta/config-manager.git] / cmframework / test / cmsnapshot_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.cmsnapshot import CMSnapshot
21 from cmframework.apis.cmerror import CMError
22
23
24 class CMSnapshotTest(unittest.TestCase):
25     @staticmethod
26     def snapshot_list_data(name):
27         return {'snapshot_properties': {'some': 'value'},
28                 'snapshot_metadata': 'meta-{}'.format(name)}
29
30     @mock.patch('cmframework.server.cmsnapshot.logging')
31     def test_new_snapshot_object(self, mock_logging):
32         mock_handler = mock.MagicMock()
33
34         snapshot = CMSnapshot(mock_handler)
35
36         with self.assertRaises(CMError) as context:
37             snapshot.get_property('foo')
38
39     @mock.patch('cmframework.server.cmsnapshot.logging')
40     def test_restore_without_load(self, mock_logging):
41         mock_handler = mock.MagicMock()
42
43         mock_target_backend = mock.MagicMock()
44         mock_target_backend.get_properties.return_value = {"foo": "bar",
45                                                            "some": {"other": "value"}}
46
47         snapshot = CMSnapshot(mock_handler)
48
49         with self.assertRaises(CMError) as context:
50             snapshot.restore(mock_target_backend)
51
52     @mock.patch('cmframework.server.cmsnapshot.datetime.datetime')
53     @mock.patch('cmframework.server.cmsnapshot.logging')
54     def test_create(self, mock_logging, mock_datetime):
55         mock_handler = mock.MagicMock()
56         mock_handler.snapshot_exists.return_value = False
57
58         mock_source_backend = mock.MagicMock()
59         mock_source_backend.get_properties.return_value = {"foo": "bar", "some": {"other": "value"}}
60         mock_source_backend.get_property.return_value = 'some'
61
62         mock_datetime.now = mock.MagicMock()
63         from datetime import datetime
64         mock_datetime.now.return_value = datetime.now()
65         expected_creation_date = mock_datetime.now.return_value.isoformat()
66
67         snapshot = CMSnapshot(mock_handler)
68         snapshot.create('snap1', mock_source_backend)
69
70         mock_handler.set_data.assert_called_once_with(
71             'snap1',
72             {'snapshot_properties': mock_source_backend.get_properties.return_value,
73              'snapshot_metadata': {'name': 'snap1',
74                                    'creation_date': expected_creation_date,
75                                    'custom': None}})
76
77     @mock.patch('cmframework.server.cmsnapshot.logging')
78     def test_create_already_exists(self, mock_logging):
79         mock_handler = mock.MagicMock()
80         mock_handler.snapshot_exists.return_value = True
81
82         mock_backend_handler = mock.MagicMock()
83
84         snapshot = CMSnapshot(mock_handler)
85
86         with self.assertRaises(CMError) as context:
87             snapshot.create('already_exists', mock_backend_handler)
88
89     @mock.patch('cmframework.server.cmsnapshot.logging')
90     def test_load_non_existing(self, mock_logging):
91         mock_handler = mock.MagicMock()
92         mock_handler.snapshot_exists.return_value = False
93
94         snapshot = CMSnapshot(mock_handler)
95
96         with self.assertRaises(CMError) as context:
97             snapshot.load('snap1')
98
99     @mock.patch('cmframework.server.cmsnapshot.logging')
100     def test_load(self, mock_logging):
101         expected_data = {'some': 'value'}
102         expected_metadata = {'name': 'snap1',
103                              'creation_date': 'somedate',
104                              'custom': None}
105
106         mock_handler = mock.MagicMock()
107         mock_handler.snapshot_exists.return_value = True
108         mock_handler.get_data.return_value = {
109             'snapshot_properties': expected_data,
110             'snapshot_metadata': expected_metadata}
111
112         snapshot = CMSnapshot(mock_handler)
113
114         snapshot.load('already_exists')
115
116         assert snapshot._data == expected_data
117         assert snapshot._metadata == expected_metadata
118
119     @mock.patch('cmframework.server.cmsnapshot.logging')
120     def test_restore(self, mock_logging):
121         expected_data = {'foo': 'bar', 'some': {'other': 'value'}}
122         expected_metadata = {'name': 'already_exists',
123                              'creation_date': 'somedate',
124                              'custom': None}
125
126         mock_handler = mock.MagicMock()
127         mock_handler.snapshot_exists.return_value = True
128         mock_handler.get_data.return_value = {
129             'snapshot_properties': expected_data,
130             'snapshot_metadata': expected_metadata}
131
132         target_backend = mock.MagicMock()
133         target_backend.get_properties.return_value = {"a": "1", "b": "2"}
134
135         snapshot = CMSnapshot(mock_handler)
136         snapshot.load('already_exists')
137
138         snapshot.restore(target_backend)
139
140         assert {'a', 'b'} == set(target_backend.delete_properties.call_args[0][0])
141         target_backend.set_properties.assert_called_once_with(expected_data)
142
143     @mock.patch('cmframework.server.cmsnapshot.logging')
144     def test_restore_only_one_in_target(self, mock_logging):
145         expected_data = {'foo': 'bar', 'some': {'other': 'value'}}
146         expected_metadata = {'name': 'already_exists',
147                              'creation_date': 'somedate',
148                              'custom': None}
149
150         mock_handler = mock.MagicMock()
151         mock_handler.snapshot_exists.return_value = True
152         mock_handler.get_data.return_value = {
153             'snapshot_properties': expected_data,
154             'snapshot_metadata': expected_metadata}
155
156         target_backend = mock.MagicMock()
157         target_backend.get_properties.return_value = {"a": "1"}
158
159         snapshot = CMSnapshot(mock_handler)
160         snapshot.load('already_exists')
161
162         snapshot.restore(target_backend)
163
164         target_backend.delete_property.assert_called_once_with('a')
165         target_backend.set_properties.assert_called_once_with(expected_data)
166
167     @mock.patch('cmframework.server.cmsnapshot.logging')
168     def test_list(self, mock_logging):
169         mock_handler = mock.MagicMock()
170         mock_handler.get_data.side_effect = CMSnapshotTest.snapshot_list_data
171         mock_handler.list_snapshots.return_value = {'snap1', 'snap2'}
172
173         expected_snapshot_list = {'meta-snap1', 'meta-snap2'}
174
175         snapshot = CMSnapshot(mock_handler)
176         snapshot_list = snapshot.list()
177
178         assert set(snapshot_list) == expected_snapshot_list
179
180     @mock.patch('cmframework.server.cmsnapshot.logging')
181     def test_delete(self, mock_logging):
182         mock_handler = mock.MagicMock()
183         mock_handler.snapshot_exists.return_value = True
184
185         snapshot = CMSnapshot(mock_handler)
186         snapshot.delete('already_exists')
187
188         mock_handler.delete_snapshot.assert_called_once_with('already_exists')
189
190     @mock.patch('cmframework.server.cmsnapshot.logging')
191     def test_delete_non_existing(self, mock_logging):
192         mock_handler = mock.MagicMock()
193         mock_handler.snapshot_exists.return_value = False
194
195         snapshot = CMSnapshot(mock_handler)
196
197         with self.assertRaises(CMError) as context:
198             snapshot.delete('snap1')
199
200 if __name__ == '__main__':
201     unittest.main()