Support python3 uploaded to pypi websit
[parser.git] / tosca2heat / heat-translator / translator / hot / tosca / tests / test_tosca_policies.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.nodetemplate import NodeTemplate
14 from toscaparser.policy import Policy
15 from toscaparser.tests.base import TestCase
16 import toscaparser.utils.yamlparser
17 from translator.hot.tosca.tosca_compute import ToscaCompute
18 from translator.hot.tosca.tosca_policies import ToscaPolicies
19
20
21 class ToscaPoicyTest(TestCase):
22
23     def _tosca_policy_test(self, tpl_snippet, expectedprops):
24         nodetemplates = (toscaparser.utils.yamlparser.
25                          simple_parse(tpl_snippet)['node_templates'])
26         policies = (toscaparser.utils.yamlparser.
27                     simple_parse(tpl_snippet)['policies'])
28         name = list(nodetemplates.keys())[0]
29         policy_name = list(policies[0].keys())[0]
30         for policy in policies:
31             tpl = policy[policy_name]
32             targets = tpl["targets"]
33         try:
34             nodetemplate = NodeTemplate(name, nodetemplates)
35             toscacompute = ToscaCompute(nodetemplate)
36             toscacompute.handle_properties()
37
38             policy = Policy(policy_name, tpl, targets,
39                             "node_templates")
40             toscapolicy = ToscaPolicies(policy)
41             nodetemplate = [toscacompute]
42             toscapolicy.handle_properties(nodetemplate)
43
44             self.assertEqual(toscacompute.properties, expectedprops)
45         except Exception:
46             raise
47
48     def test_compute_with_policies(self):
49         tpl_snippet = '''
50         node_templates:
51           server:
52             type: tosca.nodes.Compute
53             capabilities:
54               host:
55                 properties:
56                   disk_size: 10 GB
57                   num_cpus: 4
58                   mem_size: 4 GB
59               os:
60                 properties:
61                   architecture: x86_64
62                   type: Linux
63                   distribution: Fedora
64                   version: 18.0
65         policies:
66           - my_compute_placement_policy:
67               type: tosca.policies.Placement
68               description: Apply my placement policy to my application servers
69               targets: [ server ]
70         '''
71         expectedprops = {'flavor': 'm1.large',
72                          'image': 'fedora-amd64-heat-config',
73                          'scheduler_hints': {
74                              'group': {
75                                  'get_resource':
76                                  'my_compute_placement_policy'}},
77                          'user_data_format': 'SOFTWARE_CONFIG',
78                          'software_config_transport': 'POLL_SERVER_HEAT'}
79         self._tosca_policy_test(
80             tpl_snippet,
81             expectedprops)