Skip to content

Commit 797b845

Browse files
committed
Merge branch 'main' into docker-model-runner
* main: feat: add toxiproxy module (testcontainers#3092)
2 parents a2b2ead + 6a4ef08 commit 797b845

18 files changed

Lines changed: 1145 additions & 218 deletions

File tree

.github/dependabot.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ updates:
1414
directories:
1515
- /
1616
- /examples/nginx
17-
- /examples/toxiproxy
1817
- /modulegen
1918
- /modules/aerospike
2019
- /modules/arangodb
@@ -68,6 +67,7 @@ updates:
6867
- /modules/scylladb
6968
- /modules/socat
7069
- /modules/surrealdb
70+
- /modules/toxiproxy
7171
- /modules/valkey
7272
- /modules/vault
7373
- /modules/vearch

.vscode/.testcontainers-go.code-workspace

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
"name": "example / nginx",
1010
"path": "../examples/nginx"
1111
},
12-
{
13-
"name": "example / toxiproxy",
14-
"path": "../examples/toxiproxy"
15-
},
1612
{
1713
"name": "module / aerospike",
1814
"path": "../modules/aerospike"
@@ -221,6 +217,10 @@
221217
"name": "module / surrealdb",
222218
"path": "../modules/surrealdb"
223219
},
220+
{
221+
"name": "module / toxiproxy",
222+
"path": "../modules/toxiproxy"
223+
},
224224
{
225225
"name": "module / valkey",
226226
"path": "../modules/valkey"

docs/examples/toxiproxy.md

Lines changed: 0 additions & 9 deletions
This file was deleted.

docs/modules/toxiproxy.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Toxiproxy
2+
3+
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>
4+
5+
## Introduction
6+
7+
The Testcontainers module for Toxiproxy.
8+
9+
## Adding this module to your project dependencies
10+
11+
Please run the following command to add the Toxiproxy module to your Go dependencies:
12+
13+
```
14+
go get github.com/testcontainers/testcontainers-go/modules/toxiproxy
15+
```
16+
17+
## Usage example
18+
19+
<!--codeinclude-->
20+
[Creating a Toxiproxy container](../../modules/toxiproxy/examples_test.go) inside_block:runToxiproxyContainer
21+
<!--/codeinclude-->
22+
23+
## Module Reference
24+
25+
### Run function
26+
27+
- 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>
28+
29+
The Toxiproxy module exposes one entrypoint function to create the Toxiproxy container, and this function receives three parameters:
30+
31+
```golang
32+
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error)
33+
```
34+
35+
- `context.Context`, the Go context.
36+
- `string`, the Docker image to use.
37+
- `testcontainers.ContainerCustomizer`, a variadic argument for passing options.
38+
39+
### Container Ports
40+
41+
The Toxiproxy container exposes the following ports:
42+
43+
- `8474/tcp`, the Toxiproxy control port, exported as `toxiproxy.ControlPort`.
44+
45+
### Container Options
46+
47+
When starting the Toxiproxy container, you can pass options in a variadic way to configure it.
48+
49+
#### Image
50+
51+
Use the second argument in the `Run` function to set a valid Docker image.
52+
In example: `Run(context.Background(), "shopify/toxiproxy:2.12.0")`.
53+
54+
{% include "../features/common_functional_options.md" %}
55+
56+
#### WithProxy
57+
58+
- 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>
59+
60+
The `WithProxy` option allows you to specify a proxy to be created on the Toxiproxy container.
61+
This option allocates a random port on the host and exposes it to the Toxiproxy container, allowing
62+
you to create a unique proxy for a given service, starting from the `8666/tcp` port.
63+
64+
```golang
65+
func WithProxy(name string, upstream string) Option
66+
```
67+
68+
If this option is used in combination with the `WithConfigFile` option, the proxy defined in this option
69+
is added to the proxies defined in the config file.
70+
71+
!!!info
72+
If you add proxies in a programmatic manner using the Toxiproxy client, then you need to manually
73+
add exposed ports in the Toxiproxy container.
74+
75+
#### WithConfigFile
76+
77+
- 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>
78+
79+
The `WithConfigFile` option allows you to specify a config file for the Toxiproxy container, in the form of an `io.Reader` representing
80+
the JSON file with the Toxiproxy configuration, in the valid format of the Toxiproxy configuration file.
81+
82+
<!--codeinclude-->
83+
[Configuration file](../../modules/toxiproxy/testdata/toxiproxy.json)
84+
<!--/codeinclude-->
85+
86+
```golang
87+
func WithConfigFile(r io.Reader) testcontainers.CustomizeRequestOption
88+
```
89+
90+
If this option is used in combination with the `WithProxy` option, the proxies defined in this option
91+
are added to the proxies defined with the `WithProxy` option.
92+
93+
### Container Methods
94+
95+
The Toxiproxy container exposes the following methods:
96+
97+
#### ProxiedEndpoint
98+
99+
- 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>
100+
101+
The `ProxiedEndpoint` method returns the host and port of the proxy for a given port. It's used to create new connections to the proxied service, and it returns an error in case the port has no proxy.
102+
103+
```golang
104+
func (c *Container) ProxiedEndpoint(port int) (string, string, error)
105+
```
106+
107+
<!--codeinclude-->
108+
[Get Proxied Endpoint](../../modules/toxiproxy/examples_test.go) inside_block:getProxiedEndpoint
109+
[Read Proxied Endpoint](../../modules/toxiproxy/examples_test.go) inside_block:readProxiedEndpoint
110+
<!--/codeinclude-->
111+
112+
The above examples show how to get the proxied endpoint and use it to create a new connection to the proxied service, in this case a Redis client.
113+
114+
#### URI
115+
116+
- 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>
117+
118+
The `URI` method returns the URI of the Toxiproxy container, used to create a new Toxiproxy client.
119+
120+
```golang
121+
func (c *Container) URI() string
122+
```
123+
124+
<!--codeinclude-->
125+
[Creating a Toxiproxy client](../../modules/toxiproxy/examples_test.go) inside_block:createToxiproxyClient
126+
<!--/codeinclude-->
127+
128+
- the `toxiproxy` package comes from the `github.com/Shopify/toxiproxy/v2/client` package.
129+
- the `toxiproxyContainer` variable has been created by the `Run` function.
130+
131+
### Examples
132+
133+
#### Programmatically create a proxy
134+
135+
<!--codeinclude-->
136+
[Expose port manually](../../modules/toxiproxy/examples_test.go) inside_block:defineContainerExposingPort
137+
[Creating a proxy](../../modules/toxiproxy/examples_test.go) inside_block:createProxy
138+
[Creating a Redis client](../../modules/toxiproxy/examples_test.go) inside_block:createRedisClient
139+
[Adding a latency toxic](../../modules/toxiproxy/examples_test.go) inside_block:addLatencyToxic
140+
141+
<!--/codeinclude-->

examples/toxiproxy/redis.go

Lines changed: 0 additions & 35 deletions
This file was deleted.

examples/toxiproxy/toxiproxy.go

Lines changed: 0 additions & 55 deletions
This file was deleted.

examples/toxiproxy/toxiproxy_test.go

Lines changed: 0 additions & 76 deletions
This file was deleted.

mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ nav:
120120
- modules/scylladb.md
121121
- modules/socat.md
122122
- modules/surrealdb.md
123+
- modules/toxiproxy.md
123124
- modules/valkey.md
124125
- modules/vault.md
125126
- modules/vearch.md
@@ -128,7 +129,6 @@ nav:
128129
- Examples:
129130
- examples/index.md
130131
- examples/nginx.md
131-
- examples/toxiproxy.md
132132
- System Requirements:
133133
- system_requirements/index.md
134134
- system_requirements/docker.md

0 commit comments

Comments
 (0)