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:
Wei Zhang 2018-07-11 23:39:51 +08:00 committed by z00280905
parent 8391b20805
commit 198a0695ab
2 changed files with 67 additions and 18 deletions

View File

@ -201,3 +201,41 @@ func TestAttachBlockDevice(t *testing.T) {
err = device.Detach(devReceiver) err = device.Detach(devReceiver)
assert.Nil(t, err) 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)
}

View File

@ -25,6 +25,9 @@ import (
"google.golang.org/grpc" "google.golang.org/grpc"
pb "github.com/kata-containers/agent/protocols/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" "github.com/kata-containers/runtime/virtcontainers/device/manager"
vcAnnotations "github.com/kata-containers/runtime/virtcontainers/pkg/annotations" vcAnnotations "github.com/kata-containers/runtime/virtcontainers/pkg/annotations"
"github.com/kata-containers/runtime/virtcontainers/pkg/mock" "github.com/kata-containers/runtime/virtcontainers/pkg/mock"
@ -32,8 +35,8 @@ import (
var ( var (
testKataProxyURLTempl = "unix://%s/kata-proxy-test.sock" testKataProxyURLTempl = "unix://%s/kata-proxy-test.sock"
//testBlockDeviceCtrPath = "testBlockDeviceCtrPath" testBlockDeviceCtrPath = "testBlockDeviceCtrPath"
//testPCIAddr = "04/02" testPCIAddr = "04/02"
) )
func proxyHandlerDiscard(c net.Conn) { func proxyHandlerDiscard(c net.Conn) {
@ -456,9 +459,29 @@ func TestAppendDevicesEmptyContainerDeviceList(t *testing.T) {
updatedDevList, expected) updatedDevList, expected)
} }
/*func TestAppendDevices(t *testing.T) { func TestAppendDevices(t *testing.T) {
k := kataAgent{} 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{} devList := []*pb.Device{}
expected := []*pb.Device{ expected := []*pb.Device{
{ {
@ -467,23 +490,11 @@ func TestAppendDevicesEmptyContainerDeviceList(t *testing.T) {
Id: testPCIAddr, Id: testPCIAddr,
}, },
} }
ctrDevices := []api.Device{ updatedDevList := k.appendDevices(devList, c)
&drivers.BlockDevice{
DeviceInfo: &config.DeviceInfo{
HostPath: testBlockDeviceCtrPath,
},
BlockDrive: &config.BlockDrive{
File: testBlockDeviceCtrPath,
PCIAddr: testPCIAddr,
},
},
}
updatedDevList := k.appendDevices(devList, ctrDevices)
assert.True(t, reflect.DeepEqual(updatedDevList, expected), assert.True(t, reflect.DeepEqual(updatedDevList, expected),
"Device lists didn't match: got %+v, expecting %+v", "Device lists didn't match: got %+v, expecting %+v",
updatedDevList, expected) updatedDevList, expected)
}*/ }
func TestConstraintGRPCSpec(t *testing.T) { func TestConstraintGRPCSpec(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)