Marco Zecchini
/
Example_RTOS
Rtos API example
mbed-os/tools/test_db.py@0:9fca2b23d0ba, 2019-02-23 (annotated)
- Committer:
- marcozecchini
- Date:
- Sat Feb 23 12:13:36 2019 +0000
- Revision:
- 0:9fca2b23d0ba
final commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcozecchini | 0:9fca2b23d0ba | 1 | """ |
marcozecchini | 0:9fca2b23d0ba | 2 | mbed SDK |
marcozecchini | 0:9fca2b23d0ba | 3 | Copyright (c) 2011-2014 ARM Limited |
marcozecchini | 0:9fca2b23d0ba | 4 | |
marcozecchini | 0:9fca2b23d0ba | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
marcozecchini | 0:9fca2b23d0ba | 6 | you may not use this file except in compliance with the License. |
marcozecchini | 0:9fca2b23d0ba | 7 | You may obtain a copy of the License at |
marcozecchini | 0:9fca2b23d0ba | 8 | |
marcozecchini | 0:9fca2b23d0ba | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
marcozecchini | 0:9fca2b23d0ba | 10 | |
marcozecchini | 0:9fca2b23d0ba | 11 | Unless required by applicable law or agreed to in writing, software |
marcozecchini | 0:9fca2b23d0ba | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
marcozecchini | 0:9fca2b23d0ba | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
marcozecchini | 0:9fca2b23d0ba | 14 | See the License for the specific language governing permissions and |
marcozecchini | 0:9fca2b23d0ba | 15 | limitations under the License. |
marcozecchini | 0:9fca2b23d0ba | 16 | |
marcozecchini | 0:9fca2b23d0ba | 17 | Author: Przemyslaw Wirkus <Przemyslaw.Wirkus@arm.com> |
marcozecchini | 0:9fca2b23d0ba | 18 | """ |
marcozecchini | 0:9fca2b23d0ba | 19 | |
marcozecchini | 0:9fca2b23d0ba | 20 | import re |
marcozecchini | 0:9fca2b23d0ba | 21 | import json |
marcozecchini | 0:9fca2b23d0ba | 22 | |
marcozecchini | 0:9fca2b23d0ba | 23 | |
marcozecchini | 0:9fca2b23d0ba | 24 | class BaseDBAccess(): |
marcozecchini | 0:9fca2b23d0ba | 25 | """ Class used to connect with test database and store test results |
marcozecchini | 0:9fca2b23d0ba | 26 | """ |
marcozecchini | 0:9fca2b23d0ba | 27 | def __init__(self): |
marcozecchini | 0:9fca2b23d0ba | 28 | self.db_object = None |
marcozecchini | 0:9fca2b23d0ba | 29 | self.db_type = None |
marcozecchini | 0:9fca2b23d0ba | 30 | # Connection credentials |
marcozecchini | 0:9fca2b23d0ba | 31 | self.host = None |
marcozecchini | 0:9fca2b23d0ba | 32 | self.user = None |
marcozecchini | 0:9fca2b23d0ba | 33 | self.passwd = None |
marcozecchini | 0:9fca2b23d0ba | 34 | self.db = None |
marcozecchini | 0:9fca2b23d0ba | 35 | |
marcozecchini | 0:9fca2b23d0ba | 36 | # Test Suite DB scheme (table names) |
marcozecchini | 0:9fca2b23d0ba | 37 | self.TABLE_BUILD_ID = 'mtest_build_id' |
marcozecchini | 0:9fca2b23d0ba | 38 | self.TABLE_BUILD_ID_STATUS = 'mtest_build_id_status' |
marcozecchini | 0:9fca2b23d0ba | 39 | self.TABLE_BUILD_ID_TYPE = 'mtest_build_id_type' |
marcozecchini | 0:9fca2b23d0ba | 40 | self.TABLE_TARGET = 'mtest_target' |
marcozecchini | 0:9fca2b23d0ba | 41 | self.TABLE_TEST_ENTRY = 'mtest_test_entry' |
marcozecchini | 0:9fca2b23d0ba | 42 | self.TABLE_TEST_ID = 'mtest_test_id' |
marcozecchini | 0:9fca2b23d0ba | 43 | self.TABLE_TEST_RESULT = 'mtest_test_result' |
marcozecchini | 0:9fca2b23d0ba | 44 | self.TABLE_TEST_TYPE = 'mtest_test_type' |
marcozecchini | 0:9fca2b23d0ba | 45 | self.TABLE_TOOLCHAIN = 'mtest_toolchain' |
marcozecchini | 0:9fca2b23d0ba | 46 | # Build ID status PKs |
marcozecchini | 0:9fca2b23d0ba | 47 | self.BUILD_ID_STATUS_STARTED = 1 # Started |
marcozecchini | 0:9fca2b23d0ba | 48 | self.BUILD_ID_STATUS_IN_PROGRESS = 2 # In Progress |
marcozecchini | 0:9fca2b23d0ba | 49 | self.BUILD_ID_STATUS_COMPLETED = 3 #Completed |
marcozecchini | 0:9fca2b23d0ba | 50 | self.BUILD_ID_STATUS_FAILED = 4 # Failed |
marcozecchini | 0:9fca2b23d0ba | 51 | # Build ID type PKs |
marcozecchini | 0:9fca2b23d0ba | 52 | self.BUILD_ID_TYPE_TEST = 1 # Test |
marcozecchini | 0:9fca2b23d0ba | 53 | self.BUILD_ID_TYPE_BUILD_ONLY = 2 # Build Only |
marcozecchini | 0:9fca2b23d0ba | 54 | |
marcozecchini | 0:9fca2b23d0ba | 55 | def get_hostname(self): |
marcozecchini | 0:9fca2b23d0ba | 56 | """ Useful when creating build_id in database |
marcozecchini | 0:9fca2b23d0ba | 57 | Function returns (hostname, uname) which can be used as (build_id_name, build_id_desc) |
marcozecchini | 0:9fca2b23d0ba | 58 | """ |
marcozecchini | 0:9fca2b23d0ba | 59 | # Get hostname from socket |
marcozecchini | 0:9fca2b23d0ba | 60 | import socket |
marcozecchini | 0:9fca2b23d0ba | 61 | hostname = socket.gethostbyaddr(socket.gethostname())[0] |
marcozecchini | 0:9fca2b23d0ba | 62 | # Get uname from platform resources |
marcozecchini | 0:9fca2b23d0ba | 63 | import platform |
marcozecchini | 0:9fca2b23d0ba | 64 | uname = json.dumps(platform.uname()) |
marcozecchini | 0:9fca2b23d0ba | 65 | return (hostname, uname) |
marcozecchini | 0:9fca2b23d0ba | 66 | |
marcozecchini | 0:9fca2b23d0ba | 67 | def get_db_type(self): |
marcozecchini | 0:9fca2b23d0ba | 68 | """ Returns database type. E.g. 'mysql', 'sqlLite' etc. |
marcozecchini | 0:9fca2b23d0ba | 69 | """ |
marcozecchini | 0:9fca2b23d0ba | 70 | return self.db_type |
marcozecchini | 0:9fca2b23d0ba | 71 | |
marcozecchini | 0:9fca2b23d0ba | 72 | def detect_database(self, verbose=False): |
marcozecchini | 0:9fca2b23d0ba | 73 | """ detect database and return VERION data structure or string (verbose=True) |
marcozecchini | 0:9fca2b23d0ba | 74 | """ |
marcozecchini | 0:9fca2b23d0ba | 75 | return None |
marcozecchini | 0:9fca2b23d0ba | 76 | |
marcozecchini | 0:9fca2b23d0ba | 77 | def parse_db_connection_string(self, str): |
marcozecchini | 0:9fca2b23d0ba | 78 | """ Parsing SQL DB connection string. String should contain: |
marcozecchini | 0:9fca2b23d0ba | 79 | - DB Name, user name, password, URL (DB host), name |
marcozecchini | 0:9fca2b23d0ba | 80 | Function should return tuple with parsed (db_type, username, password, host, db_name) or None if error |
marcozecchini | 0:9fca2b23d0ba | 81 | |
marcozecchini | 0:9fca2b23d0ba | 82 | (db_type, username, password, host, db_name) = self.parse_db_connection_string(db_url) |
marcozecchini | 0:9fca2b23d0ba | 83 | |
marcozecchini | 0:9fca2b23d0ba | 84 | E.g. connection string: 'mysql://username:password@127.0.0.1/db_name' |
marcozecchini | 0:9fca2b23d0ba | 85 | """ |
marcozecchini | 0:9fca2b23d0ba | 86 | result = None |
marcozecchini | 0:9fca2b23d0ba | 87 | if type(str) == type(''): |
marcozecchini | 0:9fca2b23d0ba | 88 | PATTERN = '^([\w]+)://([\w]+):([\w]*)@(.*)/([\w]+)' |
marcozecchini | 0:9fca2b23d0ba | 89 | result = re.match(PATTERN, str) |
marcozecchini | 0:9fca2b23d0ba | 90 | if result is not None: |
marcozecchini | 0:9fca2b23d0ba | 91 | result = result.groups() # Tuple (db_name, host, user, passwd, db) |
marcozecchini | 0:9fca2b23d0ba | 92 | return result # (db_type, username, password, host, db_name) |
marcozecchini | 0:9fca2b23d0ba | 93 | |
marcozecchini | 0:9fca2b23d0ba | 94 | def is_connected(self): |
marcozecchini | 0:9fca2b23d0ba | 95 | """ Returns True if we are connected to database |
marcozecchini | 0:9fca2b23d0ba | 96 | """ |
marcozecchini | 0:9fca2b23d0ba | 97 | pass |
marcozecchini | 0:9fca2b23d0ba | 98 | |
marcozecchini | 0:9fca2b23d0ba | 99 | def connect(self, host, user, passwd, db): |
marcozecchini | 0:9fca2b23d0ba | 100 | """ Connects to DB and returns DB object |
marcozecchini | 0:9fca2b23d0ba | 101 | """ |
marcozecchini | 0:9fca2b23d0ba | 102 | pass |
marcozecchini | 0:9fca2b23d0ba | 103 | |
marcozecchini | 0:9fca2b23d0ba | 104 | def connect_url(self, db_url): |
marcozecchini | 0:9fca2b23d0ba | 105 | """ Connects to database using db_url (database url parsing), |
marcozecchini | 0:9fca2b23d0ba | 106 | store host, username, password, db_name |
marcozecchini | 0:9fca2b23d0ba | 107 | """ |
marcozecchini | 0:9fca2b23d0ba | 108 | pass |
marcozecchini | 0:9fca2b23d0ba | 109 | |
marcozecchini | 0:9fca2b23d0ba | 110 | def reconnect(self): |
marcozecchini | 0:9fca2b23d0ba | 111 | """ Reconnects to DB and returns DB object using stored host name, |
marcozecchini | 0:9fca2b23d0ba | 112 | database name and credentials (user name and password) |
marcozecchini | 0:9fca2b23d0ba | 113 | """ |
marcozecchini | 0:9fca2b23d0ba | 114 | pass |
marcozecchini | 0:9fca2b23d0ba | 115 | |
marcozecchini | 0:9fca2b23d0ba | 116 | def disconnect(self): |
marcozecchini | 0:9fca2b23d0ba | 117 | """ Close DB connection |
marcozecchini | 0:9fca2b23d0ba | 118 | """ |
marcozecchini | 0:9fca2b23d0ba | 119 | pass |
marcozecchini | 0:9fca2b23d0ba | 120 | |
marcozecchini | 0:9fca2b23d0ba | 121 | def escape_string(self, str): |
marcozecchini | 0:9fca2b23d0ba | 122 | """ Escapes string so it can be put in SQL query between quotes |
marcozecchini | 0:9fca2b23d0ba | 123 | """ |
marcozecchini | 0:9fca2b23d0ba | 124 | pass |
marcozecchini | 0:9fca2b23d0ba | 125 | |
marcozecchini | 0:9fca2b23d0ba | 126 | def select_all(self, query): |
marcozecchini | 0:9fca2b23d0ba | 127 | """ Execute SELECT query and get all results |
marcozecchini | 0:9fca2b23d0ba | 128 | """ |
marcozecchini | 0:9fca2b23d0ba | 129 | pass |
marcozecchini | 0:9fca2b23d0ba | 130 | |
marcozecchini | 0:9fca2b23d0ba | 131 | def insert(self, query, commit=True): |
marcozecchini | 0:9fca2b23d0ba | 132 | """ Execute INSERT query, define if you want to commit |
marcozecchini | 0:9fca2b23d0ba | 133 | """ |
marcozecchini | 0:9fca2b23d0ba | 134 | pass |
marcozecchini | 0:9fca2b23d0ba | 135 | |
marcozecchini | 0:9fca2b23d0ba | 136 | def get_next_build_id(self, name, desc='', location='', type=None, status=None): |
marcozecchini | 0:9fca2b23d0ba | 137 | """ Insert new build_id (DB unique build like ID number to send all test results) |
marcozecchini | 0:9fca2b23d0ba | 138 | """ |
marcozecchini | 0:9fca2b23d0ba | 139 | pass |
marcozecchini | 0:9fca2b23d0ba | 140 | |
marcozecchini | 0:9fca2b23d0ba | 141 | def get_table_entry_pk(self, table, column, value, update_db=True): |
marcozecchini | 0:9fca2b23d0ba | 142 | """ Checks for entries in tables with two columns (<TABLE_NAME>_pk, <column>) |
marcozecchini | 0:9fca2b23d0ba | 143 | If update_db is True updates table entry if value in specified column doesn't exist |
marcozecchini | 0:9fca2b23d0ba | 144 | """ |
marcozecchini | 0:9fca2b23d0ba | 145 | pass |
marcozecchini | 0:9fca2b23d0ba | 146 | |
marcozecchini | 0:9fca2b23d0ba | 147 | def update_table_entry(self, table, column, value): |
marcozecchini | 0:9fca2b23d0ba | 148 | """ Updates table entry if value in specified column doesn't exist |
marcozecchini | 0:9fca2b23d0ba | 149 | Locks table to perform atomic read + update |
marcozecchini | 0:9fca2b23d0ba | 150 | """ |
marcozecchini | 0:9fca2b23d0ba | 151 | pass |
marcozecchini | 0:9fca2b23d0ba | 152 | |
marcozecchini | 0:9fca2b23d0ba | 153 | def update_build_id_info(self, build_id, **kw): |
marcozecchini | 0:9fca2b23d0ba | 154 | """ Update additional data inside build_id table |
marcozecchini | 0:9fca2b23d0ba | 155 | Examples: |
marcozecchini | 0:9fca2b23d0ba | 156 | db.update_build_is(build_id, _status_fk=self.BUILD_ID_STATUS_COMPLETED, _shuffle_seed=0.0123456789): |
marcozecchini | 0:9fca2b23d0ba | 157 | """ |
marcozecchini | 0:9fca2b23d0ba | 158 | pass |
marcozecchini | 0:9fca2b23d0ba | 159 | |
marcozecchini | 0:9fca2b23d0ba | 160 | def insert_test_entry(self, build_id, target, toolchain, test_type, test_id, test_result, test_time, test_timeout, test_loop, test_extra=''): |
marcozecchini | 0:9fca2b23d0ba | 161 | """ Inserts test result entry to database. All checks regarding existing |
marcozecchini | 0:9fca2b23d0ba | 162 | toolchain names in DB are performed. |
marcozecchini | 0:9fca2b23d0ba | 163 | If some data is missing DB will be updated |
marcozecchini | 0:9fca2b23d0ba | 164 | """ |
marcozecchini | 0:9fca2b23d0ba | 165 | pass |