Merge "[k8s] Run the tests using bluval"
[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 from pathlib import Path
24 import click
25 import yaml
26
27 def run_testcase(testcase):
28     """Runs a single testcase
29     """
30     show_stopper = testcase.get('show_stopper', False)
31     what = testcase.get('what')
32     mypath = Path(__file__).absolute()
33     results_path = mypath.parents[2].joinpath("results/"+testcase.get('layer')+"/"+what)
34     test_path = mypath.parents[1].joinpath("tests/"+testcase.get('layer')+"/"+what)
35
36     # add to the variables file the path to where to sotre the logs
37     variables_file = mypath.parents[1].joinpath("tests/variables.yaml")
38     variables_dict = yaml.safe_load(variables_file.open())
39     variables_dict['log_path'] = str(results_path)
40     variables_file.write_text(str(variables_dict))
41
42     # run the test
43     args = ["robot", "-V", str(variables_file), "-d", str(results_path), str(test_path)]
44
45     print('Executing testcase {}'.format(testcase['name']))
46     print('          show_stopper {}'.format(show_stopper))
47     print('Invoking {}'.format(args))
48     try:
49         status = subprocess.call(args, shell=False)
50         if status != 0 and show_stopper:
51             print('Show stopper testcase failed')
52             return status
53     except OSError:
54         print('Error while executing {}'.format(args))
55         return -1
56     return status
57
58
59 def validate_layer(blueprint, layer):
60     """validates a layer by validating all testcases under that layer
61     """
62     print('## Layer {}'.format(layer))
63     for testcase in blueprint[layer]:
64         testcase['layer'] = layer
65         run_testcase(testcase)
66
67
68 def validate_blueprint(yaml_loc, layer):
69     """Parse yaml file and validates given layer. If no layer given all layers
70     validated
71     """
72     with open(yaml_loc) as yaml_file:
73         yamldoc = yaml.safe_load(yaml_file)
74     blueprint = yamldoc['blueprint']
75     if layer is None:
76         for each_layer in blueprint['layers']:
77             validate_layer(blueprint, each_layer)
78     else:
79         validate_layer(blueprint, layer)
80
81
82 @click.command()
83 @click.argument('blueprint')
84 @click.option('--layer', '-l')
85 def main(blueprint, layer):
86     """Takes blueprint name and optional layer. Validates inputs and derives
87     yaml location from blueprint name. Invokes validate on blue print.
88     """
89     mypath = Path(__file__).absolute()
90     yaml_loc = mypath.parents[0].joinpath('bluval-{}.yaml'.format(blueprint))
91     if layer is not None:
92         layer = layer.lower()
93     validate_blueprint(yaml_loc, layer)
94
95
96 if __name__ == "__main__":
97     # pylint: disable=no-value-for-parameter
98     main()