diff --git a/integration/dockertestutil/network.go b/integration/dockertestutil/network.go index fbf57fc9..9b51986b 100644 --- a/integration/dockertestutil/network.go +++ b/integration/dockertestutil/network.go @@ -2,8 +2,9 @@ package dockertestutil import ( "errors" - "net" "fmt" + "log" + "net" "github.com/ory/dockertest/v3" "github.com/ory/dockertest/v3/docker" @@ -84,3 +85,23 @@ func RandomFreeHostPort() (int, error) { //nolint:forcetypeassert return listener.Addr().(*net.TCPAddr).Port, nil } + +// CleanUnreferencedNetworks removes networks that are not referenced by any containers. +func CleanUnreferencedNetworks(pool *dockertest.Pool) error { + filter := "name=hs-" + networks, err := pool.NetworksByName(filter) + if err != nil { + return fmt.Errorf("getting networks by filter %q: %w", filter, err) + } + + for _, network := range networks { + if network.Network.Containers == nil || len(network.Network.Containers) == 0 { + err := pool.RemoveNetwork(&network) + if err != nil { + log.Printf("removing network %s: %s", network.Network.Name, err) + } + } + } + + return nil +} diff --git a/integration/scenario.go b/integration/scenario.go index e0cbdc21..94b37e16 100644 --- a/integration/scenario.go +++ b/integration/scenario.go @@ -165,6 +165,11 @@ func NewScenario(spec ScenarioSpec) (*Scenario, error) { return nil, fmt.Errorf("could not connect to docker: %w", err) } + // Opportunity to clean up unreferenced networks. + // This might be a no op, but it is worth a try as we sometime + // dont clean up nicely after ourselves. + dockertestutil.CleanUnreferencedNetworks(pool) + if spec.MaxWait == 0 { pool.MaxWait = dockertestMaxWait() } else { @@ -292,6 +297,8 @@ func (s *Scenario) Services(name string) ([]*dockertest.Resource, error) { } func (s *Scenario) ShutdownAssertNoPanics(t *testing.T) { + defer dockertestutil.CleanUnreferencedNetworks(s.pool) + s.controlServers.Range(func(_ string, control ControlServer) bool { stdoutPath, stderrPath, err := control.Shutdown() if err != nil {