[VVP] udpating scripts for casablanca 2
[vvp/validation-scripts.git] / ice_validator / tests / test_subnet_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 re
42
43 import pytest
44 from tests import cached_yaml as yaml
45
46 from .utils.network_roles import get_network_role_from_port, \
47     property_uses_get_resource
48
49
50 def test_subnet_format(heat_template):
51     '''
52     Make sure all subnet properties follow the allowed naming
53     conventions
54     '''
55     formats = [
56               ["subnet_id", "string", "internal",
57                re.compile(r'int_(.+?)_subnet_id')],
58               ["subnet_id", "string", "internal",
59                re.compile(r'int_(.+?)_v6_subnet_id')],
60               ["subnet_id", "string", "external",
61                re.compile(r'(.+?)_subnet_id')],
62               ["subnet_id", "string", "external",
63                re.compile(r'(.+?)_v6_subnet_id')]]
64
65     with open(heat_template) as fh:
66         yml = yaml.load(fh)
67
68     # skip if resources are not defined
69     if "resources" not in yml:
70         pytest.skip("No resources specified in the heat template")
71
72     invalid_subnets = []
73     for v1 in yml["resources"].values():
74         if not isinstance(v1, dict):
75             continue
76         if "properties" not in v1:
77             continue
78         if v1.get("type") != "OS::Neutron::Port":
79             continue
80         if property_uses_get_resource(v1, "network"):
81             continue
82         network_role = get_network_role_from_port(v1)
83
84         # get the network param to define the network_type
85         try:
86             network_param = v1["properties"]["network"]["get_param"]
87         except KeyError:
88             continue
89
90         # define the network_type
91         network_type = 'external'
92         if network_param.startswith('int_'):
93             network_type = 'internal'
94
95         for k2, v2 in v1["properties"].items():
96             if k2 != "fixed_ips":
97                 continue
98
99             for v3 in v2:
100                 if "subnet_id" not in v3:
101                     continue
102
103                 subnet_id = v3["subnet_id"]
104                 for v4 in formats:
105                     if v4[2] != network_type:
106                         continue
107
108                     # get the param or resource
109                     if network_type == "external":
110                         param_or_res = subnet_id["get_param"]
111                     elif network_type == "internal":
112                         if subnet_id.get("get_param"):
113                             param_or_res = subnet_id["get_param"]
114                         elif subnet_id.get("get_resource"):
115                             param_or_res = subnet_id["get_resource"]
116                         else:
117                             continue
118                     else:
119                         continue
120
121                     m = v4[3].match(param_or_res)
122                     if m and m.group(1):
123                         snr = m.group(1)
124                         if snr.replace("_v6", "") != network_role:
125                             invalid_subnets.append(param_or_res)
126
127     assert not set(invalid_subnets)