2015-01-24 22:32:58 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2015 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/>.
|
|
|
|
|
2015-02-23 16:21:39 +00:00
|
|
|
|
|
|
|
import aiohttp
|
2015-07-21 12:20:58 +00:00
|
|
|
import asyncio
|
2015-02-23 16:21:39 +00:00
|
|
|
import os
|
2015-07-21 12:20:58 +00:00
|
|
|
import tarfile
|
2015-02-23 16:21:39 +00:00
|
|
|
from unittest.mock import patch
|
2015-07-21 12:20:58 +00:00
|
|
|
|
|
|
|
|
2015-06-11 07:18:02 +00:00
|
|
|
from gns3server.config import Config
|
2015-01-24 22:32:58 +00:00
|
|
|
|
2015-07-21 12:20:58 +00:00
|
|
|
|
2015-02-23 16:21:39 +00:00
|
|
|
def test_index_upload(server):
|
2015-02-23 10:27:07 +00:00
|
|
|
response = server.get('/upload', api_version=None)
|
2015-01-24 22:32:58 +00:00
|
|
|
assert response.status == 200
|
2015-02-23 10:27:07 +00:00
|
|
|
html = response.html
|
|
|
|
assert "GNS3 Server" in html
|
|
|
|
assert "Select & Upload" in html
|
2015-02-23 16:21:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_upload(server, tmpdir):
|
|
|
|
|
|
|
|
with open(str(tmpdir / "test"), "w+") as f:
|
|
|
|
f.write("TEST")
|
|
|
|
body = aiohttp.FormData()
|
2015-03-07 13:27:09 +00:00
|
|
|
body.add_field("type", "QEMU")
|
2015-02-23 16:21:39 +00:00
|
|
|
body.add_field("file", open(str(tmpdir / "test"), "rb"), content_type="application/iou", filename="test2")
|
|
|
|
|
2015-06-11 07:18:02 +00:00
|
|
|
Config.instance().set("Server", "images_path", str(tmpdir))
|
|
|
|
response = server.post('/upload', api_version=None, body=body, raw=True)
|
2015-02-23 16:21:39 +00:00
|
|
|
|
2015-03-07 13:27:09 +00:00
|
|
|
with open(str(tmpdir / "QEMU" / "test2")) as f:
|
2015-02-23 16:21:39 +00:00
|
|
|
assert f.read() == "TEST"
|
|
|
|
|
|
|
|
assert "test2" in response.body.decode("utf-8")
|
2015-07-21 12:20:58 +00:00
|
|
|
|
|
|
|
|
2015-07-21 14:14:03 +00:00
|
|
|
def test_upload_images_backup(server, tmpdir):
|
|
|
|
Config.instance().set("Server", "images_path", str(tmpdir / 'images'))
|
|
|
|
os.makedirs(str(tmpdir / 'images' / 'IOU'))
|
|
|
|
# An old IOU image that we need to replace
|
|
|
|
with open(str(tmpdir / 'images' / 'IOU' / 'b.img'), 'w+') as f:
|
|
|
|
f.write('bad')
|
|
|
|
|
|
|
|
os.makedirs(str(tmpdir / 'old' / 'QEMU'))
|
|
|
|
with open(str(tmpdir / 'old' / 'QEMU' / 'a.img'), 'w+') as f:
|
|
|
|
f.write('hello')
|
|
|
|
os.makedirs(str(tmpdir / 'old' / 'IOU'))
|
|
|
|
with open(str(tmpdir / 'old' / 'IOU' / 'b.img'), 'w+') as f:
|
|
|
|
f.write('world')
|
|
|
|
|
|
|
|
os.chdir(str(tmpdir / 'old'))
|
|
|
|
with tarfile.open(str(tmpdir / 'test.tar'), 'w') as tar:
|
|
|
|
tar.add('.', recursive=True)
|
|
|
|
|
|
|
|
body = aiohttp.FormData()
|
|
|
|
body.add_field('type', 'IMAGES')
|
|
|
|
body.add_field('file', open(str(tmpdir / 'test.tar'), 'rb'), content_type='application/x-gtar', filename='test.tar')
|
|
|
|
response = server.post('/upload', api_version=None, body=body, raw=True)
|
|
|
|
assert response.status == 200
|
|
|
|
|
|
|
|
with open(str(tmpdir / 'images' / 'QEMU' / 'a.img')) as f:
|
|
|
|
assert f.read() == 'hello'
|
|
|
|
with open(str(tmpdir / 'images' / 'IOU' / 'b.img')) as f:
|
|
|
|
assert f.read() == 'world'
|
|
|
|
|
|
|
|
assert 'a.img' in response.body.decode('utf-8')
|
|
|
|
assert 'b.img' in response.body.decode('utf-8')
|
|
|
|
assert not os.path.exists(str(tmpdir / 'images' / 'archive.tar'))
|
|
|
|
|
|
|
|
|
|
|
|
def test_upload_projects_backup(server, tmpdir):
|
|
|
|
Config.instance().set("Server", "projects_path", str(tmpdir / 'projects'))
|
|
|
|
os.makedirs(str(tmpdir / 'projects' / 'b'))
|
|
|
|
# An old b image that we need to replace
|
|
|
|
with open(str(tmpdir / 'projects' / 'b' / 'b.img'), 'w+') as f:
|
|
|
|
f.write('bad')
|
|
|
|
|
|
|
|
os.makedirs(str(tmpdir / 'old' / 'a'))
|
|
|
|
with open(str(tmpdir / 'old' / 'a' / 'a.img'), 'w+') as f:
|
|
|
|
f.write('hello')
|
|
|
|
os.makedirs(str(tmpdir / 'old' / 'b'))
|
|
|
|
with open(str(tmpdir / 'old' / 'b' / 'b.img'), 'w+') as f:
|
|
|
|
f.write('world')
|
|
|
|
|
|
|
|
os.chdir(str(tmpdir / 'old'))
|
|
|
|
with tarfile.open(str(tmpdir / 'test.tar'), 'w') as tar:
|
|
|
|
tar.add('.', recursive=True)
|
|
|
|
|
|
|
|
body = aiohttp.FormData()
|
|
|
|
body.add_field('type', 'PROJECTS')
|
|
|
|
body.add_field('file', open(str(tmpdir / 'test.tar'), 'rb'), content_type='application/x-gtar', filename='test.tar')
|
|
|
|
response = server.post('/upload', api_version=None, body=body, raw=True)
|
|
|
|
assert response.status == 200
|
|
|
|
|
|
|
|
with open(str(tmpdir / 'projects' / 'a' / 'a.img')) as f:
|
|
|
|
assert f.read() == 'hello'
|
|
|
|
with open(str(tmpdir / 'projects' / 'b' / 'b.img')) as f:
|
|
|
|
assert f.read() == 'world'
|
|
|
|
|
|
|
|
assert 'a.img' not in response.body.decode('utf-8')
|
|
|
|
assert 'b.img' not in response.body.decode('utf-8')
|
|
|
|
assert not os.path.exists(str(tmpdir / 'projects' / 'archive.tar'))
|
|
|
|
|
|
|
|
|
2015-07-21 12:20:58 +00:00
|
|
|
def test_backup_images(server, tmpdir, loop):
|
|
|
|
Config.instance().set('Server', 'images_path', str(tmpdir))
|
|
|
|
|
|
|
|
os.makedirs(str(tmpdir / 'QEMU'))
|
|
|
|
with open(str(tmpdir / 'QEMU' / 'a.img'), 'w+') as f:
|
|
|
|
f.write('hello')
|
|
|
|
with open(str(tmpdir / 'QEMU' / 'b.img'), 'w+') as f:
|
|
|
|
f.write('world')
|
|
|
|
|
2015-07-21 14:14:03 +00:00
|
|
|
response = server.get('/backup/images.tar', api_version=None, raw=True)
|
2015-07-21 12:20:58 +00:00
|
|
|
assert response.status == 200
|
|
|
|
assert response.headers['CONTENT-TYPE'] == 'application/x-gtar'
|
|
|
|
|
|
|
|
with open(str(tmpdir / 'images.tar'), 'wb+') as f:
|
|
|
|
print(len(response.body))
|
|
|
|
f.write(response.body)
|
|
|
|
|
|
|
|
tar = tarfile.open(str(tmpdir / 'images.tar'), 'r')
|
|
|
|
os.makedirs(str(tmpdir / 'extract'))
|
|
|
|
os.chdir(str(tmpdir / 'extract'))
|
|
|
|
# Extract to current working directory
|
|
|
|
tar.extractall()
|
|
|
|
tar.close()
|
|
|
|
|
|
|
|
assert os.path.exists(os.path.join('QEMU', 'a.img'))
|
2015-11-03 11:34:22 +00:00
|
|
|
assert open(os.path.join('QEMU', 'a.img')).read() == 'hello'
|
2015-07-21 12:20:58 +00:00
|
|
|
|
|
|
|
assert os.path.exists(os.path.join('QEMU', 'b.img'))
|
2015-11-03 11:34:22 +00:00
|
|
|
assert open(os.path.join('QEMU', 'b.img')).read() == 'world'
|
2015-07-21 12:20:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_backup_projects(server, tmpdir, loop):
|
|
|
|
Config.instance().set('Server', 'projects_path', str(tmpdir))
|
|
|
|
|
|
|
|
os.makedirs(str(tmpdir / 'a'))
|
|
|
|
with open(str(tmpdir / 'a' / 'a.gns3'), 'w+') as f:
|
|
|
|
f.write('hello')
|
|
|
|
os.makedirs(str(tmpdir / 'b'))
|
|
|
|
with open(str(tmpdir / 'b' / 'b.gns3'), 'w+') as f:
|
|
|
|
f.write('world')
|
|
|
|
|
2015-07-21 14:14:03 +00:00
|
|
|
response = server.get('/backup/projects.tar', api_version=None, raw=True)
|
2015-07-21 12:20:58 +00:00
|
|
|
assert response.status == 200
|
|
|
|
assert response.headers['CONTENT-TYPE'] == 'application/x-gtar'
|
|
|
|
|
|
|
|
with open(str(tmpdir / 'projects.tar'), 'wb+') as f:
|
|
|
|
print(len(response.body))
|
|
|
|
f.write(response.body)
|
|
|
|
|
|
|
|
tar = tarfile.open(str(tmpdir / 'projects.tar'), 'r')
|
|
|
|
os.makedirs(str(tmpdir / 'extract'))
|
|
|
|
os.chdir(str(tmpdir / 'extract'))
|
|
|
|
# Extract to current working directory
|
|
|
|
tar.extractall()
|
|
|
|
tar.close()
|
|
|
|
|
|
|
|
assert os.path.exists(os.path.join('a', 'a.gns3'))
|
2015-11-03 11:34:22 +00:00
|
|
|
assert open(os.path.join('a', 'a.gns3')).read() == 'hello'
|
2015-07-21 12:20:58 +00:00
|
|
|
|
|
|
|
assert os.path.exists(os.path.join('b', 'b.gns3'))
|
2015-11-03 11:34:22 +00:00
|
|
|
assert open(os.path.join('b', 'b.gns3')).read() == 'world'
|