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 six import string_types
43 from tests.helpers import validates, get_environment_pair
44 from tests.structures import Heat
48 def test_vm_role_hardcoded(yaml_file):
50 Validate vm_role value when hardcoded in the template
52 heat = Heat(filepath=yaml_file)
53 servers = heat.get_resource_by_type("OS::Nova::Server")
55 for r_id, server in servers.items():
56 props = server.get("properties") or {}
57 metadata = props.get("metadata") or {}
58 if "vm_role" not in metadata:
60 vm_role_value = metadata["vm_role"]
61 if isinstance(vm_role_value, dict):
62 continue # Likely using get_param - validate separately
63 if not re.match(r"^\w+$", vm_role_value):
65 "OS::Nova::Server {} vm_role = {}".format(r_id, vm_role_value)
69 "vm_role's value must only contain alphanumerics and underscores. "
70 + "Invalid vm_role's detected: "
73 assert not errors, msg
77 def test_vm_role_from_env_file(yaml_file):
79 Validate vm_role when using parameters and env file
81 pair = get_environment_pair(yaml_file)
83 pytest.skip("Unable to resolve environment pair")
84 template_params = pair["yyml"].get("parameters") or {}
85 env_params = pair["eyml"].get("parameters") or {}
87 if "vm_role" not in template_params:
88 pytest.skip("vm_role not in parameters")
90 if "vm_role" not in env_params:
91 pytest.skip("vm_role not in environment file. Error checked elsewhere")
93 vm_role = env_params.get("vm_role", "")
94 if not isinstance(vm_role, string_types):
95 vm_role = str(vm_role)
96 msg = "vm_role {} contains non-alphanumeric or non-underscore characters".format(
99 assert re.match(r"^\w+$", vm_role), msg