-
Notifications
You must be signed in to change notification settings - Fork 85
Fix the S3 endpoint constructor ambiguity #700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4a7afae
36a6f3c
f4e4ee4
422b48d
c444523
44db239
146fd5e
fce796e
59f159f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| * Copyright (c) 2025, NVIDIA CORPORATION. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include <gtest/gtest.h> | ||
| #include <kvikio/remote_handle.hpp> | ||
|
|
||
| #include <unordered_map> | ||
| #include "utils/env.hpp" | ||
|
|
||
| TEST(RemoteHandleTest, s3_endpoint_constructor) | ||
| { | ||
| kvikio::test::EnvVarContext env_var_ctx{{{"AWS_DEFAULT_REGION", "my_aws_default_region"}, | ||
| {"AWS_ACCESS_KEY_ID", "my_aws_access_key_id"}, | ||
| {"AWS_SECRET_ACCESS_KEY", "my_aws_secrete_access_key"}, | ||
| {"AWS_ENDPOINT_URL", "https://my_aws_endpoint_url"}}}; | ||
| std::string url = "https://my_aws_endpoint_url/bucket_name/object_name"; | ||
| std::string aws_region = "my_aws_region"; | ||
| // Use the overload where the full url and the optional aws_region are specified. | ||
| kvikio::S3Endpoint s1(url, aws_region); | ||
|
|
||
| std::string bucket_name = "bucket_name"; | ||
| std::string object_name = "object_name"; | ||
| // Use the other overload where the bucket and object names are specified. | ||
| kvikio::S3Endpoint s2(std::make_pair(bucket_name, object_name)); | ||
|
|
||
| EXPECT_EQ(s1.str(), s2.str()); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * Copyright (c) 2025, NVIDIA CORPORATION. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include "env.hpp" | ||
| #include <cstdlib> | ||
| #include <sstream> | ||
| #include <utility> | ||
|
|
||
| #include <kvikio/error.hpp> | ||
|
|
||
| namespace kvikio::test { | ||
| EnvVarContext::EnvVarContext( | ||
| std::initializer_list<std::pair<std::string, std::string>> env_var_entries) | ||
| { | ||
| for (auto const& [key, current_value] : env_var_entries) { | ||
| EnvVarState env_var_state; | ||
| if (auto const res = std::getenv(key.c_str()); res != nullptr) { | ||
| env_var_state.existed_before = true; | ||
| env_var_state.previous_value = res; | ||
| } | ||
| SYSCALL_CHECK(setenv(key.c_str(), current_value.c_str(), 1 /* allow overwrite */)); | ||
| if (_env_var_map.find(key) != _env_var_map.end()) { | ||
| std::stringstream ss; | ||
| ss << "Environment variable " << key << " has already been set in this context."; | ||
| KVIKIO_FAIL(ss.str()); | ||
| } | ||
| _env_var_map.insert({key, std::move(env_var_state)}); | ||
| } | ||
| } | ||
|
|
||
| EnvVarContext::~EnvVarContext() | ||
| { | ||
| for (auto const& [key, state] : _env_var_map) { | ||
| if (state.existed_before) { | ||
| SYSCALL_CHECK(setenv(key.c_str(), state.previous_value.c_str(), 1 /* allow overwrite */)); | ||
| } else { | ||
| SYSCALL_CHECK(unsetenv(key.c_str())); | ||
| } | ||
| } | ||
| } | ||
| } // namespace kvikio::test | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * Copyright (c) 2025, NVIDIA CORPORATION. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <initializer_list> | ||
| #include <string> | ||
| #include <unordered_map> | ||
| #include <utility> | ||
|
|
||
| namespace kvikio::test { | ||
| /** | ||
| * @brief RAII class used to temporarily set environment variables to new values upon construction, | ||
| * and restore them to previous values upon destruction. | ||
| */ | ||
| class EnvVarContext { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. neat! I might borrow this for cudf compression tests, they are getting to a point where more than one env var is being set
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps in a future PR this class could be moved out of the test and become part of KvikIO source. Same for the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO it's fine in tests, it's not a part of the functionality of the kvikIO library |
||
| private: | ||
| /** | ||
| * @brief The state of an environment variable | ||
| */ | ||
| struct EnvVarState { | ||
| // Whether the environment variable existed before entering the context | ||
| bool existed_before{}; | ||
| // The previous value of the environment variable, if existed | ||
| std::string previous_value{}; | ||
| }; | ||
|
|
||
| public: | ||
| /** | ||
| * @brief Set the environment variables to new values | ||
| * | ||
| * @param env_var_entries User-specified environment variables. Each entry includes the variable | ||
| * name and value. | ||
| */ | ||
| EnvVarContext(std::initializer_list<std::pair<std::string, std::string>> env_var_entries); | ||
|
|
||
| /** | ||
| * @brief Restore the environment variables to previous values | ||
| * | ||
| * Reset the environment variables to their previous states: | ||
| * - If one existed before, restore to its previous value. | ||
| * - Otherwise, remove it from the environment. | ||
| */ | ||
| ~EnvVarContext(); | ||
|
|
||
| EnvVarContext(EnvVarContext const&) = delete; | ||
| EnvVarContext(EnvVarContext&&) = delete; | ||
| EnvVarContext& operator=(EnvVarContext const&) = delete; | ||
| EnvVarContext& operator=(EnvVarContext&&) = delete; | ||
|
|
||
| private: | ||
| std::unordered_map<std::string, EnvVarState> _env_var_map; | ||
vuule marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
| } // namespace kvikio::test | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you know we'd ever need to unset an env var instead of setting to the specified value?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking of CUDF_BENCHMARK_DROP_CACHE. No matter what this env var's value is, as long as it exists, the page cache is to be dropped (i.e.
CUDF_BENCHMARK_DROP_CACHE=<nothing>/true/false/123all have the same effect). In this case to fully revert to a previous state, we need to unset the env var.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not referring to reverting the state, but the actual test state requiring an env var to be unset.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I think a thorough test of S3 endpoint would need this feature. The constructor of S3 endpoint has "optional" parameters, but if they are neither passed as argument or set via the env var, an exception will be thrown. We may want to check if the constructor can throw properly when some arguments are missing in one test, and then check when another combination of arguments are missing in another test. This is where env var context could help.