testcontainers-go icon indicating copy to clipboard operation
testcontainers-go copied to clipboard

How to define a proper wait strategy

Open Vesli opened this issue 5 years ago • 1 comments

Describe the bug I am trying to setup a zookeeper / kafka docker environment in order to run integration testing with Go. Thus, I am setting up Zookeeper container, wait for it to be ready and then setup a kafka container. But before being able to reach kafka the error:

panic: runtime error: index out of range [0] with length 0 [recovered]
	panic: runtime error: index out of range [0] with length 0

Occurs when trying to access zookeeperContainer.Endpoint() and it crashes in 3s if the container was already downloaded

To Reproduce

func setupKafkaContainer(ctx context.Context) (context.Context, testcontainers.Container, error) {
	zookeeperEnv := make(map[string]string, 3)
	zookeeperEnv["ZOOKEEPER_CLIENT_PORT"] = "2181"
	zookeeperEnv["ZOOKEEPER_TICK_TIME"] = "2000"
	zookeeperReq := testcontainers.ContainerRequest{
		Image:        "confluentinc/cp-zookeeper:5.2.2",
		ExposedPorts: []string{"2181/tcp"},
		Env:          zookeeperEnv,
		WaitingFor:   wait.ForListeningPort("2181/tcp"),
	}

	zookeeperContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
		ContainerRequest: zookeeperReq,
		Started:          true,
	})
	if err != nil {
		return ctx, nil, err
	}

	zookeeperAddr, err := zookeeperContainer.Endpoint(ctx, "") <- crashes when being accessed later on by kafka container
	if err != nil {
		return ctx, nil, err
	}
}

Which makes me think that the kafka container started before zookeeper was ready

Expected behavior zookeeper should have started properly and accessing container endpoint shouldn't panic

** docker info ** output of the command:

$ docker info
Client:
 Debug Mode: false

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 34
 Server Version: 19.03.8
 Storage Driver: overlay2
  Backing Filesystem: <unknown>
  Supports d_type: true
  Native Overlay Diff: true
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd
 init version: fec3683
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 4.19.76-linuxkit
 Operating System: Docker Desktop
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 1.944GiB
 Name: docker-desktop
 ID: RWR6:WDHQ:NHGL:KW5V:WAMR:5JBY:AFNW:ABK4:Z5NN:D5CF:IAGR:5GPS
 Docker Root Dir: /var/lib/docker
 Debug Mode: true
  File Descriptors: 40
  Goroutines: 52
  System Time: 2020-06-12T14:15:49.8731093Z
  EventsListeners: 3
 HTTP Proxy: gateway.docker.internal:3128
 HTTPS Proxy: gateway.docker.internal:3129
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false
 Product License: Community Engine

Additional context My main problem here isn't so much about the bug (if there is one) but about the lack of documentation:

Endpoint(ctx, "")

what is the second argument string for?

How do you use the WaitUntilReady() ?

Vesli avatar Jun 12 '20 14:06 Vesli

The second argument is the URL scheme.

// Endpoint gets proto://host:port string for the first exposed port
// Will returns just host:port if proto is ""

https://github.com/testcontainers/testcontainers-go/blob/master/docker.go#L57-L73

mniak avatar Aug 06 '20 18:08 mniak

I think that, since these docs for wait strategies exist, this question can be closed, or moved to the Slack.

Please reopen if needed, thanks!

mdelapenya avatar Oct 19 '22 16:10 mdelapenya