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

virt-handler: Fix panic when listing devices on host without USB devices #11047

Merged
merged 1 commit into from Jan 24, 2024

Conversation

jschintag
Copy link
Contributor

@jschintag jschintag commented Jan 19, 2024

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:

Fix potential crash when trying to list USB devices on host without any

@kubevirt-bot kubevirt-bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. dco-signoff: yes Indicates the PR's author has DCO signed all their commits. labels Jan 19, 2024
@kubevirt-bot kubevirt-bot added size/XS needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jan 19, 2024
@kubevirt-bot
Copy link
Contributor

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 /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

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.

@0xFelix
Copy link
Member

0xFelix commented Jan 19, 2024

Hi, thanks for the fix! Can you add a test case for it?

@jschintag
Copy link
Contributor Author

Hi, thanks for the fix! Can you add a test case for it?

Sure, will do

@jschintag
Copy link
Contributor Author

I have added a test for this case.

oldPathToUSBDevices := PathToUSBDevices
PathToUSBDevices = "/this/path/does/not/exist"
DeferCleanup(func() {
PathToUSBDevices = oldPathToUSBDevices
Copy link
Member

@alicefr alicefr Jan 19, 2024

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

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

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

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good

Copy link
Contributor Author

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

@victortoso
Copy link
Member

Hi Jan! My minor nit would only be to break it in two commits, moving the existing tests under their own Context() would make the diff of the fix much nicer to check. Other than that, many thanks for the fix!

@alicefr
Copy link
Member

alicefr commented Jan 19, 2024

/test pull-kubevirt-build

@jschintag
Copy link
Contributor Author

Hi Jan! My minor nit would only be to break it in two commits, moving the existing tests under their own Context() would make the diff of the fix much nicer to check. Other than that, many thanks for the fix!

Done

@jschintag jschintag force-pushed the fix-usb-error branch 2 times, most recently from 269a291 to 884d6c4 Compare January 19, 2024 13:03
@alicefr
Copy link
Member

alicefr commented Jan 19, 2024

/ok-to-test

@kubevirt-bot kubevirt-bot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jan 19, 2024
@alicefr
Copy link
Member

alicefr commented Jan 19, 2024

/lgtm

@kubevirt-bot kubevirt-bot added the lgtm Indicates that a PR is ready to be merged. label Jan 19, 2024
Copy link
Member

@0xFelix 0xFelix left a 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 (
Copy link
Member

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?

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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() {
Copy link
Member

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.

Copy link
Contributor Author

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"
Copy link
Member

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() {
Copy link
Member

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?

Copy link
Contributor Author

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)

Copy link
Member

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.

Copy link
Contributor Author

@jschintag jschintag Jan 22, 2024

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.

Then it is back to the question, use a DeferCleanup or drop the BeforeEach and do it inside the test?
What do you think @0xFelix @alicefr

Copy link
Member

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.)

Copy link
Contributor Author

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.

Copy link
Contributor Author

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>
@jschintag
Copy link
Contributor Author

/retest

@victortoso
Copy link
Member

Thanks again,
/lgtm

@kubevirt-bot kubevirt-bot added the lgtm Indicates that a PR is ready to be merged. label Jan 23, 2024
@alicefr
Copy link
Member

alicefr commented Jan 23, 2024

The code looks fine to me
/approve
/hold
Waiting @0xFelix to have a second look

@kubevirt-bot
Copy link
Contributor

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubevirt-bot kubevirt-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jan 23, 2024
@kubevirt-commenter-bot
Copy link

Required labels detected, running phase 2 presubmits:
/test pull-kubevirt-e2e-k8s-1.27-ipv6-sig-network

Copy link
Member

@0xFelix 0xFelix left a 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

@kubevirt-bot kubevirt-bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jan 23, 2024
@victortoso
Copy link
Member

I think this could be cherry picked to v1.1.0 ? Not sure how that is done.

@victortoso
Copy link
Member

/retest-required

@0xFelix
Copy link
Member

0xFelix commented Jan 23, 2024

/cherry-pick release-1.1

@kubevirt-bot
Copy link
Contributor

@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:

/cherry-pick release-1.1

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.

@kubevirt-commenter-bot
Copy link

/retest-required
This bot automatically retries required jobs that failed/flaked on approved PRs.
Silence the bot with an /lgtm cancel or /hold comment for consistent failures.

@jschintag
Copy link
Contributor Author

/retest

@kubevirt-bot kubevirt-bot merged commit 9b39fcf into kubevirt:main Jan 24, 2024
39 checks passed
@kubevirt-bot
Copy link
Contributor

@0xFelix: new pull request created: #11079

In response to this:

/cherry-pick release-1.1

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. dco-signoff: yes Indicates the PR's author has DCO signed all their commits. lgtm Indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/S
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants