You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Test debugger server utils."""
  16. import json
  17. import os
  18. import time
  19. from tests.st.func.debugger.conftest import DEBUGGER_EXPECTED_RESULTS, DEBUGGER_BASE_URL
  20. from tests.utils.tools import compare_result_with_file, get_url
  21. def check_waiting_state(app_client):
  22. """Wait for waiting state."""
  23. url = 'retrieve'
  24. body_data = {'mode': 'all'}
  25. max_try_times = 30
  26. count = 0
  27. flag = False
  28. while count < max_try_times:
  29. res = get_request_result(app_client, url, body_data)
  30. state = res.get('metadata', {}).get('state')
  31. if state == 'waiting':
  32. flag = True
  33. break
  34. count += 1
  35. time.sleep(0.1)
  36. assert flag is True
  37. def get_request_result(app_client, url, body_data, method='post', expect_code=200, full_url=False):
  38. """Get request result."""
  39. if not full_url:
  40. real_url = os.path.join(DEBUGGER_BASE_URL, url)
  41. else:
  42. real_url = url
  43. if method == 'post':
  44. response = app_client.post(real_url, data=json.dumps(body_data))
  45. else:
  46. real_url = get_url(real_url, body_data)
  47. response = app_client.get(real_url)
  48. assert response.status_code == expect_code
  49. res = response.get_json()
  50. return res
  51. def send_and_compare_result(app_client, url, body_data, expect_file=None, method='post', full_url=False):
  52. """Send and compare result."""
  53. res = get_request_result(app_client, url, body_data, method=method, full_url=full_url)
  54. delete_random_items(res)
  55. if expect_file:
  56. real_path = os.path.join(DEBUGGER_EXPECTED_RESULTS, 'restful_results', expect_file)
  57. compare_result_with_file(res, real_path)
  58. def send_and_save_result(app_client, url, body_data, file_path, method='post'):
  59. """Send and save result."""
  60. res = get_request_result(app_client, url, body_data, method=method)
  61. delete_random_items(res)
  62. real_path = os.path.join(DEBUGGER_EXPECTED_RESULTS, 'restful_results', file_path)
  63. json.dump(res, open(real_path, 'w'))
  64. def delete_random_items(res):
  65. """delete the random items in metadata."""
  66. if res.get('metadata'):
  67. if res['metadata'].get('ip'):
  68. res['metadata'].pop('ip')
  69. if res['metadata'].get('pos'):
  70. res['metadata'].pop('pos')