2024-03-06 01:32:38 +01:00
|
|
|
(local { : view &as fennel } (require :fennel))
|
|
|
|
(local anoia (require :anoia))
|
|
|
|
|
|
|
|
(var fake-system (fn [s] (print "executing " s)))
|
|
|
|
(tset anoia :system #(fake-system $1))
|
2024-03-05 00:06:03 +01:00
|
|
|
|
2024-03-07 00:18:28 +01:00
|
|
|
(macro expect= [actual expected]
|
|
|
|
`(let [ve# (view ,expected)
|
|
|
|
va# (view ,actual)]
|
|
|
|
(when (not (= ve# va#))
|
|
|
|
(assert false
|
|
|
|
(.. "\nexpected " ve# "\ngot " va#)
|
|
|
|
))))
|
|
|
|
|
2024-03-05 23:57:20 +01:00
|
|
|
(fn event-generator [events]
|
|
|
|
(coroutine.wrap
|
|
|
|
(fn []
|
|
|
|
(each [_ e (ipairs events)] (coroutine.yield e)))))
|
|
|
|
|
|
|
|
(fn file-events [path]
|
|
|
|
(let [data (with-open [e (io.open path "r")] (e:read "*a"))
|
2024-03-05 00:06:03 +01:00
|
|
|
parse (fennel.parser data)]
|
2024-03-05 23:57:20 +01:00
|
|
|
(icollect [_ ast parse]
|
|
|
|
ast)))
|
|
|
|
|
2024-03-06 01:10:59 +01:00
|
|
|
(set _G.arg (doto [] (tset 0 "test")))
|
2024-03-05 18:56:59 +01:00
|
|
|
(local ifwait (require :ifwait))
|
|
|
|
|
2024-03-06 00:22:46 +01:00
|
|
|
(let [gen (event-generator (file-events "events-fixture"))]
|
2024-03-07 00:18:28 +01:00
|
|
|
(ifwait.run ["dummy0" "up"] #gen)
|
2024-03-06 00:22:46 +01:00
|
|
|
(match (pcall gen)
|
|
|
|
(true _) true
|
|
|
|
(false msg) (error "didn't detect dummy0 up event")))
|
2024-03-06 01:32:38 +01:00
|
|
|
|
2024-03-07 00:18:28 +01:00
|
|
|
(var upsies [])
|
2024-03-06 01:32:38 +01:00
|
|
|
(set fake-system
|
|
|
|
(fn [s]
|
2024-03-07 00:18:28 +01:00
|
|
|
(if (s:match "-u change addmember")
|
|
|
|
(table.insert upsies :u)
|
|
|
|
(s:match "-d change addmember")
|
|
|
|
(table.insert upsies :d))))
|
|
|
|
|
|
|
|
(fn newlink [name up running]
|
|
|
|
{:event "newlink"
|
|
|
|
:hwaddr "b6:7d:5c:38:89:1d"
|
|
|
|
:index (string.unpack ">i2" name)
|
|
|
|
:mtu 1500
|
|
|
|
: name
|
|
|
|
: running
|
|
|
|
:stamp 857161382
|
|
|
|
: up })
|
|
|
|
|
|
|
|
"when it gets events that don't match the interface, nothing happens"
|
|
|
|
|
|
|
|
(let [gen (-> [(newlink "eth1" "no" "no")] event-generator)]
|
|
|
|
(set upsies [])
|
|
|
|
(ifwait.run [ "-s" "addmember" "dummy0" "up"] #gen)
|
|
|
|
(expect= upsies []))
|
|
|
|
|
|
|
|
"when it gets an event that should start the service, the service starts"
|
|
|
|
|
|
|
|
(let [gen (->
|
|
|
|
[(newlink "dummy0" "no" "no")
|
|
|
|
(newlink "dummy0" "yes" "no")
|
|
|
|
(newlink "eth1" "no" "no")]
|
|
|
|
event-generator)]
|
|
|
|
(set upsies [])
|
|
|
|
(ifwait.run ["-s" "addmember" "dummy0" "up"] #gen)
|
|
|
|
(expect= upsies [:d :u]))
|
|
|
|
|
|
|
|
"when it gets an event that should stop the service, the service stops"
|
|
|
|
|
|
|
|
(let [gen (->
|
|
|
|
[(newlink "dummy0" "no" "no")
|
|
|
|
(newlink "dummy0" "yes" "no")
|
|
|
|
(newlink "dummy0" "no" "no")
|
|
|
|
]
|
|
|
|
event-generator)]
|
|
|
|
(set upsies [])
|
|
|
|
(ifwait.run ["-s" "addmember" "dummy0" "up"] #gen)
|
|
|
|
(expect= upsies [:d :u :d]))
|
|
|
|
|
|
|
|
"it does not call s6-rc again if the service is already in required state"
|
|
|
|
|
|
|
|
(let [gen (->
|
|
|
|
[(newlink "dummy0" "no" "no")
|
|
|
|
(newlink "dummy0" "yes" "no")
|
|
|
|
(newlink "dummy0" "yes" "yes")
|
|
|
|
(newlink "dummy0" "yes" "yes")
|
|
|
|
(newlink "dummy0" "yes" "no")
|
|
|
|
(newlink "dummy0" "no" "no")
|
|
|
|
]
|
|
|
|
event-generator)]
|
|
|
|
(set upsies [])
|
|
|
|
(ifwait.run ["-s" "addmember" "dummy0" "up"] #gen)
|
|
|
|
(expect= upsies [:d :u :d]))
|
|
|
|
|
|
|
|
"it handles an error return from s6-rc"
|
|
|
|
|
|
|
|
(set fake-system
|
|
|
|
(fn [s]
|
|
|
|
(if (s:match "-u change addmember")
|
|
|
|
(table.insert upsies :u)
|
|
|
|
(s:match "-d change addmember")
|
|
|
|
(table.insert upsies :d))
|
|
|
|
(error "false")
|
|
|
|
))
|
|
|
|
|
|
|
|
(let [gen (->
|
|
|
|
[(newlink "dummy0" "yes" "no")
|
|
|
|
(newlink "dummy0" "yes" "yes")
|
|
|
|
(newlink "dummy0" "yes" "yes")
|
|
|
|
(newlink "dummy0" "yes" "no")
|
|
|
|
(newlink "dummy0" "no" "no")
|
|
|
|
]
|
|
|
|
event-generator)]
|
|
|
|
(set upsies [])
|
|
|
|
(ifwait.run ["-s" "addmember" "dummy0" "up"] #gen)
|
|
|
|
(expect= upsies [:u :u :u :u]))
|