From 94c3328b6138f75d0f027e04b7d9570cc479324f Mon Sep 17 00:00:00 2001 From: Dor Hen Date: Tue, 13 Feb 2024 11:51:01 +0200 Subject: [PATCH] Provide unified solution for dir name randomization in tests This approach actually wraps the unique identifier generation into the function that provides the output path for a test. This way we don't need to add `CreateRandomUuid()` everywhere that we have `test::OutputPath` and instead just rename to `test::OutputPathRandomDir` Bug: webrtc:15833 Change-Id: Ic9b69b5b599727f07b2906569a84a40edeecd1a0 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/338645 Reviewed-by: Harald Alvestrand Reviewed-by: Mirko Bonadei Commit-Queue: Jeremy Leconte Reviewed-by: Artem Titov Cr-Commit-Position: refs/heads/main@{#41730} --- test/BUILD.gn | 1 + test/testsupport/file_utils.cc | 8 ++++++++ test/testsupport/file_utils.h | 5 +++++ test/testsupport/file_utils_unittest.cc | 22 ++++++++++++++++++++++ 4 files changed, 36 insertions(+) diff --git a/test/BUILD.gn b/test/BUILD.gn index 574496f2e3..ed5dc5173d 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -861,6 +861,7 @@ rtc_library("fileutils") { ":fileutils_override_api", ":fileutils_override_impl", "../rtc_base:checks", + "../rtc_base:ssl", "../rtc_base:stringutils", ] absl_deps = [ diff --git a/test/testsupport/file_utils.cc b/test/testsupport/file_utils.cc index 47fed9ac05..58ee16282e 100644 --- a/test/testsupport/file_utils.cc +++ b/test/testsupport/file_utils.cc @@ -54,6 +54,7 @@ #include "absl/strings/string_view.h" #include "rtc_base/checks.h" +#include "rtc_base/helpers.h" #include "rtc_base/string_utils.h" #include "rtc_base/strings/string_builder.h" #include "test/testsupport/file_utils_override.h" @@ -94,6 +95,13 @@ std::string OutputPath() { return webrtc::test::internal::OutputPath(); } +std::string OutputPathWithRandomDirectory() { + std::string path = webrtc::test::internal::OutputPath(); + std::string rand_dir = path + rtc::CreateRandomUuid(); + + return CreateDir(rand_dir) ? rand_dir + std::string(kPathDelimiter) : path; +} + std::string WorkingDir() { return webrtc::test::internal::WorkingDir(); } diff --git a/test/testsupport/file_utils.h b/test/testsupport/file_utils.h index ab80ca4454..120c6cb279 100644 --- a/test/testsupport/file_utils.h +++ b/test/testsupport/file_utils.h @@ -42,6 +42,11 @@ ABSL_CONST_INIT extern const absl::string_view kPathDelimiter; // found, the current working directory ("./") is returned as a fallback. std::string OutputPath(); +// Same as the above but appends a randomly named folder at the end of the path +// Primerly used to provide a solution for stress testing environments to +// prevent colission of files and folders. +std::string OutputPathWithRandomDirectory(); + // Generates an empty file with a unique name in the specified directory and // returns the file name and path. // TODO(titovartem) rename to TempFile and next method to TempFilename diff --git a/test/testsupport/file_utils_unittest.cc b/test/testsupport/file_utils_unittest.cc index 666d574a85..8f91d6db7c 100644 --- a/test/testsupport/file_utils_unittest.cc +++ b/test/testsupport/file_utils_unittest.cc @@ -120,6 +120,28 @@ TEST_F(FileUtilsTest, OutputPathFromRootWorkingDir) { ASSERT_THAT(result, EndsWith(expected_end)); } +TEST_F(FileUtilsTest, RandomOutputPathFromUnchangedWorkingDir) { + rtc::SetRandomTestMode(true); + std::string fixed_first_uuid = "def01482-f829-429a-bfd4-841706e92cdd"; + std::string expected_end = ExpectedRootDirByPlatform() + fixed_first_uuid + + std::string(kPathDelimiter); + std::string result = webrtc::test::OutputPathWithRandomDirectory(); + + ASSERT_THAT(result, EndsWith(expected_end)); +} + +TEST_F(FileUtilsTest, RandomOutputPathFromRootWorkingDir) { + ASSERT_EQ(0, chdir(kPathDelimiter.data())); + + rtc::SetRandomTestMode(true); + std::string fixed_first_uuid = "def01482-f829-429a-bfd4-841706e92cdd"; + std::string expected_end = ExpectedRootDirByPlatform() + fixed_first_uuid + + std::string(kPathDelimiter); + std::string result = webrtc::test::OutputPathWithRandomDirectory(); + + ASSERT_THAT(result, EndsWith(expected_end)); +} + TEST_F(FileUtilsTest, TempFilename) { std::string temp_filename = webrtc::test::TempFilename( webrtc::test::OutputPath(), "TempFilenameTest");