class TestRemoteContext(unittest.TestCase): def setUp(self): self.context = RemoteContext(working_ssh_host) def tearDown(self): try: self.remote_server_handle.terminate() except Exception: pass def test_check_output(self): """ Test check_output ssh Assumes the running user can ssh to working_ssh_host """ output = self.context.check_output(["echo", "-n", "luigi"]) self.assertEqual(output, b"luigi") def test_tunnel(self): print("Setting up remote listener...") self.remote_server_handle = self.context.Popen([ "python", "-c", '"{0}"'.format(HELLO_SERVER_CMD) ], stdout=subprocess.PIPE) print("Setting up tunnel") with self.context.tunnel(2135, 2134): print("Tunnel up!") # hack to make sure the listener process is up # and running before we write to it server_output = self.remote_server_handle.stdout.read(5) self.assertEqual(server_output, b"ready") print("Connecting to server via tunnel") s = socket.socket() s.connect(("localhost", 2135)) print("Receiving...",) response = s.recv(5) self.assertEqual(response, b"hello") print("Closing connection") s.close() print("Waiting for listener...") output, _ = self.remote_server_handle.communicate() self.assertEqual(self.remote_server_handle.returncode, 0) print("Closing tunnel")
class TestRemoteTarget(unittest.TestCase): """ These tests assume RemoteContext working in order for setUp and tearDown to work """ def setUp(self): self.ctx = RemoteContext(working_ssh_host) self.filepath = "/tmp/luigi_remote_test.dat" self.target = RemoteTarget( self.filepath, working_ssh_host, ) self.ctx.check_output(["rm", "-rf", self.filepath]) self.ctx.check_output(["echo -n 'hello' >", self.filepath]) def tearDown(self): self.ctx.check_output(["rm", "-rf", self.filepath]) def test_exists(self): self.assertTrue(self.target.exists()) no_file = RemoteTarget( "/tmp/_file_that_doesnt_exist_", working_ssh_host, ) self.assertFalse(no_file.exists()) def test_remove(self): self.target.remove() self.assertRaises( subprocess.CalledProcessError, self.ctx.check_output, ["cat", self.filepath] ) def test_open(self): f = self.target.open('r') file_content = f.read() f.close() self.assertEqual(file_content, "hello") self.assertTrue(self.target.fs.exists(self.filepath)) self.assertFalse(self.target.fs.isdir(self.filepath)) def test_context_manager(self): with self.target.open('r') as f: file_content = f.read() self.assertEqual(file_content, "hello")
class TestRemoteContext(unittest.TestCase): def setUp(self): self.context = RemoteContext(working_ssh_host) def test_check_output(self): """ Test check_output ssh Assumes the running user can ssh to working_ssh_host """ output = self.context.check_output(["echo", "-n", "luigi"]) self.assertEqual(output, "luigi") def test_tunnel(self): print("Setting up remote listener...") remote_server_handle = self.context.Popen( ["python", "-c", '"{0}"'.format(HELLO_SERVER_CMD)], stdout=subprocess.PIPE) print("Setting up tunnel") with self.context.tunnel(2135, 2134): print("Tunnel up!") # hack to make sure the listener process is up # and running before we write to it server_output = remote_server_handle.stdout.read(5) self.assertEqual(server_output, "ready") print("Connecting to server via tunnel") s = socket.socket() s.connect(("localhost", 2135)) print("Receiving...", ) response = s.recv(5) self.assertEqual(response, "hello") print("Closing connection") s.close() print("Waiting for listener...") output, _ = remote_server_handle.communicate() self.assertEqual(remote_server_handle.returncode, 0) print("Closing tunnel")
class TestRemoteTarget(unittest.TestCase): """ These tests assume RemoteContext working in order for setUp and tearDown to work """ def setUp(self): self.ctx = RemoteContext(working_ssh_host) self.filepath = "/tmp/luigi_remote_test.dat" self.target = RemoteTarget( self.filepath, working_ssh_host, ) self.ctx.check_output(["rm", "-rf", self.filepath]) self.ctx.check_output(["echo -n 'hello' >", self.filepath]) def tearDown(self): self.ctx.check_output(["rm", "-rf", self.filepath]) def test_exists(self): self.assertTrue(self.target.exists()) no_file = RemoteTarget( "/tmp/_file_that_doesnt_exist_", working_ssh_host, ) self.assertFalse(no_file.exists()) def test_remove(self): self.target.remove() self.assertRaises(subprocess.CalledProcessError, self.ctx.check_output, ["cat", self.filepath]) def test_open(self): f = self.target.open('r') file_content = f.read() f.close() self.assertEqual(file_content, "hello") self.assertTrue(self.target.fs.exists(self.filepath)) self.assertFalse(self.target.fs.isdir(self.filepath)) def test_context_manager(self): with self.target.open('r') as f: file_content = f.read() self.assertEqual(file_content, "hello")
def run(self): remote = RemoteContext(SSH_HOST) print remote.check_output([ "ps aux > {0}".format(self.output().path) ])
def run(self): remote = RemoteContext(SSH_HOST) print remote.check_output(["ps aux > {0}".format(self.output().path)])
def execute_command(context: RemoteContext, command: str): try: output = context.check_output(shlex.split(command)) return output except RemoteCalledProcessError as e: print(f"execute error: {e.__str__()}")
def run(self): parameters.setParameters(parameters.data) remote = RemoteContext(SSH_HOST) remote.check_output(['mkdir -p ' + parameters.data['remote_storage']])