Git Product home page Git Product logo

Comments (5)

ruz avatar ruz commented on August 20, 2024 3

For people looking for answer.

It's possible to get fixed port mappings using PortBindings. See kafka example.

Also, got single container variant working:

	resource, err := pool.RunWithOptions(
		&dockertest.RunOptions{
			Repository: "docker.io/bitnami/kafka",
			Tag:        "3.2",
			Env: []string{
				"KAFKA_ENABLE_KRAFT=yes",
				"KAFKA_BROKER_ID=1",
				"KAFKA_CFG_PROCESS_ROLES=broker,controller",
				"KAFKA_CFG_CONTROLLER_LISTENER_NAMES=CONTROLLER",
				"KAFKA_CFG_LISTENERS=PLAINTEXT://:9092,CONTROLLER://:9093",
				"KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT",
				"KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://127.0.0.1:9092",
				"[email protected]:9093",
				"ALLOW_PLAINTEXT_LISTENER=yes",
			},
			ExposedPorts: []string{"9092/tcp"},
			PortBindings: map[docker.Port][]docker.PortBinding{
				"9092/tcp": {{HostIP: "localhost", HostPort: "9092/tcp"}},
			},
		})

from dockertest.

aeneasr avatar aeneasr commented on August 20, 2024 2

If I understand correctly, as dockertest uses fsouza/go-dockerclient, it should be possible to use a similar approach to allow for dockertest to offer this feature. Perhaps by allowing a ":" to be present in an ExposedPorts declaration such that a host port and container port can be specified e.g 9092:9092

Yes, this was actually the case until the v3 release, but was replaced in favor of dynamic port allocation which avoids port conflicts, which happened a lot. I'm open to having an option to force specific ports though.

from dockertest.

aeneasr avatar aeneasr commented on August 20, 2024 1

You need to set the port in Sarama to 9092, you can get the current port mapping by doing, in the case of mysql, resource.GetPort("3306/tcp"))):

db, err = sql.Open("mysql", fmt.Sprintf("root:secret@(localhost:%s)/mysql?parseTime=true", resource.GetPort("3306/tcp")))

For kafka this is probably something like resource.GetPort("32911/tcp"). You can dump the ports to stdout to see which values are available

from dockertest.

adbourne avatar adbourne commented on August 20, 2024

Thanks for the reply @arekkas.

In regards to mysql this would certainly be the solution, however things have gotten a bit trickier since trying dockertest with Kafka.

I use resource.GetPort("9092/tcp") initially to get the port number of Kafka that Docker has assigned (e.g 32911). This is used for the successful initial connection. The challenge is that this is only used to get metadata from Kafka, after that Sarama uses the port number Kafka tells it about, which is the container port number and not the host port number:

Sarama                           Kafka
  |                                |
  | get metadata (localhost:32911) |
  |------------------------------->| 
  |                                |
  | broker on localhost:9092       |
  |<-------------------------------| 
  |                                |
  | consume topic (localhost:9092) |
  |-------------------------------X|
  |                                | 

For the moment, I have "solved" the issue by using fsouza/go-dockerclient directly. This allows for specifying the host ports the container ports should be mapped to: https://github.com/adbourne/go-archetype-kafka-processor/blob/master/app_integration_test.go#L124

If I understand correctly, as dockertest uses fsouza/go-dockerclient, it should be possible to use a similar approach to allow for dockertest to offer this feature. Perhaps by allowing a ":" to be present in an ExposedPorts declaration such that a host port and container port can be specified e.g 9092:9092 . This would be a similar approach to the Docker cli. The logic could live here: https://github.com/ory/dockertest/blob/v3/dockertest.go#L119

from dockertest.

ruz avatar ruz commented on August 20, 2024

Same problem here. I understand issue with port conflicts during testing.

Is there way to know port beforehand so I can configure Kafka properly via environment variables?

Is there way to use some sort of substitutions in Env variables?

Is there way to run a command to form environment variable?

from dockertest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.