Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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