2021-10-22 04:02:52 +08:00
|
|
|
import io
|
|
|
|
import sys
|
2021-10-27 23:43:34 +08:00
|
|
|
import json
|
2021-10-22 04:02:52 +08:00
|
|
|
import unittest
|
|
|
|
import unittest.mock as mock
|
2021-10-27 23:43:34 +08:00
|
|
|
from gradio import tunneling, networking, Interface
|
|
|
|
import threading
|
|
|
|
import paramiko
|
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 = networking.url_request(networking.GRADIO_API_SERVER)
|
|
|
|
payload = json.loads(response.read().decode("utf-8"))[0]
|
|
|
|
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):
|
2021-10-27 23:43:34 +08:00
|
|
|
"""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.
|
|
|
|
|
|
|
|
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(), '')
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
sys.stdout = sys.__stdout__
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|