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