From f941f83f73a7a820d4401d0de3d76e3ab77f3498 Mon Sep 17 00:00:00 2001 From: Roger Light Date: Mon, 27 Apr 2026 09:14:13 +0100 Subject: [PATCH] Tests: Refactor client argv tests --- .../02-subscribe-argv-errors-tls-psk.py | 21 +------------------ test/client/02-subscribe-argv-errors-tls.py | 20 +----------------- .../02-subscribe-argv-errors-without-tls.py | 20 +----------------- test/client/03-publish-argv-errors-tls-psk.py | 20 +----------------- test/client/03-publish-argv-errors-tls.py | 20 +----------------- .../03-publish-argv-errors-without-tls.py | 20 +----------------- test/client/04-rr-argv-errors-tls-psk.py | 19 +---------------- test/client/04-rr-argv-errors-tls.py | 18 +--------------- test/client/04-rr-argv-errors-without-tls.py | 19 +---------------- test/client/mosq_test_helper.py | 15 +++++++++++++ 10 files changed, 24 insertions(+), 168 deletions(-) diff --git a/test/client/02-subscribe-argv-errors-tls-psk.py b/test/client/02-subscribe-argv-errors-tls-psk.py index 19be2a1de..7d7a2d8a7 100755 --- a/test/client/02-subscribe-argv-errors-tls-psk.py +++ b/test/client/02-subscribe-argv-errors-tls-psk.py @@ -7,26 +7,7 @@ from mosq_test_helper import * mosq_test.require_features(["WITH_CLIENTS", "WITH_TLS", "WITH_TLS_PSK"]) def do_test(args, stderr_expected, rc_expected): - rc = 1 - - port = mosq_test.get_port() - - env = { - 'XDG_CONFIG_HOME':'/tmp/missing' - } - env = mosq_test.env_add_ld_library_path(env) - cmd = [mosq_paths.mosquitto_sub] + args - - sub = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) - if mosq_test.wait_for_subprocess(sub): - print("sub not terminated") - raise mosq_test.TestError(1) - (stdo, stde) = sub.communicate() - if sub.returncode != rc_expected: - raise mosq_test.TestError(sub.returncode) - if stderr_expected is not None and stderr_expected not in stde.decode('utf-8'): - raise mosq_test.TestError(stde) - + client_run(mosq_paths.mosquitto_sub, args, stderr_expected, rc_expected) if __name__ == '__main__': # Missing args for TLS-PSK related options diff --git a/test/client/02-subscribe-argv-errors-tls.py b/test/client/02-subscribe-argv-errors-tls.py index 2a587c290..e448f7b3e 100755 --- a/test/client/02-subscribe-argv-errors-tls.py +++ b/test/client/02-subscribe-argv-errors-tls.py @@ -7,25 +7,7 @@ from mosq_test_helper import * mosq_test.require_features(["WITH_TLS"]) def do_test(args, stderr_expected, rc_expected): - rc = 1 - - port = mosq_test.get_port() - - env = { - 'XDG_CONFIG_HOME':'/tmp/missing' - } - env = mosq_test.env_add_ld_library_path(env) - cmd = [mosq_paths.mosquitto_sub] + args - - sub = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) - if mosq_test.wait_for_subprocess(sub): - print("sub not terminated") - raise mosq_test.TestError(1) - (stdo, stde) = sub.communicate() - if sub.returncode != rc_expected: - raise mosq_test.TestError(sub.returncode) - if stderr_expected is not None and stderr_expected not in stde.decode('utf-8'): - raise mosq_test.TestError(stde) + client_run(mosq_paths.mosquitto_sub, args, stderr_expected, rc_expected) if __name__ == '__main__': diff --git a/test/client/02-subscribe-argv-errors-without-tls.py b/test/client/02-subscribe-argv-errors-without-tls.py index 7c67acd73..9eeadd0b4 100755 --- a/test/client/02-subscribe-argv-errors-without-tls.py +++ b/test/client/02-subscribe-argv-errors-without-tls.py @@ -5,25 +5,7 @@ from mosq_test_helper import * def do_test(args, stderr_expected, rc_expected): - rc = 1 - - port = mosq_test.get_port() - - env = { - 'XDG_CONFIG_HOME':'/tmp/missing' - } - env = mosq_test.env_add_ld_library_path(env) - cmd = [mosq_paths.mosquitto_sub] + args - - sub = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) - if mosq_test.wait_for_subprocess(sub): - print("sub not terminated") - raise mosq_test.TestError(1) - (stdo, stde) = sub.communicate() - if sub.returncode != rc_expected: - raise mosq_test.TestError(sub.returncode) - if stderr_expected is not None and stderr_expected not in stde.decode('utf-8'): - raise mosq_test.TestError(stde) + client_run(mosq_paths.mosquitto_sub, args, stderr_expected, rc_expected) if __name__ == '__main__': diff --git a/test/client/03-publish-argv-errors-tls-psk.py b/test/client/03-publish-argv-errors-tls-psk.py index 947eea4c2..a5f1ed079 100755 --- a/test/client/03-publish-argv-errors-tls-psk.py +++ b/test/client/03-publish-argv-errors-tls-psk.py @@ -7,25 +7,7 @@ from mosq_test_helper import * mosq_test.require_features(["WITH_CLIENTS", "WITH_TLS", "WITH_TLS_PSK"]) def do_test(args, stderr_expected, rc_expected): - rc = 1 - - port = mosq_test.get_port() - - env = { - 'XDG_CONFIG_HOME':'/tmp/missing' - } - env = mosq_test.env_add_ld_library_path(env) - cmd = [mosq_paths.mosquitto_pub] + args - - pub = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) - if mosq_test.wait_for_subprocess(pub): - print("pub not terminated") - raise mosq_test.TestError(1) - (stdo, stde) = pub.communicate() - if pub.returncode != rc_expected: - raise mosq_test.TestError(pub.returncode) - if stderr_expected is not None and stderr_expected not in stde.decode('utf-8'): - raise mosq_test.TestError(stde) + client_run(mosq_paths.mosquitto_pub, args, stderr_expected, rc_expected) if __name__ == '__main__': diff --git a/test/client/03-publish-argv-errors-tls.py b/test/client/03-publish-argv-errors-tls.py index 435821530..d4dc313ed 100755 --- a/test/client/03-publish-argv-errors-tls.py +++ b/test/client/03-publish-argv-errors-tls.py @@ -7,25 +7,7 @@ from mosq_test_helper import * mosq_test.require_features(["WITH_TLS"]) def do_test(args, stderr_expected, rc_expected): - rc = 1 - - port = mosq_test.get_port() - - env = { - 'XDG_CONFIG_HOME':'/tmp/missing' - } - env = mosq_test.env_add_ld_library_path(env) - cmd = [mosq_paths.mosquitto_pub] + args - - pub = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) - if mosq_test.wait_for_subprocess(pub): - print("pub not terminated") - raise mosq_test.TestError(1) - (stdo, stde) = pub.communicate() - if pub.returncode != rc_expected: - raise mosq_test.TestError(pub.returncode) - if stderr_expected is not None and stderr_expected not in stde.decode('utf-8'): - raise mosq_test.TestError(stde.decode('utf-8')) + client_run(mosq_paths.mosquitto_pub, args, stderr_expected, rc_expected) if __name__ == '__main__': diff --git a/test/client/03-publish-argv-errors-without-tls.py b/test/client/03-publish-argv-errors-without-tls.py index 41b6b1cbc..0d10eed6b 100755 --- a/test/client/03-publish-argv-errors-without-tls.py +++ b/test/client/03-publish-argv-errors-without-tls.py @@ -5,25 +5,7 @@ from mosq_test_helper import * def do_test(args, stderr_expected, rc_expected): - rc = 1 - - port = mosq_test.get_port() - - env = { - 'XDG_CONFIG_HOME':'/tmp/missing' - } - env = mosq_test.env_add_ld_library_path(env) - cmd = [mosq_paths.mosquitto_pub] + args - - pub = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) - if mosq_test.wait_for_subprocess(pub): - print("pub not terminated") - raise mosq_test.TestError(1) - (stdo, stde) = pub.communicate() - if pub.returncode != rc_expected: - raise mosq_test.TestError(pub.returncode) - if stderr_expected is not None and stderr_expected not in stde.decode('utf-8'): - raise mosq_test.TestError(stde) + client_run(mosq_paths.mosquitto_pub, args, stderr_expected, rc_expected) if __name__ == '__main__': diff --git a/test/client/04-rr-argv-errors-tls-psk.py b/test/client/04-rr-argv-errors-tls-psk.py index 4c9c4a0d2..00d3f0767 100755 --- a/test/client/04-rr-argv-errors-tls-psk.py +++ b/test/client/04-rr-argv-errors-tls-psk.py @@ -7,24 +7,7 @@ from mosq_test_helper import * mosq_test.require_features(["WITH_CLIENTS", "WITH_TLS", "WITH_TLS_PSK"]) def do_test(args, stderr_expected, rc_expected): - rc = 1 - - port = mosq_test.get_port() - - env = { - 'XDG_CONFIG_HOME':'/tmp/missing' - } - env = mosq_test.env_add_ld_library_path(env) - - cmd = [mosq_paths.mosquitto_rr] + args - - sub = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) - sub.wait() - (stdo, stde) = sub.communicate() - if sub.returncode != rc_expected: - raise mosq_test.TestError(sub.returncode) - if stderr_expected is not None and stderr_expected not in stde.decode('utf-8'): - raise mosq_test.TestError(stde) + client_run(mosq_paths.mosquitto_rr, args, stderr_expected, rc_expected) if __name__ == '__main__': diff --git a/test/client/04-rr-argv-errors-tls.py b/test/client/04-rr-argv-errors-tls.py index 966ae12c8..d5f6f0013 100755 --- a/test/client/04-rr-argv-errors-tls.py +++ b/test/client/04-rr-argv-errors-tls.py @@ -7,23 +7,7 @@ from mosq_test_helper import * mosq_test.require_features(["WITH_TLS"]) def do_test(args, stderr_expected, rc_expected): - rc = 1 - - port = mosq_test.get_port() - - env = { - 'XDG_CONFIG_HOME':'/tmp/missing' - } - env = mosq_test.env_add_ld_library_path(env) - cmd = [mosq_paths.mosquitto_rr] + args - - sub = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) - sub.wait() - (stdo, stde) = sub.communicate() - if sub.returncode != rc_expected: - raise mosq_test.TestError(sub.returncode) - if stderr_expected is not None and stderr_expected not in stde.decode('utf-8'): - raise mosq_test.TestError(stde) + client_run(mosq_paths.mosquitto_rr, args, stderr_expected, rc_expected) if __name__ == '__main__': diff --git a/test/client/04-rr-argv-errors-without-tls.py b/test/client/04-rr-argv-errors-without-tls.py index 5e215fcf2..e1dc13f62 100755 --- a/test/client/04-rr-argv-errors-without-tls.py +++ b/test/client/04-rr-argv-errors-without-tls.py @@ -5,24 +5,7 @@ from mosq_test_helper import * def do_test(args, stderr_expected, rc_expected): - rc = 1 - - port = mosq_test.get_port() - - env = { - 'XDG_CONFIG_HOME':'/tmp/missing' - } - env = mosq_test.env_add_ld_library_path(env) - cmd = [mosq_paths.mosquitto_rr] + args - - sub = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) - if mosq_test.wait_for_subprocess(sub): - print("sub not terminated") - raise mosq_test.TestError(1) - if sub.returncode != rc_expected: - raise mosq_test.TestError(sub.returncode) - if stderr_expected is not None and stderr_expected not in stde.decode('utf-8'): - raise mosq_test.TestError(stde) + client_run(mosq_paths.mosquitto_rr, args, stderr_expected, rc_expected) if __name__ == '__main__': diff --git a/test/client/mosq_test_helper.py b/test/client/mosq_test_helper.py index 7e83feaf0..ddc08b023 100644 --- a/test/client/mosq_test_helper.py +++ b/test/client/mosq_test_helper.py @@ -21,3 +21,18 @@ import errno from pathlib import Path source_dir = Path(__file__).resolve().parent + +def client_run(cmd_path, args, stderr_expected, rc_expected): + port = mosq_test.get_port() + + env = { + 'XDG_CONFIG_HOME':'/tmp/missing' + } + env = mosq_test.env_add_ld_library_path(env) + cmd = [cmd_path] + args + + client = subprocess.run(cmd, capture_output=True, text=True, env=env) + if client.returncode != rc_expected: + raise mosq_test.TestError(client.returncode) + if stderr_expected is not None and stderr_expected not in client.stderr: + raise mosq_test.TestError(f"Got:\n{client.stderr}\nExpected:\n{stderr_expected}") \ No newline at end of file