github.com/k8snetworkplumbingwg/sriov-network-operator@v1.2.1-0.20240408194816-2d2e5a45d453/pkg/host/internal/kernel/kernel_test.go (about) 1 package kernel 2 3 import ( 4 . "github.com/onsi/ginkgo/v2" 5 . "github.com/onsi/gomega" 6 7 "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" 8 "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/types" 9 "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils" 10 "github.com/k8snetworkplumbingwg/sriov-network-operator/test/util/fakefilesystem" 11 "github.com/k8snetworkplumbingwg/sriov-network-operator/test/util/helpers" 12 ) 13 14 var _ = Describe("Kernel", func() { 15 Context("Drivers", func() { 16 var ( 17 k types.KernelInterface 18 ) 19 BeforeEach(func() { 20 k = New(utils.New()) 21 }) 22 Context("Unbind, UnbindDriverByBusAndDevice", func() { 23 It("unknown device", func() { 24 Expect(k.UnbindDriverByBusAndDevice(consts.BusPci, "unknown-dev")).NotTo(HaveOccurred()) 25 }) 26 It("known device, no driver", func() { 27 helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{Dirs: []string{"/sys/bus/pci/devices/0000:d8:00.0"}}) 28 Expect(k.Unbind("0000:d8:00.0")).NotTo(HaveOccurred()) 29 }) 30 It("has driver, succeed", func() { 31 helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{ 32 Dirs: []string{ 33 "/sys/bus/pci/devices/0000:d8:00.0", 34 "/sys/bus/pci/drivers/test-driver"}, 35 Symlinks: map[string]string{ 36 "/sys/bus/pci/devices/0000:d8:00.0/driver": "../../../../bus/pci/drivers/test-driver"}, 37 Files: map[string][]byte{ 38 "/sys/bus/pci/drivers/test-driver/unbind": {}}, 39 }) 40 Expect(k.Unbind("0000:d8:00.0")).NotTo(HaveOccurred()) 41 // check that echo to unbind path was done 42 helpers.GinkgoAssertFileContentsEquals("/sys/bus/pci/drivers/test-driver/unbind", "0000:d8:00.0") 43 }) 44 It("has driver, failed to unbind", func() { 45 helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{ 46 Dirs: []string{ 47 "/sys/bus/pci/devices/0000:d8:00.0"}, 48 Symlinks: map[string]string{ 49 "/sys/bus/pci/devices/0000:d8:00.0/driver": "../../../../bus/pci/drivers/test-driver"}, 50 }) 51 Expect(k.Unbind("0000:d8:00.0")).To(HaveOccurred()) 52 }) 53 }) 54 Context("HasDriver", func() { 55 It("unknown device", func() { 56 has, driver := k.HasDriver("unknown-dev") 57 Expect(has).To(BeFalse()) 58 Expect(driver).To(BeEmpty()) 59 }) 60 It("known device, no driver", func() { 61 helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{Dirs: []string{"/sys/bus/pci/devices/0000:d8:00.0"}}) 62 has, driver := k.HasDriver("0000:d8:00.0") 63 Expect(has).To(BeFalse()) 64 Expect(driver).To(BeEmpty()) 65 }) 66 It("has driver", func() { 67 helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{ 68 Dirs: []string{ 69 "/sys/bus/pci/devices/0000:d8:00.0", 70 "/sys/bus/pci/drivers/test-driver"}, 71 Symlinks: map[string]string{ 72 "/sys/bus/pci/devices/0000:d8:00.0/driver": "../../../../bus/pci/drivers/test-driver"}, 73 }) 74 has, driver := k.HasDriver("0000:d8:00.0") 75 Expect(has).To(BeTrue()) 76 Expect(driver).To(Equal("test-driver")) 77 }) 78 }) 79 Context("BindDefaultDriver", func() { 80 It("unknown device", func() { 81 Expect(k.BindDefaultDriver("unknown-dev")).To(HaveOccurred()) 82 }) 83 It("no driver", func() { 84 helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{ 85 Dirs: []string{ 86 "/sys/bus/pci/devices/0000:d8:00.0"}, 87 Files: map[string][]byte{ 88 "/sys/bus/pci/drivers_probe": {}, "/sys/bus/pci/devices/0000:d8:00.0/driver_override": {}}, 89 }) 90 Expect(k.BindDefaultDriver("0000:d8:00.0")).NotTo(HaveOccurred()) 91 // should probe driver for dev 92 helpers.GinkgoAssertFileContentsEquals("/sys/bus/pci/drivers_probe", "0000:d8:00.0") 93 }) 94 It("already bind to default driver", func() { 95 helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{ 96 Dirs: []string{ 97 "/sys/bus/pci/devices/0000:d8:00.0"}, 98 Symlinks: map[string]string{ 99 "/sys/bus/pci/devices/0000:d8:00.0/driver": "../../../../bus/pci/drivers/test-driver"}, 100 }) 101 Expect(k.BindDefaultDriver("0000:d8:00.0")).NotTo(HaveOccurred()) 102 }) 103 It("bind to dpdk driver", func() { 104 helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{ 105 Dirs: []string{ 106 "/sys/bus/pci/devices/0000:d8:00.0", 107 "/sys/bus/pci/drivers/vfio-pci"}, 108 Symlinks: map[string]string{ 109 "/sys/bus/pci/devices/0000:d8:00.0/driver": "../../../../bus/pci/drivers/vfio-pci"}, 110 Files: map[string][]byte{ 111 "/sys/bus/pci/drivers_probe": {}, 112 "/sys/bus/pci/drivers/vfio-pci/unbind": {}}, 113 }) 114 Expect(k.BindDefaultDriver("0000:d8:00.0")).NotTo(HaveOccurred()) 115 // should unbind from dpdk driver 116 helpers.GinkgoAssertFileContentsEquals("/sys/bus/pci/drivers/vfio-pci/unbind", "0000:d8:00.0") 117 // should probe driver for dev 118 helpers.GinkgoAssertFileContentsEquals("/sys/bus/pci/drivers_probe", "0000:d8:00.0") 119 }) 120 }) 121 Context("BindDpdkDriver", func() { 122 It("unknown device", func() { 123 Expect(k.BindDpdkDriver("unknown-dev", "vfio-pci")).To(HaveOccurred()) 124 }) 125 It("no driver", func() { 126 helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{ 127 Dirs: []string{ 128 "/sys/bus/pci/devices/0000:d8:00.0", 129 "/sys/bus/pci/drivers/vfio-pci"}, 130 Files: map[string][]byte{ 131 "/sys/bus/pci/devices/0000:d8:00.0/driver_override": {}}, 132 }) 133 Expect(k.BindDpdkDriver("0000:d8:00.0", "vfio-pci")).NotTo(HaveOccurred()) 134 // should reset driver override 135 helpers.GinkgoAssertFileContentsEquals("/sys/bus/pci/devices/0000:d8:00.0/driver_override", "\x00") 136 }) 137 It("already bind to required driver", func() { 138 helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{ 139 Dirs: []string{ 140 "/sys/bus/pci/devices/0000:d8:00.0"}, 141 Symlinks: map[string]string{ 142 "/sys/bus/pci/devices/0000:d8:00.0/driver": "../../../../bus/pci/drivers/vfio-pci"}, 143 }) 144 Expect(k.BindDpdkDriver("0000:d8:00.0", "vfio-pci")).NotTo(HaveOccurred()) 145 }) 146 It("bind to wrong driver", func() { 147 helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{ 148 Dirs: []string{ 149 "/sys/bus/pci/devices/0000:d8:00.0", 150 "/sys/bus/pci/drivers/test-driver", 151 "/sys/bus/pci/drivers/vfio-pci"}, 152 Symlinks: map[string]string{ 153 "/sys/bus/pci/devices/0000:d8:00.0/driver": "../../../../bus/pci/drivers/test-driver"}, 154 Files: map[string][]byte{ 155 "/sys/bus/pci/drivers/test-driver/unbind": {}, 156 "/sys/bus/pci/drivers/vfio-pci/bind": {}, 157 "/sys/bus/pci/devices/0000:d8:00.0/driver_override": {}}, 158 }) 159 Expect(k.BindDpdkDriver("0000:d8:00.0", "vfio-pci")).NotTo(HaveOccurred()) 160 // should unbind from driver1 161 helpers.GinkgoAssertFileContentsEquals("/sys/bus/pci/drivers/test-driver/unbind", "0000:d8:00.0") 162 // should bind to driver2 163 helpers.GinkgoAssertFileContentsEquals("/sys/bus/pci/drivers/vfio-pci/bind", "0000:d8:00.0") 164 }) 165 It("fail to bind", func() { 166 helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{ 167 Dirs: []string{ 168 "/sys/bus/pci/devices/0000:d8:00.0", 169 "/sys/bus/pci/drivers/test-driver"}, 170 Symlinks: map[string]string{ 171 "/sys/bus/pci/devices/0000:d8:00.0/driver": "../../../../bus/pci/drivers/test-driver"}, 172 Files: map[string][]byte{ 173 "/sys/bus/pci/drivers/test-driver/unbind": {}, 174 "/sys/bus/pci/devices/0000:d8:00.0/driver_override": {}}, 175 }) 176 Expect(k.BindDpdkDriver("0000:d8:00.0", "vfio-pci")).To(HaveOccurred()) 177 }) 178 }) 179 Context("BindDriverByBusAndDevice", func() { 180 It("device doesn't support driver_override", func() { 181 helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{ 182 Dirs: []string{ 183 "/sys/bus/pci/devices/0000:d8:00.0", 184 "/sys/bus/pci/drivers/test-driver", 185 "/sys/bus/pci/drivers/vfio-pci"}, 186 Symlinks: map[string]string{ 187 "/sys/bus/pci/devices/0000:d8:00.0/driver": "../../../../bus/pci/drivers/test-driver"}, 188 Files: map[string][]byte{ 189 "/sys/bus/pci/drivers/test-driver/unbind": {}, 190 "/sys/bus/pci/drivers/vfio-pci/bind": {}}, 191 }) 192 Expect(k.BindDriverByBusAndDevice(consts.BusPci, "0000:d8:00.0", "vfio-pci")).NotTo(HaveOccurred()) 193 // should unbind from driver1 194 helpers.GinkgoAssertFileContentsEquals("/sys/bus/pci/drivers/test-driver/unbind", "0000:d8:00.0") 195 // should bind to driver2 196 helpers.GinkgoAssertFileContentsEquals("/sys/bus/pci/drivers/vfio-pci/bind", "0000:d8:00.0") 197 }) 198 }) 199 Context("GetDriverByBusAndDevice", func() { 200 It("device has driver", func() { 201 helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{ 202 Dirs: []string{ 203 "/sys/bus/vdpa/devices/vdpa:0000:d8:00.3"}, 204 Symlinks: map[string]string{ 205 "/sys/bus/vdpa/devices/vdpa:0000:d8:00.3/driver": "../../../../../bus/vdpa/drivers/vhost_vdpa"}, 206 }) 207 driver, err := k.GetDriverByBusAndDevice(consts.BusVdpa, "vdpa:0000:d8:00.3") 208 Expect(err).NotTo(HaveOccurred()) 209 Expect(driver).To(Equal("vhost_vdpa")) 210 }) 211 It("device has no driver", func() { 212 helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{ 213 Dirs: []string{ 214 "/sys/bus/vdpa/devices/vdpa:0000:d8:00.3"}, 215 }) 216 driver, err := k.GetDriverByBusAndDevice(consts.BusVdpa, "vdpa:0000:d8:00.3") 217 Expect(err).NotTo(HaveOccurred()) 218 Expect(driver).To(BeEmpty()) 219 }) 220 }) 221 222 Context("IsKernelLockdownMode", func() { 223 It("should return true when kernel boots in lockdown integrity", func() { 224 helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{ 225 Dirs: []string{"/host/sys/kernel/security"}, 226 Files: map[string][]byte{ 227 "/host/sys/kernel/security/lockdown": []byte("none [integrity] confidentiality")}, 228 }) 229 230 Expect(k.IsKernelLockdownMode()).To(BeTrue()) 231 }) 232 233 It("should return false when kernel lockdown is none", func() { 234 helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{ 235 Dirs: []string{"/host/sys/kernel/security"}, 236 Files: map[string][]byte{ 237 "/host/sys/kernel/security/lockdown": []byte("[none] integrity confidentiality")}, 238 }) 239 240 Expect(k.IsKernelLockdownMode()).To(BeFalse()) 241 }) 242 }) 243 }) 244 })