Merge "[VVP] Creating table for RST ingestion"
[vvp/validation-scripts.git] / ice_validator / tests / test_vm_type_case.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 #
39
40 """
41 resources:
42 {vm-type}_{vm-type_index}_{network-role}_port_{port-index}:
43   type: OS::Neutron::Port
44   properties:
45     network: { get_param: ...}
46     fixed_ips: [ { "ipaddress": { get_param: ... } } ]
47     binding:vnic_type: direct           #only SR-IOV ports, not OVS ports
48     value_specs: {
49       vlan_filter: { get_param: ... },  #all NC ports
50       public_vlans: { get_param: ... }, #all NC ports
51       private_vlans: { get_param: ... },#all NC ports
52       guest_vlans: { get_param: ... },  #SR-IOV Trunk Port only
53       vlan_mirror: { get_param: ... },  #SRIOV Trunk Port
54                                         # Receiving Mirrored Traffic only
55      ATT_FABRIC_CONFIGURATION_REQUIRED: true #all NC ports
56     }
57   metadata:
58     port_type: SR-IOV_Trunk             #SR-IOV Trunk Port
59     port_type: SR-IOV_Non_Trunk         #SR-IOV Non Trunk Port
60     port_type: OVS                      #OVS Port
61     port_type: SR-IOV_Mirrored_Trunk    #SR-IOV Trunk Port
62                                         # Receiving Mirrored Traffic
63 """
64
65 import collections
66 import re
67
68 import pytest
69
70 from .structures import Heat
71 from .helpers import validates
72
73 VERSION = "1.2.0"
74
75
76 def case_mismatch(vm_type, param):
77     """Return True if vm_type matches a portion of param in a case
78     insensitive search, but does not equal that portion;
79     return False otherwise.
80     The "portions" of param are delimited by "_".
81     """
82     re_portion = re.compile(
83         "(^(%(x)s)_)|(_(%(x)s)_)|(_(%(x)s)$)" % dict(x=vm_type), re.IGNORECASE
84     )
85     found = re_portion.search(param)
86     if found:
87         param_vm_type = [x for x in found.groups()[1::2] if x][0]
88         return param_vm_type != vm_type
89     else:
90         return False
91
92
93 # pylint: disable=invalid-name
94
95
96 @validates("R-32394")
97 def test_vm_type_case(heat_template):
98     """
99     A VNF's Heat Orchestration Template's use of ``{vm-type}`` in all Resource
100     property parameter names **MUST** be the same case.
101     """
102     heat = Heat(filepath=heat_template)
103     resources = heat.resources
104     if not resources:
105         pytest.skip("No resources found")
106     bad = collections.defaultdict(list)
107     for rid, resource in resources.items():
108         vm_type = heat.get_vm_type(rid, resource=resource)
109         if vm_type:
110             properties = resource.get("properties")
111             if isinstance(properties, dict):
112                 for prop, dic in properties.items():
113                     param = heat.nested_get(dic, "get_param")
114                     if isinstance(param, list):
115                         param = param[0]
116                     if isinstance(param, str) and case_mismatch(vm_type, param):
117                         bad[(rid, vm_type)].append((prop, param))
118     if bad:
119         raise AssertionError(
120             "vm-type/parameter case mis-match %s"
121             % (
122                 "; ".join(
123                     "resource: %s vm-type: %s %s"
124                     % (k[0], k[1], ", ".join("%s: %s" % i for i in v))
125                     for k, v in bad.items()
126                 )
127             )
128         )