2016-07-26 11:32:43 +03: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 os
|
|
|
|
import pytest
|
2016-08-15 14:30:02 +03:00
|
|
|
from unittest.mock import patch, MagicMock
|
2016-07-26 11:32:43 +03:00
|
|
|
|
|
|
|
from gns3server.controller.project import Project
|
|
|
|
from gns3server.controller.snapshot import Snapshot
|
|
|
|
|
|
|
|
from tests.utils import AsyncioMagicMock
|
|
|
|
|
|
|
|
|
2020-06-16 07:29:03 +03:00
|
|
|
# @pytest.fixture
|
|
|
|
# def project(controller):
|
|
|
|
# project = Project(controller=controller, name="Test")
|
|
|
|
# controller._projects[project.id] = project
|
|
|
|
# return project
|
2016-07-26 11:32:43 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_snapshot_name(project):
|
|
|
|
"""
|
|
|
|
Test create a snapshot object with a name
|
|
|
|
"""
|
2020-06-16 07:29:03 +03:00
|
|
|
|
2016-07-26 11:32:43 +03:00
|
|
|
snapshot = Snapshot(project, name="test1")
|
|
|
|
assert snapshot.name == "test1"
|
|
|
|
assert snapshot._created_at > 0
|
|
|
|
assert snapshot.path.startswith(os.path.join(project.path, "snapshots", "test1_"))
|
|
|
|
assert snapshot.path.endswith(".gns3project")
|
|
|
|
|
|
|
|
# Check if UTC conversion doesn't corrupt the path
|
|
|
|
snap2 = Snapshot(project, filename=os.path.basename(snapshot.path))
|
|
|
|
assert snap2.path == snapshot.path
|
|
|
|
|
|
|
|
|
|
|
|
def test_snapshot_filename(project):
|
|
|
|
"""
|
|
|
|
Test create a snapshot object with a filename
|
|
|
|
"""
|
2020-06-16 07:29:03 +03:00
|
|
|
|
2016-07-26 11:32:43 +03:00
|
|
|
snapshot = Snapshot(project, filename="test1_260716_100439.gns3project")
|
|
|
|
assert snapshot.name == "test1"
|
|
|
|
assert snapshot._created_at == 1469527479.0
|
|
|
|
assert snapshot.path == os.path.join(project.path, "snapshots", "test1_260716_100439.gns3project")
|
|
|
|
|
|
|
|
|
|
|
|
def test_json(project):
|
2020-06-16 07:29:03 +03:00
|
|
|
|
2024-07-05 13:04:53 +03:00
|
|
|
snapshot = Snapshot(project, filename="snapshot_test_260716_100439.gns3project")
|
2016-07-26 11:32:43 +03:00
|
|
|
assert snapshot.__json__() == {
|
|
|
|
"snapshot_id": snapshot._id,
|
2024-07-05 13:04:53 +03:00
|
|
|
"name": "snapshot_test",
|
2016-07-26 11:32:43 +03:00
|
|
|
"project_id": project.id,
|
|
|
|
"created_at": 1469527479
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-05 13:04:53 +03:00
|
|
|
def test_invalid_snapshot_filename(project):
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
Snapshot(project, filename="snapshot_test_invalid_file.gns3project")
|
|
|
|
|
|
|
|
|
2020-06-16 07:29:03 +03:00
|
|
|
async def test_restore(project, controller):
|
|
|
|
|
2016-07-26 11:32:43 +03:00
|
|
|
compute = AsyncioMagicMock()
|
|
|
|
compute.id = "local"
|
|
|
|
controller._computes["local"] = compute
|
|
|
|
response = AsyncioMagicMock()
|
|
|
|
response.json = {"console": 2048}
|
|
|
|
compute.post = AsyncioMagicMock(return_value=response)
|
|
|
|
|
2020-06-16 07:29:03 +03:00
|
|
|
await project.add_node(compute, "test1", None, node_type="vpcs", properties={"startup_config": "test.cfg"})
|
|
|
|
snapshot = await project.snapshot(name="test")
|
2016-07-26 11:32:43 +03:00
|
|
|
|
|
|
|
# We add a node after the snapshots
|
2020-06-16 07:29:03 +03:00
|
|
|
await project.add_node(compute, "test2", None, node_type="vpcs", properties={"startup_config": "test.cfg"})
|
2016-07-26 11:32:43 +03:00
|
|
|
|
|
|
|
# project-files should be reset when reimporting
|
|
|
|
test_file = os.path.join(project.path, "project-files", "test.txt")
|
|
|
|
os.makedirs(os.path.join(project.path, "project-files"))
|
|
|
|
open(test_file, "a+").close()
|
|
|
|
|
|
|
|
assert os.path.exists(test_file)
|
|
|
|
assert len(project.nodes) == 2
|
|
|
|
|
2016-08-15 14:30:02 +03:00
|
|
|
controller._notification = MagicMock()
|
2016-07-26 11:32:43 +03:00
|
|
|
with patch("gns3server.config.Config.get_section_config", return_value={"local": True}):
|
2020-06-16 07:29:03 +03:00
|
|
|
await snapshot.restore()
|
2016-07-26 11:32:43 +03:00
|
|
|
|
2018-08-16 17:31:56 +03:00
|
|
|
assert "snapshot.restored" in [c[0][0] for c in controller.notification.project_emit.call_args_list]
|
2016-08-15 14:30:02 +03:00
|
|
|
# project.closed notification should not be send when restoring snapshots
|
2018-08-16 17:31:56 +03:00
|
|
|
assert "project.closed" not in [c[0][0] for c in controller.notification.project_emit.call_args_list]
|
2016-08-15 14:30:02 +03:00
|
|
|
|
2016-07-26 11:32:43 +03:00
|
|
|
project = controller.get_project(project.id)
|
|
|
|
assert not os.path.exists(test_file)
|
|
|
|
assert len(project.nodes) == 1
|