runner.mjs 3.0 KB

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