# test_remctl.py -- Test suite for remctl Python bindings # # Written by Russ Allbery # Copyright 2008, 2011-2012, 2014 # The Board of Trustees of the Leland Stanford Junior University # # SPDX-License-Identifier: MIT import remctl import errno, os, re, signal, time, unittest def needs_kerberos(func): """unittest test method decorator to skip tests requiring Kerberos Used to annotate test methods that require a Kerberos configuration. Ignores failures in the annotated test method. """ def wrapper(*args, **kw): if not os.path.isfile('config/principal'): return True else: return func(*args, **kw) wrapper.__name__ = func.__name__ wrapper.__doc__ = func.__doc__ return wrapper class TestRemctl(unittest.TestCase): def get_principal(self): file = open('config/principal', 'r') principal = file.read().rstrip() file.close() return principal @needs_kerberos def start_remctld(self): try: os.mkdir('tmp') os.remove('tmp/pid') except OSError, (error, strerror): if error != errno.ENOENT and error != errno.EEXIST: raise principal = self.get_principal() child = os.fork() if child == 0: output = open('tmp/output', 'w') os.dup2(output.fileno(), 1) os.dup2(output.fileno(), 2) os.chdir('@abs_top_srcdir@/tests') os.execl('@abs_top_builddir@/server/remctld', 'remctld', '-m', '-p', '14373', '-s', principal, '-f', 'data/conf-simple', '-P', '@abs_top_builddir@/tests/tmp/pid', '-d', '-S', '-F', '-k', '@abs_top_builddir@/tests/config/keytab') if not os.path.isfile('tmp/pid'): time.sleep(1) def stop_remctld(self): try: file = open('tmp/pid', 'r') pid = file.read().rstrip() file.close() os.kill(int(pid), signal.SIGTERM) child, status = os.waitpid(int(pid), 0) os.remove('tmp/pid') except IOError, (error, strerror): if error != errno.ENOENT: raise except OSError, (error, strerror): if error != errno.ENOENT: raise @needs_kerberos def run_kinit(self): os.environ['KRB5CCNAME'] = 'tmp/krb5cc_test' self.principal = self.get_principal() commands = ( 'kinit --no-afslog -k -t config/keytab ' + self.principal, 'kinit -k -t config/keytab ' + self.principal, 'kinit -t config/keytab ' + self.principal, 'kinit -k -K config/keytab ' + self.principal) for command in commands: if os.system(command + ' >/dev/null 2>&1