Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 32 additions & 17 deletions spec/services/s3_backup_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@
let(:compressed_filename) { "database-#{timestamp}.tar.gz" }
let(:s3_key) { "db_backups/#{compressed_filename}" }

# Helper method to mock tar compression and create the compressed file
def mock_tar_compression(service, content_size: 1000)
allow(service).to receive(:system).with(
"tar", "-czf", anything,
"-C", anything, anything,
exception: true
) do |*args|
# Extract the destination path from the tar command arguments
dest_path = args[2]
# Create the compressed file that would be created by tar
FileUtils.touch(dest_path)
File.write(dest_path, "compressed content" * content_size)
true
end
end

before do
allow(Time).to receive(:current).and_return(Time.zone.parse("2024-01-15 10:00:00"))
allow(ENV).to receive(:[]).and_call_original
Expand Down Expand Up @@ -86,15 +102,7 @@
).and_return(true)

# Mock tar compression
allow(service).to receive(:system).with(
"tar", "-czf", anything,
"-C", anything, anything,
exception: true
).and_return(true)

# Create mock compressed file
FileUtils.touch(temp_compressed_path)
File.write(temp_compressed_path, "compressed content" * 1000)
mock_tar_compression(service)

# Mock S3 upload
allow(s3_service).to receive(:upload)
Expand Down Expand Up @@ -338,13 +346,8 @@
exception: true
).and_return(true)

allow(service).to receive(:system).with(
"tar", "-czf", /database-2025-12-31\.tar\.gz/,
"-C", anything, "database-2025-12-31.sqlite3",
exception: true
).and_return(true)

FileUtils.touch(temp_compressed_path)
# Mock tar compression
mock_tar_compression(service)
allow(s3_service).to receive(:upload)
allow(bucket).to receive(:objects).and_return([])

Expand All @@ -358,8 +361,20 @@
let(:temp_compressed_path) { temp_dir.join(compressed_filename) }

before do
# Default stub for any system calls
allow(service).to receive(:system).and_return(true)
FileUtils.touch(temp_compressed_path)

# Mock database backup creation
allow(service).to receive(:system).with(
"sqlite3",
anything,
/\.backup/,
exception: true
).and_return(true)

# Mock tar compression
mock_tar_compression(service)

allow(s3_service).to receive(:upload)
end

Expand Down
Loading