Commit seed code for validation-scripts
[vvp/validation-scripts.git] / ice_validator / tests / test_allowed_address_pair_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_allowed_address_pairs_format(heat_template):
48     '''
49     Make sure all allowed_address_pairs properties follow the allowed
50     naming conventions
51     '''
52     allowed_formats = [
53                       ["allowed_address_pairs", "string", "internal",
54                        re.compile(r'(.+?)_int_(.+?)_floating_v6_ip')],
55                       ["allowed_address_pairs", "string", "internal",
56                        re.compile(r'(.+?)_int_(.+?)_floating_ip')],
57                       ["allowed_address_pairs", "string", "external",
58                        re.compile(r'(.+?)_floating_v6_ip')],
59                       ["allowed_address_pairs", "string", "external",
60                        re.compile(r'(.+?)_floating_ip')],
61                       ["allowed_address_pairs", "string", "internal",
62                        re.compile(r'(.+?)_int_(.+?)_v6_ip_\d+')],
63                       ["allowed_address_pairs", "string", "internal",
64                        re.compile(r'(.+?)_int_(.+?)_ip_\d+')],
65                       ["allowed_address_pairs", "string", "external",
66                        re.compile(r'(.+?)_v6_ip_\d+')],
67                       ["allowed_address_pairs", "string", "external",
68                        re.compile(r'(.+?)_ip_\d+')],
69                       ["allowed_address_pairs", "comma_delimited_list",
70                        "internal", re.compile(r'(.+?)_int_(.+?)_v6_ips')],
71                       ["allowed_address_pairs", "comma_delimited_list",
72                        "internal", re.compile(r'(.+?)_int_(.+?)_ips')],
73                       ["allowed_address_pairs", "comma_delimited_list",
74                        "external", re.compile(r'(.+?)_v6_ips')],
75                       ["allowed_address_pairs", "comma_delimited_list",
76                        "external", re.compile(r'(.+?)_ips')],
77                       ]
78
79     with open(heat_template) as fh:
80         yml = yaml.load(fh)
81
82     # skip if resources are not defined
83     if "resources" not in yml:
84         pytest.skip("No resources specified in the heat template")
85
86     # check both valid and invalid patterns to catch edge cases
87     invalid_allowed_address_pairs = []
88
89     for v1 in yml["resources"].values():
90         if not isinstance(v1, dict):
91             continue
92         if "properties" not in v1:
93             continue
94         if v1.get("type") != "OS::Neutron::Port":
95             continue
96         network_role = get_network_role_from_port(v1)
97
98         for k2, v2 in v1["properties"].items():
99             if k2 != "allowed_address_pairs":
100                 continue
101             for v3 in v2:
102                 if "ip_address" not in v3:
103                     continue
104                 if "get_param" not in v3["ip_address"]:
105                     continue
106
107                 valid_allowed_address_pair = False
108                 for v4 in allowed_formats:
109                     param = v3["ip_address"]["get_param"]
110                     if isinstance(param, list):
111                         param = param[0]
112
113                     # check if pattern matches
114                     m = v4[3].match(param)
115                     if m:
116                         if v4[2] == "internal" and\
117                             len(m.groups()) > 1 and\
118                                 m.group(2) == network_role:
119                                 valid_allowed_address_pair = True
120                                 break
121                         elif v4[2] == "external" and\
122                                 len(m.groups()) > 0 and\
123                                 m.group(1).endswith("_" + network_role):
124                                     valid_allowed_address_pair = True
125                                     break
126
127                 if not valid_allowed_address_pair:
128                     invalid_allowed_address_pairs.append(param)
129
130     assert not set(invalid_allowed_address_pairs)