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