aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/testing/common/ut
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/testing/common/ut
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/testing/common/ut')
-rw-r--r--library/cpp/testing/common/ut/env_ut.cpp162
-rw-r--r--library/cpp/testing/common/ut/network_ut.cpp54
-rw-r--r--library/cpp/testing/common/ut/scope_ut.cpp28
-rw-r--r--library/cpp/testing/common/ut/ya.make19
4 files changed, 263 insertions, 0 deletions
diff --git a/library/cpp/testing/common/ut/env_ut.cpp b/library/cpp/testing/common/ut/env_ut.cpp
new file mode 100644
index 00000000000..2aed1e4a255
--- /dev/null
+++ b/library/cpp/testing/common/ut/env_ut.cpp
@@ -0,0 +1,162 @@
+#include <library/cpp/testing/common/env.h>
+#include <library/cpp/testing/common/scope.h>
+#include <library/cpp/testing/gtest/gtest.h>
+
+#include <util/folder/dirut.h>
+#include <util/stream/file.h>
+#include <util/system/env.h>
+#include <util/system/execpath.h>
+#include <util/system/fs.h>
+
+
+TEST(Runtime, ArcadiaSourceRoot) {
+ NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename
+ {
+ auto tmpDir = ::GetSystemTempDir();
+ NTesting::TScopedEnvironment guard("ARCADIA_SOURCE_ROOT", tmpDir);
+ Singleton<NPrivate::TTestEnv>()->ReInitialize();
+ EXPECT_EQ(tmpDir, ArcadiaSourceRoot());
+ }
+ {
+ NTesting::TScopedEnvironment guard("ARCADIA_SOURCE_ROOT", "");
+ Singleton<NPrivate::TTestEnv>()->ReInitialize();
+ EXPECT_FALSE(ArcadiaSourceRoot().empty());
+ }
+}
+
+TEST(Runtime, BuildRoot) {
+ NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename
+ {
+ auto tmpDir = ::GetSystemTempDir();
+ NTesting::TScopedEnvironment guard("ARCADIA_BUILD_ROOT", tmpDir);
+ Singleton<NPrivate::TTestEnv>()->ReInitialize();
+ EXPECT_EQ(tmpDir, BuildRoot());
+ }
+ {
+ NTesting::TScopedEnvironment guard("ARCADIA_BUILD_ROOT", "");
+ Singleton<NPrivate::TTestEnv>()->ReInitialize();
+ EXPECT_FALSE(BuildRoot().empty());
+ }
+}
+
+TEST(Runtime, BinaryPath) {
+ NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename
+ Singleton<NPrivate::TTestEnv>()->ReInitialize();
+ EXPECT_TRUE(TFsPath(BinaryPath("library/cpp/testing/common/ut")).Exists());
+}
+
+TEST(Runtime, GetArcadiaTestsData) {
+ NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename
+ {
+ auto tmpDir = ::GetSystemTempDir();
+ NTesting::TScopedEnvironment guard("ARCADIA_TESTS_DATA_DIR", tmpDir);
+ Singleton<NPrivate::TTestEnv>()->ReInitialize();
+ EXPECT_EQ(tmpDir, GetArcadiaTestsData());
+ }
+ {
+ NTesting::TScopedEnvironment guard("ARCADIA_TESTS_DATA_DIR", "");
+ Singleton<NPrivate::TTestEnv>()->ReInitialize();
+ auto path = GetArcadiaTestsData();
+ // it is not error if path is empty
+ const bool ok = (path.empty() || GetBaseName(path) == "arcadia_tests_data");
+ EXPECT_TRUE(ok);
+ }
+}
+
+TEST(Runtime, GetWorkPath) {
+ NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename
+ {
+ auto tmpDir = ::GetSystemTempDir();
+ NTesting::TScopedEnvironment guard("TEST_WORK_PATH", tmpDir);
+ Singleton<NPrivate::TTestEnv>()->ReInitialize();
+ EXPECT_EQ(tmpDir, GetWorkPath());
+ }
+ {
+ NTesting::TScopedEnvironment guard("TEST_WORK_PATH", "");
+ Singleton<NPrivate::TTestEnv>()->ReInitialize();
+ EXPECT_TRUE(!GetWorkPath().empty());
+ }
+}
+
+TEST(Runtime, GetOutputPath) {
+ NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename
+ Singleton<NPrivate::TTestEnv>()->ReInitialize();
+ EXPECT_EQ(GetOutputPath().Basename(), "testing_out_stuff");
+}
+
+TEST(Runtime, GetRamDrivePath) {
+ NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename
+ auto tmpDir = ::GetSystemTempDir();
+ NTesting::TScopedEnvironment guard("YA_TEST_RAM_DRIVE_PATH", tmpDir);
+ Singleton<NPrivate::TTestEnv>()->ReInitialize();
+ EXPECT_EQ(tmpDir, GetRamDrivePath());
+}
+
+TEST(Runtime, GetOutputRamDrivePath) {
+ NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename
+ auto tmpDir = ::GetSystemTempDir();
+ NTesting::TScopedEnvironment guard("YA_TEST_OUTPUT_RAM_DRIVE_PATH", tmpDir);
+ Singleton<NPrivate::TTestEnv>()->ReInitialize();
+ EXPECT_EQ(tmpDir, GetOutputRamDrivePath());
+}
+
+#ifdef _linux_
+TEST(Runtime, GdbPath) {
+ Singleton<NPrivate::TTestEnv>()->ReInitialize();
+ EXPECT_TRUE(NFs::Exists(::GdbPath()));
+}
+#endif
+
+TString ReInitializeContext(TStringBuf data) {
+ auto tmpDir = ::GetSystemTempDir();
+ auto filename = tmpDir + "/context.json";
+ TOFStream stream(filename);
+ stream.Write(data.data(), data.size());
+ stream.Finish();
+
+ NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", filename);
+ Singleton<NPrivate::TTestEnv>()->ReInitialize();
+
+ return filename;
+}
+
+TEST(Runtime, GetTestParam) {
+ TString context = R"json({
+ "runtime": {
+ "test_params": {
+ "a": "b",
+ "c": "d"
+ }
+ }
+ })json";
+ auto filename = ReInitializeContext(context);
+
+ EXPECT_EQ("b", GetTestParam("a"));
+ EXPECT_EQ("d", GetTestParam("c"));
+ EXPECT_EQ("", GetTestParam("e"));
+ EXPECT_EQ("w", GetTestParam("e", "w"));
+
+ Singleton<NPrivate::TTestEnv>()->AddTestParam("e", "e");
+ EXPECT_EQ("e", GetTestParam("e"));
+}
+
+TEST(Runtime, WatchProcessCore) {
+ TString context = R"json({
+ "internal": {
+ "core_search_file": "watch_core.txt"
+ }
+ })json";
+ auto filename = ReInitializeContext(context);
+
+ WatchProcessCore(1, "bin1", "pwd");
+ WatchProcessCore(2, "bin1");
+ StopProcessCoreWatching(2);
+
+ TIFStream file("watch_core.txt");
+ auto data = file.ReadAll();
+ TString expected = R"json({"cmd":"add","pid":1,"binary_path":"bin1","cwd":"pwd"}
+{"cmd":"add","pid":2,"binary_path":"bin1"}
+{"cmd":"drop","pid":2}
+)json";
+ EXPECT_EQ(expected, data);
+}
diff --git a/library/cpp/testing/common/ut/network_ut.cpp b/library/cpp/testing/common/ut/network_ut.cpp
new file mode 100644
index 00000000000..6a40775fd93
--- /dev/null
+++ b/library/cpp/testing/common/ut/network_ut.cpp
@@ -0,0 +1,54 @@
+#include <library/cpp/testing/common/network.h>
+#include <library/cpp/testing/common/scope.h>
+
+#include <util/generic/hash_set.h>
+
+#include <util/folder/dirut.h>
+#include <util/folder/path.h>
+#include <util/folder/tempdir.h>
+#include <util/network/sock.h>
+#include <util/system/fs.h>
+
+#include <library/cpp/testing/gtest/gtest.h>
+
+static TTempDir TmpDir;
+
+TEST(NetworkTest, FreePort) {
+ NTesting::TScopedEnvironment envGuard("PORT_SYNC_PATH", TmpDir.Name());
+
+ TVector<NTesting::TPortHolder> ports(Reserve(100));
+
+ for (size_t i = 0; i < 100; ++i) {
+ ports.push_back(NTesting::GetFreePort());
+ }
+
+ THashSet<ui16> uniqPorts;
+ for (auto& port : ports) {
+ const TString guardPath = TmpDir.Path() / ToString(static_cast<ui16>(port));
+ EXPECT_TRUE(NFs::Exists(guardPath));
+ EXPECT_TRUE(uniqPorts.emplace(port).second);
+
+ TInetStreamSocket sock;
+ TSockAddrInet addr(TIpHost{INADDR_ANY}, port);
+ ASSERT_EQ(0, SetSockOpt(sock, SOL_SOCKET, SO_REUSEADDR, 1));
+ EXPECT_EQ(0, sock.Bind(&addr));
+ }
+ ports.clear();
+ for (ui16 port : uniqPorts) {
+ const TString guardPath = TmpDir.Path() / ToString(port);
+ EXPECT_FALSE(NFs::Exists(guardPath));
+ }
+}
+
+
+TEST(FreePortTest, FreePortsRange) {
+ NTesting::TScopedEnvironment envGuard("PORT_SYNC_PATH", TmpDir.Name());
+
+ for (ui16 i = 2; i < 10; ++i) {
+ TVector<NTesting::TPortHolder> ports = NTesting::NLegacy::GetFreePortsRange(i);
+ ASSERT_EQ(i, ports.size());
+ for (ui16 j = 1; j < i; ++j) {
+ EXPECT_EQ(static_cast<ui16>(ports[j]), static_cast<ui16>(ports[0]) + j);
+ }
+ }
+}
diff --git a/library/cpp/testing/common/ut/scope_ut.cpp b/library/cpp/testing/common/ut/scope_ut.cpp
new file mode 100644
index 00000000000..4fb82c2466d
--- /dev/null
+++ b/library/cpp/testing/common/ut/scope_ut.cpp
@@ -0,0 +1,28 @@
+#include <library/cpp/testing/common/scope.h>
+
+#include <util/system/env.h>
+
+#include <library/cpp/testing/gtest/gtest.h>
+
+TEST(TScopedEnvironment, SingleValue) {
+ auto before = GetEnv("ARCADIA_SOURCE_ROOT");
+ {
+ NTesting::TScopedEnvironment guard("ARCADIA_SOURCE_ROOT", "source");
+ EXPECT_EQ("source", GetEnv("ARCADIA_SOURCE_ROOT"));
+ }
+ EXPECT_EQ(before, GetEnv("ARCADIA_SOURCE_ROOT"));
+}
+
+TEST(TScopedEnvironment, MultiValue) {
+ TVector<TString> before{GetEnv("ARCADIA_SOURCE_ROOT"), GetEnv("ARCADIA_BUILD_ROOT")};
+ {
+ NTesting::TScopedEnvironment guard{{
+ {"ARCADIA_SOURCE_ROOT", "source"},
+ {"ARCADIA_BUILD_ROOT", "build"},
+ }};
+ EXPECT_EQ("source", GetEnv("ARCADIA_SOURCE_ROOT"));
+ EXPECT_EQ("build", GetEnv("ARCADIA_BUILD_ROOT"));
+ }
+ TVector<TString> after{GetEnv("ARCADIA_SOURCE_ROOT"), GetEnv("ARCADIA_BUILD_ROOT")};
+ EXPECT_EQ(before, after);
+}
diff --git a/library/cpp/testing/common/ut/ya.make b/library/cpp/testing/common/ut/ya.make
new file mode 100644
index 00000000000..053aa380791
--- /dev/null
+++ b/library/cpp/testing/common/ut/ya.make
@@ -0,0 +1,19 @@
+GTEST()
+OWNER(
+ amatanhead
+ bulatman
+ thegeorg
+ g:cpp-contrib
+)
+
+SRCS(
+ env_ut.cpp
+ network_ut.cpp
+ scope_ut.cpp
+)
+
+PEERDIR(
+ library/cpp/testing/common
+)
+
+END()