github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/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 devType string 62 major int64 63 expected bool 64 } 65 66 data := []testData{ 67 {"b", config.VhostUserBlkMajor, true}, 68 {"c", config.VhostUserBlkMajor, false}, 69 {"b", config.VhostUserSCSIMajor, false}, 70 {"c", config.VhostUserSCSIMajor, false}, 71 {"b", 240, false}, 72 } 73 74 for _, d := range data { 75 isVhostUserBlk := isVhostUserBlk( 76 config.DeviceInfo{ 77 DevType: d.devType, 78 Major: d.major, 79 }) 80 assert.Equal(t, d.expected, isVhostUserBlk) 81 } 82 } 83 84 func TestIsVhostUserSCSI(t *testing.T) { 85 type testData struct { 86 devType string 87 major int64 88 expected bool 89 } 90 91 data := []testData{ 92 {"b", config.VhostUserBlkMajor, false}, 93 {"c", config.VhostUserBlkMajor, false}, 94 {"b", config.VhostUserSCSIMajor, true}, 95 {"c", config.VhostUserSCSIMajor, false}, 96 {"b", 240, false}, 97 } 98 99 for _, d := range data { 100 isVhostUserSCSI := isVhostUserSCSI( 101 config.DeviceInfo{ 102 DevType: d.devType, 103 Major: d.major, 104 }) 105 assert.Equal(t, d.expected, isVhostUserSCSI) 106 } 107 } 108 109 func TestIsLargeBarSpace(t *testing.T) { 110 assert := assert.New(t) 111 112 // File not exist 113 bs, err := isLargeBarSpace("/abc/xyz/123/rgb") 114 assert.Error(err) 115 assert.False(bs) 116 117 f, err := ioutil.TempFile("", "pci") 118 assert.NoError(err) 119 defer f.Close() 120 defer os.RemoveAll(f.Name()) 121 122 type testData struct { 123 resourceInfo string 124 error bool 125 result bool 126 } 127 128 for _, d := range []testData{ 129 {"", false, false}, 130 {"\t\n\t ", false, false}, 131 {"abc zyx", false, false}, 132 {"abc zyx rgb", false, false}, 133 {"abc\t zyx \trgb", false, false}, 134 {"0x00015\n0x0013", false, false}, 135 {"0x00000000c6000000 0x00000000c6ffffff 0x0000000000040200", false, false}, 136 {"0x0000383bffffffff 0x0000383800000000", false, false}, // start greater than end 137 {"0x0000383800000000 0x0000383bffffffff", false, true}, 138 {"0x0000383800000000 0x0000383bffffffff 0x000000000014220c", false, true}, 139 } { 140 f.WriteAt([]byte(d.resourceInfo), 0) 141 bs, err = isLargeBarSpace(f.Name()) 142 assert.NoError(f.Truncate(0)) 143 if d.error { 144 assert.Error(err, d.resourceInfo) 145 } else { 146 assert.NoError(err, d.resourceInfo) 147 } 148 assert.Equal(d.result, bs, d.resourceInfo) 149 } 150 }