Nicolas Borla
/
BBR_1Ebene
BBR 1 Ebene
mbed-os/tools/test_mysql.py@0:fbdae7e6d805, 2018-05-14 (annotated)
- Committer:
- borlanic
- Date:
- Mon May 14 11:29:06 2018 +0000
- Revision:
- 0:fbdae7e6d805
BBR
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
borlanic | 0:fbdae7e6d805 | 1 | """ |
borlanic | 0:fbdae7e6d805 | 2 | mbed SDK |
borlanic | 0:fbdae7e6d805 | 3 | Copyright (c) 2011-2014 ARM Limited |
borlanic | 0:fbdae7e6d805 | 4 | |
borlanic | 0:fbdae7e6d805 | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
borlanic | 0:fbdae7e6d805 | 6 | you may not use this file except in compliance with the License. |
borlanic | 0:fbdae7e6d805 | 7 | You may obtain a copy of the License at |
borlanic | 0:fbdae7e6d805 | 8 | |
borlanic | 0:fbdae7e6d805 | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
borlanic | 0:fbdae7e6d805 | 10 | |
borlanic | 0:fbdae7e6d805 | 11 | Unless required by applicable law or agreed to in writing, software |
borlanic | 0:fbdae7e6d805 | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
borlanic | 0:fbdae7e6d805 | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
borlanic | 0:fbdae7e6d805 | 14 | See the License for the specific language governing permissions and |
borlanic | 0:fbdae7e6d805 | 15 | limitations under the License. |
borlanic | 0:fbdae7e6d805 | 16 | |
borlanic | 0:fbdae7e6d805 | 17 | Author: Przemyslaw Wirkus <Przemyslaw.Wirkus@arm.com> |
borlanic | 0:fbdae7e6d805 | 18 | """ |
borlanic | 0:fbdae7e6d805 | 19 | |
borlanic | 0:fbdae7e6d805 | 20 | import re |
borlanic | 0:fbdae7e6d805 | 21 | import MySQLdb as mdb |
borlanic | 0:fbdae7e6d805 | 22 | |
borlanic | 0:fbdae7e6d805 | 23 | # Imports from TEST API |
borlanic | 0:fbdae7e6d805 | 24 | from tools.test_db import BaseDBAccess |
borlanic | 0:fbdae7e6d805 | 25 | |
borlanic | 0:fbdae7e6d805 | 26 | |
borlanic | 0:fbdae7e6d805 | 27 | class MySQLDBAccess(BaseDBAccess): |
borlanic | 0:fbdae7e6d805 | 28 | """ Wrapper for MySQL DB access for common test suite interface |
borlanic | 0:fbdae7e6d805 | 29 | """ |
borlanic | 0:fbdae7e6d805 | 30 | def __init__(self): |
borlanic | 0:fbdae7e6d805 | 31 | BaseDBAccess.__init__(self) |
borlanic | 0:fbdae7e6d805 | 32 | self.DB_TYPE = 'mysql' |
borlanic | 0:fbdae7e6d805 | 33 | |
borlanic | 0:fbdae7e6d805 | 34 | def detect_database(self, verbose=False): |
borlanic | 0:fbdae7e6d805 | 35 | """ detect database and return VERION data structure or string (verbose=True) |
borlanic | 0:fbdae7e6d805 | 36 | """ |
borlanic | 0:fbdae7e6d805 | 37 | query = 'SHOW VARIABLES LIKE "%version%"' |
borlanic | 0:fbdae7e6d805 | 38 | rows = self.select_all(query) |
borlanic | 0:fbdae7e6d805 | 39 | if verbose: |
borlanic | 0:fbdae7e6d805 | 40 | result = [] |
borlanic | 0:fbdae7e6d805 | 41 | for row in rows: |
borlanic | 0:fbdae7e6d805 | 42 | result.append("\t%s: %s"% (row['Variable_name'], row['Value'])) |
borlanic | 0:fbdae7e6d805 | 43 | result = "\n".join(result) |
borlanic | 0:fbdae7e6d805 | 44 | else: |
borlanic | 0:fbdae7e6d805 | 45 | result = rows |
borlanic | 0:fbdae7e6d805 | 46 | return result |
borlanic | 0:fbdae7e6d805 | 47 | |
borlanic | 0:fbdae7e6d805 | 48 | def parse_db_connection_string(self, str): |
borlanic | 0:fbdae7e6d805 | 49 | """ Parsing SQL DB connection string. String should contain: |
borlanic | 0:fbdae7e6d805 | 50 | - DB Name, user name, password, URL (DB host), name |
borlanic | 0:fbdae7e6d805 | 51 | Function should return tuple with parsed (host, user, passwd, db) or None if error |
borlanic | 0:fbdae7e6d805 | 52 | E.g. connection string: 'mysql://username:password@127.0.0.1/db_name' |
borlanic | 0:fbdae7e6d805 | 53 | """ |
borlanic | 0:fbdae7e6d805 | 54 | result = BaseDBAccess().parse_db_connection_string(str) |
borlanic | 0:fbdae7e6d805 | 55 | if result is not None: |
borlanic | 0:fbdae7e6d805 | 56 | (db_type, username, password, host, db_name) = result |
borlanic | 0:fbdae7e6d805 | 57 | if db_type != 'mysql': |
borlanic | 0:fbdae7e6d805 | 58 | result = None |
borlanic | 0:fbdae7e6d805 | 59 | return result |
borlanic | 0:fbdae7e6d805 | 60 | |
borlanic | 0:fbdae7e6d805 | 61 | def is_connected(self): |
borlanic | 0:fbdae7e6d805 | 62 | """ Returns True if we are connected to database |
borlanic | 0:fbdae7e6d805 | 63 | """ |
borlanic | 0:fbdae7e6d805 | 64 | return self.db_object is not None |
borlanic | 0:fbdae7e6d805 | 65 | |
borlanic | 0:fbdae7e6d805 | 66 | def connect(self, host, user, passwd, db): |
borlanic | 0:fbdae7e6d805 | 67 | """ Connects to DB and returns DB object |
borlanic | 0:fbdae7e6d805 | 68 | """ |
borlanic | 0:fbdae7e6d805 | 69 | try: |
borlanic | 0:fbdae7e6d805 | 70 | self.db_object = mdb.connect(host=host, user=user, passwd=passwd, db=db) |
borlanic | 0:fbdae7e6d805 | 71 | # Let's remember connection credentials |
borlanic | 0:fbdae7e6d805 | 72 | self.db_type = self.DB_TYPE |
borlanic | 0:fbdae7e6d805 | 73 | self.host = host |
borlanic | 0:fbdae7e6d805 | 74 | self.user = user |
borlanic | 0:fbdae7e6d805 | 75 | self.passwd = passwd |
borlanic | 0:fbdae7e6d805 | 76 | self.db = db |
borlanic | 0:fbdae7e6d805 | 77 | except mdb.Error, e: |
borlanic | 0:fbdae7e6d805 | 78 | print "Error %d: %s"% (e.args[0], e.args[1]) |
borlanic | 0:fbdae7e6d805 | 79 | self.db_object = None |
borlanic | 0:fbdae7e6d805 | 80 | self.db_type = None |
borlanic | 0:fbdae7e6d805 | 81 | self.host = None |
borlanic | 0:fbdae7e6d805 | 82 | self.user = None |
borlanic | 0:fbdae7e6d805 | 83 | self.passwd = None |
borlanic | 0:fbdae7e6d805 | 84 | self.db = None |
borlanic | 0:fbdae7e6d805 | 85 | |
borlanic | 0:fbdae7e6d805 | 86 | def connect_url(self, db_url): |
borlanic | 0:fbdae7e6d805 | 87 | """ Connects to database using db_url (database url parsing), |
borlanic | 0:fbdae7e6d805 | 88 | store host, username, password, db_name |
borlanic | 0:fbdae7e6d805 | 89 | """ |
borlanic | 0:fbdae7e6d805 | 90 | result = self.parse_db_connection_string(db_url) |
borlanic | 0:fbdae7e6d805 | 91 | if result is not None: |
borlanic | 0:fbdae7e6d805 | 92 | (db_type, username, password, host, db_name) = result |
borlanic | 0:fbdae7e6d805 | 93 | if db_type == self.DB_TYPE: |
borlanic | 0:fbdae7e6d805 | 94 | self.connect(host, username, password, db_name) |
borlanic | 0:fbdae7e6d805 | 95 | |
borlanic | 0:fbdae7e6d805 | 96 | def reconnect(self): |
borlanic | 0:fbdae7e6d805 | 97 | """ Reconnects to DB and returns DB object using stored host name, |
borlanic | 0:fbdae7e6d805 | 98 | database name and credentials (user name and password) |
borlanic | 0:fbdae7e6d805 | 99 | """ |
borlanic | 0:fbdae7e6d805 | 100 | self.connect(self.host, self.user, self.passwd, self.db) |
borlanic | 0:fbdae7e6d805 | 101 | |
borlanic | 0:fbdae7e6d805 | 102 | def disconnect(self): |
borlanic | 0:fbdae7e6d805 | 103 | """ Close DB connection |
borlanic | 0:fbdae7e6d805 | 104 | """ |
borlanic | 0:fbdae7e6d805 | 105 | if self.db_object: |
borlanic | 0:fbdae7e6d805 | 106 | self.db_object.close() |
borlanic | 0:fbdae7e6d805 | 107 | self.db_object = None |
borlanic | 0:fbdae7e6d805 | 108 | self.db_type = None |
borlanic | 0:fbdae7e6d805 | 109 | |
borlanic | 0:fbdae7e6d805 | 110 | def escape_string(self, str): |
borlanic | 0:fbdae7e6d805 | 111 | """ Escapes string so it can be put in SQL query between quotes |
borlanic | 0:fbdae7e6d805 | 112 | """ |
borlanic | 0:fbdae7e6d805 | 113 | con = self.db_object |
borlanic | 0:fbdae7e6d805 | 114 | result = con.escape_string(str) |
borlanic | 0:fbdae7e6d805 | 115 | return result if result else '' |
borlanic | 0:fbdae7e6d805 | 116 | |
borlanic | 0:fbdae7e6d805 | 117 | def select_all(self, query): |
borlanic | 0:fbdae7e6d805 | 118 | """ Execute SELECT query and get all results |
borlanic | 0:fbdae7e6d805 | 119 | """ |
borlanic | 0:fbdae7e6d805 | 120 | con = self.db_object |
borlanic | 0:fbdae7e6d805 | 121 | cur = con.cursor(mdb.cursors.DictCursor) |
borlanic | 0:fbdae7e6d805 | 122 | cur.execute(query) |
borlanic | 0:fbdae7e6d805 | 123 | rows = cur.fetchall() |
borlanic | 0:fbdae7e6d805 | 124 | return rows |
borlanic | 0:fbdae7e6d805 | 125 | |
borlanic | 0:fbdae7e6d805 | 126 | def insert(self, query, commit=True): |
borlanic | 0:fbdae7e6d805 | 127 | """ Execute INSERT query, define if you want to commit |
borlanic | 0:fbdae7e6d805 | 128 | """ |
borlanic | 0:fbdae7e6d805 | 129 | con = self.db_object |
borlanic | 0:fbdae7e6d805 | 130 | cur = con.cursor() |
borlanic | 0:fbdae7e6d805 | 131 | cur.execute(query) |
borlanic | 0:fbdae7e6d805 | 132 | if commit: |
borlanic | 0:fbdae7e6d805 | 133 | con.commit() |
borlanic | 0:fbdae7e6d805 | 134 | return cur.lastrowid |
borlanic | 0:fbdae7e6d805 | 135 | |
borlanic | 0:fbdae7e6d805 | 136 | def get_next_build_id(self, name, desc='', location='', type=None, status=None): |
borlanic | 0:fbdae7e6d805 | 137 | """ Insert new build_id (DB unique build like ID number to send all test results) |
borlanic | 0:fbdae7e6d805 | 138 | """ |
borlanic | 0:fbdae7e6d805 | 139 | if status is None: |
borlanic | 0:fbdae7e6d805 | 140 | status = self.BUILD_ID_STATUS_STARTED |
borlanic | 0:fbdae7e6d805 | 141 | |
borlanic | 0:fbdae7e6d805 | 142 | if type is None: |
borlanic | 0:fbdae7e6d805 | 143 | type = self.BUILD_ID_TYPE_TEST |
borlanic | 0:fbdae7e6d805 | 144 | |
borlanic | 0:fbdae7e6d805 | 145 | query = """INSERT INTO `%s` (%s_name, %s_desc, %s_location, %s_type_fk, %s_status_fk) |
borlanic | 0:fbdae7e6d805 | 146 | VALUES ('%s', '%s', '%s', %d, %d)"""% (self.TABLE_BUILD_ID, |
borlanic | 0:fbdae7e6d805 | 147 | self.TABLE_BUILD_ID, |
borlanic | 0:fbdae7e6d805 | 148 | self.TABLE_BUILD_ID, |
borlanic | 0:fbdae7e6d805 | 149 | self.TABLE_BUILD_ID, |
borlanic | 0:fbdae7e6d805 | 150 | self.TABLE_BUILD_ID, |
borlanic | 0:fbdae7e6d805 | 151 | self.TABLE_BUILD_ID, |
borlanic | 0:fbdae7e6d805 | 152 | self.escape_string(name), |
borlanic | 0:fbdae7e6d805 | 153 | self.escape_string(desc), |
borlanic | 0:fbdae7e6d805 | 154 | self.escape_string(location), |
borlanic | 0:fbdae7e6d805 | 155 | type, |
borlanic | 0:fbdae7e6d805 | 156 | status) |
borlanic | 0:fbdae7e6d805 | 157 | index = self.insert(query) # Provide inserted record PK |
borlanic | 0:fbdae7e6d805 | 158 | return index |
borlanic | 0:fbdae7e6d805 | 159 | |
borlanic | 0:fbdae7e6d805 | 160 | def get_table_entry_pk(self, table, column, value, update_db=True): |
borlanic | 0:fbdae7e6d805 | 161 | """ Checks for entries in tables with two columns (<TABLE_NAME>_pk, <column>) |
borlanic | 0:fbdae7e6d805 | 162 | If update_db is True updates table entry if value in specified column doesn't exist |
borlanic | 0:fbdae7e6d805 | 163 | """ |
borlanic | 0:fbdae7e6d805 | 164 | # TODO: table buffering |
borlanic | 0:fbdae7e6d805 | 165 | result = None |
borlanic | 0:fbdae7e6d805 | 166 | table_pk = '%s_pk'% table |
borlanic | 0:fbdae7e6d805 | 167 | query = """SELECT `%s` |
borlanic | 0:fbdae7e6d805 | 168 | FROM `%s` |
borlanic | 0:fbdae7e6d805 | 169 | WHERE `%s`='%s'"""% (table_pk, |
borlanic | 0:fbdae7e6d805 | 170 | table, |
borlanic | 0:fbdae7e6d805 | 171 | column, |
borlanic | 0:fbdae7e6d805 | 172 | self.escape_string(value)) |
borlanic | 0:fbdae7e6d805 | 173 | rows = self.select_all(query) |
borlanic | 0:fbdae7e6d805 | 174 | if len(rows) == 1: |
borlanic | 0:fbdae7e6d805 | 175 | result = rows[0][table_pk] |
borlanic | 0:fbdae7e6d805 | 176 | elif len(rows) == 0 and update_db: |
borlanic | 0:fbdae7e6d805 | 177 | # Update DB with new value |
borlanic | 0:fbdae7e6d805 | 178 | result = self.update_table_entry(table, column, value) |
borlanic | 0:fbdae7e6d805 | 179 | return result |
borlanic | 0:fbdae7e6d805 | 180 | |
borlanic | 0:fbdae7e6d805 | 181 | def update_table_entry(self, table, column, value): |
borlanic | 0:fbdae7e6d805 | 182 | """ Updates table entry if value in specified column doesn't exist |
borlanic | 0:fbdae7e6d805 | 183 | Locks table to perform atomic read + update |
borlanic | 0:fbdae7e6d805 | 184 | """ |
borlanic | 0:fbdae7e6d805 | 185 | result = None |
borlanic | 0:fbdae7e6d805 | 186 | con = self.db_object |
borlanic | 0:fbdae7e6d805 | 187 | cur = con.cursor() |
borlanic | 0:fbdae7e6d805 | 188 | cur.execute("LOCK TABLES `%s` WRITE"% table) |
borlanic | 0:fbdae7e6d805 | 189 | table_pk = '%s_pk'% table |
borlanic | 0:fbdae7e6d805 | 190 | query = """SELECT `%s` |
borlanic | 0:fbdae7e6d805 | 191 | FROM `%s` |
borlanic | 0:fbdae7e6d805 | 192 | WHERE `%s`='%s'"""% (table_pk, |
borlanic | 0:fbdae7e6d805 | 193 | table, |
borlanic | 0:fbdae7e6d805 | 194 | column, |
borlanic | 0:fbdae7e6d805 | 195 | self.escape_string(value)) |
borlanic | 0:fbdae7e6d805 | 196 | cur.execute(query) |
borlanic | 0:fbdae7e6d805 | 197 | rows = cur.fetchall() |
borlanic | 0:fbdae7e6d805 | 198 | if len(rows) == 0: |
borlanic | 0:fbdae7e6d805 | 199 | query = """INSERT INTO `%s` (%s) |
borlanic | 0:fbdae7e6d805 | 200 | VALUES ('%s')"""% (table, |
borlanic | 0:fbdae7e6d805 | 201 | column, |
borlanic | 0:fbdae7e6d805 | 202 | self.escape_string(value)) |
borlanic | 0:fbdae7e6d805 | 203 | cur.execute(query) |
borlanic | 0:fbdae7e6d805 | 204 | result = cur.lastrowid |
borlanic | 0:fbdae7e6d805 | 205 | con.commit() |
borlanic | 0:fbdae7e6d805 | 206 | cur.execute("UNLOCK TABLES") |
borlanic | 0:fbdae7e6d805 | 207 | return result |
borlanic | 0:fbdae7e6d805 | 208 | |
borlanic | 0:fbdae7e6d805 | 209 | def update_build_id_info(self, build_id, **kw): |
borlanic | 0:fbdae7e6d805 | 210 | """ Update additional data inside build_id table |
borlanic | 0:fbdae7e6d805 | 211 | Examples: |
borlanic | 0:fbdae7e6d805 | 212 | db.update_build_id_info(build_id, _status_fk=self.BUILD_ID_STATUS_COMPLETED, _shuffle_seed=0.0123456789): |
borlanic | 0:fbdae7e6d805 | 213 | """ |
borlanic | 0:fbdae7e6d805 | 214 | if len(kw): |
borlanic | 0:fbdae7e6d805 | 215 | con = self.db_object |
borlanic | 0:fbdae7e6d805 | 216 | cur = con.cursor() |
borlanic | 0:fbdae7e6d805 | 217 | # Prepare UPDATE query |
borlanic | 0:fbdae7e6d805 | 218 | # ["`mtest_build_id_pk`=[value-1]", "`mtest_build_id_name`=[value-2]", "`mtest_build_id_desc`=[value-3]"] |
borlanic | 0:fbdae7e6d805 | 219 | set_list = [] |
borlanic | 0:fbdae7e6d805 | 220 | for col_sufix in kw: |
borlanic | 0:fbdae7e6d805 | 221 | assign_str = "`%s%s`='%s'"% (self.TABLE_BUILD_ID, col_sufix, self.escape_string(str(kw[col_sufix]))) |
borlanic | 0:fbdae7e6d805 | 222 | set_list.append(assign_str) |
borlanic | 0:fbdae7e6d805 | 223 | set_str = ', '.join(set_list) |
borlanic | 0:fbdae7e6d805 | 224 | query = """UPDATE `%s` |
borlanic | 0:fbdae7e6d805 | 225 | SET %s |
borlanic | 0:fbdae7e6d805 | 226 | WHERE `mtest_build_id_pk`=%d"""% (self.TABLE_BUILD_ID, |
borlanic | 0:fbdae7e6d805 | 227 | set_str, |
borlanic | 0:fbdae7e6d805 | 228 | build_id) |
borlanic | 0:fbdae7e6d805 | 229 | cur.execute(query) |
borlanic | 0:fbdae7e6d805 | 230 | con.commit() |
borlanic | 0:fbdae7e6d805 | 231 | |
borlanic | 0:fbdae7e6d805 | 232 | def insert_test_entry(self, build_id, target, toolchain, test_type, test_id, test_result, test_output, test_time, test_timeout, test_loop, test_extra=''): |
borlanic | 0:fbdae7e6d805 | 233 | """ Inserts test result entry to database. All checks regarding existing |
borlanic | 0:fbdae7e6d805 | 234 | toolchain names in DB are performed. |
borlanic | 0:fbdae7e6d805 | 235 | If some data is missing DB will be updated |
borlanic | 0:fbdae7e6d805 | 236 | """ |
borlanic | 0:fbdae7e6d805 | 237 | # Get all table FK and if entry is new try to insert new value |
borlanic | 0:fbdae7e6d805 | 238 | target_fk = self.get_table_entry_pk(self.TABLE_TARGET, self.TABLE_TARGET + '_name', target) |
borlanic | 0:fbdae7e6d805 | 239 | toolchain_fk = self.get_table_entry_pk(self.TABLE_TOOLCHAIN, self.TABLE_TOOLCHAIN + '_name', toolchain) |
borlanic | 0:fbdae7e6d805 | 240 | test_type_fk = self.get_table_entry_pk(self.TABLE_TEST_TYPE, self.TABLE_TEST_TYPE + '_name', test_type) |
borlanic | 0:fbdae7e6d805 | 241 | test_id_fk = self.get_table_entry_pk(self.TABLE_TEST_ID, self.TABLE_TEST_ID + '_name', test_id) |
borlanic | 0:fbdae7e6d805 | 242 | test_result_fk = self.get_table_entry_pk(self.TABLE_TEST_RESULT, self.TABLE_TEST_RESULT + '_name', test_result) |
borlanic | 0:fbdae7e6d805 | 243 | |
borlanic | 0:fbdae7e6d805 | 244 | con = self.db_object |
borlanic | 0:fbdae7e6d805 | 245 | cur = con.cursor() |
borlanic | 0:fbdae7e6d805 | 246 | |
borlanic | 0:fbdae7e6d805 | 247 | query = """ INSERT INTO `%s` (`mtest_build_id_fk`, |
borlanic | 0:fbdae7e6d805 | 248 | `mtest_target_fk`, |
borlanic | 0:fbdae7e6d805 | 249 | `mtest_toolchain_fk`, |
borlanic | 0:fbdae7e6d805 | 250 | `mtest_test_type_fk`, |
borlanic | 0:fbdae7e6d805 | 251 | `mtest_test_id_fk`, |
borlanic | 0:fbdae7e6d805 | 252 | `mtest_test_result_fk`, |
borlanic | 0:fbdae7e6d805 | 253 | `mtest_test_output`, |
borlanic | 0:fbdae7e6d805 | 254 | `mtest_test_time`, |
borlanic | 0:fbdae7e6d805 | 255 | `mtest_test_timeout`, |
borlanic | 0:fbdae7e6d805 | 256 | `mtest_test_loop_no`, |
borlanic | 0:fbdae7e6d805 | 257 | `mtest_test_result_extra`) |
borlanic | 0:fbdae7e6d805 | 258 | VALUES (%d, %d, %d, %d, %d, %d, '%s', %.2f, %.2f, %d, '%s')"""% (self.TABLE_TEST_ENTRY, |
borlanic | 0:fbdae7e6d805 | 259 | build_id, |
borlanic | 0:fbdae7e6d805 | 260 | target_fk, |
borlanic | 0:fbdae7e6d805 | 261 | toolchain_fk, |
borlanic | 0:fbdae7e6d805 | 262 | test_type_fk, |
borlanic | 0:fbdae7e6d805 | 263 | test_id_fk, |
borlanic | 0:fbdae7e6d805 | 264 | test_result_fk, |
borlanic | 0:fbdae7e6d805 | 265 | self.escape_string(test_output), |
borlanic | 0:fbdae7e6d805 | 266 | test_time, |
borlanic | 0:fbdae7e6d805 | 267 | test_timeout, |
borlanic | 0:fbdae7e6d805 | 268 | test_loop, |
borlanic | 0:fbdae7e6d805 | 269 | self.escape_string(test_extra)) |
borlanic | 0:fbdae7e6d805 | 270 | cur.execute(query) |
borlanic | 0:fbdae7e6d805 | 271 | con.commit() |