pytest_http_server_persistence.py 4.4 KB

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