-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestsuite.py
executable file
·10328 lines (10303 loc) · 450 KB
/
testsuite.py
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env python
""" Testcases for docker-systemctl-replacement functionality """
from __future__ import print_function
__copyright__ = "(C) Guido Draheim, licensed under the EUPL"""
__version__ = "1.0.1472"
## NOTE:
## The testcases 1000...4999 are using a --root=subdir environment
## The testcases 5000...9999 will start a docker container to work.
import subprocess
import os.path
import time
import datetime
import unittest
import shutil
import inspect
import types
import logging
import re
from fnmatch import fnmatchcase as fnmatch
from glob import glob
import json
logg = logging.getLogger("TESTING")
_python = "/usr/bin/python"
_systemctl_py = "files/docker/systemctl.py"
_cov = ""
_cov_run = "coverage2 run '--omit=*/six.py' --append -- "
_cov_cmd = "coverage2"
_cov3run = "coverage3 run '--omit=*/six.py' --append -- "
_cov3cmd = "coverage3"
_python_coverage = "python-coverage"
_python3coverage = "python3-coverage"
COVERAGE = False
IMAGES = "localhost:5000/testingsystemctl"
CENTOS = "centos:7.3.1611"
UBUNTU = "ubuntu:14.04"
OPENSUSE = "opensuse:42.3"
DOCKER_SOCKET = "/var/run/docker.sock"
PSQL_TOOL = "/usr/bin/psql"
def sh____(cmd, shell=True):
if isinstance(cmd, basestring):
logg.info(": %s", cmd)
else:
logg.info(": %s", " ".join(["'%s'" % item for item in cmd]))
return subprocess.check_call(cmd, shell=shell)
def sx____(cmd, shell=True):
if isinstance(cmd, basestring):
logg.info(": %s", cmd)
else:
logg.info(": %s", " ".join(["'%s'" % item for item in cmd]))
return subprocess.call(cmd, shell=shell)
def output(cmd, shell=True):
if isinstance(cmd, basestring):
logg.info(": %s", cmd)
else:
logg.info(": %s", " ".join(["'%s'" % item for item in cmd]))
run = subprocess.Popen(cmd, shell=shell, stdout=subprocess.PIPE)
out, err = run.communicate()
return out
def output2(cmd, shell=True):
if isinstance(cmd, basestring):
logg.info(": %s", cmd)
else:
logg.info(": %s", " ".join(["'%s'" % item for item in cmd]))
run = subprocess.Popen(cmd, shell=shell, stdout=subprocess.PIPE)
out, err = run.communicate()
return out, run.returncode
def _lines(lines):
if isinstance(lines, basestring):
lines = lines.split("\n")
if len(lines) and lines[-1] == "":
lines = lines[:-1]
return lines
def lines(text):
lines = []
for line in _lines(text):
lines.append(line.rstrip())
return lines
def grep(pattern, lines):
for line in _lines(lines):
if re.search(pattern, line.rstrip()):
yield line.rstrip()
def greps(lines, pattern):
return list(grep(pattern, lines))
def download(base_url, filename, into):
if not os.path.isdir(into):
os.makedirs(into)
if not os.path.exists(os.path.join(into, filename)):
sh____("cd {into} && wget {base_url}/{filename}".format(**locals()))
def text_file(filename, content):
filedir = os.path.dirname(filename)
if not os.path.isdir(filedir):
os.makedirs(filedir)
f = open(filename, "w")
if content.startswith("\n"):
x = re.match("(?s)\n( *)", content)
indent = x.group(1)
for line in content[1:].split("\n"):
if line.startswith(indent):
line = line[len(indent):]
f.write(line+"\n")
else:
f.write(content)
f.close()
def shell_file(filename, content):
text_file(filename, content)
os.chmod(filename, 0770)
def copy_file(filename, target):
targetdir = os.path.dirname(target)
if not os.path.isdir(targetdir):
os.makedirs(targetdir)
shutil.copyfile(filename, target)
def copy_tool(filename, target):
copy_file(filename, target)
os.chmod(target, 0750)
def get_caller_name():
frame = inspect.currentframe().f_back.f_back
return frame.f_code.co_name
def get_caller_caller_name():
frame = inspect.currentframe().f_back.f_back.f_back
return frame.f_code.co_name
def os_path(root, path):
if not root:
return path
if not path:
return path
while path.startswith(os.path.sep):
path = path[1:]
return os.path.join(root, path)
class DockerSystemctlReplacementTest(unittest.TestCase):
def caller_testname(self):
name = get_caller_caller_name()
x1 = name.find("_")
if x1 < 0: return name
x2 = name.find("_", x1+1)
if x2 < 0: return name
return name[:x2]
def testname(self, suffix = None):
name = self.caller_testname()
if suffix:
return name + "_" + suffix
return name
def testport(self):
testname = self.caller_testname()
m = re.match("test_([0123456789]+)", testname)
if m:
port = int(m.group(1))
if 5000 <= port and port <= 9999:
return port
seconds = int(str(int(time.time()))[-4:])
return 6000 + (seconds % 2000)
def testdir(self, testname = None):
testname = testname or self.caller_testname()
newdir = "tmp/tmp."+testname
if os.path.isdir(newdir):
shutil.rmtree(newdir)
os.makedirs(newdir)
return newdir
def rm_testdir(self, testname = None):
testname = testname or self.caller_testname()
newdir = "tmp/tmp."+testname
if os.path.isdir(newdir):
shutil.rmtree(newdir)
return newdir
def coverage(self, testname = None):
testname = testname or self.caller_testname()
newcoverage = ".coverage."+testname
time.sleep(1) # TODO: flush output
if os.path.isfile(".coverage"):
# shutil.copy(".coverage", newcoverage)
f = open(".coverage")
text = f.read()
f.close()
text2 = re.sub(r"(\]\}\})[^{}]*(\]\}\})$", r"\1", text)
f = open(newcoverage, "w")
f.write(text2)
f.close()
def root(self, testdir):
root_folder = os.path.join(testdir, "root")
if not os.path.isdir(root_folder):
os.makedirs(root_folder)
return os.path.abspath(root_folder)
def user(self):
import getpass
getpass.getuser()
def ip_container(self, name):
values = output("docker inspect "+name)
values = json.loads(values)
if not values or "NetworkSettings" not in values[0]:
logg.critical(" docker inspect %s => %s ", name, values)
return values[0]["NetworkSettings"]["IPAddress"]
def with_local_centos_mirror(self, ver = None):
""" detects a local centos mirror or starts a local
docker container with a centos repo mirror. It
will return the setting for extrahosts"""
rmi = "localhost:5000"
rep = "centos-repo"
ver = ver or "7.3.1611"
find_repo_image = "docker images {rmi}/{rep}:{ver}"
images = output(find_repo_image.format(**locals()))
running = output("docker ps")
if greps(images, rep) and not greps(running, rep+ver):
cmd = "docker rm --force {rep}{ver}"
sx____(cmd.format(**locals()))
cmd = "docker run --detach --name {rep}{ver} {rmi}/{rep}:{ver}"
sh____(cmd.format(**locals()))
running = output("docker ps")
if greps(running, rep+ver):
ip_a = self.ip_container(rep+ver)
logg.info("%s%s => %s", rep, ver, ip_a)
result = "mirrorlist.centos.org:%s" % ip_a
logg.info("--add-host %s", result)
return result
return ""
def with_local_opensuse_mirror(self, ver = None):
""" detects a local opensuse mirror or starts a local
docker container with a centos repo mirror. It
will return the extra_hosts setting to start
other docker containers"""
rmi = "localhost:5000"
rep = "opensuse-repo"
ver = ver or "42.2"
find_repo_image = "docker images {rmi}/{rep}:{ver}"
images = output(find_repo_image.format(**locals()))
running = output("docker ps")
if greps(images, rep) and not greps(running, rep+ver):
cmd = "docker rm --force {rep}{ver}"
sx____(cmd.format(**locals()))
cmd = "docker run --detach --name {rep}{ver} {rmi}/{rep}:{ver}"
sh____(cmd.format(**locals()))
running = output("docker ps")
if greps(running, rep+ver):
ip_a = self.ip_container(rep+ver)
logg.info("%s%s => %s", rep, ver, ip_a)
result = "download.opensuse.org:%s" % ip_a
logg.info("--add-host %s", result)
return result
return ""
def local_image(self, image):
if image.startswith("centos:"):
version = image[len("centos:"):]
add_hosts = self.with_local_centos_mirror(version)
if add_hosts:
return "--add-host '{add_hosts}' {image}".format(**locals())
if image.startswith("opensuse:"):
version = image[len("opensuse:"):]
add_hosts = self.with_local_opensuse_mirror(version)
if add_hosts:
return "--add-host '{add_hosts}' {image}".format(**locals())
return image
def drop_container(self, name):
cmd = "docker rm --force {name}"
sx____(cmd.format(**locals()))
def drop_centos(self):
self.drop_container("centos")
def drop_ubuntu(self):
self.drop_container("ubuntu")
def drop_opensuse(self):
self.drop_container("opensuse")
def make_opensuse(self):
self.make_container("opensuse", OPENSUSE)
def make_ubuntu(self):
self.make_container("ubuntu", UBUNTU)
def make_centos(self):
self.make_container("centos", CENTOS)
def make_container(self, name, image):
self.drop_container(name)
local_image = self.local_image(image)
cmd = "docker run --detach --name {name} {local_image} sleep 1000"
sh____(cmd.format(**locals()))
print(" # " + local_image)
print(" docker exec -it "+name+" bash")
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
def test_1000(self):
self.with_local_centos_mirror()
def test_1001_systemctl_testfile(self):
""" the systemctl.py file to be tested does exist """
testname = self.testname()
testdir = self.testdir()
root = self.root(testdir)
logg.info("...")
logg.info("testname %s", testname)
logg.info(" testdir %s", testdir)
logg.info("and root %s", root)
target = "/usr/bin/systemctl"
target_folder = os_path(root, os.path.dirname(target))
os.makedirs(target_folder)
target_systemctl = os_path(root, target)
shutil.copy(_systemctl_py, target_systemctl)
self.assertTrue(os.path.isfile(target_systemctl))
self.rm_testdir()
self.coverage()
def test_1002_systemctl_version(self):
systemctl = _cov + _systemctl_py
cmd = "{systemctl} --version"
out, end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)
self.assertEqual(end, 0)
self.assertTrue(greps(out, "systemd 0"))
self.assertTrue(greps(out, "[(]systemctl.py"))
self.assertTrue(greps(out, "[+]SYSVINIT"))
self.coverage()
def test_1003_systemctl_help(self):
""" the '--help' option and 'help' command do work """
systemctl = _cov + _systemctl_py
cmd = "{systemctl} --help"
out, end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)
self.assertEqual(end, 0)
self.assertTrue(greps(out, "--root=PATH"))
self.assertTrue(greps(out, "--verbose"))
self.assertTrue(greps(out, "--init"))
self.assertTrue(greps(out, "for more information"))
self.assertFalse(greps(out, "reload-or-try-restart"))
cmd = "{systemctl} help"
out, end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)
self.assertEqual(end, 0)
self.assertFalse(greps(out, "--verbose"))
self.assertTrue(greps(out, "reload-or-try-restart"))
self.coverage()
def test_1005_systemctl_help_command(self):
""" for any command, 'help command' shows the documentation """
systemctl = _cov + _systemctl_py
cmd = "{systemctl} help list-unit-files"
out, end = output2(cmd.format(**locals()))
logg.info("%s\n%s", cmd, out)
self.assertEqual(end, 0)
self.assertFalse(greps(out, "for more information"))
self.assertTrue(greps(out, "--type=service"))
self.coverage()
def test_1006_systemctl_help_command_other(self):
""" for a non-existant command, 'help command' just shows the list """
systemctl = _cov + _systemctl_py
cmd = "{systemctl} help list-foo"
out, end = output2(cmd.format(**locals()))
logg.info("%s\n%s", cmd, out)
self.assertEqual(end, 1)
self.assertFalse(greps(out, "for more information"))
self.assertTrue(greps(out, "reload-or-try-restart"))
self.coverage()
def test_1010_systemctl_daemon_reload(self):
""" daemon-reload always succeeds (does nothing) """
systemctl = _cov + _systemctl_py
cmd = "{systemctl} daemon-reload"
out, end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)
self.assertEqual(lines(out), [])
self.assertEqual(end, 0)
self.coverage()
def test_1011_systemctl_daemon_reload_root_ignored(self):
""" daemon-reload always succeeds (does nothing) """
testdir = self.testdir()
root = self.root(testdir)
systemctl = _cov + _systemctl_py + " --root=" + root
text_file(os_path(root, "/etc/systemd/system/a.service"),"""
[Unit]
Description=Testing A
[Service]
ExecStart=/usr/bin/sleep 3
""")
cmd = "{systemctl} daemon-reload"
out,end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)
self.assertEqual(lines(out), [])
self.assertEqual(end, 0)
self.rm_testdir()
self.coverage()
def test_1020_systemctl_with_systemctl_log(self):
""" when /var/log/systemctl.log exists then print INFO messages into it"""
testdir = self.testdir()
root = self.root(testdir)
systemctl = _cov + _systemctl_py + " --root=" + root
logfile = os_path(root, "/var/log/systemctl.log")
text_file(logfile,"")
#
cmd = "{systemctl} daemon-reload"
out, end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)
self.assertEqual(end, 0)
self.assertEqual(len(greps(open(logfile), " INFO ")), 2)
self.assertEqual(len(greps(open(logfile), " DEBUG ")), 0)
self.rm_testdir()
self.coverage()
def test_1021_systemctl_with_systemctl_debug_log(self):
""" when /var/log/systemctl.debug.log exists then print DEBUG messages into it"""
testdir = self.testdir()
root = self.root(testdir)
systemctl = _cov + _systemctl_py + " --root=" + root
logfile = os_path(root, "/var/log/systemctl.debug.log")
text_file(logfile,"")
#
cmd = "{systemctl} daemon-reload"
out, end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)
self.assertEqual(end, 0)
self.assertEqual(len(greps(open(logfile), " INFO ")), 2)
self.assertEqual(len(greps(open(logfile), " DEBUG ")), 3)
self.rm_testdir()
self.coverage()
def test_1030_systemctl_force_ipv4(self):
""" we can force --ipv4 for /etc/hosts """
testdir = self.testdir()
root = self.root(testdir)
systemctl = _cov + _systemctl_py + " --root=" + root
text_file(os_path(root, "/etc/hosts"),"""
127.0.0.1 localhost localhost4
::1 localhost localhost6""")
hosts = open(os_path(root, "/etc/hosts")).read()
self.assertEqual(len(lines(hosts)), 2)
self.assertTrue(greps(hosts, "127.0.0.1.*localhost4"))
self.assertTrue(greps(hosts, "::1.*localhost6"))
self.assertTrue(greps(hosts, "127.0.0.1.*localhost "))
self.assertTrue(greps(hosts, "::1.*localhost "))
#
cmd = "{systemctl} --ipv4 daemon-reload"
out, end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)
self.assertEqual(lines(out), [])
self.assertEqual(end, 0)
hosts = open(os_path(root, "/etc/hosts")).read()
self.assertEqual(len(lines(hosts)), 2)
self.assertTrue(greps(hosts, "127.0.0.1.*localhost4"))
self.assertTrue(greps(hosts, "::1.*localhost6"))
self.assertTrue(greps(hosts, "127.0.0.1.*localhost "))
self.assertFalse(greps(hosts, "::1.*localhost "))
self.rm_testdir()
self.coverage()
def test_1031_systemctl_force_ipv6(self):
""" we can force --ipv6 for /etc/hosts """
testdir = self.testdir()
root = self.root(testdir)
systemctl = _cov + _systemctl_py + " --root=" + root
text_file(os_path(root, "/etc/hosts"),"""
127.0.0.1 localhost localhost4
::1 localhost localhost6""")
hosts = open(os_path(root, "/etc/hosts")).read()
self.assertEqual(len(lines(hosts)), 2)
self.assertTrue(greps(hosts, "127.0.0.1.*localhost4"))
self.assertTrue(greps(hosts, "::1.*localhost6"))
self.assertTrue(greps(hosts, "127.0.0.1.*localhost "))
self.assertTrue(greps(hosts, "::1.*localhost "))
#
cmd = "{systemctl} --ipv6 daemon-reload"
out, end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)
self.assertEqual(lines(out), [])
self.assertEqual(end, 0)
hosts = open(os_path(root, "/etc/hosts")).read()
self.assertEqual(len(lines(hosts)), 2)
self.assertTrue(greps(hosts, "127.0.0.1.*localhost4"))
self.assertTrue(greps(hosts, "::1.*localhost6"))
self.assertFalse(greps(hosts, "127.0.0.1.*localhost "))
self.assertTrue(greps(hosts, "::1.*localhost "))
self.rm_testdir()
self.coverage()
def test_1050_can_create_a_test_service(self):
""" check that a unit file can be created for testing """
testname = self.testname()
testdir = self.testdir()
root = self.root(testdir)
systemctl = _cov + _systemctl_py + " --root=" + root
text_file(os_path(root, "/etc/systemd/system/a.service"),"""
[Unit]
Description=Testing A""")
textA = file(os_path(root, "/etc/systemd/system/a.service")).read()
self.assertTrue(greps(textA, "Testing A"))
self.assertIn("\nDescription", textA)
self.rm_testdir()
self.coverage()
def test_1051_can_parse_the_service_file(self):
""" check that a unit file can be parsed atleast for a description """
testname = self.testname()
testdir = self.testdir()
root = self.root(testdir)
systemctl = _cov + _systemctl_py + " --root=" + root
text_file(os_path(root, "/etc/systemd/system/a.service"),"""
[Unit]
Description=Testing A""")
textA = file(os_path(root, "/etc/systemd/system/a.service")).read()
self.assertTrue(greps(textA, "Testing A"))
cmd = "{systemctl} __get_description a.service"
out, end = output2(cmd.format(**locals()))
logg.info("%s => \n%s", cmd, out)
self.assertEqual(end, 0)
self.assertTrue(greps(out, "Testing A"))
self.rm_testdir()
self.coverage()
def test_1052_can_describe_a_pid_file(self):
""" check that a unit file can have a specific pdi file """
testname = self.testname()
testdir = self.testdir()
root = self.root(testdir)
systemctl = _cov + _systemctl_py + " --root=" + root
text_file(os_path(root, "/etc/systemd/system/a.service"),"""
[Unit]
Description=Testing A
[Service]
PIDFile=/var/run/foo.pid
""")
textA = file(os_path(root, "/etc/systemd/system/a.service")).read()
self.assertTrue(greps(textA, "Testing A"))
self.assertTrue(greps(textA, "PIDFile="))
cmd = "{systemctl} __get_pid_file a.service"
out, end = output2(cmd.format(**locals()))
logg.info("%s => \n%s", cmd, out)
self.assertEqual(end, 0)
self.assertTrue(greps(out, "/var/run/foo.pid"))
self.rm_testdir()
self.coverage()
def test_1053_can_have_default_pid_file_for_simple_service(self):
""" check that a unit file has a default pid file for simple services """
testname = self.testname()
testdir = self.testdir()
root = self.root(testdir)
systemctl = _cov + _systemctl_py + " --root=" + root
text_file(os_path(root, "/etc/systemd/system/a.service"),"""
[Unit]
Description=Testing A
[Service]
Type=simple
""")
textA = file(os_path(root, "/etc/systemd/system/a.service")).read()
self.assertTrue(greps(textA, "Testing A"))
self.assertFalse(greps(textA, "PIDFile="))
cmd = "{systemctl} __get_pid_file a.service"
out, end = output2(cmd.format(**locals()))
logg.info("%s => \n%s", cmd, out)
self.assertEqual(end, 0)
self.assertTrue(greps(out, "/var/run/a.service.pid"))
self.rm_testdir()
self.coverage()
def test_1055_other_services_use_a_status_file(self):
""" check that other unit files may have a default status file """
testname = self.testname()
testdir = self.testdir()
root = self.root(testdir)
systemctl = _cov + _systemctl_py + " --root=" + root
text_file(os_path(root, "/etc/systemd/system/a.service"),"""
[Unit]
Description=Testing A
[Service]
Type=oneshot
""")
textA = file(os_path(root, "/etc/systemd/system/a.service")).read()
self.assertTrue(greps(textA, "Testing A"))
self.assertFalse(greps(textA, "PIDFile="))
cmd = "{systemctl} __get_status_file a.service"
out, end = output2(cmd.format(**locals()))
logg.info("%s => \n%s", cmd, out)
self.assertEqual(end, 0)
self.assertTrue(greps(out, "/var/run/a.service.status"))
self.rm_testdir()
self.coverage()
def test_1060_can_have_shell_like_commments(self):
""" check that a unit file can have comment lines with '#' """
testname = self.testname()
testdir = self.testdir()
root = self.root(testdir)
systemctl = _cov + _systemctl_py + " --root=" + root
text_file(os_path(root, "/etc/systemd/system/a.service"),"""
[Unit]
Description=Testing A
[Service]
#PIDFile=/var/run/foo.pid
""")
textA = file(os_path(root, "/etc/systemd/system/a.service")).read()
self.assertTrue(greps(textA, "Testing A"))
self.assertTrue(greps(textA, "PIDFile="))
cmd = "{systemctl} __get_pid_file a.service"
out, end = output2(cmd.format(**locals()))
logg.info("%s => \n%s", cmd, out)
self.assertEqual(end, 0)
self.assertFalse(greps(out, "/var/run/foo.pid"))
self.assertTrue(greps(out, "/var/run/a.service.pid"))
self.rm_testdir()
self.coverage()
def test_1061_can_have_winini_like_commments(self):
""" check that a unit file can have comment lines with ';' """
testname = self.testname()
testdir = self.testdir()
root = self.root(testdir)
systemctl = _cov + _systemctl_py + " --root=" + root
text_file(os_path(root, "/etc/systemd/system/a.service"),"""
[Unit]
Description=Testing A
[Service]
;PIDFile=/var/run/foo.pid
""")
textA = file(os_path(root, "/etc/systemd/system/a.service")).read()
self.assertTrue(greps(textA, "Testing A"))
self.assertTrue(greps(textA, "PIDFile="))
cmd = "{systemctl} __get_pid_file a.service"
out, end = output2(cmd.format(**locals()))
logg.info("%s => \n%s", cmd, out)
self.assertEqual(end, 0)
self.assertFalse(greps(out, "/var/run/foo.pid"))
self.assertTrue(greps(out, "/var/run/a.service.pid"))
self.rm_testdir()
self.coverage()
def test_1062_can_have_multi_line_settings_with_linebreak_mark(self):
""" check that a unit file can have settings with '\\' at the line end """
testname = self.testname()
testdir = self.testdir()
root = self.root(testdir)
systemctl = _cov + _systemctl_py + " --root=" + root
text_file(os_path(root, "/etc/systemd/system/a.service"),"""
[Unit]
Description=Testing A \
which is quite special
[Service]
PIDFile=/var/run/foo.pid
""")
textA = file(os_path(root, "/etc/systemd/system/a.service")).read()
self.assertTrue(greps(textA, "Testing A"))
self.assertTrue(greps(textA, "quite special"))
self.assertTrue(greps(textA, "PIDFile="))
cmd = "{systemctl} __get_description a.service"
out, end = output2(cmd.format(**locals()))
logg.info("%s => \n%s", cmd, out)
self.assertEqual(end, 0)
self.assertTrue(greps(out, "Testing A"))
self.assertTrue(greps(out, "quite special"))
self.rm_testdir()
self.coverage()
def test_1063_but_a_missing_linebreak_is_a_syntax_error(self):
""" check that a unit file can have 'bad ini' lines throwing an exception """
# the original systemd daemon would ignore services with syntax errors
testname = self.testname()
testdir = self.testdir()
root = self.root(testdir)
systemctl = _cov + _systemctl_py + " --root=" + root
text_file(os_path(root, "/etc/systemd/system/a.service"),"""
[Unit]
Description=Testing A
which is quite special
[Service]
PIDFile=/var/run/foo.pid
""")
textA = file(os_path(root, "/etc/systemd/system/a.service")).read()
self.assertTrue(greps(textA, "Testing A"))
self.assertTrue(greps(textA, "quite special"))
self.assertTrue(greps(textA, "PIDFile="))
cmd = "{systemctl} __get_description a.service"
out, end = output2(cmd.format(**locals()))
logg.info("%s => \n%s", cmd, out)
self.assertEqual(end, 0)
self.assertFalse(greps(out, "Testing A"))
self.assertFalse(greps(out, "quite special"))
self.rm_testdir()
self.coverage()
def test_1070_external_env_files_can_be_parsed(self):
""" check that a unit file can have a valid EnvironmentFile for settings """
testname = self.testname()
testdir = self.testdir()
root = self.root(testdir)
systemctl = _cov + _systemctl_py + " --root=" + root
text_file(os_path(root, "/etc/systemd/system/a.service"),"""
[Unit]
Description=Testing A
which is quite special
[Service]
EnvironmentFile=/etc/sysconfig/a.conf
""")
text_file(os_path(root, "/etc/sysconfig/a.conf"),"""
CONF1=a1
CONF2="b2"
CONF3='c3'
#CONF4=b4
""")
cmd = "{systemctl} __read_env_file /etc/sysconfig/a.conf -vv"
out, end = output2(cmd.format(**locals()))
logg.info("%s => \n%s", cmd, out)
self.assertEqual(end, 0)
self.assertTrue(greps(out, "CONF1"))
self.assertTrue(greps(out, "CONF2"))
self.assertTrue(greps(out, "CONF3"))
self.assertFalse(greps(out, "CONF4"))
self.assertTrue(greps(out, "a1"))
self.assertTrue(greps(out, "b2"))
self.assertTrue(greps(out, "c3"))
self.assertFalse(greps(out, '"b2"'))
self.assertFalse(greps(out, "'c3'"))
self.rm_testdir()
self.coverage()
def test_1080_preset_files_can_be_parsed(self):
""" check that preset files do work internally"""
testname = self.testname()
testdir = self.testdir()
root = self.root(testdir)
systemctl = _cov + _systemctl_py + " --root=" + root
text_file(os_path(root, "/etc/systemd/system/a.service"),"""
[Unit]
Description=Testing A""")
text_file(os_path(root, "/etc/systemd/system/b.service"),"""
[Unit]
Description=Testing B
[Install]
WantedBy=multi-user.target""")
text_file(os_path(root, "/etc/systemd/system/c.service"),"""
[Unit]
Description=Testing C
[Install]
WantedBy=multi-user.target""")
text_file(os_path(root, "/etc/systemd/system-preset/our.preset"),"""
enable b.service
disable c.service""")
#
cmd = "{systemctl} __load_preset_files"
out, end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)
self.assertEqual(end, 0)
self.assertTrue(greps(out, r"^our.preset"))
self.assertEqual(len(lines(out)), 1)
#
cmd = "{systemctl} __get_preset_of_unit a.service"
out, end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)
self.assertEqual(end, 0)
# self.assertTrue(greps(out, r"^our.preset"))
self.assertEqual(len(lines(out)), 0)
#
cmd = "{systemctl} __get_preset_of_unit b.service"
out, end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)
self.assertEqual(end, 0)
self.assertTrue(greps(out, r"^enable"))
self.assertEqual(len(lines(out)), 1)
#
cmd = "{systemctl} __get_preset_of_unit c.service"
out, end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)
self.assertEqual(end, 0)
self.assertTrue(greps(out, r"^disable"))
self.assertEqual(len(lines(out)), 1)
def test_1090_syntax_errors_are_shown_on_daemon_reload(self):
""" check that preset files do work internally"""
testname = self.testname()
testdir = self.testdir()
root = self.root(testdir)
systemctl = _cov + _systemctl_py + " --root=" + root
text_file(os_path(root, "/etc/systemd/system/a.service"),"""
[Unit]
Description=Testing A""")
text_file(os_path(root, "/etc/systemd/system/b.service"),"""
[Unit]
Description=Testing B
[Service]
Type=foo
ExecStart=runA
ExecReload=runB
ExecStop=runC
[Install]
WantedBy=multi-user.target""")
text_file(os_path(root, "/etc/systemd/system/c.service"),"""
[Unit]
Description=Testing C
[Service]
type=simple
ExecReload=/usr/bin/kill -SIGHUP $MAINPID
ExecStop=/usr/bin/kill $MAINPID
[Install]
WantedBy=multi-user.target""")
text_file(os_path(root, "/etc/systemd/system/d.service"),"""
[Unit]
Description=Testing D
[Service]
type=forking
[Install]
WantedBy=multi-user.target""")
text_file(os_path(root, "/etc/systemd/system/g.service"),"""
[Unit]
Description=Testing G
[Service]
Type=foo
ExecStart=runA
ExecStart=runA2
ExecReload=runB
ExecReload=runB2
ExecStop=runC
ExecStop=runC2
[Install]
WantedBy=multi-user.target""")
#
cmd = "{systemctl} daemon-reload -vv 2>&1"
out, end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)
self.assertEqual(end, 0)
self.assertTrue(greps(out, r"a.service:.* file without .Service. section"))
self.assertTrue(greps(out, r"b.service:.* Executable path is not absolute"))
self.assertTrue(greps(out, r"c.service: Service has no ExecStart"))
self.assertTrue(greps(out, r"d.service: Service lacks both ExecStart and ExecStop"))
self.assertTrue(greps(out, r"g.service: there may be only one ExecStart statement"))
self.assertTrue(greps(out, r"g.service: there may be only one ExecStop statement"))
self.assertTrue(greps(out, r"g.service: there may be only one ExecReload statement"))
self.assertTrue(greps(out, r"c.service: the use of /bin/kill is not recommended"))
def test_2001_can_create_test_services(self):
""" check that two unit files can be created for testing """
testname = self.testname()
testdir = self.testdir()
root = self.root(testdir)
systemctl = _cov + _systemctl_py + " --root=" + root
text_file(os_path(root, "/etc/systemd/system/a.service"),"""
[Unit]
Description=Testing A""")
text_file(os_path(root, "/etc/systemd/system/b.service"),"""
[Unit]
Description=Testing B""")
textA = file(os_path(root, "/etc/systemd/system/a.service")).read()
textB = file(os_path(root, "/etc/systemd/system/b.service")).read()
self.assertTrue(greps(textA, "Testing A"))
self.assertTrue(greps(textB, "Testing B"))
self.assertIn("\nDescription", textA)
self.assertIn("\nDescription", textB)
self.rm_testdir()
self.coverage()
def test_2002_list_units(self):
""" check that two unit files can be found for 'list-units' """
testname = self.testname()
testdir = self.testdir()
root = self.root(testdir)
systemctl = _cov + _systemctl_py + " --root=" + root
text_file(os_path(root, "/etc/systemd/system/a.service"),"""
[Unit]
Description=Testing A""")
text_file(os_path(root, "/etc/systemd/system/b.service"),"""
[Unit]
Description=Testing B""")
cmd = "{systemctl} list-units"
out, end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)
self.assertEqual(end, 0)
self.assertTrue(greps(out, r"a.service\s+loaded inactive dead\s+.*Testing A"))
self.assertTrue(greps(out, r"b.service\s+loaded inactive dead\s+.*Testing B"))
self.assertIn("loaded units listed.", out)
self.assertIn("To show all installed unit files use", out)
self.assertEqual(len(lines(out)), 5)
cmd = "{systemctl} --no-legend list-units"
out, end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)
self.assertEqual(end, 0)
self.assertTrue(greps(out, r"a.service\s+loaded inactive dead\s+.*Testing A"))
self.assertTrue(greps(out, r"b.service\s+loaded inactive dead\s+.*Testing B"))
self.assertNotIn("loaded units listed.", out)
self.assertNotIn("To show all installed unit files use", out)
self.assertEqual(len(lines(out)), 2)
self.rm_testdir()
self.coverage()
def test_2003_list_unit_files(self):
""" check that two unit service files can be found for 'list-unit-files' """
testname = self.testname()
testdir = self.testdir()
root = self.root(testdir)
systemctl = _cov + _systemctl_py + " --root=" + root
text_file(os_path(root, "/etc/systemd/system/a.service"),"""
[Unit]
Description=Testing A""")
text_file(os_path(root, "/etc/systemd/system/b.service"),"""
[Unit]
Description=Testing B""")
cmd = "{systemctl} --type=service list-unit-files"
out, end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)
self.assertEqual(end, 0)
self.assertTrue(greps(out, r"a.service\s+static"))
self.assertTrue(greps(out, r"b.service\s+static"))
self.assertIn("unit files listed.", out)
self.assertEqual(len(lines(out)), 5)
cmd = "{systemctl} --no-legend --type=service list-unit-files"
out, end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)
self.assertEqual(end, 0)
self.assertTrue(greps(out, r"a.service\s+static"))
self.assertTrue(greps(out, r"b.service\s+static"))
self.assertNotIn("unit files listed.", out)
self.assertEqual(len(lines(out)), 2)
self.rm_testdir()
self.coverage()
def test_2004_list_unit_files_wanted(self):
""" check that two unit files can be found for 'list-unit-files'
with an enabled status """
testname = self.testname()
testdir = self.testdir()
root = self.root(testdir)
systemctl = _cov + _systemctl_py + " --root=" + root
text_file(os_path(root, "/etc/systemd/system/a.service"),"""
[Unit]
Description=Testing A""")
text_file(os_path(root, "/etc/systemd/system/b.service"),"""
[Unit]
Description=Testing B
[Install]
WantedBy=multi-user.target""")
cmd = "{systemctl} --type=service list-unit-files"
out, end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)
self.assertEqual(end, 0)
self.assertTrue(greps(out, r"a.service\s+static"))
self.assertTrue(greps(out, r"b.service\s+disabled"))
self.assertIn("unit files listed.", out)
self.assertEqual(len(lines(out)), 5)
cmd = "{systemctl} --no-legend --type=service list-unit-files"
out, end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)
self.assertEqual(end, 0)
self.assertTrue(greps(out, r"a.service\s+static"))
self.assertTrue(greps(out, r"b.service\s+disabled"))
self.assertNotIn("unit files listed.", out)
self.assertEqual(len(lines(out)), 2)
self.rm_testdir()
self.coverage()
def test_2006_list_unit_files_wanted_and_unknown_type(self):
""" check that two unit files can be found for 'list-unit-files'
with an enabled status plus handling unkonwn services"""
testname = self.testname()
testdir = self.testdir()
root = self.root(testdir)
systemctl = _cov + _systemctl_py + " --root=" + root
text_file(os_path(root, "/etc/systemd/system/a.service"),"""
[Unit]
Description=Testing A""")
text_file(os_path(root, "/etc/systemd/system/b.service"),"""
[Unit]
Description=Testing B
[Install]
WantedBy=multi-user.target""")
cmd = "{systemctl} --type=foo list-unit-files"
out, end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)
self.assertEqual(end, 0)
self.assertIn("0 unit files listed.", out)
self.assertEqual(len(lines(out)), 3)
self.rm_testdir()
self.coverage()
def test_2008_list_unit_files_locations(self):
""" check that unit files can be found for 'list-unit-files'
in different standard locations on disk. """
testname = self.testname()
testdir = self.testdir()
root = self.root(testdir)
systemctl = _cov + _systemctl_py + " --root=" + root
text_file(os_path(root, "/etc/systemd/system/a.service"),"""
[Unit]
Description=Testing A
[Install]
WantedBy=multi-user.target""")
text_file(os_path(root, "/usr/lib/systemd/system/b.service"),"""
[Unit]
Description=Testing B
[Install]
WantedBy=multi-user.target""")
text_file(os_path(root, "/lib/systemd/system/c.service"),"""
[Unit]
Description=Testing C
[Install]
WantedBy=multi-user.target""")
text_file(os_path(root, "/var/run/systemd/system/d.service"),"""
[Unit]
Description=Testing D
[Install]
WantedBy=multi-user.target""")
cmd = "{systemctl} --type=service list-unit-files"
out, end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)
self.assertEqual(end, 0)
self.assertTrue(greps(out, r"a.service\s+disabled"))
self.assertTrue(greps(out, r"b.service\s+disabled"))
self.assertTrue(greps(out, r"c.service\s+disabled"))
self.assertTrue(greps(out, r"d.service\s+disabled"))
self.assertIn("4 unit files listed.", out)
self.assertEqual(len(lines(out)), 7)
#
cmd = "{systemctl} enable a.service"
out, end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)
self.assertEqual(end, 0)
cmd = "{systemctl} enable b.service"
out, end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)
self.assertEqual(end, 0)
cmd = "{systemctl} enable c.service"
out, end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)
self.assertEqual(end, 0)
cmd = "{systemctl} enable d.service"
out, end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)