Skip to content

Commit 61ac226

Browse files
committed
Fix: connection tests plugin pre-setup
1 parent 409d3ee commit 61ac226

File tree

2 files changed

+128
-90
lines changed

2 files changed

+128
-90
lines changed

tests/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -970,15 +970,22 @@ def check_c_extension(exc=None):
970970

971971

972972
@lru_cache(maxsize=10, typed=False)
973-
def is_host_reachable(host):
973+
def is_host_reachable(host, log=False):
974974
"""Attempts to reach a host, by using the ping command.
975975
Returns True if success else False.
976976
"""
977977
param_attemps = "-n" if os.name == "nt" else "-c"
978978
attemps = "1"
979979
command = ["ping", param_attemps, attemps, host]
980980
try:
981-
return subprocess.call(command) == 0
981+
if log:
982+
top_dir = os.getcwd()
983+
ping_log_path = os.path.join(top_dir, 'ping.log')
984+
with open(ping_log_path, 'a+') as ping_log:
985+
return subprocess.call(command, stdout=ping_log) == 0
986+
else:
987+
return subprocess.call(command, stdout=subprocess.DEVNULL) == 0
988+
982989
except OSError:
983990
return False
984991

tests/test_connection.py

Lines changed: 119 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -2571,17 +2571,18 @@ def test_connect_with_can_handle_expired_pw_flag(self):
25712571

25722572

25732573
@unittest.skipIf(tests.MYSQL_VERSION < (5, 7),
2574-
"Authentication with ldap_simple not supported")
2575-
#Skip if remote ldap server is not reachable.
2576-
@unittest.skipIf(not tests.is_host_reachable("10.172.166.126"),
2577-
"ldap server is not reachable")
2578-
@unittest.skipIf(not tests.is_plugin_available("authentication_ldap_sasl"),
2579-
"Plugin authentication_ldap_sasl not available")
2574+
"Authentication with ldap_sasl not supported")
25802575
class WL14110(tests.MySQLConnectorTests):
25812576
"""WL#14110: Add support for SCRAM-SHA-1
25822577
"""
25832578
def setUp(self):
25842579
self.server = tests.MYSQL_SERVERS[0]
2580+
if not "com" in self.server.license:
2581+
self.skipTest("Plugin not available in this version")
2582+
if not tests.is_host_reachable("10.172.166.126"):
2583+
#Skip if remote ldap server is not reachable.
2584+
self.skipTest("Remote ldap server is not reachable")
2585+
25852586
self.server_cnf = self.server._cnf
25862587
self.config = tests.get_mysql_config()
25872588
self.config.pop("unix_socket", None)
@@ -2614,24 +2615,31 @@ def setUp(self):
26142615
self.server.wait_up()
26152616
sleep(1)
26162617

2617-
cnx = connection.MySQLConnection(**self.config)
2618-
2619-
try:
2620-
cnx.cmd_query("DROP USER '{}'@'{}'".format(self.user, self.host))
2621-
cnx.cmd_query("DROP USER '{}'@'{}'".format("common", self.host))
2622-
except:
2623-
pass
2618+
with connection.MySQLConnection(**self.config) as cnx:
2619+
cnx.cmd_query("SHOW PLUGINS")
2620+
res = cnx.get_rows()
2621+
available = False
2622+
for row in res[0]:
2623+
if row[0].lower() == "authentication_ldap_sasl":
2624+
if row[1] == "ACTIVE":
2625+
available = True
2626+
if not available:
2627+
self.skipTest("Plugin authentication_ldap_sasl not available")
26242628

2625-
cnx.cmd_query("CREATE USER '{}'@'{}' IDENTIFIED "
2626-
"WITH authentication_ldap_sasl"
2627-
"".format(self.user, self.host))
2629+
try:
2630+
cnx.cmd_query("DROP USER '{}'@'{}'".format(self.user, self.host))
2631+
cnx.cmd_query("DROP USER '{}'@'{}'".format("common", self.host))
2632+
except:
2633+
pass
26282634

2629-
cnx.cmd_query("CREATE USER '{}'@'{}'"
2630-
"".format("common", self.host))
2631-
cnx.cmd_query("GRANT ALL ON *.* TO '{}'@'{}'"
2632-
"".format("common", self.host))
2635+
cnx.cmd_query("CREATE USER '{}'@'{}' IDENTIFIED "
2636+
"WITH authentication_ldap_sasl"
2637+
"".format(self.user, self.host))
26332638

2634-
cnx.close()
2639+
cnx.cmd_query("CREATE USER '{}'@'{}'"
2640+
"".format("common", self.host))
2641+
cnx.cmd_query("GRANT ALL ON *.* TO '{}'@'{}'"
2642+
"".format("common", self.host))
26352643

26362644
def tearDown(self):
26372645
return
@@ -2645,7 +2653,7 @@ def tearDown(self):
26452653
cnx.close()
26462654

26472655
@tests.foreach_cnx()
2648-
def test_authentication_ldap_sasl_client(self):
2656+
def test_authentication_ldap_sasl_client_with_SCRAM_SHA_1(self):
26492657
"""test_authentication_ldap_sasl_client_with_SCRAM-SHA-1"""
26502658
# Not running with c-ext if plugin libraries are not setup
26512659
if self.cnx.__class__ == CMySQLConnection and \
@@ -2700,16 +2708,17 @@ def test_authentication_ldap_sasl_client(self):
27002708

27012709
@unittest.skipIf(tests.MYSQL_VERSION < (5, 7),
27022710
"Authentication with ldap_simple not supported")
2703-
@unittest.skipIf(not tests.is_plugin_available("authentication_ldap_simple"),
2704-
"Plugin authentication_ldap_simple not available")
2705-
#Skip if remote ldap server is not reachable.
2706-
@unittest.skipIf(not tests.is_host_reachable("100.103.18.98"),
2707-
"ldap server is not reachable")
27082711
class WL13994(tests.MySQLConnectorTests):
27092712
"""WL#13994: Support clear text passwords
27102713
"""
27112714
def setUp(self):
27122715
self.server = tests.MYSQL_SERVERS[0]
2716+
if not "com" in self.server.license:
2717+
self.skipTest("Plugin not available in this version")
2718+
if not tests.is_host_reachable("100.103.18.98"):
2719+
# Skip test, remote ldap server is not reachable.
2720+
self.skipTest("Remote ldap server is not reachable")
2721+
27132722
self.server_cnf = self.server._cnf
27142723
self.config = tests.get_mysql_config()
27152724
self.config.pop("unix_socket", None)
@@ -2746,17 +2755,26 @@ def setUp(self):
27462755
self.server.wait_up()
27472756
sleep(1)
27482757

2749-
cnx = connection.MySQLConnection(**self.config)
2750-
2751-
identified_by = "CN=test1,CN=Users,DC=mysql,DC=local"
2752-
2753-
cnx.cmd_query("CREATE USER '{}'@'{}' IDENTIFIED "
2754-
"WITH authentication_ldap_simple AS"
2755-
"'{}'".format(self.user, self.host, identified_by))
2756-
cnx.cmd_query("GRANT ALL ON *.* TO '{}'@'{}'"
2757-
"".format(self.user, self.host))
2758-
cnx.cmd_query("FLUSH PRIVILEGES")
2759-
cnx.close()
2758+
with connection.MySQLConnection(**self.config) as cnx:
2759+
cnx.cmd_query("SHOW PLUGINS")
2760+
res = cnx.get_rows()
2761+
available = False
2762+
for row in res[0]:
2763+
if row[0].lower() == "authentication_ldap_simple":
2764+
if row[1] == "ACTIVE":
2765+
available = True
2766+
2767+
if not available:
2768+
self.skipTest("Plugin authentication_ldap_simple not available")
2769+
2770+
identified_by = "CN=test1,CN=Users,DC=mysql,DC=local"
2771+
2772+
cnx.cmd_query("CREATE USER '{}'@'{}' IDENTIFIED "
2773+
"WITH authentication_ldap_simple AS"
2774+
"'{}'".format(self.user, self.host, identified_by))
2775+
cnx.cmd_query("GRANT ALL ON *.* TO '{}'@'{}'"
2776+
"".format(self.user, self.host))
2777+
cnx.cmd_query("FLUSH PRIVILEGES")
27602778

27612779
def tearDown(self):
27622780
cnx = connection.MySQLConnection(**self.config)
@@ -2862,20 +2880,19 @@ def test_clear_text_pass(self):
28622880
self.assertRaises(InterfaceError, self.cnx.__class__, **conn_args)
28632881

28642882

2865-
plugin_opts = (("authentication_ldap_sasl_auth_method_name", "SCRAM-SHA-256"),)
2866-
@unittest.skipIf(tests.MYSQL_VERSION < (8, 2, 22),
2867-
"Authentication with ldap_simple not supported")
2868-
#Skip if remote ldap server is not reachable.
2869-
@unittest.skipIf(not tests.is_host_reachable("100.103.19.5"),
2870-
"ldap server is not reachable")
2871-
@unittest.skipIf(not tests.is_plugin_available("authentication_ldap_sasl",
2872-
plugin_opts),
2873-
"Plugin authentication_ldap_simple not available")
2883+
@unittest.skipIf(tests.MYSQL_VERSION < (8, 0, 22),
2884+
"Authentication with ldap_sasl not supported")
28742885
class WL14263(tests.MySQLConnectorTests):
28752886
"""WL#14110: Add support for SCRAM-SHA-256
28762887
"""
28772888
def setUp(self):
28782889
self.server = tests.MYSQL_SERVERS[0]
2890+
if not "com" in self.server.license:
2891+
self.skipTest("Plugin not available in this version")
2892+
if not tests.is_host_reachable("100.103.19.5"):
2893+
# Skip if remote ldap server is not reachable.
2894+
self.skipTest("Remote ldap server is not reachable")
2895+
28792896
self.server_cnf = self.server._cnf
28802897
self.config = tests.get_mysql_config()
28812898
self.config.pop("unix_socket", None)
@@ -2908,27 +2925,33 @@ def setUp(self):
29082925
self.server.wait_up()
29092926
sleep(1)
29102927

2911-
cnx = connection.MySQLConnection(**self.config)
2912-
2913-
try:
2914-
cnx.cmd_query("DROP USER '{}'@'{}'".format(self.user, self.host))
2915-
cnx.cmd_query("DROP USER '{}'@'{}'".format("common", self.host))
2916-
except:
2917-
pass
2928+
with connection.MySQLConnection(**self.config) as cnx:
2929+
cnx.cmd_query("SHOW PLUGINS")
2930+
res = cnx.get_rows()
2931+
available = False
2932+
for row in res[0]:
2933+
if row[0].lower() == "authentication_ldap_sasl":
2934+
if row[1] == "ACTIVE":
2935+
available = True
2936+
if not available:
2937+
self.skipTest("Plugin authentication_ldap_sasl not available")
29182938

2919-
cnx.cmd_query("CREATE USER '{}'@'{}' IDENTIFIED "
2920-
"WITH authentication_ldap_sasl"
2921-
"".format(self.user, self.host))
2939+
try:
2940+
cnx.cmd_query("DROP USER '{}'@'{}'".format(self.user, self.host))
2941+
cnx.cmd_query("DROP USER '{}'@'{}'".format("common", self.host))
2942+
except:
2943+
pass
29222944

2923-
cnx.cmd_query("CREATE USER '{}'@'{}'"
2924-
"".format("common", self.host))
2925-
cnx.cmd_query("GRANT ALL ON *.* TO '{}'@'{}'"
2926-
"".format("common", self.host))
2945+
cnx.cmd_query("CREATE USER '{}'@'{}' IDENTIFIED "
2946+
"WITH authentication_ldap_sasl"
2947+
"".format(self.user, self.host))
29272948

2928-
cnx.close()
2949+
cnx.cmd_query("CREATE USER '{}'@'{}'"
2950+
"".format("common", self.host))
2951+
cnx.cmd_query("GRANT ALL ON *.* TO '{}'@'{}'"
2952+
"".format("common", self.host))
29292953

29302954
def tearDown(self):
2931-
return
29322955
cnx = connection.MySQLConnection(**self.config)
29332956
try:
29342957
cnx.cmd_query("DROP USER '{}'@'{}'".format(self.user, self.host))
@@ -2939,7 +2962,7 @@ def tearDown(self):
29392962
cnx.close()
29402963

29412964
@tests.foreach_cnx()
2942-
def test_authentication_ldap_sasl_client(self):
2965+
def test_authentication_ldap_sasl_client_with_SCRAM_SHA_256(self):
29432966
"""test_authentication_ldap_sasl_client_with_SCRAM-SHA-256"""
29442967
# Not running with c-ext if plugin libraries are not setup
29452968
if self.cnx.__class__ == CMySQLConnection and \
@@ -3086,19 +3109,20 @@ def test_failover(self):
30863109
_ = connect(**cnx_config)
30873110

30883111
@unittest.skipIf(tests.MYSQL_VERSION < (5, 7),
3089-
"Authentication with ldap_simple not supported")
3090-
#Skip if remote ldap server is not reachable.
3091-
@unittest.skipIf(not tests.is_host_reachable("100.103.18.98"),
3092-
"ldap server is not reachable")
3093-
@unittest.skipIf(not tests.is_plugin_available("authentication_ldap_sasl"),
3094-
"Plugin authentication_ldap_sasl not available")
3095-
@unittest.skipIf(os.name == "nt", "Skipping due to GRANT PROXY issue")
3112+
"Authentication with sasl GSSAPI not supported")
3113+
@unittest.skipIf(os.name == "nt", "Not available on Windows")
30963114
@unittest.skipIf(gssapi == None, "GSSAPI Module not installed")
30973115
class WL14213(tests.MySQLConnectorTests):
30983116
"""WL#14213: Add support for Kerberos authentication.
30993117
"""
31003118
def setUp(self):
31013119
self.server = tests.MYSQL_SERVERS[0]
3120+
if not "com" in self.server.license:
3121+
self.skipTest("Plugin not available in this version")
3122+
if not tests.is_host_reachable("100.103.18.98"):
3123+
# Skip if remote ldap server is not reachable.
3124+
self.skipTest("Remote ldap server is not reachable")
3125+
31023126
self.server_cnf = self.server._cnf
31033127
self.config = tests.get_mysql_config()
31043128
self.user = "test3"
@@ -3133,26 +3157,33 @@ def setUp(self):
31333157
self.server.wait_up()
31343158
sleep(1)
31353159

3136-
cnx = connection.MySQLConnection(**self.config)
3137-
3138-
try:
3139-
cnx.cmd_query("DROP USER '{}@{}'".format(self.user, self.host))
3140-
cnx.cmd_query("DROP USER '{}'".format("mysql_engineering"))
3141-
except:
3142-
pass
3160+
with connection.MySQLConnection(**self.config) as cnx:
3161+
cnx.cmd_query("SHOW PLUGINS")
3162+
res = cnx.get_rows()
3163+
available = False
3164+
for row in res[0]:
3165+
if row[0].lower() == "authentication_ldap_sasl":
3166+
if row[1] == "ACTIVE":
3167+
available = True
3168+
if not available:
3169+
self.skipTest("Plugin authentication_ldap_sasl not available")
31433170

3144-
cnx.cmd_query("CREATE USER '{}@{}' IDENTIFIED "
3145-
"WITH authentication_ldap_sasl "
3146-
"BY \"#testgrp=mysql_engineering\""
3147-
"".format(self.user, self.host))
3171+
try:
3172+
cnx.cmd_query("DROP USER '{}@{}'".format(self.user, self.host))
3173+
cnx.cmd_query("DROP USER '{}'".format("mysql_engineering"))
3174+
except:
3175+
pass
31483176

3149-
cnx.cmd_query("CREATE USER '{}'".format("mysql_engineering"))
3150-
cnx.cmd_query("GRANT ALL ON myconnpy.* TO '{}'"
3151-
"".format("mysql_engineering"))
3152-
cnx.cmd_query("GRANT PROXY on '{}' TO '{}@{}'"
3153-
"".format("mysql_engineering", self.user, self.host))
3177+
cnx.cmd_query("CREATE USER '{}@{}' IDENTIFIED "
3178+
"WITH authentication_ldap_sasl "
3179+
"BY \"#testgrp=mysql_engineering\""
3180+
"".format(self.user, self.host))
31543181

3155-
cnx.close()
3182+
cnx.cmd_query("CREATE USER '{}'".format("mysql_engineering"))
3183+
cnx.cmd_query("GRANT ALL ON myconnpy.* TO '{}'"
3184+
"".format("mysql_engineering"))
3185+
cnx.cmd_query("GRANT PROXY on '{}' TO '{}@{}'"
3186+
"".format("mysql_engineering", self.user, self.host))
31563187

31573188
def tearDown(self):
31583189
return

0 commit comments

Comments
 (0)