From 923839aa6f90ee8f57bd3b25532e966110bf68da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jarl=20Andr=C3=A9=20H=C3=BCbenthal?= Date: Thu, 30 Nov 2023 08:24:34 +0100 Subject: [PATCH] wip --- lib/docker/api.ex | 38 ++++++++++++++------------------- test/support/nginx_container.ex | 5 ++++- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/lib/docker/api.ex b/lib/docker/api.ex index 449863b..ddd10e3 100644 --- a/lib/docker/api.ex +++ b/lib/docker/api.ex @@ -68,34 +68,28 @@ defmodule Testcontainers.Docker.Api do end end - def put_file(container_id, connection, path, file_contents) do - with {:ok, temp_file} <- write_temp_file(file_contents), - {:ok, tar_file_contents} <- create_tar_stream(temp_file) do + def put_file(container_id, connection, path, file_name, file_contents) do + with {:ok, tar_file_contents} <- create_tar_stream(file_name, file_contents) do Api.Container.put_container_archive(connection, container_id, path, tar_file_contents) end end - # Helper function to write file contents to a temporary file - defp write_temp_file(contents) do - temp_file = "/tmp/#{UUID.uuid4()}.tmp" - - case File.write(temp_file, contents) do - :ok -> - {:ok, temp_file} - - {:error, reason} -> - {:error, reason} - end - end - # Helper function to create a tar stream from a file - defp create_tar_stream(file_path) do - tar_file = "#{file_path}.tar" - # file_path must be charlist ref https://til.kaiwern.com/tags/88 - :ok = - :erl_tar.create(tar_file, [String.to_charlist(file_path)], [:write, :compressed, :verbose]) + defp create_tar_stream(file_name, file_contents) do + tar_file = System.tmp_dir!() |> Path.join("#{UUID.uuid4()})-#{file_name}.tar") - File.read(tar_file) + :ok = + :erl_tar.create( + tar_file, + # file_name must be charlist ref https://til.kaiwern.com/tags/88 + [{file_name |> String.to_charlist(), file_contents}], + [:write, :compressed] + ) + + with {:ok, tar_file_contents} <- File.read(tar_file), + :ok <- File.rm(tar_file) do + {:ok, tar_file_contents} + end end def inspect_exec(exec_id, conn) do diff --git a/test/support/nginx_container.ex b/test/support/nginx_container.ex index 2a9dd3f..cb7f513 100644 --- a/test/support/nginx_container.ex +++ b/test/support/nginx_container.ex @@ -2,18 +2,21 @@ defmodule Test.NginxContainer do defstruct [] defimpl Testcontainers.ContainerBuilder do + alias Testcontainers.CommandWaitStrategy alias Testcontainers.Docker import Testcontainers.Container @impl true def build(%Test.NginxContainer{}) do new("nginx:alpine") + |> with_waiting_strategy(CommandWaitStrategy.new(["cat", "/tmp/foo.txt"])) end @impl true @spec is_starting(%Test.NginxContainer{}, %Testcontainers.Container{}, %Tesla.Env{}) :: any() def is_starting(_config, container, conn) do - {:ok, _} = Docker.Api.put_file(container.container_id, conn, "/tmp", "Hello foo bar") + {:ok, _} = + Docker.Api.put_file(container.container_id, conn, "/tmp", "foo.txt", "Hello foo bar") nil end