1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-13 20:08:55 +00:00

/start /stop and /suspend API for VM via container

This commit is contained in:
Julien Duponchelle 2016-04-12 18:02:36 +02:00
parent ad7ca86f32
commit 2055cdea5e
No known key found for this signature in database
GPG Key ID: CE8B29639E07F5E8
5 changed files with 144 additions and 1 deletions

View File

@ -110,6 +110,27 @@ class VM:
else:
self._properties[key] = value
@asyncio.coroutine
def start(self):
"""
Start a VM
"""
yield from self._hypervisor.post("/projects/{}/{}/vms/{}/start".format(self._project.id, self._vm_type, self._id))
@asyncio.coroutine
def stop(self):
"""
Stop a VM
"""
yield from self._hypervisor.post("/projects/{}/{}/vms/{}/stop".format(self._project.id, self._vm_type, self._id))
@asyncio.coroutine
def suspend(self):
"""
Suspend a VM
"""
yield from self._hypervisor.post("/projects/{}/{}/vms/{}/suspend".format(self._project.id, self._vm_type, self._id))
@asyncio.coroutine
def post(self, path, data={}):
"""

View File

@ -47,3 +47,66 @@ class VMHandler:
vm = yield from project.addVM(hypervisor, request.json.pop("vm_id", None), **request.json)
response.set_status(201)
response.json(vm)
@classmethod
@Route.post(
r"/projects/{project_id}/vms/{vm_id}/start",
parameters={
"project_id": "UUID for the project",
"vm_id": "UUID for the VM"
},
status_codes={
201: "Instance created",
400: "Invalid request"
},
description="Start a VM instance",
output=VM_OBJECT_SCHEMA)
def start(request, response):
project = Controller.instance().getProject(request.match_info["project_id"])
vm = project.getVM(request.match_info["vm_id"])
yield from vm.start()
response.set_status(201)
response.json(vm)
@classmethod
@Route.post(
r"/projects/{project_id}/vms/{vm_id}/stop",
parameters={
"project_id": "UUID for the project",
"vm_id": "UUID for the VM"
},
status_codes={
201: "Instance created",
400: "Invalid request"
},
description="Start a VM instance",
output=VM_OBJECT_SCHEMA)
def stop(request, response):
project = Controller.instance().getProject(request.match_info["project_id"])
vm = project.getVM(request.match_info["vm_id"])
yield from vm.stop()
response.set_status(201)
response.json(vm)
@classmethod
@Route.post(
r"/projects/{project_id}/vms/{vm_id}/suspend",
parameters={
"project_id": "UUID for the project",
"vm_id": "UUID for the VM"
},
status_codes={
201: "Instance created",
400: "Invalid request"
},
description="Start a VM instance",
output=VM_OBJECT_SCHEMA)
def suspend(request, response):
project = Controller.instance().getProject(request.match_info["project_id"])
vm = project.getVM(request.match_info["vm_id"])
yield from vm.suspend()
response.set_status(201)
response.json(vm)

View File

@ -86,6 +86,30 @@ def test_create(vm, hypervisor, project, async_run):
assert vm._properties == {"startup_script": "echo test"}
def test_start(vm, hypervisor, project, async_run):
hypervisor.post = AsyncioMagicMock()
async_run(vm.start())
hypervisor.post.assert_called_with("/projects/{}/vpcs/vms/{}/start".format(vm.project.id, vm.id))
def test_stop(vm, hypervisor, project, async_run):
hypervisor.post = AsyncioMagicMock()
async_run(vm.stop())
hypervisor.post.assert_called_with("/projects/{}/vpcs/vms/{}/stop".format(vm.project.id, vm.id))
def test_suspend(vm, hypervisor, project, async_run):
hypervisor.post = AsyncioMagicMock()
async_run(vm.suspend())
hypervisor.post.assert_called_with("/projects/{}/vpcs/vms/{}/suspend".format(vm.project.id, vm.id))
def test_create_without_console(vm, hypervisor, project, async_run):
"""
None properties should be send. Because it can mean the emulator doesn"t support it

View File

@ -31,6 +31,7 @@ from tests.utils import asyncio_patch, AsyncioMagicMock
from gns3server.handlers.api.controller.project_handler import ProjectHandler
from gns3server.controller import Controller
from gns3server.controller.vm import VM
@pytest.fixture
@ -46,6 +47,13 @@ def project(http_controller, async_run):
return async_run(Controller.instance().addProject())
@pytest.fixture
def vm(project, hypervisor, async_run):
vm = VM(project, hypervisor, name="test", vm_type="vpcs")
project._vms[vm.id] = vm
return vm
def test_create_vm(http_controller, tmpdir, project, hypervisor):
response = MagicMock()
response.json = {"console": 2048}
@ -62,3 +70,30 @@ def test_create_vm(http_controller, tmpdir, project, hypervisor):
assert response.status == 201
assert response.json["name"] == "test"
assert "name" not in response.json["properties"]
def test_start_vm(http_controller, tmpdir, project, hypervisor, vm):
response = MagicMock()
hypervisor.post = AsyncioMagicMock()
response = http_controller.post("/projects/{}/vms/{}/start".format(project.id, vm.id), example=True)
assert response.status == 201
assert response.json["name"] == vm.name
def test_stop_vm(http_controller, tmpdir, project, hypervisor, vm):
response = MagicMock()
hypervisor.post = AsyncioMagicMock()
response = http_controller.post("/projects/{}/vms/{}/stop".format(project.id, vm.id), example=True)
assert response.status == 201
assert response.json["name"] == vm.name
def test_suspend_vm(http_controller, tmpdir, project, hypervisor, vm):
response = MagicMock()
hypervisor.post = AsyncioMagicMock()
response = http_controller.post("/projects/{}/vms/{}/suspend".format(project.id, vm.id), example=True)
assert response.status == 201
assert response.json["name"] == vm.name

View File

@ -22,7 +22,7 @@ import pytest
def test_udp_allocation(http_hypervisor, project):
response = http_hypervisor.post('/projects/{}/ports/udp'.format(project.id), {}, example=True)
assert response.status == 201
assert response.json == {'udp_port': 10000}
assert response.json['udp_port'] is not None
# Netfifaces is not available on Travis