mattmartian

mattmartian

Nxdomain when trying to use local stack for S3 testing

I have set up local stack within docker-compose so that I can do integration testing with s3, however with my setup I seem to be running into a nxdomain error that I can’t seem to figure out, below is my setup and some of my code for testing - am I missing something or doing something wrong? I know that I was originally using host networking but switched to bridge network

cross posted here: LocalStack Discuss Archive

error

#0 16.15 20:51:48.018 application=ex_aws domain=elixir file=lib/ex_aws/request.ex function=request_and_retry/7 line=78 mfa=ExAws.Request.request_and_retry/7 module=ExAws.Request pid=<0.591.0> [warn]  ExAws: HTTP ERROR: :nxdomain for URL: "http://localstack:4566/test-bucket/" ATTEMPT: 9
#0 18.28 
#0 18.28 20:51:50.156 application=ex_aws domain=elixir file=lib/ex_aws/request.ex function=request_and_retry/7 line=78 mfa=ExAws.Request.request_and_retry/7 module=ExAws.Request pid=<0.591.0> [warn]  ExAws: HTTP ERROR: :nxdomain for URL: "http://localstack:4566/test-bucket/" ATTEMPT: 10
#0 18.31 
#0 18.31 
#0 18.31   1) test get_object/2 get existing file from s3 (S3Test)
#0 18.31      test/s3_test.exs:12
#0 18.31      ** (ExAws.Error) ExAws Request Error!
#0 18.31      
#0 18.31      {:error, :nxdomain}
#0 18.31      
#0 18.31      code: ExAws.request!(ExAws.S3.put_bucket("test-bucket", "us-west-2"))
#0 18.31      stacktrace:
#0 18.31        (ex_aws 2.3.2) lib/ex_aws.ex:89: ExAws.request!/2
#0 18.31        test/s3_test.exs:28: (test)

config/test.exs

config :ex_aws,
  access_key_id: "test",
  secret_access_key: "test",
  region: "us-west-2"

config :ex_aws, :s3,
  scheme: "http://",
  host: "localstack",
  port: 4566,
  region: "us-west-2"

s3_test.exs

test "get existing file from s3" do
      # Create some random bytes to store in the file.
      contents = :crypto.strong_rand_bytes(100)

      # We set up an on_exit callback to empty and then delete the bucket
      # when the test exit, so that the next test has a clean slate.
      on_exit(fn ->
        "test-bucket"
        |> ExAws.S3.list_objects()
        |> ExAws.stream!()
        |> Enum.each(&ExAws.request!(ExAws.S3.delete_object("test-bucket", &1.key)))

        ExAws.request!(ExAws.S3.delete_bucket("test-bucket"))
      end)

      # Create bucket.
      ExAws.request!(ExAws.S3.put_bucket("test-bucket", "us-west-2"))

      # Upload a file.
      ExAws.request!(ExAws.S3.put_object("test-bucket", "my/random/file", contents))

      # Now, we run our code and assert on its behavior.
      {:ok, object} = S3Helper.get_object("test-bucket", "my/random/file")

      assert(Map.has_key?(object, :status_code) and object.status_code == 200)
    end

s3 logic

 @ex_aws_mod Application.compile_env(:tls_worker, [:test_doubles, :ex_aws], ExAws)

  def get_object(bucket, path) do
    ExAws.S3.get_object(bucket, path)
    |> @ex_aws_mod.request(region: Application.get_env(:ex_aws, :region))
  end

docker-compose

version: "3"
services:
  test:
    build:
      context: .
      dockerfile: Dockerfile.test
      args:
        MIX_ENV: test
        ENVIRONMENT: test
    volumes:
      - /app
    working_dir: /app
    command: MIX_ENV=test mix test
    depends_on:
      - localstack
    networks:
      - test
    stdin_open: true
    tty: true

  localstack:
    container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
    image: localstack/localstack
    environment:
      - AWS_DEFAULT_REGION="us-west-2"
      - SERVICES="s3"
      - EDGE_PORT="4566"
    networks:
      - test
    ports:
      - "4566:4566"  
    volumes:
      - "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"

networks:
  test:
    driver: bridge

First Post!

jarlah

jarlah

This is not an answer to your problem, but personally I would rather try to use Testcontainers ceph container module. Instead of setting this up outside the tests

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

Other popular topics Top

Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement