You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mosquitto/test/client/03-publish-file.py

77 lines
1.9 KiB
Python

#!/usr/bin/env python3
#
from mosq_test_helper import *
def write_file(filename):
with open(filename, 'w') as f:
f.write("line1\n")
f.write("line2\n")
def do_test(proto_ver):
rc = 1
port = mosq_test.get_port()
data_file = os.path.basename(__file__).replace('.py', '.data')
if proto_ver == 5:
V = 'mqttv5'
elif proto_ver == 4:
V = 'mqttv311'
else:
V = 'mqttv31'
env = {
'XDG_CONFIG_HOME':'/tmp/missing'
}
env = mosq_test.env_add_ld_library_path(env)
cmd = [f'{mosq_test.get_build_root()}/client/mosquitto_pub',
'-p', str(port),
'-q', '1',
'-t', '03/pub/file/test',
'-f', data_file,
'-V', V
]
publish_packet = mosq_test.gen_publish("03/pub/file/test", qos=0, payload="line1\nline2\n", proto_ver=proto_ver)
broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port)
write_file(data_file)
try:
sock = mosq_test.sub_helper(port=port, topic="#", qos=0, proto_ver=proto_ver)
pub = subprocess.Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, env=env)
pub_terminate_rc = 0
if mosq_test.wait_for_subprocess(pub):
print("pub not terminated")
pub_terminate_rc = 1
mosq_test.expect_packet(sock, "publish", publish_packet)
rc = pub_terminate_rc
sock.close()
except mosq_test.TestError:
pass
except Exception as e:
print(e)
finally:
os.remove(data_file)
broker.terminate()
if mosq_test.wait_for_subprocess(broker):
print("broker not terminated")
if rc == 0: rc=1
(stdo, stde) = broker.communicate()
if rc:
print(stde.decode('utf-8'))
print("proto_ver=%d" % (proto_ver))
exit(rc)
do_test(proto_ver=3)
do_test(proto_ver=4)
do_test(proto_ver=5)