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
virt-handler: Fix panic when listing devices on host without USB devices #11047
Conversation
Hi @jschintag. Thanks for your PR. I'm waiting for a kubevirt member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
Hi, thanks for the fix! Can you add a test case for it? |
Sure, will do |
d979fc9
to
1f87c2c
Compare
I have added a test for this case. |
oldPathToUSBDevices := PathToUSBDevices | ||
PathToUSBDevices = "/this/path/does/not/exist" | ||
DeferCleanup(func() { | ||
PathToUSBDevices = oldPathToUSBDevices |
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.
Please initialize this variable in a BeforeEach
section for the tests
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 have moved the code to a BeforeEach
Section.
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 mean without the DeferCleanup
. You can add another BeforeEach
in the other context with the valid path.
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.
In that case i would rather just drop the DeferCleanup
code entirely, since the other context does not call the discoverPluggedUSBDevices
function at all, instead overriding it with their own implementation to ensure consistent test results.
I mainly added DeferCleanup
to not leave the unit in a weird state afterwards, so setting the variable to it's default value should not need to happen in a BeforeEach
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.
Sounds good
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 have dropped the DeferCleanup
1f87c2c
to
d3ab2c5
Compare
Hi Jan! My minor nit would only be to break it in two commits, moving the existing tests under their own |
/test pull-kubevirt-build |
d3ab2c5
to
343da72
Compare
Done |
269a291
to
884d6c4
Compare
/ok-to-test |
/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.
/hold
@@ -44,7 +44,7 @@ import ( | |||
pluginapi "kubevirt.io/kubevirt/pkg/virt-handler/device-manager/deviceplugin/v1beta1" | |||
) | |||
|
|||
const ( | |||
var ( |
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 don't think its good to make this exported const a writable var. Can you change discoverPluggedUSBDevices
to take the path as a function parameter instead?
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.
An alternative is to un-export 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.
I went with unexporting it, since as far as i can tell at least inside kubevirt this export is never used anyway.
Additionally, since for all intents and purposes this is a constant outside of tests, i don't think something like
var discoverLocalUSBDevicesFunc = func(){
discoverPluggedUSBDevices("/sys/bus/usb/devices")
}
would be the right choice.
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 to confirm, It was a mistake on my side to have it exported, probably a left over from different interactions in the code...
DevicePath: "/dev/bus/usb/002/010", | ||
}, | ||
} | ||
Context("Host has USB Devices", func() { |
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.
While it's good to distinguish between host has USB devices or not this makes the diff pretty large. Can you instead just add the test case for an invalid path to USB devices? That's also more generic, because having an invalid path or an error during accessing this path doesn't necessarily mean a host has no USB devices.
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 have changed it to a more generic Should return empty when encountering an error
and reverted the commit that added the context.
}) | ||
Context("Host has no USB Devices", func() { | ||
BeforeEach(func() { | ||
PathToUSBDevices = "/this/path/does/not/exist" |
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.
When making the path a function parameter the BeforeEach can be dropped.
@@ -216,6 +216,13 @@ var _ = Describe("USB Device", func() { | |||
}, | |||
), | |||
) | |||
BeforeEach(func() { |
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.
This will break or impact the other tests, won't it? Can you set the unexported var in the test case It
instead?
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.
It won't since the other test does not call discoverPluggedUSBDevices()
. As for having it this way, please see this thread #11047 (comment)
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 don't think we should taint other test cases, even though they might not use this variable right now.
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 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 inside the test is fine. Will that permanently alter the variable for subsequent runs of tests? (With BeforeEach it does too, but at least it's obvious.)
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 normal go test it should, since the test is running in the same process. For ginkgo i don't know, but i think it uses go test somehow so likely it is the same here.
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 have dropped the framework specific BeforeEach
, because it is intended for repeating setup operations, whereas in this instance we only really want the variable to be changed for a single test. It is not shared code and should therefore not happen outside of the actual test function.
Signed-off-by: Jan Schintag <jan.schintag@de.ibm.com>
00562aa
to
3a414ea
Compare
/retest |
Thanks again, |
The code looks fine to me |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: alicefr The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Required labels detected, running phase 2 presubmits: |
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!
/lgtm
/hold cancel
I think this could be cherry picked to v1.1.0 ? Not sure how that is done. |
/retest-required |
/cherry-pick release-1.1 |
@0xFelix: once the present PR merges, I will cherry-pick it on top of release-1.1 in a new PR and assign it to you. In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
/retest-required |
/retest |
@0xFelix: new pull request created: #11079 In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
What this PR does / why we need it:
Fix a panic when trying to list USB Devices on host without USB devices
Which issue(s) this PR fixes (optional, in
fixes #<issue number>(, fixes #<issue_number>, ...)
format, will close the issue(s) when PR gets merged):None
Special notes for your reviewer:
Checklist
This checklist is not enforcing, but it's a reminder of items that could be relevant to every PR.
Approvers are expected to review this list.
Release note: