yaml parser for 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 sections, testcases and executes each
19 testcase
20 """
21
22 import subprocess
23 import sys
24 import yaml
25
26 def run_testcase(testcase):
27     """Runs a single testcase
28     """
29     show_stopper = testcase.get('show_stopper', False)
30
31     print('Executing testcase {}'.format(testcase['name']))
32     print('          show_stopper {}'.format(show_stopper))
33     cmd = 'robot {}'.format(testcase['what'])
34     print('Invoking {}'.format(cmd))
35     try:
36         status = subprocess.call(cmd, shell=True)
37         if status != 0 and testcase['show_stopper']:
38             print('Show stopper testcase failed')
39             return status
40     except OSError:
41         print('Error while executing {}'.format(cmd))
42         return -1
43     return status
44
45
46 def parse_yaml(testcase_loc):
47     """Parse yaml file and do tasks on each testcase
48     """
49     with open(testcase_loc) as testcase_file:
50         testcases = yaml.safe_load(testcase_file)
51     blueprint = testcases['blueprint']
52     for section in blueprint['sections']:
53         print('## Section {}'.format(section))
54         for testcase in blueprint[section]:
55             run_testcase(testcase)
56
57
58 if __name__ == "__main__":
59     if len(sys.argv) != 2:
60         print('usage: bluval.py <testcase.yaml>')
61         sys.exit(1)
62     parse_yaml(sys.argv[1])