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