gitee.com/leisunstar/runtime@v0.0.0-20200521203717-5cef3e7b53f9/virtcontainers/device/manager/utils_test.go (about) 1 // Copyright (c) 2017 Intel Corporation 2 // Copyright (c) 2018 Huawei Corporation 3 // 4 // SPDX-License-Identifier: Apache-2.0 5 // 6 7 package manager 8 9 import ( 10 "io/ioutil" 11 "os" 12 "testing" 13 14 "github.com/kata-containers/runtime/virtcontainers/device/config" 15 "github.com/stretchr/testify/assert" 16 ) 17 18 func TestIsVFIO(t *testing.T) { 19 type testData struct { 20 path string 21 expected bool 22 } 23 24 data := []testData{ 25 {"/dev/vfio/16", true}, 26 {"/dev/vfio/1", true}, 27 {"/dev/vfio/", false}, 28 {"/dev/vfio", false}, 29 {"/dev/vf", false}, 30 {"/dev", false}, 31 {"/dev/vfio/vfio", false}, 32 {"/dev/vfio/vfio/12", false}, 33 } 34 35 for _, d := range data { 36 isVFIO := isVFIO(d.path) 37 assert.Equal(t, d.expected, isVFIO) 38 } 39 } 40 41 func TestIsBlock(t *testing.T) { 42 type testData struct { 43 devType string 44 expected bool 45 } 46 47 data := []testData{ 48 {"b", true}, 49 {"c", false}, 50 {"u", false}, 51 } 52 53 for _, d := range data { 54 isBlock := isBlock(config.DeviceInfo{DevType: d.devType}) 55 assert.Equal(t, d.expected, isBlock) 56 } 57 } 58 59 func TestIsVhostUserBlk(t *testing.T) { 60 type testData struct { 61 major int64 62 expected bool 63 } 64 65 data := []testData{ 66 {config.VhostUserBlkMajor, true}, 67 {config.VhostUserSCSIMajor, false}, 68 {240, false}, 69 } 70 71 for _, d := range data { 72 isVhostUserBlk := isVhostUserBlk(config.DeviceInfo{Major: d.major}) 73 assert.Equal(t, d.expected, isVhostUserBlk) 74 } 75 } 76 77 func TestIsVhostUserSCSI(t *testing.T) { 78 type testData struct { 79 major int64 80 expected bool 81 } 82 83 data := []testData{ 84 {config.VhostUserBlkMajor, false}, 85 {config.VhostUserSCSIMajor, true}, 86 {240, false}, 87 } 88 89 for _, d := range data { 90 isVhostUserSCSI := isVhostUserSCSI(config.DeviceInfo{Major: d.major}) 91 assert.Equal(t, d.expected, isVhostUserSCSI) 92 } 93 } 94 95 func TestIsLargeBarSpace(t *testing.T) { 96 assert := assert.New(t) 97 98 // File not exist 99 bs, err := isLargeBarSpace("/abc/xyz/123/rgb") 100 assert.Error(err) 101 assert.False(bs) 102 103 f, err := ioutil.TempFile("", "pci") 104 assert.NoError(err) 105 defer f.Close() 106 defer os.RemoveAll(f.Name()) 107 108 type testData struct { 109 resourceInfo string 110 error bool 111 result bool 112 } 113 114 for _, d := range []testData{ 115 {"", false, false}, 116 {"\t\n\t ", false, false}, 117 {"abc zyx", false, false}, 118 {"abc zyx rgb", false, false}, 119 {"abc\t zyx \trgb", false, false}, 120 {"0x00015\n0x0013", false, false}, 121 {"0x00000000c6000000 0x00000000c6ffffff 0x0000000000040200", false, false}, 122 {"0x0000383bffffffff 0x0000383800000000", false, false}, // start greater than end 123 {"0x0000383800000000 0x0000383bffffffff", false, true}, 124 {"0x0000383800000000 0x0000383bffffffff 0x000000000014220c", false, true}, 125 } { 126 f.WriteAt([]byte(d.resourceInfo), 0) 127 bs, err = isLargeBarSpace(f.Name()) 128 assert.NoError(f.Truncate(0)) 129 if d.error { 130 assert.Error(err, d.resourceInfo) 131 } else { 132 assert.NoError(err, d.resourceInfo) 133 } 134 assert.Equal(d.result, bs, d.resourceInfo) 135 } 136 }