2017-02-01 12:30:14 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
2021-10-18 10:34:30 +03:00
|
|
|
# Copyright (C) 2021 GNS3 Technologies Inc.
|
2017-02-01 12:30:14 +02:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2021-10-18 10:34:30 +03:00
|
|
|
import os
|
2020-10-02 09:37:50 +03:00
|
|
|
import pytest
|
2021-10-18 10:34:30 +03:00
|
|
|
import shutil
|
2017-02-01 12:30:14 +02:00
|
|
|
|
2020-12-02 10:09:08 +02:00
|
|
|
from fastapi import FastAPI, status
|
|
|
|
from httpx import AsyncClient
|
2022-12-22 03:24:17 +02:00
|
|
|
from gns3server.controller import Controller
|
2020-10-02 09:37:50 +03:00
|
|
|
|
2020-12-02 10:09:08 +02:00
|
|
|
pytestmark = pytest.mark.asyncio
|
2017-04-12 15:35:49 +03:00
|
|
|
|
2020-12-02 10:09:08 +02:00
|
|
|
|
2021-10-18 10:34:30 +03:00
|
|
|
class TestApplianceRoutes:
|
2020-12-02 10:09:08 +02:00
|
|
|
|
2022-12-22 03:24:17 +02:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def _install_builtin_appliances(self, controller: Controller):
|
|
|
|
|
|
|
|
controller.appliance_manager.install_builtin_appliances()
|
|
|
|
controller.appliance_manager.load_appliances()
|
|
|
|
|
2021-10-18 10:34:30 +03:00
|
|
|
async def test_appliances_list(self, app: FastAPI, client: AsyncClient) -> None:
|
|
|
|
|
|
|
|
response = await client.get(app.url_path_for("get_appliances"))
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
assert len(response.json()) > 0
|
|
|
|
|
2021-10-18 14:16:50 +03:00
|
|
|
async def test_get_appliance(self, app: FastAPI, client: AsyncClient) -> None:
|
2021-10-18 10:34:30 +03:00
|
|
|
|
|
|
|
appliance_id = "3bf492b6-5717-4257-9bfd-b34617c6f133" # Cisco IOSv appliance
|
2021-10-18 14:16:50 +03:00
|
|
|
response = await client.get(app.url_path_for("get_appliance", appliance_id=appliance_id))
|
2021-10-18 10:34:30 +03:00
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
assert response.json()["appliance_id"] == appliance_id
|
|
|
|
|
|
|
|
async def test_docker_appliance_install(self, app: FastAPI, client: AsyncClient) -> None:
|
|
|
|
|
|
|
|
appliance_id = "fc520ae2-a4e5-48c3-9a13-516bb2e94668" # Alpine Linux appliance
|
|
|
|
response = await client.post(app.url_path_for("install_appliance", appliance_id=appliance_id))
|
|
|
|
assert response.status_code == status.HTTP_204_NO_CONTENT
|
|
|
|
|
|
|
|
async def test_docker_appliance_install_with_version(self, app: FastAPI, client: AsyncClient) -> None:
|
|
|
|
|
|
|
|
appliance_id = "fc520ae2-a4e5-48c3-9a13-516bb2e94668" # Alpine Linux appliance
|
|
|
|
params = {"version": "123"}
|
|
|
|
response = await client.post(app.url_path_for("install_appliance", appliance_id=appliance_id), params=params)
|
|
|
|
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
|
|
|
|
|
|
|
async def test_qemu_appliance_install_with_version(self, app: FastAPI, client: AsyncClient, images_dir: str) -> None:
|
|
|
|
|
|
|
|
shutil.copy("tests/resources/empty8G.qcow2", os.path.join(images_dir, "QEMU", "empty8G.qcow2"))
|
|
|
|
appliance_id = "1cfdf900-7c30-4cb7-8f03-3f61d2581633" # Empty VM appliance
|
|
|
|
params = {"version": "8G"}
|
|
|
|
response = await client.post(app.url_path_for("install_appliance", appliance_id=appliance_id), params=params)
|
|
|
|
assert response.status_code == status.HTTP_204_NO_CONTENT
|
|
|
|
|
|
|
|
async def test_qemu_appliance_install_without_version(self, app: FastAPI, client: AsyncClient, images_dir: str) -> None:
|
|
|
|
|
|
|
|
appliance_id = "1cfdf900-7c30-4cb7-8f03-3f61d2581633" # Empty VM appliance
|
|
|
|
response = await client.post(app.url_path_for("install_appliance", appliance_id=appliance_id))
|
|
|
|
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
2021-10-19 07:45:10 +03:00
|
|
|
|
|
|
|
async def test_add_version_appliance(self, app: FastAPI, client: AsyncClient) -> None:
|
|
|
|
|
|
|
|
appliance_id = "1cfdf900-7c30-4cb7-8f03-3f61d2581633" # Empty VM appliance
|
|
|
|
new_version = {
|
|
|
|
"name": "99G",
|
|
|
|
"images": {
|
|
|
|
"hda_disk_image": "empty99G.qcow2"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
response = await client.post(app.url_path_for("add_appliance_version", appliance_id=appliance_id), json=new_version)
|
|
|
|
assert response.status_code == status.HTTP_201_CREATED
|
|
|
|
assert new_version in response.json()["versions"]
|
|
|
|
|
|
|
|
async def test_add_existing_version_appliance(self, app: FastAPI, client: AsyncClient) -> None:
|
|
|
|
|
|
|
|
appliance_id = "1cfdf900-7c30-4cb7-8f03-3f61d2581633" # Empty VM appliance
|
|
|
|
new_version = {
|
|
|
|
"name": "8G",
|
|
|
|
"images": {
|
|
|
|
"hda_disk_image": "empty8G.qcow2"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
response = await client.post(app.url_path_for("add_appliance_version", appliance_id=appliance_id), json=new_version)
|
|
|
|
assert response.status_code == status.HTTP_409_CONFLICT
|