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

Add unit tests for pkg/agent/util/net #4312

Merged
merged 1 commit into from
Mar 17, 2023
Merged

Add unit tests for pkg/agent/util/net #4312

merged 1 commit into from
Mar 17, 2023

Conversation

qiyueyao
Copy link
Contributor

Signed-off-by: Qiyue Yao yaoq@vmware.com

@codecov
Copy link

codecov bot commented Oct 18, 2022

Codecov Report

Merging #4312 (05e9060) into main (8eb5c8d) will increase coverage by 1.43%.
The diff coverage is 95.53%.

Impacted file tree graph

@@            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     
Flag Coverage Δ *Carryforward flag
e2e-tests 38.40% <ø> (+0.02%) ⬆️ Carriedforward from 8eb5c8d
integration-tests 34.41% <36.84%> (+0.08%) ⬆️
kind-e2e-tests 47.88% <24.56%> (+0.72%) ⬆️
unit-tests 61.33% <95.53%> (+1.56%) ⬆️

*This pull request uses carry forward flags. Click here to find out more.

Impacted Files Coverage Δ
pkg/agent/util/net_windows.go 83.55% <90.90%> (+76.48%) ⬆️
pkg/agent/util/net.go 82.95% <100.00%> (+24.62%) ⬆️
pkg/agent/util/net_linux.go 87.05% <100.00%> (+54.07%) ⬆️
...nt/apiserver/handlers/serviceexternalip/handler.go 37.03% <0.00%> (-14.82%) ⬇️
pkg/agent/controller/networkpolicy/packetin.go 70.27% <0.00%> (-6.76%) ⬇️
pkg/apiserver/certificate/certificate.go 70.37% <0.00%> (-6.49%) ⬇️
pkg/controller/networkpolicy/tier.go 53.84% <0.00%> (-4.62%) ⬇️
pkg/agent/cniserver/ipam/antrea_ipam.go 75.75% <0.00%> (-4.33%) ⬇️
...g/agent/controller/serviceexternalip/controller.go 79.18% <0.00%> (-2.39%) ⬇️
pkg/agent/ipassigner/local_ip_detector_linux.go 75.75% <0.00%> (-2.03%) ⬇️
... and 25 more

@qiyueyao qiyueyao changed the title Add UT to net.go Add unit tests for pkg/agent/util/net Oct 27, 2022
@luolanzone luolanzone mentioned this pull request Oct 28, 2022
37 tasks
@qiyueyao qiyueyao force-pushed the net-ut branch 3 times, most recently from d4cc01c to 89e0b43 Compare November 2, 2022 03:14
@qiyueyao qiyueyao force-pushed the net-ut branch 5 times, most recently from 1c14185 to c81dfed Compare December 22, 2022 15:35
@qiyueyao qiyueyao force-pushed the net-ut branch 5 times, most recently from 7548847 to 33e64d8 Compare January 10, 2023 05:42
@qiyueyao qiyueyao marked this pull request as ready for review January 10, 2023 17:40
@qiyueyao
Copy link
Contributor Author

@wenyingd Got a question for this WindowsHyperVEnabled function, why it returns true instead of false when cmd returns an error?

@wenyingd
Copy link
Contributor

@wenyingd Got a question for this WindowsHyperVEnabled function, why it returns true instead of false when cmd returns an error?

It should be a mistake, false is expected.

@luolanzone
Copy link
Contributor

Hi @qiyueyao could you resolve the code conflicts? is this ready for review?

@qiyueyao qiyueyao force-pushed the net-ut branch 2 times, most recently from 44e03bc to 41e610e Compare February 2, 2023 18:07
@qiyueyao
Copy link
Contributor Author

qiyueyao commented Feb 2, 2023

Hi @qiyueyao could you resolve the code conflicts? is this ready for review?

Done. This is ready for review.


for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
getNS = func(nspath string) (ns.NetNS, error) {
Copy link
Contributor

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.go Outdated Show resolved Hide resolved
pkg/agent/util/net_windows_test.go Outdated Show resolved Hide resolved
pkg/agent/util/net_windows_test.go Outdated Show resolved Hide resolved

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
runCommand = func(cmd string) (string, error) {
Copy link
Contributor

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.

Copy link
Contributor Author

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.

pkg/agent/util/net_linux_test.go Outdated Show resolved Hide resolved
pkg/agent/util/net_linux_test.go Outdated Show resolved Hide resolved
pkg/agent/util/net_linux_test.go Outdated Show resolved Hide resolved
pkg/agent/util/net_linux_test.go Outdated Show resolved Hide resolved
pkg/agent/util/net_linux_test.go Outdated Show resolved Hide resolved
pkg/agent/util/net_test.go Outdated Show resolved Hide resolved
pkg/agent/util/net_test.go Outdated Show resolved Hide resolved
pkg/agent/util/net_windows_test.go Outdated Show resolved Hide resolved
pkg/agent/util/net_windows_test.go Outdated Show resolved Hide resolved
pkg/agent/util/net_windows_test.go Outdated Show resolved Hide resolved
@luolanzone luolanzone added this to the Antrea v1.11 release milestone Mar 2, 2023
@luolanzone
Copy link
Contributor

@qiyueyao any update for this PR?

@qiyueyao qiyueyao force-pushed the net-ut branch 3 times, most recently from 866355a to ceae3fc Compare March 8, 2023 07:51
@qiyueyao qiyueyao requested review from wenyingd and luolanzone March 8, 2023 18:08
pkg/agent/util/net.go Outdated Show resolved Hide resolved
pkg/agent/util/net.go Outdated Show resolved Hide resolved
pkg/agent/util/net_linux.go Outdated Show resolved Hide resolved
pkg/agent/util/net_linux_test.go Show resolved Hide resolved
pkg/agent/util/net_test.go Outdated Show resolved Hide resolved
pkg/agent/util/net_test.go Outdated Show resolved Hide resolved
pkg/agent/util/net_windows_test.go Outdated Show resolved Hide resolved
pkg/agent/util/net_windows_test.go Outdated Show resolved Hide resolved
Comment on lines 1 to 16
// 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
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// 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.

Copy link
Contributor

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.

Copy link
Contributor Author

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)
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

@qiyueyao qiyueyao force-pushed the net-ut branch 2 times, most recently from a23489c to 4d039ff Compare March 14, 2023 12:59
Copy link
Contributor

@wenyingd wenyingd left a 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.

pkg/agent/util/net_windows_test.go Show resolved Hide resolved
wenyingd
wenyingd previously approved these changes Mar 15, 2023
Copy link
Contributor

@wenyingd wenyingd left a comment

Choose a reason for hiding this comment

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

LGTM

luolanzone
luolanzone previously approved these changes Mar 16, 2023
Copy link
Contributor

@luolanzone luolanzone left a 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.

Comment on lines 1 to 16
// 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
Copy link
Contributor

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>
Copy link
Contributor

@luolanzone luolanzone left a comment

Choose a reason for hiding this comment

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

LGTM

@qiyueyao
Copy link
Contributor Author

qiyueyao commented Mar 17, 2023

/test-all
/test-windows-all
/test-ipv6-all
/test-ipv6-only-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 for the effort.

@tnqn tnqn merged commit c8ed472 into antrea-io:main Mar 17, 2023
@qiyueyao
Copy link
Contributor Author

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 qiyueyao deleted the net-ut branch March 17, 2023 07:45
@tnqn
Copy link
Member

tnqn commented Mar 17, 2023

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.

jainpulkit22 pushed a commit to urharshitha/antrea that referenced this pull request Apr 28, 2023
Signed-off-by: Qiyue Yao <yaoq@vmware.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/test Issues or PRs related to unit and integration tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants