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