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.cmsnapshot import CMSnapshot
21 from cmframework.apis.cmerror import CMError
24 class CMSnapshotTest(unittest.TestCase):
26 def snapshot_list_data(name):
27 return {'snapshot_properties': {'some': 'value'},
28 'snapshot_metadata': 'meta-{}'.format(name)}
30 @mock.patch('cmframework.server.cmsnapshot.logging')
31 def test_new_snapshot_object(self, mock_logging):
32 mock_handler = mock.MagicMock()
34 snapshot = CMSnapshot(mock_handler)
36 with self.assertRaises(CMError) as context:
37 snapshot.get_property('foo')
39 @mock.patch('cmframework.server.cmsnapshot.logging')
40 def test_restore_without_load(self, mock_logging):
41 mock_handler = mock.MagicMock()
43 mock_target_backend = mock.MagicMock()
44 mock_target_backend.get_properties.return_value = {"foo": "bar",
45 "some": {"other": "value"}}
47 snapshot = CMSnapshot(mock_handler)
49 with self.assertRaises(CMError) as context:
50 snapshot.restore(mock_target_backend)
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
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'
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()
67 snapshot = CMSnapshot(mock_handler)
68 snapshot.create('snap1', mock_source_backend)
70 mock_handler.set_data.assert_called_once_with(
72 {'snapshot_properties': mock_source_backend.get_properties.return_value,
73 'snapshot_metadata': {'name': 'snap1',
74 'creation_date': expected_creation_date,
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
82 mock_backend_handler = mock.MagicMock()
84 snapshot = CMSnapshot(mock_handler)
86 with self.assertRaises(CMError) as context:
87 snapshot.create('already_exists', mock_backend_handler)
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
94 snapshot = CMSnapshot(mock_handler)
96 with self.assertRaises(CMError) as context:
97 snapshot.load('snap1')
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',
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}
112 snapshot = CMSnapshot(mock_handler)
114 snapshot.load('already_exists')
116 assert snapshot._data == expected_data
117 assert snapshot._metadata == expected_metadata
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',
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}
132 target_backend = mock.MagicMock()
133 target_backend.get_properties.return_value = {"a": "1", "b": "2"}
135 snapshot = CMSnapshot(mock_handler)
136 snapshot.load('already_exists')
138 snapshot.restore(target_backend)
140 assert {'a', 'b'} == set(target_backend.delete_properties.call_args[0][0])
141 target_backend.set_properties.assert_called_once_with(expected_data)
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',
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}
156 target_backend = mock.MagicMock()
157 target_backend.get_properties.return_value = {"a": "1"}
159 snapshot = CMSnapshot(mock_handler)
160 snapshot.load('already_exists')
162 snapshot.restore(target_backend)
164 target_backend.delete_property.assert_called_once_with('a')
165 target_backend.set_properties.assert_called_once_with(expected_data)
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'}
173 expected_snapshot_list = {'meta-snap1', 'meta-snap2'}
175 snapshot = CMSnapshot(mock_handler)
176 snapshot_list = snapshot.list()
178 assert set(snapshot_list) == expected_snapshot_list
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
185 snapshot = CMSnapshot(mock_handler)
186 snapshot.delete('already_exists')
188 mock_handler.delete_snapshot.assert_called_once_with('already_exists')
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
195 snapshot = CMSnapshot(mock_handler)
197 with self.assertRaises(CMError) as context:
198 snapshot.delete('snap1')
200 if __name__ == '__main__':