2 ##############################################################################
3 # Copyright (c) 2019 AT&T Intellectual Property. #
4 # Copyright (c) 2019 Nokia. #
6 # Licensed under the Apache License, Version 2.0 (the "License"); you may #
7 # not use this file except in compliance with the License. #
9 # You may obtain a copy of the License at #
10 # http://www.apache.org/licenses/LICENSE-2.0 #
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
25 from pathlib import Path
30 from bluutil import BluvalError
31 from bluutil import ShowStopperError
33 _OPTIONAL_ALSO = False
35 def run_testcase(testcase):
36 """Runs a single testcase
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))
45 print("_OPTIONAL_ALSO {}".format(_OPTIONAL_ALSO))
46 if not _OPTIONAL_ALSO and optional.lower() == "true":
48 print('Ignoring Optional {} testcase'.format(name))
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)
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_file.write_text(str(variables_dict))
65 args = ["robot", "-V", str(variables_file), "-d",
66 str(results_path), str(test_path)]
68 print('Executing testcase {}'.format(name))
69 print('show_stopper {}'.format(show_stopper))
70 print('Invoking {}'.format(args))
72 status = subprocess.call(args, shell=False)
73 if status != 0 and show_stopper.lower() == "true":
74 raise ShowStopperError(name)
76 #print('Error while executing {}'.format(args))
77 raise BluvalError(OSError)
80 def validate_layer(blueprint, layer):
81 """validates a layer by validating all testcases under that layer
83 print('## Layer {}'.format(layer))
84 for testcase in blueprint[layer]:
85 testcase['layer'] = layer
86 run_testcase(testcase)
89 def validate_blueprint(yaml_loc, layer):
90 """Parse yaml file and validates given layer. If no layer given all layers
93 with open(str(yaml_loc)) as yaml_file:
94 yamldoc = yaml.safe_load(yaml_file)
95 blueprint = yamldoc['blueprint']
96 validate_layer(blueprint, layer)
100 @click.argument('blueprint')
101 @click.option('--layer', '-l')
102 @click.option('--optional_also', '-o', is_flag=True)
103 def main(blueprint, layer, optional_also):
104 """Takes blueprint name and optional layer. Validates inputs and derives
105 yaml location from blueprint name. Invokes validate on blue print.
107 global _OPTIONAL_ALSO # pylint: disable=global-statement
108 mypath = Path(__file__).absolute()
109 yaml_loc = mypath.parents[0].joinpath('bluval-{}.yaml'.format(blueprint))
110 if layer is not None:
111 layer = layer.lower()
113 _OPTIONAL_ALSO = True
114 print("_OPTIONAL_ALSO {}".format(_OPTIONAL_ALSO))
117 validate_blueprint(yaml_loc, layer)
118 except ShowStopperError as err:
119 print('ShowStopperError:', err)
120 except BluvalError as err:
121 print('Unexpected BluvalError', err)
124 print("Exception in user code:")
126 traceback.print_exc(file=sys.stdout)
131 if __name__ == "__main__":
132 # pylint: disable=no-value-for-parameter