change if bad to assert not bad, message
[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 from .structures import Heat
69 from .helpers import validates
70
71 VERSION = "1.2.0"
72
73
74 def case_mismatch(vm_type, param):
75     """Return True if vm_type matches a portion of param in a case
76     insensitive search, but does not equal that portion;
77     return False otherwise.
78     The "portions" of param are delimited by "_".
79     """
80     re_portion = re.compile(
81         "(^(%(x)s)_)|(_(%(x)s)_)|(_(%(x)s)$)" % dict(x=vm_type), re.IGNORECASE
82     )
83     found = re_portion.search(param)
84     if found:
85         param_vm_type = [x for x in found.groups()[1::2] if x][0]
86         return param_vm_type != vm_type
87     else:
88         return False
89
90
91 # pylint: disable=invalid-name
92
93
94 @validates("R-32394")
95 def test_vm_type_case(yaml_file):
96     """
97     A VNF's Heat Orchestration Template's use of ``{vm-type}`` in all Resource
98     property parameter names **MUST** be the same case.
99     """
100     heat = Heat(filepath=yaml_file)
101     resources = heat.resources
102     bad = collections.defaultdict(list)
103     for rid, resource in resources.items():
104         vm_type = heat.get_vm_type(rid, resource=resource)
105         if vm_type:
106             properties = resource.get("properties")
107             if isinstance(properties, dict):
108                 for prop, dic in properties.items():
109                     param = heat.nested_get(dic, "get_param")
110                     if isinstance(param, list):
111                         param = param[0]
112                     if isinstance(param, str) and case_mismatch(vm_type, param):
113                         bad[(rid, vm_type)].append((prop, param))
114
115     msg = 'vm-type/parameter case mis-match %s' \
116         % '; '.join('resource: %s vm-type: %s %s' % (k[0], k[1],
117                     ', '.join('%s: %s' % i for i in v)) for (k, v) in
118                     bad.items())
119
120     assert not bad, msg