-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add usbdevice and usbdevice controller test case
Signed-off-by: Jack Yu <jack.yu@suse.com>
- Loading branch information
Showing
37 changed files
with
3,081 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
pkg/controller/usbdevice/usbdevice_claim_controller_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package usbdevice | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
kubevirtv1 "kubevirt.io/api/core/v1" | ||
|
||
"github.com/harvester/pcidevices/pkg/apis/devices.harvesterhci.io/v1beta1" | ||
"github.com/harvester/pcidevices/pkg/generated/clientset/versioned/fake" | ||
"github.com/harvester/pcidevices/pkg/util/fakeclients" | ||
) | ||
|
||
var ( | ||
mockUsbDevice1 = &v1beta1.USBDevice{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-node-0951-1666-001002", | ||
Labels: map[string]string{ | ||
"nodename": "test-node", | ||
}, | ||
}, | ||
Status: v1beta1.USBDeviceStatus{ | ||
VendorID: "0951", | ||
ProductID: "1666", | ||
ResourceName: "kubevirt.io/test-node-0951-1666-001002", | ||
DevicePath: "/dev/bus/usb/001/002", | ||
NodeName: "test-node", | ||
Description: "DataTraveler 100 G3/G4/SE9 G2/50 Kyson (Kingston Technology)", | ||
}, | ||
} | ||
|
||
mockKubeVirt = &kubevirtv1.KubeVirt{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "kubevirt", | ||
Namespace: KubeVirtNamespace, | ||
}, | ||
Spec: kubevirtv1.KubeVirtSpec{}, | ||
} | ||
|
||
mockUsbDeviceClaim1 = &v1beta1.USBDeviceClaim{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-node-0951-1666-001002", | ||
OwnerReferences: []metav1.OwnerReference{ | ||
{ | ||
APIVersion: "devices.harvesterhci.io/v1beta1", | ||
Kind: "USBDevice", | ||
Name: "test-node-0951-1666-001002", | ||
UID: "test-uid", | ||
}, | ||
}, | ||
}, | ||
Status: v1beta1.USBDeviceClaimStatus{ | ||
NodeName: "test-node", | ||
}, | ||
} | ||
) | ||
|
||
func Test_OnUSBDeviceClaimChanged(t *testing.T) { | ||
client := fake.NewSimpleClientset(mockUsbDevice1, mockUsbDeviceClaim1, mockKubeVirt) | ||
|
||
handler := NewClaimHandler( | ||
fakeclients.USBDeviceCache(client.DevicesV1beta1().USBDevices), | ||
fakeclients.USBDeviceClaimsClient(client.DevicesV1beta1().USBDeviceClaims), | ||
fakeclients.USBDevicesClient(client.DevicesV1beta1().USBDevices), | ||
fakeclients.KubeVirtClient(client.KubevirtV1().KubeVirts), | ||
) | ||
|
||
_, err := handler.OnUSBDeviceClaimChanged("", mockUsbDeviceClaim1) | ||
assert.NoError(t, err) | ||
|
||
kubevirt, err := client.KubevirtV1().KubeVirts(mockKubeVirt.Namespace).Get(context.Background(), mockKubeVirt.Name, metav1.GetOptions{}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, kubevirtv1.KubeVirtSpec{ | ||
Configuration: kubevirtv1.KubeVirtConfiguration{ | ||
PermittedHostDevices: &kubevirtv1.PermittedHostDevices{ | ||
USB: []kubevirtv1.USBHostDevice{ | ||
{ | ||
ResourceName: "kubevirt.io/test-node-0951-1666-001002", | ||
ExternalResourceProvider: true, | ||
Selectors: []kubevirtv1.USBSelector{ | ||
{ | ||
Vendor: "0951", | ||
Product: "1666", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, kubevirt.Spec) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package usbdevice | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/harvester/pcidevices/pkg/deviceplugins" | ||
"github.com/harvester/pcidevices/pkg/generated/clientset/versioned/fake" | ||
"github.com/harvester/pcidevices/pkg/util/fakeclients" | ||
) | ||
|
||
var mockWalkUSBDevices = func() (map[int][]*deviceplugins.USBDevice, error) { | ||
return map[int][]*deviceplugins.USBDevice{ | ||
0: { | ||
{ | ||
Name: "test", | ||
Manufacturer: "test", | ||
Vendor: 2385, | ||
Product: 5734, | ||
DevicePath: "/dev/bus/usb/001/002", | ||
PCIAddress: "0000:02:01.0", | ||
}, | ||
}, | ||
}, nil | ||
} | ||
|
||
var mockCommonLabel = &commonLabel{ | ||
nodeName: "test-node", | ||
} | ||
|
||
func Test_ReconcileUSBDevices(t *testing.T) { | ||
// detect one usb device, create a USBDevice CR | ||
walkUSBDevices = mockWalkUSBDevices | ||
cl = mockCommonLabel | ||
client := fake.NewSimpleClientset() | ||
|
||
usbHandler := NewHandler( | ||
fakeclients.USBDevicesClient(client.DevicesV1beta1().USBDevices), | ||
fakeclients.USBDeviceClaimsClient(client.DevicesV1beta1().USBDeviceClaims), | ||
) | ||
|
||
err := usbHandler.ReconcileUSBDevices() | ||
assert.NoError(t, err) | ||
|
||
list, err := client.DevicesV1beta1().USBDevices().List(context.Background(), metav1.ListOptions{}) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, 1, len(list.Items)) | ||
assert.Equal(t, "test-node-0951-1666-001002", list.Items[0].Name) | ||
assert.Equal(t, "0951", list.Items[0].Status.VendorID) | ||
assert.Equal(t, "1666", list.Items[0].Status.ProductID) | ||
assert.Equal(t, "kubevirt.io/test-node-0951-1666-001002", list.Items[0].Status.ResourceName) | ||
assert.Equal(t, "/dev/bus/usb/001/002", list.Items[0].Status.DevicePath) | ||
assert.Equal(t, cl.nodeName, list.Items[0].Status.NodeName) | ||
assert.Equal(t, "DataTraveler 100 G3/G4/SE9 G2/50 Kyson (Kingston Technology)", list.Items[0].Status.Description) | ||
|
||
// detect no usb device after few minutes, delete existing USBDevice CR | ||
walkUSBDevices = func() (map[int][]*deviceplugins.USBDevice, error) { return map[int][]*deviceplugins.USBDevice{}, nil } | ||
|
||
err = usbHandler.ReconcileUSBDevices() | ||
assert.NoError(t, err) | ||
|
||
list, err = client.DevicesV1beta1().USBDevices().List(context.Background(), metav1.ListOptions{}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, 0, len(list.Items)) | ||
} |
Oops, something went wrong.