-
-
Notifications
You must be signed in to change notification settings - Fork 547
feat: add socat container #3071
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
fd2bdcd
chore: create socat module from modulegen
mdelapenya 055f790
chore: implement socat
mdelapenya 0c92b8d
chore: bump tc-go
mdelapenya d46b8cf
chore: mod tidy
mdelapenya 1648611
chore: fix examples
mdelapenya fc13d97
fix: lint
mdelapenya 4c9d2f7
chore: make field private
mdelapenya 613fb6e
fix: remove from copy&paste
mdelapenya 6afa458
chore: make option return error
mdelapenya da9fb0d
chore: simplify passing the port
mdelapenya f3967af
docs: document functions
mdelapenya a646bdd
chore: simplify WithTarget option to avoid variad arguments
mdelapenya 7b0aeeb
docs: describe socat
mdelapenya 5083dff
chore: add a test with multiple targets
mdelapenya f2628c0
chore: more illustrative examples
mdelapenya 400c495
chore: add unit tests for target validation
mdelapenya 340e336
fix: use errors.New
mdelapenya 0423819
Merge branch 'main' into socat-module
mdelapenya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# Socat | ||
|
||
Not available until the next release of testcontainers-go <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a> | ||
|
||
## Introduction | ||
|
||
The Testcontainers module for Socat, a utility container that provides TCP port forwarding and network tunneling between services, enabling transparent communication between containers and networks. | ||
|
||
This is particularly useful in testing scenarios where you need to simulate network connections or provide transparent access to services running in different containers. | ||
|
||
## Adding this module to your project dependencies | ||
|
||
Please run the following command to add the Socat module to your Go dependencies: | ||
|
||
``` | ||
go get github.com/testcontainers/testcontainers-go/modules/socat | ||
``` | ||
|
||
## Usage example | ||
|
||
<!--codeinclude--> | ||
[Create a Network](../../modules/socat/examples_test.go) inside_block:createNetwork | ||
[Create a Hello World Container](../../modules/socat/examples_test.go) inside_block:createHelloWorldContainer | ||
[Create a Socat Container](../../modules/socat/examples_test.go) inside_block:createSocatContainer | ||
[Read from Socat Container](../../modules/socat/examples_test.go) inside_block:readFromSocat | ||
<!--/codeinclude--> | ||
|
||
## Module Reference | ||
|
||
### Run function | ||
|
||
- Not available until the next release of testcontainers-go <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a> | ||
|
||
The Socat module exposes one entrypoint function to create the Socat container, and this function receives three parameters: | ||
|
||
```golang | ||
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*SocatContainer, error) | ||
``` | ||
|
||
- `context.Context`, the Go context. | ||
- `string`, the Docker image to use. | ||
- `testcontainers.ContainerCustomizer`, a variadic argument for passing options. | ||
|
||
### Container Options | ||
|
||
When starting the Socat container, you can pass options in a variadic way to configure it. | ||
|
||
#### Image | ||
|
||
Use the second argument in the `Run` function to set a valid Docker image. | ||
In example: `Run(context.Background(), "alpine/socat:1.8.0.1")`. | ||
|
||
{% include "../features/common_functional_options.md" %} | ||
|
||
#### WithTarget | ||
|
||
The `WithTarget` function sets a single target for the Socat container, defined by the `Target` struct. | ||
This struct can be built using the the following functions: | ||
|
||
- `NewTarget(exposedPort int, host string)`: Creates a new target for the Socat container. The target's internal port is set to the same value as the exposed port. | ||
- `NewTargetWithInternalPort(exposedPort int, internalPort int, host string)`: Creates a new target for the Socat container with an internal port. Use this function when you want to map a container to a different port than the default one. | ||
|
||
<!--codeinclude--> | ||
[Passing a target](../../modules/socat/examples_test.go) inside_block:createSocatContainer | ||
<!--/codeinclude--> | ||
|
||
In the above example, there is a `helloworld` container thatis listening on port `8080` and `8081`. Please check [the helloworld container source code](https://github.com/testcontainers/helloworld/blob/141af7909907e04b124e691d3cd6fc7c32da2207/internal/server/server.go#L26-L27) for more details. | ||
|
||
### Container Methods | ||
|
||
The Socat container exposes the following methods: | ||
|
||
#### TargetURL | ||
|
||
The `TargetURL(port int)` method returns the URL for the exposed port of a target, nil if the port is not mapped. | ||
|
||
<!--codeinclude--> | ||
[Read from Socat using TargetURL](../../modules/socat/examples_test.go) inside_block:readFromSocat | ||
<!--/codeinclude--> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
include ../../commons-test.mk | ||
|
||
.PHONY: test | ||
test: | ||
$(MAKE) test-socat |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
package socat_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/testcontainers/testcontainers-go" | ||
"github.com/testcontainers/testcontainers-go/modules/socat" | ||
"github.com/testcontainers/testcontainers-go/network" | ||
) | ||
|
||
func ExampleRun() { | ||
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.GenericContainer(ctx, testcontainers.GenericContainerRequest{ | ||
ContainerRequest: testcontainers.ContainerRequest{ | ||
Image: "testcontainers/helloworld:1.2.0", | ||
ExposedPorts: []string{"8080/tcp"}, | ||
Networks: []string{nw.Name}, | ||
NetworkAliases: map[string][]string{ | ||
nw.Name: {"helloworld"}, | ||
}, | ||
}, | ||
Started: true, | ||
}) | ||
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, "alpine/socat:1.8.0.1", | ||
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 | ||
} | ||
|
||
func ExampleRun_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.GenericContainer(ctx, testcontainers.GenericContainerRequest{ | ||
ContainerRequest: testcontainers.ContainerRequest{ | ||
Image: "testcontainers/helloworld:1.2.0", | ||
ExposedPorts: []string{"8080/tcp"}, | ||
Networks: []string{nw.Name}, | ||
NetworkAliases: map[string][]string{ | ||
nw.Name: {"helloworld"}, | ||
}, | ||
}, | ||
Started: true, | ||
}) | ||
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, "alpine/socat:1.8.0.1", | ||
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
module github.com/testcontainers/testcontainers-go/modules/socat | ||
|
||
go 1.23.0 | ||
|
||
toolchain go1.23.6 | ||
|
||
require ( | ||
github.com/docker/go-connections v0.5.0 | ||
github.com/stretchr/testify v1.10.0 | ||
github.com/testcontainers/testcontainers-go v0.36.0 | ||
) | ||
|
||
require ( | ||
dario.cat/mergo v1.0.1 // indirect | ||
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect | ||
github.com/Microsoft/go-winio v0.6.2 // indirect | ||
github.com/cenkalti/backoff/v4 v4.2.1 // indirect | ||
github.com/containerd/log v0.1.0 // indirect | ||
github.com/containerd/platforms v0.2.1 // indirect | ||
github.com/cpuguy83/dockercfg v0.3.2 // indirect | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/distribution/reference v0.6.0 // indirect | ||
github.com/docker/docker v28.0.1+incompatible // indirect | ||
github.com/docker/go-units v0.5.0 // indirect | ||
github.com/ebitengine/purego v0.8.2 // indirect | ||
github.com/felixge/httpsnoop v1.0.4 // indirect | ||
github.com/go-logr/logr v1.4.2 // indirect | ||
github.com/go-logr/stdr v1.2.2 // indirect | ||
github.com/go-ole/go-ole v1.2.6 // indirect | ||
github.com/gogo/protobuf v1.3.2 // indirect | ||
github.com/google/uuid v1.6.0 // indirect | ||
github.com/klauspost/compress v1.17.4 // indirect | ||
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect | ||
github.com/magiconair/properties v1.8.9 // indirect | ||
github.com/moby/docker-image-spec v1.3.1 // indirect | ||
github.com/moby/patternmatcher v0.6.0 // indirect | ||
github.com/moby/sys/sequential v0.5.0 // indirect | ||
github.com/moby/sys/user v0.1.0 // indirect | ||
github.com/moby/sys/userns v0.1.0 // indirect | ||
github.com/moby/term v0.5.0 // indirect | ||
github.com/morikuni/aec v1.0.0 // indirect | ||
github.com/opencontainers/go-digest v1.0.0 // indirect | ||
github.com/opencontainers/image-spec v1.1.1 // indirect | ||
github.com/pkg/errors v0.9.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect | ||
github.com/shirou/gopsutil/v4 v4.25.1 // indirect | ||
github.com/sirupsen/logrus v1.9.3 // indirect | ||
github.com/tklauser/go-sysconf v0.3.12 // indirect | ||
github.com/tklauser/numcpus v0.6.1 // indirect | ||
github.com/yusufpapurcu/wmi v1.2.4 // indirect | ||
go.opentelemetry.io/auto/sdk v1.1.0 // indirect | ||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect | ||
go.opentelemetry.io/otel v1.35.0 // indirect | ||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.35.0 // indirect | ||
go.opentelemetry.io/otel/metric v1.35.0 // indirect | ||
go.opentelemetry.io/otel/sdk v1.35.0 // indirect | ||
go.opentelemetry.io/otel/trace v1.35.0 // indirect | ||
go.opentelemetry.io/proto/otlp v1.5.0 // indirect | ||
golang.org/x/crypto v0.31.0 // indirect | ||
golang.org/x/sys v0.31.0 // indirect | ||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250313205543-e70fdf4c4cb4 // indirect | ||
google.golang.org/protobuf v1.36.5 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) | ||
|
||
replace github.com/testcontainers/testcontainers-go => ../.. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.