-
Notifications
You must be signed in to change notification settings - Fork 731
test: refactor container_kill_linux_test.go to use Tigron #4693
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
base: main
Are you sure you want to change the base?
test: refactor container_kill_linux_test.go to use Tigron #4693
Conversation
| assert.Equal(t, iptablesutil.ForwardExists(t, ipt, chain, containerIP, hostPort), true) | ||
|
|
||
| base.Cmd("kill", testContainerName).AssertOK() | ||
| assert.Equal(t, iptablesutil.ForwardExists(t, ipt, chain, containerIP, hostPort), false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@microness I found the refactoring dropped a check "iptables rule removed after kill". Would you mind adding the post-kill validation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the comment! Fixed.
| t.Skip("pkg/testutil/iptables does not support rootless") | ||
| } | ||
|
|
||
| const hostPort = 9999 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, instead of hard-coding the port number, it is better to use the port number acquired via portlock.Acquire(). So, could you rewrite it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the comment! Fixed.
| // skip if rootless | ||
| if rootlessutil.IsRootless() { | ||
| t.Skip("pkg/testutil/iptables does not support rootless") | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we re-write using nerdtest.Rootless ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the comment! Fixed.
| }, | ||
| } | ||
|
|
||
| testCase.Cleanup = func(data test.Data, helpers test.Helpers) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add logic to release the acquired port number?
- https://github.com/containerd/nerdctl/blob/main/pkg/testutil/portlock/portlock.go#L24
- https://github.com/containerd/nerdctl/blob/main/pkg/testutil/portlock/portlock.go#L59
if p := data.Labels().Get("hostPort"); p != "" {
if port, err := strconv.Atoi(p); err == nil {
_ = portlock.Release(port)
}
}| hostPort, _ := strconv.Atoi(data.Labels().Get("hostPort")) | ||
| chain := data.Labels().Get("chain") | ||
|
|
||
| assert.Equal(t, iptablesutil.ForwardExists(t, ipt, chain, containerIP, hostPort), true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Testing it within the Setup seems odd—couldn't we implement it using helper.Custom?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the comments!
I’ve fixed on your feedback and have a few questions.
I’d appreciate a re-review.
Q1. Using helpers.Custom in Setup
I also felt that putting assert.Assert directly in Setup was a bit awkward,
So I refactored it with helpers.Custom as suggested below.
// Description: "iptables forwarding rule should exist before container is killed"
// Setup:
helpers.Custom(
"iptables",
"-t", "nat",
"-S", chain,
).Run(&test.Expected{
ExitCode: expect.ExitCodeSuccess,
Output: func(stdout string, t tig.T) {
assert.Assert(t, strings.Contains(stdout, fmt.Sprintf("--dport %d", hostPort)))
assert.Assert(t, strings.Contains(stdout, "DNAT"))
assert.Assert(t, strings.Contains(stdout, containerIP))
},
})According to the documentation, helpers can be used in all phases including Setup and Cleanup.
nerdctl/mod/tigron/test/interfaces.go
Lines 70 to 72 in 30bc993
| // Helpers provides a set of helpers to run commands with simple expectations, | |
| // available at all stages of a test (Setup, Cleanup, etc...) | |
| type Helpers interface { |
Does that mean using helpers.Custom in Setup is acceptable in your view?
Q2. Using iptablesutil.ForwardExists vs helpers.Custom with iptables command
For the same reason, I replaced iptablesutil.ForwardExists with a helpers.Custom with iptables command.
Do you think this is a better fit for Setup, or would you prefer keeping the iptablesutil?
Thanks again for the review, I learned a lot.
Signed-off-by: microness <[email protected]>
e195486 to
1529fad
Compare
related: #4613