[VVP] updating validation scripts in dublin
[vvp/validation-scripts.git] / ice_validator / tests / test_contrail_fqdn.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 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
39 #
40
41 """
42 contrail fqdn
43 """
44
45 import re
46
47 import pytest
48
49 from .structures import Heat
50 from .helpers import validates
51
52 VERSION = "1.1.0"
53
54 RE_NETWORK_ROLE = re.compile(
55     r".+_" r"\d+_" r"(int_)?" r"(subint_)?" r"(?P<network_role>.+)" r"_vmi_" r"\d$"
56 )
57
58
59 def get_network_role(rid):
60     """return the network_role parsed from the rid.
61     """
62     match = RE_NETWORK_ROLE.match(rid)
63     return match.groupdict()["network_role"] if match else None
64
65
66 def run_test(heat_template, validate):
67     """call validate for each fixed_ips
68     """
69     heat = Heat(filepath=heat_template)
70     if not heat.resources:
71         pytest.skip("No resources found")
72
73     contrail_resources = heat.contrail_resources
74     if not contrail_resources:
75         pytest.skip("No Contrail resources found")
76
77     skip = True
78     bad = {}
79     for rid, resource in contrail_resources.items():
80         network_role = get_network_role(rid)
81         if network_role is None:
82             continue
83         virtual_network_refs = heat.nested_get(
84             resource, "properties", "virtual_network_refs"
85         )
86         if virtual_network_refs is None:
87             continue
88         if not isinstance(virtual_network_refs, list):
89             bad[rid] = "properties.virtual_network_refs must be a list."
90             continue
91         error = validate(heat, virtual_network_refs, network_role)
92         if error:
93             bad[rid] = error
94             continue
95         skip = False
96     if bad:
97         raise AssertionError(
98             "Bad OS::ContrailV2::VirtualMachineInterface: %s"
99             % (", ".join("%s: %s" % (rid, error) for rid, error in bad.items()))
100         )
101     if skip:
102         pytest.skip("No Contrail virtual_network_refs found")
103
104
105 def validate_virtual_network_refs(heat, virtual_network_refs, network_role):
106     """ensure there is a matching virtual_network_ref in the list.
107     Returns error message string or None.
108     """
109     expect = "%s_net_fqdn" % network_role
110     for vn_ref in virtual_network_refs:
111         param = heat.nested_get(vn_ref, "get_param")
112         if param == expect:
113             param_type = heat.nested_get(heat.parameters, param, "type")
114             if param_type != "string":
115                 return (
116                     'virtual_network_ref parameter "%s" '
117                     'type "%s" must be "string"' % (param, param_type)
118                 )
119             else:
120                 return None
121     return "virtual_network_refs must include {get_param: %s}" % expect
122
123
124 # pylint: disable=invalid-name
125
126
127 @validates("R-02164")
128 def test_contrail_fqdn(heat_template):
129     """
130     When a VNF's Heat Orchestration Template's Contrail resource
131     has a property that
132     references an external network that requires the network's
133     Fully Qualified Domain Name (FQDN), the property parameter
134
135     * **MUST** follow the format ``{network-role}_net_fqdn``
136     * **MUST** be declared as type ``string``
137     """
138     run_test(heat_template, validate_virtual_network_refs)