network

package
v0.27.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 27, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(ctx context.Context, opts ...NetworkCustomizer) (*testcontainers.DockerNetwork, error)

New creates a new network with a random UUID name, calling the already existing GenericNetwork APIs. Those existing APIs are deprecated and will be removed in the future, so this function will implement the new network APIs when they will be available. By default, the network is created with the following options: - Driver: bridge - Labels: the Testcontainers for Go generic labels, to be managed by Ryuk. Please see the GenericLabels() function And those options can be modified by the user, using the CreateModifier function field.

Example

Create a network.

// createNetwork {
ctx := context.Background()

net, err := network.New(ctx,
	network.WithCheckDuplicate(),
	network.WithAttachable(),
	network.WithInternal(),
	network.WithLabels(map[string]string{"this-is-a-test": "value"}),
)
if err != nil {
	fmt.Println(err)
	return
}
defer func() {
	if err := net.Remove(ctx); err != nil {
		panic(err)
	}
}()

networkName := net.Name
// }

nginxC, _ := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
	ContainerRequest: testcontainers.ContainerRequest{
		Image: "nginx:alpine",
		ExposedPorts: []string{
			"80/tcp",
		},
		Networks: []string{
			networkName,
		},
	},
	Started: true,
})
defer func() {
	if err := nginxC.Terminate(ctx); err != nil {
		log.Fatalf("failed to terminate container: %s", err)
	}
}()

client, err := testcontainers.NewDockerClientWithOpts(context.Background())
if err != nil {
	fmt.Println(err)
	return
}

args := filters.NewArgs()
args.Add("name", networkName)

resources, err := client.NetworkList(context.Background(), types.NetworkListOptions{
	Filters: args,
})
if err != nil {
	fmt.Println(err)
	return
}

fmt.Println(len(resources))

newNetwork := resources[0]

expectedLabels := testcontainers.GenericLabels()
expectedLabels["this-is-a-test"] = "true"

fmt.Println(newNetwork.Attachable)
fmt.Println(newNetwork.Internal)
fmt.Println(newNetwork.Labels["this-is-a-test"])

state, err := nginxC.State(ctx)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Println(state.Running)
Output:

1
true
true
value
true

func WithNetwork

func WithNetwork(aliases []string, nw *testcontainers.DockerNetwork) testcontainers.CustomizeRequestOption

WithNetwork reuses an already existing network, attaching the container to it. Finally it sets the network alias on that network to the given alias.

func WithNewNetwork

func WithNewNetwork(ctx context.Context, aliases []string, opts ...NetworkCustomizer) testcontainers.CustomizeRequestOption

WithNewNetwork creates a new network with random name and customizers, and attaches the container to it. Finally it sets the network alias on that network to the given alias.

Types

type CustomizeNetworkOption

type CustomizeNetworkOption func(req *types.NetworkCreate)

CustomizeNetworkOption is a type that can be used to configure the network create request.

func WithAttachable

func WithAttachable() CustomizeNetworkOption

WithAttachable allows to set the network as attachable.

func WithCheckDuplicate

func WithCheckDuplicate() CustomizeNetworkOption

WithCheckDuplicate allows to check if a network with the same name already exists.

func WithDriver

func WithDriver(driver string) CustomizeNetworkOption

WithDriver allows to override the default network driver, which is "bridge".

func WithEnableIPv6

func WithEnableIPv6() CustomizeNetworkOption

WithEnableIPv6 allows to set the network as IPv6 enabled. Please use this option if and only if IPv6 is enabled on the Docker daemon.

func WithIPAM

func WithIPAM(ipam *network.IPAM) CustomizeNetworkOption

WithIPAM allows to change the default IPAM configuration.

func WithInternal

func WithInternal() CustomizeNetworkOption

WithInternal allows to set the network as internal.

func WithLabels

func WithLabels(labels map[string]string) CustomizeNetworkOption

WithLabels allows to set the network labels, adding the new ones to the default Testcontainers for Go labels.

func (CustomizeNetworkOption) Customize

func (opt CustomizeNetworkOption) Customize(req *types.NetworkCreate)

Customize implements the NetworkCustomizer interface, applying the option to the network create request.

type NetworkCustomizer

type NetworkCustomizer interface {
	Customize(req *types.NetworkCreate)
}

NetworkCustomizer is an interface that can be used to configure the network create request.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL