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

Added tests for the nodeportlocal package #5917

Merged
merged 1 commit into from
Feb 20, 2024

Conversation

VaibhavMalik4187
Copy link
Contributor

This commit introduces new tests for the pkg/agent/nodeportlocal/rules package.

Fixes: #5650

@VaibhavMalik4187 VaibhavMalik4187 marked this pull request as draft January 24, 2024 20:58
@VaibhavMalik4187 VaibhavMalik4187 marked this pull request as ready for review January 25, 2024 08:20
@VaibhavMalik4187
Copy link
Contributor Author

@luolanzone, the netnat_rule.go file is giving the following warning:

This file is within module ".", which is not included in your workspace.
To fix this problem, you can add a go.work file that uses this directory.
See the documentation for more information on setting up your workspace:
https://github.com/golang/tools/blob/master/gopls/doc/workspace.md.go list

The iptables commands in the iptable_rule.go are failing due to the permission denied error when run during the tests.
Could you share some suggestions on how to fix these issues?

@tnqn
Copy link
Member

tnqn commented Jan 29, 2024

The iptables commands in the iptable_rule.go are failing due to the permission denied error when run during the tests.
Could you share some suggestions on how to fix these issues?

Unit tests shouldn't need to really interact with iptables of the system. Instead, you should replace the iptables instance with a mock implementation and only validate if the expected calls has been made. Check the following test as an example:

mockIPTables := iptablestest.NewMockInterface(ctrl)

@VaibhavMalik4187 VaibhavMalik4187 force-pushed the nodeportlocal-tests branch 2 times, most recently from a7eca14 to 1371690 Compare January 30, 2024 13:46
@VaibhavMalik4187
Copy link
Contributor Author

The iptables commands in the iptable_rule.go are failing due to the permission denied error when run during the tests.
Could you share some suggestions on how to fix these issues?

Unit tests shouldn't need to really interact with iptables of the system. Instead, you should replace the iptables instance with a mock implementation and only validate if the expected calls has been made. Check the following test as an example:

mockIPTables := iptablestest.NewMockInterface(ctrl)

It makes sense, thanks for the suggestion. I've updated the code, I think that this PR is ready for a code review.

@XinShuYang
Copy link
Contributor

hi @tnqn, do you know how to enable the github action in this PR for the UT workflow?

@tnqn
Copy link
Member

tnqn commented Feb 4, 2024

The iptables commands in the iptable_rule.go are failing due to the permission denied error when run during the tests.
Could you share some suggestions on how to fix these issues?

Unit tests shouldn't need to really interact with iptables of the system. Instead, you should replace the iptables instance with a mock implementation and only validate if the expected calls has been made. Check the following test as an example:

mockIPTables := iptablestest.NewMockInterface(ctrl)

It makes sense, thanks for the suggestion. I've updated the code, I think that this PR is ready for a code review.

I don't see the code reflects the change. It still calls the iptables command of the system. As seen from the test code, none of the call can succeed, so the test can't really ensure code work as expected. TestAddAndDeleteRule can get a "Successfully added DNAT rule" only because it uses an empty iptables.Client{}, which does nothing actually.

I think you should at least replace *iptables.Client in iptablesRules with iptables.Interface then you can use a mock implementation and check whether expected calls are made. The link I shared have more examples.

@tnqn
Copy link
Member

tnqn commented Feb 4, 2024

hi @tnqn, do you know how to enable the github action in this PR for the UT workflow?

First time contributor needs a manual approval before the the tests can run. But since the code is not correct, I haven't clicked it yet.

@VaibhavMalik4187
Copy link
Contributor Author

The iptables commands in the iptable_rule.go are failing due to the permission denied error when run during the tests.
Could you share some suggestions on how to fix these issues?

Unit tests shouldn't need to really interact with iptables of the system. Instead, you should replace the iptables instance with a mock implementation and only validate if the expected calls has been made. Check the following test as an example:

mockIPTables := iptablestest.NewMockInterface(ctrl)

It makes sense, thanks for the suggestion. I've updated the code, I think that this PR is ready for a code review.

I don't see the code reflects the change. It still calls the iptables command of the system. As seen from the test code, none of the call can succeed, so the test can't really ensure code work as expected. TestAddAndDeleteRule can get a "Successfully added DNAT rule" only because it uses an empty iptables.Client{}, which does nothing actually.

I think you should at least replace *iptables.Client in iptablesRules with iptables.Interface then you can use a mock implementation and check whether expected calls are made. The link I shared have more examples.

Thanks for sharing the pointer @tnqn. This time, I was able to write some tests using the mockIPTable interface.

@VaibhavMalik4187 VaibhavMalik4187 force-pushed the nodeportlocal-tests branch 2 times, most recently from 9d25d17 to ed9f5a9 Compare February 7, 2024 05:37
@VaibhavMalik4187
Copy link
Contributor Author

@tnqn, I've made some updates to the code. Could you please review it again and let me know if the tests are now effective?

Copy link
Member

@tnqn tnqn left a comment

Choose a reason for hiding this comment

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

I think this version is on the right direction.

pkg/agent/nodeportlocal/rules/iptable_rule_test.go Outdated Show resolved Hide resolved
pkg/agent/nodeportlocal/rules/iptable_rule_test.go Outdated Show resolved Hide resolved
pkg/agent/nodeportlocal/rules/iptable_rule_test.go Outdated Show resolved Hide resolved
PodPort: 17,
PodIP: "localhost",
Protocol: "tcp",
Protocols: []string{"tcp", "udp"},
Copy link
Member

Choose a reason for hiding this comment

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

The field is deprecated, you could just set Protocol.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Protocols are still being used at:

for _, protocol := range nplData.Protocols {

This is why I added this field to the tests. I've removed it in the latest version though.

Copy link
Member

Choose a reason for hiding this comment

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

You are right. The code shouldn't keep using it. It will be changed by #5943 (review). For this PR, it will have to expect wrong output (note that the iptables rules used by "iptables-restore" are empty)

pkg/agent/nodeportlocal/rules/iptable_rule_test.go Outdated Show resolved Hide resolved
pkg/agent/nodeportlocal/rules/iptable_rule_test.go Outdated Show resolved Hide resolved
This commit introduces new tests for the `pkg/agent/nodeportlocal/rules`
package.

Fixes: antrea-io#5650

Signed-off-by: VaibhavMalik4187 <vaibhavmalik2018@gmail.com>
@tnqn
Copy link
Member

tnqn commented Feb 20, 2024

/skip-all

Copy link
Member

@tnqn tnqn left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @VaibhavMalik4187

@tnqn tnqn merged commit a13f054 into antrea-io:main Feb 20, 2024
50 of 54 checks passed
@VaibhavMalik4187
Copy link
Contributor Author

VaibhavMalik4187 commented Feb 20, 2024

LGTM, thanks @VaibhavMalik4187

Thank you for the pointers and the code review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve NodePortLocal unit test coverage
3 participants