relocate_unprocessable_mails_spec.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. UNPROCESSABLE_DIR_OLD = Rails.root.join('tmp/unprocessable_mail')
  4. UNPROCESSABLE_DIR_NEW = Rails.root.join('var/spool/unprocessable_mail')
  5. OVERSIZED_DIR_OLD = Rails.root.join('tmp/oversized_mail')
  6. OVERSIZED_DIR_NEW = Rails.root.join('var/spool/oversized_mail')
  7. OLD_DIRS = [UNPROCESSABLE_DIR_OLD, OVERSIZED_DIR_OLD].freeze
  8. NEW_DIRS = [UNPROCESSABLE_DIR_NEW, OVERSIZED_DIR_NEW].freeze
  9. DIRS = [UNPROCESSABLE_DIR_OLD, UNPROCESSABLE_DIR_NEW, OVERSIZED_DIR_OLD, OVERSIZED_DIR_NEW].freeze
  10. VAR_DIR = Rails.root.join('var')
  11. RSpec.describe RelocateUnprocessableMails, :aggregate_failures, type: :db_migration do
  12. def remove_all_directories
  13. [*DIRS, VAR_DIR].each { |dir| FileUtils.rm_r(dir) if File.exist?(dir) }
  14. end
  15. def create_old_directories
  16. OLD_DIRS.each { |dir| FileUtils.mkdir_p(dir) }
  17. end
  18. def create_all_directories
  19. DIRS.each { |dir| FileUtils.mkdir_p(dir) }
  20. end
  21. before do
  22. remove_all_directories
  23. end
  24. after do
  25. remove_all_directories
  26. end
  27. context 'when migrating' do
  28. context 'without unprocessable mail directory' do
  29. it 'does nothing' do
  30. migrate
  31. DIRS.each { |dir| expect(dir).not_to exist }
  32. end
  33. end
  34. context 'with empty unprocessable mail directory' do
  35. before do
  36. create_old_directories
  37. end
  38. it 'does nothing' do
  39. migrate
  40. expect(OLD_DIRS).to all(exist)
  41. NEW_DIRS.each { |dir| expect(dir).not_to exist }
  42. end
  43. end
  44. context 'with unprocessable mails present' do
  45. before do
  46. create_old_directories
  47. %w[unprocessable_mail oversized_mail].each do |type|
  48. files = [
  49. "tmp/#{type}/1.eml",
  50. "tmp/#{type}/2.eml",
  51. ]
  52. FileUtils.touch(files.map { |f| Rails.root.join(f) })
  53. end
  54. end
  55. context 'with var folder being not creatable' do
  56. before do
  57. # Fake a situation with a readonly FS where the var folder cannot be created by
  58. # placing a reguar 'var' file instead. Migration must tolerate this and skip.
  59. FileUtils.touch(VAR_DIR)
  60. end
  61. after do
  62. # Remove the regular 'var' file again.
  63. FileUtils.rm(VAR_DIR)
  64. end
  65. it 'silently skips the migration' do
  66. migrate
  67. expect([UNPROCESSABLE_DIR_OLD, OVERSIZED_DIR_OLD]).to all(exist)
  68. end
  69. end
  70. context 'with var folder being creatable' do
  71. it 'removes source directories' do
  72. migrate
  73. [UNPROCESSABLE_DIR_OLD, OVERSIZED_DIR_OLD].each { |dir| expect(dir).not_to exist }
  74. end
  75. it 'migrates files' do
  76. migrate
  77. %w[unprocessable_mail oversized_mail].each do |type|
  78. files = [
  79. "var/spool/#{type}/1.eml",
  80. "var/spool/#{type}/2.eml",
  81. ]
  82. files.each { |f| expect(Pathname.new(f)).to exist }
  83. end
  84. end
  85. end
  86. context 'with var folder being already present and having entries' do
  87. before do
  88. create_all_directories
  89. %w[unprocessable_mail oversized_mail].each do |type|
  90. files = [
  91. "var/spool/#{type}/2.eml",
  92. "var/spool/#{type}/3.eml",
  93. ]
  94. FileUtils.touch(files.map { |f| Rails.root.join(f) })
  95. end
  96. end
  97. it 'migrates files and keeps existing' do
  98. migrate
  99. %w[unprocessable_mail oversized_mail].each do |type|
  100. files = [
  101. "var/spool/#{type}/1.eml",
  102. "var/spool/#{type}/2.eml",
  103. "var/spool/#{type}/3.eml",
  104. ]
  105. files.each { |f| expect(Pathname.new(f)).to exist }
  106. end
  107. end
  108. end
  109. end
  110. end
  111. end