diff --git a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py index 35a719a..06c5691 100644 --- a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py +++ b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py @@ -7,19 +7,12 @@ import time from threading import Thread import ttfw_idf +from common_test_methods import get_my_ip4_by_dest_ip from tiny_test_fw import DUT msgid = -1 -def get_my_ip(): - s1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - s1.connect(('8.8.8.8', 80)) - my_ip = s1.getsockname()[0] - s1.close() - return my_ip - - def mqqt_server_sketch(my_ip, port): global msgid print('Starting the server on {}'.format(my_ip)) @@ -68,19 +61,20 @@ def test_examples_protocol_mqtt_qos1(env, extra_data): binary_file = os.path.join(dut1.app.binary_path, 'mqtt_tcp.bin') bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance('mqtt_tcp_bin_size', '{}KB'.format(bin_size // 1024)) - # 1. start mqtt broker sketch - host_ip = get_my_ip() - thread1 = Thread(target=mqqt_server_sketch, args=(host_ip,1883)) - thread1.start() - # 2. start the dut test and wait till client gets IP address + # 1. start the dut test and wait till client gets IP address dut1.start_app() # waiting for getting the IP address try: - ip_address = dut1.expect(re.compile(r'IPv4 address: ([^,]+),'), timeout=30) + ip_address = dut1.expect(re.compile(r'IPv4 address: ([^,]+),'), timeout=30)[0] print('Connected to AP/Ethernet with IP: {}'.format(ip_address)) except DUT.ExpectTimeout: raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP/Ethernet') + # 2. start mqtt broker sketch + host_ip = get_my_ip4_by_dest_ip(ip_address) + thread1 = Thread(target=mqqt_server_sketch, args=(host_ip,1883)) + thread1.start() + print('writing to device: {}'.format('mqtt://' + host_ip + '\n')) dut1.write('mqtt://' + host_ip + '\n') thread1.join() diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py index da22edb..2e99d02 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py @@ -15,6 +15,7 @@ from threading import Event, Lock, Thread import paho.mqtt.client as mqtt import ttfw_idf +from common_test_methods import get_my_ip4_by_dest_ip DEFAULT_MSG_SIZE = 16 @@ -33,19 +34,6 @@ def set_server_cert_cn(ip): raise('openssl command {} failed'.format(args)) -def get_my_ip(): - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - try: - # doesn't even have to be reachable - s.connect(('10.255.255.255', 1)) - IP = s.getsockname()[0] - except Exception: - IP = '127.0.0.1' - finally: - s.close() - return IP - - # Publisher class creating a python client to send/receive published data from esp-mqtt client class MqttPublisher: @@ -247,8 +235,8 @@ class TlsServer: self.shutdown.set() -def connection_tests(dut, cases): - ip = get_my_ip() +def connection_tests(dut, cases, dut_ip): + ip = get_my_ip4_by_dest_ip(dut_ip) set_server_cert_cn(ip) server_port = 2222 @@ -314,7 +302,7 @@ def connection_tests(dut, cases): teardown_connection_suite() -@ttfw_idf.idf_custom_test(env_tag='Example_EthKitV1', group='test-apps') +@ttfw_idf.idf_custom_test(env_tag='ethernet_router', group='test-apps') def test_app_protocol_mqtt_publish_connect(env, extra_data): """ steps: @@ -348,11 +336,11 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): raise dut1.start_app() - esp_ip = dut1.expect(re.compile(r' IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)'), timeout=30) - print('Got IP={}'.format(esp_ip[0])) + esp_ip = dut1.expect(re.compile(r' IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)'), timeout=30)[0] + print('Got IP={}'.format(esp_ip)) if not os.getenv('MQTT_SKIP_CONNECT_TEST'): - connection_tests(dut1,cases) + connection_tests(dut1,cases,esp_ip) # # start publish tests only if enabled in the environment (for weekend tests only)