pytest_http_server_persistence.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 pytest_embedded import Dut
  18. # When running on local machine execute the following before running this script
  19. # > make app bootloader
  20. # > make print_flash_cmd | tail -n 1 > build/download.config
  21. @pytest.mark.esp32
  22. @pytest.mark.esp32c3
  23. @pytest.mark.esp32s2
  24. @pytest.mark.esp32s3
  25. @pytest.mark.wifi
  26. def test_examples_protocol_http_server_persistence(dut: Dut) -> None:
  27. # Get binary file
  28. binary_file = os.path.join(dut.app.binary_path, 'persistent_sockets.bin')
  29. bin_size = os.path.getsize(binary_file)
  30. logging.info('http_server_bin_size : {}KB'.format(bin_size // 1024))
  31. # Upload binary and start testing
  32. logging.info('Starting http_server persistance test app')
  33. # Parse IP address of STA
  34. logging.info('Waiting to connect with AP')
  35. got_ip = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)', timeout=30)[1].decode()
  36. got_port = dut.expect(r"(?:[\s\S]*)Starting server on port: '(\d+)'", timeout=30)[1].decode()
  37. logging.info('Got IP : {}'.format(got_ip))
  38. logging.info('Got Port : {}'.format(got_port))
  39. # Expected Logs
  40. dut.expect('Registering URI handlers', timeout=30)
  41. # Run test script
  42. conn = client.start_session(got_ip, got_port)
  43. visitor = 0
  44. adder = 0
  45. # Test PUT request and initialize session context
  46. num = random.randint(0,100)
  47. client.putreq(conn, '/adder', str(num))
  48. visitor += 1
  49. dut.expect('/adder visitor count = ' + str(visitor), timeout=30)
  50. dut.expect('/adder PUT handler read ' + str(num), timeout=30)
  51. dut.expect('PUT allocating new session', timeout=30)
  52. # Retest PUT request and change session context value
  53. num = random.randint(0,100)
  54. logging.info('Adding: {}'.format(num))
  55. client.putreq(conn, '/adder', str(num))
  56. visitor += 1
  57. adder += num
  58. dut.expect('/adder visitor count = ' + str(visitor), timeout=30)
  59. dut.expect('/adder PUT handler read ' + str(num), timeout=30)
  60. try:
  61. # Re allocation shouldn't happen
  62. dut.expect('PUT allocating new session', timeout=30)
  63. # Not expected
  64. raise RuntimeError
  65. except Exception:
  66. # As expected
  67. pass
  68. # Test POST request and session persistence
  69. random_nums = [random.randint(0,100) for _ in range(100)]
  70. for num in random_nums:
  71. logging.info('Adding: {}'.format(num))
  72. client.postreq(conn, '/adder', str(num))
  73. visitor += 1
  74. adder += num
  75. dut.expect('/adder visitor count = ' + str(visitor), timeout=30)
  76. dut.expect('/adder handler read ' + str(num), timeout=30)
  77. # Test GET request and session persistence
  78. logging.info('Matching final sum: {}'.format(adder))
  79. if client.getreq(conn, '/adder').decode() != str(adder):
  80. raise RuntimeError
  81. visitor += 1
  82. dut.expect('/adder visitor count = ' + str(visitor), timeout=30)
  83. dut.expect('/adder GET handler send ' + str(adder), timeout=30)
  84. logging.info('Ending session')
  85. # Close connection and check for invocation of context "Free" function
  86. client.end_session(conn)
  87. dut.expect('/adder Free Context function called', timeout=30)
  88. logging.info('Validating user context data')
  89. # Start another session to check user context data
  90. client.start_session(got_ip, got_port)
  91. num = random.randint(0,100)
  92. client.putreq(conn, '/adder', str(num))
  93. visitor += 1
  94. dut.expect('/adder visitor count = ' + str(visitor), timeout=30)
  95. dut.expect('/adder PUT handler read ' + str(num), timeout=30)
  96. dut.expect('PUT allocating new session', timeout=30)
  97. client.end_session(conn)
  98. dut.expect('/adder Free Context function called', timeout=30)