mirror of
				https://github.com/juanfont/headscale.git
				synced 2025-10-28 10:51:44 +01:00 
			
		
		
		
	Join test suite container to network, allowing seperate networks
Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
This commit is contained in:
		
							parent
							
								
									eda4321486
								
							
						
					
					
						commit
						f109b54e79
					
				@ -1,6 +1,13 @@
 | 
			
		||||
package dockertestutil
 | 
			
		||||
 | 
			
		||||
import "github.com/ory/dockertest/v3"
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
 | 
			
		||||
	"github.com/ory/dockertest/v3"
 | 
			
		||||
	"github.com/ory/dockertest/v3/docker"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var ErrContainerNotFound = errors.New("container not found")
 | 
			
		||||
 | 
			
		||||
func GetFirstOrCreateNetwork(pool *dockertest.Pool, name string) (*dockertest.Network, error) {
 | 
			
		||||
	networks, err := pool.NetworksByName(name)
 | 
			
		||||
@ -19,3 +26,37 @@ func GetFirstOrCreateNetwork(pool *dockertest.Pool, name string) (*dockertest.Ne
 | 
			
		||||
 | 
			
		||||
	return &networks[0], nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func AddContainerToNetwork(
 | 
			
		||||
	pool *dockertest.Pool,
 | 
			
		||||
	network *dockertest.Network,
 | 
			
		||||
	testContainer string,
 | 
			
		||||
) error {
 | 
			
		||||
	containers, err := pool.Client.ListContainers(docker.ListContainersOptions{
 | 
			
		||||
		All: true,
 | 
			
		||||
		Filters: map[string][]string{
 | 
			
		||||
			"name": {testContainer},
 | 
			
		||||
		},
 | 
			
		||||
	})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = pool.Client.ConnectNetwork(network.Network.ID, docker.NetworkConnectionOptions{
 | 
			
		||||
		Container: containers[0].ID,
 | 
			
		||||
	})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// TODO(kradalby): This doesnt work reliably, but calling the exact same functions
 | 
			
		||||
	// seem to work fine...
 | 
			
		||||
	// if container, ok := pool.ContainerByName("/" + testContainer); ok {
 | 
			
		||||
	// 	err := container.ConnectToNetwork(network)
 | 
			
		||||
	// 	if err != nil {
 | 
			
		||||
	// 		return err
 | 
			
		||||
	// 	}
 | 
			
		||||
	// }
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -61,6 +61,14 @@ func NewScenario() (*Scenario, error) {
 | 
			
		||||
		return nil, fmt.Errorf("failed to create or get network: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// We run the test suite in a docker container that calls a couple of endpoints for
 | 
			
		||||
	// readiness checks, this ensures that we can run the tests with individual networks
 | 
			
		||||
	// and have the client reach the different containers
 | 
			
		||||
	err = dockertestutil.AddContainerToNetwork(pool, network, "headscale-test-suite")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("failed to add test suite container to network: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &Scenario{
 | 
			
		||||
		controlServers: make(map[string]ControlServer),
 | 
			
		||||
		namespaces:     make(map[string]*Namespace),
 | 
			
		||||
@ -88,11 +96,9 @@ func (s *Scenario) Shutdown() error {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// TODO(kradalby): This breaks the "we need to create a network before we start"
 | 
			
		||||
	// part, since we now run the tests in a container...
 | 
			
		||||
	// if err := s.pool.RemoveNetwork(s.network); err != nil {
 | 
			
		||||
	// 	return fmt.Errorf("failed to remove network: %w", err)
 | 
			
		||||
	// }
 | 
			
		||||
	if err := s.pool.RemoveNetwork(s.network); err != nil {
 | 
			
		||||
		return fmt.Errorf("failed to remove network: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// TODO(kradalby): This seem redundant to the previous call
 | 
			
		||||
	// if err := s.network.Close(); err != nil {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user