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