aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAnthony G. Basile <blueness@gentoo.org>2015-06-30 22:15:35 -0400
committerAnthony G. Basile <blueness@gentoo.org>2015-06-30 22:15:35 -0400
commit8885d6a5b9035f2eb0c02e697dc0bfb45c8b5a7f (patch)
tree165b052003e580934854a5e9da1182cde1312d62 /tests
downloadgrss-8885d6a5b9035f2eb0c02e697dc0bfb45c8b5a7f.tar.gz
grss-8885d6a5b9035f2eb0c02e697dc0bfb45c8b5a7f.tar.bz2
grss-8885d6a5b9035f2eb0c02e697dc0bfb45c8b5a7f.zip
Initial commit.
Diffstat (limited to 'tests')
-rwxr-xr-xtests/test-constants.py37
-rwxr-xr-xtests/test-daemon.py91
-rwxr-xr-xtests/test-log.py47
-rwxr-xr-xtests/test-mountdirectories.py56
-rwxr-xr-xtests/test-tarit.py23
5 files changed, 254 insertions, 0 deletions
diff --git a/tests/test-constants.py b/tests/test-constants.py
new file mode 100755
index 0000000..1327671
--- /dev/null
+++ b/tests/test-constants.py
@@ -0,0 +1,37 @@
+#!/usr/bin/python
+
+import os
+import sys
+sys.path.append(os.path.abspath('..'))
+
+from grs import CONST
+
+if __name__ == "__main__":
+ # Test that you can't change or delete an already defined constant
+ original = CONST.WORKDIR
+ CONST.WORKDIR = 'not original'
+ assert CONST.WORKDIR == original
+ del CONST.WORKDIR
+ assert CONST.WORKDIR == original
+
+ # Test that a non existant constant is None
+ assert CONST.I_DONT_EXIST == None
+
+ # Teat that you can add new constants
+ original = 'new value'
+ CONST.I_AM_NEW = original
+ assert CONST.I_AM_NEW == original
+ CONST.I_AM_NEW = 'not original'
+ assert CONST.I_AM_NEW == original
+ del CONST.I_AM_NEW
+ assert CONST.I_AM_NEW == original
+
+ print(CONST.nameservers)
+ print(CONST.repo_uris)
+ print(CONST.stage_uris)
+ print(CONST.names)
+ print(CONST.libdirs)
+ print(CONST.logfiles)
+ print(CONST.workdirs)
+ print(CONST.kernelroots)
+ print(CONST.portage_configroots)
diff --git a/tests/test-daemon.py b/tests/test-daemon.py
new file mode 100755
index 0000000..27300e2
--- /dev/null
+++ b/tests/test-daemon.py
@@ -0,0 +1,91 @@
+#!/usr/bin/python
+
+import os
+import sys
+sys.path.append(os.path.abspath('..'))
+
+import signal
+import time
+from grs import Daemon, Execute
+
+class MyDaemon(Daemon):
+ def run(self):
+ def handler(signum, frame):
+ if signum == signal.SIGHUP:
+ with open('/tmp/daemon.hup', 'a') as f:
+ f.write('%s\n' % self.value)
+ if signum == signal.SIGTERM:
+ with open('/tmp/daemon.term', 'a') as f:
+ f.write('%s\n' % self.value)
+ sys.exit(0)
+ signal.signal(signal.SIGHUP, handler)
+ signal.signal(signal.SIGTERM, handler)
+ while True: time.sleep(1)
+
+if __name__ == "__main__":
+ mypid1 = '/tmp/daemon1.pid'
+ mypid2 = '/tmp/daemon2.pid'
+
+ daemon1 = MyDaemon(mypid1, value='test1')
+ daemon2 = MyDaemon(mypid2, value='test2')
+
+ cgroupdir = '/sys/fs/cgroup'
+ grs_cgroup = 'grs'
+ grs_cgroupdir = os.path.join(cgroupdir, grs_cgroup)
+
+ os.makedirs(grs_cgroupdir, mode=0o755, exist_ok=True)
+ if not os.path.ismount(grs_cgroupdir):
+ cmd = 'mount -t cgroup -o none,name=grs grs %s' % grs_cgroupdir
+ Execute(cmd)
+
+ subcgroup = 'test-daemon'
+ subcgroupdir = os.path.join(grs_cgroupdir, subcgroup)
+ os.makedirs(subcgroupdir, exist_ok=True)
+
+ cmd = 'cgclassify -g name=%s:/%s %d' % (grs_cgroup, subcgroup, os.getpid())
+ Execute(cmd)
+
+ if len(sys.argv) != 2:
+ print('%s [start1 start2 startb stop1 stop2 restart1 restart2 pids killall]' % sys.argv[0])
+ sys.exit(1)
+
+ if 'start1' == sys.argv[1]:
+ daemon1.start()
+ elif 'start2' == sys.argv[1]:
+ daemon2.start()
+ elif 'startb' == sys.argv[1]:
+ if not os.fork():
+ daemon1.start()
+ elif not os.fork():
+ daemon2.start()
+ elif 'stop1' == sys.argv[1]:
+ daemon1.stop()
+ elif 'stop2' == sys.argv[1]:
+ daemon2.stop()
+ elif 'restart1' == sys.argv[1]:
+ daemon1.restart()
+ elif 'restart2' == sys.argv[1]:
+ daemon2.restart()
+ elif 'pids' == sys.argv[1]:
+ try:
+ print('daemon1:\n%s' % open(mypid1, 'r').read())
+ except FileNotFoundError:
+ pass
+ try:
+ print('daemon2:\n%s' % open(mypid2, 'r').read())
+ except FileNotFoundError:
+ pass
+ try:
+ print('cgroup:\n%s' % open(os.path.join(subcgroupdir, 'tasks'), 'r').read())
+ except FileNotFoundError:
+ pass
+ elif 'killall' == sys.argv[1]:
+ with open(os.path.join(subcgroupdir, 'tasks'), 'r') as f:
+ for p in f.readlines():
+ pd = int(p.strip())
+ if pd == os.getpid():
+ continue
+ os.kill(pd, signal.SIGTERM)
+ else:
+ print("Unknown command")
+ sys.exit(2)
diff --git a/tests/test-log.py b/tests/test-log.py
new file mode 100755
index 0000000..7f19b58
--- /dev/null
+++ b/tests/test-log.py
@@ -0,0 +1,47 @@
+#!/usr/bin/python
+
+import os
+import sys
+sys.path.append(os.path.abspath('..'))
+
+import hashlib
+import shutil
+from grs import Log
+
+logdir = '/tmp/test-log'
+
+def doit(stamped = False):
+ try:
+ shutil.rmtree(logdir)
+ except FileNotFoundError:
+ pass
+ os.makedirs(logdir)
+ logfile = os.path.join(logdir, 'test.log')
+
+ lo = Log(logfile)
+ for i in range(10):
+ lo.log('first %d' % i, stamped)
+
+ lo.rotate_logs()
+ lo.rotate_logs()
+ lo.rotate_logs()
+ for i in list(range(9,-1,-1)):
+ lo.log('second %d' % i, stamped)
+
+
+def hashtest(expect_pass = True):
+ m = hashlib.md5()
+ for i in [ '', '.0', '.1', '.2']:
+ log = os.path.join(logdir, 'test.log%s' % i)
+ with open(log, 'r') as f:
+ m.update(f.read().encode('ascii'))
+ if expect_pass:
+ assert(m.hexdigest() == '485b8bf3a9e08bd5ccfdff7e1a8fe4e1')
+ else:
+ assert(m.hexdigest() != '485b8bf3a9e08bd5ccfdff7e1a8fe4e1')
+
+if __name__ == "__main__":
+ doit(stamped=False)
+ hashtest(expect_pass=True)
+ doit(stamped=True)
+ hashtest(expect_pass=False)
diff --git a/tests/test-mountdirectories.py b/tests/test-mountdirectories.py
new file mode 100755
index 0000000..cf8482a
--- /dev/null
+++ b/tests/test-mountdirectories.py
@@ -0,0 +1,56 @@
+#!/usr/bin/python
+
+import os
+import sys
+sys.path.append(os.path.abspath('..'))
+
+from grs import Execute
+from grs import MountDirectories
+
+if __name__ == "__main__":
+
+ package = '/tmp/test-package'
+ try:
+ os.makedirs(package)
+ except FileExistsError:
+ pass
+ empty_file = os.path.join(package, 'empty')
+ open(empty_file, 'a').close()
+
+ configroot = '/tmp/test-mountdirectories'
+ directories = [ 'dev', 'dev/pts', 'dev/shm', 'proc', 'sys', 'usr/portage', 'usr/portage/packages' ]
+ for d in directories:
+ try:
+ os.makedirs(os.path.join(configroot, d))
+ except FileExistsError:
+ pass
+ alt_empty_file = os.path.join(configroot, 'usr/portage/packages/empty')
+
+ md = MountDirectories(portage_configroot=configroot, package=package, logfile='/dev/null')
+
+ md.umount_all()
+ some_mounted, all_mounted = md.are_mounted()
+ assert(some_mounted == False)
+ assert(all_mounted == False)
+
+ md.mount_all()
+ some_mounted, all_mounted = md.are_mounted()
+ assert(some_mounted == True)
+ assert(all_mounted == True)
+
+ # /tmp/test-package/aaa and /tmp/test-mountdirectories/usr/portage/packages/empty exist
+ assert(os.path.isfile(alt_empty_file) == True)
+ Execute('umount --force %s' % os.path.dirname(alt_empty_file))
+ some_mounted, all_mounted = md.are_mounted()
+ assert(some_mounted == True)
+ assert(all_mounted == False)
+ # /tmp/test-mountdirectories/usr/portage/packages/empty doesn't exist anymore
+ assert(os.path.isfile(alt_empty_file) == False)
+
+ assert(md.ismounted(package) == False)
+ assert(md.ismounted(os.path.join(configroot, 'dev')) == True)
+
+ md.umount_all()
+ some_mounted, all_mounted = md.are_mounted()
+ assert(some_mounted == False)
+ assert(all_mounted == False)
diff --git a/tests/test-tarit.py b/tests/test-tarit.py
new file mode 100755
index 0000000..c3369c1
--- /dev/null
+++ b/tests/test-tarit.py
@@ -0,0 +1,23 @@
+#!/usr/bin/env python
+
+import os
+import sys
+sys.path.append(os.path.abspath('..'))
+
+from grs import TarIt
+
+if __name__ == "__main__":
+ package = '/tmp/test-tarit'
+ try:
+ os.makedirs(package)
+ except FileExistsError:
+ pass
+ for i in range(10):
+ empty_file = os.path.join(package,'empty-%d' % i)
+ with open(empty_file, 'w') as f:
+ f.write('%d\n' % i)
+ f.close()
+
+ bi = TarIt('tar-test', portage_configroot=package, logfile='/dev/null')
+ bi.tarit()
+ bi.hashit()