-
Notifications
You must be signed in to change notification settings - Fork 6
/
dockerdir.tests.py
executable file
·143 lines (133 loc) · 4.8 KB
/
dockerdir.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
#! /usr/bin/python3
"""
You can just call tests by their number, or a common prefix thereof.
(so that "./testsuite.py -v test_107" will run the tests from test_1070 to test_1079).
"""
from typing import Union, Optional, List, cast
import unittest
import sys
import os
import subprocess
import inspect
from fnmatch import fnmatchcase as fnmatch
import logging
logg = logging.getLogger("TEST")
PYTHON = "python3"
SCRIPT = "dockerdir.py"
string_types = str
xrange = range
def make_file(name: str, content: str) -> None:
with open(name, "w") as f:
f.write(content)
def drop_file(name: str) -> None:
if os.path.exists(name):
os.remove(name)
def decodes(text: Union[str, bytes]) -> 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 sh____(cmd: Union[str, List[str]], shell: bool = True) -> int:
if isinstance(cmd, string_types):
logg.debug(": %s", cmd)
else:
logg.debug(": %s", " ".join(["'%s'" % item for item in cmd]))
return subprocess.check_call(cmd, shell=shell)
def sx____(cmd: Union[str, List[str]], shell: bool = True) -> int:
if isinstance(cmd, string_types):
logg.debug(": %s", cmd)
else:
logg.debug(": %s", " ".join(["'%s'" % item for item in cmd]))
return subprocess.call(cmd, shell=shell)
def output(cmd: Union[str, List[str]], shell: bool = True) -> str:
if isinstance(cmd, string_types):
logg.debug(": %s", cmd)
else:
logg.debug(": %s", " ".join(["'%s'" % item for item in cmd]))
run = subprocess.Popen(cmd, shell=shell, stdout=subprocess.PIPE)
out, err = run.communicate()
return decodes(out)
def get_caller_name() -> str:
currentframe = inspect.currentframe()
if not currentframe:
return "global"
if not currentframe.f_back:
frame = currentframe
elif not currentframe.f_back.f_back:
frame = currentframe.f_back
else:
frame = currentframe.f_back.f_back
return frame.f_code.co_name
def get_caller_caller_name() -> str:
currentframe = inspect.currentframe()
if not currentframe:
return "global"
if not currentframe.f_back:
frame = currentframe
elif not currentframe.f_back.f_back:
frame = currentframe.f_back
elif not currentframe.f_back.f_back.f_back:
frame = currentframe.f_back.f_back
else:
frame = currentframe.f_back.f_back.f_back
return frame.f_code.co_name
class DockerDirScriptsTest(unittest.TestCase):
def caller_testname(self) -> str:
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: Optional[str] = None) -> str:
name = self.caller_testname()
if suffix:
return name + "_" + suffix
return name
def test_001_hello(self) -> None:
print("... starting the testsuite ...")
logg.info("starting the testsuite ...")
def test_100(self) -> None:
python = PYTHON
script = SCRIPT
testname = self.testname()
filename = testname + ".tmp"
make_file(filename,
"""FROM centos:8.3.2011
ENV foo=1
ENV _commit test_100""")
sh____("{python} {script} {filename}".format(**locals()))
self.assertTrue(os.path.isfile("docker.tmp/{testname}/build.sh".format(**locals())))
drop_file(filename)
if __name__ == "__main__":
from optparse import OptionParser
_o = OptionParser("%prog [options] test*",
epilog=__doc__.strip().split("\n")[0])
_o.add_option("--failfast", action="store_true", default=False,
help="Stop the test run on the first error or failure. [%default]")
_o.add_option("-v", "--verbose", action="count", default=0,
help="increase logging level [%default]")
opt, args = _o.parse_args()
logging.basicConfig(level=logging.WARNING - opt.verbose * 5)
# unittest.main()
suite = unittest.TestSuite()
if not args: args = ["test_*"]
for arg in args:
for classname in sorted(globals()):
if not classname.endswith("Test"):
continue
testclass = globals()[classname]
for method in sorted(dir(testclass)):
if "*" not in arg: arg += "*"
if fnmatch(method, arg):
suite.addTest(testclass(method))
Runner = unittest.TextTestRunner
result = Runner(verbosity=opt.verbose, failfast=opt.failfast).run(suite)
if not result.wasSuccessful():
sys.exit(1)