class TestSzig(unittest.TestCase): def setUp(self): self.szig = SZIG("", HandlerMock) def test_get_value(self): self.assertEquals(self.szig.get_value(""), None) self.assertEquals(self.szig.get_value("service"), None) self.assertEquals(self.szig.get_value("info.policy.file"), "/etc/zorp/policy.py") self.assertEquals(self.szig.get_value("stats.thread_number"), 5) self.assertEquals( self.szig.get_value( "service.service_http_transparent.sessions_running"), 0) def test_get_sibling(self): self.assertEquals(self.szig.get_sibling("conns"), "info") self.assertEquals(self.szig.get_sibling("stats.threads_running"), "stats.thread_rate_max") self.assertEquals(self.szig.get_sibling("stats.thread_rate_max"), "stats.audit_number") self.assertEquals(self.szig.get_sibling("stats.thread_number"), None) def test_get_child(self): self.assertEquals(self.szig.get_child(""), "conns") self.assertEquals(self.szig.get_child("info"), "info.policy") self.assertEquals(self.szig.get_child("info.policy"), "info.policy.reload_stamp") self.assertEquals(self.szig.get_child("info.policy.reload_stamp"), None) def test_get_set_loglevel(self): loglevel = 6 self.szig.loglevel = loglevel self.assertEquals(self.szig.loglevel, loglevel) def test_get_set_logspec(self): logspec = "this is a logspec" self.szig.logspec = logspec self.assertEquals(self.szig.logspec, logspec) def test_get_set_deadlockcheck(self): deadlockcheck = False self.szig.deadlockcheck = deadlockcheck self.assertEquals(self.szig.deadlockcheck, deadlockcheck) def test_reload_and_reload_result(self): self.szig.reload() self.assertEquals(self.szig.reload_result(), True) def test_coredump(self): try: self.szig.coredump() self.assertTrue( False, "szig coredump should not work while not repaired") except: self.assertTrue(True, "szig coredump is not working yet")
class TestSzig(unittest.TestCase): def setUp(self): self.szig = SZIG("", HandlerMock) def test_get_value(self): self.assertEquals(self.szig.get_value(""), None) self.assertEquals(self.szig.get_value("service"), None) self.assertEquals(self.szig.get_value("info.policy.file"), "/etc/zorp/policy.py") self.assertEquals(self.szig.get_value("stats.thread_number"), 5) self.assertEquals(self.szig.get_value("service.service_http_transparent.sessions_running"), 0) def test_get_sibling(self): self.assertEquals(self.szig.get_sibling("conns"), "info") self.assertEquals(self.szig.get_sibling("stats.threads_running"), "stats.thread_rate_max") self.assertEquals(self.szig.get_sibling("stats.thread_rate_max"), "stats.audit_number") self.assertEquals(self.szig.get_sibling("stats.thread_number"), None) def test_get_child(self): self.assertEquals(self.szig.get_child(""), "conns") self.assertEquals(self.szig.get_child("info"), "info.policy") self.assertEquals(self.szig.get_child("info.policy"), "info.policy.reload_stamp") self.assertEquals(self.szig.get_child("info.policy.reload_stamp"), None) def test_get_set_loglevel(self): loglevel = 6 self.szig.loglevel = loglevel self.assertEquals(self.szig.loglevel, loglevel) def test_get_set_logspec(self): logspec = "this is a logspec" self.szig.logspec = logspec self.assertEquals(self.szig.logspec, logspec) def test_get_set_deadlockcheck(self): deadlockcheck = False self.szig.deadlockcheck = deadlockcheck self.assertEquals(self.szig.deadlockcheck, deadlockcheck) def test_reload_and_reload_result(self): self.szig.reload() self.assertEquals(self.szig.reload_result(), True) def test_coredump(self): try: self.szig.coredump() self.assertTrue(False, "szig coredump should not work while not repaired") except: self.assertTrue(True, "szig coredump is not working yet")
class GUIStatusAlgorithm(ProcessAlgorithm): def __init__(self): super(GUIStatusAlgorithm, self).__init__() def errorHandling(self): running = self.isRunning(self.instance.process_name) if not running: self.status.pid = "missing" self.status.running = "missing" self.status.threads_running = "missing" self.status.thread_number = "missing" self.status.thread_rate_avg1 = "missing" self.status.thread_rate_avg5 = "missing" self.status.thread_rate_avg15 = "missing" return self.status try: self.szig = SZIG(self.instance.process_name) except IOError as e: return CommandResultFailure(e.message) def gui_status(self): self.status.pid = self.getProcessPid(self.instance.process_name) self.status.running = "running" self.status.threads_running = self.szig.get_value( 'stats.threads_running') self.status.thread_number = self.szig.get_value('stats.thread_number') self.status.thread_rate_avg1 = self.szig.get_value( 'stats.thread_rate_avg1') self.status.thread_rate_avg5 = self.szig.get_value( 'stats.thread_rate_avg5') self.status.thread_rate_avg15 = self.szig.get_value( 'stats.thread_rate_avg15') return self.status def execute(self): self.status = GUIStatus(self.instance.process_name) error = self.errorHandling() if error != None: return error try: return self.gui_status() except SZIGError as e: return CommandResultFailure( "error while communicating through szig: %s" % e.msg)
class SzigWalkAlgorithm(ProcessAlgorithm): def __init__(self, root=""): self.root = root super(SzigWalkAlgorithm, self).__init__() def errorHandling(self): running = self.isRunning(self.instance.process_name) if not running: # Ignore not running process return CommandResultSuccess(running.msg) try: self.szig = SZIG(self.instance.process_name) except IOError as e: return CommandResultFailure(e.message) return None def getChilds(self, node): child = self.szig.get_child(node) if child: result = {} result[child.split('.')[-1]] = self.walk(child) sibling = self.szig.get_sibling(child) while sibling: result[sibling.split('.')[-1]] = self.walk(sibling) sibling = self.szig.get_sibling(sibling) return result else: return None def walk(self, node): value = self.szig.get_value(node) if value != None: return value else: return self.getChilds(node) def execute(self): def _prepend_instance_name(tree): return {self.instance.process_name: tree} error = self.errorHandling() if error != None: return error try: if self.root: szig_dict = _prepend_instance_name( {self.root: self.walk(self.root)}) else: szig_dict = _prepend_instance_name(self.walk(self.root)) return CommandResultSuccess(value=szig_dict) except SZIGError as e: return CommandResultFailure( 'Error while communicating through szig: ' + e.msg)
class GUIStatusAlgorithm(ProcessAlgorithm): def __init__(self): super(GUIStatusAlgorithm, self).__init__() def errorHandling(self): running = self.isRunning(self.instance.process_name) if not running: self.status.pid = "missing" self.status.running = "missing" self.status.threads_running = "missing" self.status.thread_number = "missing" self.status.thread_rate_avg1 = "missing" self.status.thread_rate_avg5 = "missing" self.status.thread_rate_avg15 = "missing" return self.status try: self.szig = SZIG(self.instance.process_name) except IOError as e: return CommandResultFailure(e.message) def gui_status(self): self.status.pid = self.getProcessPid(self.instance.process_name) self.status.running = "running" self.status.threads_running = self.szig.get_value('stats.threads_running') self.status.thread_number = self.szig.get_value('stats.thread_number') self.status.thread_rate_avg1 = self.szig.get_value('stats.thread_rate_avg1') self.status.thread_rate_avg5 = self.szig.get_value('stats.thread_rate_avg5') self.status.thread_rate_avg15 = self.szig.get_value('stats.thread_rate_avg15') return self.status def execute(self): self.status = GUIStatus(self.instance.process_name) error = self.errorHandling() if error != None: return error try: return self.gui_status() except SZIGError as e: return CommandResultFailure("error while communicating through szig: %s" % e.msg)
class SzigWalkAlgorithm(ProcessAlgorithm): def __init__(self, root=""): self.root = root super(SzigWalkAlgorithm, self).__init__() def errorHandling(self): running = self.isRunning(self.instance.process_name) if not running: return running try: self.szig = SZIG(self.instance.process_name) except IOError as e: return CommandResultFailure(e.message) return None def getChilds(self, node): child = self.szig.get_child(node) if child: result = {} result[child.split('.')[-1]] = self.walk(child) sibling = self.szig.get_sibling(child) while sibling: result[sibling.split('.')[-1]] = self.walk(sibling) sibling = self.szig.get_sibling(sibling) return result else: return None def walk(self, node): value = self.szig.get_value(node) if value != None: return value else: return self.getChilds(node) def execute(self): def _prepend_instance_name(tree): return {self.instance.process_name : tree} error = self.errorHandling() if error != None: return error try: if self.root: szig_dict = _prepend_instance_name({self.root : self.walk(self.root)}) else: szig_dict = _prepend_instance_name(self.walk(self.root)) return CommandResultSuccess("", szig_dict) except SZIGError as e: return CommandResultFailure("error while communicating through szig: %s" % e.msg)
class StatusAlgorithm(ProcessAlgorithm): def __init__(self): super(StatusAlgorithm, self).__init__() def errorHandling(self): running = self.isRunning(self.instance.process_name) if not running: return running try: self.szig = SZIG(self.instance.process_name) except IOError as e: return CommandResultFailure(e.message) def status(self): status = ProcessStatus(self.instance.process_name) status.pid = self.getProcessPid(self.instance.process_name) status.threads = int(self.szig.get_value('stats.threads_running')) status.policy_file = self.szig.get_value('info.policy.file') status.timestamp_szig = self.szig.get_value('info.policy.file_stamp') status.reload_timestamp = self.szig.get_value( 'info.policy.reload_stamp') status.timestamp_os = os.path.getmtime(status.policy_file) status.reloaded = str(status.timestamp_szig) == str( status.timestamp_os).split('.')[0] return status def execute(self): error = self.errorHandling() if error != None: return error try: return self.status() except SZIGError as e: return CommandResultFailure( "error while communicating through szig: %s" % e.msg)
class StatusAlgorithm(ProcessAlgorithm): def __init__(self): super(StatusAlgorithm, self).__init__() def errorHandling(self): running = self.isRunning(self.instance.process_name) if not running: return running try: self.szig = SZIG(self.instance.process_name) except IOError as e: return CommandResultFailure(e.strerror) def status(self): status = ProcessStatus(self.instance.process_name) status.pid = self.getProcessPid(self.instance.process_name) status.threads = int(self.szig.get_value('stats.threads_running')) status.policy_file = self.szig.get_value('info.policy.file') status.timestamp_szig = self.szig.get_value('info.policy.file_stamp') status.reload_timestamp = self.szig.get_value('info.policy.reload_stamp') status.timestamp_os = os.path.getmtime(status.policy_file) status.reloaded = str(status.timestamp_szig) == str(status.timestamp_os).split('.')[0] return status def execute(self): error = self.errorHandling() if error != None: return error try: return self.status() except SZIGError as e: return CommandResultFailure("error while communicating through szig: %s" % e.msg)