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.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. while count < max_try_times:
  28. res = get_request_result(app_client, url, body_data)
  29. state = res.get('metadata', {}).get('state')
  30. if state == 'waiting':
  31. return True
  32. count += 1
  33. time.sleep(0.1)
  34. return False
  35. def get_request_result(app_client, url, body_data, method='post', expect_code=200):
  36. """Get request result."""
  37. real_url = os.path.join(DEBUGGER_BASE_URL, url)
  38. if method == 'post':
  39. response = app_client.post(real_url, data=json.dumps(body_data))
  40. else:
  41. real_url = get_url(real_url, body_data)
  42. response = app_client.get(real_url)
  43. assert response.status_code == expect_code
  44. res = response.get_json()
  45. return res
  46. def send_and_compare_result(app_client, url, body_data, expect_file=None, method='post'):
  47. """Send and compare result."""
  48. res = get_request_result(app_client, url, body_data, method=method)
  49. delete_random_items(res)
  50. if expect_file:
  51. real_path = os.path.join(DEBUGGER_EXPECTED_RESULTS, 'restful_results', expect_file)
  52. compare_result_with_file(res, real_path)
  53. def send_and_save_result(app_client, url, body_data, file_path, method='post'):
  54. """Send and save result."""
  55. res = get_request_result(app_client, url, body_data, method=method)
  56. delete_random_items(res)
  57. real_path = os.path.join(DEBUGGER_EXPECTED_RESULTS, 'restful_results', file_path)
  58. json.dump(res, open(real_path, 'w'))
  59. def delete_random_items(res):
  60. """delete the random items in metadata."""
  61. if res.get('metadata'):
  62. if res['metadata'].get('ip'):
  63. res['metadata'].pop('ip')
  64. if res['metadata'].get('pos'):
  65. res['metadata'].pop('pos')