Add /tmp/ to .gitignore and simplify s3 cleanups in tests (#3889)

This commit is contained in:
ruslandoga 2024-03-13 00:58:05 +08:00 committed by GitHub
parent 5a3072ca21
commit f2350b5165
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 26 deletions

3
.gitignore vendored
View File

@ -19,6 +19,9 @@ erl_crash.dump
# Also ignore archive artifacts (built via "mix archive.build"). # Also ignore archive artifacts (built via "mix archive.build").
*.ez *.ez
# Temporary files, for example, from tests.
/tmp/
# Ignore package tarball (built via "mix hex.build"). # Ignore package tarball (built via "mix hex.build").
plausible-*.tar plausible-*.tar

View File

@ -15,11 +15,30 @@ defmodule Plausible.Imported.CSVImporterTest do
s3 = fn op -> ExAws.request!(op, connection_opts) end s3 = fn op -> ExAws.request!(op, connection_opts) end
s3.(ExAws.S3.put_bucket("imports", "us-east-1")) s3.(ExAws.S3.put_bucket("imports", "us-east-1"))
on_exit(fn -> s3.(ExAws.S3.delete_bucket("imports")) end) s3.(ExAws.S3.put_bucket("exports", "us-east-1"))
{:ok, container: minio, s3: s3} {:ok, container: minio, s3: s3}
end end
setup %{container: minio, s3: s3} do
connection_opts = MinioContainer.connection_opts(minio)
clean_bucket = fn bucket ->
ExAws.S3.list_objects_v2(bucket)
|> ExAws.stream!(connection_opts)
|> Stream.each(fn objects ->
keys = objects |> List.wrap() |> Enum.map(& &1.key)
s3.(ExAws.S3.delete_all_objects(bucket, keys))
end)
|> Stream.run()
end
clean_bucket.("imports")
clean_bucket.("exports")
:ok
end
describe "new_import/3 and parse_args/1" do describe "new_import/3 and parse_args/1" do
setup [:create_user, :create_new_site] setup [:create_user, :create_new_site]
@ -301,16 +320,11 @@ defmodule Plausible.Imported.CSVImporterTest do
} }
] ]
on_exit(fn ->
keys = Enum.map(csvs, fn csv -> "#{site.id}/#{csv.name}" end)
s3.(ExAws.S3.delete_all_objects("imports", keys))
end)
uploads = uploads =
for %{name: name, body: body} <- csvs do for %{name: name, body: body} <- csvs do
key = "#{site.id}/#{name}" key = "#{site.id}/#{name}"
s3.(ExAws.S3.put_object("imports", key, body)) s3.(ExAws.S3.put_object("imports", key, body))
%{"filename" => name, "s3_url" => s3_url(minio, "imports", key)} %{"filename" => name, "s3_url" => minio_url(minio, "imports", key)}
end end
{:ok, job} = {:ok, job} =
@ -362,16 +376,11 @@ defmodule Plausible.Imported.CSVImporterTest do
} }
] ]
on_exit(fn ->
keys = Enum.map(csvs, fn csv -> "#{site.id}/#{csv.name}" end)
s3.(ExAws.S3.delete_all_objects("imports", keys))
end)
uploads = uploads =
for %{name: name, body: body} <- csvs do for %{name: name, body: body} <- csvs do
key = "#{site.id}/#{name}" key = "#{site.id}/#{name}"
s3.(ExAws.S3.put_object("imports", key, body)) s3.(ExAws.S3.put_object("imports", key, body))
%{"filename" => name, "s3_url" => s3_url(minio, "imports", key)} %{"filename" => name, "s3_url" => minio_url(minio, "imports", key)}
end end
{:ok, job} = {:ok, job} =
@ -466,9 +475,6 @@ defmodule Plausible.Imported.CSVImporterTest do
]) ])
# export archive to s3 # export archive to s3
s3.(ExAws.S3.put_bucket("exports", "us-east-1"))
on_exit(fn -> s3.(ExAws.S3.delete_bucket("exports")) end)
Oban.insert!( Oban.insert!(
Plausible.Workers.ExportCSV.new(%{ Plausible.Workers.ExportCSV.new(%{
"site_id" => site.id, "site_id" => site.id,
@ -479,11 +485,9 @@ defmodule Plausible.Imported.CSVImporterTest do
}) })
) )
on_exit(fn -> s3.(ExAws.S3.delete_object("exports", "#{site.id}/Plausible.zip")) end)
assert %{success: 1} = Oban.drain_queue(queue: :s3_csv_export, with_safety: false) assert %{success: 1} = Oban.drain_queue(queue: :s3_csv_export, with_safety: false)
# download archive # download archive
s3.( s3.(
ExAws.S3.download_file( ExAws.S3.download_file(
"exports", "exports",
@ -496,16 +500,11 @@ defmodule Plausible.Imported.CSVImporterTest do
{:ok, files} = :zip.unzip(to_charlist(Path.join(tmp_dir, "Plausible.zip")), cwd: tmp_dir) {:ok, files} = :zip.unzip(to_charlist(Path.join(tmp_dir, "Plausible.zip")), cwd: tmp_dir)
# upload csvs # upload csvs
on_exit(fn ->
keys = Enum.map(files, fn file -> "#{site.id}/#{Path.basename(file)}" end)
s3.(ExAws.S3.delete_all_objects("imports", keys))
end)
uploads = uploads =
Enum.map(files, fn file -> Enum.map(files, fn file ->
key = "#{site.id}/#{Path.basename(file)}" key = "#{site.id}/#{Path.basename(file)}"
s3.(ExAws.S3.put_object("imports", key, File.read!(file))) s3.(ExAws.S3.put_object("imports", key, File.read!(file)))
%{"filename" => Path.basename(file), "s3_url" => s3_url(minio, "imports", key)} %{"filename" => Path.basename(file), "s3_url" => minio_url(minio, "imports", key)}
end) end)
# run importer # run importer
@ -519,7 +518,6 @@ defmodule Plausible.Imported.CSVImporterTest do
) )
job = Repo.reload!(job) job = Repo.reload!(job)
assert :ok = Plausible.Workers.ImportAnalytics.perform(job) assert :ok = Plausible.Workers.ImportAnalytics.perform(job)
# validate import # validate import
@ -534,7 +532,7 @@ defmodule Plausible.Imported.CSVImporterTest do
end end
end end
defp s3_url(minio, bucket, key) do defp minio_url(minio, bucket, key) do
port = minio |> MinioContainer.connection_opts() |> Keyword.fetch!(:port) port = minio |> MinioContainer.connection_opts() |> Keyword.fetch!(:port)
Path.join(["http://172.17.0.1:#{port}", bucket, key]) Path.join(["http://172.17.0.1:#{port}", bucket, key])
end end