[VVP] adding heat template-validate test
[vvp/validation-scripts.git] / ice_validator / tests / test_valid_heat.py
1 # -*- coding: utf8 -*-
2 # ============LICENSE_START=======================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2019 AT&T Intellectual Property. All rights reserved.
6 # ===================================================================
7 #
8 # Unless otherwise specified, all software contained herein is licensed
9 # under the Apache License, Version 2.0 (the "License");
10 # you may not use this software except in compliance with the License.
11 # You may obtain a copy of the License at
12 #
13 #             http://www.apache.org/licenses/LICENSE-2.0
14 #
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
20 #
21 #
22 #
23 # Unless otherwise specified, all documentation contained herein is licensed
24 # under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25 # you may not use this documentation except in compliance with the License.
26 # You may obtain a copy of the License at
27 #
28 #             https://creativecommons.org/licenses/by/4.0/
29 #
30 # Unless required by applicable law or agreed to in writing, documentation
31 # distributed under the License is distributed on an "AS IS" BASIS,
32 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33 # See the License for the specific language governing permissions and
34 # limitations under the License.
35 #
36 # ============LICENSE_END============================================
37 import mock
38 import os
39 import pytest
40
41 from heat.common import template_format
42 from heat.engine import resources
43 from heat.engine import service
44 from heat.tests.openstack.nova import fakes as fakes_nova
45 from heat.tests import utils
46
47 from tests import cached_yaml as yaml
48 from tests.utils.nested_files import get_list_of_nested_files
49 from tests.helpers import categories, validates
50
51
52 def load_file(filename, file_cache):
53     basename = os.path.basename(filename)
54     if basename not in file_cache:
55         with open(filename, "r") as fh:
56             file_cache[basename] = fh.read()
57
58     return file_cache[basename]
59
60
61 def generate_parameters(yml_data):
62     parameters = yml_data.get("parameters", {})
63     dummy_params = {}
64
65     for p, v in parameters.items():
66         param_type = v.get("type", "")
67         if param_type == "comma_delimited_list":
68             param = "1,2,3"
69         elif param_type == "string":
70             param = "123"
71         elif param_type == "json":
72             param = {"abc": "123"}
73         elif param_type == "number":
74             param = 123
75         elif param_type == "boolean":
76             param = True
77         else:
78             param = "123"
79         dummy_params[p] = param
80
81     return {"parameters": dummy_params}
82
83
84 class HOTValidator:
85     def __init__(self, yaml_file, files, yml_data):
86         resources.initialise()
87         self.fc = fakes_nova.FakeClient()
88         self.gc = fakes_nova.FakeClient()
89         resources.initialise()
90         self.ctx = utils.dummy_context()
91         self.mock_isa = mock.patch(
92             "heat.engine.resource.Resource.is_service_available",
93             return_value=(True, None),
94         )
95         self.mock_is_service_available = self.mock_isa.start()
96         self.engine = service.EngineService("a", "t")
97
98         self.yaml_file = yaml_file
99         self.files = files
100         self.yml_data = yml_data
101
102     def validate_heat(self):
103         ymldata = load_file(self.yaml_file, self.files)
104         parameters = generate_parameters(self.yml_data)
105
106         t = template_format.parse(ymldata)
107
108         try:
109             res = dict(
110                 self.engine.validate_template(
111                     self.ctx, t, files=self.files, params=parameters, show_nested=False
112                 )
113             )
114         except Exception as e:
115             res = {"Error": e.__context__}
116
117         if isinstance(res, dict) and "Error" in res:
118             return res.get("Error")
119         else:
120             return None
121
122
123 @validates("R-92635")
124 @categories("openstack")
125 def test_heat(yaml_file):
126     with open(yaml_file, "r") as f:
127         yml = yaml.load(f)
128
129     # skip if resources are not defined
130     if "resources" not in yml:
131         pytest.skip("No resources specified in the heat template")
132
133     files = {}
134     dirname = os.path.dirname(yaml_file)
135     for file in set(get_list_of_nested_files(yml, dirname)):
136         load_file(file, files)
137
138     validator = HOTValidator(yaml_file, files, yml)
139     msg = validator.validate_heat()
140
141     assert not msg, "Invalid OpenStack Heat detected in {}: {}".format(
142         os.path.basename(yaml_file), msg
143     )