Documentation
¶
Index ¶
Examples ¶
Constants ¶
const ( // defaultImage { // DefaultImage is the default image used by the Socat container. DefaultImage = "alpine/socat:1.8.0.1" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Container ¶
type Container struct {
testcontainers.Container
// contains filtered or unexported fields
}
Container represents the Socat container type used in the module. A socat container is used as a TCP proxy, enabling any TCP port of another container to be exposed publicly, even if that container does not make the port public itself.
func Run ¶
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error)
Run creates an instance of the Socat container type
Example ¶
ctx := context.Background()
nw, err := network.New(ctx)
if err != nil {
log.Printf("failed to create network: %v", err)
return
}
defer func() {
if err := nw.Remove(ctx); err != nil {
log.Printf("failed to remove network: %s", err)
}
}()
ctr, err := testcontainers.Run(ctx, "testcontainers/helloworld:1.2.0",
testcontainers.WithExposedPorts("8080/tcp"),
network.WithNetwork([]string{"helloworld"}, nw),
)
if err != nil {
log.Printf("failed to create container: %v", err)
return
}
defer func() {
if err := testcontainers.TerminateContainer(ctr); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
target := socat.NewTarget(8080, "helloworld")
socatContainer, err := socat.Run(
ctx, socat.DefaultImage,
socat.WithTarget(target),
network.WithNetwork([]string{"socat"}, nw),
)
if err != nil {
log.Printf("failed to create container: %v", err)
return
}
defer func() {
if err := testcontainers.TerminateContainer(socatContainer); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
// readFromSocat {
httpClient := http.DefaultClient
baseURI := socatContainer.TargetURL(target.ExposedPort())
resp, err := httpClient.Get(baseURI.String() + "/ping")
if err != nil {
log.Printf("failed to get response: %v", err)
return
}
defer resp.Body.Close()
// }
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Printf("failed to read body: %v", err)
return
}
fmt.Printf("%d - %s", resp.StatusCode, string(body))
Output: 200 - PONG
Example (MultipleTargets) ¶
ctx := context.Background()
// createNetwork {
nw, err := network.New(ctx)
if err != nil {
log.Printf("failed to create network: %v", err)
return
}
defer func() {
if err := nw.Remove(ctx); err != nil {
log.Printf("failed to remove network: %s", err)
}
}()
// }
// createHelloWorldContainer {
ctr, err := testcontainers.Run(ctx, "testcontainers/helloworld:1.2.0",
network.WithNetwork([]string{"helloworld"}, nw),
testcontainers.WithExposedPorts("8080/tcp"),
)
if err != nil {
log.Printf("failed to create container: %v", err)
return
}
defer func() {
if err := testcontainers.TerminateContainer(ctr); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
// }
// createSocatContainer {
const (
// The helloworld container is listening on both ports: 8080 and 8081
port1 = 8080
port2 = 8081
// The helloworld container is not listening on these ports,
// but the socat container will forward the traffic to the correct port
port3 = 9080
port4 = 9081
)
targets := []socat.Target{
socat.NewTarget(port1, "helloworld"), // using a default port
socat.NewTarget(port2, "helloworld"), // using a default port
socat.NewTargetWithInternalPort(port3, port1, "helloworld"), // using a different port
socat.NewTargetWithInternalPort(port4, port2, "helloworld"), // using a different port
}
socatContainer, err := socat.Run(
ctx, socat.DefaultImage,
socat.WithTarget(targets[0]),
socat.WithTarget(targets[1]),
socat.WithTarget(targets[2]),
socat.WithTarget(targets[3]),
network.WithNetwork([]string{"socat"}, nw),
)
if err != nil {
log.Printf("failed to create container: %v", err)
return
}
defer func() {
if err := testcontainers.TerminateContainer(socatContainer); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
// }
httpClient := http.DefaultClient
for _, target := range targets {
baseURI := socatContainer.TargetURL(target.ExposedPort())
resp, err := httpClient.Get(baseURI.String() + "/ping")
if err != nil {
log.Printf("failed to get response: %v", err)
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Printf("failed to read body: %v", err)
return
}
fmt.Printf("%d - %s\n", resp.StatusCode, string(body))
}
Output: 200 - PONG 200 - PONG 200 - PONG 200 - PONG
type Option ¶
type Option func(*options) error
Option is an option for the Socat container.
func WithTarget ¶
WithTarget sets a single target for the socat container. The host of the target must be without the port, as it is internally mapped to the exposed port. Multiple calls to WithTarget will accumulate targets.
func (Option) Customize ¶
func (o Option) Customize(*testcontainers.GenericContainerRequest) error
Customize is a NOOP. It's defined to satisfy the testcontainers.ContainerCustomizer interface.
type Target ¶
type Target struct {
// contains filtered or unexported fields
}
Target represents a target for the socat container. Create a new target with NewTarget or NewTargetWithInternalPort.
func NewTarget ¶
NewTarget creates a new target for the socat container. The host of the target must be without the port, as it is internally mapped to the exposed port. The exposed port is exposed by the socat container.
func NewTargetWithInternalPort ¶
NewTargetWithInternalPort creates a new target for the socat container. The host of the target must be without the port, as it is internally mapped to the exposed port. The exposed port is the port of the socat container, and the internal port is the port of the target container.
func (Target) ExposedPort ¶
ExposedPort returns the exposed port of the target.