Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getting :badarg or :econnrefused using mix testcontainers.test #156

Open
pavlenski opened this issue Dec 15, 2024 · 10 comments
Open

getting :badarg or :econnrefused using mix testcontainers.test #156

pavlenski opened this issue Dec 15, 2024 · 10 comments

Comments

@pavlenski
Copy link

Having trouble running mix testcontainers.test

Getting the following error when trying to run the mix command:

Image

after a couple of runs the error switches and I get the following as a response:

00:37:57.785 [info] Docker host: {"http+unix://%2FUsers%2Fjohndoe%2F.colima%2Fdefault%2Fdocker.sock/v1.41", "unix:///Users/johndoe/.colima/default/docker.sock"}
** (EXIT from #PID<0.94.0>) :badarg

I get these responses with my colima setup as the docker runtime, failed to come even close with docker desktop

macOS - Sonoma 14.5
elixir - 1.17.3-otp-27 (tried 1.17.1 also)
docker - 27.4.0
colima - v0.8.0

anyone got an idea?

@jarlah
Copy link
Member

jarlah commented Dec 15, 2024

Will check! thanks for the report!

@jarlah
Copy link
Member

jarlah commented Dec 15, 2024

i actually fixed a bug on master lately about TC elixir always trying to open a connection to Ryuk on localhost. Ill cut a release

@jarlah
Copy link
Member

jarlah commented Dec 15, 2024

try with new version 1.11.7

just tested on my macOS with docker desktop and tests passes for this repo at least. But I had some weirdness with docker desktop. TC elixir seems to find docker.sock in home folder first, but then there are some permission stuff that follows from that when creating containers. Changing to check for /etc/run/docker.sock first resolved this for me. I will release this too if you struggle more. There are a lot of strategies and all are tried in order.

@jarlah
Copy link
Member

jarlah commented Dec 15, 2024

btw i will try out Colima .. it looks cool

@jarlah
Copy link
Member

jarlah commented Dec 15, 2024

hmm .. works with Colima with this "hack"

sudo ln -s /Users/jarlah/.colima/docker.sock /var/run/docker.sock

at least it runs tests in this project :) failing a couple (as expected)

@jarlah
Copy link
Member

jarlah commented Dec 18, 2024

can you try with the new version @pavlenski ?

@pavlenski
Copy link
Author

@jarlah Pardon for the late reply

I've managed to get it running with colima, linking the docker.sock worked fine with 1.11.7

However, some test runs work completely fine, on some I get the following error:

10:35:46.700 [error] Postgrex.Protocol (#PID<0.195.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:32884): connection refused - :econnrefused

10:35:46.702 [error] :gen_statem #PID<0.195.0> terminating
** (DBConnection.ConnectionError) tcp connect (localhost:32884): connection refused - :econnrefused
    (db_connection 2.7.0) lib/db_connection/connection.ex:104: DBConnection.Connection.handle_event/4
    (stdlib 6.1.2) gen_statem.erl:3737: :gen_statem.loop_state_callback/11
    (stdlib 6.1.2) proc_lib.erl:329: :proc_lib.init_p_do_apply/3
Queue: [internal: {:connect, :init}]
Postponed: []
State: Postgrex.Protocol
Callback mode: :handle_event_function, state_enter: false
Running ExUnit with seed: 944901, max_cases: 16

On each run I always get the following outputs, either passing or not:

10:39:43.438 [info] Docker host: {"http+unix://%2Fvar%2Frun%2Fdocker.sock/v1.41", "unix:///var/run/docker.sock"}

10:39:45.095 [debug] Not running in docker environment, using localhost

10:39:45.311 [info] Connection refused. Retrying... Attempt 1/3

10:39:50.315 [info] Testcontainers initialized
No folders specified. Only running tests.

10:39:52.137 [debug] Command execution in container c6e70db1de89e81da65735918f6d54002f21b9da34e916e8a82a1bec326b35f5 failed with exit_code 2, retrying in 200ms.

(the command executions retries 3-4 times)

One pattern I've recognized is when I make changes to my test file, running the first couple of times will end with an error. After a couple of runs this worked fine afterwards without any changes.

For reference, here's the simple test file:

defmodule StockerApiWeb.TradeOptionsControllerTest do
  use StockerApiWeb.ConnCase

  # import StockerApi.Factory
  alias StockerApi.Stocks.Stock
  alias StockerApi.Repo

  setup params do
    IO.inspect(Repo.config())
    {:ok, %{conn: params.conn}}
  end

  describe "dummy" do
    test "testing dummy!", %{conn: conn} = params do
      stock =
        %Stock{}
        |> Stock.create_changeset(%{
          name: "Test2",
          ticker: "TTS2",
          created_at: "2013-05-18"
        })
        |> Repo.insert!()

      stocks = Repo.all(Stock)
      IO.inspect(stocks)
      assert 1 == 1
    end
  end
end

@pavlenski
Copy link
Author

@jarlah Interestingly, this seems to happen only on my machine for now. I've set up a github action CI, and the tests running there always go through without connection errors..

@jarlah
Copy link
Member

jarlah commented Dec 19, 2024

I would give 1.17 with otp 25 a try. Just to check. No big hopes but worth a try. I will debug when this when i get the time 😃 havent had such big problems with this on my own machines.

@jarlah
Copy link
Member

jarlah commented Dec 22, 2024

You could also try container reuse :) but its experimental

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants