2 # ============LICENSE_START=======================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2019 AT&T Intellectual Property. All rights reserved.
6 # ===================================================================
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
28 # https://creativecommons.org/licenses/by/4.0/
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.
36 # ============LICENSE_END============================================
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
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
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()
58 return file_cache[basename]
61 def generate_parameters(yml_data):
62 parameters = yml_data.get("parameters", {})
65 for p, v in parameters.items():
66 param_type = v.get("type", "")
67 if param_type == "comma_delimited_list":
69 elif param_type == "string":
71 elif param_type == "json":
72 param = {"abc": "123"}
73 elif param_type == "number":
75 elif param_type == "boolean":
79 dummy_params[p] = param
81 return {"parameters": dummy_params}
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),
95 self.mock_is_service_available = self.mock_isa.start()
96 self.engine = service.EngineService("a", "t")
98 self.yaml_file = yaml_file
100 self.yml_data = yml_data
102 def validate_heat(self):
103 ymldata = load_file(self.yaml_file, self.files)
104 parameters = generate_parameters(self.yml_data)
106 t = template_format.parse(ymldata)
110 self.engine.validate_template(
111 self.ctx, t, files=self.files, params=parameters, show_nested=False
114 except Exception as e:
115 res = {"Error": e.__context__}
117 if isinstance(res, dict) and "Error" in res:
118 return res.get("Error")
123 @validates("R-92635")
124 @categories("openstack")
125 def test_heat(yaml_file):
126 with open(yaml_file, "r") as f:
129 # skip if resources are not defined
130 if "resources" not in yml:
131 pytest.skip("No resources specified in the heat template")
134 dirname = os.path.dirname(yaml_file)
135 for file in set(get_list_of_nested_files(yaml_file, dirname)):
136 load_file(file, files)
138 validator = HOTValidator(yaml_file, files, yml)
139 msg = validator.validate_heat()
141 assert not msg, "Invalid OpenStack Heat detected in {}: {}".format(
142 os.path.basename(yaml_file), msg