[VVP] Added new three new reports
[vvp/validation-scripts.git] / ice_validator / tests / test_fixed_ips_format.py
1 # -*- coding: utf8 -*-
2 # ============LICENSE_START=======================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2018 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 import pytest
42 import yaml
43 from .utils.ports import is_reserved_port
44 from .utils.network_roles import get_network_role_from_port, property_uses_get_resource
45 import re
46
47
48 def test_fixed_ips_format(heat_template):
49     '''
50     Make sure all fixed_ips properties follow the allowed
51     naming conventions
52     '''
53     formats = [
54         ["fixed_ips", "string", "internal",
55          re.compile(r'(.+?)_int_(.+?)_ip_\d+')],
56         ["fixed_ips", "string", "internal",
57          re.compile(r'(.+?)_int_(.+?)_v6_ip_\d+')],
58         ["fixed_ips", "string", "external",
59          re.compile(r'(.+?)_ip_\d+')],
60         ["fixed_ips", "string", "external",
61          re.compile(r'(.+?)_v6_ip_\d+')],
62         ["fixed_ips", "comma_delimited_list", "internal",
63          re.compile(r'(.+?)_int_(.+?)_ips')],
64         ["fixed_ips", "comma_delimited_list", "internal",
65          re.compile(r'(.+?)_int_(.+?)_v6_ips')],
66         ["fixed_ips", "comma_delimited_list", "external",
67          re.compile(r'(.+?)_ips')],
68         ["fixed_ips", "comma_delimited_list", "external",
69          re.compile(r'(.+?)_v6_ips')],
70     ]
71
72     with open(heat_template) as fh:
73         yml = yaml.load(fh)
74
75     # skip if resources are not defined
76     if "resources" not in yml:
77         pytest.skip("No resources specified in the heat template")
78
79     invalid_fixed_ips = []
80     for k1, v1 in yml["resources"].items():
81         if not isinstance(v1, dict):
82             continue
83         if "properties" not in v1:
84             continue
85         if v1.get("type") != "OS::Neutron::Port":
86             continue
87         if is_reserved_port(k1):
88             continue
89         if property_uses_get_resource(v1, "network"):
90             continue
91         network_role = get_network_role_from_port(v1)
92
93         for k2, v2 in v1["properties"].items():
94             if k2 != "fixed_ips":
95                 continue
96             for v3 in v2:
97                 if "ip_address" not in v3:
98                     continue
99                 if "get_param" not in v3["ip_address"]:
100                     continue
101
102                 valid_fixed_ip = False
103                 for v4 in formats:
104                     param = v3["ip_address"]["get_param"]
105                     if isinstance(param, list):
106                         param = param[0]
107                     m = v4[3].match(param)
108                     if m:
109                         if v4[2] == "internal" and\
110                            len(m.groups()) > 1 and\
111                            m.group(2) == network_role:
112                             valid_fixed_ip = True
113                             break
114                         elif v4[2] == "external" and\
115                             len(m.groups()) > 0 and\
116                                 m.group(1).endswith("_" + network_role):
117                             valid_fixed_ip = True
118                             break
119
120                 if not valid_fixed_ip:
121                     invalid_fixed_ips.append(param)
122
123     assert not set(invalid_fixed_ips)