feat: use an interface for container customization in modules#1042
Merged
mdelapenya merged 13 commits intotestcontainers:mainfrom Apr 14, 2023
Merged
feat: use an interface for container customization in modules#1042mdelapenya merged 13 commits intotestcontainers:mainfrom
mdelapenya merged 13 commits intotestcontainers:mainfrom
Conversation
✅ Deploy Preview for testcontainers-go ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
* main: Add support for LocalStack v2 (testcontainers#994) docs: document tc_host inside the networking section (testcontainers#1041)
48 tasks
mdelapenya
commented
Apr 14, 2023
|
|
||
| // transfer options to the config | ||
|
|
||
| if bucketCustomizer, ok := opt.(bucketCustomizer); ok { |
Member
Author
There was a problem hiding this comment.
@fbiville this is how I'm transferring the functional option state to the container config
Comment on lines
+657
to
+673
| type serviceCustomizer struct { | ||
| enabledService Service | ||
| } | ||
|
|
||
| for _, service := range enabledServices { | ||
| for _, port := range service.ports { | ||
| exposedPorts = append(exposedPorts, port+"/tcp") | ||
| } | ||
| func (c serviceCustomizer) Customize(req *testcontainers.GenericContainerRequest) { | ||
| for _, port := range c.enabledService.ports { | ||
| req.ExposedPorts = append(req.ExposedPorts, port+"/tcp") | ||
| } | ||
| } | ||
|
|
||
| // withService creates a serviceCustomizer for the given service. | ||
| // It's private to prevent users from creating other services than the Analytics and Eventing services. | ||
| func withService(service Service) serviceCustomizer { | ||
| return serviceCustomizer{ | ||
| enabledService: service, | ||
| } | ||
| } |
Member
Author
There was a problem hiding this comment.
@fbiville this is how I'm creating a custom implementation of a customizer for services, holding a state for the services to enable, and adding a Customize request method for the customizer which modifies the container request.
The services, because they are not part of the request, need to be transferred to the config above.
|
Kudos, SonarCloud Quality Gate passed!
|
mdelapenya
commented
Apr 14, 2023
| - Define container options for the module. We consider that a best practice for the options is to return a function that returns a modified `testcontainers.GenericContainerRequest` type, and for that, the library already provides with a `testcontainers.CustomizeRequestOption` type representing this function signature. | ||
| - Define container options for the module leveraging the `testcontainers.ContainerCustomizer` interface, that has one single method: `Customize(req *GenericContainerRequest)`. | ||
| - We consider that a best practice for the options is define a function using the `With` prefix, that returns a function returning a modified `testcontainers.GenericContainerRequest` type. For that, the library already provides with a `testcontainers.CustomizeRequestOption` type implementing the `ContainerCustomizer` interface, and we encourage you use this type for creating your own customizer functions. | ||
| - At the same time, you could need to create your own container customizers for your module. Make sure they implement the `testcontainers.ContainerCustomizer` interface. Defining your own customizer functions is useful when you need to transfer certain state that is not present at the `ContainerRequest` to the container, possibly using an intermediate Config struct. |
This was referenced Apr 14, 2023
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What does this PR do?
This PR leverages a new interface for container customization:
ContainerCustomizer, which has one single method:Customize.Therefore we are adding a polymorphic behaviour for customization, which opens the gate to module authors to create an in-module struct implementing the interface and pass it as a customizer.
As a result, we are updating all existing modules to follow this design.
Why is it important?
Accepting interfaces as options allow users to define their own customizers, making it more flexible the creation of modules.
Related issues