//nolint:typecheck package devices import ( "io/fs" "path/filepath" . "github.com/onsi/ginkgo/v2" //nolint:revive "github.com/onsi/gomega" . "github.com/onsi/gomega" //nolint:revive ) var _ = Describe("Crawl device tree", func() { It("Match all devices", func() { opts := []Option{WithMatchAll(true)} devices, err := Find(opts...) Expect(err).To(BeNil()) Expect(devices).To(HaveLen(8)) // expect 8 devices in the device tree }) It("Match all input devices", func() { m := NewMatcher().WithRules(NewPropRule("SUBSYSTEM", "input")) Expect(m.SearchPaths()).To(gomega.ContainElement(filepath.Join(SysPath, "class", "input"))) subSystemSymlinkFn = func(path string) (string, error) { return filepath.Dir(filepath.Dir(path)), nil } opts := []Option{WithMatchers(m)} devices, err := Find(opts...) Expect(err).To(BeNil()) Expect(devices).To(HaveLen(3)) // expect input devices and its children }) It("Match against idVendor and idProduct attributes", func() { m := NewMatcher().WithRules(NewAttrRule("idProduct", "0002"), NewAttrRule("idVendor", "1d6b"), NewPropRule("SUBSYSTEM", "usb")) Expect(m.SearchPaths()).To(gomega.ContainElement(filepath.Join(SysPath, "class", "usb"))) opts := []Option{WithMatchers(m)} devices, err := Find(opts...) Expect(err).To(BeNil()) Expect(devices).To(HaveLen(8)) // expect 8 input devices in the device tree }) It("Match against idVendor and idProduct attributes through glob", func() { m := NewMatcher().WithRules(NewAttrRule("idProduct", "*"), NewAttrRule("idVendor", "*"), NewPropRule("SUBSYSTEM", "usb")) Expect(m.SearchPaths()).To(gomega.ContainElement(filepath.Join(SysPath, "class", "usb"))) opts := []Option{WithMatchers(m)} devices, err := Find(opts...) Expect(err).To(BeNil()) Expect(devices).To(HaveLen(8)) // expect 8 input devices in the device tree }) }) var _ = Describe("Device has ascendants", func() { It("Root device has no ascendants", func() { opts := []Option{WithMatchAll(true)} devices, err := Find(opts...) Expect(err).To(BeNil()) Expect(hasAscendant(testDevicePath, devices)).To(BeFalse()) }) It("Immediate child of root device as ascendant", func() { opts := []Option{WithMatchAll(true)} devices, err := Find(opts...) Expect(err).To(BeNil()) Expect(hasAscendant(filepath.Join(testDevicePath, "1-5"), devices)).To(BeTrue()) }) It("Deep rooted child device has ascendant", func() { opts := []Option{WithMatchAll(true)} devices, err := Find(opts...) Expect(err).To(BeNil()) Expect(hasAscendant(filepath.Join(testDevicePath, "1-5", "1-5-1-0"), devices)).To(BeTrue()) }) }) var _ = Describe("Device symlink resolved to absolute path", func() { It("Real absolute path", func() { realPath, err := fetchRealPath(sysDevicePath) Expect(err).To(BeNil()) Expect(realPath).To(BeEquivalentTo(sysDevicePath)) }) It("Symlink path", func() { isSymlinkFn = func(_ fs.FileInfo) bool { return true } realPath, err := fetchRealPath(filepath.Join(sysClassPath, "input")) Expect(err).To(BeNil()) // expect to include bazel symlink and absolute path to input dir Expect(realPath).To(ContainSubstring(filepath.Join("pkg", "lib", "kernel", "devices", sysClassPath, "input"))) }) })