Fix tests

This commit is contained in:
grossmj
2023-10-31 15:40:50 +10:00
parent 0cd7d7e4c2
commit d10c3c7308
4 changed files with 32 additions and 21 deletions

View File

@@ -53,6 +53,7 @@ def vncConsole(node, port, command):
command = command.replace("%P", node.project().name().replace('"', '\\"'))
command = command.replace("%i", node.project().id())
command = command.replace("%n", str(node.id()))
command = command.replace("%c", Controller.instance().httpClient().fullUrl())
command = command.replace("{host}", host)
command = command.replace("{port}", str(port))

View File

@@ -75,6 +75,7 @@ def controller():
Controller._instance = None
c = Controller.instance()
c._http_client = MagicMock()
c._http_client.fullUrl.return_value = "http://localhost:3080"
return c

View File

@@ -17,28 +17,30 @@
import os
import shlex
import unittest
from unittest.mock import patch
from gns3.spice_console import spiceConsole
def test_spice_console_on_linux_and_mac(vpcs_device):
with patch('subprocess.Popen') as popen, \
patch('sys.platform', new="linux"):
vpcs_device.settings()["console_host"] = "localhost"
spiceConsole(vpcs_device, '2525', 'command %h %p')
popen.assert_called_once_with(shlex.split('command localhost 2525'), env=os.environ)
popen.assert_called_with(shlex.split('command localhost 2525'), env=os.environ)
spiceConsole(vpcs_device, '2525', 'command {host} {port}')
popen.assert_called_with(shlex.split('command localhost 2525'), env=os.environ)
def test_spice_console_on_windows(vpcs_device):
with patch('subprocess.call') as popen, \
with patch('subprocess.call') as p, \
patch('sys.platform', new="win"):
vpcs_device.settings()["console_host"] = "localhost"
spiceConsole(vpcs_device, '2525', 'command %h %p')
popen.assert_called_once_with('command localhost 2525', env=unittest.mock.ANY)
p.assert_called_with('command localhost 2525', env=os.environ)
spiceConsole(vpcs_device, '2525', 'command {host} {port}')
p.assert_called_with('command localhost 2525', env=os.environ)
# def test_spice_console_on_linux_with_popen_issues():
# with patch('subprocess.Popen', side_effect=OSError("Dummy")), \
@@ -49,9 +51,11 @@ def test_spice_console_on_windows(vpcs_device):
def test_spice_console_with_ipv6_support(vpcs_device):
with patch('subprocess.Popen') as popen, \
patch('sys.platform', new="linux"):
vpcs_device.settings()["console_host"] = "::1"
spiceConsole(vpcs_device, '2525', 'command %h %p')
popen.assert_called_once_with(shlex.split('command [::1] 2525'), env=os.environ)
popen.assert_called_with(shlex.split('command [::1] 2525'), env=os.environ)
spiceConsole(vpcs_device, '2525', 'command {host} {port}')
popen.assert_called_with(shlex.split('command [::1] 2525'), env=os.environ)

View File

@@ -17,31 +17,36 @@
import os
import shlex
import unittest
from unittest.mock import patch
from gns3.vnc_console import vncConsole
def test_vnc_console_on_linux_and_mac(vpcs_device):
with patch('subprocess.Popen') as popen, \
with patch('subprocess.call') as p, \
patch('sys.platform', new="linux"):
vpcs_device.settings()["console_host"] = "localhost"
vncConsole(vpcs_device, 6000, 'command %h %p %P')
popen.assert_called_once_with(shlex.split('command localhost 6000 100'), env=os.environ)
vncConsole(vpcs_device, 6000, 'command %h %p %D')
p.assert_called_with(shlex.split('command localhost 6000 100'), env=os.environ)
vncConsole(vpcs_device, 6000, 'command {host} {port} {display}')
p.assert_called_with(shlex.split('command localhost 6000 100'), env=os.environ)
def test_vnc_console_on_windows(vpcs_device):
with patch('subprocess.call') as popen, \
with patch('subprocess.call') as p, \
patch('sys.platform', new="win"):
vpcs_device.settings()["console_host"] = "localhost"
vncConsole(vpcs_device, 6000, 'command %h %p %P')
popen.assert_called_once_with('command localhost 6000 100', env=unittest.mock.ANY)
vncConsole(vpcs_device, 6000, 'command %h %p %D')
p.assert_called_with('command localhost 6000 100', env=os.environ)
vncConsole(vpcs_device, 6000, 'command {host} {port} {display}')
p.assert_called_with('command localhost 6000 100', env=os.environ)
# def test_vnc_console_on_linux_with_popen_issues():
# with patch('subprocess.Popen', side_effect=OSError("Dummy")), \
# def test_vnc_console_on_linux_with_popen_issues(vpcs_device):
# with patch('subprocess.call', side_effect=subprocess.SubprocessError()), \
# patch('sys.platform', new="linux"):
# vpcs_device.settings()["console_host"] = "localhost"
#
# with pytest.raises(OSError):
# vncConsole('localhost', 6000, 'command %h %p %P')
# with pytest.raises(subprocess.SubprocessError):
# vncConsole(vpcs_device, 6000, 'command %h %p %P')