env_ut.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include <library/cpp/testing/common/env.h>
  2. #include <library/cpp/testing/common/scope.h>
  3. #include <library/cpp/testing/gtest/gtest.h>
  4. #include <util/folder/dirut.h>
  5. #include <util/stream/file.h>
  6. #include <util/system/env.h>
  7. #include <util/system/execpath.h>
  8. #include <util/system/fs.h>
  9. TEST(Runtime, ArcadiaSourceRoot) {
  10. NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename
  11. {
  12. auto tmpDir = ::GetSystemTempDir();
  13. NTesting::TScopedEnvironment guard("ARCADIA_SOURCE_ROOT", tmpDir);
  14. Singleton<NPrivate::TTestEnv>()->ReInitialize();
  15. EXPECT_EQ(tmpDir, ArcadiaSourceRoot());
  16. }
  17. {
  18. NTesting::TScopedEnvironment guard("ARCADIA_SOURCE_ROOT", "");
  19. Singleton<NPrivate::TTestEnv>()->ReInitialize();
  20. EXPECT_FALSE(ArcadiaSourceRoot().empty());
  21. }
  22. }
  23. TEST(Runtime, BuildRoot) {
  24. NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename
  25. {
  26. auto tmpDir = ::GetSystemTempDir();
  27. NTesting::TScopedEnvironment guard("ARCADIA_BUILD_ROOT", tmpDir);
  28. Singleton<NPrivate::TTestEnv>()->ReInitialize();
  29. EXPECT_EQ(tmpDir, BuildRoot());
  30. }
  31. {
  32. NTesting::TScopedEnvironment guard("ARCADIA_BUILD_ROOT", "");
  33. Singleton<NPrivate::TTestEnv>()->ReInitialize();
  34. EXPECT_FALSE(BuildRoot().empty());
  35. }
  36. }
  37. TEST(Runtime, BinaryPath) {
  38. NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename
  39. Singleton<NPrivate::TTestEnv>()->ReInitialize();
  40. EXPECT_TRUE(TFsPath(BinaryPath("library/cpp/testing/common/ut")).Exists());
  41. }
  42. TEST(Runtime, GetWorkPath) {
  43. NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename
  44. {
  45. auto tmpDir = ::GetSystemTempDir();
  46. NTesting::TScopedEnvironment guard("TEST_WORK_PATH", tmpDir);
  47. Singleton<NPrivate::TTestEnv>()->ReInitialize();
  48. EXPECT_EQ(tmpDir, GetWorkPath());
  49. }
  50. {
  51. NTesting::TScopedEnvironment guard("TEST_WORK_PATH", "");
  52. Singleton<NPrivate::TTestEnv>()->ReInitialize();
  53. EXPECT_TRUE(!GetWorkPath().empty());
  54. }
  55. }
  56. TEST(Runtime, GetOutputPath) {
  57. NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename
  58. Singleton<NPrivate::TTestEnv>()->ReInitialize();
  59. EXPECT_EQ(GetOutputPath().Basename(), "testing_out_stuff");
  60. }
  61. TEST(Runtime, GetRamDrivePath) {
  62. NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename
  63. auto tmpDir = ::GetSystemTempDir();
  64. NTesting::TScopedEnvironment guard("YA_TEST_RAM_DRIVE_PATH", tmpDir);
  65. Singleton<NPrivate::TTestEnv>()->ReInitialize();
  66. EXPECT_EQ(tmpDir, GetRamDrivePath());
  67. }
  68. TEST(Runtime, GetOutputRamDrivePath) {
  69. NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename
  70. auto tmpDir = ::GetSystemTempDir();
  71. NTesting::TScopedEnvironment guard("YA_TEST_OUTPUT_RAM_DRIVE_PATH", tmpDir);
  72. Singleton<NPrivate::TTestEnv>()->ReInitialize();
  73. EXPECT_EQ(tmpDir, GetOutputRamDrivePath());
  74. }
  75. #ifdef _linux_
  76. TEST(Runtime, GdbPath) {
  77. Singleton<NPrivate::TTestEnv>()->ReInitialize();
  78. EXPECT_TRUE(NFs::Exists(::GdbPath()));
  79. }
  80. #endif
  81. TString ReInitializeContext(TStringBuf data) {
  82. auto tmpDir = ::GetSystemTempDir();
  83. auto filename = tmpDir + "/context.json";
  84. TOFStream stream(filename);
  85. stream.Write(data.data(), data.size());
  86. stream.Finish();
  87. NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", filename);
  88. Singleton<NPrivate::TTestEnv>()->ReInitialize();
  89. return filename;
  90. }
  91. TEST(Runtime, GetTestParam) {
  92. TString context = R"json({
  93. "runtime": {
  94. "test_params": {
  95. "a": "b",
  96. "c": "d"
  97. }
  98. }
  99. })json";
  100. auto filename = ReInitializeContext(context);
  101. EXPECT_EQ("b", GetTestParam("a"));
  102. EXPECT_EQ("d", GetTestParam("c"));
  103. EXPECT_EQ("", GetTestParam("e"));
  104. EXPECT_EQ("w", GetTestParam("e", "w"));
  105. Singleton<NPrivate::TTestEnv>()->AddTestParam("e", "e");
  106. EXPECT_EQ("e", GetTestParam("e"));
  107. }
  108. TEST(Runtime, WatchProcessCore) {
  109. TString context = R"json({
  110. "internal": {
  111. "core_search_file": "watch_core.txt"
  112. }
  113. })json";
  114. auto filename = ReInitializeContext(context);
  115. WatchProcessCore(1, "bin1", "pwd");
  116. WatchProcessCore(2, "bin1");
  117. StopProcessCoreWatching(2);
  118. TIFStream file("watch_core.txt");
  119. auto data = file.ReadAll();
  120. TString expected = R"json({"cmd":"add","pid":1,"binary_path":"bin1","cwd":"pwd"}
  121. {"cmd":"add","pid":2,"binary_path":"bin1"}
  122. {"cmd":"drop","pid":2}
  123. )json";
  124. EXPECT_EQ(expected, data);
  125. }
  126. TEST(Runtime, GlobalResources) {
  127. TString context = R"json({
  128. "resources": {
  129. "global": {
  130. "TOOL_NAME_RESOURCE_GLOBAL": "path"
  131. }
  132. }
  133. })json";
  134. auto filename = ReInitializeContext(context);
  135. EXPECT_EQ("path", GetGlobalResource("TOOL_NAME_RESOURCE_GLOBAL"));
  136. }