[VVP] Disallow vf_module_index look ups per R-55307
[vvp/validation-scripts.git] / ice_validator / tests / test_vf_module_index.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 from tests.helpers import validates, traverse
38 from tests.structures import Heat
39
40
41 class Finder:
42     """
43     If called sets found flag to True (used by traverse in uses_vf_module_index
44     """
45
46     def __init__(self):
47         self.found = False
48
49     def __call__(self, path, value):
50         self.found = True
51
52
53 def uses_vf_module_index(prop_value):
54     """
55     Returns True if prop_value uses vf_module_index, False otherwise
56     """
57     finder = Finder()
58     traverse(prop_value, "vf_module_index", finder)
59     return finder.found
60
61
62 def check_vf_module_index_errors(yaml_file, resource_type, property):
63     """
64     Finds all resources of resource_type where the property uses vf_module_index and
65     returns a set of all resource IDs that violate the condition.
66     """
67     resources = Heat(yaml_file).get_resource_by_type(resource_type)
68     errors = set()
69     for r_id, resource in resources.items():
70         if (
71             resource_type in ("OS::Neutron::Port", "OS::ContrailV2::InstanceIp")
72             and "_int_" in r_id
73         ):
74             continue  # rules do not apply to internal IPs
75         prop_value = resource.get("properties", {}).get(property)
76         if uses_vf_module_index(prop_value):
77             errors.add(r_id)
78     assert not errors, (
79         "The following {} resources use "
80         "vf_module_index to look up the {} property, "
81         "but that is not supported: {}"
82     ).format(resource_type, property, ", ".join(errors))
83
84
85 @validates("R-55307")
86 def test_no_vf_module_index_server_names(yaml_file):
87     check_vf_module_index_errors(yaml_file, "OS::Nova::Server", "name")
88
89
90 @validates("R-55307")
91 def test_no_vf_module_index_port_ips(yaml_file):
92     check_vf_module_index_errors(yaml_file, "OS::Neutron::Port", "fixed_ips")
93
94
95 @validates("R-55307")
96 def test_no_vf_module_index_contrail_ips(yaml_file):
97     check_vf_module_index_errors(
98         yaml_file, "OS::ContrailV2::InstanceIp", "instance_ip_address"
99     )