env.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // Package yatest provides access to testing context, when running under ya make -t.
  2. package yatest
  3. import (
  4. "bufio"
  5. "encoding/json"
  6. "fmt"
  7. "os"
  8. "path"
  9. "path/filepath"
  10. "runtime"
  11. "sync"
  12. )
  13. type TestContext struct {
  14. Build struct {
  15. BuildType string `json:"build_type"`
  16. Flags map[string]string `json:"flags"`
  17. Sanitizer string `json:"sanitizer"`
  18. } `json:"build"`
  19. Runtime struct {
  20. BuildRoot string `json:"build_root"`
  21. OutputPath string `json:"output_path"`
  22. ProjectPath string `json:"project_path"`
  23. PythonBin string `json:"python_bin"`
  24. PythonLibPath string `json:"python_lib_path"`
  25. RAMDrivePath string `json:"ram_drive_path"`
  26. YtHDDPath string `json:"yt_hdd_path"`
  27. TestOutputRAMDrivePath string `json:"test_output_ram_drive_path"`
  28. SourceRoot string `json:"source_root"`
  29. WorkPath string `json:"work_path"`
  30. TestToolPath string `json:"test_tool_path"`
  31. TestParameters map[string]string `json:"test_params"`
  32. } `json:"runtime"`
  33. Resources struct {
  34. Global map[string]string `json:"global"`
  35. } `json:"resources"`
  36. Internal struct {
  37. EnvFile string `json:"env_file"`
  38. } `json:"internal"`
  39. Initialized bool
  40. }
  41. var (
  42. context TestContext
  43. isRunningUnderGoTest bool
  44. initOnce sync.Once
  45. )
  46. func lazyInit() {
  47. initOnce.Do(doInit)
  48. }
  49. func verifyContext() {
  50. if !context.Initialized {
  51. panic("test context isn't initialized")
  52. }
  53. }
  54. func initTestContext() {
  55. data, err := os.ReadFile(getenv("YA_TEST_CONTEXT_FILE"))
  56. if err != nil {
  57. panic(err)
  58. }
  59. err = json.Unmarshal([]byte(data), &context)
  60. if err != nil {
  61. panic(err)
  62. }
  63. setupEnv()
  64. context.Initialized = true
  65. }
  66. func setupEnv() {
  67. file, err := os.Open(context.Internal.EnvFile)
  68. if err != nil {
  69. return
  70. }
  71. defer file.Close()
  72. scanner := bufio.NewScanner(file)
  73. for scanner.Scan() {
  74. line := scanner.Text()
  75. var objmap map[string]json.RawMessage
  76. var val string
  77. err := json.Unmarshal([]byte(line), &objmap)
  78. if err != nil {
  79. panic(err)
  80. }
  81. for k, v := range objmap {
  82. err := json.Unmarshal(v, &val)
  83. if err != nil {
  84. panic(err)
  85. }
  86. err = os.Setenv(k, val)
  87. if err != nil {
  88. panic(err)
  89. }
  90. }
  91. }
  92. }
  93. func HasYaTestContext() bool {
  94. lazyInit()
  95. return context.Initialized
  96. }
  97. // GlobalResourcePath returns absolute path to a directory
  98. // containing global build resource.
  99. func GlobalResourcePath(name string) string {
  100. resource, ok := RelaxedGlobalResourcePath(name)
  101. if !ok {
  102. panic(fmt.Sprintf("global resource %s is not defined", name))
  103. }
  104. return resource
  105. }
  106. func RelaxedGlobalResourcePath(name string) (string, bool) {
  107. lazyInit()
  108. verifyContext()
  109. resource, ok := context.Resources.Global[name]
  110. return resource, ok
  111. }
  112. func getenv(name string) string {
  113. value := os.Getenv(name)
  114. if value == "" {
  115. panic(fmt.Sprintf("environment variable %s is not set", name))
  116. }
  117. return value
  118. }
  119. func CCompilerPath() string {
  120. lazyInit()
  121. return getenv("YA_CC")
  122. }
  123. func CxxCompilerPath() string {
  124. lazyInit()
  125. return getenv("YA_CXX")
  126. }
  127. // PythonBinPath returns absolute path to the python
  128. //
  129. // Warn: if you are using build with system python (-DUSE_SYSTEM_PYTHON=X) beware that some python bundles
  130. // are built in a stripped-down form that is needed for building, not running tests.
  131. // See comments in the file below to find out which version of python is compatible with tests.
  132. // https://a.yandex-team.ru/arc/trunk/arcadia/build/platform/python/resources.inc
  133. func PythonBinPath() string {
  134. lazyInit()
  135. verifyContext()
  136. return context.Runtime.PythonBin
  137. }
  138. func PythonLibPath() string {
  139. lazyInit()
  140. verifyContext()
  141. return context.Runtime.PythonLibPath
  142. }
  143. // SourcePath returns absolute path to source directory.
  144. //
  145. // arcadiaPath must be declared using DATA macro inside ya.make.
  146. func SourcePath(arcadiaPath string) string {
  147. lazyInit()
  148. if path.IsAbs(arcadiaPath) {
  149. panic(fmt.Sprintf("relative path expected, but got %q", arcadiaPath))
  150. }
  151. // Don't verify context for SourcePath - it can be mined without context
  152. return filepath.Join(context.Runtime.SourceRoot, arcadiaPath)
  153. }
  154. // BuildPath returns absolute path to the build directory.
  155. func BuildPath(dataPath string) string {
  156. lazyInit()
  157. if path.IsAbs(dataPath) {
  158. panic(fmt.Sprintf("relative path expected, but got %q", dataPath))
  159. }
  160. verifyContext()
  161. return filepath.Join(context.Runtime.BuildRoot, dataPath)
  162. }
  163. // WorkPath returns absolute path to the work directory (initial test cwd).
  164. func WorkPath(dataPath string) string {
  165. lazyInit()
  166. if path.IsAbs(dataPath) {
  167. panic(fmt.Sprintf("relative path expected, but got %q", dataPath))
  168. }
  169. verifyContext()
  170. return filepath.Join(context.Runtime.WorkPath, dataPath)
  171. }
  172. // OutputPath returns absolute path to the output directory (testing_out_stuff).
  173. func OutputPath(dataPath string) string {
  174. lazyInit()
  175. verifyContext()
  176. return filepath.Join(context.Runtime.OutputPath, dataPath)
  177. }
  178. // RAMDrivePath returns absolute path to the ramdrive directory
  179. func RAMDrivePath(dataPath string) string {
  180. lazyInit()
  181. if path.IsAbs(dataPath) {
  182. panic(fmt.Sprintf("relative path expected, but got %q", dataPath))
  183. }
  184. verifyContext()
  185. return filepath.Join(context.Runtime.RAMDrivePath, dataPath)
  186. }
  187. // YtHDDPath returns absolute path to the directory mounted to ext4 fs in YT
  188. func YtHDDPath(dataPath string) string {
  189. lazyInit()
  190. if path.IsAbs(dataPath) {
  191. panic(fmt.Sprintf("relative path expected, but got %q", dataPath))
  192. }
  193. verifyContext()
  194. return filepath.Join(context.Runtime.YtHDDPath, dataPath)
  195. }
  196. // OutputRAMDrivePath returns absolute path to the ramdrive output directory
  197. func OutputRAMDrivePath(dataPath string) string {
  198. lazyInit()
  199. if path.IsAbs(dataPath) {
  200. panic(fmt.Sprintf("relative path expected, but got %q", dataPath))
  201. }
  202. verifyContext()
  203. return filepath.Join(context.Runtime.TestOutputRAMDrivePath, dataPath)
  204. }
  205. // HasRAMDrive returns true if ramdrive is enabled in tests
  206. func HasRAMDrive() bool {
  207. lazyInit()
  208. verifyContext()
  209. return context.Runtime.RAMDrivePath != ""
  210. }
  211. func BinaryPath(dataPath string) (string, error) {
  212. if runtime.GOOS == "windows" {
  213. dataPath += ".exe"
  214. }
  215. buildPath := BuildPath("")
  216. binaryPath := filepath.Join(buildPath, dataPath)
  217. if _, err := os.Stat(binaryPath); os.IsNotExist(err) {
  218. return "", fmt.Errorf("cannot find binary %s: make sure it was added in the DEPENDS section", dataPath)
  219. } else {
  220. return binaryPath, nil
  221. }
  222. }
  223. // ProjectPath returns arcadia-relative path to the test project
  224. func ProjectPath() string {
  225. lazyInit()
  226. verifyContext()
  227. return context.Runtime.ProjectPath
  228. }
  229. // BuildType returns build type that was used to compile the test
  230. func BuildType() string {
  231. lazyInit()
  232. verifyContext()
  233. return context.Build.BuildType
  234. }
  235. // BuildFlag returns the value of the requested build flag
  236. func BuildFlag(name string) (string, bool) {
  237. lazyInit()
  238. verifyContext()
  239. val, ok := context.Build.Flags[name]
  240. return val, ok
  241. }
  242. // TestParam returns the value of the requested test parameter
  243. func TestParam(name string) (string, bool) {
  244. lazyInit()
  245. verifyContext()
  246. val, ok := context.Runtime.TestParameters[name]
  247. return val, ok
  248. }
  249. // Sanitizer returns sanitizer name that was used to compile the test
  250. func Sanitizer() string {
  251. lazyInit()
  252. verifyContext()
  253. return context.Build.Sanitizer
  254. }
  255. func EnvFile() string {
  256. lazyInit()
  257. verifyContext()
  258. return context.Internal.EnvFile
  259. }
  260. func TestToolPath() string {
  261. lazyInit()
  262. verifyContext()
  263. return context.Runtime.TestToolPath
  264. }