Merge "Replace logging with services layer"
[validation.git] / bluval / bluval.py
1 #!/usr/bin/python3
2 ##############################################################################
3 # Copyright (c) 2019 AT&T Intellectual Property.                             #
4 # Copyright (c) 2019 Nokia.                                                  #
5 #                                                                            #
6 # Licensed under the Apache License, Version 2.0 (the "License"); you may    #
7 # not use this file except in compliance with the License.                   #
8 #                                                                            #
9 # You may obtain a copy of the License at                                    #
10 #       http://www.apache.org/licenses/LICENSE-2.0                           #
11 #                                                                            #
12 # Unless required by applicable law or agreed to in writing, software        #
13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT  #
14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.           #
15 # See the License for the specific language governing permissions and        #
16 # limitations under the License.                                             #
17 ##############################################################################
18 """This module parses yaml file, reads layers, testcases and executes each
19 testcase
20 """
21
22 import subprocess
23 import sys
24 import traceback
25 from pathlib import Path
26
27 import click
28 import yaml
29
30 from bluutil import BluvalError
31 from bluutil import ShowStopperError
32
33 _OPTIONAL_ALSO = False
34
35 def run_testcase(testcase):
36     """Runs a single testcase
37     """
38     name = testcase.get('name')
39     skip = testcase.get('skip', "False")
40     optional = testcase.get('optional', "False")
41     if skip.lower() == "true":
42         # skip is mentioned and true.
43         print('Skipping {}'.format(name))
44         return
45     print("_OPTIONAL_ALSO {}".format(_OPTIONAL_ALSO))
46     if  not _OPTIONAL_ALSO and optional.lower() == "true":
47         # Optional Test case.
48         print('Ignoring Optional {} testcase'.format(name))
49         return
50     show_stopper = testcase.get('show_stopper', "False")
51     what = testcase.get('what')
52     mypath = Path(__file__).absolute()
53     results_path = mypath.parents[2].joinpath(
54         "results/"+testcase.get('layer')+"/"+what)
55     test_path = mypath.parents[1].joinpath(
56         "tests/"+testcase.get('layer')+"/"+what)
57
58     # add to the variables file the path to where to sotre the logs
59     variables_file = mypath.parents[1].joinpath("tests/variables.yaml")
60     variables_dict = yaml.safe_load(variables_file.open())
61     variables_dict['log_path'] = str(results_path)
62     variables_updated_file = mypath.parents[1].joinpath("tests/variables_updated.yaml")
63     variables_updated_file.write_text(str(variables_dict))
64
65     # run the test
66     args = ["robot", "-V", str(variables_updated_file), "-d", str(results_path),
67             "-b", "debug.log", str(test_path)]
68
69     print('Executing testcase {}'.format(name))
70     print('show_stopper {}'.format(show_stopper))
71     print('Invoking {}'.format(args))
72     try:
73         status = subprocess.call(args, shell=False)
74         if status != 0 and show_stopper.lower() == "true":
75             raise ShowStopperError(name)
76     except OSError:
77         #print('Error while executing {}'.format(args))
78         raise BluvalError(OSError)
79
80
81 def validate_layer(blueprint, layer):
82     """validates a layer by validating all testcases under that layer
83     """
84     print('## Layer {}'.format(layer))
85     for testcase in blueprint[layer]:
86         testcase['layer'] = layer
87         run_testcase(testcase)
88
89
90 def validate_blueprint(yaml_loc, layer):
91     """Parse yaml file and validates given layer. If no layer given all layers
92     validated
93     """
94     with open(str(yaml_loc)) as yaml_file:
95         yamldoc = yaml.safe_load(yaml_file)
96     blueprint = yamldoc['blueprint']
97     validate_layer(blueprint, layer)
98
99
100 def write_test_info(layer):
101     """writes testing info to test_info.yaml
102     """
103     data = dict(
104         test_info=dict(
105             layer=layer,
106             optional=_OPTIONAL_ALSO,
107         )
108     )
109
110     with open('/opt/akraino/results/test_info.yaml', 'w') as outfile:
111         yaml.dump(data, outfile, default_flow_style=False)
112
113
114 @click.command()
115 @click.argument('blueprint')
116 @click.option('--layer', '-l')
117 @click.option('--optional_also', '-o', is_flag=True)
118 def main(blueprint, layer, optional_also):
119     """Takes blueprint name and optional layer. Validates inputs and derives
120     yaml location from blueprint name. Invokes validate on blue print.
121     """
122     global _OPTIONAL_ALSO  # pylint: disable=global-statement
123     mypath = Path(__file__).absolute()
124     yaml_loc = mypath.parents[0].joinpath('bluval-{}.yaml'.format(blueprint))
125     if layer is not None:
126         layer = layer.lower()
127     if optional_also:
128         _OPTIONAL_ALSO = True
129         print("_OPTIONAL_ALSO {}".format(_OPTIONAL_ALSO))
130
131     try:
132         write_test_info(layer)
133         validate_blueprint(yaml_loc, layer)
134     except ShowStopperError as err:
135         print('ShowStopperError:', err)
136     except BluvalError as err:
137         print('Unexpected BluvalError', err)
138         raise
139     except:
140         print("Exception in user code:")
141         print("-"*60)
142         traceback.print_exc(file=sys.stdout)
143         print("-"*60)
144         raise
145
146
147 if __name__ == "__main__":
148     # pylint: disable=no-value-for-parameter
149     main()