runTest.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. var color = require('cli-color')
  2. var execSync = require('child_process').execSync || require('execSync').exec
  3. var fs = require('fs')
  4. var path = require('path')
  5. /**
  6. * So most of this stuff will be sync, because we are moving
  7. * files around building docker images, etc. These all have
  8. * to be done in a specific order or chaos ensues
  9. */
  10. var cwd = path.join(process.cwd(), 'tests')
  11. process.stdout.write('Fetching Distributions... ')
  12. var distros = fs.readdirSync(cwd)
  13. var failed = []
  14. process.stdout.write('[' + color.green('OK!') + ']\n')
  15. distros.forEach(function (v1) {
  16. if (!fs.statSync(path.join(cwd, v1)).isDirectory()) return
  17. process.stdout.write('Fetching versions of ' + capitalize(v1) + '... ')
  18. var versions = fs.readdirSync(path.join(cwd, v1))
  19. process.stdout.write('[' + color.green('OK!') + ']\n')
  20. versions.forEach(function (v2) {
  21. if (!fs.statSync(path.join(cwd, v1, v2)).isDirectory()) return
  22. // If Dockerfile already exists, delete it.
  23. if (fs.existsSync(path.join(process.cwd(), 'Dockerfile'))) { fs.unlinkSync(path.join(process.cwd(), 'Dockerfile')) }
  24. // Move the dockerfile to the base of our repo
  25. fs.linkSync(path.join(cwd, v1, v2, 'Dockerfile'), path.join(process.cwd(), 'Dockerfile'))
  26. // Build the docker image using the dockerfile
  27. process.stdout.write('Building version ' + v2 + ' of ' + capitalize(v1) + '... ')
  28. try {
  29. var dockerResult = execSync('docker build -t "getos:' + v1 + v2 + '" .', { stdio: [] })
  30. } catch (e) {
  31. dockerResult = dockerResult || {}
  32. dockerResult.code = e
  33. }
  34. if (dockerResult.code && dockerResult.code !== 0) {
  35. failed.push(dockerResult)
  36. process.stdout.write('[' + color.red('FAILED!') + ']\n')
  37. } else {
  38. process.stdout.write('[' + color.green('OK!') + ']\n')
  39. process.stdout.write('Running container... ')
  40. // Show output from distribution
  41. try {
  42. var nodeResult = execSync('docker run -d getos:' + v1 + v2, { stdio: [] })
  43. } catch (e) {
  44. nodeResult = nodeResult || {}
  45. nodeResult.code = e
  46. }
  47. if (nodeResult.code && nodeResult.code !== 0) {
  48. failed.push(nodeResult)
  49. process.stdout.write('[' + color.red('FAILED!') + ']\n')
  50. } else {
  51. try {
  52. var dockerLog = execSync('sleep 2s && docker logs ' + (nodeResult.stdout || nodeResult.toString()), { stdio: [] })
  53. } catch (e) {
  54. dockerLog = dockerLog || {}
  55. dockerLog.code = e
  56. }
  57. if (dockerLog.code && dockerLog.code !== 0) {
  58. failed.push(dockerLog)
  59. process.stdout.write('[' + color.red('FAILED!') + ']\n')
  60. } else {
  61. process.stdout.write('[' + color.green('OK!') + ']\n')
  62. process.stdout.write('Output from version ' + v2 + ' of ' + capitalize(v1) + ': \n')
  63. process.stdout.write(dockerLog.stdout || dockerLog.toString())
  64. }
  65. }
  66. }
  67. // Delete the dockerfile
  68. fs.unlinkSync(path.join(process.cwd(), 'Dockerfile'))
  69. })
  70. })
  71. if (failed.length > 0) {
  72. fs.writeFileSync('tests.log', JSON.stringify(failed, null, ' '))
  73. }
  74. function capitalize (str) {
  75. return str.charAt(0).toUpperCase() + str.slice(1)
  76. }