2016-07-26 11:32:43 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
2020-06-16 07:29:03 +03:00
|
|
|
# Copyright (C) 2020 GNS3 Technologies Inc.
|
2016-07-26 11:32:43 +03: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/>.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import uuid
|
|
|
|
import pytest
|
|
|
|
|
2020-12-02 10:09:08 +02:00
|
|
|
from fastapi import FastAPI, status
|
|
|
|
from httpx import AsyncClient
|
|
|
|
|
|
|
|
from gns3server.controller import Controller
|
|
|
|
from gns3server.controller.project import Project
|
|
|
|
from gns3server.controller.snapshot import Snapshot
|
|
|
|
|
|
|
|
pytestmark = pytest.mark.asyncio
|
|
|
|
|
2016-07-26 11:32:43 +03:00
|
|
|
|
|
|
|
@pytest.fixture
|
2020-12-02 10:09:08 +02:00
|
|
|
async def project(app: FastAPI, client: AsyncClient, controller: Controller) -> Project:
|
2020-06-16 07:29:03 +03:00
|
|
|
|
2016-07-26 11:32:43 +03:00
|
|
|
u = str(uuid.uuid4())
|
2020-06-16 07:29:03 +03:00
|
|
|
params = {"name": "test", "project_id": u}
|
2020-12-02 10:09:08 +02:00
|
|
|
await client.post(app.url_path_for("create_project"), json=params)
|
2016-07-26 11:32:43 +03:00
|
|
|
project = controller.get_project(u)
|
|
|
|
return project
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2020-12-02 10:09:08 +02:00
|
|
|
async def snapshot(project: Project):
|
2020-06-16 07:29:03 +03:00
|
|
|
|
|
|
|
snapshot = await project.snapshot("test")
|
2016-07-26 11:32:43 +03:00
|
|
|
return snapshot
|
|
|
|
|
|
|
|
|
2020-12-02 10:09:08 +02:00
|
|
|
async def test_list_snapshots(app: FastAPI, client: AsyncClient, project: Project, snapshot: Snapshot) -> None:
|
2020-06-16 07:29:03 +03:00
|
|
|
|
|
|
|
assert snapshot.name == "test"
|
2020-12-02 10:09:08 +02:00
|
|
|
response = await client.get(app.url_path_for("get_snapshots", project_id=project.id))
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
assert len(response.json()) == 1
|
2016-07-26 11:32:43 +03:00
|
|
|
|
|
|
|
|
2020-12-02 10:09:08 +02:00
|
|
|
async def test_delete_snapshot(app: FastAPI, client: AsyncClient, project: Project, snapshot: Snapshot) -> None:
|
2020-06-16 07:29:03 +03:00
|
|
|
|
2020-12-02 10:09:08 +02:00
|
|
|
response = await client.delete(app.url_path_for("delete_snapshot", project_id=project.id, snapshot_id=snapshot.id))
|
|
|
|
assert response.status_code == status.HTTP_204_NO_CONTENT
|
2016-07-26 11:32:43 +03:00
|
|
|
assert not os.path.exists(snapshot.path)
|
|
|
|
|
|
|
|
|
2020-12-02 10:09:08 +02:00
|
|
|
async def test_restore_snapshot(app: FastAPI, client: AsyncClient, project: Project, snapshot: Snapshot) -> None:
|
2020-06-16 07:29:03 +03:00
|
|
|
|
2020-12-02 10:09:08 +02:00
|
|
|
response = await client.post(app.url_path_for("restore_snapshot", project_id=project.id, snapshot_id=snapshot.id))
|
|
|
|
assert response.status_code == status.HTTP_201_CREATED
|
|
|
|
assert response.json()["name"] == project.name
|
2016-07-26 11:32:43 +03:00
|
|
|
|
|
|
|
|
2020-12-02 10:09:08 +02:00
|
|
|
async def test_create_snapshot(app: FastAPI, client: AsyncClient, project: Project) -> None:
|
2020-06-16 07:29:03 +03:00
|
|
|
|
2020-12-02 10:09:08 +02:00
|
|
|
response = await client.post(app.url_path_for("create_snapshot", project_id=project.id), json={"name": "snap1"})
|
|
|
|
assert response.status_code == status.HTTP_201_CREATED
|
2016-07-26 11:32:43 +03:00
|
|
|
assert len(os.listdir(os.path.join(project.path, "snapshots"))) == 1
|