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: Nxdomain when trying to use local stack for S3 testing - Platform - LocalStack Discuss

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

aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
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

Other popular topics Top

openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
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
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

We're in Beta

About us Mission Statement