Nikita | 4d9d4e5 | 2021-04-08 11:38:50 +0100 | [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 sqlite
|
| 12 |
|
| 13 |
|
| 14 | class YAMLParser:
|
| 15 | """
|
| 16 | Class to represent a YAML Parser and creates database
|
| 17 |
|
| 18 | Methods:
|
| 19 | create_table: Creates sqlite db table with necessary fields.
|
| 20 | parse_file: Parses the yaml file to obtain necessary data for the test result table and updates it.
|
| 21 | update_test_config_table: Parses the yaml file to obtain necessary data fot the test config table and updates it
|
| 22 | """
|
| 23 | root_string = ""
|
| 24 | test_suite_list = []
|
| 25 |
|
| 26 | # contents of the test_config table
|
| 27 | test_config_table = [
|
| 28 | "build_id",
|
| 29 | "target",
|
| 30 | "bitbake_version",
|
| 31 | "yocto_version"
|
| 32 | ]
|
| 33 |
|
| 34 | # contents of test_result table
|
| 35 | test_result_table = [
|
| 36 | "build_id",
|
| 37 | "date",
|
| 38 | "test_suite",
|
| 39 | "test_case",
|
| 40 | "result"
|
| 41 | ]
|
| 42 |
|
| 43 | def __init__(self, file_name=sys.argv[1]):
|
| 44 | """Creates an instance for sqlite_obj and loads the contents of the yamlfile to be parsed """
|
| 45 |
|
| 46 | try:
|
| 47 | self.sqlite_obj = sqlite.Database("report.db")
|
| 48 | with open(file_name) as file:
|
| 49 | self.contents = yaml.load(file, Loader=yaml.FullLoader)
|
| 50 | self.root_string = [i for i in self.contents.keys()][0]
|
| 51 | except Exception as err:
|
| 52 | print(err)
|
| 53 |
|
| 54 | def create_table(self):
|
| 55 | """Creates empty tables in the sqlite database from the contents of test_config_table and test_result_table"""
|
| 56 |
|
| 57 | test_config_query = """
|
| 58 | CREATE TABLE `test_configuration` (
|
| 59 | {0} TEXT,
|
| 60 | {1} TEXT,
|
| 61 | {2} TEXT,
|
| 62 | {3} TEXT,
|
| 63 | PRIMARY KEY ({0})
|
| 64 | );
|
| 65 | """.format(self.test_config_table[0], self.test_config_table[1], self.test_config_table[2],
|
| 66 | self.test_config_table[3])
|
| 67 |
|
| 68 | test_results_query = """
|
| 69 | CREATE TABLE `test_results` (
|
| 70 | {0} TEXT,
|
| 71 | {1} TEXT,
|
| 72 | {2} TEXT,
|
| 73 | {3} TEXT,
|
| 74 | {4} TEXT,
|
| 75 | FOREIGN KEY ({0}) REFERENCES `test_configuration`({0})
|
| 76 | );
|
| 77 | """.format(self.test_result_table[0], self.test_result_table[1], self.test_result_table[2],
|
| 78 | self.test_result_table[3], self.test_result_table[4])
|
| 79 |
|
| 80 | self.sqlite_obj.execute_query(test_config_query)
|
| 81 | self.sqlite_obj.execute_query(test_results_query)
|
| 82 |
|
| 83 | def parse_file(self):
|
| 84 | """Parses the yaml file"""
|
| 85 |
|
| 86 | build_id = self.contents[self.root_string]['metadata']['CI_PIPELINE_ID']
|
| 87 | # dependent on the generated yaml file. Code will be uncommented based
|
| 88 | # on the yaml file
|
| 89 | # self.contents[self.root_string]['metadata']['CI_COMMIT_TIMESTAMP']
|
| 90 | date = ""
|
| 91 | for test_suite in self.contents[self.root_string]['test-suites'].keys():
|
| 92 | for test_case in self.contents[self.root_string]['test-suites'][test_suite]['test-results'].keys():
|
| 93 | result = self.contents[self.root_string]['test-suites'][test_suite]['test-results'][test_case]["status"]
|
| 94 | update_result_table_query = "INSERT INTO test_results VALUES ('{0}', '{1}', '{2}', '{3}', '{4}')". \
|
| 95 | format(build_id, date, test_suite, test_case, result)
|
| 96 | self.sqlite_obj.execute_query(update_result_table_query)
|
| 97 |
|
| 98 | def update_test_config_table(self):
|
| 99 | """Updates tables in the report.db with the values from the yaml file"""
|
| 100 |
|
| 101 | build_id = self.contents[self.root_string]['metadata']['CI_PIPELINE_ID']
|
| 102 | target = self.contents[self.root_string]['target']['platform'] + \
|
| 103 | "_" + self.contents[self.root_string]['target']['version']
|
| 104 |
|
| 105 | bitbake_version = "1.0"
|
| 106 | yocto_version = "2.0"
|
| 107 | update_table_query = "INSERT INTO test_configuration VALUES ('{0}', '{1}', '{2}', '{3}')".\
|
| 108 | format(build_id, target, bitbake_version, yocto_version)
|
| 109 | self.sqlite_obj.execute_query(update_table_query)
|
| 110 |
|
| 111 |
|
| 112 | if __name__ == "__main__":
|
| 113 | yaml_obj = YAMLParser()
|
| 114 | yaml_obj.create_table()
|
| 115 | yaml_obj.parse_file()
|
| 116 | yaml_obj.update_test_config_table()
|