runner.mjs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import * as process from 'node:process'
  2. import waitOn from 'wait-on'
  3. import { spawn } from 'node:child_process'
  4. export default function getRunner () {
  5. const type = process.env.OLIVETIN_TEST_RUNNER
  6. console.log('OLIVETIN_TEST_RUNNER env value is: ', type)
  7. switch (type) {
  8. case 'local':
  9. return new OliveTinTestRunnerStartLocalProcess()
  10. case 'vm':
  11. return new OliveTinTestRunnerVm()
  12. case 'container':
  13. return new OliveTinTestRunnerEnv()
  14. default:
  15. return new OliveTinTestRunnerStartLocalProcess()
  16. }
  17. }
  18. class OliveTinTestRunner {
  19. BASE_URL = 'http://nohost:1337/';
  20. baseUrl() {
  21. return this.BASE_URL
  22. }
  23. metricsUrl() {
  24. return new URL('metrics', this.baseUrl());
  25. }
  26. }
  27. class OliveTinTestRunnerStartLocalProcess extends OliveTinTestRunner {
  28. async start (cfg) {
  29. let stdout = ""
  30. let stderr = ""
  31. console.log(" OliveTin starting local process...")
  32. this.ot = spawn('./../service/OliveTin', ['-configdir', 'tests/' + cfg + '/'], {
  33. env: process.env
  34. })
  35. let logStdout = false
  36. if (process.env.CI === 'true') {
  37. logStdout = true;
  38. } else {
  39. logStdout = process.env.OLIVETIN_TEST_RUNNER_LOG_STDOUT === '1'
  40. }
  41. this.ot.stdout.on('data', (data) => {
  42. stdout += data
  43. if (logStdout) {
  44. console.log(`stdout: ${data}`)
  45. }
  46. })
  47. this.ot.stderr.on('data', (data) => {
  48. stderr += data
  49. if (logStdout) {
  50. console.log(`stderr: ${data}`)
  51. }
  52. })
  53. this.ot.on('close', (code) => {
  54. if (code != null) {
  55. console.log(`OliveTin local process exited with code ${code}`)
  56. console.log(stdout)
  57. console.log(stderr)
  58. console.log(this.ot.exitCode)
  59. }
  60. })
  61. if (this.ot.exitCode == null) {
  62. this.BASE_URL = 'http://localhost:1337/'
  63. console.log(" OliveTin waiting for local process to start...")
  64. await waitOn({
  65. resources: [this.BASE_URL]
  66. })
  67. console.log(" OliveTin local process started and webUI accessible")
  68. } else {
  69. console.log(" OliveTin local process start FAILED!")
  70. console.log(stdout)
  71. console.log(stderr)
  72. console.log(this.ot.exitCode)
  73. }
  74. }
  75. async stop () {
  76. if ((await this.ot.exitCode) != null) {
  77. console.log(" OliveTin local process tried stop(), but it already exited with code", this.ot.exitCode)
  78. } else {
  79. await this.ot.kill()
  80. console.log(" OliveTin local process killed")
  81. }
  82. if (process.env.CI === 'true') {
  83. // GitHub runners seem to need a bit more time to clean up
  84. await new Promise((res) => setTimeout(res, 3000))
  85. } else {
  86. await new Promise((res) => setTimeout(res, 100))
  87. }
  88. }
  89. }
  90. class OliveTinTestRunnerEnv extends OliveTinTestRunner {
  91. constructor () {
  92. super()
  93. const IP = process.env.IP
  94. const PORT = process.env.PORT
  95. this.BASE_URL = 'http://' + IP + ':' + PORT + '/'
  96. console.log('Runner ENV endpoint: ' + this.BASE_URL)
  97. }
  98. async start () {
  99. await waitOn({
  100. resources: [this.BASE_URL]
  101. })
  102. }
  103. async stop () {
  104. }
  105. }
  106. class OliveTinTestRunnerVm extends OliveTinTestRunnerEnv {
  107. constructor() {
  108. super()
  109. }
  110. async start (cfg) {
  111. console.log("vagrant changing config")
  112. spawn('vagrant', ['ssh', '-c', '"ln -sf /etc/OliveTin/ /opt/OliveTin-configs/' + cfg + '/config.yaml"'])
  113. spawn('vagrant', ['ssh', '-c', '"systemctl restart OliveTin"'])
  114. return null
  115. }
  116. }