[VVP] Allow base templates to not have resources
[vvp/validation-scripts.git] / ice_validator / tests / test_heat_template_structure.py
1 # -*- coding: utf8 -*-
2 # ============LICENSE_START====================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2017 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 #
38 #
39
40 """Test heat template structure
41 """
42 import pytest
43
44 from tests import cached_yaml as yaml
45 from .helpers import validates, is_base_module, load_yaml
46
47 VERSION = "1.2.0"
48
49 # pylint: disable=invalid-name
50
51
52 @validates("R-27078")
53 def test_heat_template_structure_contains_heat_template_version(yaml_file):
54     """
55     Check that all heat templates have the required sections
56     """
57     required_key_values = ["heat_template_version"]
58
59     with open(yaml_file) as fh:
60         yml = yaml.load(fh)
61     assert all(
62         [k in yml for k in required_key_values]
63     ), "{} doesn't contain the {} section, but it is required".format(
64         yaml_file, required_key_values[0]
65     )
66
67
68 @validates("R-39402")
69 def test_heat_template_structure_contains_description(yaml_file):
70     """
71     Check that all heat templates have the required sections
72     """
73     required_key_values = ["description"]
74
75     with open(yaml_file) as fh:
76         yml = yaml.load(fh)
77     assert all(
78         [k in yml for k in required_key_values]
79     ), "{} doesn't contain the {} section, but it is required".format(
80         yaml_file, required_key_values[0]
81     )
82
83
84 @validates("R-35414")
85 def test_heat_template_structure_contains_parameters(yaml_file):
86     """
87     Check that all heat templates have the required sections
88     """
89     required_key_values = ["parameters"]
90
91     with open(yaml_file) as fh:
92         yml = yaml.load(fh)
93     assert all(
94         [k in yml for k in required_key_values]
95     ), "{} doesn't contain the {} section, but it is required".format(
96         yaml_file, required_key_values[0]
97     )
98
99
100 @validates("R-23664")
101 def test_heat_template_structure_contains_resources(heat_template):
102     """
103     Check that all heat templates have the required sections
104     """
105     if is_base_module(heat_template):
106         pytest.skip("Not applicable to base modules")
107     template = load_yaml(heat_template)
108     assert "resources" in template, "This template must contain a resources section"
109
110
111 @validates("R-11441")
112 def test_parameter_type(yaml_file):
113     """A VNF's Heat Orchestration Template's parameter type **MUST**
114     be one of the following values:
115     """
116     types = ["string", "number", "json", "comma_delimited_list", "boolean"]
117     with open(yaml_file) as fh:
118         yml = yaml.load(fh)
119     for key, param in yml.get("parameters", {}).items():
120         assert isinstance(param, dict), "%s parameter %s is not dict" % (yaml_file, key)
121         if "type" not in param:
122             continue
123         typ = param["type"]
124         assert typ in types, '%s parameter %s has invalid type "%s"' % (
125             yaml_file,
126             key,
127             typ,
128         )