Add basic test harness

This commit is contained in:
Thomas Orozco 2015-02-22 23:45:26 -08:00
parent ec2f701835
commit fb689766ca
5 changed files with 105 additions and 0 deletions

View File

@ -10,6 +10,9 @@ $(BIN): $(OBJ)
$(OBJ):
check:
python test/test.py
install: all
mkdir -p $(DESTDIR)$(PREFIX)/bin
cp -f $(BIN) $(DESTDIR)$(PREFIX)/bin

25
test/reaping/stage_1.py Executable file
View File

@ -0,0 +1,25 @@
#!/usr/bin/env python3
import os
import subprocess
import time
if __name__ == "__main__":
p = subprocess.Popen([os.path.join(os.path.dirname(__file__), "stage_2.py")])
p.wait()
# These are the only PIDs that should remain if the system is well-behaved:
# - This process
# - Init
expected_pids = [1, os.getpid()]
print("Expecting pids to remain: {0}".format(", ".join(str(pid) for pid in expected_pids)))
while 1:
pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
print("Has pids: {0}".format(", ".join(pids)))
if set(int(pid) for pid in pids) == set(expected_pids):
break
time.sleep(1)

11
test/reaping/stage_2.py Executable file
View File

@ -0,0 +1,11 @@
#!/usr/bin/env python3
from __future__ import print_function
import subprocess
import os
if __name__ == "__main__":
# Spawn lots of process
for i in range(0, 10):
cmd = ["sleep", str(1 + i % 2)]
proc = subprocess.Popen(cmd)

6
test/signals/test.py Executable file
View File

@ -0,0 +1,6 @@
#!/usr/bin/env python3
import time
if __name__ == "__main__":
while 1:
time.sleep(10)

60
test/test.py Normal file
View File

@ -0,0 +1,60 @@
#coding:utf-8
import os
import time
import subprocess
import threading
class Command(object):
def __init__(self, cmd, post_cmd=None, post_delay=None):
self.cmd = cmd
self.post_cmd = post_cmd
self.post_delay = post_delay
self._process = None
def run(self, timeout):
def target():
self._process = subprocess.Popen(self.cmd)
self._process.communicate()
thread = threading.Thread(target=target)
thread.start()
if self.post_cmd is not None:
if self.post_delay is not None:
time.sleep(self.post_delay)
subprocess.check_call(self.post_cmd)
thread.join(timeout)
if thread.is_alive():
raise Exception("Test failed!")
if __name__ == "__main__":
root = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
# Tests rely on exiting fast enough (exiting at all, in fact).
base_cmd = [
"docker",
"run",
"-it",
"--rm",
"--name=tini-test",
"-v",
"{0}:{0}".format(root),
"ubuntu",
"{0}/tini".format(root),
"-vvv",
"--",
]
# Reaping test
Command(base_cmd + ["/Users/thomas/dev/tini/test/reaping/stage_1.py"]).run(timeout=10)
# Signals test
for sig in ["INT", "TERM"]:
Command(
base_cmd + ["/Users/thomas/dev/tini/test/signals/test.py"],
["docker", "kill", "-s", sig, "tini-test"],
2
).run(timeout=10)