2016-03-11 19:13:52 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016 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
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import asyncio
|
2016-03-14 16:40:27 +00:00
|
|
|
import aiohttp
|
2016-03-11 19:13:52 +00:00
|
|
|
from unittest.mock import MagicMock
|
|
|
|
|
|
|
|
from gns3server.controller.project import Project
|
2016-04-15 15:57:06 +00:00
|
|
|
from gns3server.controller.compute import Compute
|
2016-03-11 19:13:52 +00:00
|
|
|
from gns3server.controller.udp_link import UDPLink
|
|
|
|
from gns3server.controller.vm import VM
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def project():
|
|
|
|
return Project()
|
|
|
|
|
|
|
|
|
|
|
|
def test_create(async_run, project):
|
2016-04-15 15:57:06 +00:00
|
|
|
compute1 = MagicMock()
|
|
|
|
compute2 = MagicMock()
|
2016-03-11 19:13:52 +00:00
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
vm1 = VM(project, compute1, vm_type="vpcs")
|
|
|
|
vm2 = VM(project, compute2, vm_type="vpcs")
|
2016-03-11 19:13:52 +00:00
|
|
|
|
|
|
|
link = UDPLink(project)
|
|
|
|
async_run(link.addVM(vm1, 0, 4))
|
|
|
|
async_run(link.addVM(vm2, 3, 1))
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2016-04-15 15:57:06 +00:00
|
|
|
def compute1_callback(path, data={}):
|
2016-03-11 19:13:52 +00:00
|
|
|
"""
|
|
|
|
Fake server
|
|
|
|
"""
|
|
|
|
if "/ports/udp" in path:
|
|
|
|
response = MagicMock()
|
|
|
|
response.json = {"udp_port": 1024}
|
|
|
|
return response
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2016-04-15 15:57:06 +00:00
|
|
|
def compute2_callback(path, data={}):
|
2016-03-11 19:13:52 +00:00
|
|
|
"""
|
|
|
|
Fake server
|
|
|
|
"""
|
|
|
|
if "/ports/udp" in path:
|
|
|
|
response = MagicMock()
|
|
|
|
response.json = {"udp_port": 2048}
|
|
|
|
return response
|
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
compute1.post.side_effect = compute1_callback
|
|
|
|
compute1.host = "example.com"
|
|
|
|
compute2.post.side_effect = compute2_callback
|
|
|
|
compute2.host = "example.org"
|
2016-03-11 19:13:52 +00:00
|
|
|
async_run(link.create())
|
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
compute1.post.assert_any_call("/projects/{}/vpcs/vms/{}/adapters/0/ports/4/nio".format(project.id, vm1.id), data={
|
2016-03-11 19:13:52 +00:00
|
|
|
"lport": 1024,
|
2016-04-15 15:57:06 +00:00
|
|
|
"rhost": compute2.host,
|
2016-03-11 19:13:52 +00:00
|
|
|
"rport": 2048,
|
|
|
|
"type": "nio_udp"
|
|
|
|
})
|
2016-04-15 15:57:06 +00:00
|
|
|
compute2.post.assert_any_call("/projects/{}/vpcs/vms/{}/adapters/3/ports/1/nio".format(project.id, vm2.id), data={
|
2016-03-11 19:13:52 +00:00
|
|
|
"lport": 2048,
|
2016-04-15 15:57:06 +00:00
|
|
|
"rhost": compute1.host,
|
2016-03-11 19:13:52 +00:00
|
|
|
"rport": 1024,
|
|
|
|
"type": "nio_udp"
|
|
|
|
})
|
2016-03-14 16:40:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_delete(async_run, project):
|
2016-04-15 15:57:06 +00:00
|
|
|
compute1 = MagicMock()
|
|
|
|
compute2 = MagicMock()
|
2016-03-14 16:40:27 +00:00
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
vm1 = VM(project, compute1, vm_type="vpcs")
|
|
|
|
vm2 = VM(project, compute2, vm_type="vpcs")
|
2016-03-14 16:40:27 +00:00
|
|
|
|
|
|
|
link = UDPLink(project)
|
|
|
|
async_run(link.addVM(vm1, 0, 4))
|
|
|
|
async_run(link.addVM(vm2, 3, 1))
|
|
|
|
|
|
|
|
async_run(link.delete())
|
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
compute1.delete.assert_any_call("/projects/{}/vpcs/vms/{}/adapters/0/ports/4/nio".format(project.id, vm1.id))
|
|
|
|
compute2.delete.assert_any_call("/projects/{}/vpcs/vms/{}/adapters/3/ports/1/nio".format(project.id, vm2.id))
|