test_anyscale_job_wrapper.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import pytest
  2. import sys
  3. import json
  4. from ray_release.command_runner._anyscale_job_wrapper import (
  5. main,
  6. run_bash_command,
  7. TIMEOUT_RETURN_CODE,
  8. OUTPUT_JSON_FILENAME,
  9. )
  10. cloud_storage_kwargs = dict(
  11. results_cloud_storage_uri=None,
  12. metrics_cloud_storage_uri=None,
  13. output_cloud_storage_uri=None,
  14. upload_cloud_storage_uri=None,
  15. artifact_path=None,
  16. )
  17. def test_run_bash_command_success():
  18. assert run_bash_command("exit 0", 1000) == 0
  19. def test_run_bash_command_fail():
  20. assert run_bash_command("exit 1", 1000) == 1
  21. def test_run_bash_command_timeout():
  22. assert run_bash_command("sleep 10", 1) == TIMEOUT_RETURN_CODE
  23. def _check_output_json(expected_return_code, prepare_return_codes=None):
  24. with open(OUTPUT_JSON_FILENAME, "r") as fp:
  25. output = json.load(fp)
  26. assert output["return_code"] == expected_return_code
  27. assert output["prepare_return_codes"] == (prepare_return_codes or [])
  28. assert output["uploaded_results"] is False
  29. assert output["collected_metrics"] is False
  30. assert output["uploaded_metrics"] is False
  31. def test_prepare_commands_validation(tmpdir):
  32. with pytest.raises(ValueError):
  33. main(
  34. test_workload="exit 0",
  35. test_workload_timeout=10,
  36. test_no_raise_on_timeout=False,
  37. prepare_commands=["exit 0"],
  38. prepare_commands_timeouts=[],
  39. **cloud_storage_kwargs
  40. )
  41. with pytest.raises(ValueError):
  42. main(
  43. test_workload="exit 0",
  44. test_workload_timeout=10,
  45. test_no_raise_on_timeout=False,
  46. prepare_commands=[],
  47. prepare_commands_timeouts=[1],
  48. **cloud_storage_kwargs
  49. )
  50. def test_end_to_end(tmpdir):
  51. expected_return_code = 0
  52. assert (
  53. main(
  54. test_workload="exit 0",
  55. test_workload_timeout=10,
  56. test_no_raise_on_timeout=False,
  57. prepare_commands=[],
  58. prepare_commands_timeouts=[],
  59. **cloud_storage_kwargs
  60. )
  61. == expected_return_code
  62. )
  63. _check_output_json(expected_return_code)
  64. def test_end_to_end_prepare_commands(tmpdir):
  65. expected_return_code = 0
  66. assert (
  67. main(
  68. test_workload="exit 0",
  69. test_workload_timeout=10,
  70. test_no_raise_on_timeout=False,
  71. prepare_commands=["exit 0", "exit 0"],
  72. prepare_commands_timeouts=[1, 1],
  73. **cloud_storage_kwargs
  74. )
  75. == expected_return_code
  76. )
  77. _check_output_json(expected_return_code, [0, 0])
  78. def test_end_to_end_long_running(tmpdir):
  79. expected_return_code = 0
  80. assert (
  81. main(
  82. test_workload="sleep 10",
  83. test_workload_timeout=1,
  84. test_no_raise_on_timeout=True,
  85. prepare_commands=[],
  86. prepare_commands_timeouts=[],
  87. **cloud_storage_kwargs
  88. )
  89. == expected_return_code
  90. )
  91. _check_output_json(TIMEOUT_RETURN_CODE)
  92. def test_end_to_end_timeout(tmpdir):
  93. expected_return_code = TIMEOUT_RETURN_CODE
  94. assert (
  95. main(
  96. test_workload="sleep 10",
  97. test_workload_timeout=1,
  98. test_no_raise_on_timeout=False,
  99. prepare_commands=[],
  100. prepare_commands_timeouts=[],
  101. **cloud_storage_kwargs
  102. )
  103. == expected_return_code
  104. )
  105. _check_output_json(expected_return_code)
  106. def test_end_to_end_prepare_timeout(tmpdir):
  107. expected_return_code = 1
  108. assert (
  109. main(
  110. test_workload="exit 0",
  111. test_workload_timeout=10,
  112. test_no_raise_on_timeout=False,
  113. prepare_commands=["exit 0", "sleep 10"],
  114. prepare_commands_timeouts=[1, 1],
  115. **cloud_storage_kwargs
  116. )
  117. == expected_return_code
  118. )
  119. _check_output_json(None, [0, TIMEOUT_RETURN_CODE])
  120. @pytest.mark.parametrize("long_running", (True, False))
  121. def test_end_to_end_failure(tmpdir, long_running):
  122. expected_return_code = 1
  123. assert (
  124. main(
  125. test_workload="exit 1",
  126. test_workload_timeout=1,
  127. test_no_raise_on_timeout=long_running,
  128. prepare_commands=[],
  129. prepare_commands_timeouts=[],
  130. **cloud_storage_kwargs
  131. )
  132. == expected_return_code
  133. )
  134. _check_output_json(expected_return_code)
  135. def test_end_to_end_prepare_failure(tmpdir):
  136. expected_return_code = 1
  137. assert (
  138. main(
  139. test_workload="exit 0",
  140. test_workload_timeout=10,
  141. test_no_raise_on_timeout=False,
  142. prepare_commands=["exit 0", "exit 1"],
  143. prepare_commands_timeouts=[1, 1],
  144. **cloud_storage_kwargs
  145. )
  146. == expected_return_code
  147. )
  148. _check_output_json(None, [0, 1])
  149. if __name__ == "__main__":
  150. sys.exit(pytest.main(["-v", __file__]))