From f474af16600081e6be3edfd233f26234479b08a5 Mon Sep 17 00:00:00 2001 From: Wei Zhang Date: Wed, 11 Jul 2018 23:39:51 +0800 Subject: [PATCH] devices: add some test cases Add test cases for device manager reworks. Signed-off-by: Wei Zhang --- virtcontainers/device/manager/manager_test.go | 38 +++++++++++++++ virtcontainers/kata_agent_test.go | 47 ++++++++++++------- 2 files changed, 67 insertions(+), 18 deletions(-) diff --git a/virtcontainers/device/manager/manager_test.go b/virtcontainers/device/manager/manager_test.go index 79b00f3347..2818820109 100644 --- a/virtcontainers/device/manager/manager_test.go +++ b/virtcontainers/device/manager/manager_test.go @@ -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) + +} diff --git a/virtcontainers/kata_agent_test.go b/virtcontainers/kata_agent_test.go index c7484640e7..5aa2cbc08a 100644 --- a/virtcontainers/kata_agent_test.go +++ b/virtcontainers/kata_agent_test.go @@ -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)