github.com/smartcontractkit/chainlink-testing-framework/libs@v0.0.0-20240227141906-ec710b4eb1a3/docker/test_env/utils.go (about)

     1  package test_env
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/docker/go-connections/nat"
     9  	tc "github.com/testcontainers/testcontainers-go"
    10  )
    11  
    12  func NatPortFormat(port string) string {
    13  	return fmt.Sprintf("%s/tcp", port)
    14  }
    15  
    16  func NatPort(port string) nat.Port {
    17  	return nat.Port(NatPortFormat(port))
    18  }
    19  
    20  // GetHost returns the host of a container, if localhost then force ipv4 localhost
    21  // to avoid ipv6 docker bugs https://github.com/moby/moby/issues/42442 https://github.com/moby/moby/issues/42375
    22  func GetHost(ctx context.Context, container tc.Container) (string, error) {
    23  	host, err := container.Host(ctx)
    24  	if err != nil {
    25  		return "", err
    26  	}
    27  	// if localhost then force it to ipv4 localhost
    28  	if host == "localhost" {
    29  		host = "127.0.0.1"
    30  	}
    31  	return host, nil
    32  }
    33  
    34  // GetEndpoint returns the endpoint of a container, if localhost then force ipv4 localhost
    35  // to avoid ipv6 docker bugs https://github.com/moby/moby/issues/42442 https://github.com/moby/moby/issues/42375
    36  func GetEndpoint(ctx context.Context, container tc.Container, endpointType string) (string, error) {
    37  	endpoint, err := container.Endpoint(ctx, endpointType)
    38  	if err != nil {
    39  		return "", err
    40  	}
    41  	return strings.Replace(endpoint, "localhost", "127.0.0.1", 1), nil
    42  }
    43  
    44  func FormatHttpUrl(host string, port string) string {
    45  	return fmt.Sprintf("http://%s:%s", host, port)
    46  }
    47  
    48  func FormatWsUrl(host string, port string) string {
    49  	return fmt.Sprintf("ws://%s:%s", host, port)
    50  }