Skip to content

Commit

Permalink
Merge pull request #99 from Shopify/unlink-after-upload
Browse files Browse the repository at this point in the history
Clean up files after uploading to GCS
  • Loading branch information
bmansoob authored Oct 19, 2023
2 parents 22278f7 + da242af commit 08b25f1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 2 additions & 0 deletions lib/app_profiler/storage/google_cloud_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def upload(profile, _params = {})
content_type: "application/json",
content_encoding: "gzip",
)
ensure
profile.file.unlink
end
end

Expand Down
17 changes: 10 additions & 7 deletions test/app_profiler/storage/google_cloud_storage_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ def teardown
end

test "upload file" do
with_mock_gcs_bucket do
uploaded_file = GoogleCloudStorage.upload(stub(context: "context", file: json_test_file))
profile = profile_from_stackprof
with_mock_gcs_bucket(profile) do
uploaded_file = GoogleCloudStorage.upload(profile)
assert_equal(uploaded_file.url, TEST_FILE_URL)
assert_not_predicate(profile.file, :exist?)
end
end

Expand All @@ -43,8 +45,9 @@ def teardown
assert(tags[:file_size].present?)
event_emitted = true
end
with_mock_gcs_bucket do
uploaded_file = GoogleCloudStorage.upload(stub(context: "context", file: json_test_file))
profile = profile_from_stackprof
with_mock_gcs_bucket(profile) do
uploaded_file = GoogleCloudStorage.upload(profile)
assert_equal(uploaded_file.url, TEST_FILE_URL)
assert(event_emitted)
end
Expand Down Expand Up @@ -102,14 +105,14 @@ def json_test_file
file_fixture("test_file.json")
end

def with_mock_gcs_bucket
def with_mock_gcs_bucket(profile)
file = Google::Cloud::Storage::File.new
file.stubs(:url).returns(TEST_FILE_URL)

bucket = Google::Cloud::Storage::Bucket.new
bucket.expects(:create_file).once.with do |input, filename, data|
assert_equal(Zlib::GzipReader.new(input).read, File.open(json_test_file).read)
assert_equal(filename, "context/test_file.json")
assert_equal(Zlib::GzipReader.new(input).read, profile.file.open.read)
assert_equal(filename, GoogleCloudStorage.send(:gcs_filename, profile))
assert_equal(data[:content_type], "application/json")
assert_equal(data[:content_encoding], "gzip")
end.returns(file)
Expand Down

0 comments on commit 08b25f1

Please sign in to comment.