feat: more complete metadata diff, scp publishing and rendering context
This commit is contained in:
@@ -3,7 +3,10 @@ defmodule BDS.PublishingTest do
|
||||
|
||||
setup do
|
||||
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
|
||||
temp_dir = Path.join(System.tmp_dir!(), "bds-publishing-#{System.unique_integer([:positive])}")
|
||||
|
||||
temp_dir =
|
||||
Path.join(System.tmp_dir!(), "bds-publishing-#{System.unique_integer([:positive])}")
|
||||
|
||||
File.mkdir_p!(temp_dir)
|
||||
on_exit(fn -> File.rm_rf(temp_dir) end)
|
||||
|
||||
@@ -11,7 +14,10 @@ defmodule BDS.PublishingTest do
|
||||
%{project: project, temp_dir: temp_dir}
|
||||
end
|
||||
|
||||
test "upload_site creates a publish job, uploads all targets, and excludes media sidecars", %{project: project, temp_dir: temp_dir} do
|
||||
test "upload_site creates a publish job, uploads all targets, and excludes media sidecars", %{
|
||||
project: project,
|
||||
temp_dir: temp_dir
|
||||
} do
|
||||
test_pid = self()
|
||||
|
||||
File.mkdir_p!(Path.join([temp_dir, "html"]))
|
||||
@@ -25,7 +31,11 @@ defmodule BDS.PublishingTest do
|
||||
File.write!(Path.join([temp_dir, "media", "asset.jpg.meta"]), "meta")
|
||||
|
||||
uploader = fn target, files, credentials ->
|
||||
send(test_pid, {:uploaded, target.kind, target.remote_dir, Enum.sort(files), credentials.ssh_mode})
|
||||
send(
|
||||
test_pid,
|
||||
{:uploaded, target.kind, target.remote_dir, Enum.sort(files), credentials.ssh_mode}
|
||||
)
|
||||
|
||||
:ok
|
||||
end
|
||||
|
||||
@@ -46,7 +56,10 @@ defmodule BDS.PublishingTest do
|
||||
assert_receive {:uploaded, :media, "/srv/blog/media", ["asset.jpg"], :rsync}
|
||||
end
|
||||
|
||||
test "upload_site runs rsync commands with SSH agent env and media exclude filters", %{project: project, temp_dir: temp_dir} do
|
||||
test "upload_site runs rsync commands with SSH agent env and media exclude filters", %{
|
||||
project: project,
|
||||
temp_dir: temp_dir
|
||||
} do
|
||||
test_pid = self()
|
||||
|
||||
File.mkdir_p!(Path.join([temp_dir, "html"]))
|
||||
@@ -78,17 +91,47 @@ defmodule BDS.PublishingTest do
|
||||
assert wait_for_publish_job(job.id, &(&1.status == :completed)).status == :completed
|
||||
|
||||
assert_receive {:command_run, "rsync", html_args, html_opts}
|
||||
assert html_args == ["--update", "--compress", "--verbose", "-e", "ssh", Path.join([temp_dir, "html"]) <> "/", "deploy@example.com:/srv/blog/"]
|
||||
|
||||
assert html_args == [
|
||||
"--update",
|
||||
"--compress",
|
||||
"--verbose",
|
||||
"-e",
|
||||
"ssh",
|
||||
Path.join([temp_dir, "html"]) <> "/",
|
||||
"deploy@example.com:/srv/blog/"
|
||||
]
|
||||
|
||||
assert html_opts[:env] == [{"SSH_AUTH_SOCK", "/tmp/test-agent.sock"}]
|
||||
|
||||
assert_receive {:command_run, "rsync", thumb_args, _thumb_opts}
|
||||
assert thumb_args == ["--update", "--compress", "--verbose", "-e", "ssh", Path.join([temp_dir, "thumbnails"]) <> "/", "deploy@example.com:/srv/blog/thumbnails/"]
|
||||
|
||||
assert thumb_args == [
|
||||
"--update",
|
||||
"--compress",
|
||||
"--verbose",
|
||||
"-e",
|
||||
"ssh",
|
||||
Path.join([temp_dir, "thumbnails"]) <> "/",
|
||||
"deploy@example.com:/srv/blog/thumbnails/"
|
||||
]
|
||||
|
||||
assert_receive {:command_run, "rsync", media_args, _media_opts}
|
||||
assert media_args == ["--update", "--compress", "--verbose", "--exclude=*.meta", "-e", "ssh", Path.join([temp_dir, "media"]) <> "/", "deploy@example.com:/srv/blog/media/"]
|
||||
|
||||
assert media_args == [
|
||||
"--update",
|
||||
"--compress",
|
||||
"--verbose",
|
||||
"--exclude=*.meta",
|
||||
"-e",
|
||||
"ssh",
|
||||
Path.join([temp_dir, "media"]) <> "/",
|
||||
"deploy@example.com:/srv/blog/media/"
|
||||
]
|
||||
end
|
||||
|
||||
test "upload_site runs scp commands for each eligible file and fails when a command exits non-zero", %{project: project, temp_dir: temp_dir} do
|
||||
test "upload_site runs scp commands for each eligible file and fails when a command exits non-zero",
|
||||
%{project: project, temp_dir: temp_dir} do
|
||||
test_pid = self()
|
||||
html_index = Path.join([temp_dir, "html", "index.html"])
|
||||
html_entry = Path.join([temp_dir, "html", "posts", "entry.html"])
|
||||
@@ -129,14 +172,26 @@ defmodule BDS.PublishingTest do
|
||||
failed_job = wait_for_publish_job(job.id, &(&1.status == :failed))
|
||||
assert failed_job.error =~ "thumbnail failure"
|
||||
|
||||
assert_receive {:command_run, "scp", ["-q", ^html_index, "deploy@example.com:/srv/blog/index.html"], opts_a}
|
||||
assert_receive {:command_run, "scp",
|
||||
["-q", ^html_index, "deploy@example.com:/srv/blog/index.html"], opts_a}
|
||||
|
||||
assert opts_a[:env] == [{"SSH_AUTH_SOCK", "/tmp/test-agent.sock"}]
|
||||
assert_receive {:command_run, "scp", ["-q", ^html_entry, "deploy@example.com:/srv/blog/posts/entry.html"], _opts_b}
|
||||
assert_receive {:command_run, "scp", ["-q", ^thumb_path, "deploy@example.com:/srv/blog/thumbnails/thumb.jpg"], _opts_c}
|
||||
refute_receive {:command_run, "scp", ["-q", _, "deploy@example.com:/srv/blog/media/asset.jpg"], _opts_d}
|
||||
|
||||
assert_receive {:command_run, "scp",
|
||||
["-q", ^html_entry, "deploy@example.com:/srv/blog/posts/entry.html"], _opts_b}
|
||||
|
||||
assert_receive {:command_run, "scp",
|
||||
["-q", ^thumb_path, "deploy@example.com:/srv/blog/thumbnails/thumb.jpg"],
|
||||
_opts_c}
|
||||
|
||||
refute_receive {:command_run, "scp",
|
||||
["-q", _, "deploy@example.com:/srv/blog/media/asset.jpg"], _opts_d}
|
||||
end
|
||||
|
||||
test "upload_site marks the publish job failed when a target upload fails", %{project: project, temp_dir: temp_dir} do
|
||||
test "upload_site marks the publish job failed when a target upload fails", %{
|
||||
project: project,
|
||||
temp_dir: temp_dir
|
||||
} do
|
||||
File.mkdir_p!(Path.join([temp_dir, "html"]))
|
||||
File.write!(Path.join([temp_dir, "html", "index.html"]), "<html />")
|
||||
File.mkdir_p!(Path.join([temp_dir, "thumbnails"]))
|
||||
@@ -161,6 +216,75 @@ defmodule BDS.PublishingTest do
|
||||
assert failed_job.error == "thumbnail failure"
|
||||
end
|
||||
|
||||
test "upload_site skips unchanged files for scp and only re-uploads files with newer mtimes", %{
|
||||
project: project,
|
||||
temp_dir: temp_dir
|
||||
} do
|
||||
test_pid = self()
|
||||
html_index = Path.join([temp_dir, "html", "index.html"])
|
||||
media_asset = Path.join([temp_dir, "media", "asset.jpg"])
|
||||
|
||||
File.mkdir_p!(Path.dirname(html_index))
|
||||
File.write!(html_index, "<html />")
|
||||
File.mkdir_p!(Path.join([temp_dir, "thumbnails"]))
|
||||
File.write!(Path.join([temp_dir, "thumbnails", "thumb.jpg"]), "thumb")
|
||||
File.mkdir_p!(Path.dirname(media_asset))
|
||||
File.write!(media_asset, "asset")
|
||||
|
||||
runner = fn command, args, opts ->
|
||||
send(test_pid, {:command_run, command, args, opts})
|
||||
{"", 0}
|
||||
end
|
||||
|
||||
credentials = %{
|
||||
ssh_host: "example.com",
|
||||
ssh_user: "deploy",
|
||||
ssh_remote_path: "/srv/blog",
|
||||
ssh_mode: :scp
|
||||
}
|
||||
|
||||
assert {:ok, first_job} =
|
||||
BDS.Publishing.upload_site(project.id, credentials,
|
||||
command_runner: runner,
|
||||
ssh_auth_sock: "/tmp/test-agent.sock"
|
||||
)
|
||||
|
||||
assert wait_for_publish_job(first_job.id, &(&1.status == :completed)).status == :completed
|
||||
first_uploads = collect_command_runs()
|
||||
assert length(first_uploads) == 3
|
||||
|
||||
assert {:ok, second_job} =
|
||||
BDS.Publishing.upload_site(project.id, credentials,
|
||||
command_runner: runner,
|
||||
ssh_auth_sock: "/tmp/test-agent.sock"
|
||||
)
|
||||
|
||||
assert wait_for_publish_job(second_job.id, &(&1.status == :completed)).status == :completed
|
||||
assert collect_command_runs() == []
|
||||
|
||||
:ok = File.touch(html_index, {{2099, 1, 1}, {0, 0, 0}})
|
||||
|
||||
assert {:ok, third_job} =
|
||||
BDS.Publishing.upload_site(project.id, credentials,
|
||||
command_runner: runner,
|
||||
ssh_auth_sock: "/tmp/test-agent.sock"
|
||||
)
|
||||
|
||||
assert wait_for_publish_job(third_job.id, &(&1.status == :completed)).status == :completed
|
||||
|
||||
assert [html_upload] = collect_command_runs()
|
||||
assert elem(html_upload, 0) == "scp"
|
||||
assert elem(html_upload, 1) == ["-q", html_index, "deploy@example.com:/srv/blog/index.html"]
|
||||
end
|
||||
|
||||
defp collect_command_runs(acc \\ []) do
|
||||
receive do
|
||||
{:command_run, command, args, _opts} -> collect_command_runs([{command, args} | acc])
|
||||
after
|
||||
50 -> Enum.reverse(acc)
|
||||
end
|
||||
end
|
||||
|
||||
defp wait_for_publish_job(job_id, predicate, attempts \\ 100)
|
||||
|
||||
defp wait_for_publish_job(job_id, predicate, attempts) when attempts > 0 do
|
||||
|
||||
Reference in New Issue
Block a user