forked from hauleth/erlang-systemd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystemd_SUITE.erl
459 lines (381 loc) · 15.2 KB
/
systemd_SUITE.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
-module(systemd_SUITE).
-compile(export_all).
-include_lib("stdlib/include/assert.hrl").
-include_lib("common_test/include/ct.hrl").
all() -> [notify, watchdog, ready, listen_fds, socket, fds].
init_per_testcase(Name, Config0) ->
PrivDir = ?config(priv_dir, Config0),
Path = socket_path(PrivDir),
{ok, Socket} = socket:open(local, dgram, default),
{ok, _Port} = socket:bind(Socket, #{family => local, path => Path}),
Config1 = [{socket, Socket}, {path, Path} | Config0],
case erlang:function_exported(?MODULE, Name, 2) of
true -> ?MODULE:Name(init, Config1);
false -> Config1
end.
%% This hack is needed to reduce length of the socket path which is limited to
%% 104-108 characters (depends on the OS).
-spec socket_path(PrivDir :: file:name()) -> string().
socket_path(PrivDir0) ->
{ok, Cwd} = file:get_cwd(),
PrivDir = string:prefix(PrivDir0, Cwd),
erlang:binary_to_list(iolist_to_binary([".", PrivDir, "systemd.sock"])).
end_per_testcase(Name, Config0) ->
Config = case erlang:function_exported(?MODULE, Name, 2) of
true -> ?MODULE:Name(finish, Config0);
false -> Config0
end,
_ = application:stop(systemd),
_ = socket:close(?config(socket, Config)),
_ = file:delete(?config(path, Config)),
systemd:unset_env(notify),
systemd:unset_env(watchdog),
systemd:unset_env(listen_fds),
Config.
notify(init, Config) ->
Socket = ?config(socket, Config),
ok = start_with_socket(Socket),
Config;
notify(_, Config) ->
Config.
notify(Config) ->
Socket = ?config(socket, Config),
systemd:notify(ready),
{ok, <<"READY=1\n">>} = recv(Socket),
systemd:notify(stopping),
{ok, <<"STOPPING=1\n">>} = recv(Socket),
systemd:notify(reloading),
{ok, <<"RELOADING=1\n">>} = recv(Socket),
systemd:notify({status, "example status"}),
{ok, <<"STATUS=example status\n">>} = recv(Socket),
systemd:notify({errno, 10}),
{ok, <<"ERRNO=10\n">>} = recv(Socket),
systemd:notify({buserror, "test.example.bus.service.Error"}),
{ok, <<"BUSERROR=test.example.bus.service.Error\n">>} = recv(Socket),
systemd:notify({extend_timeout, {5, microsecond}}),
{ok, <<"EXTEND_TIMEOUT_USEC=5\n">>} = recv(Socket),
systemd:notify({extend_timeout, {5, millisecond}}),
{ok, <<"EXTEND_TIMEOUT_USEC=5000\n">>} = recv(Socket),
systemd:notify({extend_timeout, {5, second}}),
{ok, <<"EXTEND_TIMEOUT_USEC=5000000\n">>} = recv(Socket),
systemd:notify({custom, "message"}),
{ok, <<"CUSTOM=message\n">>} = recv(Socket),
ct:log("Connection address persists between process restarts"),
gen_server:stop(systemd_socket, error, 100),
systemd:notify(ready),
{ok, <<"READY=1\n">>} = recv(Socket),
ok.
watchdog(init, Config) -> Config;
watchdog(finish, Config) ->
os:unsetenv("WATCHDOG_USEC"),
os:unsetenv("WATCHDOG_PID"),
Config.
watchdog(Config) ->
Socket = ?config(socket, Config),
Timeout = 200,
Timeout0 = erlang:convert_time_unit(Timeout,
millisecond,
microsecond),
TimeoutList = integer_to_list(Timeout0),
% -------------------------------------------------------------------------
ct:log("Watchdog sends messages when WATCHDOG_USEC is set"),
os:putenv("WATCHDOG_USEC", TimeoutList),
ok = start_with_socket(Socket),
?assertEqual(Timeout, systemd:watchdog(state)),
ct:sleep(300),
{ok, <<"WATCHDOG=1\n">>} = recv(Socket),
{ok, <<"WATCHDOG=1\n">>} = recv(Socket),
ok = stop(Config),
% % -------------------------------------------------------------------------
ct:log("Watchdog do not send messages when WATCHDOG_PID mismatch"),
os:putenv("WATCHDOG_PID", "foo"),
os:putenv("WATCHDOG_USEC", TimeoutList),
ok = start_with_socket(Socket),
false = systemd:watchdog(state),
ct:sleep(300),
ok = empty(Socket),
ok = stop(Config),
% % -------------------------------------------------------------------------
ct:log("Watchdog send messages when WATCHDOG_PID match"),
os:putenv("WATCHDOG_PID", os:getpid()),
os:putenv("WATCHDOG_USEC", TimeoutList),
ok = start_with_socket(Socket),
Timeout = systemd:watchdog(state),
ct:sleep(300),
{ok, <<"WATCHDOG=1\n">>} = recv(Socket),
{ok, <<"WATCHDOG=1\n">>} = recv(Socket),
ok = stop(Config),
% % -------------------------------------------------------------------------
ct:log("Enabling invalid Watchdog does nothing"),
os:putenv("WATCHDOG_USEC", "0"),
ok = start_with_socket(Socket),
false = systemd:watchdog(state),
ok = empty(Socket),
systemd:watchdog(enable),
false = systemd:watchdog(state),
ok = empty(Socket),
ok = stop(Config),
% % -------------------------------------------------------------------------
ct:log("Watchdog do not send messages when WATCHDOG_USEC is zero"),
os:putenv("WATCHDOG_USEC", "0"),
ok = start_with_socket(Socket),
false = systemd:watchdog(state),
ct:sleep(300),
ok = empty(Socket),
ok = stop(Config),
% % -------------------------------------------------------------------------
ct:log("Watchdog do not send messages when WATCHDOG_USEC is non integer"),
os:putenv("WATCHDOG_USEC", "foo"),
ok = start_with_socket(Socket),
false = systemd:watchdog(state),
ct:sleep(300),
ok = empty(Socket),
ok = stop(Config),
os:putenv("WATCHDOG_USEC", "1.0"),
ok = start_with_socket(Socket),
false = systemd:watchdog(state),
ct:sleep(300),
ok = empty(Socket),
ok = stop(Config),
% % -------------------------------------------------------------------------
ct:log("Watchdog control functions"),
os:putenv("WATCHDOG_USEC", TimeoutList),
ok = start_with_socket(Socket),
{ok, <<"WATCHDOG=1\n">>} = recv(Socket),
flush(Socket),
ct:log("-> state enabled"),
?assertEqual(Timeout, systemd:watchdog(state)),
ct:sleep(300),
{ok, <<"WATCHDOG=1\n">>} = recv(Socket),
{ok, <<"WATCHDOG=1\n">>} = recv(Socket),
flush(Socket),
ct:log("-> disable"),
ok = systemd:watchdog(disable),
ct:sleep(300),
ok = empty(Socket),
ct:log("-> state disabled"),
?assertEqual(false, systemd:watchdog(state)),
ct:sleep(300),
ok = empty(Socket),
ct:log("-> ping disabled"),
ok = systemd:watchdog(ping),
ct:sleep(300),
{ok, <<"WATCHDOG=1\n">>} = recv(Socket),
flush(Socket),
ct:log("-> enable"),
ok = systemd:watchdog(enable),
ct:sleep(300),
{ok, <<"WATCHDOG=1\n">>} = recv(Socket),
{ok, <<"WATCHDOG=1\n">>} = recv(Socket),
flush(Socket),
ct:log("-> trigger"),
ok = systemd:watchdog(trigger),
{ok, <<"WATCHDOG=trigger\n">>} = recv(Socket),
flush(Socket),
ok = stop(Config),
% % -------------------------------------------------------------------------
ct:log("Watchdog process send messages after restart"),
os:putenv("WATCHDOG_PID", os:getpid()),
os:putenv("WATCHDOG_USEC", TimeoutList),
ok = start_with_socket(Socket),
{ok, <<"WATCHDOG=1\n">>} = recv(Socket),
ok = empty(Socket),
gen_server:stop(systemd_watchdog, error, 100),
{ok, <<"WATCHDOG=1\n">>} = recv(Socket),
ok = empty(Socket),
ok = stop(Config),
% % -------------------------------------------------------------------------
ct:log("By default unsets variables"),
os:putenv("WATCHDOG_PID", os:getpid()),
os:putenv("WATCHDOG_USEC", TimeoutList),
ok = start_with_socket(Socket),
false = os:getenv("WATCHDOG_PID"),
false = os:getenv("WATCHDOG_USEC"),
ok = stop(Config),
% % -------------------------------------------------------------------------
ct:log("Do not unset env when unset_env is false"),
os:putenv("WATCHDOG_PID", os:getpid()),
os:putenv("WATCHDOG_USEC", TimeoutList),
ok = application:set_env(systemd, unset_env, false),
ok = start_with_socket(Socket),
?assertEqual(os:getpid(), os:getenv("WATCHDOG_PID")),
TimeoutList = os:getenv("WATCHDOG_USEC"),
ok = application:set_env(systemd, unset_env, true),
ok = stop(Config),
ok.
ready(init, Config) ->
ok = start_with_socket(?config(socket, Config)),
Config;
ready(_, Config) ->
stop(Config),
Config.
ready(Config) ->
Socket = ?config(socket, Config),
{ok, _Pid} = mock_supervisor:start_link([systemd:ready()]),
{ok, <<"READY=1\n">>} = recv(Socket),
ok.
listen_fds(init, Config) -> Config;
listen_fds(finish, Config) ->
os:unsetenv("LISTEN_PID"),
os:unsetenv("LISTEN_FDS"),
os:unsetenv("LISTEN_FDNAMES"),
Config.
listen_fds(_Config) ->
% -------------------------------------------------------------------------
ct:log("When no environment variables it returns empty list"),
?assertEqual([], systemd:listen_fds()),
systemd:unset_env(listen_fds),
% -------------------------------------------------------------------------
ct:log("When no LISTEN_PID environment variable is set it returns empty list"),
os:putenv("LISTEN_FDS", "3"),
os:putenv("LISTEN_FDNAMES", "3"),
?assertEqual([], systemd:listen_fds()),
systemd:unset_env(listen_fds),
% -------------------------------------------------------------------------
ct:log("When LISTEN_PID mismatch it returns empty list"),
os:putenv("LISTEN_PID", "foo"),
os:putenv("LISTEN_FDS", "3"),
os:putenv("LISTEN_FDNAMES", "foo:bar:baz"),
?assertEqual([], systemd:listen_fds()),
systemd:unset_env(listen_fds),
% -------------------------------------------------------------------------
ct:log("When LISTEN_PID match returned list has LISTEN_FDS amount of entries"),
os:putenv("LISTEN_PID", os:getpid()),
os:putenv("LISTEN_FDS", "3"),
?assertEqual(3, length(systemd:listen_fds())),
systemd:unset_env(listen_fds),
% -------------------------------------------------------------------------
ct:log("When LISTEN_FDS is not set, it is assumed to be 0"),
os:putenv("LISTEN_PID", os:getpid()),
?assertEqual([], systemd:listen_fds()),
systemd:unset_env(listen_fds),
% -------------------------------------------------------------------------
ct:log("When LISTEN_FDS is not integer, it is assumed to be 0"),
os:putenv("LISTEN_PID", os:getpid()),
os:putenv("LISTEN_FDS", "foo"),
?assertEqual([], systemd:listen_fds()),
systemd:unset_env(listen_fds),
os:putenv("LISTEN_PID", os:getpid()),
os:putenv("LISTEN_FDS", "1.0"),
?assertEqual([], systemd:listen_fds()),
systemd:unset_env(listen_fds),
% -------------------------------------------------------------------------
ct:log("Returned list has PIDs with respective names"),
os:putenv("LISTEN_PID", os:getpid()),
os:putenv("LISTEN_FDS", "3"),
os:putenv("LISTEN_FDNAMES", "foo:bar:baz"),
?assertMatch([{_, "foo"}, {_, "bar"}, {_, "baz"}], systemd:listen_fds()),
systemd:unset_env(listen_fds),
% -------------------------------------------------------------------------
ct:log("When subsequent calls will return the same data"),
os:putenv("LISTEN_PID", os:getpid()),
os:putenv("LISTEN_FDS", "3"),
os:putenv("LISTEN_FDNAMES", "foo:bar:baz"),
First = systemd:listen_fds(),
First = systemd:listen_fds(),
systemd:unset_env(listen_fds),
[] = systemd:listen_fds(),
[] = systemd:listen_fds(),
% -------------------------------------------------------------------------
ct:log("When name is empty it returns raw FD"),
os:putenv("LISTEN_PID", os:getpid()),
os:putenv("LISTEN_FDS", "3"),
os:putenv("LISTEN_FDNAMES", "foo::baz"),
?assertMatch([{_, "foo"}, _, {_, "baz"}], systemd:listen_fds()),
systemd:unset_env(listen_fds),
% -------------------------------------------------------------------------
ct:log("When names list is shorter it returns raw FDs"),
os:putenv("LISTEN_PID", os:getpid()),
os:putenv("LISTEN_FDS", "3"),
os:putenv("LISTEN_FDNAMES", "foo:bar"),
?assertMatch([{_, "foo"}, {_, "bar"}, _], systemd:listen_fds()),
systemd:unset_env(listen_fds),
ok.
socket(Config) ->
ct:log("~p", [Config]),
Socket = ?config(socket, Config),
% -------------------------------------------------------------------------
ct:log("When started without NOTIFY_SOCKET it is noop"),
{ok, _} = application:ensure_all_started(systemd),
ok = systemd:notify(ready),
ok = empty(Socket),
ok = stop(Config),
% -------------------------------------------------------------------------
ct:log("When started with invalid NOTIFY_SOCKET it is noop"),
ok = start_with_path("/non/existent"),
ok = systemd:notify(ready),
ok = stop(Config),
% -------------------------------------------------------------------------
ct:log("When started with empty NOTIFY_SOCKET it is noop"),
ok = start_with_path(""),
ok = systemd:notify(ready),
ok = stop(Config),
% -------------------------------------------------------------------------
ct:log("By default unset NOTIFY_SOCKET"),
ok = start_with_socket(Socket),
ok = systemd:notify(ready),
{ok, <<"READY=1\n">>} = recv(Socket),
ok = empty(Socket),
false = os:getenv("NOTIFY_SOCKET"),
ok = stop(Config),
% -------------------------------------------------------------------------
ct:log("Do not unset NOTIFY_SOCKET when unset_env is false"),
ok = application:set_env(systemd, unset_env, false),
ok = start_with_socket(Socket),
ok = systemd:notify(ready),
{ok, <<"READY=1\n">>} = recv(Socket),
ok = empty(Socket),
?assertEqual(?config(path, Config), os:getenv("NOTIFY_SOCKET")),
ok = stop(Config),
ok = application:set_env(systemd, unset_env, true),
ok.
fds(init, Config) ->
Socket = ?config(socket, Config),
ok = start_with_socket(Socket),
Config;
fds(_, Config) ->
Config.
fds(Config) ->
Socket = ?config(socket, Config),
ok = systemd:store_fds([1]),
?assertMatch({ok, #{iov := [<<"FDSTORE=1\n">>],
ctrl := [#{type := rights}]}}, recvmsg(Socket)),
ok = systemd:store_fds([{1, "foo"}]),
?assertMatch({ok, #{iov := [<<"FDSTORE=1\nFDNAMES=foo\n">>],
ctrl := [#{type := rights}]}}, recvmsg(Socket)),
ok = systemd:store_fds([1, {2, "foo"}]),
?assertMatch({ok, #{iov := [<<"FDSTORE=1\nFDNAMES=:foo\n">>],
ctrl := [#{type := rights}]}}, recvmsg(Socket)),
{error, bad_descriptor} = systemd:store_fds([999]),
ok = empty(Socket),
systemd:clear_fds(["foo", "bar"]),
{ok, <<"FDSTOREREMOVE=1\nFDNAMES=foo:bar\n">>} = recv(Socket),
ok.
start_with_socket(Socket) ->
{ok, #{path := Path}} = socket:sockname(Socket),
start_with_path(Path).
start_with_path(Path) ->
ct:log("Start app with socket at ~p", [Path]),
os:putenv("NOTIFY_SOCKET", Path),
{ok, _} = application:ensure_all_started(systemd),
ok.
stop(Config) ->
ok = application:stop(systemd),
ok = flush(?config(socket, Config)),
ok.
recv(S) ->
socket:recv(S, 0, 1000).
recvmsg(S) ->
socket:recvmsg(S, 0, 1000).
empty(S) ->
case socket:recv(S, 0, 0) of
{ok, <<>>} -> ok;
{error, timeout} -> ok;
{ok, Msg} -> {error, {received, Msg}};
{error, _} = Error -> Error
end.
flush(S) ->
case socket:recv(S, 0, 0) of
{ok, _} -> flush(S);
_ -> ok
end.