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