def test_handle_logfile_watch(self): sock = FakeSocket(socket.AF_INET, socket.SOCK_STREAM) self.useFixture(fixtures.MonkeyPatch('time.sleep', self.fake_sleep)) with utils.temporary_directory() as path: with utils.temporary_directory() as logdir: om = output_manager.OutputManager(path) om.config = {'main': {'trans_log_dir': logdir}} with open(os.path.join(logdir, 'trans_42.log'), 'w') as f: f.write('This\nis\na\nlog\nfile') self.sleep_count = 0 self.sleep_writes_to = f out = om.handle_logfile( {'action': 'logfile.watch', 'payload': {'task_id': '42', 'dest_ip': '127.0.0.1', 'dest_port': 4242, 'timeout': 10}}, sock=sock) self.assertEqual(out['result_code'], 0) # NOTE(mikal): this one is a little complicated. The # timeout is from the last bit of seen data... So, because # we return data for the first ten sleeps, we expect to # sleep 20 times before we timeout. self.assertEqual(self.sleep_count, 20) self.assertNotEqual(sock.sent, []) self.assertEqual(len(sock.sent), 10)
def test_handle_logfile_watch(self): sock = FakeSocket(socket.AF_INET, socket.SOCK_STREAM) self.useFixture(fixtures.MonkeyPatch("time.sleep", self.fake_sleep)) with utils.temporary_directory() as path: with utils.temporary_directory() as logdir: om = output_manager.OutputManager(path) om.config = {"main": {"trans_log_dir": logdir}} with open(os.path.join(logdir, "trans_42.log"), "w") as f: f.write("This\nis\na\nlog\nfile") self.sleep_count = 0 self.sleep_writes_to = f out = om.handle_logfile( { "action": "logfile.watch", "payload": {"task_id": "42", "dest_ip": "127.0.0.1", "dest_port": 4242, "timeout": 10}, }, sock=sock, ) self.assertEqual(out["result_code"], 0) # NOTE(mikal): this one is a little complicated. The # timeout is from the last bit of seen data... So, because # we return data for the first ten sleeps, we expect to # sleep 20 times before we timeout. self.assertEqual(self.sleep_count, 20) self.assertNotEqual(sock.sent, []) self.assertEqual(len(sock.sent), 10)
def test_handle_logfile_no_such_logfile(self): with utils.temporary_directory() as path: with utils.temporary_directory() as logdir: om = output_manager.OutputManager(path) om.config = {"main": {"trans_log_dir": logdir}} out = om.handle_logfile( {"action": "modules.load", "payload": {"task_id": "42", "dest_ip": "127.0.0.1", "dest_port": 4242}} ) self.assertEqual(out["result_code"], 1) self.assertEqual(out["result_str"], "no such transaction log file")
def test_handle_logfile_remote_connection_failed(self): with utils.temporary_directory() as path: with utils.temporary_directory() as logdir: om = output_manager.OutputManager(path) om.config = {"main": {"trans_log_dir": logdir}} with open(os.path.join(logdir, "trans_42.log"), "w") as f: f.write("This\nis\na\nlog\nfile") out = om.handle_logfile( {"action": "logfile.tail", "payload": {"task_id": "42", "dest_ip": "127.0.0.1", "dest_port": 4242}} ) self.assertEqual(out["result_code"], 1)
def test_handle_logfile_no_such_logfile(self): with utils.temporary_directory() as path: with utils.temporary_directory() as logdir: om = output_manager.OutputManager(path) om.config = {'main': {'trans_log_dir': logdir}} out = om.handle_logfile({'action': 'modules.load', 'payload': {'task_id': '42', 'dest_ip': '127.0.0.1', 'dest_port': 4242, 'offset': 1024}}) self.assertEqual(out['result_code'], 1) self.assertEqual(out['result_str'], 'no such transaction log file')
def test_handle_logfile_remote_connection_failed(self): with utils.temporary_directory() as path: with utils.temporary_directory() as logdir: om = output_manager.OutputManager(path) om.config = {'main': {'trans_log_dir': logdir}} with open(os.path.join(logdir, 'trans_42.log'), 'w') as f: f.write('This\nis\na\nlog\nfile') out = om.handle_logfile({'action': 'logfile.tail', 'payload': {'task_id': '42', 'dest_ip': '127.0.0.1', 'dest_port': 4242, 'offset': 1024}}) self.assertEqual(out['result_code'], 1)
def test_read_config_with_included_directory(self): agent = OpenCenterAgentNoInitialization([]) with utils.temporary_directory() as path: config_file = os.path.join(path, 'config') included_dir = os.path.join(path, 'included') with open(config_file, 'w') as f: f.write("""[taskerator] endpoint = http://127.0.0.1:8080/admin original = foo include_dir = %s""" % included_dir) agent.config_section = 'taskerator' self.assertRaises(RuntimeError, agent._read_config, config_file) os.mkdir(included_dir) with open(os.path.join(included_dir, 'banana'), 'w') as f: f.write("""[taskerator] endpoint = notthis""") config = agent._read_config(config_file) self.assertEquals(config['taskerator']['endpoint'], 'http://127.0.0.1:8080/admin') with open(os.path.join(included_dir, 'foo.conf'), 'w') as f: f.write("""[taskerator] endpoint = butthis""") config = agent._read_config(config_file) self.assertEquals(config['taskerator']['endpoint'], 'butthis') self.assertEquals(config['taskerator']['original'], 'foo')
def test_register_action_duplicate(self): with utils.temporary_directory() as path: om = output_manager.OutputManager(path) om.dispatch_table['action'] = {'short_path': 'shortpath', 'method': 'method'} self.assertRaises(NameError, om.register_action, 'shortpath', 'plugin', 'action', 'method')
def test_handle_modules_load(self): with utils.temporary_directory() as path: om = output_manager.OutputManager(path) om.loadfile = self.fake_loadfile out = om.handle_modules({'action': 'modules.load'}) self.assertEqual(out['result_code'], 1) self.assertEqual(out['result_str'], 'no payload specified') out = om.handle_modules({'action': 'modules.load', 'payload': {'gerbil': True}}) self.assertEqual(out['result_code'], 1) self.assertEqual(out['result_str'], 'no "path" specified in payload') out = om.handle_modules({'action': 'modules.load', 'payload': {'path': '/tmp/no/such'}}) self.assertEqual(out['result_code'], 1) self.assertEqual(out['result_str'], 'specified module does not exist') self.loadfile_calls = 0 with utils.temporary_file() as path: out = om.handle_modules({'action': 'modules.load', 'payload': {'path': path}}) self.assertEqual(out['result_code'], 0) self.assertEqual(self.loadfile_calls, 1)
def test_handle_logfile_tail_socket_fail(self): self.useFixture(fixtures.MonkeyPatch("socket.socket", FakeSocketSendFails)) with utils.temporary_directory() as path: with utils.temporary_directory() as logdir: om = output_manager.OutputManager(path) om.config = {"main": {"trans_log_dir": logdir}} with open(os.path.join(logdir, "trans_42.log"), "w") as f: f.write("This\nis\na\nlog\nfile") out = om.handle_logfile( {"action": "logfile.tail", "payload": {"task_id": "42", "dest_ip": "127.0.0.1", "dest_port": 4242}} ) self.assertEqual(out["result_code"], 1) self.assertEqual(out["result_str"], "remote socket disconnect")
def test_handle_logfile_tail(self): sock = FakeSocket(socket.AF_INET, socket.SOCK_STREAM) with utils.temporary_directory() as path: with utils.temporary_directory() as logdir: om = output_manager.OutputManager(path) om.config = {"main": {"trans_log_dir": logdir}} with open(os.path.join(logdir, "trans_42.log"), "w") as f: f.write("This\nis\na\nlog\nfile") out = om.handle_logfile( {"action": "logfile.tail", "payload": {"task_id": "42", "dest_ip": "127.0.0.1", "dest_port": 4242}}, sock=sock, ) self.assertEqual(out["result_code"], 0) self.assertNotEqual(sock.sent, [])
def test_handle_logfile_no_such_logfile(self): with utils.temporary_directory() as path: with utils.temporary_directory() as logdir: om = output_manager.OutputManager(path) om.config = {'main': {'trans_log_dir': logdir}} out = om.handle_logfile({ 'action': 'modules.load', 'payload': { 'task_id': '42', 'dest_ip': '127.0.0.1', 'dest_port': 4242, 'offset': 1024 } }) self.assertEqual(out['result_code'], 1) self.assertEqual(out['result_str'], 'no such transaction log file')
def test_handle_modules_actions(self): with utils.temporary_directory() as path: om = output_manager.OutputManager(path) out = om.handle_modules({"action": "modules.actions"}) self.assertEqual(out["result_code"], 0) self.assertEqual(out["result_str"], "success") self.assertTrue("name" in out["result_data"]) self.assertEqual(out["result_data"]["name"], "opencenter_agent_actions") self.assertTrue("value" in out["result_data"])
def test_load_directory_empty(self): with utils.temporary_directory() as path: self.files_loaded = 0 m = manager.Manager(path) m._load_file = self.fake_load_file m._load_directory(path) self.assertEqual(self.files_loaded, 0)
def test_handle_logfile_tail(self): sock = FakeSocket(socket.AF_INET, socket.SOCK_STREAM) with utils.temporary_directory() as path: with utils.temporary_directory() as logdir: om = output_manager.OutputManager(path) om.config = {'main': {'trans_log_dir': logdir}} with open(os.path.join(logdir, 'trans_42.log'), 'w') as f: f.write('This\nis\na\nlog\nfile') out = om.handle_logfile({'action': 'logfile.tail', 'payload': {'task_id': '42', 'dest_ip': '127.0.0.1', 'dest_port': 4242, 'offset': 1024}}, sock=sock) self.assertEqual(out['result_code'], 0) self.assertNotEqual(sock.sent, [])
def test_register_action_duplicate(self): with utils.temporary_directory() as path: om = output_manager.OutputManager(path) om.dispatch_table['action'] = { 'short_path': 'shortpath', 'method': 'method' } self.assertRaises(NameError, om.register_action, 'shortpath', 'plugin', 'action', 'method')
def test_handle_logfile_remote_connection_failed(self): with utils.temporary_directory() as path: with utils.temporary_directory() as logdir: om = output_manager.OutputManager(path) om.config = {'main': {'trans_log_dir': logdir}} with open(os.path.join(logdir, 'trans_42.log'), 'w') as f: f.write('This\nis\na\nlog\nfile') out = om.handle_logfile({ 'action': 'logfile.tail', 'payload': { 'task_id': '42', 'dest_ip': '127.0.0.1', 'dest_port': 4242, 'offset': 1024 } }) self.assertEqual(out['result_code'], 1)
def test_handle_modules_actions(self): with utils.temporary_directory() as path: om = output_manager.OutputManager(path) out = om.handle_modules({'action': 'modules.actions'}) self.assertEqual(out['result_code'], 0) self.assertEqual(out['result_str'], 'success') self.assertTrue('name' in out['result_data']) self.assertEqual(out['result_data']['name'], 'opencenter_agent_actions') self.assertTrue('value' in out['result_data'])
def test_handle_logfile_tail_socket_fail(self): self.useFixture(fixtures.MonkeyPatch('socket.socket', FakeSocketSendFails)) with utils.temporary_directory() as path: with utils.temporary_directory() as logdir: om = output_manager.OutputManager(path) om.config = {'main': {'trans_log_dir': logdir}} with open(os.path.join(logdir, 'trans_42.log'), 'w') as f: f.write('This\nis\na\nlog\nfile') out = om.handle_logfile({'action': 'logfile.tail', 'payload': {'task_id': '42', 'dest_ip': '127.0.0.1', 'dest_port': 4242, 'offset': 1024}}) self.assertEqual(out['result_code'], 1) self.assertEqual(out['result_str'], 'remote socket disconnect')
def test_handle_logfile_tail(self): sock = FakeSocket(socket.AF_INET, socket.SOCK_STREAM) with utils.temporary_directory() as path: with utils.temporary_directory() as logdir: om = output_manager.OutputManager(path) om.config = {'main': {'trans_log_dir': logdir}} with open(os.path.join(logdir, 'trans_42.log'), 'w') as f: f.write('This\nis\na\nlog\nfile') out = om.handle_logfile( { 'action': 'logfile.tail', 'payload': { 'task_id': '42', 'dest_ip': '127.0.0.1', 'dest_port': 4242, 'offset': 1024 } }, sock=sock) self.assertEqual(out['result_code'], 0) self.assertNotEqual(sock.sent, [])
def test_handle_logfile_tail_socket_fail(self): self.useFixture( fixtures.MonkeyPatch('socket.socket', FakeSocketSendFails)) with utils.temporary_directory() as path: with utils.temporary_directory() as logdir: om = output_manager.OutputManager(path) om.config = {'main': {'trans_log_dir': logdir}} with open(os.path.join(logdir, 'trans_42.log'), 'w') as f: f.write('This\nis\na\nlog\nfile') out = om.handle_logfile({ 'action': 'logfile.tail', 'payload': { 'task_id': '42', 'dest_ip': '127.0.0.1', 'dest_port': 4242, 'offset': 1024 } }) self.assertEqual(out['result_code'], 1) self.assertEqual(out['result_str'], 'remote socket disconnect')
def test_handle_logfile_watch(self): sock = FakeSocket(socket.AF_INET, socket.SOCK_STREAM) self.useFixture(fixtures.MonkeyPatch('time.sleep', self.fake_sleep)) with utils.temporary_directory() as path: with utils.temporary_directory() as logdir: om = output_manager.OutputManager(path) om.config = {'main': {'trans_log_dir': logdir}} with open(os.path.join(logdir, 'trans_42.log'), 'w') as f: f.write('This\nis\na\nlog\nfile') self.sleep_count = 0 self.sleep_writes_to = f out = om.handle_logfile( { 'action': 'logfile.watch', 'payload': { 'task_id': '42', 'dest_ip': '127.0.0.1', 'dest_port': 4242, 'timeout': 10 } }, sock=sock) self.assertEqual(out['result_code'], 0) # NOTE(mikal): this one is a little complicated. The # timeout is from the last bit of seen data... So, because # we return data for the first ten sleeps, we expect to # sleep 20 times before we timeout. self.assertEqual(self.sleep_count, 20) self.assertNotEqual(sock.sent, []) self.assertEqual(len(sock.sent), 10)
def test_load_directory_empty(self): with utils.temporary_directory() as path: os.mkdir(os.path.join(path, 'dir')) with open(os.path.join(path, 'file'), 'w') as f: f.write('fdsjkghfgjk') with open(os.path.join(path, 'file.py'), 'w') as f: f.write('fdsjkghfgjk') self.files_loaded = 0 m = manager.Manager(path) m._load_file = self.fake_load_file m._load_directory(path) self.assertEqual(self.files_loaded, 1)
def test_read_config_with_included_file(self): agent = OpenCenterAgentNoInitialization([]) with utils.temporary_directory() as path: config_file = os.path.join(path, 'config') included_file = os.path.join(path, 'included') with open(config_file, 'w') as f: f.write("""[taskerator] endpoint = http://127.0.0.1:8080/admin include = %s""" % included_file) agent.config_section = 'taskerator' self.assertRaises(RuntimeError, agent._read_config, config_file) with open(included_file, 'w') as f: f.write("""[taskerator] included_value = fish""") config = agent._read_config(config_file) self.assertEquals(config['taskerator']['endpoint'], 'http://127.0.0.1:8080/admin') self.assertEqual(config['taskerator']['included_value'], 'fish')
def test_handle_modules_load(self): with utils.temporary_directory() as path: om = output_manager.OutputManager(path) om.loadfile = self.fake_loadfile out = om.handle_modules({'action': 'modules.load'}) self.assertEqual(out['result_code'], 1) self.assertEqual(out['result_str'], 'no payload specified') out = om.handle_modules({ 'action': 'modules.load', 'payload': { 'gerbil': True } }) self.assertEqual(out['result_code'], 1) self.assertEqual(out['result_str'], 'no "path" specified in payload') out = om.handle_modules({ 'action': 'modules.load', 'payload': { 'path': '/tmp/no/such' } }) self.assertEqual(out['result_code'], 1) self.assertEqual(out['result_str'], 'specified module does not exist') self.loadfile_calls = 0 with utils.temporary_file() as path: out = om.handle_modules({ 'action': 'modules.load', 'payload': { 'path': path } }) self.assertEqual(out['result_code'], 0) self.assertEqual(self.loadfile_calls, 1)
def test_handle_modules_load(self): with utils.temporary_directory() as path: om = output_manager.OutputManager(path) om.loadfile = self.fake_loadfile out = om.handle_modules({"action": "modules.load"}) self.assertEqual(out["result_code"], 1) self.assertEqual(out["result_str"], "no payload specified") out = om.handle_modules({"action": "modules.load", "payload": {"gerbil": True}}) self.assertEqual(out["result_code"], 1) self.assertEqual(out["result_str"], 'no "path" specified in payload') out = om.handle_modules({"action": "modules.load", "payload": {"path": "/tmp/no/such"}}) self.assertEqual(out["result_code"], 1) self.assertEqual(out["result_str"], "specified module does not exist") self.loadfile_calls = 0 with utils.temporary_file() as path: out = om.handle_modules({"action": "modules.load", "payload": {"path": path}}) self.assertEqual(out["result_code"], 0) self.assertEqual(self.loadfile_calls, 1)
def test_load_file(self): with utils.temporary_directory() as path: subdir_path = os.path.join(path, 'dir') os.mkdir(subdir_path) single_file = os.path.join(path, 'file.py') with open(single_file, 'w') as f: f.write('fdsjkghfgjk') with open(os.path.join(subdir_path, 'file.py'), 'w') as f: f.write('fdsjkghfgjk') with open(os.path.join(subdir_path, 'file2.py'), 'w') as f: f.write('fdsjkghfgjk') self.files_loaded = 0 m = manager.Manager(single_file) m._load_file = self.fake_load_file m.load(single_file) self.assertEqual(self.files_loaded, 1) self.files_loaded = 0 m = manager.Manager(subdir_path) m._load_file = self.fake_load_file m.load(subdir_path) self.assertEqual(self.files_loaded, 2)
def test_actions(self): with utils.temporary_directory() as path: om = output_manager.OutputManager(path) self.assertTrue(len(om.actions()) > 0)
def test_handle_modules_reload(self): with utils.temporary_directory() as path: om = output_manager.OutputManager(path) out = om.handle_modules({"action": "modules.reload"}) self.assertEqual(out["result_code"], 0) self.assertEqual(out["result_str"], "success")
def test_temporary_directory(self): with utils.temporary_directory() as path: self.assertTrue(os.path.exists(path)) self.assertFalse(os.path.exists(path))
def test_handle_logfile_no_payload(self): with utils.temporary_directory() as path: om = output_manager.OutputManager(path) out = om.handle_logfile({"action": "modules.load", "payload": {}}) self.assertEqual(out["result_code"], 1)
def test_handle_logfile_no_payload(self): with utils.temporary_directory() as path: om = output_manager.OutputManager(path) out = om.handle_logfile({'action': 'modules.load', 'payload': {}}) self.assertEqual(out['result_code'], 1)
def test_init(self): with utils.temporary_directory() as path: om = output_manager.OutputManager(path) self.assertTrue(len(om.dispatch_table) > 0)
def test_handle_modules_reload(self): with utils.temporary_directory() as path: om = output_manager.OutputManager(path) out = om.handle_modules({'action': 'modules.reload'}) self.assertEqual(out['result_code'], 0) self.assertEqual(out['result_str'], 'success')
def test_register_action_duplicate(self): with utils.temporary_directory() as path: om = output_manager.OutputManager(path) om.dispatch_table["action"] = {"short_path": "shortpath", "method": "method"} self.assertRaises(NameError, om.register_action, "shortpath", "plugin", "action", "method")