-
Notifications
You must be signed in to change notification settings - Fork 28
/
testbuilds.py
executable file
·3549 lines (3532 loc) · 162 KB
/
testbuilds.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 python3
""" Testcases for docker-systemctl-replacement functionality """
from __future__ import print_function
__copyright__ = "(C) Guido Draheim, licensed under the EUPL"""
__version__ = "1.5.8065"
# 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 string
import random
import logging
import re
from fnmatch import fnmatchcase as fnmatch
from glob import glob
import json
import sys
if sys.version[0] == '3':
basestring = str
xrange = range
logg = logging.getLogger("TESTING")
_epel7 = False
_opensuse14 = False
_python2 = "/usr/bin/python"
_python3 = "/usr/bin/python3"
_python = ""
_systemctl_py = "files/docker/systemctl3.py"
_top_recent = "ps -eo etime,pid,ppid,args --sort etime,pid | grep '^ *0[0123]:[^ :]* ' | grep -v -e ' ps ' -e ' grep ' -e 'kworker/'"
_top_list = "ps -eo etime,pid,ppid,args --sort etime,pid"
SAVETO = "localhost:5000/systemctl"
IMAGES = "localhost:5000/systemctl/image"
CENTOS7 = "centos:7.7.1908"
CENTOS = "almalinux:9.1"
UBUNTU = "ubuntu:22.04"
OPENSUSE = "opensuse/leap:15.5"
_curl = "curl"
_curl_timeout4 = "--max-time 4"
_docker = "docker"
DOCKER_SOCKET = "/var/run/docker.sock"
PSQL_TOOL = "/usr/bin/psql"
PLAYBOOK_TOOL = "/usr/bin/ansible-playbook"
RUNTIME = "/tmp/run-"
_maindir = os.path.dirname(sys.argv[0])
_mirror = os.path.join(_maindir, "docker_mirror.py")
_password = ""
def decodes(text):
if text is None: return None
if isinstance(text, bytes):
encoded = sys.getdefaultencoding()
if encoded in ["ascii"]:
encoded = "utf-8"
try:
return text.decode(encoded)
except:
return text.decode("latin-1")
return text
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 decodes(out), run.returncode
def output3(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, stderr=subprocess.PIPE)
out, err = run.communicate()
return decodes(out), decodes(err), run.returncode
def background(cmd, shell=True):
BackgroundProcess = collections.namedtuple("BackgroundProcess", ["pid", "run", "log"])
log = open(os.devnull, "wb")
run = subprocess.Popen(cmd, shell=shell, stdout=log, stderr=log)
pid = run.pid
logg.info("PID %s = %s", pid, cmd)
return BackgroundProcess(pid, run, log)
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)):
curl = _curl
sh____("cd {into} && {curl} -O {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, 0o770)
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, 0o750)
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)
def docname(path):
return os.path.splitext(os.path.basename(path))[0]
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 makedirs(self, path):
if not os.path.isdir(path):
os.makedirs(path)
def real_folders(self):
yield "/etc/systemd/system"
yield "/var/run/systemd/system"
yield "/usr/lib/systemd/system"
yield "/lib/systemd/system"
yield "/etc/init.d"
yield "/var/run/init.d"
yield "/var/run"
yield "/etc/sysconfig"
yield "/etc/systemd/system/multi-user.target.wants"
yield "/usr/bin"
def rm_zzfiles(self, root):
for folder in self.real_folders():
for item in glob(os_path(root, folder + "/zz*")):
logg.info("rm %s", item)
os.remove(item)
for item in glob(os_path(root, folder + "/test_*")):
logg.info("rm %s", item)
os.remove(item)
def root(self, testdir, real=None):
if real: return "/"
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 newpassword(self):
if _password:
return _password
out = "Password."
out += random.choice(string.ascii_uppercase)
out += random.choice(string.ascii_lowercase)
out += random.choice(string.ascii_lowercase)
out += random.choice(string.ascii_lowercase)
out += random.choice(string.ascii_lowercase)
out += random.choice(",.-+")
out += random.choice("0123456789")
out += random.choice("0123456789")
return out
def user(self):
import getpass
getpass.getuser()
def ip_container(self, name):
docker = _docker
cmd = "{docker} inspect {name}"
values = output(cmd.format(**locals()))
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 local_image(self, image):
""" attach local centos-repo / opensuse-repo to docker-start enviroment.
Effectivly when it is required to 'docker start centos:x.y' then do
'docker start centos-repo:x.y' before and extend the original to
'docker start --add-host mirror...:centos-repo centos:x.y'. """
if os.environ.get("NONLOCAL", ""):
return image
add_hosts = self.start_mirror(image)
if add_hosts:
return "{add_hosts} {image}".format(**locals())
return image
def local_addhosts(self, dockerfile, extras=None):
image = ""
for line in open(dockerfile):
m = re.match('[Ff][Rr][Oo][Mm] *"([^"]*)"', line)
if m:
image = m.group(1)
break
m = re.match("[Ff][Rr][Oo][Mm] *(\w[^ ]*)", line)
if m:
image = m.group(1).strip()
break
logg.debug("--\n-- '%s' FROM '%s'", dockerfile, image)
if image:
return self.start_mirror(image, extras)
return ""
def start_mirror(self, image, extras=None):
extras = extras or ""
docker = _docker
mirror = _mirror
cmd = "{mirror} start {image} --add-hosts {extras}"
out = output(cmd.format(**locals()))
return decodes(out).strip()
def drop_container(self, name):
docker = _docker
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)
docker = _docker
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".format(**locals()))
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
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()
def test_1002_systemctl_version(self):
systemctl = _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 219"))
self.assertTrue(greps(out, "via systemctl.py"))
self.assertTrue(greps(out, "[+]SYSVINIT"))
def real_1002_systemctl_version(self):
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, r"systemd [234]\d\d"))
self.assertFalse(greps(out, "via systemctl.py"))
self.assertTrue(greps(out, "[+]SYSVINIT"))
def test_1003_systemctl_help(self):
""" the '--help' option and 'help' command do work """
systemctl = _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"))
def test_2007_centos7_httpd_dockerfile(self):
""" WHEN using a dockerfile for systemd-enabled CentOS 7 and python2,
THEN we can create an image with an Apache HTTP service
being installed and enabled.
Without a special startup.sh script or container-cmd
one can just start the image and in the container
expecting that the service is started. Therefore,
WHEN we start the image as a docker container
THEN we can download the root html showing 'OK'
because the test script has placed an index.html
in the webserver containing that text. """
if not os.path.exists(DOCKER_SOCKET): self.skipTest("docker-based test")
docker = _docker
curl = _curl
testname = self.testname()
testdir = self.testdir()
name = "centos7-httpd"
dockerfile = "centos7-httpd.dockerfile"
addhosts = self.local_addhosts(dockerfile)
savename = docname(dockerfile)
saveto = SAVETO
images = IMAGES
# WHEN
cmd = "{docker} build . -f {dockerfile} {addhosts} --tag {images}:{testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rm --force {testname}"
sx____(cmd.format(**locals()))
cmd = "{docker} run -d --name {testname} {images}:{testname}"
sh____(cmd.format(**locals()))
container = self.ip_container(testname)
# THEN
cmd = "sleep 5; {curl} -o {testdir}/{testname}.txt http://{container}"
sh____(cmd.format(**locals()))
cmd = "grep OK {testdir}/{testname}.txt"
sh____(cmd.format(**locals()))
#cmd = "{docker} cp {testname}:/var/log/systemctl.log {testdir}/systemctl.log"
# sh____(cmd.format(**locals()))
# SAVE
cmd = "{docker} stop {testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rm --force {testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rmi {saveto}/{savename}:latest"
sx____(cmd.format(**locals()))
cmd = "{docker} tag {images}:{testname} {saveto}/{savename}:latest"
sh____(cmd.format(**locals()))
cmd = "{docker} rmi {images}:{testname}"
sx____(cmd.format(**locals()))
self.rm_testdir()
def test_2008_centos8_httpd_dockerfile(self):
""" WHEN using a dockerfile for systemd-enabled CentOS 8 and python3,
THEN we can create an image with an Apache HTTP service
being installed and enabled.
Without a special startup.sh script or container-cmd
one can just start the image and in the container
expecting that the service is started. Therefore,
WHEN we start the image as a docker container
THEN we can download the root html showing 'OK'
because the test script has placed an index.html
in the webserver containing that text. """
if not os.path.exists(DOCKER_SOCKET): self.skipTest("docker-based test")
docker = _docker
curl = _curl
python = _python or _python3
if not python.endswith("python3"): self.skipTest("using python3 on centos:8")
testname = self.testname()
testdir = self.testdir()
name = "centos8-httpd"
dockerfile = "centos8-httpd.dockerfile"
addhosts = self.local_addhosts(dockerfile)
savename = docname(dockerfile)
saveto = SAVETO
images = IMAGES
# WHEN
cmd = "{docker} build . -f {dockerfile} {addhosts} --tag {images}:{testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rm --force {testname}"
sx____(cmd.format(**locals()))
cmd = "{docker} run -d --name {testname} {images}:{testname}"
sh____(cmd.format(**locals()))
container = self.ip_container(testname)
# THEN
cmd = "sleep 5; {curl} -o {testdir}/{testname}.txt http://{container}"
sh____(cmd.format(**locals()))
cmd = "grep OK {testdir}/{testname}.txt"
sh____(cmd.format(**locals()))
#cmd = "{docker} cp {testname}:/var/log/systemctl.log {testdir}/systemctl.log"
# sh____(cmd.format(**locals()))
# SAVE
cmd = "{docker} stop {testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rm --force {testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rmi {saveto}/{savename}:latest"
sx____(cmd.format(**locals()))
cmd = "{docker} tag {images}:{testname} {saveto}/{savename}:latest"
sh____(cmd.format(**locals()))
cmd = "{docker} rmi {images}:{testname}"
sx____(cmd.format(**locals()))
self.rm_testdir()
def test_2009_centos9_httpd_dockerfile(self):
""" WHEN using a dockerfile for systemd-enabled AlmaLinux 9 and python3,
THEN we can create an image with an Apache HTTP service
being installed and enabled.
Without a special startup.sh script or container-cmd
one can just start the image and in the container
expecting that the service is started. Therefore,
WHEN we start the image as a docker container
THEN we can download the root html showing 'OK'
because the test script has placed an index.html
in the webserver containing that text. """
if not os.path.exists(DOCKER_SOCKET): self.skipTest("docker-based test")
docker = _docker
curl = _curl
python = _python or _python3
if not python.endswith("python3"): self.skipTest("using python3 on centos:9")
testname = self.testname()
testdir = self.testdir()
name = "centos9-httpd"
dockerfile = "centos9-httpd.dockerfile"
addhosts = self.local_addhosts(dockerfile)
savename = docname(dockerfile)
saveto = SAVETO
images = IMAGES
# WHEN
cmd = "{docker} build . -f {dockerfile} {addhosts} --tag {images}:{testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rm --force {testname}"
sx____(cmd.format(**locals()))
cmd = "{docker} run -d --name {testname} {images}:{testname}"
sh____(cmd.format(**locals()))
container = self.ip_container(testname)
# THEN
cmd = "sleep 5; {curl} -o {testdir}/{testname}.txt http://{container}"
sh____(cmd.format(**locals()))
cmd = "grep OK {testdir}/{testname}.txt"
sh____(cmd.format(**locals()))
#cmd = "{docker} cp {testname}:/var/log/systemctl.log {testdir}/systemctl.log"
# sh____(cmd.format(**locals()))
# SAVE
cmd = "{docker} stop {testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rm --force {testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rmi {saveto}/{savename}:latest"
sx____(cmd.format(**locals()))
cmd = "{docker} tag {images}:{testname} {saveto}/{savename}:latest"
sh____(cmd.format(**locals()))
cmd = "{docker} rmi {images}:{testname}"
sx____(cmd.format(**locals()))
self.rm_testdir()
def test_2047_centos7_httpd_not_user_dockerfile(self):
""" WHEN using a dockerfile for systemd-enabled CentOS 7 and python2,
THEN we can create an image with an Apache HTTP service
being installed and enabled.
AND in this variant it runs under User=httpd right
there from PID-1 started implicity in --user mode
THEN it fails."""
if not os.path.exists(DOCKER_SOCKET): self.skipTest("docker-based test")
docker = _docker
testname = self.testname()
testdir = self.testdir()
name = "centos7-httpd"
dockerfile = "centos7-httpd-not-user.dockerfile"
addhosts = self.local_addhosts(dockerfile)
savename = docname(dockerfile)
saveto = SAVETO
images = IMAGES
# WHEN
cmd = "{docker} build . -f {dockerfile} {addhosts} --tag {images}:{testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rm --force {testname}"
sx____(cmd.format(**locals()))
cmd = "{docker} run -d --name {testname} {images}:{testname} sleep 300"
sh____(cmd.format(**locals()))
container = self.ip_container(testname)
cmd = "{docker} exec {testname} systemctl start httpd --user"
out, err, end = output3(cmd.format(**locals()))
logg.info(" %s =>%s\n%s\n%s", cmd, end, out, err)
self.assertEqual(end, 1)
self.assertTrue(greps(err, "Unit httpd.service not for --user mode"))
cmd = "{docker} exec {testname} /usr/sbin/httpd -DFOREGROUND"
out, err, end = output3(cmd.format(**locals()))
logg.info(" %s =>%s\n%s\n%s", cmd, end, out, err)
self.assertEqual(end, 1)
self.assertTrue(greps(err, "Unable to open logs"))
# self.assertTrue(greps(err, "could not bind to address 0.0.0.0:80"))
cmd = "{docker} stop {testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rm --force {testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rmi {images}:{testname}"
sx____(cmd.format(**locals()))
self.rm_testdir()
def test_2048_centos8_httpd_not_user_dockerfile(self):
""" WHEN using a dockerfile for systemd-enabled CentOS 8 and python3,
THEN we can create an image with an Apache HTTP service
being installed and enabled.
AND in this variant it runs under User=httpd right
there from PID-1 started implicity in --user mode
THEN it fails."""
if not os.path.exists(DOCKER_SOCKET): self.skipTest("docker-based test")
docker = _docker
python = _python or _python3
if not python.endswith("python3"): self.skipTest("using python3 on centos:8")
testname = self.testname()
testdir = self.testdir()
name = "centos7-httpd"
dockerfile = "centos8-httpd-not-user.dockerfile"
addhosts = self.local_addhosts(dockerfile)
savename = docname(dockerfile)
saveto = SAVETO
images = IMAGES
# WHEN
cmd = "{docker} build . -f {dockerfile} {addhosts} --tag {images}:{testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rm --force {testname}"
sx____(cmd.format(**locals()))
cmd = "{docker} run -d --name {testname} {images}:{testname} sleep 300"
sh____(cmd.format(**locals()))
container = self.ip_container(testname)
cmd = "{docker} exec {testname} systemctl start httpd --user"
out, err, end = output3(cmd.format(**locals()))
logg.info(" %s =>%s\n%s\n%s", cmd, end, out, err)
self.assertEqual(end, 1)
self.assertTrue(greps(err, "Unit httpd.service not for --user mode"))
cmd = "{docker} exec {testname} /usr/sbin/httpd -DFOREGROUND"
out, err, end = output3(cmd.format(**locals()))
logg.info(" %s =>%s\n%s\n%s", cmd, end, out, err)
self.assertEqual(end, 1)
self.assertTrue(greps(err, "Unable to open logs"))
# self.assertTrue(greps(err, "could not bind to address 0.0.0.0:80"))
cmd = "{docker} stop {testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rm --force {testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rmi {images}:{testname}"
sx____(cmd.format(**locals()))
self.rm_testdir()
def test_2057_centos7_httpd_user_dockerfile(self):
""" WHEN using a dockerfile for systemd-enabled CentOS 7 and python2,
THEN we can create an image with an Apache HTTP service
being installed and enabled.
AND in this variant it runs under User=httpd right
there from PID-1 started implicity in --user mode.
THEN it succeeds if modified"""
if not os.path.exists(DOCKER_SOCKET): self.skipTest("docker-based test")
docker = _docker
curl = _curl
python = _python or _python2
if python.endswith("python3"): self.skipTest("no python3 on centos:7")
testname = self.testname()
testdir = self.testdir()
name = "centos7-httpd"
dockerfile = "centos7-httpd-user.dockerfile"
addhosts = self.local_addhosts(dockerfile)
savename = docname(dockerfile)
saveto = SAVETO
images = IMAGES
# WHEN
cmd = "{docker} build . -f {dockerfile} {addhosts} --tag {images}:{testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rm --force {testname}"
sx____(cmd.format(**locals()))
cmd = "{docker} run -d --name {testname} {images}:{testname} sleep 300"
sh____(cmd.format(**locals()))
cmd = "{docker} exec {testname} systemctl start httpd --user"
out, err, end = output3(cmd.format(**locals()))
logg.info(" %s =>%s\n%s\n%s", cmd, end, out, err)
self.assertEqual(end, 0)
cmd = "{docker} rm -f {testname}"
sh____(cmd.format(**locals()))
#
cmd = "{docker} run -d --name {testname} {images}:{testname}"
sh____(cmd.format(**locals()))
container = self.ip_container(testname)
# THEN
cmd = "sleep 5; {curl} -o {testdir}/{testname}.txt http://{container}:8080"
sh____(cmd.format(**locals()))
cmd = "grep OK {testdir}/{testname}.txt"
sh____(cmd.format(**locals()))
#cmd = "{docker} cp {testname}:/var/log/systemctl.log {testdir}/systemctl.log"
# sh____(cmd.format(**locals()))
cmd = "{docker} exec {testname} ps axu"
out, end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)
self.assertTrue(greps(out, "apache.*python.*systemctl"))
self.assertFalse(greps(out, "root"))
# SAVE
cmd = "{docker} stop {testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rm --force {testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rmi {saveto}/{savename}:latest"
sx____(cmd.format(**locals()))
cmd = "{docker} tag {images}:{testname} {saveto}/{savename}:latest"
sh____(cmd.format(**locals()))
cmd = "{docker} rmi {images}:{testname}"
sx____(cmd.format(**locals()))
self.rm_testdir()
def test_2058_centos8_httpd_user_dockerfile(self):
""" WHEN using a dockerfile for systemd-enabled CentOS 8 and python3,
THEN we can create an image with an Apache HTTP service
being installed and enabled.
AND in this variant it runs under User=httpd right
there from PID-1 started implicity in --user mode.
THEN it succeeds if modified"""
if not os.path.exists(DOCKER_SOCKET): self.skipTest("docker-based test")
docker = _docker
curl = _curl
python = _python or _python3
if not python.endswith("python3"): self.skipTest("using python3 on centos:8")
testname = self.testname()
testdir = self.testdir()
name = "centos8-httpd"
dockerfile = "centos8-httpd-user.dockerfile"
addhosts = self.local_addhosts(dockerfile)
savename = docname(dockerfile)
saveto = SAVETO
images = IMAGES
# WHEN
cmd = "{docker} build . -f {dockerfile} {addhosts} --tag {images}:{testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rm --force {testname}"
sx____(cmd.format(**locals()))
cmd = "{docker} run -d --name {testname} {images}:{testname} sleep 300"
sh____(cmd.format(**locals()))
cmd = "{docker} exec {testname} systemctl start httpd --user"
out, err, end = output3(cmd.format(**locals()))
logg.info(" %s =>%s\n%s\n%s", cmd, end, out, err)
self.assertEqual(end, 0)
cmd = "{docker} rm -f {testname}"
sh____(cmd.format(**locals()))
#
cmd = "{docker} run -d --name {testname} {images}:{testname}"
sh____(cmd.format(**locals()))
container = self.ip_container(testname)
# THEN
cmd = "sleep 5; {curl} -o {testdir}/{testname}.txt http://{container}:8080"
sh____(cmd.format(**locals()))
cmd = "grep OK {testdir}/{testname}.txt"
sh____(cmd.format(**locals()))
#cmd = "{docker} cp {testname}:/var/log/systemctl.log {testdir}/systemctl.log"
# sh____(cmd.format(**locals()))
cmd = "{docker} exec {testname} ps axu"
out, end = output2(cmd.format(**locals()))
logg.info(" %s =>%s\n%s", cmd, end, out)
self.assertTrue(greps(out, "apache.*python.*systemctl"))
self.assertFalse(greps(out, "root"))
# SAVE
cmd = "{docker} stop {testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rm --force {testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rmi {saveto}/{savename}:latest"
sx____(cmd.format(**locals()))
cmd = "{docker} tag {images}:{testname} {saveto}/{savename}:latest"
sh____(cmd.format(**locals()))
cmd = "{docker} rmi {images}:{testname}"
sx____(cmd.format(**locals()))
self.rm_testdir()
def test_2114_ubuntu_apache2(self):
""" WHEN using a systemd enabled Ubuntu as the base image
THEN we can create an image with an Apache HTTP service
being installed and enabled.
Without a special startup.sh script or container-cmd
one can just start the image and in the container
expecting that the service is started. Therefore,
WHEN we start the image as a docker container
THEN we can download the root html showing 'OK'
because the test script has placed an index.html
in the webserver containing that text. """
if not os.path.exists(DOCKER_SOCKET): self.skipTest("docker-based test")
docker = _docker
curl = _curl
self.skipTest("test_216 makes it through a dockerfile")
testname = self.testname()
testdir = self.testdir()
saveto = SAVETO
images = IMAGES
basename = "ubuntu:16.04"
savename = "ubuntu-apache2-test"
image = self.local_image(basename)
python_base = os.path.basename(_python or _python3)
systemctl_py = _systemctl_py
logg.info("%s:%s %s", testname, port, basename)
#
cmd = "{docker} rm --force {testname}"
sx____(cmd.format(**locals()))
cmd = "{docker} run --detach --name={testname} {image} sleep 200"
sh____(cmd.format(**locals()))
cmd = "{docker} exec {testname} touch /var/log/systemctl.log"
sh____(cmd.format(**locals()))
cmd = "{docker} exec {testname} apt-get update"
sh____(cmd.format(**locals()))
cmd = "{docker} exec {testname} apt-get install -y apache2 {python_base}"
sh____(cmd.format(**locals()))
cmd = "{docker} cp {systemctl_py} {testname}:/usr/bin/systemctl"
sh____(cmd.format(**locals()))
cmd = "{docker} exec {testname} bash -c 'test -L /bin/systemctl || ln -sf /usr/bin/systemctl /bin/systemctl'"
sh____(cmd.format(**locals()))
cmd = "{docker} exec {testname} systemctl enable apache2"
sh____(cmd.format(**locals()))
cmd = "{docker} exec {testname} bash -c 'echo TEST_OK > /var/www/html/index.html'"
sh____(cmd.format(**locals()))
# .........................................
cmd = "{docker} commit -c 'CMD [\"/usr/bin/systemctl\"]' {testname} {images}:{testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} stop {testname}"
sx____(cmd.format(**locals()))
cmd = "{docker} rm --force {testname}"
sx____(cmd.format(**locals()))
cmd = "{docker} run -d --name {testname} {images}:{testname}"
sh____(cmd.format(**locals()))
container = self.ip_container(testname)
# THEN
cmd = "sleep 5; {curl} -o {testdir}/{testname}.txt http://{container}"
sh____(cmd.format(**locals()))
cmd = "grep OK {testdir}/{testname}.txt"
sh____(cmd.format(**locals()))
cmd = "{docker} cp {testname}:/var/log/systemctl.log {testdir}/systemctl.log"
sh____(cmd.format(**locals()))
# SAVE
cmd = "{docker} stop {testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rm --force {testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rmi {saveto}/{savename}:latest"
sx____(cmd.format(**locals()))
cmd = "{docker} tag {images}:{testname} {saveto}/{savename}:latest"
sh____(cmd.format(**locals()))
cmd = "{docker} rmi {images}:{testname}"
sx____(cmd.format(**locals()))
self.rm_testdir()
def test_2116_ubuntu16_apache2(self):
""" WHEN using a dockerfile for systemd enabled Ubuntu 16 with python2
THEN we can create an image with an Apache HTTP service
being installed and enabled.
Without a special startup.sh script or container-cmd
one can just start the image and in the container
expecting that the service is started. Therefore,
WHEN we start the image as a docker container
THEN we can download the root html showing 'OK'
because the test script has placed an index.html
in the webserver containing that text. """
if not os.path.exists(DOCKER_SOCKET): self.skipTest("docker-based test")
docker = _docker
curl = _curl
testname = self.testname()
testdir = self.testdir()
dockerfile = "ubuntu16-apache2.dockerfile"
addhosts = self.local_addhosts(dockerfile)
savename = docname(dockerfile)
saveto = SAVETO
images = IMAGES
# WHEN
cmd = "{docker} build . -f {dockerfile} {addhosts} --tag {images}:{testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rm --force {testname}"
sx____(cmd.format(**locals()))
cmd = "{docker} run -d --name {testname} {images}:{testname}"
sh____(cmd.format(**locals()))
container = self.ip_container(testname)
# THEN
cmd = "sleep 5; {curl} -o {testdir}/{testname}.txt http://{container}"
sh____(cmd.format(**locals()))
cmd = "grep OK {testdir}/{testname}.txt"
sh____(cmd.format(**locals()))
#cmd = "{docker} cp {testname}:/var/log/systemctl.log {testdir}/systemctl.log"
# sh____(cmd.format(**locals()))
# SAVE
cmd = "{docker} stop {testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rm --force {testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rmi {saveto}/{savename}:latest"
sx____(cmd.format(**locals()))
cmd = "{docker} tag {images}:{testname} {saveto}/{savename}:latest"
sh____(cmd.format(**locals()))
cmd = "{docker} rmi {images}:{testname}"
sx____(cmd.format(**locals()))
self.rm_testdir()
def test_2118_ubuntu18_apache2(self):
""" WHEN using a dockerfile for systemd enabled Ubuntu 18 with python3
THEN we can create an image with an Apache HTTP service
being installed and enabled.
Without a special startup.sh script or container-cmd
one can just start the image and in the container
expecting that the service is started. Therefore,
WHEN we start the image as a docker container
THEN we can download the root html showing 'OK'
because the test script has placed an index.html
in the webserver containing that text. """
if not os.path.exists(DOCKER_SOCKET): self.skipTest("docker-based test")
docker = _docker
curl = _curl
testname = self.testname()
testdir = self.testdir()
dockerfile = "ubuntu18-apache2.dockerfile"
addhosts = self.local_addhosts(dockerfile)
savename = docname(dockerfile)
saveto = SAVETO
images = IMAGES
# WHEN
cmd = "{docker} build . -f {dockerfile} {addhosts} --tag {images}:{testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rm --force {testname}"
sx____(cmd.format(**locals()))
cmd = "{docker} run -d --name {testname} {images}:{testname}"
sh____(cmd.format(**locals()))
container = self.ip_container(testname)
# THEN
cmd = "sleep 5; {curl} -o {testdir}/{testname}.txt http://{container}"
sh____(cmd.format(**locals()))
cmd = "grep OK {testdir}/{testname}.txt"
sh____(cmd.format(**locals()))
#cmd = "{docker} cp {testname}:/var/log/systemctl.log {testdir}/systemctl.log"
# sh____(cmd.format(**locals()))
# SAVE
cmd = "{docker} stop {testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rm --force {testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rmi {saveto}/{savename}:latest"
sx____(cmd.format(**locals()))
cmd = "{docker} tag {images}:{testname} {saveto}/{savename}:latest"
sh____(cmd.format(**locals()))
cmd = "{docker} rmi {images}:{testname}"
sx____(cmd.format(**locals()))
self.rm_testdir()
def test_2122_ubuntu22_apache2(self):
""" WHEN using a dockerfile for systemd enabled Ubuntu 22 with python3
THEN we can create an image with an Apache HTTP service
being installed and enabled.
Without a special startup.sh script or container-cmd
one can just start the image and in the container
expecting that the service is started. Therefore,
WHEN we start the image as a docker container
THEN we can download the root html showing 'OK'
because the test script has placed an index.html
in the webserver containing that text. """
if not os.path.exists(DOCKER_SOCKET): self.skipTest("docker-based test")
docker = _docker
curl = _curl
testname = self.testname()
testdir = self.testdir()
dockerfile = "ubuntu22-apache2.dockerfile"
addhosts = self.local_addhosts(dockerfile)
savename = docname(dockerfile)
saveto = SAVETO
images = IMAGES
# WHEN
cmd = "{docker} build . -f {dockerfile} {addhosts} --tag {images}:{testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rm --force {testname}"
sx____(cmd.format(**locals()))
cmd = "{docker} run -d --name {testname} {images}:{testname}"
sh____(cmd.format(**locals()))
container = self.ip_container(testname)
# THEN
cmd = "sleep 5; {curl} -o {testdir}/{testname}.txt http://{container}"
sh____(cmd.format(**locals()))
cmd = "grep OK {testdir}/{testname}.txt"
sh____(cmd.format(**locals()))
#cmd = "{docker} cp {testname}:/var/log/systemctl.log {testdir}/systemctl.log"
# sh____(cmd.format(**locals()))
# SAVE
cmd = "{docker} stop {testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rm --force {testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rmi {saveto}/{savename}:latest"
sx____(cmd.format(**locals()))
cmd = "{docker} tag {images}:{testname} {saveto}/{savename}:latest"
sh____(cmd.format(**locals()))
cmd = "{docker} rmi {images}:{testname}"
sx____(cmd.format(**locals()))
self.rm_testdir()
def test_2215_opensuse15_apache2_dockerfile(self):
""" WHEN using a dockerfile for systemd-enabled CentOS 8 and python3,
THEN we can create an image with an Apache HTTP service
being installed and enabled.
Without a special startup.sh script or container-cmd
one can just start the image and in the container
expecting that the service is started. Therefore,
WHEN we start the image as a docker container
THEN we can download the root html showing 'OK'
because the test script has placed an index.html
in the webserver containing that text. """
if not os.path.exists(DOCKER_SOCKET): self.skipTest("docker-based test")
docker = _docker
curl = _curl
python = _python or _python3
testname = self.testname()
testdir = self.testdir()
name = "opensuse15-apache2"
dockerfile = "opensuse15-apache2.dockerfile"
addhosts = self.local_addhosts(dockerfile)
savename = docname(dockerfile)
saveto = SAVETO
images = IMAGES
# WHEN
cmd = "{docker} build . -f {dockerfile} {addhosts} --tag {images}:{testname}"
sh____(cmd.format(**locals()))
cmd = "{docker} rm --force {testname}"
sx____(cmd.format(**locals()))
cmd = "{docker} run -d --name {testname} {images}:{testname}"
sh____(cmd.format(**locals()))
container = self.ip_container(testname)
# THEN
cmd = "sleep 5; {curl} -o {testdir}/{testname}.txt http://{container}"
sh____(cmd.format(**locals()))
cmd = "grep OK {testdir}/{testname}.txt"
sh____(cmd.format(**locals()))
#cmd = "{docker} cp {testname}:/var/log/systemctl.log {testdir}/systemctl.log"
# sh____(cmd.format(**locals()))