runner.mjs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. }
  24. class OliveTinTestRunnerStartLocalProcess extends OliveTinTestRunner {
  25. async start (cfg) {
  26. let stdout = ""
  27. let stderr = ""
  28. this.ot = spawn('./../OliveTin', ['-configdir', 'configs/' + cfg + '/'])
  29. const logStdout = process.env.OLIVETIN_TEST_RUNNER_LOG_STDOUT === '1'
  30. this.ot.stdout.on('data', (data) => {
  31. stdout += data
  32. if (logStdout) {
  33. console.log(`stdout: ${data}`)
  34. }
  35. })
  36. this.ot.stderr.on('data', (data) => {
  37. stderr += data
  38. if (logStdout) {
  39. console.log(`stderr: ${data}`)
  40. }
  41. })
  42. this.ot.on('close', (code) => {
  43. if (code != null) {
  44. console.log(`OliveTin local process exited with code ${code}`)
  45. console.log(stdout)
  46. console.log(stderr)
  47. console.log(this.ot.exitCode)
  48. }
  49. })
  50. if (this.ot.exitCode == null) {
  51. this.BASE_URL = 'http://localhost:1337/'
  52. await waitOn({
  53. resources: [this.BASE_URL]
  54. })
  55. console.log(" OliveTin local process started and webUI accessible")
  56. } else {
  57. console.log(" OliveTin local process start FAILED!")
  58. console.log(stdout)
  59. console.log(stderr)
  60. console.log(this.ot.exitCode)
  61. }
  62. }
  63. async stop () {
  64. if ((await this.ot.exitCode) != null) {
  65. console.log(" OliveTin local process tried stop(), but it already exited with code", this.ot.exitCode)
  66. } else {
  67. await this.ot.kill()
  68. console.log(" OliveTin local process killed")
  69. }
  70. await new Promise((res) => setTimeout(res, 100))
  71. }
  72. }
  73. class OliveTinTestRunnerEnv extends OliveTinTestRunner {
  74. constructor () {
  75. super()
  76. const IP = process.env.IP
  77. const PORT = process.env.PORT
  78. this.BASE_URL = 'http://' + IP + ':' + PORT + '/'
  79. console.log('Runner ENV endpoint: ' + this.BASE_URL)
  80. }
  81. async start () {
  82. await waitOn({
  83. resources: [this.BASE_URL]
  84. })
  85. }
  86. async stop () {
  87. }
  88. }
  89. class OliveTinTestRunnerVm extends OliveTinTestRunnerEnv {
  90. constructor() {
  91. super()
  92. }
  93. async start (cfg) {
  94. console.log("vagrant changing config")
  95. spawn('vagrant', ['ssh', '-c', '"ln -sf /etc/OliveTin/ /opt/OliveTin-configs/' + cfg + '/config.yaml"'])
  96. spawn('vagrant', ['ssh', '-c', '"systemctl restart OliveTin"'])
  97. return null
  98. }
  99. }