Skip to content

Commit

Permalink
send user_agent in connection constructor (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah committed May 15, 2024
1 parent 8ced2f3 commit a4e48a5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 35 deletions.
8 changes: 7 additions & 1 deletion lib/connection/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
defmodule Testcontainers.Connection do
@moduledoc false

alias Testcontainers.Constants
alias Testcontainers.DockerUrl
alias Testcontainers.Logger
alias Testcontainers.DockerHostStrategyEvaluator
Expand All @@ -17,7 +18,12 @@ defmodule Testcontainers.Connection do

Logger.log("Using docker host url: #{docker_host_url}")

options = Keyword.merge(options, base_url: docker_host_url, recv_timeout: @timeout)
options =
Keyword.merge(options,
base_url: docker_host_url,
recv_timeout: @timeout,
user_agent: Constants.user_agent()
)

{Connection.new(options), docker_host_url}
end
Expand Down
46 changes: 12 additions & 34 deletions lib/docker/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ defmodule Testcontainers.Docker.Api do

alias DockerEngineAPI.Api
alias Testcontainers.Container
alias Testcontainers.Constants

def get_container(container_id, conn)
when is_binary(container_id) do
case Api.Container.container_inspect(conn, container_id,
"User-Agent": Constants.user_agent()
) do
case Api.Container.container_inspect(conn, container_id) do
{:error, %Tesla.Env{status: other}} ->
{:error, {:http_error, other}}

Expand All @@ -29,8 +26,7 @@ defmodule Testcontainers.Docker.Api do

case Api.Image.image_create(conn,
fromImage: image,
"X-Registry-Auth": auth,
"User-Agent": Constants.user_agent()
"X-Registry-Auth": auth
) do
{:ok, %Tesla.Env{status: 200}} ->
{:ok, nil}
Expand All @@ -44,9 +40,7 @@ defmodule Testcontainers.Docker.Api do
end

def create_container(%Container{} = container, conn) do
case Api.Container.container_create(conn, container_create_request(container),
"User-Agent": Constants.user_agent()
) do
case Api.Container.container_create(conn, container_create_request(container)) do
{:error, %Tesla.Env{status: other}} ->
{:error, {:http_error, other}}

Expand All @@ -59,9 +53,7 @@ defmodule Testcontainers.Docker.Api do
end

def start_container(id, conn) when is_binary(id) do
case Api.Container.container_start(conn, id,
"User-Agent": Constants.user_agent()
) do
case Api.Container.container_start(conn, id) do
{:ok, %Tesla.Env{status: 204}} ->
:ok

Expand All @@ -75,22 +67,16 @@ defmodule Testcontainers.Docker.Api do

def stop_container(container_id, conn) when is_binary(container_id) do
with {:ok, _} <-
Api.Container.container_kill(conn, container_id,
"User-Agent": Constants.user_agent()
),
Api.Container.container_kill(conn, container_id),
{:ok, _} <-
Api.Container.container_delete(conn, container_id,
"User-Agent": Constants.user_agent()
) do
Api.Container.container_delete(conn, container_id) do
:ok
end
end

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,
"User-Agent": Constants.user_agent()
)
Api.Container.put_container_archive(connection, container_id, path, tar_file_contents)
end
end

Expand All @@ -113,7 +99,7 @@ defmodule Testcontainers.Docker.Api do
end

def inspect_exec(exec_id, conn) do
case Api.Exec.exec_inspect(conn, exec_id, "User-Agent": Constants.user_agent()) do
case Api.Exec.exec_inspect(conn, exec_id) do
{:ok, %DockerEngineAPI.Model.ExecInspectResponse{} = body} ->
{:ok, parse_inspect_result(body)}

Expand All @@ -140,8 +126,7 @@ defmodule Testcontainers.Docker.Api do
conn,
container_id,
stdout: true,
stderr: true,
"User-Agent": Constants.user_agent()
stderr: true
) do
{:ok, %Tesla.Env{body: body}} ->
{:ok, body}
Expand All @@ -155,9 +140,7 @@ defmodule Testcontainers.Docker.Api do
end

def get_bridge_gateway(conn) do
case Api.Network.network_inspect(conn, "bridge",
"User-Agent": Constants.user_agent()
) do
case Api.Network.network_inspect(conn, "bridge") do
{:ok, %DockerEngineAPI.Model.Network{IPAM: %DockerEngineAPI.Model.Ipam{Config: config}}} ->
with_gateway =
config
Expand Down Expand Up @@ -271,9 +254,7 @@ defmodule Testcontainers.Docker.Api do
defp create_exec(container_id, command, conn) do
data = %{"Cmd" => command}

case Api.Exec.container_exec(conn, container_id, data,
"User-Agent": Constants.user_agent()
) do
case Api.Exec.container_exec(conn, container_id, data) do
{:ok, %DockerEngineAPI.Model.IdResponse{Id: id}} ->
{:ok, id}

Expand All @@ -289,10 +270,7 @@ defmodule Testcontainers.Docker.Api do
end

defp start_exec(exec_id, conn) do
case Api.Exec.exec_start(conn, exec_id,
body: %{},
"User-Agent": Constants.user_agent()
) do
case Api.Exec.exec_start(conn, exec_id, body: %{}) do
{:ok, %Tesla.Env{status: 200}} ->
:ok

Expand Down

0 comments on commit a4e48a5

Please sign in to comment.