mariadb/Sanity/basic-functions-check/checkfun.py
2020-12-08 11:48:11 +01:00

148 lines
3.9 KiB
Python
Executable file

#!/usr/bin/python3
# vim: dict=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# runtest.sh of /CoreOS/mariadb/Sanity/basic-functions-check
# Description: basic-functions-check
# Author: Martin Cermak <mcermak@redhat.com>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Copyright (c) 2012 Red Hat, Inc. All rights reserved.
#
# This copyrighted material is made available to anyone wishing
# to use, modify, copy, or redistribute it subject to the terms
# and conditions of the GNU General Public License version 2.
#
# This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# The script should work with both, python 2 and python 3
from __future__ import print_function
import sys
#import _mysql
import MySQLdb as _mysql
import random
# db connection params
db_user = sys.argv[1]
db_pass = sys.argv[2]
db_name = sys.argv[3]
# testing params
LIMIT = float(sys.argv[4])
COUNT = int(sys.argv[5])
PRECI = int(sys.argv[6])
# error exit codes
CF_WRONG_PARAM_COUNT = 3
CF_CANT_CONNECT = 4
CF_CANT_CREATE = 5
CF_CANT_INSERT = 6
CF_CANT_SELECT = 7
CF_MAX_DIFFER = 8
CF_MIN_DIFFER = 9
CF_AVG_DIFFER = 10
if len(sys.argv) != 7:
print("ERROR: wrong param count")
sys.exit(CF_WRONG_PARAM_COUNT)
try:
db_con = _mysql.connect("localhost", db_user, db_pass, db_name)
except _mysql.Error as e:
print("ERROR: can't connect to DB")
print(e)
sys.exit(CF_CANT_CONNECT)
try:
db_con.query("DROP TABLE IF EXISTS test;")
db_con.query("CREATE TABLE test (id int, val DOUBLE);")
except _mysql.Error as e:
print("ERROR: can't create table")
print(e)
sys.exit(CF_CANT_CREATE)
i = 1
my_max = 0
my_min = LIMIT
my_sum = 0
my_avg = 0
while i < COUNT:
randval = random.random() * LIMIT
try:
db_con.query("INSERT INTO test VALUES ('" + str(i) + "', '" + str(randval) + "');");
except _mysql.Error as e:
print("ERROR: can't insert")
print(e)
sys.exit(CF_CANT_INSERT)
if randval < my_min: my_min = randval
if randval > my_max: my_max = randval
my_sum = my_sum + randval
i = i + 1
my_avg = my_sum / (i - 1)
try:
db_con.query("SELECT MAX(val), MIN(val), AVG(val) FROM test;")
except _mysql.Error as e:
print("ERROR: can't select")
print(e)
sys.exit(CF_CANT_SELECT)
res = db_con.store_result()
row = res.fetch_row()
# http://docs.python.org/release/3.1.3/tutorial/floatingpoint.html
sql_max = str(round(float(row[0][0]), PRECI))
sql_min = str(round(float(row[0][1]), PRECI))
sql_avg = str(round(float(row[0][2]), PRECI))
my_max = str(round(my_max, PRECI))
my_min = str(round(my_min, PRECI))
my_avg = str(round(my_avg, PRECI))
print("my_max\t: ", end=' ')
print(my_max)
print("sql_max\t: ", end=' ')
print(sql_max)
print("my_min\t: ", end=' ')
print(my_min)
print("sql_min\t: ", end=' ')
print(sql_min)
print("my_avg\t: ", end=' ')
print(my_avg)
print("sql_avg\t: ", end=' ')
print(sql_avg)
if my_max != sql_max:
print("ERROR: max differ")
sys.exit(CF_MAX_DIFFER)
if my_min != sql_min:
print("ERROR: min differ")
sys.exit(CF_MIN_DIFFER)
if my_avg != sql_avg:
print("ERROR: avg differ")
sys.exit(CF_AVG_DIFFER)
print("INFO: all ok")
sys.exit(0)