/proc/self/cwd/test/test_common/environment.cc
Line | Count | Source (jump to first uncovered line) |
1 | | #include "test/test_common/environment.h" |
2 | | |
3 | | #include <fstream> |
4 | | #include <iostream> |
5 | | #include <regex> |
6 | | #include <sstream> |
7 | | #include <string> |
8 | | #include <vector> |
9 | | |
10 | | #include "envoy/common/platform.h" |
11 | | |
12 | | #include "source/common/common/assert.h" |
13 | | #include "source/common/common/compiler_requirements.h" |
14 | | #include "source/common/common/logger.h" |
15 | | #include "source/common/common/macros.h" |
16 | | #include "source/common/common/utility.h" |
17 | | #include "source/common/filesystem/directory.h" |
18 | | |
19 | | #include "absl/container/node_hash_map.h" |
20 | | |
21 | | #ifdef ENVOY_HANDLE_SIGNALS |
22 | | #include "source/common/signal/signal_action.h" |
23 | | #endif |
24 | | |
25 | | #ifdef ENVOY_DISABLE_EXCEPTIONS |
26 | | #include "source/server/options_impl_base.h" |
27 | | #else |
28 | | #include "source/server/options_impl.h" |
29 | | #endif |
30 | | |
31 | | #include "test/test_common/file_system_for_test.h" |
32 | | #include "test/test_common/network_utility.h" |
33 | | |
34 | | #include "absl/debugging/symbolize.h" |
35 | | #include "absl/strings/match.h" |
36 | | #include "absl/strings/str_format.h" |
37 | | #include "gtest/gtest.h" |
38 | | #include "spdlog/spdlog.h" |
39 | | |
40 | | using bazel::tools::cpp::runfiles::Runfiles; |
41 | | using Envoy::Filesystem::Directory; |
42 | | using Envoy::Filesystem::DirectoryEntry; |
43 | | |
44 | | namespace Envoy { |
45 | | namespace { |
46 | | |
47 | 13 | std::string makeTempDir(std::string basename_template) { |
48 | | #ifdef WIN32 |
49 | | std::string name_template{}; |
50 | | if (std::getenv("TMP")) { |
51 | | name_template = TestEnvironment::getCheckedEnvVar("TMP") + "\\" + basename_template; |
52 | | } else if (std::getenv("WINDIR")) { |
53 | | name_template = TestEnvironment::getCheckedEnvVar("WINDIR") + "\\TEMP\\" + basename_template; |
54 | | } else { |
55 | | name_template = basename_template; |
56 | | } |
57 | | char* dirname = ::_mktemp(&name_template[0]); |
58 | | RELEASE_ASSERT(dirname != nullptr, fmt::format("failed to create tempdir from template: {} {}", |
59 | | name_template, errorDetails(errno))); |
60 | | TestEnvironment::createPath(dirname); |
61 | | #else |
62 | 13 | std::string name_template = "/tmp/" + basename_template; |
63 | 13 | char* dirname = ::mkdtemp(&name_template[0]); |
64 | 13 | RELEASE_ASSERT(dirname != nullptr, fmt::format("failed to create tempdir from template: {} {}", |
65 | 13 | name_template, errorDetails(errno))); |
66 | 13 | #endif |
67 | 13 | return {dirname}; |
68 | 13 | } |
69 | | |
70 | 0 | std::string getOrCreateUnixDomainSocketDirectory() { |
71 | 0 | const char* path = std::getenv("TEST_UDSDIR"); |
72 | 0 | if (path != nullptr) { |
73 | 0 | return {path}; |
74 | 0 | } |
75 | | // Generate temporary path for Unix Domain Sockets only. This is a workaround |
76 | | // for the sun_path limit on `sockaddr_un`, since TEST_TMPDIR as generated by |
77 | | // Bazel may be too long. |
78 | 0 | return makeTempDir("envoy_test_uds.XXXXXX"); |
79 | 0 | } |
80 | | |
81 | 13 | std::string getTemporaryDirectory() { |
82 | 13 | std::string temp_dir; |
83 | 13 | if (std::getenv("TEST_TMPDIR")) { |
84 | 0 | temp_dir = TestEnvironment::getCheckedEnvVar("TEST_TMPDIR"); |
85 | 13 | } else if (std::getenv("TMPDIR")) { |
86 | 0 | temp_dir = TestEnvironment::getCheckedEnvVar("TMPDIR"); |
87 | 13 | } else { |
88 | 13 | return makeTempDir("envoy_test_tmp.XXXXXX"); |
89 | 13 | } |
90 | 0 | TestEnvironment::createPath(temp_dir); |
91 | 0 | return temp_dir; |
92 | 13 | } |
93 | | |
94 | | // Allow initializeOptions() to remember CLI args for getOptions(). |
95 | | int argc_; |
96 | | char** argv_; |
97 | | |
98 | | } // namespace |
99 | | |
100 | 6.02k | void TestEnvironment::createPath(const std::string& path) { |
101 | 6.02k | if (Filesystem::fileSystemForTest().directoryExists(path)) { |
102 | 3.01k | return; |
103 | 3.01k | } |
104 | 3.01k | const Filesystem::PathSplitResult parent = |
105 | 3.01k | Filesystem::fileSystemForTest().splitPathFromFilename(path).value(); |
106 | 3.01k | if (parent.file_.length() > 0) { |
107 | 3.01k | TestEnvironment::createPath(std::string(parent.directory_)); |
108 | 3.01k | } |
109 | 3.01k | #ifndef WIN32 |
110 | 3.01k | RELEASE_ASSERT(::mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0, |
111 | 3.01k | absl::StrCat("failed to create path: ", path)); |
112 | | #else |
113 | | RELEASE_ASSERT(::CreateDirectory(path.c_str(), NULL), |
114 | | absl::StrCat("failed to create path: ", path)); |
115 | | #endif |
116 | 3.01k | } |
117 | | |
118 | | // On linux, attempt to unlink any file that exists at path, |
119 | | // ignoring the result code, to avoid traversing a symlink, |
120 | | // On windows, also attempt to remove the directory in case |
121 | | // it is actually a symlink/junction, ignoring the result code. |
122 | | // Proceed to iteratively recurse the directory if it still remains |
123 | 3.01k | void TestEnvironment::removePath(const std::string& path) { |
124 | 3.01k | RELEASE_ASSERT(absl::StartsWith(path, TestEnvironment::temporaryDirectory()), |
125 | 3.01k | "cowardly refusing to remove test directory not in temp path"); |
126 | 3.01k | #ifndef WIN32 |
127 | 3.01k | (void)::unlink(path.c_str()); |
128 | | #else |
129 | | (void)::DeleteFile(path.c_str()); |
130 | | (void)::RemoveDirectory(path.c_str()); |
131 | | #endif |
132 | 3.01k | if (!Filesystem::fileSystemForTest().directoryExists(path)) { |
133 | 0 | return; |
134 | 0 | } |
135 | 3.01k | Directory directory(path); |
136 | 3.01k | std::string entry_name; |
137 | 3.01k | entry_name.reserve(path.size() + 256); |
138 | 3.01k | entry_name.append(path); |
139 | 3.01k | entry_name.append("/"); |
140 | 3.01k | size_t fileidx = entry_name.size(); |
141 | 9.03k | for (const DirectoryEntry& entry : directory) { |
142 | 9.03k | entry_name.resize(fileidx); |
143 | 9.03k | entry_name.append(entry.name_); |
144 | 9.03k | if (entry.type_ == Envoy::Filesystem::FileType::Regular) { |
145 | 3.01k | #ifndef WIN32 |
146 | 3.01k | RELEASE_ASSERT(::unlink(entry_name.c_str()) == 0, |
147 | 3.01k | absl::StrCat("failed to remove file: ", entry_name)); |
148 | | #else |
149 | | RELEASE_ASSERT(::DeleteFile(entry_name.c_str()), |
150 | | absl::StrCat("failed to remove file: ", entry_name)); |
151 | | #endif |
152 | 6.02k | } else if (entry.type_ == Envoy::Filesystem::FileType::Directory) { |
153 | 6.02k | if (entry.name_ != "." && entry.name_ != "..") { |
154 | 0 | removePath(entry_name); |
155 | 0 | } |
156 | 6.02k | } |
157 | 9.03k | } |
158 | 3.01k | #ifndef WIN32 |
159 | 3.01k | RELEASE_ASSERT(::rmdir(path.c_str()) == 0, |
160 | 3.01k | absl::StrCat("failed to remove path: ", path, " (rmdir failed)")); |
161 | | #else |
162 | | RELEASE_ASSERT(::RemoveDirectory(path.c_str()), absl::StrCat("failed to remove path: ", path)); |
163 | | #endif |
164 | 3.01k | } |
165 | | |
166 | 0 | void TestEnvironment::renameFile(const std::string& old_name, const std::string& new_name) { |
167 | 0 | Filesystem::fileSystemForTest().renameFile(old_name, new_name); |
168 | 0 | } |
169 | | |
170 | 0 | void TestEnvironment::createSymlink(const std::string& target, const std::string& link) { |
171 | | #ifdef WIN32 |
172 | | const DWORD attributes = ::GetFileAttributes(target.c_str()); |
173 | | ASSERT_NE(attributes, INVALID_FILE_ATTRIBUTES); |
174 | | int flags = SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE; |
175 | | if (attributes & FILE_ATTRIBUTE_DIRECTORY) { |
176 | | flags |= SYMBOLIC_LINK_FLAG_DIRECTORY; |
177 | | } |
178 | | |
179 | | const BOOLEAN rc = ::CreateSymbolicLink(link.c_str(), target.c_str(), flags); |
180 | | ASSERT_NE(rc, 0); |
181 | | #else |
182 | 0 | const int rc = ::symlink(target.c_str(), link.c_str()); |
183 | 0 | ASSERT_EQ(rc, 0); |
184 | 0 | #endif |
185 | 0 | } |
186 | | |
187 | 1.97k | absl::optional<std::string> TestEnvironment::getOptionalEnvVar(const std::string& var) { |
188 | 1.97k | const char* path = std::getenv(var.c_str()); |
189 | 1.97k | if (path == nullptr) { |
190 | 1.97k | return {}; |
191 | 1.97k | } |
192 | 0 | return std::string(path); |
193 | 1.97k | } |
194 | | |
195 | 0 | std::string TestEnvironment::getCheckedEnvVar(const std::string& var) { |
196 | 0 | auto optional = getOptionalEnvVar(var); |
197 | 0 | RELEASE_ASSERT(optional.has_value(), var); |
198 | 0 | return optional.value(); |
199 | 0 | } |
200 | | |
201 | 0 | void TestEnvironment::initializeTestMain(char* program_name) { |
202 | | #ifdef WIN32 |
203 | | _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT); |
204 | | |
205 | | _set_invalid_parameter_handler([](const wchar_t* expression, const wchar_t* function, |
206 | | const wchar_t* file, unsigned int line, |
207 | | uintptr_t pReserved) {}); |
208 | | |
209 | | WSADATA wsa_data; |
210 | | const WORD version_requested = MAKEWORD(2, 2); |
211 | | RELEASE_ASSERT(WSAStartup(version_requested, &wsa_data) == 0, ""); |
212 | | #endif |
213 | |
|
214 | 0 | absl::InitializeSymbolizer(program_name); |
215 | |
|
216 | | #ifdef ENVOY_HANDLE_SIGNALS |
217 | | // Enabled by default. Control with "bazel --define=signal_trace=disabled" |
218 | | static Envoy::SignalAction handle_sigs; |
219 | | #endif |
220 | 0 | } |
221 | | |
222 | 132 | void TestEnvironment::initializeOptions(int argc, char** argv) { |
223 | 132 | argc_ = argc; |
224 | 132 | argv_ = argv; |
225 | 132 | } |
226 | | |
227 | 9.36k | bool TestEnvironment::shouldRunTestForIpVersion(Network::Address::IpVersion type) { |
228 | 9.36k | const char* value = std::getenv("ENVOY_IP_TEST_VERSIONS"); |
229 | 9.36k | std::string option(value ? value : ""); |
230 | 9.36k | if (option.empty()) { |
231 | 9.36k | return true; |
232 | 9.36k | } |
233 | 0 | if ((type == Network::Address::IpVersion::v4 && option == "v6only") || |
234 | 0 | (type == Network::Address::IpVersion::v6 && option == "v4only")) { |
235 | 0 | return false; |
236 | 0 | } |
237 | 0 | return true; |
238 | 0 | } |
239 | | |
240 | 4.68k | std::vector<Network::Address::IpVersion> TestEnvironment::getIpVersionsForTest() { |
241 | 4.68k | std::vector<Network::Address::IpVersion> parameters; |
242 | 9.36k | for (auto version : {Network::Address::IpVersion::v4, Network::Address::IpVersion::v6}) { |
243 | 9.36k | if (TestEnvironment::shouldRunTestForIpVersion(version)) { |
244 | 9.36k | parameters.push_back(version); |
245 | 9.36k | if (!Network::Test::supportsIpVersion(version)) { |
246 | 0 | const auto version_string = Network::Test::addressVersionAsString(version); |
247 | 0 | ENVOY_LOG_TO_LOGGER( |
248 | 0 | Logger::Registry::getLog(Logger::Id::testing), warn, |
249 | 0 | "Testing with IP{} addresses may not be supported on this machine. If " |
250 | 0 | "testing fails, set the environment variable ENVOY_IP_TEST_VERSIONS to 'v{}only'.", |
251 | 0 | version_string, version_string); |
252 | 0 | } |
253 | 9.36k | } |
254 | 9.36k | } |
255 | 4.68k | return parameters; |
256 | 4.68k | } |
257 | | |
258 | 0 | std::vector<spdlog::logger*> TestEnvironment::getSpdLoggersForTest() { |
259 | 0 | std::vector<spdlog::logger*> logger_parameters; |
260 | 0 | logger_parameters.push_back(&Logger::Registry::loggers()[0].getLogger()); |
261 | 0 | std::atomic<spdlog::logger*> flogger{nullptr}; |
262 | 0 | getFineGrainLogContext().initFineGrainLogger(__FILE__, flogger); |
263 | 0 | logger_parameters.push_back(flogger.load(std::memory_order_relaxed)); |
264 | 0 | return logger_parameters; |
265 | 0 | } |
266 | | |
267 | 2.24k | Server::Options& TestEnvironment::getOptions() { |
268 | | #ifdef ENVOY_DISABLE_EXCEPTIONS |
269 | | // tclap, used for command line parsing, throws exceptions. While Envoy won't |
270 | | // do full command line parsing in exception-free mode, handling -l trace is a |
271 | | // nice-to-have for Envoy mobile e2e tests. |
272 | | static OptionsImplBase* options = new OptionsImplBase(); |
273 | | for (int i = 0; i < argc_; ++i) { |
274 | | if (absl::StartsWith(argv_[i], "-l ")) { |
275 | | static absl::StatusOr<spdlog::level::level_enum> level = |
276 | | OptionsImplBase::parseAndValidateLogLevel(argv_[i] + 3); |
277 | | if (level.status().ok()) { |
278 | | options->setLogLevel(*level); |
279 | | } |
280 | | } |
281 | | } |
282 | | #else |
283 | 2.24k | static OptionsImpl* options = new OptionsImpl( |
284 | 2.24k | argc_, argv_, [](bool) { return "1"; }, spdlog::level::err); |
285 | 2.24k | #endif |
286 | 2.24k | return *options; |
287 | 2.24k | } |
288 | | |
289 | 403 | std::vector<Grpc::ClientType> TestEnvironment::getsGrpcVersionsForTest() { |
290 | 403 | #ifdef ENVOY_GOOGLE_GRPC |
291 | 403 | return {Grpc::ClientType::EnvoyGrpc, Grpc::ClientType::GoogleGrpc}; |
292 | | #else |
293 | | return {Grpc::ClientType::EnvoyGrpc}; |
294 | | #endif |
295 | 403 | } |
296 | | |
297 | 15.4k | const std::string& TestEnvironment::temporaryDirectory() { |
298 | 15.4k | CONSTRUCT_ON_FIRST_USE(std::string, getTemporaryDirectory()); |
299 | 15.4k | } |
300 | | |
301 | 0 | std::string TestEnvironment::runfilesDirectory(const std::string& workspace) { |
302 | 0 | RELEASE_ASSERT(runfiles_ != nullptr, ""); |
303 | 0 | auto path = runfiles_->Rlocation(workspace); |
304 | | #ifdef WIN32 |
305 | | path = std::regex_replace(path, std::regex("\\\\"), "/"); |
306 | | #endif |
307 | 0 | return path; |
308 | 0 | } |
309 | | |
310 | 0 | std::string TestEnvironment::runfilesPath(const std::string& path, const std::string& workspace) { |
311 | 0 | RELEASE_ASSERT(runfiles_ != nullptr, ""); |
312 | 0 | return runfiles_->Rlocation(absl::StrCat(workspace, "/", path)); |
313 | 0 | } |
314 | | |
315 | 0 | const std::string TestEnvironment::unixDomainSocketDirectory() { |
316 | 0 | CONSTRUCT_ON_FIRST_USE(std::string, getOrCreateUnixDomainSocketDirectory()); |
317 | 0 | } |
318 | | |
319 | | std::string TestEnvironment::substitute(const std::string& str, |
320 | 0 | Network::Address::IpVersion version) { |
321 | 0 | const absl::node_hash_map<std::string, std::string> path_map = { |
322 | 0 | {"test_tmpdir", TestEnvironment::temporaryDirectory()}, |
323 | 0 | {"test_udsdir", TestEnvironment::unixDomainSocketDirectory()}, |
324 | 0 | {"test_rundir", runfiles_ != nullptr ? TestEnvironment::runfilesDirectory() : "invalid"}, |
325 | 0 | }; |
326 | |
|
327 | 0 | std::string out_json_string = str; |
328 | 0 | for (const auto& it : path_map) { |
329 | 0 | const std::regex port_regex("\\{\\{ " + it.first + " \\}\\}"); |
330 | 0 | out_json_string = std::regex_replace(out_json_string, port_regex, it.second); |
331 | 0 | } |
332 | | |
333 | | // Substitute platform specific null device. |
334 | 0 | const std::regex null_device_regex(R"(\{\{ null_device_path \}\})"); |
335 | 0 | out_json_string = std::regex_replace(out_json_string, null_device_regex, |
336 | 0 | std::string(Platform::null_device_path).c_str()); |
337 | | |
338 | | // Substitute IP loopback addresses. |
339 | 0 | const std::regex loopback_address_regex(R"(\{\{ ip_loopback_address \}\})"); |
340 | 0 | out_json_string = std::regex_replace(out_json_string, loopback_address_regex, |
341 | 0 | Network::Test::getLoopbackAddressString(version)); |
342 | 0 | const std::regex ntop_loopback_address_regex(R"(\{\{ ntop_ip_loopback_address \}\})"); |
343 | 0 | out_json_string = std::regex_replace(out_json_string, ntop_loopback_address_regex, |
344 | 0 | Network::Test::getLoopbackAddressString(version)); |
345 | | |
346 | | // Substitute IP any addresses. |
347 | 0 | const std::regex any_address_regex(R"(\{\{ ip_any_address \}\})"); |
348 | 0 | out_json_string = std::regex_replace(out_json_string, any_address_regex, |
349 | 0 | Network::Test::getAnyAddressString(version)); |
350 | | |
351 | | // Substitute dns lookup family. |
352 | 0 | const std::regex lookup_family_regex(R"(\{\{ dns_lookup_family \}\})"); |
353 | 0 | switch (version) { |
354 | 0 | case Network::Address::IpVersion::v4: |
355 | 0 | out_json_string = std::regex_replace(out_json_string, lookup_family_regex, "v4_only"); |
356 | 0 | break; |
357 | 0 | case Network::Address::IpVersion::v6: |
358 | 0 | out_json_string = std::regex_replace(out_json_string, lookup_family_regex, "v6_only"); |
359 | 0 | break; |
360 | 0 | } |
361 | | |
362 | | // Substitute socket options arguments. |
363 | 0 | const std::regex sol_socket_regex(R"(\{\{ sol_socket \}\})"); |
364 | 0 | out_json_string = |
365 | 0 | std::regex_replace(out_json_string, sol_socket_regex, std::to_string(SOL_SOCKET)); |
366 | 0 | const std::regex so_reuseport_regex(R"(\{\{ so_reuseport \}\})"); |
367 | 0 | out_json_string = |
368 | 0 | std::regex_replace(out_json_string, so_reuseport_regex, std::to_string(SO_REUSEPORT)); |
369 | |
|
370 | 0 | return out_json_string; |
371 | 0 | } |
372 | | |
373 | | std::string TestEnvironment::temporaryFileSubstitute(const std::string& path, |
374 | | const PortMap& port_map, |
375 | 0 | Network::Address::IpVersion version) { |
376 | 0 | return temporaryFileSubstitute(path, ParamMap(), port_map, version); |
377 | 0 | } |
378 | | |
379 | 0 | std::string TestEnvironment::readFileToStringForTest(const std::string& filename) { |
380 | 0 | return Filesystem::fileSystemForTest().fileReadToEnd(filename).value(); |
381 | 0 | } |
382 | | |
383 | | std::string TestEnvironment::temporaryFileSubstitute(const std::string& path, |
384 | | const ParamMap& param_map, |
385 | | const PortMap& port_map, |
386 | 0 | Network::Address::IpVersion version) { |
387 | 0 | RELEASE_ASSERT(!path.empty(), "requested path to substitute in is empty"); |
388 | | // Load the entire file as a string, regex replace one at a time and write it back out. Proper |
389 | | // templating might be better one day, but this works for now. |
390 | 0 | const std::string json_path = TestEnvironment::runfilesPath(path); |
391 | 0 | std::string out_json_string = readFileToStringForTest(json_path); |
392 | | |
393 | | // Substitute params. |
394 | 0 | for (const auto& it : param_map) { |
395 | 0 | const std::regex param_regex("\\{\\{ " + it.first + " \\}\\}"); |
396 | 0 | out_json_string = std::regex_replace(out_json_string, param_regex, it.second); |
397 | 0 | } |
398 | | |
399 | | // Substitute ports. |
400 | 0 | for (const auto& it : port_map) { |
401 | 0 | const std::regex port_regex("\\{\\{ " + it.first + " \\}\\}"); |
402 | 0 | out_json_string = std::regex_replace(out_json_string, port_regex, std::to_string(it.second)); |
403 | 0 | } |
404 | | |
405 | | // Substitute paths and other common things. |
406 | 0 | out_json_string = substitute(out_json_string, version); |
407 | |
|
408 | 0 | auto name = Filesystem::fileSystemForTest().splitPathFromFilename(path).value().file_; |
409 | 0 | const std::string extension = std::string(name.substr(name.rfind('.'))); |
410 | 0 | const std::string out_json_path = |
411 | 0 | TestEnvironment::temporaryPath(name) + ".with.ports" + extension; |
412 | 0 | { |
413 | 0 | std::ofstream out_json_file(out_json_path, std::ios::binary); |
414 | 0 | out_json_file << out_json_string; |
415 | 0 | } |
416 | 0 | return out_json_path; |
417 | 0 | } |
418 | | |
419 | | Json::ObjectSharedPtr TestEnvironment::jsonLoadFromString(const std::string& json, |
420 | 0 | Network::Address::IpVersion version) { |
421 | 0 | return Json::Factory::loadFromString(substitute(json, version)); |
422 | 0 | } |
423 | | |
424 | | // This function is not used for Envoy Mobile tests, and ::system() is not supported on iOS. |
425 | | #ifndef TARGET_OS_IOS |
426 | 0 | void TestEnvironment::exec(const std::vector<std::string>& args) { |
427 | 0 | std::stringstream cmd; |
428 | | // Symlinked args[0] can confuse Python when importing module relative, so we let Python know |
429 | | // where it can find its module relative files. |
430 | 0 | cmd << "bash -c \"PYTHONPATH=$(dirname " << args[0] << ") "; |
431 | 0 | for (auto& arg : args) { |
432 | 0 | cmd << arg << " "; |
433 | 0 | } |
434 | 0 | cmd << "\""; |
435 | 0 | if (::system(cmd.str().c_str()) != 0) { |
436 | 0 | std::cerr << "Failed " << cmd.str() << "\n"; |
437 | 0 | RELEASE_ASSERT(false, ""); |
438 | 0 | } |
439 | 0 | } |
440 | | #endif |
441 | | |
442 | | std::string TestEnvironment::writeStringToFileForTest(const std::string& filename, |
443 | | const std::string& contents, |
444 | 9.46k | bool fully_qualified_path, bool do_unlink) { |
445 | 9.46k | const std::string out_path = |
446 | 9.46k | fully_qualified_path ? filename : TestEnvironment::temporaryPath(filename); |
447 | 9.46k | if (do_unlink) { |
448 | 9.46k | unlink(out_path.c_str()); |
449 | 9.46k | } |
450 | | |
451 | 9.46k | Filesystem::FilePathAndType out_file_info{Filesystem::DestinationType::File, out_path}; |
452 | 9.46k | Filesystem::FilePtr file = Filesystem::fileSystemForTest().createFile(out_file_info); |
453 | 9.46k | const Filesystem::FlagSet flags{1 << Filesystem::File::Operation::Write | |
454 | 9.46k | 1 << Filesystem::File::Operation::Create}; |
455 | 9.46k | const Api::IoCallBoolResult open_result = file->open(flags); |
456 | 18.9k | EXPECT_TRUE(open_result.return_value_) << open_result.err_->getErrorDetails(); |
457 | 9.46k | const Api::IoCallSizeResult result = file->write(contents); |
458 | 9.46k | EXPECT_EQ(contents.length(), result.return_value_); |
459 | 9.46k | return out_path; |
460 | 9.46k | } |
461 | | |
462 | 0 | void TestEnvironment::setEnvVar(const std::string& name, const std::string& value, int overwrite) { |
463 | | #ifdef WIN32 |
464 | | if (!overwrite) { |
465 | | size_t requiredSize; |
466 | | const int rc = ::getenv_s(&requiredSize, nullptr, 0, name.c_str()); |
467 | | ASSERT_EQ(0, rc); |
468 | | if (requiredSize != 0) { |
469 | | return; |
470 | | } |
471 | | } |
472 | | const int rc = ::_putenv_s(name.c_str(), value.c_str()); |
473 | | ASSERT_EQ(0, rc); |
474 | | #else |
475 | 0 | const int rc = ::setenv(name.c_str(), value.c_str(), overwrite); |
476 | 0 | ASSERT_EQ(0, rc); |
477 | 0 | #endif |
478 | 0 | } |
479 | | |
480 | 0 | void TestEnvironment::unsetEnvVar(const std::string& name) { |
481 | | #ifdef WIN32 |
482 | | const int rc = ::_putenv_s(name.c_str(), ""); |
483 | | ASSERT_EQ(0, rc); |
484 | | #else |
485 | 0 | const int rc = ::unsetenv(name.c_str()); |
486 | 0 | ASSERT_EQ(0, rc); |
487 | 0 | #endif |
488 | 0 | } |
489 | | |
490 | 0 | void TestEnvironment::setRunfiles(Runfiles* runfiles) { runfiles_ = runfiles; } |
491 | | |
492 | | Runfiles* TestEnvironment::runfiles_{}; |
493 | | |
494 | | AtomicFileUpdater::AtomicFileUpdater(const std::string& filename) |
495 | | : link_(filename), new_link_(absl::StrCat(filename, ".new")), |
496 | 0 | target1_(absl::StrCat(filename, ".target1")), target2_(absl::StrCat(filename, ".target2")) { |
497 | 0 | unlink(link_.c_str()); |
498 | 0 | unlink(new_link_.c_str()); |
499 | 0 | unlink(target1_.c_str()); |
500 | 0 | unlink(target2_.c_str()); |
501 | 0 | } |
502 | | |
503 | 0 | void AtomicFileUpdater::update(const std::string& contents) { |
504 | 0 | const std::string target = use_target1_ ? target1_ : target2_; |
505 | 0 | use_target1_ = !use_target1_; |
506 | 0 | { |
507 | 0 | std::ofstream file(target, std::ios_base::binary); |
508 | 0 | file << contents; |
509 | 0 | } |
510 | 0 | TestEnvironment::createSymlink(target, new_link_); |
511 | 0 | TestEnvironment::renameFile(new_link_, link_); |
512 | 0 | } |
513 | | |
514 | | } // namespace Envoy |