From 653df674d381b574d7869eb1c4c341e2a5ebbb6e Mon Sep 17 00:00:00 2001 From: Bo Chen Date: Mon, 29 Jun 2020 21:01:05 -0700 Subject: [PATCH] kata_agent: Add unit tests [ port from runtime commit 6be76fcd07a3d74ca5521af2feaf966dd6f2c344 ] This patch adds the unit test for 'handleDeviceBlockVolume()'. Signed-off-by: Bo Chen Signed-off-by: Peng Tao --- src/runtime/virtcontainers/kata_agent_test.go | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/src/runtime/virtcontainers/kata_agent_test.go b/src/runtime/virtcontainers/kata_agent_test.go index 082bf157d..3848a12d9 100644 --- a/src/runtime/virtcontainers/kata_agent_test.go +++ b/src/runtime/virtcontainers/kata_agent_test.go @@ -39,8 +39,13 @@ import ( var ( testKataProxyURLTempl = "unix://%s/kata-proxy-test.sock" + testBlkDriveFormat = "testBlkDriveFormat" testBlockDeviceCtrPath = "testBlockDeviceCtrPath" + testDevNo = "testDevNo" + testNvdimmID = "testNvdimmID" testPCIAddr = "04/02" + testSCSIAddr = "testSCSIAddr" + testVirtPath = "testVirtPath" ) func testGenerateKataProxySockDir() (string, error) { @@ -398,6 +403,110 @@ func TestHandleLocalStorage(t *testing.T) { assert.Equal(t, localMountPoint, expected) } +func TestHandleDeviceBlockVolume(t *testing.T) { + k := kataAgent{} + + tests := []struct { + BlockDeviceDriver string + inputDev *drivers.BlockDevice + resultVol *pb.Storage + }{ + { + inputDev: &drivers.BlockDevice{ + BlockDrive: &config.BlockDrive{ + Pmem: true, + NvdimmID: testNvdimmID, + Format: testBlkDriveFormat, + }, + }, + resultVol: &pb.Storage{ + Driver: kataNvdimmDevType, + Source: fmt.Sprintf("/dev/pmem%s", testNvdimmID), + Fstype: testBlkDriveFormat, + Options: []string{"dax"}, + }, + }, + { + BlockDeviceDriver: config.VirtioBlockCCW, + inputDev: &drivers.BlockDevice{ + BlockDrive: &config.BlockDrive{ + DevNo: testDevNo, + }, + }, + resultVol: &pb.Storage{ + Driver: kataBlkCCWDevType, + Source: testDevNo, + }, + }, + { + BlockDeviceDriver: config.VirtioBlock, + inputDev: &drivers.BlockDevice{ + BlockDrive: &config.BlockDrive{ + PCIAddr: testPCIAddr, + VirtPath: testVirtPath, + }, + }, + resultVol: &pb.Storage{ + Driver: kataBlkDevType, + Source: testPCIAddr, + }, + }, + { + BlockDeviceDriver: config.VirtioBlock, + inputDev: &drivers.BlockDevice{ + BlockDrive: &config.BlockDrive{ + VirtPath: testVirtPath, + }, + }, + resultVol: &pb.Storage{ + Driver: kataBlkDevType, + Source: testVirtPath, + }, + }, + { + BlockDeviceDriver: config.VirtioMmio, + inputDev: &drivers.BlockDevice{ + BlockDrive: &config.BlockDrive{ + VirtPath: testVirtPath, + }, + }, + resultVol: &pb.Storage{ + Driver: kataMmioBlkDevType, + Source: testVirtPath, + }, + }, + { + BlockDeviceDriver: config.VirtioSCSI, + inputDev: &drivers.BlockDevice{ + BlockDrive: &config.BlockDrive{ + SCSIAddr: testSCSIAddr, + }, + }, + resultVol: &pb.Storage{ + Driver: kataSCSIDevType, + Source: testSCSIAddr, + }, + }, + } + + for _, test := range tests { + c := &Container{ + sandbox: &Sandbox{ + config: &SandboxConfig{ + HypervisorConfig: HypervisorConfig{ + BlockDeviceDriver: test.BlockDeviceDriver, + }, + }, + }, + } + + vol, _ := k.handleDeviceBlockVolume(c, test.inputDev) + assert.True(t, reflect.DeepEqual(vol, test.resultVol), + "Volume didn't match: got %+v, expecting %+v", + vol, test.resultVol) + } +} + func TestHandleBlockVolume(t *testing.T) { k := kataAgent{}