|
| 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--> |
0 commit comments