TestConfig.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. const fs = require('fs')
  2. const Path = require('path')
  3. // use functions to get a fresh copy, not a reference, each time
  4. function s3Config() {
  5. return {
  6. key: process.env.AWS_ACCESS_KEY_ID,
  7. secret: process.env.AWS_SECRET_ACCESS_KEY,
  8. endpoint: process.env.AWS_S3_ENDPOINT,
  9. pathStyle: true,
  10. partSize: 100 * 1024 * 1024,
  11. }
  12. }
  13. function s3ConfigDefaultProviderCredentials() {
  14. return {
  15. endpoint: process.env.AWS_S3_ENDPOINT,
  16. pathStyle: true,
  17. partSize: 100 * 1024 * 1024,
  18. }
  19. }
  20. function s3Stores() {
  21. return {
  22. user_files: process.env.AWS_S3_USER_FILES_BUCKET_NAME,
  23. template_files: process.env.AWS_S3_TEMPLATE_FILES_BUCKET_NAME,
  24. public_files: process.env.AWS_S3_PUBLIC_FILES_BUCKET_NAME,
  25. }
  26. }
  27. function gcsConfig() {
  28. return {
  29. endpoint: {
  30. apiEndpoint: process.env.GCS_API_ENDPOINT,
  31. projectId: 'fake',
  32. },
  33. directoryKeyRegex: /^[0-9a-fA-F]{24}\/[0-9a-fA-F]{24}/,
  34. unlockBeforeDelete: false, // fake-gcs does not support this
  35. deletedBucketSuffix: '-deleted',
  36. }
  37. }
  38. function gcsStores() {
  39. return {
  40. user_files: process.env.GCS_USER_FILES_BUCKET_NAME,
  41. template_files: process.env.GCS_TEMPLATE_FILES_BUCKET_NAME,
  42. public_files: process.env.GCS_PUBLIC_FILES_BUCKET_NAME,
  43. }
  44. }
  45. function fsStores() {
  46. return {
  47. user_files: Path.resolve(__dirname, '../../../user_files'),
  48. public_files: Path.resolve(__dirname, '../../../public_files'),
  49. template_files: Path.resolve(__dirname, '../../../template_files'),
  50. }
  51. }
  52. function fallbackStores(primaryConfig, fallbackConfig) {
  53. return {
  54. [primaryConfig.user_files]: fallbackConfig.user_files,
  55. [primaryConfig.public_files]: fallbackConfig.public_files,
  56. [primaryConfig.template_files]: fallbackConfig.template_files,
  57. }
  58. }
  59. module.exports = {
  60. SHARD_01_FSPersistor: {
  61. backend: 'fs',
  62. stores: fsStores(),
  63. },
  64. SHARD_01_S3Persistor: {
  65. backend: 's3',
  66. s3: s3Config(),
  67. stores: s3Stores(),
  68. },
  69. SHARD_01_S3PersistorDefaultProviderCredentials: {
  70. backend: 's3',
  71. s3: s3ConfigDefaultProviderCredentials(),
  72. stores: s3Stores(),
  73. },
  74. SHARD_01_GcsPersistor: {
  75. backend: 'gcs',
  76. gcs: gcsConfig(),
  77. stores: gcsStores(),
  78. },
  79. SHARD_02_FallbackS3ToFSPersistor: {
  80. backend: 's3',
  81. s3: s3Config(),
  82. stores: s3Stores(),
  83. fallback: {
  84. backend: 'fs',
  85. buckets: fallbackStores(s3Stores(), fsStores()),
  86. },
  87. },
  88. SHARD_02_FallbackFSToS3Persistor: {
  89. backend: 'fs',
  90. s3: s3Config(),
  91. stores: fsStores(),
  92. fallback: {
  93. backend: 's3',
  94. buckets: fallbackStores(fsStores(), s3Stores()),
  95. },
  96. },
  97. SHARD_03_FallbackGcsToS3Persistor: {
  98. backend: 'gcs',
  99. gcs: gcsConfig(),
  100. stores: gcsStores(),
  101. s3: s3Config(),
  102. fallback: {
  103. backend: 's3',
  104. buckets: fallbackStores(gcsStores(), s3Stores()),
  105. },
  106. },
  107. SHARD_03_FallbackS3ToGcsPersistor: {
  108. backend: 's3',
  109. // can use the same bucket names for gcs and s3 (in tests)
  110. stores: s3Stores(),
  111. s3: s3Config(),
  112. gcs: gcsConfig(),
  113. fallback: {
  114. backend: 'gcs',
  115. buckets: fallbackStores(s3Stores(), gcsStores()),
  116. },
  117. },
  118. }
  119. function checkForUnexpectedTestFile() {
  120. const awareOfSharding = [
  121. 'FilestoreApp.js',
  122. 'FilestoreTests.js',
  123. 'TestConfig.js',
  124. 'TestHelper.js',
  125. ]
  126. for (const file of fs.readdirSync(__dirname).sort()) {
  127. if (!awareOfSharding.includes(file)) {
  128. throw new Error(
  129. `Found new test file ${file}: All tests must be aware of the SHARD_ prefix.`
  130. )
  131. }
  132. }
  133. }
  134. checkForUnexpectedTestFile()