forked from DGNum/liminix
service starts and stops
This commit is contained in:
parent
dd4ab41f6a
commit
f164f19d95
2 changed files with 110 additions and 30 deletions
|
@ -1,9 +1,8 @@
|
||||||
(local nl (require :anoia.nl))
|
(local nl (require :anoia.nl))
|
||||||
|
(local { : assoc : system } (require :anoia))
|
||||||
|
|
||||||
; (local { : view} (require :fennel))
|
; (local { : view} (require :fennel))
|
||||||
|
|
||||||
(local { : assoc : system } (require :anoia))
|
|
||||||
|
|
||||||
(fn parse-args [args]
|
(fn parse-args [args]
|
||||||
(match args
|
(match args
|
||||||
["-v" & rest] (assoc (parse-args rest) :verbose true)
|
["-v" & rest] (assoc (parse-args rest) :verbose true)
|
||||||
|
@ -31,9 +30,19 @@
|
||||||
|
|
||||||
_
|
_
|
||||||
{})]
|
{})]
|
||||||
(. got params.expecting)))
|
(not (not (. got params.expecting)))))
|
||||||
|
|
||||||
|
(var up :unknown)
|
||||||
|
(fn toggle-service [service wanted?]
|
||||||
|
(when (not (= up wanted?))
|
||||||
|
(set up
|
||||||
|
(if wanted?
|
||||||
|
(pcall system (.. "s6-rc -u change " service))
|
||||||
|
(not (pcall system (.. "s6-rc -d change " service)))))
|
||||||
|
))
|
||||||
|
|
||||||
(fn run [args event-fn]
|
(fn run [args event-fn]
|
||||||
|
(set up :unknown)
|
||||||
(let [parameters
|
(let [parameters
|
||||||
(assert (parse-args args)
|
(assert (parse-args args)
|
||||||
(.. "Usage: ifwait [-v] ifname [present|up|running]"))]
|
(.. "Usage: ifwait [-v] ifname [present|up|running]"))]
|
||||||
|
@ -41,9 +50,13 @@
|
||||||
(print (.. "ifwait: waiting for "
|
(print (.. "ifwait: waiting for "
|
||||||
parameters.link " to be " parameters.expecting)))
|
parameters.link " to be " parameters.expecting)))
|
||||||
|
|
||||||
|
(if parameters.service
|
||||||
|
(each [e (event-fn)]
|
||||||
|
(if (= e.name parameters.link)
|
||||||
|
(toggle-service parameters.service (event-matches? parameters e))))
|
||||||
(each [e (event-fn)
|
(each [e (event-fn)
|
||||||
&until (event-matches? parameters e)]
|
&until (event-matches? parameters e)]
|
||||||
true)))
|
true))))
|
||||||
|
|
||||||
(when (not (= (. arg 0) "test"))
|
(when (not (= (. arg 0) "test"))
|
||||||
(run arg #(nl.events {:link true})))
|
(run arg #(nl.events {:link true})))
|
||||||
|
|
|
@ -4,6 +4,14 @@
|
||||||
(var fake-system (fn [s] (print "executing " s)))
|
(var fake-system (fn [s] (print "executing " s)))
|
||||||
(tset anoia :system #(fake-system $1))
|
(tset anoia :system #(fake-system $1))
|
||||||
|
|
||||||
|
(macro expect= [actual expected]
|
||||||
|
`(let [ve# (view ,expected)
|
||||||
|
va# (view ,actual)]
|
||||||
|
(when (not (= ve# va#))
|
||||||
|
(assert false
|
||||||
|
(.. "\nexpected " ve# "\ngot " va#)
|
||||||
|
))))
|
||||||
|
|
||||||
(fn event-generator [events]
|
(fn event-generator [events]
|
||||||
(coroutine.wrap
|
(coroutine.wrap
|
||||||
(fn []
|
(fn []
|
||||||
|
@ -19,34 +27,93 @@
|
||||||
(local ifwait (require :ifwait))
|
(local ifwait (require :ifwait))
|
||||||
|
|
||||||
(let [gen (event-generator (file-events "events-fixture"))]
|
(let [gen (event-generator (file-events "events-fixture"))]
|
||||||
(ifwait.run ["-v" "dummy0" "up"] #gen)
|
(ifwait.run ["dummy0" "up"] #gen)
|
||||||
(match (pcall gen)
|
(match (pcall gen)
|
||||||
(true _) true
|
(true _) true
|
||||||
(false msg) (error "didn't detect dummy0 up event")))
|
(false msg) (error "didn't detect dummy0 up event")))
|
||||||
|
|
||||||
(var succeeded? false)
|
(var upsies [])
|
||||||
(set fake-system
|
(set fake-system
|
||||||
(fn [s]
|
(fn [s]
|
||||||
(print "exec" s)
|
(if (s:match "-u change addmember")
|
||||||
(if (s:match "addmember") (set succeeded? true))))
|
(table.insert upsies :u)
|
||||||
|
(s:match "-d change addmember")
|
||||||
|
(table.insert upsies :d))))
|
||||||
|
|
||||||
(let [events
|
(fn newlink [name up running]
|
||||||
[{:event "newlink"
|
|
||||||
:hwaddr "b6:7d:5c:38:89:1d"
|
|
||||||
:index 21
|
|
||||||
:mtu 1500
|
|
||||||
:name "dummy0"
|
|
||||||
:running "no"
|
|
||||||
:stamp 857161382
|
|
||||||
:up "no"}
|
|
||||||
{:event "newlink"
|
{:event "newlink"
|
||||||
:hwaddr "52:f0:46:da:0c:0c"
|
:hwaddr "b6:7d:5c:38:89:1d"
|
||||||
:index 22
|
:index (string.unpack ">i2" name)
|
||||||
:mtu 1500
|
:mtu 1500
|
||||||
:name "dummy0"
|
: name
|
||||||
:running "no"
|
: running
|
||||||
:stamp 857161383
|
:stamp 857161382
|
||||||
:up "yes"}]
|
: up })
|
||||||
gen (event-generator events)]
|
|
||||||
(ifwait.run ["-v" "-s" "addmember" "dummy0" "up"] #gen)
|
"when it gets events that don't match the interface, nothing happens"
|
||||||
(assert succeeded?))
|
|
||||||
|
(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]))
|
||||||
|
|
Loading…
Reference in a new issue