[VVP] add bug fixes and reserve port updates
[vvp/validation-scripts.git] / ice_validator / tests / test_reserve_port_fixed_ips_has_base_outputs.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 from os import path, sep
43 import yaml
44 from .utils.ports import is_reserved_port
45
46
47 def test_reserve_port_fixed_ips_has_base_outputs(heat_template):
48     '''
49     Make sure all fixed ips specified in reserved ports are
50     also exported as outputs in the same base template
51     '''
52     basename = path.splitext(heat_template)[0].rsplit(sep, 1)[1]
53     if not (basename.endswith("_base") or
54             basename.startswith("base_") or
55             basename.find("_base_") > 0):
56             pytest.skip("Skipping as it is not a base template")
57
58     # parse the yml
59     with open(heat_template) as fh:
60         yml = yaml.load(fh)
61
62     # get the outputs
63     try:
64         outputs = yml["outputs"]
65     except (TypeError, KeyError):
66         outputs = {}
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_fixed_ips = []
73     for k1, v1 in yml["resources"].items():
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 not is_reserved_port(k1):
81             continue
82
83         for k2, v2 in v1["properties"].items():
84             if k2 != "fixed_ips":
85                 continue
86             for v3 in v2:
87                 if "ip_address" not in v3:
88                     continue
89                 if "get_param" not in v3["ip_address"]:
90                     continue
91
92                 param = v3["ip_address"]["get_param"]
93
94                 # construct the expected output param
95                 if 'v6' in param:
96                     output_param = param.replace('floating_v6_ip', 'v6_vip')
97                 else:
98                     output_param = param.replace('floating_ip', 'vip')
99
100                 # check the output is constructed correctly
101                 try:
102                     output_vip = outputs[output_param]
103                     if not output_vip:
104                         invalid_fixed_ips.append(param)
105                     else:
106                         # make sure the value is set properly using the
107                         # original param value
108                         output_value_param = output_vip["value"]["get_param"]
109                         if output_value_param != param:
110                             invalid_fixed_ips.append(param)
111                 except (TypeError, KeyError):
112                     invalid_fixed_ips.append(param)
113
114     assert not set(invalid_fixed_ips)