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