Skip to content
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

feat: use an interface for container customization in modules #1042

Merged
merged 13 commits into from
Apr 14, 2023

Conversation

mdelapenya
Copy link
Member

@mdelapenya mdelapenya commented Apr 10, 2023

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

@mdelapenya mdelapenya requested a review from a team as a code owner April 10, 2023 08:21
@mdelapenya mdelapenya added the feature New functionality or new behaviors on the existing one label Apr 10, 2023
@mdelapenya mdelapenya self-assigned this Apr 10, 2023
@netlify
Copy link

netlify bot commented Apr 10, 2023

Deploy Preview for testcontainers-go ready!

Name Link
🔨 Latest commit ab0f60d
🔍 Latest deploy log https://app.netlify.com/sites/testcontainers-go/deploys/64391e42242f79000811dd97
😎 Deploy Preview https://deploy-preview-1042--testcontainers-go.netlify.app/modules
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.


// transfer options to the config

if bucketCustomizer, ok := opt.(bucketCustomizer); ok {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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,
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

@sonarcloud
Copy link

sonarcloud bot commented Apr 14, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

- 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.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fbiville explained here in the docs

@mdelapenya mdelapenya merged commit a9c1008 into testcontainers:main Apr 14, 2023
@mdelapenya mdelapenya deleted the customize-option-interface branch April 14, 2023 10:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality or new behaviors on the existing one
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant