class SSHHookTest(unittest.TestCase): def setUp(self): configuration.load_test_config() from airflow.contrib.hooks.ssh_hook import SSHHook self.hook = SSHHook(ssh_conn_id='ssh_default', keepalive_interval=10) self.hook.no_host_key_check = True def test_ssh_connection(self): ssh_hook = self.hook.get_conn() self.assertIsNotNone(ssh_hook) def test_tunnel(self): print("Setting up remote listener") import subprocess import socket self.server_handle = subprocess.Popen(["python", "-c", HELLO_SERVER_CMD], stdout=subprocess.PIPE) print("Setting up tunnel") with self.hook.create_tunnel(2135, 2134): print("Tunnel up") server_output = self.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.server_handle.communicate() self.assertEqual(self.server_handle.returncode, 0) print("Closing tunnel")
def test_tunnel(self): hook = SSHHook(ssh_conn_id='ssh_default') import subprocess import socket server_handle = subprocess.Popen(["python", "-c", HELLO_SERVER_CMD], stdout=subprocess.PIPE) with hook.create_tunnel(2135, 2134): server_output = server_handle.stdout.read(5) self.assertEqual(server_output, b"ready") s = socket.socket() s.connect(("localhost", 2135)) response = s.recv(5) self.assertEqual(response, b"hello") s.close() output, _ = server_handle.communicate() self.assertEqual(server_handle.returncode, 0)