|
| 1 | +import pytest |
| 2 | +from ray._private.label_utils import ( |
| 3 | + parse_node_labels_string, |
| 4 | + parse_node_labels_from_yaml_file, |
| 5 | + validate_node_labels, |
| 6 | +) |
| 7 | +import sys |
| 8 | +import tempfile |
| 9 | + |
| 10 | + |
| 11 | +def test_parse_node_labels_from_string(): |
| 12 | + # Empty label argument passed |
| 13 | + labels_string = "" |
| 14 | + labels_dict = parse_node_labels_string(labels_string) |
| 15 | + assert labels_dict == {} |
| 16 | + |
| 17 | + # Valid label key with empty value |
| 18 | + labels_string = "region=" |
| 19 | + labels_dict = parse_node_labels_string(labels_string) |
| 20 | + assert labels_dict == {"region": ""} |
| 21 | + |
| 22 | + # Multiple valid label keys and values |
| 23 | + labels_string = "ray.io/accelerator-type=A100,region=us-west4" |
| 24 | + labels_dict = parse_node_labels_string(labels_string) |
| 25 | + assert labels_dict == {"ray.io/accelerator-type": "A100", "region": "us-west4"} |
| 26 | + |
| 27 | + # Invalid label |
| 28 | + labels_string = "ray.io/accelerator-type=type=A100" |
| 29 | + with pytest.raises(ValueError) as e: |
| 30 | + parse_node_labels_string(labels_string) |
| 31 | + assert "Label value is not a key-value pair" in str(e) |
| 32 | + |
| 33 | + |
| 34 | +def test_parse_node_labels_from_yaml_file(): |
| 35 | + # Empty/invalid yaml |
| 36 | + with tempfile.NamedTemporaryFile(mode="w+", delete=True) as test_file: |
| 37 | + test_file.write("") |
| 38 | + test_file.flush() # Ensure data is written |
| 39 | + with pytest.raises(ValueError) as e: |
| 40 | + parse_node_labels_from_yaml_file(test_file.name) |
| 41 | + assert "The format after deserialization is not a key-value pair map" in str(e) |
| 42 | + |
| 43 | + # With non-existent yaml file |
| 44 | + with pytest.raises(FileNotFoundError): |
| 45 | + parse_node_labels_from_yaml_file("missing-file.yaml") |
| 46 | + |
| 47 | + # Valid label key with empty value |
| 48 | + with tempfile.NamedTemporaryFile(mode="w+", delete=True) as test_file: |
| 49 | + test_file.write('"ray.io/accelerator-type": ""') |
| 50 | + test_file.flush() # Ensure data is written |
| 51 | + labels_dict = parse_node_labels_from_yaml_file(test_file.name) |
| 52 | + assert labels_dict == {"ray.io/accelerator-type": ""} |
| 53 | + |
| 54 | + # Multiple valid label keys and values |
| 55 | + with tempfile.NamedTemporaryFile(mode="w+", delete=True) as test_file: |
| 56 | + test_file.write( |
| 57 | + '"ray.io/accelerator-type": "A100"\n"region": "us"\n"market-type": "spot"' |
| 58 | + ) |
| 59 | + test_file.flush() # Ensure data is written |
| 60 | + labels_dict = parse_node_labels_from_yaml_file(test_file.name) |
| 61 | + assert labels_dict == { |
| 62 | + "ray.io/accelerator-type": "A100", |
| 63 | + "region": "us", |
| 64 | + "market-type": "spot", |
| 65 | + } |
| 66 | + |
| 67 | + # Non-string label key |
| 68 | + with tempfile.NamedTemporaryFile(mode="w+", delete=True) as test_file: |
| 69 | + test_file.write('{100: "A100"}') |
| 70 | + test_file.flush() # Ensure data is written |
| 71 | + with pytest.raises(ValueError) as e: |
| 72 | + parse_node_labels_from_yaml_file(test_file.name) |
| 73 | + assert "The key is not string type." in str(e) |
| 74 | + |
| 75 | + # Non-string label value |
| 76 | + with tempfile.NamedTemporaryFile(mode="w+", delete=True) as test_file: |
| 77 | + test_file.write('{"gpu": 100}') |
| 78 | + test_file.flush() # Ensure data is written |
| 79 | + with pytest.raises(ValueError) as e: |
| 80 | + parse_node_labels_from_yaml_file(test_file.name) |
| 81 | + assert 'The value of "gpu" is not string type' in str(e) |
| 82 | + |
| 83 | + |
| 84 | +def test_validate_node_labels(): |
| 85 | + # Custom label starts with ray.io prefix |
| 86 | + labels_dict = {"ray.io/accelerator-type": "A100"} |
| 87 | + with pytest.raises(ValueError) as e: |
| 88 | + validate_node_labels(labels_dict) |
| 89 | + assert "This is reserved for Ray defined labels." in str(e) |
| 90 | + |
| 91 | + # Invalid key prefix syntax |
| 92 | + labels_dict = {"!invalidPrefix/accelerator-type": "A100"} |
| 93 | + with pytest.raises(ValueError) as e: |
| 94 | + validate_node_labels(labels_dict) |
| 95 | + assert "Invalid label key prefix" in str(e) |
| 96 | + |
| 97 | + # Invalid key name syntax |
| 98 | + labels_dict = {"!!accelerator-type?": "A100"} |
| 99 | + with pytest.raises(ValueError) as e: |
| 100 | + validate_node_labels(labels_dict) |
| 101 | + assert "Invalid label key name" in str(e) |
| 102 | + |
| 103 | + # Invalid key value syntax |
| 104 | + labels_dict = {"accelerator-type": "??"} |
| 105 | + with pytest.raises(ValueError) as e: |
| 106 | + validate_node_labels(labels_dict) |
| 107 | + assert "Invalid label key value" in str(e) |
| 108 | + |
| 109 | + # Valid node label |
| 110 | + labels_dict = {"accelerator-type": "A100"} |
| 111 | + validate_node_labels(labels_dict) |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + import os |
| 116 | + |
| 117 | + # Skip test_basic_2_client_mode for now- the test suite is breaking. |
| 118 | + if os.environ.get("PARALLEL_CI"): |
| 119 | + sys.exit(pytest.main(["-n", "auto", "--boxed", "-vs", __file__])) |
| 120 | + else: |
| 121 | + sys.exit(pytest.main(["-sv", __file__])) |
0 commit comments