Skip to content

Commit

Permalink
chore: fix logictest statistics output
Browse files Browse the repository at this point in the history
  • Loading branch information
everpcpc committed Nov 18, 2022
1 parent 0011d5d commit ffe76bf
Show file tree
Hide file tree
Showing 28 changed files with 459 additions and 429 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ lint:
# Cargo.toml file formatter(make setup to install)
taplo fmt
# Python file formatter(make setup to install)
yapf -ri tests/
black tests/
# Bash file formatter(make setup to install)
shfmt -l -w scripts/*

Expand Down
20 changes: 13 additions & 7 deletions scripts/ci/wait_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import socket
import argparse
import time
import sys


def tcp_ping(port, timeout):

Expand All @@ -12,20 +14,24 @@ def tcp_ping(port, timeout):
while time.time() - now < timeout:
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect(('0.0.0.0', port))
sock.connect(("0.0.0.0", port))
print("OK :{} is listening".format(port))
sys.stdout.flush()
return
except:
print("not connected to :{}".format(port))
time.sleep(0.5)
except Exception:
print("... connecting to :{}".format(port))
sys.stdout.flush()
time.sleep(1)

raise Exception("fail to connect to :{}".format(port))


if __name__ == "__main__":
parser = argparse.ArgumentParser(description='block until successfully connecting to a local tcp port')
parser.add_argument('-p', '--port', type=int, help='local tcp port')
parser.add_argument('-t', '--timeout', type=int, default=5, help='time to wait.')
parser = argparse.ArgumentParser(
description="block until successfully connecting to a local tcp port"
)
parser.add_argument("-p", "--port", type=int, help="local tcp port")
parser.add_argument("-t", "--timeout", type=int, default=10, help="time to wait.")

args = parser.parse_args()

Expand Down
4 changes: 2 additions & 2 deletions scripts/setup/dev_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ EOF
cat <<EOF
Development tools (since -d was provided):
* mysql client
* python3 (boto3, yapf, yamllint, ...)
* python3 (boto3, black, yamllint, ...)
* python database drivers (mysql-connector-python, pymysql, sqlalchemy, clickhouse_driver)
* sqllogic test dependencies (PyHamcrest, environs, fire, ...)
* fuzz test dependencies (fuzzingbook)
Expand Down Expand Up @@ -549,7 +549,7 @@ if [[ "$INSTALL_DEV_TOOLS" == "true" ]]; then
install_pkg py3-pip "$PACKAGE_MANAGER"
install_pkg libffi-dev "$PACKAGE_MANAGER"
fi
python3 -m pip install --quiet boto3 "moto[all]" yapf shfmt-py toml yamllint
python3 -m pip install --quiet boto3 "moto[all]" black shfmt-py toml yamllint
# drivers
python3 -m pip install --quiet pymysql sqlalchemy clickhouse_driver
# sqllogic dependencies
Expand Down
60 changes: 27 additions & 33 deletions tests/fuzz/fuzz.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,26 @@
from fuzzingbook.Grammars import Grammar, is_valid_grammar


class mysql_client():

class mysql_client:
def __init__(self):
config = {
'user': 'root',
'host': '127.0.0.1',
'port': 3307,
'database': 'default',
"user": "root",
"host": "127.0.0.1",
"port": 3307,
"database": "default",
}
host = os.getenv("QUERY_MYSQL_HANDLER_HOST")
if host is not None:
config['host'] = host
config["host"] = host
port = os.getenv("QUERY_MYSQL_HANDLER_PORT")
if port is not None:
config['port'] = port
config["port"] = port
user = os.getenv("MYSQL_USER")
if user is not None:
config['user'] = user
config["user"] = user
default_database = os.getenv("MYSQL_DATABASE")
if default_database is not None:
config['database'] = default_database
config["database"] = default_database
self._connection = mysql.connector.connect(**config)

def close(self):
Expand All @@ -45,7 +44,7 @@ def run(self, sql):


START_SYMBOL = "<start>"
RE_NONTERMINAL = re.compile(r'(<[^<> ]*>)')
RE_NONTERMINAL = re.compile(r"(<[^<> ]*>)")


def nonterminals(expansion):
Expand Down Expand Up @@ -81,25 +80,22 @@ def nonterminals(expansion):
"SELECT <select_list> FROM <table_reference_list> order by <expression> ASC <limit_list>",
"SELECT <select_list> FROM <table_reference_list> order by <expression> DESC <limit_list>",
],
"<select_list>": [
"*", "<select_target>", "<select_target>,<select_target>"
],
"<select_list>": ["*", "<select_target>", "<select_target>,<select_target>"],
"<select_target>": ["<target_rows>"],
"<condition_expression>": ["<target_rows> <expr> <value>"],
"<table_reference_list>": [
"<table_reference>", "<table_reference>, <table_reference>"
"<table_reference>",
"<table_reference>, <table_reference>",
],
"<function_reference>": ["sum", "avg", "count", "min", "max"],
"<group_by_list>": ["<expression>", "<following_expression>"],
"<limit_list>": ["limit 1", "limit 10", "limit 100"],
"<following_expression>": ["<expression>", "<expression>,<expression>"],
"<expression>": ["<target_rows>", "<target_rows>,<target_rows>"],
"<table_reference>": ["t1", "t2", "t3"],
"<target_rows>": [
"row1", "row2", "row3", "row4", "row5", "row6", "row7", "row8"
],
"<target_rows>": ["row1", "row2", "row3", "row4", "row5", "row6", "row7", "row8"],
"<expr>": [">", "<", ">=", "<=", "!=", "="],
"<value>": ["1", "0", "null"]
"<value>": ["1", "0", "null"],
}

drop_grammar: Grammar = {
Expand All @@ -118,11 +114,13 @@ class ExpansionError(Exception):


# https://www.fuzzingbook.org/html/Grammars.html
def grammar_fuzzer(grammar,
start_symbol: str = START_SYMBOL,
max_nonterminals: int = 10,
max_expansion_trials: int = 100,
log: bool = False) -> str:
def grammar_fuzzer(
grammar,
start_symbol: str = START_SYMBOL,
max_nonterminals: int = 10,
max_expansion_trials: int = 100,
log: bool = False,
) -> str:

term = start_symbol
expansion_trials = 0
Expand All @@ -148,21 +146,18 @@ def grammar_fuzzer(grammar,
return term


class QueryGenerator():

class QueryGenerator:
def __init__(self, grammar, execute_times=100):
random.seed(time.time())
self._grammar = grammar
self.execute_times = execute_times
self._max_nonterminals = random.randint(5, 10)

def next(self):
return grammar_fuzzer(self._grammar,
max_nonterminals=self._max_nonterminals)

return grammar_fuzzer(self._grammar, max_nonterminals=self._max_nonterminals)

class QueryExecutor():

class QueryExecutor:
def __init__(self):
self._client = mysql_client()
self.prepare()
Expand All @@ -180,8 +175,7 @@ def close(self):
self._client = None


class FuzzRunner():

class FuzzRunner:
def __init__(self, generators, executor):
self._generators = generators
self._executor = executor
Expand All @@ -208,6 +202,6 @@ def query_validate(result):
QueryGenerator(drop_grammar, 10),
]

if __name__ == '__main__':
if __name__ == "__main__":
f = FuzzRunner(generator_list, QueryExecutor())
f.run()
19 changes: 8 additions & 11 deletions tests/helpers/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@


class client(object):

def __init__(self, name='', log=None):
def __init__(self, name="", log=None):
self.name = name
self.log = log
self.client = f'mysql --user default -s'
self.client = f"mysql --user default -s"
tcp_host = os.getenv("QUERY_MYSQL_HANDLER_HOST")
if tcp_host is not None:
self.client += f' --host={tcp_host}'
self.client += f" --host={tcp_host}"
else:
self.client += f' --host=127.0.0.1'
self.client += f" --host=127.0.0.1"

tcp_port = os.getenv("QUERY_MYSQL_HANDLER_PORT")
if tcp_port is not None:
Expand All @@ -42,14 +41,12 @@ def run(self, sqls):
p.communicate(input=sqls)[0]

def run_with_output(self, sqls):
p = Popen(self.client,
shell=True,
stdin=PIPE,
stdout=PIPE,
universal_newlines=True)
p = Popen(
self.client, shell=True, stdin=PIPE, stdout=PIPE, universal_newlines=True
)
return p.communicate(input=sqls)


if __name__ == '__main__':
if __name__ == "__main__":
client = client("e")
client.run("create table test2(a int);")
39 changes: 19 additions & 20 deletions tests/helpers/native_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@

import uexpect

prompt = ':\) '
end_of_block = r'.*\r\n.*\r\n'
prompt = ":\) "
end_of_block = r".*\r\n.*\r\n"


class NativeClient(object):

def __init__(self, command=None, name='', log=None):
self.client = uexpect.spawn(['/bin/bash', '--noediting'])
def __init__(self, command=None, name="", log=None):
self.client = uexpect.spawn(["/bin/bash", "--noediting"])
if command is None:
command = f'mysql --user default -N -s'
command = f"mysql --user default -N -s"
tcp_host = os.getenv("QUERY_MYSQL_HANDLER_HOST")
if tcp_host is not None:
command += f' --host={tcp_host}'
command += f" --host={tcp_host}"
else:
command += f' --host=127.0.0.1'
command += f" --host=127.0.0.1"
command += f' --prompt "{prompt}"'

tcp_port = os.getenv("QUERY_MYSQL_HANDLER_PORT")
Expand All @@ -32,30 +31,30 @@ def __init__(self, command=None, name='', log=None):
command += f" --port=3307"

self.client.command = command
self.client.eol('\r')
self.client.eol("\r")
self.client.logger(log, prefix=name)
self.client.timeout(120)
self.client.expect('[#\$] ', timeout=60)
self.client.expect("[#\$] ", timeout=60)
self.client.send(command)

def __enter__(self):
return self.client.__enter__()

def __exit__(self, type, value, traceback):
self.client.reader['kill_event'].set()
self.client.reader["kill_event"].set()
# send Ctrl-C
self.client.send('\x03', eol='')
self.client.send("\x03", eol="")
time.sleep(0.3)
self.client.send('quit', eol='\r')
self.client.send('\x03', eol='')
self.client.send("quit", eol="\r")
self.client.send("\x03", eol="")
return self.client.__exit__(type, value, traceback)


if __name__ == '__main__':
with NativeClient(name='client1>') as client1:
if __name__ == "__main__":
with NativeClient(name="client1>") as client1:
client1.expect(prompt)

client1.send('SET max_threads = 1;')
client1.expect('')
client1.send('SELECT 1 + 3;')
client1.expect('4')
client1.send("SET max_threads = 1;")
client1.expect("")
client1.send("SELECT 1 + 3;")
client1.expect("4")
Loading

0 comments on commit ffe76bf

Please sign in to comment.