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

Network is not being removed by reaper #173

Closed
ikolomiyets opened this issue Apr 5, 2020 · 0 comments · Fixed by #176
Closed

Network is not being removed by reaper #173

ikolomiyets opened this issue Apr 5, 2020 · 0 comments · Fixed by #176

Comments

@ikolomiyets
Copy link
Contributor

Describe the bug
In the complex case, when test suite creates multiple containers attached to the a new network that is also created by the test, reaper intermittently fails to remove network upon completion of the test.

go version go1.14.1 darwin/amd64
github.com/testcontainers/testcontainers-go v0.3.1

To Reproduce

func Test_MultipleContainersInTheNewNetwork(t *testing.T) {
	ctx := context.Background()
	networkName := "test-network"

	networkRequest := NetworkRequest{
		Driver:     "bridge",
		Name:       networkName,
		Attachable: true,
	}

	env := make(map[string]string)
	env["POSTGRES_PASSWORD"] = "Password1"
	dbContainerRequest := ContainerRequest{
		Image:        "postgres:12.2",
		ExposedPorts: []string{"5432/tcp"},
		AutoRemove:   true,
		Env:          env,
		WaitingFor:   wait.ForListeningPort("5432/tcp"),
		Networks:     []string{networkName},
	}

	gcr := GenericContainerRequest{
		ContainerRequest: dbContainerRequest,
		Started:          true,
	}

	provider, err := gcr.ProviderType.GetProvider()
	if err != nil {
		t.Fatal("cannot get provider")
	}

	net, err := provider.CreateNetwork(ctx, networkRequest)
	if err != nil {
		t.Fatal("cannot create network")
	}

	defer net.Remove(ctx)

	postgres, err := GenericContainer(ctx, gcr)
	if err != nil {
		t.Fatal(err)
	}

	defer postgres.Terminate(ctx)

	env = make(map[string]string)
	env["RABBITMQ_ERLANG_COOKIE"] = "f2a2d3d27c75"
	env["RABBITMQ_DEFAULT_USER"] = "admin"
	env["RABBITMQ_DEFAULT_PASS"] = "Password1"
	hp := wait.ForListeningPort("5672/tcp")
	hp.WithStartupTimeout(3 * time.Minute)
	amqpRequest := ContainerRequest{
		Image:        "rabbitmq:management-alpine",
		ExposedPorts: []string{"15672/tcp", "5672/tcp"},
		Env:          env,
		AutoRemove:   true,
		WaitingFor:   hp,
		Networks:     []string{networkName},
	}
	rabbitmq, err := GenericContainer(ctx, GenericContainerRequest{
		ContainerRequest: amqpRequest,
		Started:          true,
	})
	if err != nil {
		t.Fatal(err)
		return
	}

	defer rabbitmq.Terminate(ctx)
	fmt.Println(postgres.GetContainerID())
	fmt.Println(rabbitmq.GetContainerID())
}

Once the above test is executed I still see the test-network in the list:

$ docker network ls
NETWORK ID          NAME                     DRIVER              SCOPE
dc5e0801c8e8        bridge                   bridge              local
7ae8213f0a86        host                     host                local
67fe4e22fb3f        none                     null                local
f1d721f644a2        test-network             bridge              local

Expected behavior
Upon completion of the test I expect the test-network be removed.

** docker info **
output of the command:

$ docker info
Client:
 Debug Mode: false

Server:
 Containers: 5
  Running: 5
  Paused: 0
  Stopped: 0
 Images: 167
 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: 2
 Total Memory: 1.943GiB
 Name: docker-desktop
 ID: JBFA:YKXZ:QUPH:UKJO:UZDG:Z2LK:ZMOK:HB5K:ON7A:TCRF:KVYC:AMM2
 Docker Root Dir: /var/lib/docker
 Debug Mode: true
  File Descriptors: 76
  Goroutines: 84
  System Time: 2020-04-05T17:44:10.408114924Z
  EventsListeners: 4
 HTTP Proxy: gateway.docker.internal:3128
 HTTPS Proxy: gateway.docker.internal:3129
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: true
 Insecure Registries:
  localhost:5000
  172.30.0.0/16
  127.0.0.0/8
 Live Restore Enabled: false
 Product License: Community Engine

Additional context
Further analysis of the proeblem shows that it is likely occurs due to the fact that testcontainers-go by default creates a reaper to control lifecycle of each container or network, hence if one of the containers take time to stop, reaper that controls network might attempt to remove it while there are still containers attached to it. Hence, it leaves network behind.
Ideally, there should be a single reaper that controls the whole test stack, similarly how the Java library behaves by default.

This was referenced Apr 5, 2020
gianarb added a commit that referenced this issue Apr 14, 2020
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

Successfully merging a pull request may close this issue.

1 participant