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