[VVP] stand alone tool, script updates
[vvp/validation-scripts.git] / ice_validator / tests / test_server_and_port_vm_indices_match.py
1 # -*- coding: utf8 -*-
2 # ============LICENSE_START=======================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2019 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 import re
39
40 from tests.helpers import validates
41 from tests.structures import Heat
42 from tests.utils import nested_dict
43
44 SERVER_ID_PATTERN = re.compile(r"\w+_server_(\d+)")
45 PORT_ID_PATTERN = re.compile(r"\w+_(\d+)_\w+\d+")
46
47
48 def get_ports(server):
49     props = server.get("properties") or {}
50     networks = props.get("networks") or []
51     for network in networks:
52         r_id = nested_dict.get(network, "port", "get_resource")
53         if r_id:
54             yield r_id
55
56
57 @validates("R-304011")
58 def test_server_and_port_vmtype_indices_match(yaml_file):
59     # NOTE: This test is only going to validate that the index values
60     # match between the between the ports and server names.  Other
61     # tests already cover the other aspects of this requirement
62
63     heat = Heat(filepath=yaml_file)
64     servers = heat.get_resource_by_type("OS::Nova::Server")
65     errors = []
66     for r_id, server in servers.items():
67         match = SERVER_ID_PATTERN.match(r_id)
68         if not match:
69             continue  # other tests cover valid server ID format
70         server_index = match.group(1)
71         ports = get_ports(server)
72         for port in ports:
73             port_match = PORT_ID_PATTERN.match(port)
74             if port_match:
75                 port_vm_index = port_match.group(1)
76                 if port_vm_index != server_index:
77                     errors.append(
78                         (
79                             "{{vm-type_index}} ({}) in port ID ({}) "
80                             + "does not match the {{index}} ({}) in the "
81                             + "servers resource ID ({})"
82                         ).format(port_vm_index, port, server_index, r_id)
83                     )
84     assert not errors, ". ".join(errors)