gradio/test/test_tunneling.py

54 lines
1.7 KiB
Python
Raw Normal View History

2021-10-22 04:02:52 +08:00
import io
2021-10-27 23:43:34 +08:00
import json
import os
import sys
import threading
2021-10-22 04:02:52 +08:00
import unittest
import unittest.mock as mock
2021-10-27 23:43:34 +08:00
import paramiko
import requests
from gradio import Interface, networking, tunneling
2021-11-10 02:30:59 +08:00
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
2021-10-22 04:02:52 +08:00
2021-10-27 23:43:34 +08:00
class TestTunneling(unittest.TestCase):
def test_create_tunnel(self):
response = requests.get(networking.GRADIO_API_SERVER)
payload = response.json()[0]
2021-10-27 23:43:34 +08:00
io = Interface(lambda x: x, "text", "text")
_, path_to_local_server, _ = io.launch(prevent_thread_lock=True, share=False)
_, localhost, port = path_to_local_server.split(":")
threading.Thread.start = mock.MagicMock(return_value=None)
paramiko.SSHClient.connect = mock.MagicMock(return_value=None)
tunneling.create_tunnel(payload, localhost, port)
threading.Thread.start.assert_called_once()
paramiko.SSHClient.connect.assert_called_once()
io.close()
2021-10-22 04:02:52 +08:00
class TestVerbose(unittest.TestCase):
"""Not absolutely needed but just including them for the sake of completion."""
2021-10-22 04:02:52 +08:00
def setUp(self):
self.message = "print test"
self.capturedOutput = io.StringIO() # Create StringIO object
sys.stdout = self.capturedOutput # and redirect stdout.
2021-10-22 04:02:52 +08:00
def test_verbose_debug_true(self):
tunneling.verbose(self.message, debug_mode=True)
self.assertEqual(self.capturedOutput.getvalue().strip(), self.message)
def test_verbose_debug_false(self):
tunneling.verbose(self.message, debug_mode=False)
self.assertEqual(self.capturedOutput.getvalue().strip(), "")
2021-10-22 04:02:52 +08:00
def tearDown(self):
sys.stdout = sys.__stdout__
if __name__ == "__main__":
2021-10-22 04:02:52 +08:00
unittest.main()