env.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #include "env.h"
  2. #include <build/scripts/c_templates/svnversion.h>
  3. #include <util/folder/dirut.h>
  4. #include <util/folder/path.h>
  5. #include <util/generic/singleton.h>
  6. #include <util/stream/file.h>
  7. #include <util/stream/fwd.h>
  8. #include <util/system/env.h>
  9. #include <util/system/file.h>
  10. #include <util/system/file_lock.h>
  11. #include <util/system/guard.h>
  12. #include <library/cpp/json/json_reader.h>
  13. #include <library/cpp/json/json_value.h>
  14. #include <library/cpp/json/json_writer.h>
  15. TString ArcadiaSourceRoot() {
  16. if (const auto& sourceRoot = NPrivate::GetTestEnv().SourceRoot) {
  17. return sourceRoot;
  18. } else {
  19. return GetArcadiaSourcePath();
  20. }
  21. }
  22. TString BuildRoot() {
  23. if (const auto& buildRoot = NPrivate::GetTestEnv().BuildRoot) {
  24. return buildRoot;
  25. } else {
  26. return GetArcadiaSourcePath();
  27. }
  28. }
  29. TString ArcadiaFromCurrentLocation(TStringBuf where, TStringBuf path) {
  30. return (TFsPath(ArcadiaSourceRoot()) / TFsPath(where).Parent() / path).Fix();
  31. }
  32. TString BinaryPath(TStringBuf path) {
  33. return (TFsPath(BuildRoot()) / path).Fix();
  34. }
  35. TString GetArcadiaTestsData() {
  36. return ArcadiaSourceRoot() + "/atd_ro_snapshot";
  37. }
  38. TString GetWorkPath() {
  39. TString workPath = NPrivate::GetTestEnv().WorkPath;
  40. if (workPath) {
  41. return workPath;
  42. }
  43. return NPrivate::GetCwd();
  44. }
  45. TFsPath GetOutputPath() {
  46. return GetWorkPath() + "/testing_out_stuff";
  47. }
  48. const TString& GetRamDrivePath() {
  49. return NPrivate::GetTestEnv().RamDrivePath;
  50. }
  51. const TString& GetYtHddPath() {
  52. return NPrivate::GetTestEnv().YtHddPath;
  53. }
  54. const TString& GetOutputRamDrivePath() {
  55. return NPrivate::GetTestEnv().TestOutputRamDrivePath;
  56. }
  57. const TString& GdbPath() {
  58. return NPrivate::GetTestEnv().GdbPath;
  59. }
  60. const TString& GetTestParam(TStringBuf name) {
  61. const static TString def = "";
  62. return GetTestParam(name, def);
  63. }
  64. const TString& GetTestParam(TStringBuf name, const TString& def) {
  65. auto& testParameters = NPrivate::GetTestEnv().TestParameters;
  66. auto it = testParameters.find(name.data());
  67. if (it != testParameters.end()) {
  68. return it->second;
  69. }
  70. return def;
  71. }
  72. const TString& GetGlobalResource(TStringBuf name) {
  73. auto& resources = NPrivate::GetTestEnv().GlobalResources;
  74. auto it = resources.find(name.data());
  75. Y_ABORT_UNLESS(it != resources.end());
  76. return it->second;
  77. }
  78. void AddEntryToCoreSearchFile(const TString& filename, TStringBuf cmd, int pid, const TFsPath& binaryPath = TFsPath(), const TFsPath& cwd = TFsPath()) {
  79. auto lock = TFileLock(filename);
  80. TGuard<TFileLock> guard(lock);
  81. TOFStream output(TFile(filename, WrOnly | ForAppend | OpenAlways));
  82. NJson::TJsonWriter writer(&output, false);
  83. writer.OpenMap();
  84. writer.Write("cmd", cmd);
  85. writer.Write("pid", pid);
  86. if (binaryPath) {
  87. writer.Write("binary_path", binaryPath);
  88. }
  89. if (cwd) {
  90. writer.Write("cwd", cwd);
  91. }
  92. writer.CloseMap();
  93. writer.Flush();
  94. output.Write("\n");
  95. }
  96. void WatchProcessCore(int pid, const TFsPath& binaryPath, const TFsPath& cwd) {
  97. auto& filename = NPrivate::GetTestEnv().CoreSearchFile;
  98. if (filename) {
  99. AddEntryToCoreSearchFile(filename, "add", pid, binaryPath, cwd);
  100. }
  101. }
  102. void StopProcessCoreWatching(int pid) {
  103. auto& filename = NPrivate::GetTestEnv().CoreSearchFile;
  104. if (filename) {
  105. AddEntryToCoreSearchFile(filename, "drop", pid);
  106. }
  107. }
  108. bool FromYaTest() {
  109. return NPrivate::GetTestEnv().IsRunningFromTest;
  110. }
  111. namespace NPrivate {
  112. TTestEnv::TTestEnv() {
  113. ReInitialize();
  114. }
  115. void TTestEnv::ReInitialize() {
  116. IsRunningFromTest = false;
  117. ArcadiaTestsDataDir = "";
  118. SourceRoot = "";
  119. BuildRoot = "";
  120. WorkPath = "";
  121. RamDrivePath = "";
  122. YtHddPath = "";
  123. TestOutputRamDrivePath = "";
  124. GdbPath = "";
  125. CoreSearchFile = "";
  126. EnvFile = "";
  127. TestParameters.clear();
  128. GlobalResources.clear();
  129. const TString contextFilename = GetEnv("YA_TEST_CONTEXT_FILE");
  130. if (contextFilename && TFsPath(contextFilename).Exists()) {
  131. NJson::TJsonValue context;
  132. NJson::ReadJsonTree(TFileInput(contextFilename).ReadAll(), &context);
  133. NJson::TJsonValue* value;
  134. value = context.GetValueByPath("runtime.source_root");
  135. if (value) {
  136. SourceRoot = value->GetStringSafe("");
  137. }
  138. value = context.GetValueByPath("runtime.build_root");
  139. if (value) {
  140. BuildRoot = value->GetStringSafe("");
  141. }
  142. value = context.GetValueByPath("runtime.atd_root");
  143. if (value) {
  144. ArcadiaTestsDataDir = value->GetStringSafe("");
  145. }
  146. value = context.GetValueByPath("runtime.work_path");
  147. if (value) {
  148. WorkPath = value->GetStringSafe("");
  149. }
  150. value = context.GetValueByPath("runtime.ram_drive_path");
  151. if (value) {
  152. RamDrivePath = value->GetStringSafe("");
  153. }
  154. value = context.GetValueByPath("runtime.yt_hdd_path");
  155. if (value) {
  156. YtHddPath = value->GetStringSafe("");
  157. }
  158. value = context.GetValueByPath("runtime.test_output_ram_drive_path");
  159. if (value) {
  160. TestOutputRamDrivePath = value->GetStringSafe("");
  161. }
  162. value = context.GetValueByPath("runtime.gdb_bin");
  163. if (value) {
  164. GdbPath = value->GetStringSafe("");
  165. }
  166. value = context.GetValueByPath("runtime.test_params");
  167. if (value) {
  168. for (const auto& entry : context.GetValueByPath("runtime.test_params")->GetMap()) {
  169. TestParameters[entry.first] = entry.second.GetStringSafe("");
  170. }
  171. }
  172. value = context.GetValueByPath("resources.global");
  173. if (value) {
  174. for (const auto& entry : value->GetMap()) {
  175. GlobalResources[entry.first] = entry.second.GetStringSafe("");
  176. }
  177. }
  178. value = context.GetValueByPath("internal.core_search_file");
  179. if (value) {
  180. CoreSearchFile = value->GetStringSafe("");
  181. }
  182. value = context.GetValueByPath("internal.env_file");
  183. if (value) {
  184. EnvFile = value->GetStringSafe("");
  185. if (TFsPath(EnvFile).Exists()) {
  186. TFileInput file(EnvFile);
  187. NJson::TJsonValue envVar;
  188. TString ljson;
  189. while (file.ReadLine(ljson) > 0) {
  190. NJson::ReadJsonTree(ljson, &envVar);
  191. for (const auto& entry : envVar.GetMap()) {
  192. SetEnv(entry.first, entry.second.GetStringSafe(""));
  193. }
  194. }
  195. }
  196. }
  197. }
  198. if (!YtHddPath) {
  199. YtHddPath = GetEnv("HDD_PATH");
  200. }
  201. if (!SourceRoot) {
  202. SourceRoot = GetEnv("ARCADIA_SOURCE_ROOT");
  203. }
  204. if (!BuildRoot) {
  205. BuildRoot = GetEnv("ARCADIA_BUILD_ROOT");
  206. }
  207. if (!ArcadiaTestsDataDir) {
  208. ArcadiaTestsDataDir = GetEnv("ARCADIA_TESTS_DATA_DIR");
  209. }
  210. if (!WorkPath) {
  211. WorkPath = GetEnv("TEST_WORK_PATH");
  212. }
  213. if (!RamDrivePath) {
  214. RamDrivePath = GetEnv("YA_TEST_RAM_DRIVE_PATH");
  215. }
  216. if (!TestOutputRamDrivePath) {
  217. TestOutputRamDrivePath = GetEnv("YA_TEST_OUTPUT_RAM_DRIVE_PATH");
  218. }
  219. const TString fromEnv = GetEnv("YA_TEST_RUNNER");
  220. IsRunningFromTest = (fromEnv == "1");
  221. }
  222. void TTestEnv::AddTestParam(TStringBuf name, TStringBuf value) {
  223. TestParameters[TString{name}] = value;
  224. }
  225. TString GetCwd() {
  226. try {
  227. return NFs::CurrentWorkingDirectory();
  228. } catch (...) {
  229. return {};
  230. }
  231. }
  232. const TTestEnv& GetTestEnv() {
  233. return *Singleton<TTestEnv>();
  234. }
  235. }