-
-
Notifications
You must be signed in to change notification settings - Fork 585
feat: add toxiproxy module #3092
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
23 commits
Select commit
Hold shift + click to select a range
47b7c04
chore: initial toxiproxy module
mdelapenya cbc16e2
chore: add examples and tests for toxiproxy
mdelapenya 8bfd1c6
docs: update container types in code examples
mdelapenya f3890bd
docs: add examples
mdelapenya c1a3e8f
docs: document exposed variables
mdelapenya aebe4e2
fix: handle error in tests
mdelapenya 70c735b
fix: lint
mdelapenya c66b273
chore: add resiliency to the tests
mdelapenya 753446b
feat: add an option to pass a config file
mdelapenya 4b19c68
feat: simplify the addition of proxies and ports
mdelapenya 0200694
fix: lint
mdelapenya c157cd5
chore: run mod tidy
mdelapenya 5cdf3e3
docs: example is programmatic
mdelapenya fa0cbc3
Merge branch 'main' into toxyproxy-module
mdelapenya f696d05
chore: use new option for exposed ports
mdelapenya e99d830
chore: increase jitter for accuracy in tests
mdelapenya 58d47e1
Merge branch 'main' into toxyproxy-module
mdelapenya 1f09855
Merge branch 'main' into toxyproxy-module
mdelapenya 231ca56
fix: use pointer semantics
mdelapenya a303220
chore: reorder struct fields for readability
mdelapenya 3ab7de3
Merge branch 'main' into toxyproxy-module
mdelapenya 0ef3854
Merge branch 'main' into toxyproxy-module
mdelapenya 32fce99
Merge branch 'main' into toxyproxy-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 was deleted.
Oops, something went wrong.
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,141 @@ | ||
| # Toxiproxy | ||
|
|
||
| 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 Toxiproxy. | ||
|
|
||
| ## Adding this module to your project dependencies | ||
|
|
||
| Please run the following command to add the Toxiproxy module to your Go dependencies: | ||
|
|
||
| ``` | ||
| go get github.com/testcontainers/testcontainers-go/modules/toxiproxy | ||
| ``` | ||
|
|
||
| ## Usage example | ||
|
|
||
| <!--codeinclude--> | ||
| [Creating a Toxiproxy container](../../modules/toxiproxy/examples_test.go) inside_block:runToxiproxyContainer | ||
| <!--/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 Toxiproxy module exposes one entrypoint function to create the Toxiproxy container, and this function receives three parameters: | ||
|
|
||
| ```golang | ||
| func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error) | ||
| ``` | ||
|
|
||
| - `context.Context`, the Go context. | ||
| - `string`, the Docker image to use. | ||
| - `testcontainers.ContainerCustomizer`, a variadic argument for passing options. | ||
|
|
||
| ### Container Ports | ||
|
|
||
| The Toxiproxy container exposes the following ports: | ||
|
|
||
| - `8474/tcp`, the Toxiproxy control port, exported as `toxiproxy.ControlPort`. | ||
|
|
||
| ### Container Options | ||
|
|
||
| When starting the Toxiproxy 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(), "shopify/toxiproxy:2.12.0")`. | ||
|
|
||
| {% include "../features/common_functional_options.md" %} | ||
|
|
||
| #### WithProxy | ||
|
|
||
| - 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 `WithProxy` option allows you to specify a proxy to be created on the Toxiproxy container. | ||
| This option allocates a random port on the host and exposes it to the Toxiproxy container, allowing | ||
| you to create a unique proxy for a given service, starting from the `8666/tcp` port. | ||
|
|
||
| ```golang | ||
| func WithProxy(name string, upstream string) Option | ||
| ``` | ||
|
|
||
| If this option is used in combination with the `WithConfigFile` option, the proxy defined in this option | ||
| is added to the proxies defined in the config file. | ||
|
|
||
| !!!info | ||
| If you add proxies in a programmatic manner using the Toxiproxy client, then you need to manually | ||
| add exposed ports in the Toxiproxy container. | ||
|
|
||
| #### WithConfigFile | ||
|
|
||
| - 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 `WithConfigFile` option allows you to specify a config file for the Toxiproxy container, in the form of an `io.Reader` representing | ||
| the JSON file with the Toxiproxy configuration, in the valid format of the Toxiproxy configuration file. | ||
|
|
||
| <!--codeinclude--> | ||
| [Configuration file](../../modules/toxiproxy/testdata/toxiproxy.json) | ||
| <!--/codeinclude--> | ||
|
|
||
| ```golang | ||
| func WithConfigFile(r io.Reader) testcontainers.CustomizeRequestOption | ||
| ``` | ||
|
|
||
| If this option is used in combination with the `WithProxy` option, the proxies defined in this option | ||
| are added to the proxies defined with the `WithProxy` option. | ||
|
|
||
| ### Container Methods | ||
|
|
||
| The Toxiproxy container exposes the following methods: | ||
|
|
||
| #### ProxiedEndpoint | ||
|
|
||
| - 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 `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. | ||
|
|
||
| ```golang | ||
| func (c *Container) ProxiedEndpoint(port int) (string, string, error) | ||
| ``` | ||
|
|
||
| <!--codeinclude--> | ||
| [Get Proxied Endpoint](../../modules/toxiproxy/examples_test.go) inside_block:getProxiedEndpoint | ||
| [Read Proxied Endpoint](../../modules/toxiproxy/examples_test.go) inside_block:readProxiedEndpoint | ||
| <!--/codeinclude--> | ||
|
|
||
| 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. | ||
|
|
||
| #### URI | ||
|
|
||
| - 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 `URI` method returns the URI of the Toxiproxy container, used to create a new Toxiproxy client. | ||
|
|
||
| ```golang | ||
| func (c *Container) URI() string | ||
| ``` | ||
|
|
||
| <!--codeinclude--> | ||
| [Creating a Toxiproxy client](../../modules/toxiproxy/examples_test.go) inside_block:createToxiproxyClient | ||
| <!--/codeinclude--> | ||
|
|
||
| - the `toxiproxy` package comes from the `github.com/Shopify/toxiproxy/v2/client` package. | ||
| - the `toxiproxyContainer` variable has been created by the `Run` function. | ||
|
|
||
| ### Examples | ||
|
|
||
| #### Programmatically create a proxy | ||
|
|
||
| <!--codeinclude--> | ||
| [Expose port manually](../../modules/toxiproxy/examples_test.go) inside_block:defineContainerExposingPort | ||
| [Creating a proxy](../../modules/toxiproxy/examples_test.go) inside_block:createProxy | ||
| [Creating a Redis client](../../modules/toxiproxy/examples_test.go) inside_block:createRedisClient | ||
| [Adding a latency toxic](../../modules/toxiproxy/examples_test.go) inside_block:addLatencyToxic | ||
|
|
||
| <!--/codeinclude--> | ||
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
File renamed without changes.
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.