2021-11-13 22:54:01 -08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from ray.dashboard.modules.job.common import (
|
|
|
|
http_uri_components_to_uri,
|
|
|
|
uri_to_http_components,
|
|
|
|
validate_request_type,
|
|
|
|
JobSubmitRequest,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class TestJobSubmitRequestValidation:
|
|
|
|
def test_validate_entrypoint(self):
|
|
|
|
r = validate_request_type({"entrypoint": "abc"}, JobSubmitRequest)
|
|
|
|
assert r.entrypoint == "abc"
|
|
|
|
|
|
|
|
with pytest.raises(TypeError, match="required positional argument"):
|
|
|
|
validate_request_type({}, JobSubmitRequest)
|
|
|
|
|
|
|
|
with pytest.raises(TypeError, match="must be a string"):
|
|
|
|
validate_request_type({"entrypoint": 123}, JobSubmitRequest)
|
|
|
|
|
|
|
|
def test_validate_job_id(self):
|
|
|
|
r = validate_request_type({"entrypoint": "abc"}, JobSubmitRequest)
|
|
|
|
assert r.entrypoint == "abc"
|
|
|
|
assert r.job_id is None
|
|
|
|
|
2022-01-29 18:41:57 -08:00
|
|
|
r = validate_request_type(
|
|
|
|
{"entrypoint": "abc", "job_id": "123"}, JobSubmitRequest
|
|
|
|
)
|
2021-11-13 22:54:01 -08:00
|
|
|
assert r.entrypoint == "abc"
|
|
|
|
assert r.job_id == "123"
|
|
|
|
|
|
|
|
with pytest.raises(TypeError, match="must be a string"):
|
2022-01-29 18:41:57 -08:00
|
|
|
validate_request_type({"entrypoint": 123, "job_id": 1}, JobSubmitRequest)
|
2021-11-13 22:54:01 -08:00
|
|
|
|
|
|
|
def test_validate_runtime_env(self):
|
|
|
|
r = validate_request_type({"entrypoint": "abc"}, JobSubmitRequest)
|
|
|
|
assert r.entrypoint == "abc"
|
|
|
|
assert r.runtime_env is None
|
|
|
|
|
2022-01-29 18:41:57 -08:00
|
|
|
r = validate_request_type(
|
|
|
|
{"entrypoint": "abc", "runtime_env": {"hi": "hi2"}}, JobSubmitRequest
|
|
|
|
)
|
2021-11-13 22:54:01 -08:00
|
|
|
assert r.entrypoint == "abc"
|
|
|
|
assert r.runtime_env == {"hi": "hi2"}
|
|
|
|
|
|
|
|
with pytest.raises(TypeError, match="must be a dict"):
|
2022-01-29 18:41:57 -08:00
|
|
|
validate_request_type(
|
|
|
|
{"entrypoint": "abc", "runtime_env": 123}, JobSubmitRequest
|
|
|
|
)
|
2021-11-13 22:54:01 -08:00
|
|
|
|
|
|
|
with pytest.raises(TypeError, match="keys must be strings"):
|
2022-01-29 18:41:57 -08:00
|
|
|
validate_request_type(
|
|
|
|
{"entrypoint": "abc", "runtime_env": {1: "hi"}}, JobSubmitRequest
|
|
|
|
)
|
2021-11-13 22:54:01 -08:00
|
|
|
|
|
|
|
def test_validate_metadata(self):
|
|
|
|
r = validate_request_type({"entrypoint": "abc"}, JobSubmitRequest)
|
|
|
|
assert r.entrypoint == "abc"
|
|
|
|
assert r.metadata is None
|
|
|
|
|
2022-01-29 18:41:57 -08:00
|
|
|
r = validate_request_type(
|
|
|
|
{"entrypoint": "abc", "metadata": {"hi": "hi2"}}, JobSubmitRequest
|
|
|
|
)
|
2021-11-13 22:54:01 -08:00
|
|
|
assert r.entrypoint == "abc"
|
|
|
|
assert r.metadata == {"hi": "hi2"}
|
|
|
|
|
|
|
|
with pytest.raises(TypeError, match="must be a dict"):
|
2022-01-29 18:41:57 -08:00
|
|
|
validate_request_type(
|
|
|
|
{"entrypoint": "abc", "metadata": 123}, JobSubmitRequest
|
|
|
|
)
|
2021-11-13 22:54:01 -08:00
|
|
|
|
|
|
|
with pytest.raises(TypeError, match="keys must be strings"):
|
2022-01-29 18:41:57 -08:00
|
|
|
validate_request_type(
|
|
|
|
{"entrypoint": "abc", "metadata": {1: "hi"}}, JobSubmitRequest
|
|
|
|
)
|
2021-11-13 22:54:01 -08:00
|
|
|
|
|
|
|
with pytest.raises(TypeError, match="values must be strings"):
|
2022-01-29 18:41:57 -08:00
|
|
|
validate_request_type(
|
|
|
|
{"entrypoint": "abc", "metadata": {"hi": 1}}, JobSubmitRequest
|
|
|
|
)
|
2021-11-13 22:54:01 -08:00
|
|
|
|
|
|
|
|
|
|
|
def test_uri_to_http_and_back():
|
|
|
|
assert uri_to_http_components("gcs://hello.zip") == ("gcs", "hello")
|
|
|
|
|
|
|
|
with pytest.raises(ValueError, match="'blah' is not a valid Protocol"):
|
|
|
|
uri_to_http_components("blah://halb.zip")
|
|
|
|
|
|
|
|
with pytest.raises(ValueError, match="does not end in .zip"):
|
|
|
|
assert uri_to_http_components("gcs://hello.not_zip")
|
|
|
|
|
|
|
|
with pytest.raises(ValueError, match="does not end in .zip"):
|
|
|
|
assert uri_to_http_components("gcs://hello")
|
|
|
|
|
|
|
|
assert http_uri_components_to_uri("gcs", "hello") == "gcs://hello.zip"
|
|
|
|
assert http_uri_components_to_uri("blah", "halb") == "blah://halb.zip"
|
|
|
|
|
|
|
|
with pytest.raises(ValueError, match="should not end in .zip"):
|
|
|
|
assert http_uri_components_to_uri("gcs", "hello.zip")
|
|
|
|
|
|
|
|
original_uri = "gcs://hello.zip"
|
|
|
|
new_uri = http_uri_components_to_uri(*uri_to_http_components(original_uri))
|
|
|
|
assert new_uri == original_uri
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import sys
|
2022-01-29 18:41:57 -08:00
|
|
|
|
2021-11-13 22:54:01 -08:00
|
|
|
sys.exit(pytest.main(["-v", __file__]))
|