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