-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathdocker-copyedit-tests.py
executable file
·3569 lines (3555 loc) · 145 KB
/
docker-copyedit-tests.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
__copyright__ = "(C) 2017-2024 Guido U. Draheim, licensed under the EUPL"
__version__ = "1.4.7321"
from typing import Any, Optional, Union, List, Iterator, NamedTuple
import sys
import subprocess
import unittest
import re
import time
import inspect
import shutil
import os.path
import glob
import logging
from fnmatch import fnmatchcase as fnmatch
import json
if sys.version[0] != '2':
basestring = str
os.chdir(os.path.dirname(os.path.abspath(__file__))) # assume the scripts stayed together
logg = logging.getLogger("tests")
OK = True
IMG = "localhost:5000/docker-copyedit"
UID = 1001
TMP = "tmp"
_python = "python"
_docker = "docker"
_podman = "" # usually "podman"
_script = "docker-copyedit.py"
_force = 0
_keep = 0
_image = "centos:centos8"
_coverage = False
_coverage_file = "tmp.coverage.xml"
def _copyedit(docker: Optional[str] = None) -> str:
docker = docker or _docker
script = _script
if docker != "docker":
script += " --docker=" + docker
if _coverage:
script = "-m coverage run -a " + script
return script
def _centos() -> str:
return _image
def _nogroup(image: Optional[str] = None) -> str:
image = image or _image
if "centos" in image:
return "nobody"
if "ubuntu" in image:
return "nogroup"
return "nobody"
def get_caller_name() -> str:
currentframe = inspect.currentframe()
if currentframe is None:
return "global"
if currentframe.f_back is None:
return "global"
if currentframe.f_back.f_back is None:
return "global"
frame = currentframe.f_back.f_back
return frame.f_code.co_name
def get_caller_caller_name() -> str:
currentframe = inspect.currentframe()
if currentframe is None:
return "global"
if currentframe.f_back is None:
return "global"
if currentframe.f_back.f_back is None:
return "global"
if currentframe.f_back.f_back.f_back is None:
return "global"
frame = currentframe.f_back.f_back.f_back
return frame.f_code.co_name
def get_caller_caller_caller_name() -> str:
currentframe = inspect.currentframe()
if currentframe is None:
return "global"
if currentframe.f_back is None:
return "global"
if currentframe.f_back.f_back is None:
return "global"
if currentframe.f_back.f_back.f_back is None:
return "global"
if currentframe.f_back.f_back.f_back.f_back is None:
return "global"
frame = currentframe.f_back.f_back.f_back.f_back
return frame.f_code.co_name
def os_path(root: Optional[str], path: str) -> str:
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 decodes(text: Union[None, bytes, str]) -> Optional[str]:
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 _lines(lines: Union[str, List[str]]) -> List[str]:
if isinstance(lines, basestring):
lines = lines.split("\n")
if len(lines) and lines[-1] == "":
lines = lines[:-1]
return lines
def lines(text: Union[str, List[str]]) -> List[str]:
lines = []
for line in _lines(text):
lines.append(line.rstrip())
return lines
def _grep(pattern: str, lines: Union[str, List[str]]) -> Iterator[str]:
for line in _lines(lines):
if re.search(pattern, line.rstrip()):
yield line.rstrip()
def grep(pattern: str, lines: Union[str, List[str]]) -> List[str]:
return list(grep(pattern, lines))
def greps(lines: Union[str, List[str]], pattern: str) -> List[str]:
return list(grep(pattern, lines))
def text_file(filename: str, content: str) -> None:
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)
assert x is not None
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: str, content: str) -> None:
text_file(filename, content)
os.chmod(filename, 0o770)
class ShellResult(NamedTuple):
returncode: int
stdout: str
stderr: str
class ShellException(Exception):
def __init__(self, msg: str, result: ShellResult) -> None:
Exception.__init__(self, msg)
self.result = result
def sh(cmd: str, shell: bool = True, check: bool = True, ok: Optional[bool] = None, default: str = "") -> ShellResult:
if ok is None: ok = OK # a parameter "ok = OK" does not work in python
if not ok:
logg.info("skip %s", cmd)
return ShellResult(0, default or "", "")
run = subprocess.Popen(cmd, shell=shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
run.wait()
assert run.stdout is not None and run.stderr is not None
result = ShellResult(run.returncode, decodes(run.stdout.read()) or "", decodes(run.stderr.read()) or "")
if check and result.returncode:
logg.error("CMD %s", cmd)
logg.error("EXIT %s", result.returncode)
logg.error("STDOUT %s", result.stdout)
logg.error("STDERR %s", result.stderr)
raise ShellException("shell command failed", result)
return result
class DockerCopyeditTest(unittest.TestCase):
def caller_testname(self) -> str:
name = get_caller_caller_name()
nested = get_caller_caller_caller_name()
if nested.startswith("test_"):
name = nested
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: Optional[str] = None) -> str:
name = self.caller_testname()
if suffix:
return name + "_" + suffix
return name
def testdir(self, testname: Optional[str] = None) -> str:
testname = testname or self.caller_testname()
newdir = os.path.join(TMP, testname)
if os.path.isdir(newdir):
shutil.rmtree(newdir)
os.makedirs(newdir)
return newdir
def rm_testdir(self, testname: Optional[str] = None) -> str:
testname = testname or self.caller_testname()
newdir = os.path.join(TMP, testname)
if os.path.isdir(newdir):
if _keep:
logg.info("KEEP %s", newdir)
else:
shutil.rmtree(newdir)
return newdir
def save(self, testname: str) -> None:
if os.path.exists(".coverage"):
os.rename(".coverage", os.path.join(TMP, ".coverage." + testname))
def can_not_chown(self, docker: str) -> Optional[str]:
if _force:
return None
if docker.endswith("podman"): # may check for a specific version?
return "`podman build` can not run `chown myuser` steps"
return None
def healthcheck_not_supported(self, docker: str) -> Optional[str]:
if _force:
return None
if docker.endswith("podman"): # may check for a specific version?
return "`podman build` can support HEALTHCHECK CMD settings"
return None
def no_podman(self) -> str:
podman = _podman
if not podman:
return "no --podman=alternative tool specified"
if podman == _docker:
return "found same docker and alternative tool"
if os.path.exists(podman):
return ""
if "/" not in podman:
for check in os.environ.get("PATH", "/usr/bin").split(":"):
if os.path.exists(os.path.join(check, podman)):
return ""
return F"did not find alternative tool = {podman}"
#
def test_011_help(self, docker: Optional[str] = None) -> None:
""" docker-copyedit.py --help """
python = _python
docker = docker or _docker
copyedit = _copyedit(docker)
logg.info(": %s", python)
cmd = F"{python} {copyedit} --help"
run = sh(cmd)
logg.info("help\n%s", run.stdout)
def test_012_help(self, docker: Optional[str] = None) -> None:
""" docker-copyedit.py --help """
python = _python
docker = docker or _docker
copyedit = _copyedit(docker)
logg.info(": %s", python)
cmd = F"{python} {copyedit} --help"
run = sh(cmd)
logg.info("%s\n%s\n%s", cmd, run.stdout, run.stderr)
def test_014_help(self, docker: Optional[str] = None) -> None:
""" docker-copyedit.py --help """
python = _python
docker = docker or _docker
copyedit = _copyedit(docker)
logg.info(": %s", python)
try:
cmd = F"{python} {copyedit} --helps"
run = sh(cmd)
logg.info("help\n%s", run.stdout)
except Exception as e:
msg = str(e)
logg.info("help exception %s", msg)
self.assertEqual(msg, "shell command failed")
def test_101_fake_simple(self, docker: Optional[str] = None) -> None:
""" docker-copyedit.py from image1 into image2 --dryrun """
python = _python
docker = docker or _docker
copyedit = _copyedit(docker)
logg.info(": %s", python)
cmd = F"{python} {copyedit} from image1 into image2 --dryrun -vvv"
run = sh(cmd)
logg.info("%s\n%s\n%s", cmd, run.stdout, run.stderr)
self.save(self.testname())
def test_102_fake_simple(self, docker: Optional[str] = None) -> None:
""" docker-copyedit.py from image1 --dryrun """
python = _python
docker = docker or _docker
copyedit = _copyedit(docker)
logg.info(": %s", python)
cmd = F"{python} {copyedit} from image1 --dryrun -vvv"
try:
run = sh(cmd)
except ShellException as e:
logg.info("catch %s", e)
self.assertIn("no output image given - use 'INTO image-name'", e.result.stderr)
run = e.result
logg.info("%s\n%s\n%s", cmd, run.stdout, run.stderr)
self.save(self.testname())
def test_103_fake_simple(self, docker: Optional[str] = None) -> None:
""" docker-copyedit.py from image1 into '' --dryrun """
python = _python
docker = docker or _docker
copyedit = _copyedit(docker)
logg.info(": %s", python)
cmd = F"{python} {copyedit} from image1 into '' --dryrun -vvv"
try:
run = sh(cmd)
except ShellException as e:
logg.info("catch %s", e)
self.assertIn("no output image given - use 'INTO image-name'", e.result.stderr)
run = e.result
logg.info("%s\n%s\n%s", cmd, run.stdout, run.stderr)
self.save(self.testname())
def test_104_fake_simple(self, docker: Optional[str] = None) -> None:
""" docker-copyedit.py from image1 into ' ' --dryrun """
python = _python
docker = docker or _docker
copyedit = _copyedit(docker)
logg.info(": %s", python)
cmd = F"{python} {copyedit} from image1 into ' ' --dryrun -vvv"
run = sh(cmd)
logg.info("%s\n%s\n%s", cmd, run.stdout, run.stderr)
self.save(self.testname())
def test_105_fake_simple(self, docker: Optional[str] = None) -> None:
""" docker-copyedit.py from image1 into ' ' add note x --dryrun """
python = _python
docker = docker or _docker
copyedit = _copyedit(docker)
logg.info(": %s", python)
cmd = F"{python} {copyedit} from image1 into ' ' add note x --dryrun -vvv"
try:
run = sh(cmd)
except ShellException as e:
logg.info("catch %s", e)
self.assertIn("unknown edit command starting with add note", e.result.stderr)
run = e.result
logg.info("%s\n%s\n%s", cmd, run.stdout, run.stderr)
self.save(self.testname())
def test_118_pull_base_image(self, docker: Optional[str] = None) -> None:
if self.no_podman(): self.skipTest(self.no_podman())
self.test_112_pull_base_image(_podman)
def test_112_pull_base_image(self, docker: Optional[str] = None) -> None:
img = IMG
python = _python
docker = docker or _docker
copyedit = _copyedit(docker)
centos = _centos()
logg.info(": %s : %s", python, img)
testname = self.testname()
testdir = self.testdir()
cmd = F"{docker} image history {centos} || {docker} pull {centos}"
logg.info("%s ===========>>>", cmd)
run = sh(cmd)
logg.info("%s\n%s\n%s", cmd, run.stdout, run.stderr)
if "there might not be enough IDs available in the namespace" in run.stderr:
logg.error("you need to check /etc/subgid and /etc/subuid")
logg.error("you need to run : podman system migrate --log-level=debug")
self.save(self.testname())
def test_202_real_simple(self, docker: Optional[str] = None) -> None:
""" docker-copyedit.py from image1 into image2 """
python = _python
docker = docker or _docker
copyedit = _copyedit(docker)
logg.info(": %s", python)
cmd = F"{python} {copyedit} from image1 into image2 -vvv"
run = sh(cmd)
logg.info("%s\n%s\n%s", cmd, run.stdout, run.stderr)
self.assertTrue("nothing to do for image2", run.stderr)
self.save(self.testname())
def test_218_can_build(self, docker: Optional[str] = None) -> None:
if self.no_podman(): self.skipTest(self.no_podman())
self.test_211_can_build(_podman)
def test_211_can_build(self, docker: Optional[str] = None) -> None:
img = IMG
python = _python
docker = docker or _docker
copyedit = _copyedit(docker)
centos = _centos()
logg.info(": %s : %s", python, img)
testname = self.testname()
testdir = self.testdir()
text_file(os_path(testdir, "Dockerfile"), F"""
FROM {centos}
RUN touch /myinfo.txt
""")
cmd = F"{docker} build {testdir} -t {img}:{testname}"
run = sh(cmd)
logg.info("%s\n%s", run.stdout, run.stderr)
#
cmd = F"{docker} inspect {img}:{testname}"
run = sh(cmd)
data = json.loads(run.stdout)
logg.debug("CONFIG:\n%s", data[0]["Config"])
logg.info(F"{testname} VOLUMES = %s", data[0]["Config"].get("Volumes"))
dat1 = data
#
cmd = F"{docker} rmi {img}:{testname}"
rmi = sh(cmd)
logg.info("[%s] %s", rmi.returncode, cmd)
#
self.assertEqual(dat1[0]["Config"].get("Volumes"), None)
self.rm_testdir()
self.save(testname)
def test_221_run_unchanged_copyedit(self, docker: Optional[str] = None) -> None:
img = IMG
python = _python
docker = docker or _docker
copyedit = _copyedit(docker)
centos = _centos()
logg.info(": %s : %s", python, img)
testname = self.testname()
testdir = self.testdir()
text_file(os_path(testdir, "Dockerfile"), F"""
FROM {centos}
RUN touch /myinfo.txt
""")
cmd = F"{docker} build {testdir} -t {img}:{testname}"
run = sh(cmd)
logg.info("%s\n%s", run.stdout, run.stderr)
#
cmd = F"{docker} inspect {img}:{testname}"
run = sh(cmd)
data = json.loads(run.stdout)
logg.debug("CONFIG:\n%s", data[0]["Config"])
logg.info(F"{testname} VOLUMES = %s", data[0]["Config"].get("Volumes"))
dat1 = data
#
savename = testname + "x"
cmd = F"{python} {copyedit} FROM {img}:{testname} INTO {img}:{savename} remove label version -vv"
run = sh(cmd)
logg.info("%s\n%s\n%s", cmd, run.stdout, run.stderr)
#
cmd = F"{docker} rmi {img}:{testname} {img}:{savename}"
rmi = sh(cmd)
logg.info("[%s] %s", rmi.returncode, cmd)
#
self.assertEqual(dat1[0]["Config"].get("Volumes"), None)
self.assertIn("there was no label version", run.stderr)
self.assertIn("unchanged image", run.stderr)
self.assertIn("tagged old image", run.stderr)
self.rm_testdir()
self.save(testname)
def test_231_run_version_too_long(self, docker: Optional[str] = None) -> None:
img = IMG
python = _python
docker = docker or _docker
copyedit = _copyedit(docker)
centos = _centos()
logg.info(": %s : %s", python, img)
testname = self.testname()
testdir = self.testdir()
text_file(os_path(testdir, "Dockerfile"), F"""
FROM {centos}
RUN touch /myinfo.txt
""")
cmd = F"{docker} build {testdir} -t {img}:{testname}"
run = sh(cmd)
logg.info("%s\n%s", run.stdout, run.stderr)
#
cmd = F"{docker} inspect {img}:{testname}"
run = sh(cmd)
data = json.loads(run.stdout)
logg.debug("CONFIG:\n%s", data[0]["Config"])
logg.info(F"{testname} VOLUMES = %s", data[0]["Config"].get("Volumes"))
dat1 = data
#
savename = testname + "-has-image-name-with-a-version-longer-than-one-hundred-twenty-seven-characters"
savename += "-which-is-not-allowed-for-any-docker-image"
cmd = F"{python} {copyedit} FROM {img}:{testname} INTO {img}:{savename} remove label version -vv"
run = sh(cmd)
logg.info("%s\n%s\n%s", cmd, run.stdout, run.stderr)
#
cmd = F"{docker} rmi {img}:{testname} {img}:{savename}"
rmi = sh(cmd)
logg.info("[%s] %s", rmi.returncode, cmd)
#
self.assertEqual(dat1[0]["Config"].get("Volumes"), None)
self.assertIn("there was no label version", run.stderr)
self.assertIn("unchanged image", run.stderr)
self.assertIn("tagged old image", run.stderr)
#
self.assertIn("image version: may not be longer", run.stderr)
self.rm_testdir()
self.save(testname)
def test_232_run_version_not_too_long(self, docker: Optional[str] = None) -> None:
img = IMG
python = _python
docker = docker or _docker
copyedit = _copyedit(docker)
centos = _centos()
logg.info(": %s : %s", python, img)
testname = self.testname()
testdir = self.testdir()
text_file(os_path(testdir, "Dockerfile"), F"""
FROM {centos}
RUN touch /myinfo.txt
""")
cmd = F"{docker} build {testdir} -t {img}:{testname}"
run = sh(cmd)
logg.info("%s\n%s", run.stdout, run.stderr)
#
cmd = F"{docker} inspect {img}:{testname}"
run = sh(cmd)
data = json.loads(run.stdout)
logg.debug("CONFIG:\n%s", data[0]["Config"])
logg.info(F"{testname} VOLUMES = %s", data[0]["Config"].get("Volumes"))
dat1 = data
#
savename = testname + "-has-image-name-with-a-version-shorter-than-one-hundred-twenty-seven-characters"
cmd = F"{python} {copyedit} FROM {img}:{testname} INTO {img}:{savename} remove label version -vv"
run = sh(cmd)
logg.info("%s\n%s\n%s", cmd, run.stdout, run.stderr)
#
cmd = F"{docker} rmi {img}:{testname} {img}:{savename}"
rmi = sh(cmd)
logg.info("[%s] %s", rmi.returncode, cmd)
#
self.assertEqual(dat1[0]["Config"].get("Volumes"), None)
self.assertIn("there was no label version", run.stderr)
self.assertIn("unchanged image", run.stderr)
self.assertIn("tagged old image", run.stderr)
#
self.assertNotIn("image version: may not be longer", run.stderr)
self.rm_testdir()
self.save(testname)
def test_233_run_version_made_too_long(self, docker: Optional[str] = None) -> None:
img = IMG
python = _python
docker = docker or _docker
copyedit = _copyedit(docker)
centos = _centos()
logg.info(": %s : %s", python, img)
testname = self.testname()
testdir = self.testdir()
text_file(os_path(testdir, "Dockerfile"), F"""
FROM {centos}
RUN touch /myinfo.txt
""")
cmd = F"{docker} build {testdir} -t {img}:{testname}"
run = sh(cmd)
logg.info("%s\n%s", run.stdout, run.stderr)
#
cmd = F"{docker} inspect {img}:{testname}"
run = sh(cmd)
data = json.loads(run.stdout)
logg.debug("CONFIG:\n%s", data[0]["Config"])
logg.info(F"{testname} VOLUMES = %s", data[0]["Config"].get("Volumes"))
dat1 = data
#
savename = testname + "-has-image-name-with-a-version-shorter-than-one-hundred-twenty-seven-characters"
cmd = F"{python} {copyedit} -c MAX_VERSION=33 FROM {img}:{testname} INTO {img}:{savename} remove label version -vv"
run = sh(cmd)
logg.info("%s\n%s\n%s", cmd, run.stdout, run.stderr)
#
cmd = F"{docker} rmi {img}:{testname} {img}:{savename}"
rmi = sh(cmd)
logg.info("[%s] %s", rmi.returncode, cmd)
#
self.assertEqual(dat1[0]["Config"].get("Volumes"), None)
self.assertIn("there was no label version", run.stderr)
self.assertIn("unchanged image", run.stderr)
self.assertIn("tagged old image", run.stderr)
#
self.assertIn("image version: may not be longer", run.stderr)
self.rm_testdir()
self.save(testname)
def test_280_change_tempdir(self, docker: Optional[str] = None) -> None:
img = IMG
python = _python
docker = docker or _docker
copyedit = _copyedit(docker)
centos = _centos()
logg.info(": %s : %s", python, img)
testname = self.testname()
testdir = self.testdir()
text_file(os_path(testdir, "Dockerfile"), F"""
FROM {centos}
RUN touch /myinfo.txt
""")
cmd = F"{docker} build {testdir} -t {img}:{testname}"
run = sh(cmd)
logg.info("%s\n%s", run.stdout, run.stderr)
#
cmd = F"{docker} inspect {img}:{testname}"
run = sh(cmd)
data = json.loads(run.stdout)
logg.debug("CONFIG:\n%s", data[0]["Config"])
logg.info(F"{testname} VOLUMES = %s", data[0]["Config"].get("Volumes"))
dat1 = data
#
tempdir = testdir + "/new.tmp"
savename = testname + "x"
cmd = F"{python} {copyedit} -T {tempdir} FROM {img}:{testname} INTO {img}:{savename} remove label version -vv"
run = sh(cmd)
logg.info("%s\n%s\n%s", cmd, run.stdout, run.stderr)
#
cmd = F"{docker} rmi {img}:{testname} {img}:{savename}"
rmi = sh(cmd)
logg.info("[%s] %s", rmi.returncode, cmd)
#
self.assertEqual(dat1[0]["Config"].get("Volumes"), None)
self.assertIn("there was no label version", run.stderr)
self.assertIn("unchanged image", run.stderr)
self.assertIn("tagged old image", run.stderr)
#
datadir = tempdir + "/data"
savetar = tempdir + "/saved.tar"
loadtar = tempdir + "/ready.tar"
self.assertIn(datadir, run.stderr)
self.assertNotIn("keeping " + datadir, run.stderr)
self.assertNotIn("keeping " + savetar, run.stderr)
self.assertNotIn("keeping " + loadtar, run.stderr)
self.assertIn(F"new {datadir} from {docker} save", run.stderr)
self.assertFalse(os.path.isdir(datadir))
self.assertFalse(os.path.isfile(savetar))
self.assertFalse(os.path.isfile(loadtar)) # not packed because no change
self.rm_testdir()
self.save(testname)
def test_281_keep_datadir(self, docker: Optional[str] = None) -> None:
img = IMG
python = _python
docker = docker or _docker
copyedit = _copyedit(docker)
centos = _centos()
logg.info(": %s : %s", python, img)
testname = self.testname()
testdir = self.testdir()
text_file(os_path(testdir, "Dockerfile"), F"""
FROM {centos}
RUN touch /myinfo.txt
""")
cmd = F"{docker} build {testdir} -t {img}:{testname}"
run = sh(cmd)
logg.info("%s\n%s", run.stdout, run.stderr)
#
cmd = F"{docker} inspect {img}:{testname}"
run = sh(cmd)
data = json.loads(run.stdout)
logg.debug("CONFIG:\n%s", data[0]["Config"])
logg.info(F"{testname} VOLUMES = %s", data[0]["Config"].get("Volumes"))
dat1 = data
#
tempdir = testdir + "/new.tmp"
savename = testname + "x"
cmd = F"{python} {copyedit} -T {tempdir} -k FROM {img}:{testname} INTO {img}:{savename} remove label version -vv"
run = sh(cmd)
logg.info("%s\n%s\n%s", cmd, run.stdout, run.stderr)
#
cmd = F"{docker} rmi {img}:{testname} {img}:{savename}"
rmi = sh(cmd)
logg.info("[%s] %s", rmi.returncode, cmd)
#
self.assertEqual(dat1[0]["Config"].get("Volumes"), None)
self.assertIn("there was no label version", run.stderr)
self.assertIn("unchanged image", run.stderr)
self.assertIn("tagged old image", run.stderr)
#
datadir = tempdir + "/data"
savetar = tempdir + "/saved.tar"
loadtar = tempdir + "/ready.tar"
self.assertIn(datadir, run.stderr)
self.assertIn("keeping " + datadir, run.stderr)
self.assertNotIn("keeping " + savetar, run.stderr)
self.assertNotIn("keeping " + loadtar, run.stderr)
self.assertIn(F"new {datadir} from {docker} save", run.stderr)
self.assertTrue(os.path.isdir(datadir))
self.assertFalse(os.path.isfile(savetar))
self.assertFalse(os.path.isfile(loadtar)) # not packed because no change
self.rm_testdir()
self.save(testname)
def test_282_keep_savefile(self, docker: Optional[str] = None) -> None:
img = IMG
python = _python
docker = docker or _docker
copyedit = _copyedit(docker)
centos = _centos()
logg.info(": %s : %s", python, img)
testname = self.testname()
testdir = self.testdir()
text_file(os_path(testdir, "Dockerfile"), F"""
FROM {centos}
RUN touch /myinfo.txt
""")
cmd = F"{docker} build {testdir} -t {img}:{testname}"
run = sh(cmd)
logg.info("%s\n%s", run.stdout, run.stderr)
#
cmd = F"{docker} inspect {img}:{testname}"
run = sh(cmd)
data = json.loads(run.stdout)
logg.debug("CONFIG:\n%s", data[0]["Config"])
logg.info(F"{testname} VOLUMES = %s", data[0]["Config"].get("Volumes"))
dat1 = data
#
tempdir = testdir + "/new.tmp"
savename = testname + "x"
cmd = F"{python} {copyedit} -T {tempdir} -kk FROM {img}:{testname} INTO {img}:{savename} remove label version -vv"
run = sh(cmd)
logg.info("%s\n%s\n%s", cmd, run.stdout, run.stderr)
#
cmd = F"{docker} rmi {img}:{testname} {img}:{savename}"
rmi = sh(cmd)
logg.info("[%s] %s", rmi.returncode, cmd)
#
self.assertEqual(dat1[0]["Config"].get("Volumes"), None)
self.assertIn("there was no label version", run.stderr)
self.assertIn("unchanged image", run.stderr)
self.assertIn("tagged old image", run.stderr)
#
datadir = tempdir + "/data"
savetar = tempdir + "/saved.tar"
loadtar = tempdir + "/ready.tar"
self.assertIn(datadir, run.stderr)
self.assertIn("keeping " + datadir, run.stderr)
self.assertNotIn("keeping " + savetar, run.stderr)
self.assertNotIn("keeping " + loadtar, run.stderr)
self.assertIn(F"new {datadir} from {savetar}", run.stderr)
self.assertTrue(os.path.isdir(datadir))
self.assertFalse(os.path.isfile(savetar))
self.assertFalse(os.path.isfile(loadtar)) # not packed because no change
self.rm_testdir()
self.save(testname)
def test_283_keep_inputfile(self, docker: Optional[str] = None) -> None:
img = IMG
python = _python
docker = docker or _docker
copyedit = _copyedit(docker)
centos = _centos()
logg.info(": %s : %s", python, img)
testname = self.testname()
testdir = self.testdir()
text_file(os_path(testdir, "Dockerfile"), F"""
FROM {centos}
RUN touch /myinfo.txt
""")
cmd = F"{docker} build {testdir} -t {img}:{testname}"
run = sh(cmd)
logg.info("%s\n%s", run.stdout, run.stderr)
#
cmd = F"{docker} inspect {img}:{testname}"
run = sh(cmd)
data = json.loads(run.stdout)
logg.debug("CONFIG:\n%s", data[0]["Config"])
logg.info(F"{testname} VOLUMES = %s", data[0]["Config"].get("Volumes"))
dat1 = data
#
tempdir = testdir + "/new.tmp"
savename = testname + "x"
cmd = F"{python} {copyedit} -T {tempdir} -kkk FROM {img}:{testname} INTO {img}:{savename} remove label version -vv"
run = sh(cmd)
logg.info("%s\n%s\n%s", cmd, run.stdout, run.stderr)
#
cmd = F"{docker} rmi {img}:{testname} {img}:{savename}"
rmi = sh(cmd)
logg.info("[%s] %s", rmi.returncode, cmd)
#
self.assertEqual(dat1[0]["Config"].get("Volumes"), None)
self.assertIn("there was no label version", run.stderr)
self.assertIn("unchanged image", run.stderr)
self.assertIn("tagged old image", run.stderr)
#
datadir = tempdir + "/data"
savetar = tempdir + "/saved.tar"
loadtar = tempdir + "/ready.tar"
self.assertIn(datadir, run.stderr)
self.assertIn("keeping " + datadir, run.stderr)
self.assertIn("keeping " + savetar, run.stderr)
self.assertNotIn("keeping " + loadtar, run.stderr)
self.assertIn(F"new {datadir} from {savetar}", run.stderr)
self.assertTrue(os.path.isdir(datadir))
self.assertTrue(os.path.isfile(savetar))
self.assertFalse(os.path.isfile(loadtar)) # not packed because no change
self.rm_testdir()
self.save(testname)
def test_284_keep_outputfile(self, docker: Optional[str] = None) -> None:
img = IMG
python = _python
docker = docker or _docker
copyedit = _copyedit(docker)
centos = _centos()
logg.info(": %s : %s", python, img)
testname = self.testname()
testdir = self.testdir()
text_file(os_path(testdir, "Dockerfile"), F"""
FROM {centos}
RUN touch /myinfo.txt
""")
cmd = F"{docker} build {testdir} -t {img}:{testname}"
run = sh(cmd)
logg.info("%s\n%s", run.stdout, run.stderr)
#
cmd = F"{docker} inspect {img}:{testname}"
run = sh(cmd)
data = json.loads(run.stdout)
logg.debug("CONFIG:\n%s", data[0]["Config"])
logg.info(F"{testname} VOLUMES = %s", data[0]["Config"].get("Volumes"))
dat1 = data
#
tempdir = testdir + "/new.tmp"
savename = testname + "x"
cmd = F"{python} {copyedit} -T {tempdir} -kkkk FROM {img}:{testname} INTO {img}:{savename} remove label version -vv"
run = sh(cmd)
logg.info("%s\n%s\n%s", cmd, run.stdout, run.stderr)
#
cmd = F"{docker} rmi {img}:{testname} {img}:{savename}"
rmi = sh(cmd)
logg.info("[%s] %s", rmi.returncode, cmd)
#
self.assertEqual(dat1[0]["Config"].get("Volumes"), None)
self.assertIn("there was no label version", run.stderr)
self.assertIn("unchanged image", run.stderr)
self.assertIn("tagged old image", run.stderr)
#
datadir = tempdir + "/data"
savetar = tempdir + "/saved.tar"
loadtar = tempdir + "/ready.tar"
self.assertIn(datadir, run.stderr)
self.assertIn("keeping " + datadir, run.stderr)
self.assertIn("keeping " + savetar, run.stderr)
self.assertIn("keeping " + loadtar, run.stderr)
self.assertIn(F"new {datadir} from {savetar}", run.stderr)
self.assertTrue(os.path.isdir(datadir))
self.assertTrue(os.path.isfile(savetar))
self.assertFalse(os.path.isfile(loadtar)) # not packed because no change
self.rm_testdir()
self.save(testname)
def test_291_config_keep_datadir(self, docker: Optional[str] = None) -> None:
img = IMG
python = _python
docker = docker or _docker
copyedit = _copyedit(docker)
centos = _centos()
logg.info(": %s : %s", python, img)
testname = self.testname()
testdir = self.testdir()
text_file(os_path(testdir, "Dockerfile"), F"""
FROM {centos}
RUN touch /myinfo.txt
""")
cmd = F"{docker} build {testdir} -t {img}:{testname}"
run = sh(cmd)
logg.info("%s\n%s", run.stdout, run.stderr)
#
cmd = F"{docker} inspect {img}:{testname}"
run = sh(cmd)
data = json.loads(run.stdout)
logg.debug("CONFIG:\n%s", data[0]["Config"])
logg.info(F"{testname} VOLUMES = %s", data[0]["Config"].get("Volumes"))
dat1 = data
#
tempdir = testdir + "/new.tmp"
savename = testname + "x"
cmd = F"{python} {copyedit} -T {tempdir} -c KEEPDATADIR=1 FROM {img}:{testname} INTO {img}:{savename} remove label version -vv"
run = sh(cmd)
logg.info("%s\n%s\n%s", cmd, run.stdout, run.stderr)
#
cmd = F"{docker} rmi {img}:{testname} {img}:{savename}"
rmi = sh(cmd)
logg.info("[%s] %s", rmi.returncode, cmd)
#
self.assertEqual(dat1[0]["Config"].get("Volumes"), None)
self.assertIn("there was no label version", run.stderr)
self.assertIn("unchanged image", run.stderr)
self.assertIn("tagged old image", run.stderr)
#
datadir = tempdir + "/data"
savetar = tempdir + "/saved.tar"
loadtar = tempdir + "/ready.tar"
self.assertIn(datadir, run.stderr)
self.assertIn("keeping " + datadir, run.stderr)
self.assertNotIn("keeping " + savetar, run.stderr)
self.assertNotIn("keeping " + loadtar, run.stderr)
self.assertIn(F"new {datadir} from {docker} save", run.stderr)
self.assertTrue(os.path.isdir(datadir))
self.assertFalse(os.path.isfile(savetar))
self.assertFalse(os.path.isfile(loadtar)) # not packed because no change
# self.assertIn(savetar + " (not created)", run.stderr)
# self.assertIn(loadtar + " (not created)", run.stderr)
self.rm_testdir()
self.save(testname)
def test_292_config_keep_savefile(self, docker: Optional[str] = None) -> None:
img = IMG
python = _python
docker = docker or _docker
copyedit = _copyedit(docker)
centos = _centos()
logg.info(": %s : %s", python, img)
testname = self.testname()
testdir = self.testdir()
text_file(os_path(testdir, "Dockerfile"), F"""
FROM {centos}
RUN touch /myinfo.txt
""")
cmd = F"{docker} build {testdir} -t {img}:{testname}"
run = sh(cmd)
logg.info("%s\n%s", run.stdout, run.stderr)
#
cmd = F"{docker} inspect {img}:{testname}"
run = sh(cmd)
data = json.loads(run.stdout)
logg.debug("CONFIG:\n%s", data[0]["Config"])
logg.info(F"{testname} VOLUMES = %s", data[0]["Config"].get("Volumes"))
dat1 = data
#
tempdir = testdir + "/new.tmp"
savename = testname + "x"
cmd = F"{python} {copyedit} -T {tempdir} -c KEEPSAVEFILE=1 FROM {img}:{testname} INTO {img}:{savename} remove label version -vv"
run = sh(cmd)
logg.info("%s\n%s\n%s", cmd, run.stdout, run.stderr)
#
cmd = F"{docker} rmi {img}:{testname} {img}:{savename}"
rmi = sh(cmd)
logg.info("[%s] %s", rmi.returncode, cmd)
#
self.assertEqual(dat1[0]["Config"].get("Volumes"), None)
self.assertIn("there was no label version", run.stderr)
self.assertIn("unchanged image", run.stderr)
self.assertIn("tagged old image", run.stderr)
#
datadir = tempdir + "/data"
savetar = tempdir + "/saved.tar"
loadtar = tempdir + "/ready.tar"
self.assertIn(datadir, run.stderr)
self.assertNotIn("keeping " + datadir, run.stderr)
self.assertNotIn("keeping " + savetar, run.stderr)
self.assertNotIn("keeping " + loadtar, run.stderr)
self.assertIn(F"new {datadir} from {savetar}", run.stderr)
self.assertFalse(os.path.isdir(datadir))
self.assertFalse(os.path.isfile(savetar))
self.assertFalse(os.path.isfile(loadtar)) # not packed because no change
# self.assertIn(savetar + " (not created)", run.stderr)
# self.assertIn(loadtar + " (not created)", run.stderr)
self.rm_testdir()
self.save(testname)
def test_293_config_keep_inputfile(self, docker: Optional[str] = None) -> None:
img = IMG
python = _python
docker = docker or _docker
copyedit = _copyedit(docker)
centos = _centos()
logg.info(": %s : %s", python, img)
testname = self.testname()
testdir = self.testdir()
text_file(os_path(testdir, "Dockerfile"), F"""
FROM {centos}
RUN touch /myinfo.txt
""")
cmd = F"{docker} build {testdir} -t {img}:{testname}"
run = sh(cmd)
logg.info("%s\n%s", run.stdout, run.stderr)
#
cmd = F"{docker} inspect {img}:{testname}"
run = sh(cmd)
data = json.loads(run.stdout)
logg.debug("CONFIG:\n%s", data[0]["Config"])
logg.info(F"{testname} VOLUMES = %s", data[0]["Config"].get("Volumes"))
dat1 = data
#
tempdir = testdir + "/new.tmp"
savename = testname + "x"
cmd = F"{python} {copyedit} -T {tempdir} -c KEEPINPUTFILE=1 FROM {img}:{testname} INTO {img}:{savename} remove label version -vv"
run = sh(cmd)
logg.info("%s\n%s\n%s", cmd, run.stdout, run.stderr)
#
cmd = F"{docker} rmi {img}:{testname} {img}:{savename}"
rmi = sh(cmd)
logg.info("[%s] %s", rmi.returncode, cmd)
#
self.assertEqual(dat1[0]["Config"].get("Volumes"), None)
self.assertIn("there was no label version", run.stderr)
self.assertIn("unchanged image", run.stderr)
self.assertIn("tagged old image", run.stderr)
#
datadir = tempdir + "/data"
savetar = tempdir + "/saved.tar"
loadtar = tempdir + "/ready.tar"
self.assertIn(datadir, run.stderr)
self.assertNotIn("keeping " + datadir, run.stderr)
self.assertIn("keeping " + savetar, run.stderr)
self.assertNotIn("keeping " + loadtar, run.stderr)
self.assertIn(F"new {datadir} from {docker} save", run.stderr)
self.assertFalse(os.path.isdir(datadir))
self.assertFalse(os.path.isfile(savetar)) # was not created
self.assertFalse(os.path.isfile(loadtar)) # not packed because no change
self.assertIn(savetar + " (not created)", run.stderr)
# self.assertIn(loadtar + " (not created)", run.stderr)
self.rm_testdir()
self.save(testname)
def test_294_config_keep_outputfile(self, docker: Optional[str] = None) -> None:
img = IMG
python = _python
docker = docker or _docker
copyedit = _copyedit(docker)
centos = _centos()
logg.info(": %s : %s", python, img)
testname = self.testname()
testdir = self.testdir()
text_file(os_path(testdir, "Dockerfile"), F"""
FROM {centos}
RUN touch /myinfo.txt