Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah committed Nov 30, 2023
1 parent fcfe3d2 commit 923839a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
38 changes: 16 additions & 22 deletions lib/docker/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion test/support/nginx_container.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 923839a

Please sign in to comment.