2021-10-22 04:02:52 +08:00
|
|
|
import io
|
2021-10-27 23:43:34 +08:00
|
|
|
import json
|
2022-01-21 21:44:12 +08:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import threading
|
2021-10-22 04:02:52 +08:00
|
|
|
import unittest
|
|
|
|
import unittest.mock as mock
|
2022-01-21 21:44:12 +08:00
|
|
|
|
2021-10-27 23:43:34 +08:00
|
|
|
import paramiko
|
2022-02-10 13:10:00 +08:00
|
|
|
import requests
|
2021-11-09 22:48:55 +08:00
|
|
|
|
2022-01-21 21:44:12 +08:00
|
|
|
from gradio import Interface, networking, tunneling
|
2021-11-09 22:48:55 +08:00
|
|
|
|
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):
|
2022-02-10 13:10:00 +08:00
|
|
|
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
|
|
|
|
2022-01-21 21:44:12 +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"
|
2022-01-21 21:44:12 +08:00
|
|
|
self.capturedOutput = io.StringIO() # Create StringIO object
|
2022-02-10 03:52:26 +08:00
|
|
|
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)
|
2022-01-21 21:44:12 +08:00
|
|
|
self.assertEqual(self.capturedOutput.getvalue().strip(), "")
|
2021-10-22 04:02:52 +08:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
sys.stdout = sys.__stdout__
|
|
|
|
|
2022-01-21 21:44:12 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-10-22 04:02:52 +08:00
|
|
|
unittest.main()
|