saul-romero-arm | ce968d6 | 2021-05-18 15:01:12 +0000 | [diff] [blame] | 1 | ############################################################################## |
| 2 | # Copyright (c) 2021, ARM Limited and Contributors. All rights reserved. |
| 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | ############################################################################## |
| 6 | """ |
| 7 | Class to parse .yaml file to generate a report.db |
| 8 | """ |
| 9 | import sys |
| 10 | import yaml |
| 11 | import adaptors.sql.sqlite as sqlite |
| 12 | |
| 13 | class YAMLParser: |
| 14 | """ |
| 15 | Class to represent a YAML Parser and creates database |
| 16 | |
| 17 | Methods: |
| 18 | create_table: Creates sqlite db table with necessary fields. |
| 19 | parse_file: Parses the yaml file to obtain necessary data for the test result table and updates it. |
| 20 | update_test_config_table: Parses the yaml file to obtain necessary data fot the test config table and updates it |
| 21 | """ |
| 22 | root_string = "" |
| 23 | test_suite_list = [] |
| 24 | |
| 25 | # contents of the test_config table |
| 26 | test_config_table = [ |
| 27 | "build_id", |
| 28 | "target", |
| 29 | "bitbake_version", |
| 30 | "yocto_version" |
| 31 | ] |
| 32 | |
| 33 | # contents of test_result table |
| 34 | test_result_table = [ |
| 35 | "build_id", |
| 36 | "date", |
| 37 | "test_suite", |
| 38 | "test_case", |
| 39 | "result" |
| 40 | ] |
| 41 | |
| 42 | def __init__(self, file_name=""): |
| 43 | """Creates an instance for sqlite_obj and loads the contents of the yamlfile to be parsed """ |
| 44 | |
| 45 | try: |
| 46 | self.sqlite_obj = sqlite.Database("report.db") |
| 47 | with open(file_name) as file: |
| 48 | self.contents = yaml.load(file) |
| 49 | self.root_string = [i for i in self.contents.keys()][0] |
| 50 | except Exception as err: |
| 51 | print(err) |
| 52 | |
| 53 | def create_table(self): |
| 54 | """Creates empty tables in the sqlite database from the contents of test_config_table and test_result_table""" |
| 55 | |
| 56 | test_config_query = """ |
| 57 | CREATE TABLE `test_configuration` ( |
| 58 | {0} TEXT, |
| 59 | {1} TEXT, |
| 60 | {2} TEXT, |
| 61 | {3} TEXT, |
| 62 | PRIMARY KEY ({0}) |
| 63 | ); |
| 64 | """.format(self.test_config_table[0], self.test_config_table[1], self.test_config_table[2], |
| 65 | self.test_config_table[3]) |
| 66 | |
| 67 | test_results_query = """ |
| 68 | CREATE TABLE `test_results` ( |
| 69 | {0} TEXT, |
| 70 | {1} TEXT, |
| 71 | {2} TEXT, |
| 72 | {3} TEXT, |
| 73 | {4} TEXT, |
| 74 | FOREIGN KEY ({0}) REFERENCES `test_configuration`({0}) |
| 75 | ); |
| 76 | """.format(self.test_result_table[0], self.test_result_table[1], self.test_result_table[2], |
| 77 | self.test_result_table[3], self.test_result_table[4]) |
| 78 | |
| 79 | self.sqlite_obj.execute_query(test_config_query) |
| 80 | self.sqlite_obj.execute_query(test_results_query) |
| 81 | |
| 82 | def parse_file(self): |
| 83 | """Parses the yaml file""" |
| 84 | |
| 85 | build_id = self.contents[self.root_string]['metadata']['CI_PIPELINE_ID'] |
| 86 | for test_suite in self.contents[self.root_string]['test-suites'].keys(): |
| 87 | date = self.contents[self.root_string]['test-suites'][test_suite]['metadata']['DATE'] |
| 88 | for test_case in self.contents[self.root_string]['test-suites'][test_suite]['test-results'].keys(): |
| 89 | result = self.contents[self.root_string]['test-suites'][test_suite]['test-results'][test_case]["status"] |
| 90 | update_result_table_query = "INSERT INTO test_results VALUES ('{0}', '{1}', '{2}', '{3}', '{4}')". \ |
| 91 | format(build_id, date, test_suite, test_case, result) |
| 92 | self.sqlite_obj.execute_query(update_result_table_query) |
| 93 | |
| 94 | def update_test_config_table(self): |
| 95 | """Updates tables in the report.db with the values from the yaml file""" |
| 96 | |
| 97 | build_id = self.contents[self.root_string]['metadata']['CI_PIPELINE_ID'] |
| 98 | target = self.contents[self.root_string]['target']['platform'] + \ |
| 99 | "_" + self.contents[self.root_string]['target']['version'] |
| 100 | |
| 101 | bitbake_version = "UNAVAILABLE" |
| 102 | yocto_version = "UNAVAILABLE" |
| 103 | update_table_query = "INSERT INTO test_configuration VALUES ('{0}', '{1}', '{2}', '{3}')".\ |
| 104 | format(build_id, target, bitbake_version, yocto_version) |
| 105 | self.sqlite_obj.execute_query(update_table_query) |
| 106 | |
| 107 | |
| 108 | if __name__ == "__main__": |
| 109 | yaml_obj = YAMLParser() |
| 110 | yaml_obj.create_table() |
| 111 | yaml_obj.parse_file() |
| 112 | yaml_obj.update_test_config_table() |