Merge "Initial release documents"
[parser.git] / tosca2heat / heat-translator / translator / hot / tosca / tests / test_tosca_blockstorage.py
1 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
2 #    not use this file except in compliance with the License. You may obtain
3 #    a copy of the License at
4 #
5 #         http://www.apache.org/licenses/LICENSE-2.0
6 #
7 #    Unless required by applicable law or agreed to in writing, software
8 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 #    License for the specific language governing permissions and limitations
11 #    under the License.
12
13 from toscaparser.common.exception import InvalidPropertyValueError
14 from toscaparser.nodetemplate import NodeTemplate
15 from toscaparser.tests.base import TestCase
16 from toscaparser.utils.gettextutils import _
17 import toscaparser.utils.yamlparser
18 from translator.hot.tosca.tosca_block_storage import ToscaBlockStorage
19
20
21 class ToscaBlockStoreTest(TestCase):
22
23     def _tosca_blockstore_test(self, tpl_snippet, expectedprops):
24         nodetemplates = (toscaparser.utils.yamlparser.
25                          simple_parse(tpl_snippet)['node_templates'])
26         name = list(nodetemplates.keys())[0]
27         try:
28             nodetemplate = NodeTemplate(name, nodetemplates)
29             tosca_block_store = ToscaBlockStorage(nodetemplate)
30             tosca_block_store.handle_properties()
31             if not self._compare_properties(tosca_block_store.properties,
32                                             expectedprops):
33                 raise Exception(_("Hot Properties are not"
34                                   " same as expected properties"))
35         except Exception:
36             # for time being rethrowing. Will be handled future based
37             # on new development
38             raise
39
40     def _compare_properties(self, hotprops, expectedprops):
41         return all(item in hotprops.items() for item in expectedprops.items())
42
43     def test_node_blockstorage_with_properties(self):
44         tpl_snippet = '''
45         node_templates:
46           my_storage:
47             type: tosca.nodes.BlockStorage
48             properties:
49               size: 1024 MiB
50               snapshot_id: abc
51         '''
52         expectedprops = {'snapshot_id': 'abc',
53                          'size': 1}
54         self._tosca_blockstore_test(
55             tpl_snippet,
56             expectedprops)
57
58         tpl_snippet = '''
59         node_templates:
60           my_storage:
61             type: tosca.nodes.BlockStorage
62             properties:
63               size: 124 MB
64               snapshot_id: abc
65         '''
66         expectedprops = {'snapshot_id': 'abc',
67                          'size': 1}
68         self._tosca_blockstore_test(
69             tpl_snippet,
70             expectedprops)
71
72     def test_node_blockstorage_with_invalid_size_property(self):
73         tpl_snippet = '''
74         node_templates:
75           my_storage:
76             type: tosca.nodes.BlockStorage
77             properties:
78               size: 0 MB
79               snapshot_id: abc
80         '''
81         expectedprops = {}
82         self.assertRaises(InvalidPropertyValueError,
83                           lambda: self._tosca_blockstore_test(tpl_snippet,
84                                                               expectedprops))