-
Notifications
You must be signed in to change notification settings - Fork 373
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
Add unit test for pkg/agent/route #4295
Conversation
550f54e
to
07df504
Compare
Codecov Report
@@ Coverage Diff @@
## main #4295 +/- ##
==========================================
+ Coverage 60.60% 61.75% +1.15%
==========================================
Files 378 389 +11
Lines 54850 55654 +804
==========================================
+ Hits 33240 34371 +1131
+ Misses 19095 18673 -422
- Partials 2515 2610 +95
*This pull request uses carry forward flags. Click here to find out more.
|
1c86482
to
989fc96
Compare
95492e8
to
735a773
Compare
b611ffd
to
b359f49
Compare
/test-all |
2c6d072
to
276ec69
Compare
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.
overall lgtm
I assume that changes outside of route_linux_test.go
are cosmetic changes to allow for writing unit tests
It seems that code coverage has some issues since it reports that coverage will decrease when we merge this PR? Is it addressed by #4226 or it some other issue?
Restore(data string, flush bool, useIPv6 bool) error | ||
|
||
Save() ([]byte, error) |
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 you explain why Save
returns a []byte
but Restore
takes a string
as parameter? Intuitively the 2 functions should be "symmetric".
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.
For Restore
, if we use []byte
as the argument type, it's difficult to know what's the actual argument the method was called with when the test fails. The error message would show two long slices of bytes which are not readable, and developer needs to change the argument type like I do in this patch to get what's wrong.
For Save
, we could also change it to return string, but the above readability issue doesn't exist as we could convert the returned []byte
to string in test code. Since exec.Command
returns []byte
and the only consumer dumpIPTables
uses []byte
, I didn't make the change to avoid two conversions.
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.
Let me know if you think making them consistent is necessary.
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.
I think it's fine
pkg/agent/route/route_linux_test.go
Outdated
) | ||
|
||
func getIPNet(cidr string) *net.IPNet { | ||
_, ipNet, _ := net.ParseCIDR(cidr) |
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.
nit: maybe make this function getIPNetOrDie
and panic if there is a parsing error
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.
Just found there is already a util func MustParseCIDR
doing what you described, changed to use that function.
276ec69
to
891b723
Compare
Yes, all changes in this patch are for unit tests.
|
@@ -0,0 +1,95 @@ | |||
//go:build !windows |
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.
I remember that library netlink is only suitable on Linux, and there are some limitations in darwin.
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.
With the linux suffix in the file name, this file will only be compiled on linux, I have removed the build tag.
return netlink.NeighDel(neigh) | ||
} | ||
|
||
func (c *Client) LinkByName(name string) (netlink.Link, error) { |
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.
There are some other exising usage from netlink package in net_linux.go, are they planned to be converted to functions provided in this interface?
Besides, some other packages may use functions like netlink.LinkByName
directly ( not calling net.LinkByName()), so is it expected to give netlink.Interface as parameter to those callers, e.g., pkg/agent/cniserver/interface_configuration_linux.go
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.
Yes, when we write tests for them in the future, they will have to use the interface.
Signed-off-by: Quan Tian <qtian@vmware.com>
891b723
to
3ad73fd
Compare
@@ -94,7 +95,9 @@ type Client struct { | |||
nodeConfig *config.NodeConfig | |||
networkConfig *config.NetworkConfig | |||
noSNAT bool | |||
ipt *iptables.Client | |||
ipt iptables.Interface |
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.
Maybe name it to iptables
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.
Personally I'm fine with either but the variable was named ipt
since the begining and the PR is for unit testing, not naming style adjustment, so I would prefer keeping it as was to avoid unrelated change.
/skip-all |
Signed-off-by: Quan Tian <qtian@vmware.com>
Signed-off-by: Quan Tian <qtian@vmware.com>
Signed-off-by: Quan Tian qtian@vmware.com
For #4142