github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/device/drivers/vfio_test.go (about)

     1  // Copyright (c) 2017-2018 Intel Corporation
     2  // Copyright (c) 2018 Huawei Corporation
     3  //
     4  // SPDX-License-Identifier: Apache-2.0
     5  //
     6  
     7  package drivers
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/kata-containers/runtime/virtcontainers/device/config"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestGetVFIODetails(t *testing.T) {
    17  	type testData struct {
    18  		deviceStr   string
    19  		expectedStr string
    20  	}
    21  
    22  	data := []testData{
    23  		{"0000:02:10.0", "02:10.0"},
    24  		{"0000:0210.0", ""},
    25  		{"f79944e4-5a3d-11e8-99ce-", ""},
    26  		{"f79944e4-5a3d-11e8-99ce", ""},
    27  		{"test", ""},
    28  		{"", ""},
    29  	}
    30  
    31  	for _, d := range data {
    32  		deviceBDF, deviceSysfsDev, vfioDeviceType, err := getVFIODetails(d.deviceStr, "")
    33  
    34  		switch vfioDeviceType {
    35  		case config.VFIODeviceNormalType:
    36  			assert.Equal(t, d.expectedStr, deviceBDF)
    37  		case config.VFIODeviceMediatedType:
    38  			assert.Equal(t, d.expectedStr, deviceSysfsDev)
    39  		default:
    40  			assert.NotNil(t, err)
    41  		}
    42  
    43  		if d.expectedStr == "" {
    44  			assert.NotNil(t, err)
    45  		} else {
    46  			assert.Nil(t, err)
    47  		}
    48  	}
    49  }