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

#1152 execute HostConfigModifier at last #1153

Merged
merged 9 commits into from
May 24, 2023

Conversation

xmh19936688
Copy link
Contributor

#1152 execute HostConfigModifier at last, or some values will be overwrited

What does this PR do?

execute HostConfigModifier at last

Why is it important?

Currently some values such as PortBindings will be overwrited.

Related issues

testcontainers#1152  execute HostConfigModifier at last, or some values will be overwrite
@xmh19936688 xmh19936688 requested a review from a team as a code owner May 6, 2023 09:37
@netlify
Copy link

netlify bot commented May 6, 2023

Deploy Preview for testcontainers-go ready!

Name Link
🔨 Latest commit 959635c
🔍 Latest deploy log https://app.netlify.com/sites/testcontainers-go/deploys/6465cefd981521000833ac37
😎 Deploy Preview https://deploy-preview-1153--testcontainers-go.netlify.app
📱 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.

@mdelapenya
Copy link
Collaborator

@xmh19936688 could you extend the existing unit tests to demonstrate the bug? 🙏

lifecycle_test.go Outdated Show resolved Hide resolved
lifecycle_test.go Outdated Show resolved Hide resolved
@mdelapenya
Copy link
Collaborator

golangci-lint is complaining that there is a file not go-fmt'ed. Could you check it?

@mdelapenya
Copy link
Collaborator

Could you double check that the existing tests are passing? I see TestPreCreateModifierHook failing on CI too

@xmh19936688
Copy link
Contributor Author

At line 110, it says Host config's port bindings should be overwritten by the modifier. But I noticed that HostIP and HostPort has been modified at line 48 and 49. Why the expectd value is empty string at line 105 and 106? What's the correct behavior?

@mdelapenya
Copy link
Collaborator

Why the expectd value is empty string at line 105 and 106?

I think the tests in this case are driven by the implementation/execution of nat.ParsePortSpecs, and after debugging that function, the returned binding comes empty for the passed exposed port.

What's the correct behavior?

What is your idea or use case for it? I remember implementing the lifecycle code as a refactor of the at-that-time existing code, so I tried to replicate it adding unit tests for it.

@xmh19936688
Copy link
Contributor Author

I'd like to specify a host port instead of the random one. So the prerequisite of specifing hostIP and hostPort is it has been exposed, right?

@mdelapenya
Copy link
Collaborator

I'd like to specify a host port instead of the random one. So the prerequisite of specifing hostIP and hostPort is it has been exposed, right?

I see your point: because the host config modifier allows to define e.g. custom PortBindings, then they are not applied because the ParsePortSpecs call takes place last.

Then I'm OK with changing the order. Do you think we can update the tests to represent the changes in the behaviours?

@xmh19936688
Copy link
Contributor Author

Do you think we can update the tests to represent the changes in the behaviours?

Sounds good! I think we need some cases to represent how ExposedPorts and HostConfigModifier effacts.
For example: ExposedPorts contains 2379 and 2380, and HostConfigModifier binds 2380 to 32380 and 8080 to 38080. What will the result be? (Like this: 2379 to a random hostport, 2380 to 32380, no 8080 ?)

The port in image.ContainerConfig.ExposedPorts may also have affect, do they exposed implicitly?

@mdelapenya
Copy link
Collaborator

The port in image.ContainerConfig.ExposedPorts may also have affect, do they exposed implicitly?

We are only exposing those ports automatically if the container request exposes zero ports and the the container does not run in a container network:

if len(exposedPorts) == 0 && !hostConfig.NetworkMode.IsContainer() {

@xmh19936688
Copy link
Contributor Author

if len(exposedPorts) == 0 && !hostConfig.NetworkMode.IsContainer() {

Apologize for not getting you. Should I do with that if like this?

if len(exposedPorts) == 0 && !hostConfig.NetworkMode.IsContainer() {
    hostConfig.PortBindings = exposedPortMap
} else {
    hostConfig.PortBindings = mergePortBindings(hostConfig.PortBindings, exposedPortMap, req.ExposedPorts)
}

@mdelapenya
Copy link
Collaborator

if len(exposedPorts) == 0 && !hostConfig.NetworkMode.IsContainer() {
    hostConfig.PortBindings = exposedPortMap
} else {
    hostConfig.PortBindings = mergePortBindings(hostConfig.PortBindings, exposedPortMap, req.ExposedPorts)
}

I think that will do it: if there are no exposed ports, then keep the behavior of nat.ParsePortSpecs(exposedPorts), else merge with the fix in this PR. We could probably add a comment before the if/else block explaining why we need and not need to merge the port bindings.

wdyt?

@sonarcloud
Copy link

sonarcloud bot commented May 18, 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
14.1% 14.1% Duplication

@mdelapenya mdelapenya self-assigned this May 23, 2023
@mdelapenya mdelapenya added the bug An issue with the library label May 23, 2023
Copy link
Collaborator

@mdelapenya mdelapenya left a comment

Choose a reason for hiding this comment

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

LGTM, I'm waiting for the rest of the GH actions to pass to update this branch including all dependabot updates. Once finished, I'll merge this one.

Thanks for your patience!! 🙏

@mdelapenya mdelapenya merged commit 0d47b42 into testcontainers:main May 24, 2023
56 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An issue with the library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: config portBindings in HostConfigModifier do not work
2 participants