mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-07-03 02:26:37 +00:00
devices: add some test cases
Add test cases for device manager reworks. Signed-off-by: Wei Zhang <zhangwei555@huawei.com>
This commit is contained in:
parent
8391b20805
commit
198a0695ab
@ -201,3 +201,41 @@ func TestAttachBlockDevice(t *testing.T) {
|
||||
err = device.Detach(devReceiver)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestAttachDetachDevice(t *testing.T) {
|
||||
dm := NewDeviceManager(VirtioSCSI, nil)
|
||||
|
||||
path := "/dev/hda"
|
||||
deviceInfo := config.DeviceInfo{
|
||||
HostPath: path,
|
||||
ContainerPath: path,
|
||||
DevType: "b",
|
||||
}
|
||||
|
||||
devReceiver := &api.MockDeviceReceiver{}
|
||||
device, err := dm.NewDevice(deviceInfo)
|
||||
assert.Nil(t, err)
|
||||
|
||||
// attach device
|
||||
err = dm.AttachDevice(device.DeviceID(), devReceiver)
|
||||
assert.Nil(t, err)
|
||||
// attach device again(twice)
|
||||
err = dm.AttachDevice(device.DeviceID(), devReceiver)
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, err, ErrDeviceAttached, "attach device twice should report error %q", ErrDeviceAttached)
|
||||
|
||||
attached := dm.IsDeviceAttached(device.DeviceID())
|
||||
assert.True(t, attached)
|
||||
|
||||
// detach device
|
||||
err = dm.DetachDevice(device.DeviceID(), devReceiver)
|
||||
assert.Nil(t, err)
|
||||
// detach device again(twice)
|
||||
err = dm.DetachDevice(device.DeviceID(), devReceiver)
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, err, ErrDeviceNotAttached, "attach device twice should report error %q", ErrDeviceNotAttached)
|
||||
|
||||
attached = dm.IsDeviceAttached(device.DeviceID())
|
||||
assert.False(t, attached)
|
||||
|
||||
}
|
||||
|
@ -25,15 +25,18 @@ import (
|
||||
"google.golang.org/grpc"
|
||||
|
||||
pb "github.com/kata-containers/agent/protocols/grpc"
|
||||
"github.com/kata-containers/runtime/virtcontainers/device/api"
|
||||
"github.com/kata-containers/runtime/virtcontainers/device/config"
|
||||
"github.com/kata-containers/runtime/virtcontainers/device/drivers"
|
||||
"github.com/kata-containers/runtime/virtcontainers/device/manager"
|
||||
vcAnnotations "github.com/kata-containers/runtime/virtcontainers/pkg/annotations"
|
||||
"github.com/kata-containers/runtime/virtcontainers/pkg/mock"
|
||||
)
|
||||
|
||||
var (
|
||||
testKataProxyURLTempl = "unix://%s/kata-proxy-test.sock"
|
||||
//testBlockDeviceCtrPath = "testBlockDeviceCtrPath"
|
||||
//testPCIAddr = "04/02"
|
||||
testKataProxyURLTempl = "unix://%s/kata-proxy-test.sock"
|
||||
testBlockDeviceCtrPath = "testBlockDeviceCtrPath"
|
||||
testPCIAddr = "04/02"
|
||||
)
|
||||
|
||||
func proxyHandlerDiscard(c net.Conn) {
|
||||
@ -456,9 +459,29 @@ func TestAppendDevicesEmptyContainerDeviceList(t *testing.T) {
|
||||
updatedDevList, expected)
|
||||
}
|
||||
|
||||
/*func TestAppendDevices(t *testing.T) {
|
||||
func TestAppendDevices(t *testing.T) {
|
||||
k := kataAgent{}
|
||||
|
||||
id := "test-append-block"
|
||||
ctrDevices := []api.Device{
|
||||
&drivers.BlockDevice{
|
||||
ID: id,
|
||||
BlockDrive: &config.BlockDrive{
|
||||
PCIAddr: testPCIAddr,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
c := &Container{
|
||||
sandbox: &Sandbox{
|
||||
devManager: manager.NewDeviceManager("virtio-scsi", ctrDevices),
|
||||
},
|
||||
}
|
||||
c.devices = append(c.devices, ContainerDevice{
|
||||
ID: id,
|
||||
ContainerPath: testBlockDeviceCtrPath,
|
||||
})
|
||||
|
||||
devList := []*pb.Device{}
|
||||
expected := []*pb.Device{
|
||||
{
|
||||
@ -467,23 +490,11 @@ func TestAppendDevicesEmptyContainerDeviceList(t *testing.T) {
|
||||
Id: testPCIAddr,
|
||||
},
|
||||
}
|
||||
ctrDevices := []api.Device{
|
||||
&drivers.BlockDevice{
|
||||
DeviceInfo: &config.DeviceInfo{
|
||||
HostPath: testBlockDeviceCtrPath,
|
||||
},
|
||||
BlockDrive: &config.BlockDrive{
|
||||
File: testBlockDeviceCtrPath,
|
||||
PCIAddr: testPCIAddr,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
updatedDevList := k.appendDevices(devList, ctrDevices)
|
||||
updatedDevList := k.appendDevices(devList, c)
|
||||
assert.True(t, reflect.DeepEqual(updatedDevList, expected),
|
||||
"Device lists didn't match: got %+v, expecting %+v",
|
||||
updatedDevList, expected)
|
||||
}*/
|
||||
}
|
||||
|
||||
func TestConstraintGRPCSpec(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
Loading…
Reference in New Issue
Block a user