runner.mjs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import 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. this.ot = spawn('./../OliveTin', ['-configdir', 'configs/' + cfg + '/'])
  27. const logStdout = process.env.OLIVETIN_TEST_RUNNER_LOG_STDOUT === '1'
  28. if (logStdout) {
  29. this.ot.stdout.on('data', (data) => {
  30. console.log(`stdout: ${data}`)
  31. })
  32. this.ot.stderr.on('data', (data) => {
  33. console.error(`stderr: ${data}`)
  34. })
  35. }
  36. this.ot.on('close', (code) => {
  37. if (code != null) {
  38. console.log(`child process exited with code ${code}`)
  39. }
  40. })
  41. /*
  42. this.server = await startSomeServer({port: process.env.TEST_PORT});
  43. console.log(`server running on port ${this.server.port}`);
  44. */
  45. this.BASE_URL = 'http://localhost:1337/'
  46. await waitOn({
  47. resources: [this.BASE_URL]
  48. })
  49. }
  50. async stop () {
  51. await this.ot.kill()
  52. }
  53. }
  54. class OliveTinTestRunnerEnv extends OliveTinTestRunner {
  55. constructor () {
  56. super()
  57. const IP = process.env.IP
  58. const PORT = process.env.PORT
  59. this.BASE_URL = 'http://' + IP + ':' + PORT + '/'
  60. console.log('Runner ENV endpoint: ' + this.BASE_URL)
  61. }
  62. async start () {
  63. await waitOn({
  64. resources: [this.BASE_URL]
  65. })
  66. }
  67. async stop () {
  68. }
  69. }
  70. class OliveTinTestRunnerVm extends OliveTinTestRunnerEnv {
  71. constructor() {
  72. super()
  73. }
  74. async start (cfg) {
  75. console.log("vagrant changing config")
  76. spawn('vagrant', ['ssh', '-c', '"ln -sf /etc/OliveTin/ /opt/otConfigs/' + cfg + '/"'])
  77. spawn('vagrant', ['ssh', '-c', '"systemctl restart OliveTin"'])
  78. return null
  79. }
  80. }