Commit seed code for validation-scripts
[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.network_roles import get_network_role_from_port
44 import re
45
46
47 def test_fixed_ips_format(heat_template):
48     '''
49     Make sure all fixed_ips properties follow the allowed
50     naming conventions
51     '''
52     formats = [
53               ["fixed_ips", "string", "internal",
54                re.compile(r'(.+?)_int_(.+?)_ip_\d+')],
55               ["fixed_ips", "string", "internal",
56                re.compile(r'(.+?)_int_(.+?)_v6_ip_\d+')],
57               ["fixed_ips", "string", "external",
58                re.compile(r'(.+?)_ip_\d+')],
59               ["fixed_ips", "string", "external",
60                re.compile(r'(.+?)_v6_ip_\d+')],
61               ["fixed_ips", "comma_delimited_list", "internal",
62                re.compile(r'(.+?)_int_(.+?)_ips')],
63               ["fixed_ips", "comma_delimited_list", "internal",
64                re.compile(r'(.+?)_int_(.+?)_v6_ips')],
65               ["fixed_ips", "comma_delimited_list", "external",
66                re.compile(r'(.+?)_ips')],
67               ["fixed_ips", "comma_delimited_list", "external",
68                re.compile(r'(.+?)_v6_ips')],
69               ]
70
71     with open(heat_template) as fh:
72         yml = yaml.load(fh)
73
74     # skip if resources are not defined
75     if "resources" not in yml:
76         pytest.skip("No resources specified in the heat template")
77
78     invalid_fixed_ips = []
79     for v1 in yml["resources"].values():
80         if not isinstance(v1, dict):
81             continue
82         if "properties" not in v1:
83             continue
84         if v1.get("type") != "OS::Neutron::Port":
85             continue
86         network_role = get_network_role_from_port(v1)
87
88         for k2, v2 in v1["properties"].items():
89             if k2 != "fixed_ips":
90                 continue
91             for v3 in v2:
92                 if "ip_address" not in v3:
93                     continue
94                 if "get_param" not in v3["ip_address"]:
95                     continue
96
97                 valid_fixed_ip = False
98                 for v4 in formats:
99                     param = v3["ip_address"]["get_param"]
100                     if isinstance(param, list):
101                         param = param[0]
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)