e7da73da7bdf18258f5fa95e01d93e48f5ee92ed
[vvp/validation-scripts.git] / ice_validator / tests / test_vm_role_value.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 #
38 import re
39
40 import pytest
41 from six import string_types
42
43 from tests.helpers import validates, get_environment_pair
44 from tests.structures import Heat
45
46
47 @validates("R-86476")
48 def test_vm_role_hardcoded(yaml_file):
49     """
50     Validate vm_role value when hardcoded in the template
51     """
52     heat = Heat(filepath=yaml_file)
53     servers = heat.get_resource_by_type("OS::Nova::Server")
54     errors = []
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:
59             continue
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):
64             errors.append(
65                 "OS::Nova::Server {} vm_role = {}".format(r_id, vm_role_value)
66             )
67
68     msg = (
69         "vm_role's value must only contain alphanumerics and underscores. "
70         + "Invalid vm_role's detected: "
71         + ". ".join(errors)
72     )
73     assert not errors, msg
74
75
76 @validates("R-86476")
77 def test_vm_role_from_env_file(heat_template):
78     """
79     Validate vm_role when using parameters and env file
80     """
81     pair = get_environment_pair(heat_template)
82     if not pair:
83         pytest.skip("Unable to resolve environment pair")
84     template_params = pair["yyml"].get("parameters") or {}
85     env_params = pair["eyml"].get("parameters") or {}
86
87     if "vm_role" not in template_params:
88         pytest.skip("vm_role not in parameters")
89
90     if "vm_role" not in env_params:
91         pytest.skip("vm_role not in environment file.  Error checked elsewhere")
92
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(
97         vm_role
98     )
99     assert re.match(r"^\w+$", vm_role), msg