[VVP] udpating scripts for casablanca 2
[vvp/validation-scripts.git] / ice_validator / tests / test_environment_file_parameters.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 """ environment file structure
42 """
43 import re
44 import pytest
45
46 from .helpers import validates, get_environment_pair
47
48
49 VERSION = '1.0.0'
50
51 # pylint: disable=invalid-name
52
53
54 def check_parameter_exists(pattern, parameters):
55     if not parameters:
56         return False
57
58     for param in parameters:
59         if pattern.search(param):
60             return True
61
62     return False
63
64
65 def check_param_in_env_file(environment_pair, param, DESIRED):
66
67     if not environment_pair:
68         pytest.skip("No heat/env pair could be identified")
69
70     env_file = environment_pair.get("eyml")
71
72     pattern = re.compile(r'^{}$'.format(param))
73
74     if "parameters" not in env_file:
75         pytest.skip("No parameters specified in the environment file")
76
77     return check_parameter_exists(pattern,
78                                   env_file.get("parameters", {})
79                                   ) is not DESIRED
80
81
82 """
83 This function supports this structure, deviations
84 may or may not work without enhancement
85
86 resource_id:
87     type: <resource_type>
88     properties:
89         prop0: { get_param: parameter_0 }
90         prop1:  # this is a list of dicts
91             - nested_prop_0: { get_param: parameter_1 }
92             - nested_prop_1: { get_param: [parameter_2, {index}] }
93         prop2:  # this is a dict of dicts
94             nested_prop_0: { get_param: parameter_1 }
95 """
96
97
98 def check_resource_parameter(environment_pair,
99                              prop,
100                              DESIRED,
101                              resource_type,
102                              resource_type_inverse=False,
103                              nested_prop='',
104                              exclude_resource=''):
105
106     if not environment_pair:
107         pytest.skip("No heat/env pair could be identified")
108
109     env_file = environment_pair.get("eyml")
110     template_file = environment_pair.get("yyml")
111
112     if "parameters" not in env_file:
113         pytest.skip("No parameters specified in the environment file")
114
115     invalid_parameters = []
116
117     if template_file:
118         for resource, resource_prop in template_file.get("resources", {}).items():
119
120             if exclude_resource and re.match(exclude_resource, resource):
121                 continue
122
123             if resource_prop.get("type") == resource_type or \
124                 (resource_prop.get("type") != resource_type
125                  and resource_type_inverse):
126
127                 pattern = False
128
129                 if not resource_prop.get("properties"):
130                     continue
131
132                 resource_parameter = resource_prop.get("properties").get(prop)
133
134                 if not resource_parameter:
135                     continue
136
137                 if isinstance(resource_parameter, list) and nested_prop:
138                     for param in resource_parameter:
139
140                         nested_param = param.get(nested_prop)
141                         if not nested_param:
142                             continue
143
144                         pattern = nested_param.get("get_param")
145
146                         if not pattern:
147                             continue
148
149                         if isinstance(pattern, list):
150                             pattern = pattern[0]
151
152                 elif isinstance(resource_parameter, dict):
153
154                     if nested_prop and nested_prop in resource_parameter:
155                         resource_parameter = resource_parameter.get(nested_prop)
156
157                     pattern = resource_parameter.get("get_param")
158
159                 else:
160                     continue
161
162                 if not pattern:
163                     continue
164
165                 if check_param_in_env_file(environment_pair, pattern, DESIRED):
166                     invalid_parameters.append(pattern)
167
168     return set(invalid_parameters)
169
170
171 @validates('R-91125')
172 def test_nova_server_image_parameter_exists_in_environment_file(heat_template):
173
174     if pytest.config.getoption("validation_profile") == "heat_only":
175         pytest.skip("skipping test because validation profile is heat only")
176
177     environment_pair = get_environment_pair(heat_template)
178
179     prop = "image"
180     DESIRED = True
181     resource_type = "OS::Nova::Server"
182
183     invalid_parameters = check_resource_parameter(environment_pair,
184                                                   prop,
185                                                   DESIRED,
186                                                   resource_type)
187
188     assert not invalid_parameters, ("OS::Nova::Server {} parameters not"
189                                     " found in {} environment file {}"
190                                     .format(prop,
191                                             environment_pair.get("name"),
192                                             invalid_parameters))
193
194
195 @validates('R-69431')
196 def test_nova_server_flavor_parameter_exists_in_environment_file(heat_template):
197
198     if pytest.config.getoption("validation_profile") == "heat_only":
199         pytest.skip("skipping test because validation profile is heat only")
200
201     environment_pair = get_environment_pair(heat_template)
202
203     prop = "flavor"
204     DESIRED = True
205     resource_type = "OS::Nova::Server"
206
207     invalid_parameters = check_resource_parameter(environment_pair,
208                                                   prop,
209                                                   DESIRED,
210                                                   resource_type)
211
212     assert not invalid_parameters, ("OS::Nova::Server {} parameters not"
213                                     " found in {} environment file {}"
214                                     .format(prop,
215                                             environment_pair.get("name"),
216                                             invalid_parameters))
217
218
219 @validates('R-22838')
220 def test_nova_server_name_parameter_doesnt_exist_in_environment_file(heat_template):
221
222     if pytest.config.getoption("validation_profile") == "heat_only":
223         pytest.skip("skipping test because validation profile is heat only")
224
225     environment_pair = get_environment_pair(heat_template)
226
227     prop = "name"
228     DESIRED = False
229     resource_type = "OS::Nova::Server"
230
231     invalid_parameters = check_resource_parameter(environment_pair,
232                                                   prop,
233                                                   DESIRED,
234                                                   resource_type)
235
236     assert not invalid_parameters, ("OS::Nova::Server {} parameters"
237                                     " found in {} environment file {}"
238                                     .format(prop,
239                                             environment_pair.get("name"),
240                                             invalid_parameters))
241
242
243 @validates('R-59568')
244 def test_nova_server_az_parameter_doesnt_exist_in_environment_file(heat_template):
245
246     if pytest.config.getoption("validation_profile") == "heat_only":
247         pytest.skip("skipping test because validation profile is heat only")
248
249     environment_pair = get_environment_pair(heat_template)
250
251     prop = "availability_zone"
252     DESIRED = False
253     resource_type = "OS::Nova::Server"
254
255     invalid_parameters = check_resource_parameter(environment_pair,
256                                                   prop,
257                                                   DESIRED,
258                                                   resource_type)
259
260     assert not invalid_parameters, ("OS::Nova::Server {} parameters"
261                                     " found in {} environment file {}"
262                                     .format(prop,
263                                             environment_pair.get("name"),
264                                             invalid_parameters))
265
266
267 @validates('R-20856')
268 def test_nova_server_vnf_id_parameter_doesnt_exist_in_environment_file(heat_template):
269
270     if pytest.config.getoption("validation_profile") == "heat_only":
271         pytest.skip("skipping test because validation profile is heat only")
272
273     environment_pair = get_environment_pair(heat_template)
274
275     prop = "vnf_id"
276     DESIRED = False
277
278     invalid_parameters = check_param_in_env_file(environment_pair,
279                                                  prop,
280                                                  DESIRED)
281
282     assert not invalid_parameters, ("{} parameters"
283                                     " found in {} environment file {}"
284                                     .format(prop,
285                                             environment_pair.get("name"),
286                                             invalid_parameters))
287
288
289 @validates('R-72871')
290 def test_nova_server_vf_module_id_parameter_doesnt_exist_in_environment_file(heat_template):
291
292     if pytest.config.getoption("validation_profile") == "heat_only":
293         pytest.skip("skipping test because validation profile is heat only")
294
295     environment_pair = get_environment_pair(heat_template)
296
297     prop = "vf_module_id"
298     DESIRED = False
299
300     invalid_parameters = check_param_in_env_file(environment_pair,
301                                                  prop,
302                                                  DESIRED)
303
304     assert not invalid_parameters, ("{} parameters"
305                                     " found in {} environment file {}"
306                                     .format(prop,
307                                             environment_pair.get("name"),
308                                             invalid_parameters))
309
310
311 @validates('R-36542')
312 def test_nova_server_vnf_name_parameter_doesnt_exist_in_environment_file(heat_template):
313
314     if pytest.config.getoption("validation_profile") == "heat_only":
315         pytest.skip("skipping test because validation profile is heat only")
316
317     environment_pair = get_environment_pair(heat_template)
318
319     prop = "vnf_name"
320     DESIRED = False
321
322     invalid_parameters = check_param_in_env_file(environment_pair,
323                                                  prop,
324                                                  DESIRED)
325
326     assert not invalid_parameters, ("{} parameters"
327                                     " found in {} environment file {}"
328                                     .format(prop,
329                                             environment_pair.get("name"),
330                                             invalid_parameters))
331
332
333 @validates('R-80374')
334 def test_nova_server_vf_module_name_parameter_doesnt_exist_in_environment_file(heat_template):
335
336     if pytest.config.getoption("validation_profile") == "heat_only":
337         pytest.skip("skipping test because validation profile is heat only")
338
339     environment_pair = get_environment_pair(heat_template)
340
341     prop = "vf_module_name"
342     DESIRED = False
343
344     invalid_parameters = check_param_in_env_file(environment_pair,
345                                                  prop,
346                                                  DESIRED)
347
348     assert not invalid_parameters, ("{} parameters"
349                                     " found in {} environment file {}"
350                                     .format(prop,
351                                             environment_pair.get("name"),
352                                             invalid_parameters))
353
354
355 @validates('R-02691')
356 def test_nova_server_workload_context_parameter_doesnt_exist_in_environment_file(heat_template):
357
358     if pytest.config.getoption("validation_profile") == "heat_only":
359         pytest.skip("skipping test because validation profile is heat only")
360
361     environment_pair = get_environment_pair(heat_template)
362
363     prop = "workload_context"
364     DESIRED = False
365
366     invalid_parameters = check_param_in_env_file(environment_pair,
367                                                  prop,
368                                                  DESIRED)
369
370     assert not invalid_parameters, ("{} parameters"
371                                     " found in {} environment file {}"
372                                     .format(prop,
373                                             environment_pair.get("name"),
374                                             invalid_parameters))
375
376
377 @validates('R-13194')
378 def test_nova_server_environment_context_parameter_doesnt_exist_in_environment_file(heat_template):
379
380     if pytest.config.getoption("validation_profile") == "heat_only":
381         pytest.skip("skipping test because validation profile is heat only")
382
383     environment_pair = get_environment_pair(heat_template)
384
385     prop = "environment_context"
386     DESIRED = False
387
388     invalid_parameters = check_param_in_env_file(environment_pair,
389                                                  prop,
390                                                  DESIRED)
391
392     assert not invalid_parameters, ("{} parameters"
393                                     " found in {} environment file {}"
394                                     .format(prop,
395                                             environment_pair.get("name"),
396                                             invalid_parameters))
397
398
399 @validates('R-29872')
400 def test_nova_server_network_parameter_doesnt_exist_in_environment_file(heat_template):
401
402     if pytest.config.getoption("validation_profile") == "heat_only":
403         pytest.skip("skipping test because validation profile is heat only")
404
405     environment_pair = get_environment_pair(heat_template)
406
407     prop = "networks"
408     nested_prop = "network"
409     DESIRED = False
410     resource_type = "OS::Nova::Server"
411
412     invalid_parameters = check_resource_parameter(environment_pair,
413                                                   prop,
414                                                   DESIRED,
415                                                   resource_type,
416                                                   nested_prop=nested_prop)
417
418     assert not invalid_parameters, ("{} {} parameters"
419                                     " found in {} environment file {}"
420                                     .format(resource_type,
421                                             nested_prop,
422                                             environment_pair.get("name"),
423                                             invalid_parameters))
424
425
426 @validates('R-39841',
427            'R-87123',
428            'R-28795',
429            'R-97201',
430            'R-62590',
431            'R-93496',
432            'R-98905',
433            'R-93030',
434            'R-90206',
435            'R-98569',
436            'R-62590',
437            'R-93496')
438 def test_neutron_port_fixedips_ipaddress_parameter_doesnt_exist_in_environment_file(heat_template):
439
440     if pytest.config.getoption("validation_profile") == "heat_only":
441         pytest.skip("skipping test because validation profile is heat only")
442
443     environment_pair = get_environment_pair(heat_template)
444
445     prop = "fixed_ips"
446     nested_prop = "ip_address"
447     DESIRED = False
448     resource_type = "OS::Neutron::Port"
449
450     invalid_parameters = check_resource_parameter(environment_pair,
451                                                   prop,
452                                                   DESIRED,
453                                                   resource_type,
454                                                   nested_prop=nested_prop)
455
456     assert not invalid_parameters, ("{} {} parameters"
457                                     " found in {} environment file {}"
458                                     .format(resource_type,
459                                             nested_prop,
460                                             environment_pair.get("name"),
461                                             invalid_parameters))
462
463
464 @validates('R-83677',
465            'R-80829',
466            'R-69634',
467            'R-22288')
468 def test_neutron_port_fixedips_subnet_parameter_doesnt_exist_in_environment_file(heat_template):
469
470     if pytest.config.getoption("validation_profile") == "heat_only":
471         pytest.skip("skipping test because validation profile is heat only")
472
473     environment_pair = get_environment_pair(heat_template)
474
475     prop = "fixed_ips"
476     nested_prop = "subnet_id"
477     DESIRED = False
478     resource_type = "OS::Neutron::Port"
479
480     invalid_parameters = check_resource_parameter(environment_pair,
481                                                   prop,
482                                                   DESIRED,
483                                                   resource_type,
484                                                   nested_prop=nested_prop)
485
486     assert not invalid_parameters, ("{} {} parameters"
487                                     " found in {} environment file {}"
488                                     .format(resource_type,
489                                             nested_prop,
490                                             environment_pair.get("name"),
491                                             invalid_parameters))
492
493
494 @validates('R-83412',
495            'R-83418')
496 def test_neutron_port_aap_ip_parameter_doesnt_exist_in_environment_file(heat_template):
497
498     if pytest.config.getoption("validation_profile") == "heat_only":
499         pytest.skip("skipping test because validation profile is heat only")
500
501     environment_pair = get_environment_pair(heat_template)
502
503     prop = "allowed_address_pairs"
504     nested_prop = "ip_address"
505     DESIRED = False
506     resource_type = "OS::Neutron::Port"
507
508     invalid_parameters = check_resource_parameter(environment_pair,
509                                                   prop,
510                                                   DESIRED,
511                                                   resource_type,
512                                                   nested_prop=nested_prop)
513
514     assert not invalid_parameters, ("{} {} parameters"
515                                     " found in {} environment file {}"
516                                     .format(resource_type,
517                                             nested_prop,
518                                             environment_pair.get("name"),
519                                             invalid_parameters))
520
521
522 @validates('R-99812')
523 def test_non_nova_server_name_parameter_doesnt_exist_in_environment_file(heat_template):
524
525     if pytest.config.getoption("validation_profile") == "heat_only":
526         pytest.skip("skipping test because validation profile is heat only")
527
528     environment_pair = get_environment_pair(heat_template)
529
530     prop = "name"
531     DESIRED = False
532     resource_type = "OS::Nova::Server"
533
534     invalid_parameters = check_resource_parameter(environment_pair,
535                                                   prop,
536                                                   DESIRED,
537                                                   resource_type,
538                                                   resource_type_inverse=True)
539
540     assert not invalid_parameters, ("non-{} {} parameters"
541                                     " found in {} environment file {}"
542                                     .format(resource_type,
543                                             prop,
544                                             environment_pair.get("name"),
545                                             invalid_parameters))
546
547
548 @validates('R-92193')
549 def test_network_fqdn_parameter_doesnt_exist_in_environment_file(heat_template):
550
551     if pytest.config.getoption("validation_profile") == "heat_only":
552         pytest.skip("skipping test because validation profile is heat only")
553
554     environment_pair = get_environment_pair(heat_template)
555
556     prop = r'^(.+?)_net_fqdn$'
557     DESIRED = False
558
559     invalid_parameters = check_param_in_env_file(environment_pair,
560                                                  prop,
561                                                  DESIRED)
562
563     assert not invalid_parameters, ("{} parameters"
564                                     " found in {} environment file {}"
565                                     .format(prop,
566                                             environment_pair.get("name"),
567                                             invalid_parameters))
568
569
570 @validates('R-76682')
571 def test_contrail_route_prefixes_parameter_doesnt_exist_in_environment_file(heat_template):
572
573     if pytest.config.getoption("validation_profile") == "heat_only":
574         pytest.skip("skipping test because validation profile is heat only")
575
576     environment_pair = get_environment_pair(heat_template)
577
578     prop = "interface_route_table_routes"
579     nested_prop = "interface_route_table_routes_route"
580     DESIRED = False
581     resource_type = "OS::ContrailV2::InterfaceRouteTable"
582
583     invalid_parameters = check_resource_parameter(environment_pair,
584                                                   prop,
585                                                   DESIRED,
586                                                   resource_type,
587                                                   nested_prop=nested_prop)
588
589     assert not invalid_parameters, ("{} {} parameters"
590                                     " found in {} environment file {}"
591                                     .format(resource_type,
592                                             nested_prop,
593                                             environment_pair.get("name"),
594                                             invalid_parameters))
595
596
597 @validates('R-50011')
598 def test_heat_rg_count_parameter_exists_in_environment_file(heat_template):
599
600     if pytest.config.getoption("validation_profile") == "heat_only":
601         pytest.skip("skipping test because validation profile is heat only")
602
603     environment_pair = get_environment_pair(heat_template)
604
605     prop = "count"
606     DESIRED = True
607     resource_type = "OS::Heat::ResourceGroup"
608     exclude_resource = re.compile(r'^(.+?)_subint_(.+?)_port_(.+?)_subinterfaces$')
609
610     invalid_parameters = check_resource_parameter(environment_pair,
611                                                   prop,
612                                                   DESIRED,
613                                                   resource_type,
614                                                   exclude_resource=exclude_resource)
615
616     assert not invalid_parameters, ("{} {} parameters not"
617                                     " found in {} environment file {}"
618                                     .format(resource_type,
619                                             prop,
620                                             environment_pair.get("name"),
621                                             invalid_parameters))