main
py 208 lines 6.95 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 """Tests for vllm.ray.ray_env — env var propagation to Ray workers."""
4
5 import os
6 from unittest.mock import patch
7
8 from vllm.ray.ray_env import get_env_vars_to_copy
9 from vllm.v1.executor.ray_utils import WORKER_SPECIFIC_ENV_VARS
10
11 # ---------------------------------------------------------------------------
12 # Default prefix matching
13 # ---------------------------------------------------------------------------
14
15
16 class TestDefaultPrefixes:
17 """Built-in prefixes (VLLM_, LMCACHE_, NCCL_, UCX_, HF_, HUGGING_FACE_)
18 should be forwarded without any extra configuration."""
19
20 @patch.dict(os.environ, {"LMCACHE_LOCAL_CPU": "True"}, clear=False)
21 def test_lmcache_prefix(self):
22 result = get_env_vars_to_copy()
23 assert "LMCACHE_LOCAL_CPU" in result
24
25 @patch.dict(os.environ, {"NCCL_DEBUG": "INFO"}, clear=False)
26 def test_nccl_prefix(self):
27 result = get_env_vars_to_copy()
28 assert "NCCL_DEBUG" in result
29
30 @patch.dict(os.environ, {"UCX_TLS": "rc"}, clear=False)
31 def test_ucx_prefix(self):
32 result = get_env_vars_to_copy()
33 assert "UCX_TLS" in result
34
35 @patch.dict(os.environ, {"HF_TOKEN": "secret"}, clear=False)
36 def test_hf_token_via_prefix(self):
37 result = get_env_vars_to_copy()
38 assert "HF_TOKEN" in result
39
40 @patch.dict(os.environ, {"HUGGING_FACE_HUB_TOKEN": "secret"}, clear=False)
41 def test_hugging_face_prefix(self):
42 result = get_env_vars_to_copy()
43 assert "HUGGING_FACE_HUB_TOKEN" in result
44
45
46 # ---------------------------------------------------------------------------
47 # Default extra vars
48 # ---------------------------------------------------------------------------
49
50
51 class TestDefaultExtraVars:
52 """Individual vars listed in VLLM_RAY_EXTRA_ENV_VARS_TO_COPY's default."""
53
54 def test_pythonhashseed_in_result(self):
55 """PYTHONHASHSEED should always be in the result set (as a name to
56 copy) regardless of whether it is actually set in os.environ."""
57 result = get_env_vars_to_copy()
58 assert "PYTHONHASHSEED" in result
59
60
61 # ---------------------------------------------------------------------------
62 # User-supplied extensions
63 # ---------------------------------------------------------------------------
64
65
66 class TestUserExtensions:
67 """Users can add prefixes and extra vars at deploy time."""
68
69 @patch.dict(
70 os.environ,
71 {
72 "VLLM_RAY_EXTRA_ENV_VAR_PREFIXES_TO_COPY": "MYLIB_",
73 "MYLIB_FOO": "bar",
74 },
75 clear=False,
76 )
77 def test_user_prefix(self):
78 """User-supplied prefixes are additive — built-in defaults are kept."""
79 result = get_env_vars_to_copy()
80 assert "MYLIB_FOO" in result
81
82 @patch.dict(
83 os.environ,
84 {
85 "VLLM_RAY_EXTRA_ENV_VARS_TO_COPY": "MY_SECRET",
86 "MY_SECRET": "val",
87 },
88 clear=False,
89 )
90 def test_user_extra_var(self):
91 """User-supplied extras are additive — PYTHONHASHSEED still included."""
92 result = get_env_vars_to_copy()
93 assert "MY_SECRET" in result
94 assert "PYTHONHASHSEED" in result
95
96
97 # ---------------------------------------------------------------------------
98 # Exclusion
99 # ---------------------------------------------------------------------------
100
101
102 class TestExclusion:
103 """exclude_vars and RAY_NON_CARRY_OVER_ENV_VARS take precedence."""
104
105 @patch.dict(os.environ, {"CUDA_VISIBLE_DEVICES": "0,1"}, clear=False)
106 def test_exclude_vars(self):
107 result = get_env_vars_to_copy(exclude_vars={"CUDA_VISIBLE_DEVICES"})
108 assert "CUDA_VISIBLE_DEVICES" not in result
109
110 @patch.dict(
111 os.environ,
112 {
113 "VLLM_HOST_IP": "10.0.0.1",
114 "VLLM_NIXL_SIDE_CHANNEL_HOST": "10.0.0.1",
115 },
116 clear=False,
117 )
118 def test_worker_specific_host_vars_are_excluded(self):
119 result = get_env_vars_to_copy(exclude_vars=WORKER_SPECIFIC_ENV_VARS)
120 assert "VLLM_HOST_IP" not in result
121 assert "VLLM_NIXL_SIDE_CHANNEL_HOST" not in result
122
123 @patch.dict(os.environ, {"LMCACHE_LOCAL_CPU": "True"}, clear=False)
124 @patch(
125 "vllm.ray.ray_env.RAY_NON_CARRY_OVER_ENV_VARS",
126 {"LMCACHE_LOCAL_CPU"},
127 )
128 def test_non_carry_over_blacklist(self):
129 result = get_env_vars_to_copy()
130 assert "LMCACHE_LOCAL_CPU" not in result
131
132
133 # ---------------------------------------------------------------------------
134 # additional_vars (platform extension point)
135 # ---------------------------------------------------------------------------
136
137
138 class TestAdditionalVars:
139 """The additional_vars parameter supports platform-specific vars."""
140
141 @patch.dict(os.environ, {"CUSTOM_PLATFORM_VAR": "1"}, clear=False)
142 def test_additional_vars_passthrough(self):
143 result = get_env_vars_to_copy(additional_vars={"CUSTOM_PLATFORM_VAR"})
144 assert "CUSTOM_PLATFORM_VAR" in result
145
146
147 # ---------------------------------------------------------------------------
148 # Edge cases
149 # ---------------------------------------------------------------------------
150
151
152 class TestEdgeCases:
153 """Prefix matching should be strict (startswith, not contains)."""
154
155 @patch.dict(os.environ, {"LMCACH_TYPO": "1"}, clear=False)
156 def test_prefix_no_partial_match(self):
157 """'LMCACH_' does not match the 'LMCACHE_' prefix."""
158 result = get_env_vars_to_copy()
159 assert "LMCACH_TYPO" not in result
160
161 @patch.dict(
162 os.environ,
163 {
164 "VLLM_RAY_EXTRA_ENV_VAR_PREFIXES_TO_COPY": " MYLIB_ , OTHER_ ",
165 },
166 clear=False,
167 )
168 def test_csv_whitespace_handling(self):
169 """Whitespace around commas and tokens should be stripped."""
170 result = get_env_vars_to_copy()
171 # MYLIB_ and OTHER_ should be parsed as valid prefixes — no crash
172 assert isinstance(result, set)
173
174 @patch.dict(
175 os.environ,
176 {
177 "VLLM_RAY_EXTRA_ENV_VAR_PREFIXES_TO_COPY": "MYLIB_",
178 "LMCACHE_BACKEND": "cpu",
179 "NCCL_DEBUG": "INFO",
180 "MYLIB_FOO": "bar",
181 },
182 clear=False,
183 )
184 def test_user_prefix_additive(self):
185 """Setting VLLM_RAY_EXTRA_ENV_VAR_PREFIXES_TO_COPY does NOT drop defaults."""
186 result = get_env_vars_to_copy()
187 # Built-in defaults still present
188 assert "LMCACHE_BACKEND" in result
189 assert "NCCL_DEBUG" in result
190 # User addition also present
191 assert "MYLIB_FOO" in result
192
193 @patch.dict(
194 os.environ,
195 {
196 "VLLM_RAY_EXTRA_ENV_VARS_TO_COPY": "MY_FLAG",
197 "PYTHONHASHSEED": "42",
198 "MY_FLAG": "1",
199 },
200 clear=False,
201 )
202 def test_user_extra_additive(self):
203 """Setting VLLM_RAY_EXTRA_ENV_VARS_TO_COPY does NOT drop defaults."""
204 result = get_env_vars_to_copy()
205 # Built-in default still present
206 assert "PYTHONHASHSEED" in result
207 # User addition also present
208 assert "MY_FLAG" in result