Skip to content

Commit bab32dc

Browse files
committed
WL14634: Running unit tests against external server
Currently, unit tests are written so that the testing framework starts servers required by the tests. This worklog aims to modify existing unit tests so that they can also run against an external server not controlled by the testing framework. The following command line arguments can be used: --use-external-server Sets to use an external server. (Default: False) --host Hostname or IP address for TCP/IP connections to use with an external server. (Default: 127.0.0.1) --port TCP/IP port to use with an external server. (Default: 8806) --mysqlx-port MySQL X TCP/IP port to use with an external server. (Default: 88060) --user User to use with an external server. (Default: root) --password Password to with an external server. (Default: "") Example: python unittest.py --use-external-server --host=10.0.0.32 As an alternative to command line arguments, environment variables can be used instead: host: $MYSQL_HOST port: $MYSQL_PORT mysqlx port: $MYSQLX_PORT user: $MYSQL_USER password: $MYSQL_PASSWORD The testing system will use the command line arguments, but if they are not set, the system will try to use the environment variables. Finally, if no arguments or environment are set, the MySQL defaults will be used.
1 parent 61ac226 commit bab32dc

18 files changed

+517
-78
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Full release notes:
1111
v8.0.26
1212
=======
1313

14+
- WL#14634: Running unit tests against external server
1415
- WL#14542: Deprecate TLS 1.0 and 1.1
1516
- WL#14440: Support for authentication_kerberos_client authentication plugin
1617
- WL#14237: Support query attributes

examples/inserts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4-
# Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved.
4+
# Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.
55
#
66
# This program is free software; you can redistribute it and/or modify
77
# it under the terms of the GNU General Public License, version 2.0, as
@@ -53,7 +53,7 @@ def main(config):
5353
"CREATE TABLE names ("
5454
" id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT, "
5555
" name VARCHAR(30) DEFAULT '' NOT NULL, "
56-
" info TEXT DEFAULT '', "
56+
" info TEXT, "
5757
" age TINYINT UNSIGNED DEFAULT '30', "
5858
"PRIMARY KEY (id))"
5959
)

examples/multi_resultsets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4-
# Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved.
4+
# Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.
55
#
66
# This program is free software; you can redistribute it and/or modify
77
# it under the terms of the GNU General Public License, version 2.0, as
@@ -52,7 +52,7 @@ def main(config):
5252
"CREATE TABLE names ("
5353
" id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT, "
5454
" name VARCHAR(30) DEFAULT '' NOT NULL, "
55-
" info TEXT DEFAULT '', "
55+
" info TEXT, "
5656
" age TINYINT UNSIGNED DEFAULT '30', "
5757
" PRIMARY KEY (id))"
5858
)

tests/__init__.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ def load_source(name, path):
136136

137137
OPTIONS_INIT = False
138138

139+
MYSQL_EXTERNAL_SERVER = False
139140
MYSQL_SERVERS_NEEDED = 1
140141
MYSQL_SERVERS = []
141142
MYSQL_VERSION = ()
@@ -1000,8 +1001,20 @@ def is_plugin_available(plugin_name, config_vars=None, in_server=None):
10001001
10011002
Returns True if plugin an success else False.
10021003
"""
1003-
available = False
10041004
server = in_server if in_server else MYSQL_SERVERS[0]
1005+
if MYSQL_EXTERNAL_SERVER:
1006+
from mysql.connector import MySQLConnection
1007+
config = server.client_config.copy()
1008+
del config["database"]
1009+
with MySQLConnection(**config) as cnx:
1010+
cnx.cmd_query("SHOW PLUGINS")
1011+
res = cnx.get_rows()
1012+
for row in res[0]:
1013+
if row[0] == plugin_name and row[1] == "ACTIVE":
1014+
return True
1015+
return False
1016+
1017+
available = False
10051018
plugin_config_vars = config_vars if config_vars else []
10061019
server_cnf = server._cnf
10071020
server_cnf_bkp = server._cnf

tests/cext/test_cext_api.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,10 @@ def test_commit(self):
463463

464464
cmy1.query("DROP TABLE IF EXISTS {0}".format(table))
465465

466+
@unittest.skipIf(
467+
tests.MYSQL_EXTERNAL_SERVER,
468+
"Test not available for external MySQL servers",
469+
)
466470
def test_change_user(self):
467471
connect_kwargs = self.connect_kwargs.copy()
468472
cnx = CMySQLConnection(**connect_kwargs)
@@ -737,7 +741,7 @@ def test_get_host_info(self):
737741

738742
cmy.connect(**config)
739743

740-
if os.name == 'posix':
744+
if os.name == 'posix' and not tests.MYSQL_EXTERNAL_SERVER:
741745
# On POSIX systems we would be connected by UNIX socket
742746
self.assertTrue('via UNIX socket' in cmy.get_host_info())
743747

tests/issues/test_bug21449207.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
# Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
3+
# Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
44
#
55
# This program is free software; you can redistribute it and/or modify
66
# it under the terms of the GNU General Public License, version 2.0, as
@@ -31,6 +31,7 @@
3131
import mysql.connector
3232
from tests import foreach_cnx, cnx_config
3333
import tests
34+
import unittest
3435

3536

3637
class Bug21449207(tests.MySQLConnectorTests):
@@ -41,7 +42,7 @@ def setUp(self):
4142

4243
create_table = (
4344
"CREATE TABLE {0} ("
44-
"id INT PRIMARY KEY, "
45+
"id INT PRIMARY KEY AUTO_INCREMENT, "
4546
"a LONGTEXT "
4647
") ENGINE=Innodb DEFAULT CHARSET utf8".format(self.tbl))
4748
cnx.cmd_query(create_table)
@@ -74,6 +75,10 @@ def test_50k_compressed(self):
7475
self.assertEqual(exp, row[0])
7576
self.assertEqual(row[0][-20:], exp[-20:])
7677

78+
@unittest.skipIf(
79+
tests.MYSQL_EXTERNAL_SERVER,
80+
"Test not available for external MySQL servers",
81+
)
7782
@foreach_cnx()
7883
def test_16M_compressed(self):
7984
cur = self.cnx.cursor()

tests/issues/test_bug21449996.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,16 @@
3131
import mysql.connector
3232
from tests import foreach_cnx
3333
import tests
34+
import unittest
3435

3536
# using "/" (slash) to avoid windows scape characters
3637
DATA_FILE = "/".join(['tests', 'data', 'random_big_bin.csv'])
3738

39+
40+
@unittest.skipIf(
41+
tests.MYSQL_EXTERNAL_SERVER,
42+
"Test not available for external MySQL servers",
43+
)
3844
class Bug21449996(tests.MySQLConnectorTests):
3945

4046
def setUp(self):

tests/issues/test_bug22545879.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
# Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
3+
# Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
44
#
55
# This program is free software; you can redistribute it and/or modify
66
# it under the terms of the GNU General Public License, version 2.0, as
@@ -52,6 +52,11 @@
5252

5353
OPTION_FILE = os.path.join('tests', 'data', 'option_files', 'my.cnf')
5454

55+
56+
@unittest.skipIf(
57+
tests.MYSQL_EXTERNAL_SERVER,
58+
"Test not available for external MySQL servers",
59+
)
5560
class Bug21449996(tests.MySQLConnectorTests):
5661

5762
@cnx_config(ssl_ca=TEST_SSL['ca'], ssl_cert=TEST_SSL['cert'], ssl_key=TEST_SSL['key'],

tests/mysqld.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2009, 2020, Oracle and/or its affiliates.
1+
# Copyright (c) 2009, 2021, Oracle and/or its affiliates.
22
#
33
# This program is free software; you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License, version 2.0, as
@@ -834,6 +834,7 @@ def handle(self):
834834
self._server_replies = self._server_replies[bufsize:]
835835
return res
836836

837+
837838
class DummyMySQLServer(ThreadingMixIn, TCPServer):
838839
"""Class accepting connections for testing MySQL connections"""
839840

@@ -863,3 +864,53 @@ def reset(self):
863864

864865
def get_address(self):
865866
return 'dummy'
867+
868+
869+
class MySQLExternalServer:
870+
"""Class for managing an external MySQL Server."""
871+
872+
def __init__(self, cnf, name):
873+
self.cnf = cnf
874+
self.name = name
875+
self.version = None
876+
self.license = None
877+
878+
self.bind_address = None
879+
self.unix_socket = None
880+
self.mysqlx_unix_socket = None
881+
self.pid_file = None
882+
self.client_config = {}
883+
self.xplugin_config = {}
884+
885+
def _exit_with_unsupported_operation(self):
886+
LOGGER.error(
887+
"Operation not supported when using an external MySQL server"
888+
)
889+
sys.exit(1)
890+
891+
def bootstrap(self):
892+
self._exit_with_unsupported_operation()
893+
894+
def check_running(self, pid=None):
895+
self._exit_with_unsupported_operation()
896+
897+
def start(self, **kwargs):
898+
self._exit_with_unsupported_operation()
899+
900+
def stop(self):
901+
self._exit_with_unsupported_operation()
902+
903+
def remove(self):
904+
self._exit_with_unsupported_operation()
905+
906+
def get_exec(self, exec_name):
907+
self._exit_with_unsupported_operation()
908+
909+
def update_config(self, **kwargs):
910+
self._exit_with_unsupported_operation()
911+
912+
def wait_up(self, tries=10, delay=1):
913+
self._exit_with_unsupported_operation()
914+
915+
def wait_down(self, tries=10, delay=1):
916+
self._exit_with_unsupported_operation()

tests/test_abstracts.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2014, 2020, Oracle and/or its affiliates.
1+
# Copyright (c) 2014, 2021, Oracle and/or its affiliates.
22
#
33
# This program is free software; you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License, version 2.0, as
@@ -225,6 +225,10 @@ def test_cmd_quit(self):
225225
self.cnx.cmd_quit()
226226
self.assertFalse(self.cnx.is_connected())
227227

228+
@unittest.skipIf(
229+
tests.MYSQL_EXTERNAL_SERVER,
230+
"Test not available for external MySQL servers",
231+
)
228232
@unittest.skipIf(tests.MYSQL_VERSION >= (8, 0, 1),
229233
"As of MySQL 8.0.1, CMD_SHUTDOWN is not recognized.")
230234
@unittest.skipIf(tests.MYSQL_VERSION <= (5, 7, 1),

0 commit comments

Comments
 (0)