[VVP] udpating scripts for casablanca
[vvp/validation-scripts.git] / ice_validator / tests / helpers.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 """Helpers
42 """
43
44 import os
45 from boltons import funcutils
46 from tests import cached_yaml as yaml
47
48 VERSION = '1.1.0'
49
50
51 def check_basename_ending(template_type, basename):
52     '''
53     return True/False if the template type is matching
54     the filename
55     '''
56     if not template_type:
57         return True
58     elif template_type == 'volume':
59         return basename.endswith('_volume')
60     else:
61         return not basename.endswith('_volume')
62
63
64 def get_parsed_yml_for_yaml_files(yaml_files, sections=None):
65     '''
66     get the parsed yaml for a list of yaml files
67     '''
68     sections = [] if sections is None else sections
69     parsed_yml_list = []
70     for yaml_file in yaml_files:
71         try:
72             with open(yaml_file) as fh:
73                 yml = yaml.load(fh)
74         except yaml.YAMLError as e:
75             # pylint: disable=superfluous-parens
76             print('Error in %s: %s' % (yaml_file, e))
77             continue
78         if yml:
79             if sections:
80                 for k in yml.keys():
81                     if k not in sections:
82                         del yml[k]
83             parsed_yml_list.append(yml)
84     return parsed_yml_list
85
86
87 def validates(*requirement_ids):
88     """Decorator that tags the test function with one or more requirement IDs.
89
90     Example:
91         >>> @validates('R-12345', 'R-12346')
92         ... def test_something():
93         ...     pass
94         >>> assert test_something.requirement_ids == ['R-12345', 'R-12346']
95     """
96     # pylint: disable=missing-docstring
97     def decorator(func):
98         # NOTE: We use a utility here to ensure that function signatures are
99         # maintained because pytest inspects function signatures to inject
100         # fixtures.  I experimented with a few options, but this is the only
101         # library that worked. Other libraries dynamically generated a
102         # function at run-time, and then lost the requirement_ids attribute
103         @funcutils.wraps(func)
104         def wrapper(*args, **kw):
105             return func(*args, **kw)
106         wrapper.requirement_ids = requirement_ids
107         return wrapper
108     decorator.requirement_ids = requirement_ids
109     return decorator
110
111
112 def get_environment_pair(heat_template):
113     """Returns a yaml/env pair given a yaml file"""
114     base_dir, filename = os.path.split(heat_template)
115     basename = os.path.splitext(filename)[0]
116     env_template = os.path.join(base_dir, "{}.env".format(basename))
117     if os.path.exists(env_template):
118         with open(heat_template, "r") as fh:
119             yyml = yaml.load(fh)
120         with open(env_template, "r") as fh:
121             eyml = yaml.load(fh)
122
123         environment_pair = {"name": basename, "yyml": yyml, "eyml": eyml}
124         return environment_pair
125
126     return None