awsResultsDatastore.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. const Q = require('q');
  2. const debug = require('debug')('ylt:resultsDatastore');
  3. const path = require('path');
  4. const AWS = require('aws-sdk');
  5. function ResultsDatastore() {
  6. 'use strict';
  7. const serverSettings = require('../../../server_config/settings.json');
  8. const s3 = new AWS.S3();
  9. const resultFileName = 'results.json';
  10. const resultScreenshotName = 'screenshot.jpg';
  11. const resultsFolderName = 'results';
  12. this.saveResult = function(testResults) {
  13. const resultFilePath = path.join(resultsFolderName, testResults.runId, resultFileName);
  14. const screenshotFilePath = path.join(resultsFolderName, testResults.runId, resultScreenshotName);
  15. debug('Starting to save screenshot then results.json file on s3...');
  16. return saveScreenshotIfExists(testResults, screenshotFilePath)
  17. .then(function() {
  18. debug('Saving results file to s3, destination is %s', resultFilePath);
  19. return s3PutObject(resultFilePath, JSON.stringify(testResults, null, 2));
  20. });
  21. };
  22. this.getResult = function(runId) {
  23. const resultFilePath = path.join(resultsFolderName, runId, resultFileName);
  24. debug('Reading results (runID = %s) from AWS s3...', runId);
  25. return s3GetObject(resultFilePath).then(function(bodyBuffer) {
  26. return JSON.parse(bodyBuffer.toString('utf-8'));
  27. });
  28. };
  29. // If there is a screenshot, save it as screenshot.jpg in the same folder as the results
  30. function saveScreenshotIfExists(testResults, imagePath) {
  31. var deferred = Q.defer();
  32. if (testResults.screenshotBuffer) {
  33. s3PutObject(imagePath, testResults.screenshotBuffer)
  34. .fail(function() {
  35. debug('Image %s could not be saved on s3. Ignoring.', imagePath);
  36. })
  37. .finally(function() {
  38. delete testResults.screenshotBuffer;
  39. deferred.resolve();
  40. });
  41. } else {
  42. debug('Screenshot not found');
  43. deferred.resolve();
  44. }
  45. return deferred.promise;
  46. }
  47. this.getScreenshot = function(runId) {
  48. const screenshotFilePath = path.join(resultsFolderName, runId, resultScreenshotName);
  49. debug('Retrieving screenshot (runID = %s) from s3...', runId);
  50. return s3GetObject(screenshotFilePath);
  51. };
  52. function s3PutObject(path, body, ignoreError) {
  53. var deferred = Q.defer();
  54. s3.putObject({
  55. Bucket: serverSettings.awsHosting.s3.bucket,
  56. Key: path,
  57. Body: body
  58. }, function(err, data) {
  59. if (err) {
  60. debug('Could not save file %s on s3', path);
  61. debug(err);
  62. deferred.reject('File saving failed on s3');
  63. } else {
  64. debug('File %s saved on s3', path);
  65. deferred.resolve();
  66. }
  67. });
  68. return deferred.promise;
  69. }
  70. function s3GetObject(path) {
  71. var deferred = Q.defer();
  72. s3.getObject({
  73. Bucket: serverSettings.awsHosting.s3.bucket,
  74. Key: path
  75. }, function(err, data) {
  76. if (err) {
  77. debug('Failed retrieving object %s from s3', path);
  78. debug(err);
  79. deferred.reject(err);
  80. } else {
  81. debug('Response for %s received from s3...', path);
  82. deferred.resolve(data.Body);
  83. }
  84. });
  85. return deferred.promise;
  86. }
  87. }
  88. module.exports = ResultsDatastore;