-
Notifications
You must be signed in to change notification settings - Fork 376
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 tests for pkg/agent/util/net #4312
Conversation
Codecov Report
@@ Coverage Diff @@
## main #4312 +/- ##
==========================================
+ Coverage 69.79% 71.23% +1.43%
==========================================
Files 402 402
Lines 59570 59568 -2
==========================================
+ Hits 41579 42432 +853
+ Misses 15189 14293 -896
- Partials 2802 2843 +41
*This pull request uses carry forward flags. Click here to find out more.
|
d4cc01c
to
89e0b43
Compare
1c14185
to
c81dfed
Compare
7548847
to
33e64d8
Compare
@wenyingd Got a question for this |
It should be a mistake, |
Hi @qiyueyao could you resolve the code conflicts? is this ready for review? |
44e03bc
to
41e610e
Compare
Done. This is ready for review. |
pkg/agent/util/net_linux_test.go
Outdated
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
getNS = func(nspath string) (ns.NetNS, 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.
Could you follow this format for all the mock functions?
func mockGetNS(testNS ns.NetNS, err error) func() {
originalGetNS := getNS
getNS = func(nspath string) (ns.NetNS, error) {
return testNS, err
}
return func() {
getNS = originalGetNS
}
}
And use defer functions like this in the test cases,
...
defer mockGetNS(&mockNetNS{}, tc.getNSErr)()
...
pkg/agent/util/net_windows_test.go
Outdated
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
runCommand = func(cmd string) (string, 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.
I would prefer to have comparasion on the final command ( the parameter cmd ) with an expected comand string to run with powershell when mocking the funciont "runCommand" in this file. In this way, it is easier to catch the command modification in the future changes.
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.
Added the comparison, and since runCommand is usually called multiple times in a test function, assert.Contains
is used instead of assert.Equal
.
@qiyueyao any update for this PR? |
866355a
to
ceae3fc
Compare
pkg/agent/util/net_linux_test.go
Outdated
// Copyright 2023 Antrea Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build linux | ||
// +build linux |
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.
// Copyright 2023 Antrea Authors | |
// | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. | |
//go:build linux | |
// +build linux | |
//go:build linux | |
// +build linux | |
// Copyright 2023 Antrea Authors | |
// | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. |
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.
@qiyueyao This is not addressed.
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.
Oops I changed the one in pkg/agent/util/net_linux.go
instead, but I guess that should also be addressed.
gotMac, gotIndex, gotErr := SetLinkUp("antrea-en0") | ||
assert.Equal(t, tc.wantMac, gotMac) | ||
assert.Equal(t, tc.wantIndex, gotIndex) | ||
assert.Equal(t, tc.wantErr, gotErr) |
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.
Use assert.EqualErrorf
. So do others.
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.
May I ask the purpose of using assert.EqualErrorf
instead of assert.Equal
? Because according to the definition"EqualErrorf asserts that a function returned an error (i.e. not nil
) and that it is equal to the provided error.", then in the case of nil
error, this introduces more case check.
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.
Kept most cases, but changed required.NoError
inspired by other PR.
a23489c
to
4d039ff
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, one minor comment is left.
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.
LGTM
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.
LGTM overall, one nit. @tnqn do you have time to take a quick look? thanks.
pkg/agent/util/net_linux_test.go
Outdated
// Copyright 2023 Antrea Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build linux | ||
// +build linux |
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.
@qiyueyao This is not addressed.
Signed-off-by: Qiyue Yao <yaoq@vmware.com>
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.
LGTM
/test-all |
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.
LGTM, thanks for the effort.
Thanks @tnqn, I actually have a suggestion, to add this UT pattern to the code review comments/UT section for developers to reference. It would be of great help. |
@qiyueyao please feel free to open a PR to that doc with any suggestions that you think helpful. |
Signed-off-by: Qiyue Yao <yaoq@vmware.com>
Signed-off-by: Qiyue Yao yaoq@vmware.com