pytest_http_server_persistence.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env python
  2. #
  3. # SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD
  4. # SPDX-License-Identifier: Apache-2.0
  5. from __future__ import division, print_function, unicode_literals
  6. import logging
  7. import os
  8. import random
  9. import sys
  10. from builtins import range, str
  11. import pytest
  12. try:
  13. from idf_http_server_test import adder as client
  14. except ModuleNotFoundError:
  15. sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', '..', '..', 'tools', 'ci', 'python_packages'))
  16. from idf_http_server_test import adder as client
  17. from common_test_methods import get_env_config_variable
  18. from pytest_embedded import Dut
  19. # When running on local machine execute the following before running this script
  20. # > make app bootloader
  21. # > make print_flash_cmd | tail -n 1 > build/download.config
  22. @pytest.mark.esp32
  23. @pytest.mark.esp32c3
  24. @pytest.mark.esp32s2
  25. @pytest.mark.esp32s3
  26. @pytest.mark.wifi_router
  27. def test_examples_protocol_http_server_persistence(dut: Dut) -> None:
  28. # Get binary file
  29. binary_file = os.path.join(dut.app.binary_path, 'persistent_sockets.bin')
  30. bin_size = os.path.getsize(binary_file)
  31. logging.info('http_server_bin_size : {}KB'.format(bin_size // 1024))
  32. # Upload binary and start testing
  33. logging.info('Starting http_server persistance test app')
  34. # Parse IP address of STA
  35. logging.info('Waiting to connect with AP')
  36. if dut.app.sdkconfig.get('EXAMPLE_WIFI_SSID_PWD_FROM_STDIN') is True:
  37. dut.expect('Please input ssid password:')
  38. env_name = 'wifi_router'
  39. ap_ssid = get_env_config_variable(env_name, 'ap_ssid')
  40. ap_password = get_env_config_variable(env_name, 'ap_password')
  41. dut.write(f'{ap_ssid} {ap_password}')
  42. got_ip = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]', timeout=30)[1].decode()
  43. got_port = dut.expect(r"(?:[\s\S]*)Starting server on port: '(\d+)'", timeout=30)[1].decode()
  44. logging.info('Got IP : {}'.format(got_ip))
  45. logging.info('Got Port : {}'.format(got_port))
  46. # Expected Logs
  47. dut.expect('Registering URI handlers', timeout=30)
  48. # Run test script
  49. conn = client.start_session(got_ip, got_port)
  50. visitor = 0
  51. adder = 0
  52. # Test PUT request and initialize session context
  53. num = random.randint(0,100)
  54. client.putreq(conn, '/adder', str(num))
  55. visitor += 1
  56. dut.expect('/adder visitor count = ' + str(visitor), timeout=30)
  57. dut.expect('/adder PUT handler read ' + str(num), timeout=30)
  58. dut.expect('PUT allocating new session', timeout=30)
  59. # Retest PUT request and change session context value
  60. num = random.randint(0,100)
  61. logging.info('Adding: {}'.format(num))
  62. client.putreq(conn, '/adder', str(num))
  63. visitor += 1
  64. adder += num
  65. dut.expect('/adder visitor count = ' + str(visitor), timeout=30)
  66. dut.expect('/adder PUT handler read ' + str(num), timeout=30)
  67. try:
  68. # Re allocation shouldn't happen
  69. dut.expect('PUT allocating new session', timeout=30)
  70. # Not expected
  71. raise RuntimeError
  72. except Exception:
  73. # As expected
  74. pass
  75. # Test POST request and session persistence
  76. random_nums = [random.randint(0,100) for _ in range(100)]
  77. for num in random_nums:
  78. logging.info('Adding: {}'.format(num))
  79. client.postreq(conn, '/adder', str(num))
  80. visitor += 1
  81. adder += num
  82. dut.expect('/adder visitor count = ' + str(visitor), timeout=30)
  83. dut.expect('/adder handler read ' + str(num), timeout=30)
  84. # Test GET request and session persistence
  85. logging.info('Matching final sum: {}'.format(adder))
  86. if client.getreq(conn, '/adder').decode() != str(adder):
  87. raise RuntimeError
  88. visitor += 1
  89. dut.expect('/adder visitor count = ' + str(visitor), timeout=30)
  90. dut.expect('/adder GET handler send ' + str(adder), timeout=30)
  91. logging.info('Ending session')
  92. # Close connection and check for invocation of context "Free" function
  93. client.end_session(conn)
  94. dut.expect('/adder Free Context function called', timeout=30)
  95. logging.info('Validating user context data')
  96. # Start another session to check user context data
  97. client.start_session(got_ip, got_port)
  98. num = random.randint(0,100)
  99. client.putreq(conn, '/adder', str(num))
  100. visitor += 1
  101. dut.expect('/adder visitor count = ' + str(visitor), timeout=30)
  102. dut.expect('/adder PUT handler read ' + str(num), timeout=30)
  103. dut.expect('PUT allocating new session', timeout=30)
  104. client.end_session(conn)
  105. dut.expect('/adder Free Context function called', timeout=30)