Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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