mirror of
https://github.com/juanfont/headscale.git
synced 2025-04-24 04:20:32 -04:00
integration: clean up unreferenced hs- networks (#2534)
This commit is contained in:
parent
710d75367e
commit
f783555469
@ -2,8 +2,9 @@ package dockertestutil
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"net"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
|
||||||
"github.com/ory/dockertest/v3"
|
"github.com/ory/dockertest/v3"
|
||||||
"github.com/ory/dockertest/v3/docker"
|
"github.com/ory/dockertest/v3/docker"
|
||||||
@ -84,3 +85,23 @@ func RandomFreeHostPort() (int, error) {
|
|||||||
//nolint:forcetypeassert
|
//nolint:forcetypeassert
|
||||||
return listener.Addr().(*net.TCPAddr).Port, nil
|
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
|
||||||
|
}
|
||||||
|
@ -165,6 +165,11 @@ func NewScenario(spec ScenarioSpec) (*Scenario, error) {
|
|||||||
return nil, fmt.Errorf("could not connect to docker: %w", err)
|
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 {
|
if spec.MaxWait == 0 {
|
||||||
pool.MaxWait = dockertestMaxWait()
|
pool.MaxWait = dockertestMaxWait()
|
||||||
} else {
|
} else {
|
||||||
@ -292,6 +297,8 @@ func (s *Scenario) Services(name string) ([]*dockertest.Resource, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Scenario) ShutdownAssertNoPanics(t *testing.T) {
|
func (s *Scenario) ShutdownAssertNoPanics(t *testing.T) {
|
||||||
|
defer dockertestutil.CleanUnreferencedNetworks(s.pool)
|
||||||
|
|
||||||
s.controlServers.Range(func(_ string, control ControlServer) bool {
|
s.controlServers.Range(func(_ string, control ControlServer) bool {
|
||||||
stdoutPath, stderrPath, err := control.Shutdown()
|
stdoutPath, stderrPath, err := control.Shutdown()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user