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 json
borlanic 0:02dd72d1d465 22
borlanic 0:02dd72d1d465 23
borlanic 0:02dd72d1d465 24 class BaseDBAccess():
borlanic 0:02dd72d1d465 25 """ Class used to connect with test database and store test results
borlanic 0:02dd72d1d465 26 """
borlanic 0:02dd72d1d465 27 def __init__(self):
borlanic 0:02dd72d1d465 28 self.db_object = None
borlanic 0:02dd72d1d465 29 self.db_type = None
borlanic 0:02dd72d1d465 30 # Connection credentials
borlanic 0:02dd72d1d465 31 self.host = None
borlanic 0:02dd72d1d465 32 self.user = None
borlanic 0:02dd72d1d465 33 self.passwd = None
borlanic 0:02dd72d1d465 34 self.db = None
borlanic 0:02dd72d1d465 35
borlanic 0:02dd72d1d465 36 # Test Suite DB scheme (table names)
borlanic 0:02dd72d1d465 37 self.TABLE_BUILD_ID = 'mtest_build_id'
borlanic 0:02dd72d1d465 38 self.TABLE_BUILD_ID_STATUS = 'mtest_build_id_status'
borlanic 0:02dd72d1d465 39 self.TABLE_BUILD_ID_TYPE = 'mtest_build_id_type'
borlanic 0:02dd72d1d465 40 self.TABLE_TARGET = 'mtest_target'
borlanic 0:02dd72d1d465 41 self.TABLE_TEST_ENTRY = 'mtest_test_entry'
borlanic 0:02dd72d1d465 42 self.TABLE_TEST_ID = 'mtest_test_id'
borlanic 0:02dd72d1d465 43 self.TABLE_TEST_RESULT = 'mtest_test_result'
borlanic 0:02dd72d1d465 44 self.TABLE_TEST_TYPE = 'mtest_test_type'
borlanic 0:02dd72d1d465 45 self.TABLE_TOOLCHAIN = 'mtest_toolchain'
borlanic 0:02dd72d1d465 46 # Build ID status PKs
borlanic 0:02dd72d1d465 47 self.BUILD_ID_STATUS_STARTED = 1 # Started
borlanic 0:02dd72d1d465 48 self.BUILD_ID_STATUS_IN_PROGRESS = 2 # In Progress
borlanic 0:02dd72d1d465 49 self.BUILD_ID_STATUS_COMPLETED = 3 #Completed
borlanic 0:02dd72d1d465 50 self.BUILD_ID_STATUS_FAILED = 4 # Failed
borlanic 0:02dd72d1d465 51 # Build ID type PKs
borlanic 0:02dd72d1d465 52 self.BUILD_ID_TYPE_TEST = 1 # Test
borlanic 0:02dd72d1d465 53 self.BUILD_ID_TYPE_BUILD_ONLY = 2 # Build Only
borlanic 0:02dd72d1d465 54
borlanic 0:02dd72d1d465 55 def get_hostname(self):
borlanic 0:02dd72d1d465 56 """ Useful when creating build_id in database
borlanic 0:02dd72d1d465 57 Function returns (hostname, uname) which can be used as (build_id_name, build_id_desc)
borlanic 0:02dd72d1d465 58 """
borlanic 0:02dd72d1d465 59 # Get hostname from socket
borlanic 0:02dd72d1d465 60 import socket
borlanic 0:02dd72d1d465 61 hostname = socket.gethostbyaddr(socket.gethostname())[0]
borlanic 0:02dd72d1d465 62 # Get uname from platform resources
borlanic 0:02dd72d1d465 63 import platform
borlanic 0:02dd72d1d465 64 uname = json.dumps(platform.uname())
borlanic 0:02dd72d1d465 65 return (hostname, uname)
borlanic 0:02dd72d1d465 66
borlanic 0:02dd72d1d465 67 def get_db_type(self):
borlanic 0:02dd72d1d465 68 """ Returns database type. E.g. 'mysql', 'sqlLite' etc.
borlanic 0:02dd72d1d465 69 """
borlanic 0:02dd72d1d465 70 return self.db_type
borlanic 0:02dd72d1d465 71
borlanic 0:02dd72d1d465 72 def detect_database(self, verbose=False):
borlanic 0:02dd72d1d465 73 """ detect database and return VERION data structure or string (verbose=True)
borlanic 0:02dd72d1d465 74 """
borlanic 0:02dd72d1d465 75 return None
borlanic 0:02dd72d1d465 76
borlanic 0:02dd72d1d465 77 def parse_db_connection_string(self, str):
borlanic 0:02dd72d1d465 78 """ Parsing SQL DB connection string. String should contain:
borlanic 0:02dd72d1d465 79 - DB Name, user name, password, URL (DB host), name
borlanic 0:02dd72d1d465 80 Function should return tuple with parsed (db_type, username, password, host, db_name) or None if error
borlanic 0:02dd72d1d465 81
borlanic 0:02dd72d1d465 82 (db_type, username, password, host, db_name) = self.parse_db_connection_string(db_url)
borlanic 0:02dd72d1d465 83
borlanic 0:02dd72d1d465 84 E.g. connection string: 'mysql://username:password@127.0.0.1/db_name'
borlanic 0:02dd72d1d465 85 """
borlanic 0:02dd72d1d465 86 result = None
borlanic 0:02dd72d1d465 87 if type(str) == type(''):
borlanic 0:02dd72d1d465 88 PATTERN = '^([\w]+)://([\w]+):([\w]*)@(.*)/([\w]+)'
borlanic 0:02dd72d1d465 89 result = re.match(PATTERN, str)
borlanic 0:02dd72d1d465 90 if result is not None:
borlanic 0:02dd72d1d465 91 result = result.groups() # Tuple (db_name, host, user, passwd, db)
borlanic 0:02dd72d1d465 92 return result # (db_type, username, password, host, db_name)
borlanic 0:02dd72d1d465 93
borlanic 0:02dd72d1d465 94 def is_connected(self):
borlanic 0:02dd72d1d465 95 """ Returns True if we are connected to database
borlanic 0:02dd72d1d465 96 """
borlanic 0:02dd72d1d465 97 pass
borlanic 0:02dd72d1d465 98
borlanic 0:02dd72d1d465 99 def connect(self, host, user, passwd, db):
borlanic 0:02dd72d1d465 100 """ Connects to DB and returns DB object
borlanic 0:02dd72d1d465 101 """
borlanic 0:02dd72d1d465 102 pass
borlanic 0:02dd72d1d465 103
borlanic 0:02dd72d1d465 104 def connect_url(self, db_url):
borlanic 0:02dd72d1d465 105 """ Connects to database using db_url (database url parsing),
borlanic 0:02dd72d1d465 106 store host, username, password, db_name
borlanic 0:02dd72d1d465 107 """
borlanic 0:02dd72d1d465 108 pass
borlanic 0:02dd72d1d465 109
borlanic 0:02dd72d1d465 110 def reconnect(self):
borlanic 0:02dd72d1d465 111 """ Reconnects to DB and returns DB object using stored host name,
borlanic 0:02dd72d1d465 112 database name and credentials (user name and password)
borlanic 0:02dd72d1d465 113 """
borlanic 0:02dd72d1d465 114 pass
borlanic 0:02dd72d1d465 115
borlanic 0:02dd72d1d465 116 def disconnect(self):
borlanic 0:02dd72d1d465 117 """ Close DB connection
borlanic 0:02dd72d1d465 118 """
borlanic 0:02dd72d1d465 119 pass
borlanic 0:02dd72d1d465 120
borlanic 0:02dd72d1d465 121 def escape_string(self, str):
borlanic 0:02dd72d1d465 122 """ Escapes string so it can be put in SQL query between quotes
borlanic 0:02dd72d1d465 123 """
borlanic 0:02dd72d1d465 124 pass
borlanic 0:02dd72d1d465 125
borlanic 0:02dd72d1d465 126 def select_all(self, query):
borlanic 0:02dd72d1d465 127 """ Execute SELECT query and get all results
borlanic 0:02dd72d1d465 128 """
borlanic 0:02dd72d1d465 129 pass
borlanic 0:02dd72d1d465 130
borlanic 0:02dd72d1d465 131 def insert(self, query, commit=True):
borlanic 0:02dd72d1d465 132 """ Execute INSERT query, define if you want to commit
borlanic 0:02dd72d1d465 133 """
borlanic 0:02dd72d1d465 134 pass
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 pass
borlanic 0:02dd72d1d465 140
borlanic 0:02dd72d1d465 141 def get_table_entry_pk(self, table, column, value, update_db=True):
borlanic 0:02dd72d1d465 142 """ Checks for entries in tables with two columns (<TABLE_NAME>_pk, <column>)
borlanic 0:02dd72d1d465 143 If update_db is True updates table entry if value in specified column doesn't exist
borlanic 0:02dd72d1d465 144 """
borlanic 0:02dd72d1d465 145 pass
borlanic 0:02dd72d1d465 146
borlanic 0:02dd72d1d465 147 def update_table_entry(self, table, column, value):
borlanic 0:02dd72d1d465 148 """ Updates table entry if value in specified column doesn't exist
borlanic 0:02dd72d1d465 149 Locks table to perform atomic read + update
borlanic 0:02dd72d1d465 150 """
borlanic 0:02dd72d1d465 151 pass
borlanic 0:02dd72d1d465 152
borlanic 0:02dd72d1d465 153 def update_build_id_info(self, build_id, **kw):
borlanic 0:02dd72d1d465 154 """ Update additional data inside build_id table
borlanic 0:02dd72d1d465 155 Examples:
borlanic 0:02dd72d1d465 156 db.update_build_is(build_id, _status_fk=self.BUILD_ID_STATUS_COMPLETED, _shuffle_seed=0.0123456789):
borlanic 0:02dd72d1d465 157 """
borlanic 0:02dd72d1d465 158 pass
borlanic 0:02dd72d1d465 159
borlanic 0:02dd72d1d465 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:02dd72d1d465 161 """ Inserts test result entry to database. All checks regarding existing
borlanic 0:02dd72d1d465 162 toolchain names in DB are performed.
borlanic 0:02dd72d1d465 163 If some data is missing DB will be updated
borlanic 0:02dd72d1d465 164 """
borlanic 0:02dd72d1d465 165 pass