env.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. TString atdRoot = NPrivate::GetTestEnv().ArcadiaTestsDataDir;
  37. if (atdRoot) {
  38. return atdRoot;
  39. }
  40. TString path = NPrivate::GetCwd();
  41. const char pathsep = GetDirectorySeparator();
  42. while (!path.empty()) {
  43. TString dataDir = path + "/arcadia_tests_data";
  44. if (IsDir(dataDir)) {
  45. return dataDir;
  46. }
  47. size_t pos = path.find_last_of(pathsep);
  48. if (pos == TString::npos) {
  49. pos = 0;
  50. }
  51. path.erase(pos);
  52. }
  53. return {};
  54. }
  55. TString GetWorkPath() {
  56. TString workPath = NPrivate::GetTestEnv().WorkPath;
  57. if (workPath) {
  58. return workPath;
  59. }
  60. return NPrivate::GetCwd();
  61. }
  62. TFsPath GetOutputPath() {
  63. return GetWorkPath() + "/testing_out_stuff";
  64. }
  65. const TString& GetRamDrivePath() {
  66. return NPrivate::GetTestEnv().RamDrivePath;
  67. }
  68. const TString& GetYtHddPath() {
  69. return NPrivate::GetTestEnv().YtHddPath;
  70. }
  71. const TString& GetOutputRamDrivePath() {
  72. return NPrivate::GetTestEnv().TestOutputRamDrivePath;
  73. }
  74. const TString& GdbPath() {
  75. return NPrivate::GetTestEnv().GdbPath;
  76. }
  77. const TString& GetTestParam(TStringBuf name) {
  78. const static TString def = "";
  79. return GetTestParam(name, def);
  80. }
  81. const TString& GetTestParam(TStringBuf name, const TString& def) {
  82. auto& testParameters = NPrivate::GetTestEnv().TestParameters;
  83. auto it = testParameters.find(name.data());
  84. if (it != testParameters.end()) {
  85. return it->second;
  86. }
  87. return def;
  88. }
  89. const TString& GetGlobalResource(TStringBuf name) {
  90. auto& resources = NPrivate::GetTestEnv().GlobalResources;
  91. auto it = resources.find(name.data());
  92. Y_ABORT_UNLESS(it != resources.end());
  93. return it->second;
  94. }
  95. void AddEntryToCoreSearchFile(const TString& filename, TStringBuf cmd, int pid, const TFsPath& binaryPath = TFsPath(), const TFsPath& cwd = TFsPath()) {
  96. auto lock = TFileLock(filename);
  97. TGuard<TFileLock> guard(lock);
  98. TOFStream output(TFile(filename, WrOnly | ForAppend | OpenAlways));
  99. NJson::TJsonWriter writer(&output, false);
  100. writer.OpenMap();
  101. writer.Write("cmd", cmd);
  102. writer.Write("pid", pid);
  103. if (binaryPath) {
  104. writer.Write("binary_path", binaryPath);
  105. }
  106. if (cwd) {
  107. writer.Write("cwd", cwd);
  108. }
  109. writer.CloseMap();
  110. writer.Flush();
  111. output.Write("\n");
  112. }
  113. void WatchProcessCore(int pid, const TFsPath& binaryPath, const TFsPath& cwd) {
  114. auto& filename = NPrivate::GetTestEnv().CoreSearchFile;
  115. if (filename) {
  116. AddEntryToCoreSearchFile(filename, "add", pid, binaryPath, cwd);
  117. }
  118. }
  119. void StopProcessCoreWatching(int pid) {
  120. auto& filename = NPrivate::GetTestEnv().CoreSearchFile;
  121. if (filename) {
  122. AddEntryToCoreSearchFile(filename, "drop", pid);
  123. }
  124. }
  125. bool FromYaTest() {
  126. return NPrivate::GetTestEnv().IsRunningFromTest;
  127. }
  128. namespace NPrivate {
  129. TTestEnv::TTestEnv() {
  130. ReInitialize();
  131. }
  132. void TTestEnv::ReInitialize() {
  133. IsRunningFromTest = false;
  134. ArcadiaTestsDataDir = "";
  135. SourceRoot = "";
  136. BuildRoot = "";
  137. WorkPath = "";
  138. RamDrivePath = "";
  139. YtHddPath = "";
  140. TestOutputRamDrivePath = "";
  141. GdbPath = "";
  142. CoreSearchFile = "";
  143. EnvFile = "";
  144. TestParameters.clear();
  145. GlobalResources.clear();
  146. const TString contextFilename = GetEnv("YA_TEST_CONTEXT_FILE");
  147. if (contextFilename && TFsPath(contextFilename).Exists()) {
  148. NJson::TJsonValue context;
  149. NJson::ReadJsonTree(TFileInput(contextFilename).ReadAll(), &context);
  150. NJson::TJsonValue* value;
  151. value = context.GetValueByPath("runtime.source_root");
  152. if (value) {
  153. SourceRoot = value->GetStringSafe("");
  154. }
  155. value = context.GetValueByPath("runtime.build_root");
  156. if (value) {
  157. BuildRoot = value->GetStringSafe("");
  158. }
  159. value = context.GetValueByPath("runtime.atd_root");
  160. if (value) {
  161. ArcadiaTestsDataDir = value->GetStringSafe("");
  162. }
  163. value = context.GetValueByPath("runtime.work_path");
  164. if (value) {
  165. WorkPath = value->GetStringSafe("");
  166. }
  167. value = context.GetValueByPath("runtime.ram_drive_path");
  168. if (value) {
  169. RamDrivePath = value->GetStringSafe("");
  170. }
  171. value = context.GetValueByPath("runtime.yt_hdd_path");
  172. if (value) {
  173. YtHddPath = value->GetStringSafe("");
  174. }
  175. value = context.GetValueByPath("runtime.test_output_ram_drive_path");
  176. if (value) {
  177. TestOutputRamDrivePath = value->GetStringSafe("");
  178. }
  179. value = context.GetValueByPath("runtime.gdb_bin");
  180. if (value) {
  181. GdbPath = value->GetStringSafe("");
  182. }
  183. value = context.GetValueByPath("runtime.test_params");
  184. if (value) {
  185. for (const auto& entry : context.GetValueByPath("runtime.test_params")->GetMap()) {
  186. TestParameters[entry.first] = entry.second.GetStringSafe("");
  187. }
  188. }
  189. value = context.GetValueByPath("resources.global");
  190. if (value) {
  191. for (const auto& entry : value->GetMap()) {
  192. GlobalResources[entry.first] = entry.second.GetStringSafe("");
  193. }
  194. }
  195. value = context.GetValueByPath("internal.core_search_file");
  196. if (value) {
  197. CoreSearchFile = value->GetStringSafe("");
  198. }
  199. value = context.GetValueByPath("internal.env_file");
  200. if (value) {
  201. EnvFile = value->GetStringSafe("");
  202. if (TFsPath(EnvFile).Exists()) {
  203. TFileInput file(EnvFile);
  204. NJson::TJsonValue envVar;
  205. TString ljson;
  206. while (file.ReadLine(ljson) > 0) {
  207. NJson::ReadJsonTree(ljson, &envVar);
  208. for (const auto& entry : envVar.GetMap()) {
  209. SetEnv(entry.first, entry.second.GetStringSafe(""));
  210. }
  211. }
  212. }
  213. }
  214. }
  215. if (!YtHddPath) {
  216. YtHddPath = GetEnv("HDD_PATH");
  217. }
  218. if (!SourceRoot) {
  219. SourceRoot = GetEnv("ARCADIA_SOURCE_ROOT");
  220. }
  221. if (!BuildRoot) {
  222. BuildRoot = GetEnv("ARCADIA_BUILD_ROOT");
  223. }
  224. if (!ArcadiaTestsDataDir) {
  225. ArcadiaTestsDataDir = GetEnv("ARCADIA_TESTS_DATA_DIR");
  226. }
  227. if (!WorkPath) {
  228. WorkPath = GetEnv("TEST_WORK_PATH");
  229. }
  230. if (!RamDrivePath) {
  231. RamDrivePath = GetEnv("YA_TEST_RAM_DRIVE_PATH");
  232. }
  233. if (!TestOutputRamDrivePath) {
  234. TestOutputRamDrivePath = GetEnv("YA_TEST_OUTPUT_RAM_DRIVE_PATH");
  235. }
  236. const TString fromEnv = GetEnv("YA_TEST_RUNNER");
  237. IsRunningFromTest = (fromEnv == "1");
  238. }
  239. void TTestEnv::AddTestParam(TStringBuf name, TStringBuf value) {
  240. TestParameters[TString{name}] = value;
  241. }
  242. TString GetCwd() {
  243. try {
  244. return NFs::CurrentWorkingDirectory();
  245. } catch (...) {
  246. return {};
  247. }
  248. }
  249. const TTestEnv& GetTestEnv() {
  250. return *Singleton<TTestEnv>();
  251. }
  252. }