Skip to content

Commit 25a01ce

Browse files
sampan-s-nayaksampan
andauthored
[core] fix authentication token related tests in windows (#58444)
fix a typo (missing semicolon) in authentication_token_loader_test and use cross env compatible `ray::UnsetEnv()` and `ray::setEnv()` in tests --------- Signed-off-by: sampan <[email protected]> Co-authored-by: sampan <[email protected]>
1 parent 6d22e9e commit 25a01ce

File tree

9 files changed

+28
-30
lines changed

9 files changed

+28
-30
lines changed

src/ray/pubsub/tests/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ ray_cc_test(
5454
"//src/ray/rpc:grpc_server",
5555
"//src/ray/rpc/authentication:authentication_token",
5656
"//src/ray/rpc/authentication:authentication_token_loader",
57+
"//src/ray/util:env",
5758
"@com_google_googletest//:gtest_main",
5859
],
5960
)

src/ray/pubsub/tests/python_gcs_subscriber_auth_test.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "ray/rpc/authentication/authentication_token.h"
2525
#include "ray/rpc/authentication/authentication_token_loader.h"
2626
#include "ray/rpc/grpc_server.h"
27+
#include "ray/util/env.h"
2728
#include "src/ray/protobuf/gcs_service.grpc.pb.h"
2829

2930
namespace ray {
@@ -95,7 +96,7 @@ class PythonGcsSubscriberAuthTest : public ::testing::Test {
9596
server_->Shutdown();
9697
server_.reset();
9798
}
98-
unsetenv("RAY_AUTH_TOKEN");
99+
ray::UnsetEnv("RAY_AUTH_TOKEN");
99100
// Reset to default auth mode
100101
RayConfig::instance().initialize(R"({"auth_mode": "disabled"})");
101102
rpc::AuthenticationTokenLoader::instance().ResetCache();
@@ -135,10 +136,10 @@ class PythonGcsSubscriberAuthTest : public ::testing::Test {
135136
// Set client authentication token via environment variable
136137
void SetClientToken(const std::string &client_token) {
137138
if (!client_token.empty()) {
138-
setenv("RAY_AUTH_TOKEN", client_token.c_str(), 1);
139+
ray::SetEnv("RAY_AUTH_TOKEN", client_token);
139140
RayConfig::instance().initialize(R"({"auth_mode": "token"})");
140141
} else {
141-
unsetenv("RAY_AUTH_TOKEN");
142+
ray::UnsetEnv("RAY_AUTH_TOKEN");
142143
RayConfig::instance().initialize(R"({"auth_mode": "disabled"})");
143144
}
144145
rpc::AuthenticationTokenLoader::instance().ResetCache();

src/ray/ray_syncer/tests/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ ray_cc_test(
1414
"//src/ray/ray_syncer",
1515
"//src/ray/rpc:grpc_server",
1616
"//src/ray/rpc/authentication:authentication_token",
17+
"//src/ray/util:env",
1718
"//src/ray/util:network_util",
1819
"//src/ray/util:path_utils",
1920
"//src/ray/util:raii",

src/ray/ray_syncer/tests/ray_syncer_test.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "ray/ray_syncer/ray_syncer_server.h"
4040
#include "ray/rpc/authentication/authentication_token.h"
4141
#include "ray/rpc/grpc_server.h"
42+
#include "ray/util/env.h"
4243
#include "ray/util/network_util.h"
4344
#include "ray/util/path_utils.h"
4445
#include "ray/util/raii.h"
@@ -993,13 +994,13 @@ class SyncerAuthenticationTest : public ::testing::Test {
993994
protected:
994995
void SetUp() override {
995996
// Clear any existing environment variables and reset state
996-
unsetenv("RAY_AUTH_TOKEN");
997+
ray::UnsetEnv("RAY_AUTH_TOKEN");
997998
ray::rpc::AuthenticationTokenLoader::instance().ResetCache();
998999
RayConfig::instance().auth_mode() = "disabled";
9991000
}
10001001

10011002
void TearDown() override {
1002-
unsetenv("RAY_AUTH_TOKEN");
1003+
ray::UnsetEnv("RAY_AUTH_TOKEN");
10031004
ray::rpc::AuthenticationTokenLoader::instance().ResetCache();
10041005
RayConfig::instance().auth_mode() = "disabled";
10051006
}
@@ -1083,7 +1084,7 @@ TEST_F(SyncerAuthenticationTest, MatchingTokens) {
10831084
const std::string test_token = "matching-test-token-12345";
10841085

10851086
// Set client token via environment variable
1086-
setenv("RAY_AUTH_TOKEN", test_token.c_str(), 1);
1087+
ray::SetEnv("RAY_AUTH_TOKEN", test_token);
10871088
// Enable token authentication
10881089
RayConfig::instance().auth_mode() = "token";
10891090
ray::rpc::AuthenticationTokenLoader::instance().ResetCache();
@@ -1110,7 +1111,7 @@ TEST_F(SyncerAuthenticationTest, MismatchedTokens) {
11101111
const std::string client_token = "different-client-token";
11111112

11121113
// Set client token via environment variable
1113-
setenv("RAY_AUTH_TOKEN", client_token.c_str(), 1);
1114+
ray::SetEnv("RAY_AUTH_TOKEN", client_token);
11141115
// Enable token authentication
11151116
RayConfig::instance().auth_mode() = "token";
11161117
ray::rpc::AuthenticationTokenLoader::instance().ResetCache();
@@ -1136,7 +1137,7 @@ TEST_F(SyncerAuthenticationTest, ServerHasTokenClientDoesNot) {
11361137
const std::string server_token = "server-token-12345";
11371138

11381139
// Client has no token - auth mode is disabled (default from SetUp)
1139-
unsetenv("RAY_AUTH_TOKEN");
1140+
ray::UnsetEnv("RAY_AUTH_TOKEN");
11401141
ray::rpc::AuthenticationTokenLoader::instance().ResetCache();
11411142

11421143
// Create authenticated server
@@ -1161,7 +1162,7 @@ TEST_F(SyncerAuthenticationTest, ClientHasTokenServerDoesNotRequire) {
11611162
const std::string client_token = "different-client-token";
11621163

11631164
// Set client token
1164-
setenv("RAY_AUTH_TOKEN", client_token.c_str(), 1);
1165+
ray::SetEnv("RAY_AUTH_TOKEN", client_token);
11651166
// Enable token authentication
11661167
RayConfig::instance().auth_mode() = "token";
11671168
ray::rpc::AuthenticationTokenLoader::instance().ResetCache();

src/ray/raylet/tests/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ ray_cc_test(
9191
"//src/ray/common:id",
9292
"//src/ray/protobuf:runtime_env_agent_cc_proto",
9393
"//src/ray/raylet:runtime_env_agent_client",
94+
"//src/ray/util:env",
9495
"@boost//:asio",
9596
"@boost//:beast",
9697
"@boost//:thread",

src/ray/raylet/tests/runtime_env_agent_client_test.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "ray/common/id.h"
3434
#include "ray/common/ray_config.h"
3535
#include "ray/rpc/authentication/authentication_token_loader.h"
36+
#include "ray/util/env.h"
3637
#include "src/ray/protobuf/runtime_env_agent.pb.h"
3738

3839
namespace ray {
@@ -194,7 +195,7 @@ auto dummy_shutdown_raylet_gracefully = [](const rpc::NodeDeathInfo &) {};
194195

195196
TEST(RuntimeEnvAgentClientTest, GetOrCreateRuntimeEnvOK) {
196197
RayConfig::instance().initialize(R"({"auth_mode": "disabled"})");
197-
unsetenv("RAY_AUTH_TOKEN");
198+
ray::UnsetEnv("RAY_AUTH_TOKEN");
198199
rpc::AuthenticationTokenLoader::instance().ResetCache();
199200

200201
int port = GetFreePort();
@@ -366,7 +367,7 @@ TEST(RuntimeEnvAgentClientTest, GetOrCreateRuntimeEnvRetriesOnServerNotStarted)
366367

367368
TEST(RuntimeEnvAgentClientTest, AttachesAuthHeaderWhenEnabled) {
368369
RayConfig::instance().initialize(R"({"auth_mode": "token"})");
369-
setenv("RAY_AUTH_TOKEN", "header_token", 1);
370+
ray::SetEnv("RAY_AUTH_TOKEN", "header_token");
370371
rpc::AuthenticationTokenLoader::instance().ResetCache();
371372

372373
int port = GetFreePort();
@@ -428,7 +429,7 @@ TEST(RuntimeEnvAgentClientTest, AttachesAuthHeaderWhenEnabled) {
428429
ASSERT_EQ(observed_auth_header, "Bearer header_token");
429430

430431
RayConfig::instance().initialize(R"({"auth_mode": "disabled"})");
431-
unsetenv("RAY_AUTH_TOKEN");
432+
ray::UnsetEnv("RAY_AUTH_TOKEN");
432433
rpc::AuthenticationTokenLoader::instance().ResetCache();
433434
}
434435

src/ray/rpc/tests/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ ray_cc_test(
5252
"//src/ray/rpc:grpc_client",
5353
"//src/ray/rpc:grpc_server",
5454
"//src/ray/rpc/authentication:authentication_token_loader",
55+
"//src/ray/util:env",
5556
"@com_google_googletest//:gtest_main",
5657
],
5758
)
@@ -79,6 +80,7 @@ ray_cc_test(
7980
deps = [
8081
"//src/ray/common:ray_config",
8182
"//src/ray/rpc/authentication:authentication_token_loader",
83+
"//src/ray/util:env",
8284
"@com_google_googletest//:gtest_main",
8385
],
8486
)

src/ray/rpc/tests/authentication_token_loader_test.cc

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "gtest/gtest.h"
2121
#include "ray/common/ray_config.h"
22+
#include "ray/util/env.h"
2223

2324
#if defined(__APPLE__) || defined(__linux__)
2425
#include <sys/stat.h>
@@ -107,21 +108,9 @@ class AuthenticationTokenLoaderTest : public ::testing::Test {
107108
#endif
108109
}
109110

110-
void set_env_var(const char *name, const char *value) {
111-
#ifdef _WIN32
112-
_putenv_s(name, value);
113-
#else
114-
setenv(name, value, 1);
115-
#endif
116-
}
111+
void set_env_var(const char *name, const char *value) { ray::SetEnv(name, value); }
117112

118-
void unset_env_var(const char *name) {
119-
#ifdef _WIN32
120-
_putenv_s(name, "")
121-
#else
122-
unsetenv(name);
123-
#endif
124-
}
113+
void unset_env_var(const char *name) { ray::UnsetEnv(name); }
125114

126115
void ensure_ray_dir_exists() {
127116
#ifdef _WIN32

src/ray/rpc/tests/grpc_auth_token_tests.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "ray/rpc/grpc_client.h"
2424
#include "ray/rpc/grpc_server.h"
2525
#include "ray/rpc/tests/grpc_test_common.h"
26+
#include "ray/util/env.h"
2627
#include "src/ray/protobuf/test_service.grpc.pb.h"
2728

2829
namespace ray {
@@ -42,11 +43,11 @@ class TestGrpcServerClientTokenAuthFixture : public ::testing::Test {
4243
// Set client token in environment for ClientCallManager to read from
4344
// AuthenticationTokenLoader
4445
if (!client_token.empty()) {
45-
setenv("RAY_AUTH_TOKEN", client_token.c_str(), 1);
46+
ray::SetEnv("RAY_AUTH_TOKEN", client_token);
4647
} else {
4748
RayConfig::instance().initialize(R"({"auth_mode": "disabled"})");
4849
AuthenticationTokenLoader::instance().ResetCache();
49-
unsetenv("RAY_AUTH_TOKEN");
50+
ray::UnsetEnv("RAY_AUTH_TOKEN");
5051
}
5152

5253
// Start client thread FIRST
@@ -115,8 +116,8 @@ class TestGrpcServerClientTokenAuthFixture : public ::testing::Test {
115116
}
116117

117118
// Clean up environment variables
118-
unsetenv("RAY_AUTH_TOKEN");
119-
unsetenv("RAY_AUTH_TOKEN_PATH");
119+
ray::UnsetEnv("RAY_AUTH_TOKEN");
120+
ray::UnsetEnv("RAY_AUTH_TOKEN_PATH");
120121
// Reset the token loader for test isolation
121122
AuthenticationTokenLoader::instance().ResetCache();
122123
}

0 commit comments

Comments
 (0)