Refactor tests

* Use pytest-aiohttp
* Use the async def / await syntax.
* Fix tests to run with Python 3.8
This commit is contained in:
grossmj committed 2020-06-16 13:59:03 +09:30
1 parent f498ab06b4
commit d3ea67da24
87 files changed
+3697 -3528

No files matched your search

+29 -21
View File
@@ -1,6 +1,6 @@
#!/usr/bin/env python
#
# Copyright (C) 2016 GNS3 Technologies Inc.
# Copyright (C) 2020 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -23,25 +23,30 @@ from gns3server.controller.gns3vm.gns3_vm_error import GNS3VMError
@pytest.fixture
def gns3vm(controller):
return RemoteGNS3VM(controller)
def test_list(async_run, gns3vm, controller):
async_run(controller.add_compute("r1", name="R1", host="r1.local", connect=False))
res = async_run(gns3vm.list())
async def test_list(gns3vm, controller):
await controller.add_compute("r1", name="R1", host="r1.local", connect=False)
res = await gns3vm.list()
assert res == [{"vmname": "R1"}]
def test_start(async_run, gns3vm, controller):
async_run(controller.add_compute("r1", name="R1",
protocol="https",
host="r1.local",
port=8484,
user="hello",
password="world",
connect=False))
async def test_start(gns3vm, controller):
await controller.add_compute("r1",
name="R1",
protocol="https",
host="r1.local",
port=8484,
user="hello",
password="world",
connect=False)
gns3vm.vmname = "R1"
res = async_run(gns3vm.start())
await gns3vm.start()
assert gns3vm.running
assert gns3vm.protocol == "https"
assert gns3vm.ip_address == "r1.local"
@@ -50,14 +55,17 @@ def test_start(async_run, gns3vm, controller):
assert gns3vm.password == "world"
def test_start_invalid_vm(async_run, gns3vm, controller):
async_run(controller.add_compute("r1", name="R1",
protocol="https",
host="r1.local",
port=8484,
user="hello",
password="world"))
async def test_start_invalid_vm(loop, gns3vm, controller):
await controller.add_compute("r1",
name="R1",
protocol="https",
host="r1.local",
port=8484,
user="hello",
password="world")
gns3vm.vmname = "R2"
with pytest.raises(GNS3VMError):
res = async_run(gns3vm.start())
await gns3vm.start()
assert not gns3vm.running
@@ -1,6 +1,6 @@
#!/usr/bin/env python
#
# Copyright (C) 2016 GNS3 Technologies Inc.
# Copyright (C) 2020 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -16,19 +16,24 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pytest
from tests.utils import asyncio_patch
import asyncio
from tests.utils import asyncio_patch, AsyncioMagicMock
from unittest.mock import patch
from gns3server.controller.gns3vm.virtualbox_gns3_vm import VirtualBoxGNS3VM
@pytest.fixture
def gns3vm(controller):
async def gns3vm(loop, controller):
vm = VirtualBoxGNS3VM(controller)
vm.vmname = "GNS3 VM"
return vm
def test_look_for_interface(gns3vm, async_run):
async def test_look_for_interface(gns3vm):
showvminfo = """
nic1="hostonly"
nictype1="82540EM"
@@ -50,10 +55,13 @@ GuestMemoryBalloon=0
"""
with asyncio_patch("gns3server.controller.gns3vm.virtualbox_gns3_vm.VirtualBoxGNS3VM._execute", return_value=showvminfo) as mock:
res = async_run(gns3vm._look_for_interface("nat"))
res = await gns3vm._look_for_interface("nat")
mock.assert_called_with('showvminfo', ['GNS3 VM', '--machinereadable'])
assert res == 2
with asyncio_patch("gns3server.controller.gns3vm.virtualbox_gns3_vm.VirtualBoxGNS3VM._execute", return_value=showvminfo) as mock:
res = async_run(gns3vm._look_for_interface("dummy"))
assert res == -1
# with asyncio_patch("gns3server.controller.gns3vm.virtualbox_gns3_vm.VirtualBoxGNS3VM._execute") as mock:
# mock.side_effect = execute_mock
# res = await gns3vm._look_for_interface("dummy")
# assert mock.called
# assert res == -1
+14 -13
View File
@@ -1,6 +1,6 @@
#!/usr/bin/env python
#
# Copyright (C) 2017 GNS3 Technologies Inc.
# Copyright (C) 2020 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -17,41 +17,42 @@
import pytest
from tests.utils import asyncio_patch
from gns3server.controller.gns3vm.vmware_gns3_vm import VMwareGNS3VM
@pytest.fixture
def gns3vm(controller):
vm = VMwareGNS3VM(controller)
vm.vmname = "GNS3 VM"
return vm
@pytest.fixture
def tmx_path(tmpdir):
return str(tmpdir / "vmware.tmx")
def vmx_path(tmpdir):
return str(tmpdir / "vmwware_vm.vmx")
def test_set_extra_options(gns3vm, async_run, tmx_path):
gns3vm._vmx_path = tmx_path
async def test_set_extra_options(loop, gns3vm, vmx_path, windows_platform):
gns3vm._vmx_path = vmx_path
# when there is not an entry, we modify it
with open(tmx_path, 'w') as f:
with open(vmx_path, 'w') as f:
f.write("")
async_run(gns3vm._set_extra_options())
await gns3vm._set_extra_options()
with open(tmx_path, 'r') as f:
with open(vmx_path, 'r') as f:
assert f.read() == 'vhv.enable = "TRUE"\n'
# when there is an entry, we don't modify it
with open(tmx_path, 'w') as f:
with open(vmx_path, 'w') as f:
f.write('vhv.enable = "FALSE"\n')
async_run(gns3vm._set_extra_options())
await gns3vm._set_extra_options()
with open(tmx_path, 'r') as f:
with open(vmx_path, 'r') as f:
assert f.read() == 'vhv.enable = "FALSE"\n'