/test/TEST-15-DROPIN/test-dropin.sh

https://gitlab.com/parabola/user_lukeshu_systemd · Shell · 274 lines · 207 code · 42 blank · 25 comment · 3 complexity · 1ec55ba79674c80581f29277fcb3a64b MD5 · raw file

  1. #! /bin/bash
  2. set -e
  3. set -x
  4. _clear_service () {
  5. systemctl stop $1.service 2>/dev/null || :
  6. rm -f /{etc,run,usr/lib}/systemd/system/$1.service
  7. rm -fr /{etc,run,usr/lib}/systemd/system/$1.service.d
  8. rm -fr /{etc,run,usr/lib}/systemd/system/$1.service.{wants,requires}
  9. }
  10. clear_services () {
  11. for u in $*; do
  12. _clear_service $u
  13. done
  14. systemctl daemon-reload
  15. }
  16. create_service () {
  17. clear_services $1
  18. cat >/etc/systemd/system/$1.service<<EOF
  19. [Unit]
  20. Description=$1 unit
  21. [Service]
  22. ExecStart=/bin/sleep 100000
  23. EOF
  24. mkdir -p /{etc,run,usr/lib}/systemd/system/$1.service.d
  25. mkdir -p /etc/systemd/system/$1.service.{wants,requires}
  26. mkdir -p /run/systemd/system/$1.service.{wants,requires}
  27. mkdir -p /usr/lib/systemd/system/$1.service.{wants,requires}
  28. }
  29. create_services () {
  30. for u in $*; do
  31. create_service $u
  32. done
  33. }
  34. check_ok () {
  35. [ $# -eq 3 ] || return
  36. x="$(systemctl show --value -p $2 $1)"
  37. case "$x" in
  38. *$3*) return 0 ;;
  39. *) return 1
  40. esac
  41. }
  42. check_ko () {
  43. ! check_ok "$@"
  44. }
  45. test_basic_dropins () {
  46. echo "Testing basic dropins..."
  47. echo "*** test a wants b wants c"
  48. create_services a b c
  49. ln -s ../b.service /etc/systemd/system/a.service.wants/
  50. ln -s ../c.service /etc/systemd/system/b.service.wants/
  51. check_ok a Wants b.service
  52. check_ok b Wants c.service
  53. echo "*** test a wants,requires b"
  54. create_services a b c
  55. ln -s ../b.service /etc/systemd/system/a.service.wants/
  56. ln -s ../b.service /etc/systemd/system/a.service.requires/
  57. check_ok a Wants b.service
  58. check_ok a Requires b.service
  59. echo "*** test a wants nonexistent"
  60. create_service a
  61. ln -s ../nonexistent.service /etc/systemd/system/a.service.wants/
  62. check_ok a Wants nonexistent.service
  63. systemctl start a
  64. systemctl stop a
  65. echo "*** test a requires nonexistent"
  66. ln -sf ../nonexistent.service /etc/systemd/system/a.service.requires/
  67. systemctl daemon-reload
  68. check_ok a Requires nonexistent.service
  69. # 'b' is already loaded when 'c' pulls it in via a dropin.
  70. echo "*** test a,c require b"
  71. create_services a b c
  72. ln -sf ../b.service /etc/systemd/system/a.service.requires/
  73. ln -sf ../b.service /etc/systemd/system/c.service.requires/
  74. systemctl start a
  75. check_ok c Requires b.service
  76. systemctl stop a b
  77. # 'b' is already loaded when 'c' pulls it in via an alias dropin.
  78. echo "*** test a wants alias"
  79. create_services a b c
  80. ln -sf c.service /etc/systemd/system/c1.service
  81. ln -sf ../c.service /etc/systemd/system/a.service.wants/
  82. ln -sf ../c1.service /etc/systemd/system/b.service.wants/
  83. systemctl start a
  84. check_ok a Wants c.service
  85. check_ok b Wants c.service
  86. systemctl stop a c
  87. clear_services a b c
  88. }
  89. test_template_dropins () {
  90. echo "Testing template dropins..."
  91. create_services foo bar@ yup@
  92. ln -s /etc/systemd/system/bar@.service /etc/systemd/system/foo.service.wants/bar@1.service
  93. check_ok foo Wants bar@1.service
  94. clear_services foo bar@ yup@
  95. }
  96. test_alias_dropins () {
  97. echo "Testing alias dropins..."
  98. echo "*** test a wants b1 alias of b"
  99. create_services a b
  100. ln -sf b.service /etc/systemd/system/b1.service
  101. ln -sf ../b1.service /etc/systemd/system/a.service.wants/
  102. check_ok a Wants b.service
  103. systemctl start a
  104. systemctl --quiet is-active b
  105. systemctl stop a b
  106. rm /etc/systemd/system/b1.service
  107. clear_services a b
  108. # A weird behavior: the dependencies for 'a' may vary. It can be
  109. # changed by loading an alias...
  110. #
  111. # [1] 'a1' is loaded and then "renamed" into 'a'. 'a1' is therefore
  112. # part of the names set so all its specific dropins are loaded.
  113. #
  114. # [2] 'a' is already loaded. 'a1' is simply only merged into 'a' so
  115. # none of its dropins are loaded ('y' is missing from the deps).
  116. echo "*** test 2"
  117. create_services a x y
  118. mkdir -p /etc/systemd/system/a1.service.wants/
  119. ln -sf a.service /etc/systemd/system/a1.service
  120. ln -sf ../x.service /etc/systemd/system/a.service.wants/
  121. ln -sf ../y.service /etc/systemd/system/a1.service.wants/
  122. check_ok a1 Wants x.service # see [1]
  123. check_ok a1 Wants y.service
  124. systemctl start a
  125. check_ok a1 Wants x.service # see [2]
  126. check_ko a1 Wants y.service
  127. systemctl stop a x y
  128. rm /etc/systemd/system/a1.service
  129. clear_services a x y
  130. }
  131. test_masked_dropins () {
  132. echo "Testing masked dropins..."
  133. create_services a b
  134. # 'b' is masked for both deps
  135. echo "*** test a wants,requires b is masked"
  136. ln -sf /dev/null /etc/systemd/system/a.service.wants/b.service
  137. ln -sf /dev/null /etc/systemd/system/a.service.requires/b.service
  138. check_ko a Wants b.service
  139. check_ko a Requires b.service
  140. # 'a' wants 'b' and 'b' is masked at a lower level
  141. echo "*** test a wants b, mask override"
  142. ln -sf ../b.service /etc/systemd/system/a.service.wants/b.service
  143. ln -sf /dev/null /usr/lib/systemd/system/a.service.wants/b.service
  144. check_ok a Wants b.service
  145. # 'a' wants 'b' and 'b' is masked at a higher level
  146. echo "*** test a wants b, mask"
  147. ln -sf /dev/null /etc/systemd/system/a.service.wants/b.service
  148. ln -sf ../b.service /usr/lib/systemd/system/a.service.wants/b.service
  149. check_ko a Wants b.service
  150. # 'b1' is an alias for 'b': masking 'b' dep should not influence 'b1' dep
  151. echo "*** test a wants b, b1, and one is masked"
  152. create_services a b
  153. ln -sf b.service /etc/systemd/system/b1.service
  154. ln -sf /dev/null /etc/systemd/system/a.service.wants/b.service
  155. ln -sf ../b1.service /usr/lib/systemd/system/a.service.wants/b1.service
  156. systemctl cat a
  157. systemctl show -p Wants,Requires a
  158. systemctl cat b1
  159. systemctl show -p Wants,Requires b1
  160. check_ok a Wants b.service
  161. check_ko a Wants b1.service # the alias does not show up in the list of units
  162. rm /etc/systemd/system/b1.service
  163. # 'b1' is an alias for 'b': masking 'b1' should not influence 'b' dep
  164. echo "*** test a wants b, alias dep is masked"
  165. create_services a b
  166. ln -sf b.service /etc/systemd/system/b1.service
  167. ln -sf /dev/null /etc/systemd/system/a.service.wants/b1.service
  168. ln -sf ../b.service /usr/lib/systemd/system/a.service.wants/b.service
  169. check_ok a Wants b.service
  170. check_ko a Wants b1.service # the alias does not show up in the list of units
  171. rm /etc/systemd/system/b1.service
  172. # 'a' has Wants=b.service but also has a masking
  173. # dropin 'b': 'b' should still be pulled in.
  174. echo "*** test a wants b both ways"
  175. create_services a b
  176. ln -sf /dev/null /etc/systemd/system/a.service.wants/b.service
  177. cat >/usr/lib/systemd/system/a.service.d/wants-b.conf<<EOF
  178. [Unit]
  179. Wants=b.service
  180. EOF
  181. check_ok a Wants b.service
  182. # mask a dropin that points to an nonexistent unit.
  183. echo "*** test a wants nonexistent is masked"
  184. create_services a
  185. ln -sf /dev/null /etc/systemd/system/a.service.requires/nonexistent.service
  186. ln -sf ../nonexistent.service /usr/lib/systemd/system/a.service.requires/
  187. check_ko a Requires nonexistent.service
  188. # 'b' is already loaded when 'c' pulls it in via a dropin but 'b' is
  189. # masked at a higher level.
  190. echo "*** test a wants b is masked"
  191. create_services a b c
  192. ln -sf ../b.service /etc/systemd/system/a.service.requires/
  193. ln -sf ../b.service /run/systemd/system/c.service.requires/
  194. ln -sf /dev/null /etc/systemd/system/c.service.requires/b.service
  195. systemctl start a
  196. check_ko c Requires b.service
  197. systemctl stop a b
  198. # 'b' is already loaded when 'c' pulls it in via a dropin but 'b' is
  199. # masked at a lower level.
  200. echo "*** test a requires b is masked"
  201. create_services a b c
  202. ln -sf ../b.service /etc/systemd/system/a.service.requires/
  203. ln -sf ../b.service /etc/systemd/system/c.service.requires/
  204. ln -sf /dev/null /run/systemd/system/c.service.requires/b.service
  205. systemctl start a
  206. check_ok c Requires b.service
  207. systemctl stop a b
  208. # 'a' requires 2 aliases of 'b' and one of them is a mask.
  209. echo "*** test a requires alias of b, other alias masked"
  210. create_services a b
  211. ln -sf b.service /etc/systemd/system/b1.service
  212. ln -sf b.service /etc/systemd/system/b2.service
  213. ln -sf /dev/null /etc/systemd/system/a.service.requires/b1.service
  214. ln -sf ../b1.service /run/systemd/system/a.service.requires/
  215. ln -sf ../b2.service /usr/lib/systemd/system/a.service.requires/
  216. check_ok a Requires b
  217. # Same as above but now 'b' is masked.
  218. echo "*** test a requires alias of b, b dep masked"
  219. create_services a b
  220. ln -sf b.service /etc/systemd/system/b1.service
  221. ln -sf b.service /etc/systemd/system/b2.service
  222. ln -sf ../b1.service /run/systemd/system/a.service.requires/
  223. ln -sf ../b2.service /usr/lib/systemd/system/a.service.requires/
  224. ln -sf /dev/null /etc/systemd/system/a.service.requires/b.service
  225. check_ok a Requires b
  226. clear_services a b
  227. }
  228. test_basic_dropins
  229. test_template_dropins
  230. test_alias_dropins
  231. test_masked_dropins
  232. touch /testok