commit 80628a3d9024342cf532cf0994fe6a638024b908
parent bf0cafffedd6999959a33530825760e52af3fb84
Author: Daniel Barlow <dan@telent.net>
Date: Fri, 26 Apr 2024 21:44:02 +0100
move event matching tests to devout
in preparation for future uevent-watch not needing to do
event matching
Diffstat:
3 files changed, 47 insertions(+), 33 deletions(-)
diff --git a/pkgs/devout/devout.fnl b/pkgs/devout/devout.fnl
@@ -17,34 +17,38 @@
val# val#
(nil err#) (error (string.format "%s failed: errno=%d" ,(v expr) err#)))))
-(fn parse-event [s]
- (let [at (string.find s "@" 1 true)
- (nl nxt) (string.find s "\0" 1 true)]
- (doto
- (collect [k v (string.gmatch
- (string.sub s (+ 1 nxt))
- "(%g-)=(%g+)")]
- (k:lower) v)
- (tset :path (string.sub s (+ at 1) (- nl 1))))))
-
(fn format-event [e]
(..
(string.format "%s@%s\0" e.action e.path)
(table.concat
- (icollect [k v (pairs e)]
+ (icollect [k v (pairs e.attributes)]
(string.format "%s=%s" (string.upper k) v ))
"\n")))
-
(fn event-matches? [e terms]
(accumulate [match? true
name value (pairs terms)]
- (and match? (= value (. e name)))))
+ (and match? (= value (. e.attributes name)))))
+
+(fn parse-event [s]
+ (let [at (string.find s "@" 1 true)
+ (nl nxt) (string.find s "\0" 1 true)
+ attributes
+ (collect [k v (string.gmatch
+ (string.sub s (+ 1 nxt))
+ "(%g-)=(%g+)")]
+ (k:lower) v)]
+ { : attributes
+ :path (string.sub s (+ at 1) (- nl 1))
+ :action (string.sub s 1 (- at 1))
+ :format format-event
+ :matches? event-matches?
+ }))
(fn find-in-database [db terms]
(accumulate [found []
_ e (pairs db)]
- (if (event-matches? e terms)
+ (if (e:matches? terms)
(doto found (table.insert e))
found)))
@@ -57,7 +61,7 @@
:remove (tset db e.path nil)
)
(each [_ { : terms : callback } (pairs subscribers)]
- (if (event-matches? e terms) (callback e)))
+ (if (e:matches? terms) (callback e)))
e))
(fn database []
@@ -166,4 +170,4 @@
(ll.poll pollfds 5000)
(loop:feed (unpack-pollfds pollfds))))))
-{ : database : run : event-loop }
+{ : database : run : event-loop : parse-event }
diff --git a/pkgs/devout/test.fnl b/pkgs/devout/test.fnl
@@ -1,4 +1,4 @@
-(local { : database : event-loop } (require :devout))
+(local { : database : event-loop : parse-event } (require :devout))
(local { : view } (require :fennel))
(local ll (require :lualinux))
(import-macros { : expect : expect= } :anoia.assert)
@@ -14,11 +14,6 @@
(fail ,description err#)))
`(print :PENDING ,description)))
-(example
- "given an empty database, searching it finds no entries"
- (let [db (database)]
- (expect= (db:find {:partname "boot"}) [])))
-
(local sda-uevent
"add@/devices/pci0000:00/0000:00:13.0/usb1/1-1/1-1:1.0/host0/target0:0:0/0:0:0:0/block/sda\0ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:13.0/usb1/1-1/1-1:1.0/host0/target0:0:0/0:0:0:0/block/sda
@@ -54,14 +49,36 @@ SEQNUM=82386
MAJOR=8
MINOR=17")
+(example
+ "I can parse an event"
+ (let [e (parse-event sdb1-insert)]
+ (expect= e.attributes.seqnum "82381")
+ (expect= e.attributes.devname "/dev/sdb1")
+ (expect= e.path "/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/host1/target1:0:0/1:0:0:0/block/sdb/sdb1")
+ (expect= e.action :add)
+ (expect= e (parse-event (e:format)))))
+
+(example
+ "An event can match against terms"
+ (let [terms {:devname "foo" :partname "my-usbstick"}]
+ (expect= (: (parse-event "add@/\0SEQNUM=1") :matches? terms) false)
+ (expect= (: (parse-event "add@/\0DEVNAME=bill") :matches? terms) false)
+ (expect= (: (parse-event "add@/\0DEVNAME=foo\nPARTNAME=my-usbstick") :matches? terms) true)
+ (expect= (: (parse-event "add@/\0DEVNAME=foo\nPARTNAME=my-usbstick\nOTHERTHING=bar") :matches? terms) true)
+ ))
+
+(example
+ "given an empty database, searching it finds no entries"
+ (let [db (database)]
+ (expect= (db:find {:partname "boot"}) [])))
(example
"when I add a device, I can find it"
(let [db (database)]
(db:add sda-uevent)
(let [[m & more] (db:find {:devname "sda"})]
- (expect= m.devname "sda")
- (expect= m.major "8")
+ (expect= m.attributes.devname "sda")
+ (expect= m.attributes.major "8")
(expect= more []))))
(example
@@ -75,8 +92,8 @@ MINOR=17")
(let [db (database)]
(db:add sda-uevent)
(let [m (db:at-path "/devices/pci0000:00/0000:00:13.0/usb1/1-1/1-1:1.0/host0/target0:0:0/0:0:0:0/block/sda")]
- (expect= m.devname "sda")
- (expect= m.major "8"))))
+ (expect= m.attributes.devname "sda")
+ (expect= m.attributes.major "8"))))
(example
"when I add and then remove a device, I cannot retrieve it by path"
diff --git a/pkgs/uevent-watch/test.fnl b/pkgs/uevent-watch/test.fnl
@@ -3,13 +3,6 @@
(local subject (require :watch))
-(let [params
- {:matches {:devname "foo" :partname "my-usbstick"}}]
- (expect= (subject.event-matches? params {}) false)
- (expect= (subject.event-matches? params {:devname "bill"}) false)
- (expect= (subject.event-matches? params {:devname "foo" :partname "my-usbstick"}) true)
- (expect= (subject.event-matches? params {:devname "foo" :otherthing "bar" :partname "my-usbstick"}) true)
- )
;; Events come from the netlink socket as an initial summary line